blob: 152e2af3fd6593a3937b3a1251c1ad83e601aa99 [file] [log] [blame]
hp.com!davidm186cbb22004-09-15 11:37:04 +00001/* 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
5Permission is hereby granted, free of charge, to any person obtaining
6a copy of this software and associated documentation files (the
7"Software"), to deal in the Software without restriction, including
8without limitation the rights to use, copy, modify, merge, publish,
9distribute, sublicense, and/or sell copies of the Software, and to
10permit persons to whom the Software is furnished to do so, subject to
11the following conditions:
12
13The above copyright notice and this permission notice shall be
14included in all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22WITH 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!davidm186cbb22004-09-15 11:37:04 +000041#define panic(args...) \
42 { fprintf (stderr, args); exit (-1); }
43
44#ifndef HAVE_SIGHANDLER_T
45typedef RETSIGTYPE (*sighandler_t) (int);
46#endif
47
48int verbose;
49int num_errors;
50
Arun Sharma24112f62010-03-10 21:13:26 -080051/* These variables are global because they
52 * cause the signal stack to overflow */
53char buf[512], name[256];
54unw_cursor_t cursor;
55ucontext_t uc;
56
hp.com!davidm186cbb22004-09-15 11:37:04 +000057static void
58do_backtrace (void)
59{
hp.com!davidm186cbb22004-09-15 11:37:04 +000060 unw_word_t ip, sp, off;
hp.com!davidm186cbb22004-09-15 11:37:04 +000061 unw_proc_info_t pi;
hp.com!davidm186cbb22004-09-15 11:37:04 +000062 int ret;
63
mostang.com!davidm1ee6b0a2005-05-03 09:13:17 +000064 if (verbose)
65 printf ("\texplicit backtrace:\n");
66
hp.com!davidm186cbb22004-09-15 11:37:04 +000067 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!davidm1ee6b0a2005-05-03 09:13:17 +0000113
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!davidm186cbb22004-09-15 11:37:04 +0000125}
126
127void
128foo (long val)
129{
hp.com!davidm186cbb22004-09-15 11:37:04 +0000130 do_backtrace ();
hp.com!davidm186cbb22004-09-15 11:37:04 +0000131}
132
133void
134bar (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
164void
165sighandler (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 Belousov3b026a72010-03-10 23:51:09 +0200187#if defined __linux__
hp.com!davidm186cbb22004-09-15 11:37:04 +0000188 printf (" @ %lx", (unsigned long) uc->uc_mcontext.gregs[REG_EIP]);
Konstantin Belousov3b026a72010-03-10 23:51:09 +0200189#elif defined __FreeBSD__
190 printf (" @ %lx", (unsigned long) uc->uc_mcontext.mc_eip);
hp.com!davidm186cbb22004-09-15 11:37:04 +0000191#endif
Arun Sharma24112f62010-03-10 21:13:26 -0800192#elif UNW_TARGET_X86_64
Konstantin Belousov9bb9c972010-03-31 16:01:38 +0300193#if defined __linux__
Arun Sharma24112f62010-03-10 21:13:26 -0800194 printf (" @ %lx", (unsigned long) uc->uc_mcontext.gregs[REG_RIP]);
Konstantin Belousov9bb9c972010-03-31 16:01:38 +0300195#elif defined __FreeBSD__
196 printf (" @ %lx", (unsigned long) uc->uc_mcontext.mc_rip);
197#endif
Konstantin Belousovd7377092010-03-11 00:02:11 +0200198#endif
hp.com!davidm186cbb22004-09-15 11:37:04 +0000199 printf ("\n");
200 }
201 do_backtrace();
202}
203
204int
205main (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 Pluzhnikovb56375e2009-10-07 12:51:03 -0700229 stk.ss_sp = malloc (SIGSTKSZ);
hp.com!davidm186cbb22004-09-15 11:37:04 +0000230 if (!stk.ss_sp)
231 panic ("failed to allocate SIGSTKSZ (%u) bytes\n", SIGSTKSZ);
Paul Pluzhnikovb56375e2009-10-07 12:51:03 -0700232 stk.ss_size = SIGSTKSZ;
hp.com!davidm186cbb22004-09-15 11:37:04 +0000233 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}