mostang.com!davidm | e5b4f8a | 2003-01-23 10:04:09 +0000 | [diff] [blame] | 1 | /* libunwind - a platform-independent unwind library |
| 2 | Copyright (C) 2001-2003 Hewlett-Packard Co |
| 3 | Contributed by David Mosberger-Tang <davidm@hpl.hp.com> |
| 4 | |
| 5 | Permission is hereby granted, free of charge, to any person obtaining |
| 6 | a copy of this software and associated documentation files (the |
| 7 | "Software"), to deal in the Software without restriction, including |
| 8 | without limitation the rights to use, copy, modify, merge, publish, |
| 9 | distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | permit persons to whom the Software is furnished to do so, subject to |
| 11 | the following conditions: |
| 12 | |
| 13 | The above copyright notice and this permission notice shall be |
| 14 | included in all copies or substantial portions of the Software. |
| 15 | |
| 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 23 | |
| 24 | /* This shows how to use the unwind interface to modify any ancestor |
| 25 | frame while still returning to the parent frame. */ |
| 26 | |
| 27 | #include <signal.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | |
| 31 | #include <libunwind-ia64.h> |
| 32 | |
| 33 | #define panic(args...) \ |
| 34 | { fprintf (stderr, args); exit (-1); } |
| 35 | |
| 36 | static void |
| 37 | sighandler (int signal) |
| 38 | { |
| 39 | unw_cursor_t cursor, cursor2; |
| 40 | unw_word_t ip; |
| 41 | unw_context_t uc; |
| 42 | |
| 43 | printf ("caught signal %d\n", signal); |
| 44 | |
| 45 | unw_getcontext (&uc); |
| 46 | if (unw_init_local (&cursor, &uc) < 0) |
| 47 | panic ("unw_init() failed!\n"); |
| 48 | |
| 49 | /* get cursor for caller of sighandler: */ |
| 50 | if (unw_step (&cursor) < 0) |
| 51 | panic ("unw_step() failed!\n"); |
| 52 | |
| 53 | cursor2 = cursor; |
| 54 | while (!unw_is_signal_frame (&cursor2)) |
| 55 | if (unw_step (&cursor2) < 0) |
| 56 | panic ("failed to find signal frame!\n"); |
| 57 | |
| 58 | if (unw_step (&cursor2) < 0) |
| 59 | panic ("unw_step() failed!\n"); |
| 60 | |
| 61 | if (unw_get_reg (&cursor2, UNW_REG_IP, &ip) < 0) |
| 62 | panic ("failed to get IP!\n"); |
| 63 | |
| 64 | /* skip faulting instruction (doesn't handle MLX template) */ |
| 65 | ++ip; |
| 66 | if ((ip & 0x3) == 0x3) |
| 67 | ip += 13; |
| 68 | |
| 69 | if (unw_set_reg (&cursor2, UNW_REG_IP, ip) < 0) |
| 70 | panic ("failed to set IP!\n"); |
| 71 | |
| 72 | unw_resume (&cursor); /* update context & return to caller of sighandler() */ |
| 73 | |
| 74 | panic ("unexpected return from unw_resume()!\n"); |
| 75 | } |
| 76 | |
| 77 | static void |
| 78 | doit (volatile char *p) |
| 79 | { |
| 80 | int ch; |
| 81 | |
| 82 | ch = *p; /* trigger SIGSEGV */ |
| 83 | |
| 84 | printf ("doit: finishing execution!\n"); |
| 85 | } |
| 86 | |
| 87 | int |
| 88 | main (int argc, char **argv) |
| 89 | { |
| 90 | signal (SIGSEGV, sighandler); |
| 91 | doit (0); |
| 92 | return 0; |
| 93 | } |