blob: c9f8f2e9b5b1905099b821ee3e1d63359d735c48 [file] [log] [blame]
hp.com!davidme9e4e5f2003-01-28 03:40:06 +00001/* libunwind - a platform-independent unwind library
mostang.com!davidma4bd80c2004-04-21 07:24:35 +00002 Copyright (C) 2003-2004 Hewlett-Packard Co
hp.com!davidme9e4e5f2003-01-28 03:40:06 +00003 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5This file is part of libunwind.
6
7Permission is hereby granted, free of charge, to any person obtaining
8a copy of this software and associated documentation files (the
9"Software"), to deal in the Software without restriction, including
10without limitation the rights to use, copy, modify, merge, publish,
11distribute, sublicense, and/or sell copies of the Software, and to
12permit persons to whom the Software is furnished to do so, subject to
13the following conditions:
14
15The above copyright notice and this permission notice shall be
16included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
25
mostang.com!davidmba424722004-05-04 22:19:18 +000026#include <config.h>
27
28#ifdef HAVE_TTRACE
29
30int
31main (int argc, char **argv)
32{
33 printf ("FAILURE: ttrace() not supported yet\n");
34 return -1;
35}
36
37#else /* !HAVE_TTRACE */
38
hp.com!davidme9e4e5f2003-01-28 03:40:06 +000039#include <errno.h>
40#include <fcntl.h>
mostang.com!davidm40a15582004-01-21 01:05:07 +000041#include <libunwind-ptrace.h>
Konstantin Belousovad932152010-03-08 00:50:50 +020042#include <signal.h>
hp.com!davidme9e4e5f2003-01-28 03:40:06 +000043#include <stdio.h>
44#include <stdlib.h>
hp.com!davidmb97fa142003-02-26 08:33:57 +000045#include <string.h>
hp.com!davidme9e4e5f2003-01-28 03:40:06 +000046#include <unistd.h>
47
48#include <sys/ptrace.h>
49#include <sys/wait.h>
50
Konstantin Belousovad932152010-03-08 00:50:50 +020051extern char **environ;
52
Jan Kratochvila72abd42007-05-16 13:16:31 -060053static const int nerrors_max = 100;
David Mosberger-Tang5f3d2952007-05-16 13:19:46 -060054
55int nerrors;
mostang.com!davidm64b4ac32003-02-27 09:58:57 +000056int verbose;
mostang.com!davidma4bd80c2004-04-21 07:24:35 +000057int print_names = 1;
hp.com!davidme9e4e5f2003-01-28 03:40:06 +000058
hp.com!davidmb97fa142003-02-26 08:33:57 +000059enum
60 {
61 INSTRUCTION,
62 SYSCALL,
63 TRIGGER
64 }
65trace_mode = SYSCALL;
66
hp.com!davidmb0048ee2004-10-15 13:48:38 +000067#define panic(args...) \
hp.com!davidme9e4e5f2003-01-28 03:40:06 +000068 do { fprintf (stderr, args); ++nerrors; } while (0)
69
70static unw_addr_space_t as;
mostang.com!davidmde4410d2003-01-28 07:32:15 +000071static struct UPT_info *ui;
hp.com!davidme9e4e5f2003-01-28 03:40:06 +000072
Jan Kratochvila72abd42007-05-16 13:16:31 -060073static int killed;
74
hp.com!davidme9e4e5f2003-01-28 03:40:06 +000075void
76do_backtrace (pid_t target_pid)
77{
hp.com!davidm95c9a4a2005-05-03 09:13:17 +000078 unw_word_t ip, sp, start_ip = 0, off;
mostang.com!davidm64b4ac32003-02-27 09:58:57 +000079 int n = 0, ret;
hp.com!davidme9e4e5f2003-01-28 03:40:06 +000080 unw_proc_info_t pi;
hp.com!davidme9e4e5f2003-01-28 03:40:06 +000081 unw_cursor_t c;
82 char buf[512];
hp.com!davidm95c9a4a2005-05-03 09:13:17 +000083 size_t len;
hp.com!davidme9e4e5f2003-01-28 03:40:06 +000084
hp.com!davidme9e4e5f2003-01-28 03:40:06 +000085 ret = unw_init_remote (&c, as, ui);
86 if (ret < 0)
87 panic ("unw_init_remote() failed: ret=%d\n", ret);
88
89 do
90 {
mostang.com!davidm64b4ac32003-02-27 09:58:57 +000091 if ((ret = unw_get_reg (&c, UNW_REG_IP, &ip)) < 0
92 || (ret = unw_get_reg (&c, UNW_REG_SP, &sp)) < 0)
93 panic ("unw_get_reg/unw_get_proc_name() failed: ret=%d\n", ret);
94
hp.com!davidmb0048ee2004-10-15 13:48:38 +000095 if (n == 0)
96 start_ip = ip;
97
mostang.com!davidma4bd80c2004-04-21 07:24:35 +000098 buf[0] = '\0';
99 if (print_names)
hp.com!davidm95c9a4a2005-05-03 09:13:17 +0000100 unw_get_proc_name (&c, buf, sizeof (buf), &off);
hp.com!davidme9e4e5f2003-01-28 03:40:06 +0000101
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000102 if (verbose)
hp.com!davidm95c9a4a2005-05-03 09:13:17 +0000103 {
104 if (off)
105 {
106 len = strlen (buf);
107 if (len >= sizeof (buf) - 32)
108 len = sizeof (buf) - 32;
Jan Kratochvila72abd42007-05-16 13:16:31 -0600109 sprintf (buf + len, "+0x%lx", (unsigned long) off);
hp.com!davidm95c9a4a2005-05-03 09:13:17 +0000110 }
111 printf ("%016lx %-32s (sp=%016lx)\n", (long) ip, buf, (long) sp);
112 }
hp.com!davidme9e4e5f2003-01-28 03:40:06 +0000113
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000114 if ((ret = unw_get_proc_info (&c, &pi)) < 0)
hp.com!davidm95c9a4a2005-05-03 09:13:17 +0000115 panic ("unw_get_proc_info(ip=0x%lx) failed: ret=%d\n", (long) ip, ret);
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000116 else if (verbose)
117 printf ("\tproc=%016lx-%016lx\n\thandler=%lx lsda=%lx",
118 (long) pi.start_ip, (long) pi.end_ip,
119 (long) pi.handler, (long) pi.lsda);
hp.com!davidme9e4e5f2003-01-28 03:40:06 +0000120
121#if UNW_TARGET_IA64
122 {
123 unw_word_t bsp;
124
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000125 if ((ret = unw_get_reg (&c, UNW_IA64_BSP, &bsp)) < 0)
126 panic ("unw_get_reg() failed: ret=%d\n", ret);
127 else if (verbose)
128 printf (" bsp=%lx", bsp);
hp.com!davidme9e4e5f2003-01-28 03:40:06 +0000129 }
130#endif
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000131 if (verbose)
132 printf ("\n");
hp.com!davidme9e4e5f2003-01-28 03:40:06 +0000133
134 ret = unw_step (&c);
135 if (ret < 0)
136 {
137 unw_get_reg (&c, UNW_REG_IP, &ip);
hp.com!davidmb0048ee2004-10-15 13:48:38 +0000138 panic ("FAILURE: unw_step() returned %d for ip=%lx (start ip=%lx)\n",
139 ret, (long) ip, (long) start_ip);
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000140 }
141
hp.com!davidm95c9a4a2005-05-03 09:13:17 +0000142 if (++n > 64)
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000143 {
144 /* guard against bad unwind info in old libraries... */
hp.com!davidm95c9a4a2005-05-03 09:13:17 +0000145 panic ("too deeply nested---assuming bogus unwind (start ip=%lx)\n",
146 (long) start_ip);
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000147 break;
hp.com!davidme9e4e5f2003-01-28 03:40:06 +0000148 }
Jan Kratochvila72abd42007-05-16 13:16:31 -0600149 if (nerrors > nerrors_max)
150 {
151 panic ("Too many errors (%d)!\n", nerrors);
152 break;
153 }
hp.com!davidme9e4e5f2003-01-28 03:40:06 +0000154 }
155 while (ret > 0);
156
hp.com!davidme9e4e5f2003-01-28 03:40:06 +0000157 if (ret < 0)
158 panic ("unwind failed with ret=%d\n", ret);
159
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000160 if (verbose)
161 printf ("================\n\n");
hp.com!davidme9e4e5f2003-01-28 03:40:06 +0000162}
163
Jan Kratochvila72abd42007-05-16 13:16:31 -0600164static pid_t target_pid;
165static void target_pid_kill (void)
166{
167 kill (target_pid, SIGKILL);
168}
169
hp.com!davidme9e4e5f2003-01-28 03:40:06 +0000170int
171main (int argc, char **argv)
172{
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000173 int status, pid, pending_sig, optind = 1, state = 1;
hp.com!davidme9e4e5f2003-01-28 03:40:06 +0000174
175 as = unw_create_addr_space (&_UPT_accessors, 0);
176 if (!as)
177 panic ("unw_create_addr_space() failed");
178
179 if (argc == 1)
180 {
181 char *args[] = { "self", "/bin/ls", "/usr", NULL };
182
183 /* automated test case */
hp.com!davidme9e4e5f2003-01-28 03:40:06 +0000184 argv = args;
185 }
hp.com!davidmb97fa142003-02-26 08:33:57 +0000186 else if (argc > 1)
hp.com!davidme6ddf4c2003-03-29 07:32:50 +0000187 while (argv[optind][0] == '-')
188 {
189 if (strcmp (argv[optind], "-v") == 0)
190 ++optind, verbose = 1;
mostang.com!davidma4bd80c2004-04-21 07:24:35 +0000191 else if (strcmp (argv[optind], "-i") == 0)
hp.com!davidme6ddf4c2003-03-29 07:32:50 +0000192 ++optind, trace_mode = INSTRUCTION; /* backtrace at each insn */
193 else if (strcmp (argv[optind], "-s") == 0)
mostang.com!davidma4bd80c2004-04-21 07:24:35 +0000194 ++optind, trace_mode = SYSCALL; /* backtrace at each syscall */
hp.com!davidme6ddf4c2003-03-29 07:32:50 +0000195 else if (strcmp (argv[optind], "-t") == 0)
hp.com!davidmb0048ee2004-10-15 13:48:38 +0000196 /* Execute until raise(SIGUSR1), then backtrace at each insn
197 until raise(SIGUSR2). */
hp.com!davidme6ddf4c2003-03-29 07:32:50 +0000198 ++optind, trace_mode = TRIGGER;
mostang.com!davidma4bd80c2004-04-21 07:24:35 +0000199 else if (strcmp (argv[optind], "-c") == 0)
200 /* Enable caching of unwind-info. */
201 ++optind, unw_set_caching_policy (as, UNW_CACHE_GLOBAL);
202 else if (strcmp (argv[optind], "-n") == 0)
203 /* Don't look-up and print symbol names. */
204 ++optind, print_names = 0;
Zachary T Welch13cd3b02011-03-02 17:40:09 +0100205 else
206 fprintf(stderr, "unrecognized option: %s\n", argv[optind++]);
hp.com!davidme6ddf4c2003-03-29 07:32:50 +0000207 }
hp.com!davidme9e4e5f2003-01-28 03:40:06 +0000208
209 target_pid = fork ();
210 if (!target_pid)
211 {
212 /* child */
213
214 if (!verbose)
215 dup2 (open ("/dev/null", O_WRONLY), 1);
216
Konstantin Belousovad932152010-03-08 00:50:50 +0200217#if HAVE_DECL_PTRACE_TRACEME
hp.com!davidme9e4e5f2003-01-28 03:40:06 +0000218 ptrace (PTRACE_TRACEME, 0, 0, 0);
Konstantin Belousovad932152010-03-08 00:50:50 +0200219#elif HAVE_DECL_PT_TRACE_ME
220 ptrace (PT_TRACE_ME, 0, 0, 0);
221#else
222#error Trace me
223#endif
hp.com!davidmb97fa142003-02-26 08:33:57 +0000224 execve (argv[optind], argv + optind, environ);
hp.com!davidme9e4e5f2003-01-28 03:40:06 +0000225 _exit (-1);
226 }
Jan Kratochvila72abd42007-05-16 13:16:31 -0600227 atexit (target_pid_kill);
hp.com!davidme9e4e5f2003-01-28 03:40:06 +0000228
mostang.com!davidmde4410d2003-01-28 07:32:15 +0000229 ui = _UPT_create (target_pid);
230
Jan Kratochvila72abd42007-05-16 13:16:31 -0600231 while (nerrors <= nerrors_max)
hp.com!davidme9e4e5f2003-01-28 03:40:06 +0000232 {
233 pid = wait4 (-1, &status, 0, 0);
234 if (pid == -1)
235 {
236 if (errno == EINTR)
237 continue;
238
239 panic ("wait4() failed (errno=%d)\n", errno);
240 }
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000241 pending_sig = 0;
hp.com!davidme9e4e5f2003-01-28 03:40:06 +0000242 if (WIFSIGNALED (status) || WIFEXITED (status)
243 || (WIFSTOPPED (status) && WSTOPSIG (status) != SIGTRAP))
244 {
245 if (WIFEXITED (status))
246 {
247 if (WEXITSTATUS (status) != 0)
248 panic ("child's exit status %d\n", WEXITSTATUS (status));
249 break;
250 }
251 else if (WIFSIGNALED (status))
mostang.com!davidma4bd80c2004-04-21 07:24:35 +0000252 {
Jan Kratochvila72abd42007-05-16 13:16:31 -0600253 if (!killed)
254 panic ("child terminated by signal %d\n", WTERMSIG (status));
mostang.com!davidma4bd80c2004-04-21 07:24:35 +0000255 break;
256 }
hp.com!davidme9e4e5f2003-01-28 03:40:06 +0000257 else
hp.com!davidmb97fa142003-02-26 08:33:57 +0000258 {
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000259 pending_sig = WSTOPSIG (status);
Jan Kratochvila72abd42007-05-16 13:16:31 -0600260 /* Avoid deadlock: */
261 if (WSTOPSIG (status) == SIGKILL)
262 break;
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000263 if (trace_mode == TRIGGER)
hp.com!davidmb97fa142003-02-26 08:33:57 +0000264 {
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000265 if (WSTOPSIG (status) == SIGUSR1)
266 state = 0;
267 else if (WSTOPSIG (status) == SIGUSR2)
268 state = 1;
hp.com!davidmb97fa142003-02-26 08:33:57 +0000269 }
Jan Kratochvila72abd42007-05-16 13:16:31 -0600270 if (WSTOPSIG (status) != SIGUSR1 && WSTOPSIG (status) != SIGUSR2)
271 {
272 static int count = 0;
273
274 if (count++ > 100)
275 {
276 panic ("Too many child unexpected signals (now %d)\n",
277 WSTOPSIG (status));
278 killed = 1;
279 }
280 }
hp.com!davidmb97fa142003-02-26 08:33:57 +0000281 }
hp.com!davidmb97fa142003-02-26 08:33:57 +0000282 }
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000283
284 switch (trace_mode)
hp.com!davidmb97fa142003-02-26 08:33:57 +0000285 {
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000286 case TRIGGER:
287 if (state)
Konstantin Belousovad932152010-03-08 00:50:50 +0200288#if HAVE_DECL_PTRACE_CONT
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000289 ptrace (PTRACE_CONT, target_pid, 0, 0);
Konstantin Belousovad932152010-03-08 00:50:50 +0200290#elif HAVE_DECL_PT_CONTINUE
291 ptrace (PT_CONTINUE, target_pid, (caddr_t)1, 0);
292#else
293#error Port me
294#endif
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000295 else
296 {
297 do_backtrace (target_pid);
Konstantin Belousovad932152010-03-08 00:50:50 +0200298#if HAVE_DECL_PTRACE_SINGLESTEP
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000299 ptrace (PTRACE_SINGLESTEP, target_pid, 0, pending_sig);
Konstantin Belousovad932152010-03-08 00:50:50 +0200300#elif HAVE_DECL_PT_STEP
301 ptrace (PT_STEP, target_pid, (caddr_t)1, pending_sig);
302#else
303#error Singlestep me
304#endif
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000305 }
306 break;
307
308 case SYSCALL:
309 if (!state)
310 do_backtrace (target_pid);
311 state ^= 1;
Konstantin Belousovad932152010-03-08 00:50:50 +0200312#if HAVE_DECL_PTRACE_SYSCALL
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000313 ptrace (PTRACE_SYSCALL, target_pid, 0, pending_sig);
Konstantin Belousovad932152010-03-08 00:50:50 +0200314#elif HAVE_DECL_PT_SYSCALL
Konstantin Belousov4b3ca292010-03-08 17:01:31 +0200315 ptrace (PT_SYSCALL, target_pid, (caddr_t)1, pending_sig);
Konstantin Belousovad932152010-03-08 00:50:50 +0200316#else
317#error Syscall me
318#endif
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000319 break;
320
321 case INSTRUCTION:
hp.com!davidmb97fa142003-02-26 08:33:57 +0000322 do_backtrace (target_pid);
Konstantin Belousovad932152010-03-08 00:50:50 +0200323#if HAVE_DECL_PTRACE_SINGLESTEP
324 ptrace (PTRACE_SINGLESTEP, target_pid, 0, pending_sig);
325#elif HAVE_DECL_PT_STEP
326 ptrace (PT_STEP, target_pid, (caddr_t)1, pending_sig);
327#else
328#error Singlestep me
329#endif
mostang.com!davidm64b4ac32003-02-27 09:58:57 +0000330 break;
hp.com!davidmb97fa142003-02-26 08:33:57 +0000331 }
Jan Kratochvila72abd42007-05-16 13:16:31 -0600332 if (killed)
333 kill (target_pid, SIGKILL);
hp.com!davidme9e4e5f2003-01-28 03:40:06 +0000334 }
335
mostang.com!davidmde4410d2003-01-28 07:32:15 +0000336 _UPT_destroy (ui);
337
hp.com!davidme9e4e5f2003-01-28 03:40:06 +0000338 if (nerrors)
339 {
340 printf ("FAILURE: detected %d errors\n", nerrors);
341 exit (-1);
342 }
343 if (verbose)
344 printf ("SUCCESS\n");
345
346 return 0;
347}
mostang.com!davidmba424722004-05-04 22:19:18 +0000348
349#endif /* !HAVE_TTRACE */