blob: 62a7083131faa55a6c4a5e0d510e3f2b979b1c12 [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
187 printf (" @ %lx", (unsigned long) uc->uc_mcontext.gregs[REG_EIP]);
Arun Sharma24112f62010-03-10 21:13:26 -0800188#elif UNW_TARGET_X86_64
189 printf (" @ %lx", (unsigned long) uc->uc_mcontext.gregs[REG_RIP]);
hp.com!davidm186cbb22004-09-15 11:37:04 +0000190#endif
191 printf ("\n");
192 }
193 do_backtrace();
194}
195
196int
197main (int argc, char **argv)
198{
199 struct sigaction act;
200 stack_t stk;
201
202 verbose = (argc > 1);
203
204 if (verbose)
205 printf ("Normal backtrace:\n");
206
207 bar (1);
208
209 memset (&act, 0, sizeof (act));
210 act.sa_handler = (void (*)(int)) sighandler;
211 act.sa_flags = SA_SIGINFO;
212 if (sigaction (SIGTERM, &act, NULL) < 0)
213 panic ("sigaction: %s\n", strerror (errno));
214
215 if (verbose)
216 printf ("\nBacktrace across signal handler:\n");
217 kill (getpid (), SIGTERM);
218
219 if (verbose)
220 printf ("\nBacktrace across signal handler on alternate stack:\n");
Paul Pluzhnikovb56375e2009-10-07 12:51:03 -0700221 stk.ss_sp = malloc (SIGSTKSZ);
hp.com!davidm186cbb22004-09-15 11:37:04 +0000222 if (!stk.ss_sp)
223 panic ("failed to allocate SIGSTKSZ (%u) bytes\n", SIGSTKSZ);
Paul Pluzhnikovb56375e2009-10-07 12:51:03 -0700224 stk.ss_size = SIGSTKSZ;
hp.com!davidm186cbb22004-09-15 11:37:04 +0000225 stk.ss_flags = 0;
226 if (sigaltstack (&stk, NULL) < 0)
227 panic ("sigaltstack: %s\n", strerror (errno));
228
229 memset (&act, 0, sizeof (act));
230 act.sa_handler = (void (*)(int)) sighandler;
231 act.sa_flags = SA_ONSTACK | SA_SIGINFO;
232 if (sigaction (SIGTERM, &act, NULL) < 0)
233 panic ("sigaction: %s\n", strerror (errno));
234 kill (getpid (), SIGTERM);
235
236 if (num_errors > 0)
237 {
238 fprintf (stderr, "FAILURE: detected %d errors\n", num_errors);
239 exit (-1);
240 }
241 if (verbose)
242 printf ("SUCCESS.\n");
243 return 0;
244}