hp.com!davidm | 186cbb2 | 2004-09-15 11:37:04 +0000 | [diff] [blame] | 1 | /* libunwind - a platform-independent unwind library |
| 2 | Copyright (C) 2001-2004 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 | #ifdef HAVE_CONFIG_H |
| 25 | # include "config.h" |
| 26 | #endif |
| 27 | |
| 28 | #include <errno.h> |
| 29 | #if HAVE_EXECINFO_H |
| 30 | # include <execinfo.h> |
| 31 | #else |
| 32 | extern int backtrace (void **, int); |
| 33 | #endif |
| 34 | #include <signal.h> |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | #include <unistd.h> |
| 39 | #include <libunwind.h> |
| 40 | |
hp.com!davidm | 186cbb2 | 2004-09-15 11:37:04 +0000 | [diff] [blame] | 41 | #define panic(args...) \ |
| 42 | { fprintf (stderr, args); exit (-1); } |
| 43 | |
| 44 | #ifndef HAVE_SIGHANDLER_T |
| 45 | typedef RETSIGTYPE (*sighandler_t) (int); |
| 46 | #endif |
| 47 | |
| 48 | int verbose; |
| 49 | int num_errors; |
| 50 | |
Arun Sharma | 24112f6 | 2010-03-10 21:13:26 -0800 | [diff] [blame] | 51 | /* These variables are global because they |
| 52 | * cause the signal stack to overflow */ |
| 53 | char buf[512], name[256]; |
| 54 | unw_cursor_t cursor; |
| 55 | ucontext_t uc; |
| 56 | |
hp.com!davidm | 186cbb2 | 2004-09-15 11:37:04 +0000 | [diff] [blame] | 57 | static void |
| 58 | do_backtrace (void) |
| 59 | { |
hp.com!davidm | 186cbb2 | 2004-09-15 11:37:04 +0000 | [diff] [blame] | 60 | unw_word_t ip, sp, off; |
hp.com!davidm | 186cbb2 | 2004-09-15 11:37:04 +0000 | [diff] [blame] | 61 | unw_proc_info_t pi; |
hp.com!davidm | 186cbb2 | 2004-09-15 11:37:04 +0000 | [diff] [blame] | 62 | int ret; |
| 63 | |
mostang.com!davidm | 1ee6b0a | 2005-05-03 09:13:17 +0000 | [diff] [blame] | 64 | if (verbose) |
| 65 | printf ("\texplicit backtrace:\n"); |
| 66 | |
hp.com!davidm | 186cbb2 | 2004-09-15 11:37:04 +0000 | [diff] [blame] | 67 | unw_getcontext (&uc); |
| 68 | if (unw_init_local (&cursor, &uc) < 0) |
| 69 | panic ("unw_init_local failed!\n"); |
| 70 | |
| 71 | do |
| 72 | { |
| 73 | unw_get_reg (&cursor, UNW_REG_IP, &ip); |
| 74 | unw_get_reg (&cursor, UNW_REG_SP, &sp); |
| 75 | buf[0] = '\0'; |
| 76 | if (unw_get_proc_name (&cursor, name, sizeof (name), &off) == 0) |
| 77 | { |
| 78 | if (off) |
| 79 | snprintf (buf, sizeof (buf), "<%s+0x%lx>", name, (long) off); |
| 80 | else |
| 81 | snprintf (buf, sizeof (buf), "<%s>", name); |
| 82 | } |
| 83 | if (verbose) |
| 84 | { |
| 85 | printf ("%016lx %-32s (sp=%016lx)\n", (long) ip, buf, (long) sp); |
| 86 | |
| 87 | unw_get_proc_info (&cursor, &pi); |
| 88 | printf ("\tproc=%lx-%lx\n\thandler=%lx lsda=%lx gp=%lx", |
| 89 | (long) pi.start_ip, (long) pi.end_ip, |
| 90 | (long) pi.handler, (long) pi.lsda, (long) pi.gp); |
| 91 | |
| 92 | #if UNW_TARGET_IA64 |
| 93 | { |
| 94 | unw_word_t bsp; |
| 95 | |
| 96 | unw_get_reg (&cursor, UNW_IA64_BSP, &bsp); |
| 97 | printf (" bsp=%lx", bsp); |
| 98 | } |
| 99 | #endif |
| 100 | printf ("\n"); |
| 101 | } |
| 102 | |
| 103 | ret = unw_step (&cursor); |
| 104 | if (ret < 0) |
| 105 | { |
| 106 | unw_get_reg (&cursor, UNW_REG_IP, &ip); |
| 107 | printf ("FAILURE: unw_step() returned %d for ip=%lx\n", |
| 108 | ret, (long) ip); |
| 109 | ++num_errors; |
| 110 | } |
| 111 | } |
| 112 | while (ret > 0); |
mostang.com!davidm | 1ee6b0a | 2005-05-03 09:13:17 +0000 | [diff] [blame] | 113 | |
| 114 | { |
| 115 | void *buffer[20]; |
| 116 | int i, n; |
| 117 | |
| 118 | if (verbose) |
| 119 | printf ("\n\tvia backtrace():\n"); |
| 120 | n = backtrace (buffer, 20); |
| 121 | if (verbose) |
| 122 | for (i = 0; i < n; ++i) |
| 123 | printf ("[%d] ip=%p\n", i, buffer[i]); |
| 124 | } |
hp.com!davidm | 186cbb2 | 2004-09-15 11:37:04 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void |
| 128 | foo (long val) |
| 129 | { |
hp.com!davidm | 186cbb2 | 2004-09-15 11:37:04 +0000 | [diff] [blame] | 130 | do_backtrace (); |
hp.com!davidm | 186cbb2 | 2004-09-15 11:37:04 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void |
| 134 | bar (long v) |
| 135 | { |
| 136 | extern long f (long); |
| 137 | int arr[v]; |
| 138 | |
| 139 | /* This is a vain attempt to use up lots of registers to force |
| 140 | the frame-chain info to be saved on the memory stack on ia64. |
| 141 | It happens to work with gcc v3.3.4 and gcc v3.4.1 but perhaps |
| 142 | not with any other compiler. */ |
| 143 | foo (f (arr[0]) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) |
| 144 | + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) |
| 145 | + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) |
| 146 | + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) |
| 147 | + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) |
| 148 | + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) |
| 149 | + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) |
| 150 | + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) |
| 151 | + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) |
| 152 | + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) |
| 153 | + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) |
| 154 | + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) |
| 155 | + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) |
| 156 | + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) |
| 157 | + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) |
| 158 | + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) |
| 159 | + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + (f (v) + f (v)) |
| 160 | )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) |
| 161 | ))))))))))))))))))))))))))))))))))))))))))))))))))))))); |
| 162 | } |
| 163 | |
| 164 | void |
| 165 | sighandler (int signal, void *siginfo, void *context) |
| 166 | { |
| 167 | ucontext_t *uc = context; |
| 168 | int sp; |
| 169 | |
| 170 | if (verbose) |
| 171 | { |
| 172 | printf ("sighandler: got signal %d, sp=%p", signal, &sp); |
| 173 | #if UNW_TARGET_IA64 |
| 174 | # if defined(__linux__) |
| 175 | printf (" @ %lx", uc->uc_mcontext.sc_ip); |
| 176 | # else |
| 177 | { |
| 178 | uint16_t reason; |
| 179 | uint64_t ip; |
| 180 | |
| 181 | __uc_get_reason (uc, &reason); |
| 182 | __uc_get_ip (uc, &ip); |
| 183 | printf (" @ %lx (reason=%d)", ip, reason); |
| 184 | } |
| 185 | # endif |
| 186 | #elif UNW_TARGET_X86 |
Konstantin Belousov | 3b026a7 | 2010-03-10 23:51:09 +0200 | [diff] [blame] | 187 | #if defined __linux__ |
hp.com!davidm | 186cbb2 | 2004-09-15 11:37:04 +0000 | [diff] [blame] | 188 | printf (" @ %lx", (unsigned long) uc->uc_mcontext.gregs[REG_EIP]); |
Konstantin Belousov | 3b026a7 | 2010-03-10 23:51:09 +0200 | [diff] [blame] | 189 | #elif defined __FreeBSD__ |
| 190 | printf (" @ %lx", (unsigned long) uc->uc_mcontext.mc_eip); |
hp.com!davidm | 186cbb2 | 2004-09-15 11:37:04 +0000 | [diff] [blame] | 191 | #endif |
Arun Sharma | 24112f6 | 2010-03-10 21:13:26 -0800 | [diff] [blame] | 192 | #elif UNW_TARGET_X86_64 |
Konstantin Belousov | 9bb9c97 | 2010-03-31 16:01:38 +0300 | [diff] [blame] | 193 | #if defined __linux__ |
Arun Sharma | 24112f6 | 2010-03-10 21:13:26 -0800 | [diff] [blame] | 194 | printf (" @ %lx", (unsigned long) uc->uc_mcontext.gregs[REG_RIP]); |
Konstantin Belousov | 9bb9c97 | 2010-03-31 16:01:38 +0300 | [diff] [blame] | 195 | #elif defined __FreeBSD__ |
| 196 | printf (" @ %lx", (unsigned long) uc->uc_mcontext.mc_rip); |
| 197 | #endif |
Konstantin Belousov | d737709 | 2010-03-11 00:02:11 +0200 | [diff] [blame] | 198 | #endif |
hp.com!davidm | 186cbb2 | 2004-09-15 11:37:04 +0000 | [diff] [blame] | 199 | printf ("\n"); |
| 200 | } |
| 201 | do_backtrace(); |
| 202 | } |
| 203 | |
| 204 | int |
| 205 | main (int argc, char **argv) |
| 206 | { |
| 207 | struct sigaction act; |
| 208 | stack_t stk; |
| 209 | |
| 210 | verbose = (argc > 1); |
| 211 | |
| 212 | if (verbose) |
| 213 | printf ("Normal backtrace:\n"); |
| 214 | |
| 215 | bar (1); |
| 216 | |
| 217 | memset (&act, 0, sizeof (act)); |
| 218 | act.sa_handler = (void (*)(int)) sighandler; |
| 219 | act.sa_flags = SA_SIGINFO; |
| 220 | if (sigaction (SIGTERM, &act, NULL) < 0) |
| 221 | panic ("sigaction: %s\n", strerror (errno)); |
| 222 | |
| 223 | if (verbose) |
| 224 | printf ("\nBacktrace across signal handler:\n"); |
| 225 | kill (getpid (), SIGTERM); |
| 226 | |
| 227 | if (verbose) |
| 228 | printf ("\nBacktrace across signal handler on alternate stack:\n"); |
Paul Pluzhnikov | b56375e | 2009-10-07 12:51:03 -0700 | [diff] [blame] | 229 | stk.ss_sp = malloc (SIGSTKSZ); |
hp.com!davidm | 186cbb2 | 2004-09-15 11:37:04 +0000 | [diff] [blame] | 230 | if (!stk.ss_sp) |
| 231 | panic ("failed to allocate SIGSTKSZ (%u) bytes\n", SIGSTKSZ); |
Paul Pluzhnikov | b56375e | 2009-10-07 12:51:03 -0700 | [diff] [blame] | 232 | stk.ss_size = SIGSTKSZ; |
hp.com!davidm | 186cbb2 | 2004-09-15 11:37:04 +0000 | [diff] [blame] | 233 | stk.ss_flags = 0; |
| 234 | if (sigaltstack (&stk, NULL) < 0) |
| 235 | panic ("sigaltstack: %s\n", strerror (errno)); |
| 236 | |
| 237 | memset (&act, 0, sizeof (act)); |
| 238 | act.sa_handler = (void (*)(int)) sighandler; |
| 239 | act.sa_flags = SA_ONSTACK | SA_SIGINFO; |
| 240 | if (sigaction (SIGTERM, &act, NULL) < 0) |
| 241 | panic ("sigaction: %s\n", strerror (errno)); |
| 242 | kill (getpid (), SIGTERM); |
| 243 | |
| 244 | if (num_errors > 0) |
| 245 | { |
| 246 | fprintf (stderr, "FAILURE: detected %d errors\n", num_errors); |
| 247 | exit (-1); |
| 248 | } |
| 249 | if (verbose) |
| 250 | printf ("SUCCESS.\n"); |
| 251 | return 0; |
| 252 | } |