hp.com!davidm | f77ee14 | 2003-02-22 03:08:22 +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 illustrates the basics of using the unwind interface for |
| 25 | exception handling. */ |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <unistd.h> |
| 30 | |
| 31 | #include <libunwind.h> |
| 32 | |
| 33 | #define panic(args...) \ |
| 34 | { ++nerrors; fprintf (stderr, args); } |
| 35 | |
| 36 | int nerrors = 0; |
| 37 | int verbose = 0; |
| 38 | |
| 39 | static void b (void *); |
| 40 | |
| 41 | static void |
| 42 | raise_exception (void *addr) |
| 43 | { |
| 44 | unw_cursor_t cursor; |
| 45 | unw_word_t ip; |
| 46 | unw_context_t uc; |
| 47 | |
| 48 | unw_getcontext (&uc); |
| 49 | if (unw_init_local (&cursor, &uc) < 0) |
| 50 | { |
| 51 | panic ("unw_init_local() failed!\n"); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | /* unwind to frame b(): */ |
| 56 | if (unw_step (&cursor) < 0) |
| 57 | { |
| 58 | panic ("unw_step() failed!\n"); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | /* unwind to frame a(): */ |
| 63 | if (unw_step (&cursor) < 0) |
| 64 | { |
| 65 | panic ("unw_step() failed!\n"); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | unw_get_reg (&cursor, UNW_REG_IP, &ip); |
| 70 | if (verbose) |
| 71 | printf ("old ip = %lx, new ip = %p\n", (long) ip, addr); |
| 72 | |
| 73 | if (unw_set_reg (&cursor, UNW_REG_IP, (unw_word_t) addr) < 0) |
| 74 | { |
| 75 | panic ("unw_set_reg() failed!\n"); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | unw_resume (&cursor); /* transfer control to exception handler */ |
| 80 | } |
| 81 | |
| 82 | #if !UNW_TARGET_IA64 |
| 83 | |
| 84 | void * |
| 85 | __builtin_ia64_bsp (void) |
| 86 | { |
| 87 | return NULL; |
| 88 | } |
| 89 | |
| 90 | #endif |
| 91 | |
| 92 | static int |
| 93 | a (void) |
| 94 | { |
| 95 | long stack; |
| 96 | |
hp.com!davidm | 06e5723 | 2003-11-27 06:52:54 +0000 | [diff] [blame] | 97 | #if defined(__GNUC__) && !defined(__INTEL_COMPILER) |
mostang.com!davidm | 757746a | 2003-03-28 07:43:22 +0000 | [diff] [blame] | 98 | void *label; |
| 99 | |
hp.com!davidm | f77ee14 | 2003-02-22 03:08:22 +0000 | [diff] [blame] | 100 | if (verbose) |
| 101 | printf ("a: sp=%p bsp=%p\n", &stack, __builtin_ia64_bsp ()); |
mostang.com!davidm | 757746a | 2003-03-28 07:43:22 +0000 | [diff] [blame] | 102 | #ifdef UNW_TARGET_IA64 |
| 103 | asm ("movl %0=Label" : "=r"(label)); |
| 104 | #else |
| 105 | label = &&handler; |
| 106 | #endif |
| 107 | b (label); |
hp.com!davidm | f77ee14 | 2003-02-22 03:08:22 +0000 | [diff] [blame] | 108 | panic ("FAILURE: unexpected return from func()!\n"); |
| 109 | |
| 110 | #if UNW_TARGET_IA64 |
mostang.com!davidm | 757746a | 2003-03-28 07:43:22 +0000 | [diff] [blame] | 111 | asm volatile ("Label:"); /* force a new bundle */ |
| 112 | #else |
| 113 | handler: |
hp.com!davidm | f77ee14 | 2003-02-22 03:08:22 +0000 | [diff] [blame] | 114 | #endif |
hp.com!davidm | f77ee14 | 2003-02-22 03:08:22 +0000 | [diff] [blame] | 115 | if (verbose) |
| 116 | { |
| 117 | printf ("exception handler: here we go (sp=%p, bsp=%p)...\n", |
| 118 | &stack, __builtin_ia64_bsp ()); |
| 119 | /* This call works around a bug in gcc (up-to pre3.4) which |
| 120 | causes invalid assembly code to be generated when |
| 121 | __builtin_ia64_bsp() gets predicated. */ |
| 122 | getpid (); |
| 123 | } |
mostang.com!davidm | 797322b | 2003-03-27 04:29:07 +0000 | [diff] [blame] | 124 | #else |
| 125 | if (verbose) |
| 126 | printf ("a: this test only works with GNU C compiler.\n"); |
| 127 | #endif |
hp.com!davidm | f77ee14 | 2003-02-22 03:08:22 +0000 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static void |
| 132 | b (void *addr) |
| 133 | { |
| 134 | if (verbose) |
| 135 | printf ("b() calling raise_exception()\n"); |
| 136 | raise_exception (addr); |
| 137 | panic ("FAILURE: b() returned from raise_exception()!!\n"); |
| 138 | } |
| 139 | |
| 140 | int |
| 141 | main (int argc, char **argv) |
| 142 | { |
| 143 | if (argc > 1) |
| 144 | ++verbose; |
| 145 | |
| 146 | if (a () != 0 || nerrors > 0) |
| 147 | { |
| 148 | fprintf (stderr, "FAILURE: test failed; try again?\n"); |
| 149 | exit (-1); |
| 150 | } |
| 151 | |
| 152 | if (verbose) |
| 153 | printf ("SUCCESS!\n"); |
| 154 | return 0; |
| 155 | } |