1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2003-2004 Hewlett-Packard Co
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
5 This file is part of libunwind.
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
33 printf ("FAILURE: ttrace() not supported yet\n");
37 #else /* !HAVE_TTRACE */
41 #include <libunwind-ptrace.h>
48 #include <sys/ptrace.h>
51 extern char **environ;
53 static const int nerrors_max = 100;
67 #define panic(args...) \
68 do { fprintf (stderr, args); ++nerrors; } while (0)
70 static unw_addr_space_t as;
71 static struct UPT_info *ui;
78 unw_word_t ip, sp, start_ip = 0, off;
85 ret = unw_init_remote (&c, as, ui);
87 panic ("unw_init_remote() failed: ret=%d\n", ret);
91 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);
100 unw_get_proc_name (&c, buf, sizeof (buf), &off);
107 if (len >= sizeof (buf) - 32)
108 len = sizeof (buf) - 32;
109 sprintf (buf + len, "+0x%lx", (unsigned long) off);
111 printf ("%016lx %-32s (sp=%016lx)\n", (long) ip, buf, (long) sp);
114 if ((ret = unw_get_proc_info (&c, &pi)) < 0)
115 panic ("unw_get_proc_info(ip=0x%lx) failed: ret=%d\n", (long) ip, ret);
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);
125 if ((ret = unw_get_reg (&c, UNW_IA64_BSP, &bsp)) < 0)
126 panic ("unw_get_reg() failed: ret=%d\n", ret);
128 printf (" bsp=%lx", bsp);
137 unw_get_reg (&c, UNW_REG_IP, &ip);
138 panic ("FAILURE: unw_step() returned %d for ip=%lx (start ip=%lx)\n",
139 ret, (long) ip, (long) start_ip);
144 /* guard against bad unwind info in old libraries... */
145 panic ("too deeply nested---assuming bogus unwind (start ip=%lx)\n",
149 if (nerrors > nerrors_max)
151 panic ("Too many errors (%d)!\n", nerrors);
158 panic ("unwind failed with ret=%d\n", ret);
161 printf ("================\n\n");
164 static pid_t target_pid;
165 static void target_pid_kill (void)
167 kill (target_pid, SIGKILL);
171 main (int argc, char **argv)
173 int status, pid, pending_sig, optind = 1, state = 1;
175 as = unw_create_addr_space (&_UPT_accessors, 0);
177 panic ("unw_create_addr_space() failed");
181 static char *args[] = { "self", "/bin/ls", "/usr", NULL };
183 /* automated test case */
187 while (argv[optind][0] == '-')
189 if (strcmp (argv[optind], "-v") == 0)
190 ++optind, verbose = 1;
191 else if (strcmp (argv[optind], "-i") == 0)
192 ++optind, trace_mode = INSTRUCTION; /* backtrace at each insn */
193 else if (strcmp (argv[optind], "-s") == 0)
194 ++optind, trace_mode = SYSCALL; /* backtrace at each syscall */
195 else if (strcmp (argv[optind], "-t") == 0)
196 /* Execute until raise(SIGUSR1), then backtrace at each insn
197 until raise(SIGUSR2). */
198 ++optind, trace_mode = TRIGGER;
199 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;
206 fprintf(stderr, "unrecognized option: %s\n", argv[optind++]);
211 target_pid = fork ();
217 dup2 (open ("/dev/null", O_WRONLY), 1);
219 #if HAVE_DECL_PTRACE_TRACEME
220 ptrace (PTRACE_TRACEME, 0, 0, 0);
221 #elif HAVE_DECL_PT_TRACE_ME
222 ptrace (PT_TRACE_ME, 0, 0, 0);
227 if ((argc > 1) && (optind == argc)) {
228 fprintf(stderr, "Need to specify a command line for the child\n");
231 execve (argv[optind], argv + optind, environ);
234 atexit (target_pid_kill);
236 ui = _UPT_create (target_pid);
238 while (nerrors <= nerrors_max)
240 pid = wait4 (-1, &status, 0, NULL);
246 panic ("wait4() failed (errno=%d)\n", errno);
249 if (WIFSIGNALED (status) || WIFEXITED (status)
250 || (WIFSTOPPED (status) && WSTOPSIG (status) != SIGTRAP))
252 if (WIFEXITED (status))
254 if (WEXITSTATUS (status) != 0)
255 panic ("child's exit status %d\n", WEXITSTATUS (status));
258 else if (WIFSIGNALED (status))
261 panic ("child terminated by signal %d\n", WTERMSIG (status));
266 pending_sig = WSTOPSIG (status);
267 /* Avoid deadlock: */
268 if (WSTOPSIG (status) == SIGKILL)
270 if (trace_mode == TRIGGER)
272 if (WSTOPSIG (status) == SIGUSR1)
274 else if (WSTOPSIG (status) == SIGUSR2)
277 if (WSTOPSIG (status) != SIGUSR1 && WSTOPSIG (status) != SIGUSR2)
279 static int count = 0;
283 panic ("Too many child unexpected signals (now %d)\n",
295 #if HAVE_DECL_PTRACE_CONT
296 ptrace (PTRACE_CONT, target_pid, 0, 0);
297 #elif HAVE_DECL_PT_CONTINUE
298 ptrace (PT_CONTINUE, target_pid, (caddr_t)1, 0);
305 #if HAVE_DECL_PTRACE_SINGLESTEP
306 ptrace (PTRACE_SINGLESTEP, target_pid, 0, pending_sig);
307 #elif HAVE_DECL_PT_STEP
308 ptrace (PT_STEP, target_pid, (caddr_t)1, pending_sig);
319 #if HAVE_DECL_PTRACE_SYSCALL
320 ptrace (PTRACE_SYSCALL, target_pid, 0, pending_sig);
321 #elif HAVE_DECL_PT_SYSCALL
322 ptrace (PT_SYSCALL, target_pid, (caddr_t)1, pending_sig);
330 #if HAVE_DECL_PTRACE_SINGLESTEP
331 ptrace (PTRACE_SINGLESTEP, target_pid, 0, pending_sig);
332 #elif HAVE_DECL_PT_STEP
333 ptrace (PT_STEP, target_pid, (caddr_t)1, pending_sig);
340 kill (target_pid, SIGKILL);
344 unw_destroy_addr_space (as);
348 printf ("FAILURE: detected %d errors\n", nerrors);
352 printf ("SUCCESS\n");
357 #endif /* !HAVE_TTRACE */