1 /* Test program for unwinding of frames.
2 Copyright (C) 2013, 2014 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 elfutils is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include <stdio_ext.h>
30 #include <sys/resource.h>
31 #include <sys/ptrace.h>
33 #include <sys/types.h>
39 #include ELFUTILS_HEADER(dwfl)
44 main (int argc __attribute__ ((unused)), char **argv)
46 fprintf (stderr, "%s: Unwinding not supported for this architecture\n",
54 dump_modules (Dwfl_Module *mod, void **userdata __attribute__ ((unused)),
55 const char *name, Dwarf_Addr start,
56 void *arg __attribute__ ((unused)))
59 dwfl_module_info (mod, NULL, NULL, &end, NULL, NULL, NULL, NULL);
60 printf ("%#" PRIx64 "\t%#" PRIx64 "\t%s\n", (uint64_t) start, (uint64_t) end,
65 static bool is_x86_64_native;
66 static pid_t check_tid;
69 callback_verify (pid_t tid, unsigned frameno, Dwarf_Addr pc,
70 const char *symname, Dwfl *dwfl)
72 static bool seen_main = false;
73 if (symname && *symname == '.')
75 if (symname && strcmp (symname, "main") == 0)
86 // For the main thread we are only interested if we can unwind till
87 // we see the "main" symbol.
91 static bool reduce_frameno = false;
94 if (! is_x86_64_native && frameno >= 2)
96 const char *symname2 = NULL;
100 if (! reduce_frameno && symname
101 && (strcmp (symname, "__kernel_vsyscall") == 0
102 || strcmp (symname, "__libc_do_syscall") == 0))
103 reduce_frameno = true;
105 assert (symname && strcmp (symname, "raise") == 0);
108 assert (symname != NULL && strcmp (symname, "sigusr2") == 0);
110 case 2: // x86_64 only
111 /* __restore_rt - glibc maybe does not have to have this symbol. */
113 case 3: // x86_64 only
114 if (is_x86_64_native)
116 /* Verify we trapped on the very first instruction of jmp. */
117 assert (symname != NULL && strcmp (symname, "jmp") == 0);
118 mod = dwfl_addrmodule (dwfl, pc - 1);
120 symname2 = dwfl_module_addrname (mod, pc - 1);
121 assert (symname2 == NULL || strcmp (symname2, "jmp") != 0);
126 assert (symname != NULL && strcmp (symname, "stdarg") == 0);
129 /* Verify we trapped on the very last instruction of child. */
130 assert (symname != NULL && strcmp (symname, "backtracegen") == 0);
131 mod = dwfl_addrmodule (dwfl, pc);
133 symname2 = dwfl_module_addrname (mod, pc);
135 // Note that the following assert might in theory even fail on x86_64,
136 // there is no guarantee that the compiler doesn't reorder the
137 // instructions or even inserts some padding instructions at the end
138 // (which apparently happens on ppc64).
139 if (is_x86_64_native)
140 assert (symname2 == NULL || strcmp (symname2, "backtracegen") != 0);
146 frame_callback (Dwfl_Frame *state, void *frame_arg)
148 int *framenop = frame_arg;
154 error (0, 0, "Too many frames: %d\n", *framenop);
155 return DWARF_CB_ABORT;
158 if (! dwfl_frame_pc (state, &pc, &isactivation))
160 error (0, 0, "%s", dwfl_errmsg (-1));
161 return DWARF_CB_ABORT;
163 Dwarf_Addr pc_adjusted = pc - (isactivation ? 0 : 1);
165 /* Get PC->SYMNAME. */
166 Dwfl_Thread *thread = dwfl_frame_thread (state);
167 Dwfl *dwfl = dwfl_thread_dwfl (thread);
168 Dwfl_Module *mod = dwfl_addrmodule (dwfl, pc_adjusted);
169 const char *symname = NULL;
171 symname = dwfl_module_addrname (mod, pc_adjusted);
173 printf ("#%2d %#" PRIx64 "%4s\t%s\n", *framenop, (uint64_t) pc,
174 ! isactivation ? "- 1" : "", symname);
175 pid_t tid = dwfl_thread_tid (thread);
176 callback_verify (tid, *framenop, pc, symname, dwfl);
183 thread_callback (Dwfl_Thread *thread, void *thread_arg __attribute__((unused)))
185 printf ("TID %ld:\n", (long) dwfl_thread_tid (thread));
187 switch (dwfl_thread_getframes (thread, frame_callback, &frameno))
192 return DWARF_CB_ABORT;
194 error (0, 0, "dwfl_thread_getframes: %s", dwfl_errmsg (-1));
195 /* All platforms do not have yet proper unwind termination. */
206 ptrdiff_t ptrdiff = dwfl_getmodules (dwfl, dump_modules, NULL, 0);
207 assert (ptrdiff == 0);
209 switch (dwfl_getthreads (dwfl, thread_callback, NULL))
217 error (0, 0, "dwfl_getthreads: %s", dwfl_errmsg (-1));
223 callback_verify (0, 0, 0, NULL, dwfl);
228 struct see_exec_module
231 char selfpath[PATH_MAX + 1];
235 see_exec_module (Dwfl_Module *mod, void **userdata __attribute__ ((unused)),
236 const char *name __attribute__ ((unused)),
237 Dwarf_Addr start __attribute__ ((unused)), void *arg)
239 struct see_exec_module *data = arg;
240 if (strcmp (name, data->selfpath) != 0)
242 assert (data->mod == NULL);
248 PC will get changed to function 'jmp' by backtrace.c function
249 prepare_thread. Then SIGUSR2 will be signalled to backtrace-child
250 which will invoke function sigusr2.
251 This is all done so that signal interrupts execution of the very first
252 instruction of a function. Properly handled unwind should not slip into
253 the previous unrelated function. */
256 prepare_thread (pid_t pid2 __attribute__ ((unused)),
257 void (*jmp) (void) __attribute__ ((unused)))
264 l = ptrace (PTRACE_POKEUSER, pid2,
265 (void *) (intptr_t) offsetof (struct user_regs_struct, rip), jmp);
266 assert_perror (errno);
268 l = ptrace (PTRACE_CONT, pid2, NULL, (void *) (intptr_t) SIGUSR2);
270 pid_t got = waitpid (pid2, &status, __WALL);
271 assert_perror (errno);
272 assert (got == pid2);
273 assert (WIFSTOPPED (status));
274 assert (WSTOPSIG (status) == SIGUSR1);
275 #endif /* __x86_64__ */
278 #include <asm/unistd.h>
280 #define tgkill(pid, tid, sig) syscall (__NR_tgkill, (pid), (tid), (sig))
283 report_pid (Dwfl *dwfl, pid_t pid)
285 int result = dwfl_linux_proc_report (dwfl, pid);
287 error (2, 0, "dwfl_linux_proc_report: %s", dwfl_errmsg (-1));
289 error (2, result, "dwfl_linux_proc_report");
291 if (dwfl_report_end (dwfl, NULL, NULL) != 0)
292 error (2, 0, "dwfl_report_end: %s", dwfl_errmsg (-1));
294 result = dwfl_linux_proc_attach (dwfl, pid, true);
296 error (2, 0, "dwfl_linux_proc_attach: %s", dwfl_errmsg (-1));
298 error (2, result, "dwfl_linux_proc_attach");
302 pid_to_dwfl (pid_t pid)
304 static char *debuginfo_path;
305 static const Dwfl_Callbacks proc_callbacks =
307 .find_debuginfo = dwfl_standard_find_debuginfo,
308 .debuginfo_path = &debuginfo_path,
310 .find_elf = dwfl_linux_proc_find_elf,
312 Dwfl *dwfl = dwfl_begin (&proc_callbacks);
314 error (2, 0, "dwfl_begin: %s", dwfl_errmsg (-1));
315 report_pid (dwfl, pid);
320 exec_dump (const char *exec)
328 execl (exec, exec, "--ptraceme", NULL);
334 /* Catch the main thread. Catch it first otherwise the /proc evaluation of
335 PID may have caught still ourselves before executing execl above. */
338 pid_t got = waitpid (pid, &status, 0);
339 assert_perror (errno);
341 assert (WIFSTOPPED (status));
342 // Main thread will signal SIGUSR2. Other thread will signal SIGUSR1.
343 assert (WSTOPSIG (status) == SIGUSR2);
345 /* Catch the spawned thread. Do not use __WCLONE as we could get racy
346 __WCLONE, probably despite pthread_create already had to be called the new
347 task is not yet alive enough for waitpid. */
348 pid_t pid2 = waitpid (-1, &status, __WALL);
349 assert_perror (errno);
351 assert (pid2 != pid);
352 assert (WIFSTOPPED (status));
353 // Main thread will signal SIGUSR2. Other thread will signal SIGUSR1.
354 assert (WSTOPSIG (status) == SIGUSR1);
356 Dwfl *dwfl = pid_to_dwfl (pid);
358 int i = asprintf (&selfpathname, "/proc/%ld/exe", (long) pid);
360 struct see_exec_module data;
361 ssize_t ssize = readlink (selfpathname, data.selfpath,
362 sizeof (data.selfpath));
364 assert (ssize > 0 && ssize < (ssize_t) sizeof (data.selfpath));
365 data.selfpath[ssize] = '\0';
367 ptrdiff_t ptrdiff = dwfl_getmodules (dwfl, see_exec_module, &data, 0);
368 assert (ptrdiff == 0);
369 assert (data.mod != NULL);
371 Elf *elf = dwfl_module_getelf (data.mod, &loadbase);
372 GElf_Ehdr ehdr_mem, *ehdr = gelf_getehdr (elf, &ehdr_mem);
373 assert (ehdr != NULL);
374 /* It is false also on x86_64 with i386 inferior. */
376 is_x86_64_native = false;
377 #else /* __x86_64__ */
378 is_x86_64_native = ehdr->e_ident[EI_CLASS] == ELFCLASS64;
379 #endif /* __x86_64__ */
381 if (is_x86_64_native)
383 // Find inferior symbol named "jmp".
384 int nsym = dwfl_module_getsymtab (data.mod);
386 for (symi = 1; symi < nsym; ++symi)
389 const char *symbol_name = dwfl_module_getsym (data.mod, symi, &symbol, NULL);
390 if (symbol_name == NULL)
392 switch (GELF_ST_TYPE (symbol.st_info))
399 if (strcmp (symbol_name, "jmp") != 0)
403 /* LOADBASE is already applied here. */
404 jmp = (void (*) (void)) (uintptr_t) symbol.st_value;
407 assert (symi < nsym);
408 prepare_thread (pid2, jmp);
412 dwfl = pid_to_dwfl (pid);
417 #define OPT_BACKTRACE_EXEC 0x100
419 static const struct argp_option options[] =
421 { "backtrace-exec", OPT_BACKTRACE_EXEC, "EXEC", 0, N_("Run executable"), 0 },
422 { NULL, 0, NULL, 0, NULL, 0 }
427 parse_opt (int key, char *arg, struct argp_state *state)
432 state->child_inputs[0] = state->input;
435 case OPT_BACKTRACE_EXEC:
440 return ARGP_ERR_UNKNOWN;
446 main (int argc __attribute__ ((unused)), char **argv)
448 /* We use no threads here which can interfere with handling a stream. */
449 __fsetlocking (stdin, FSETLOCKING_BYCALLER);
450 __fsetlocking (stdout, FSETLOCKING_BYCALLER);
451 __fsetlocking (stderr, FSETLOCKING_BYCALLER);
454 (void) setlocale (LC_ALL, "");
456 elf_version (EV_CURRENT);
459 const struct argp_child argp_children[] =
461 { .argp = dwfl_standard_argp () },
464 const struct argp argp =
466 options, parse_opt, NULL, NULL, argp_children, NULL, NULL
468 (void) argp_parse (&argp, argc, argv, 0, NULL, &dwfl);
469 assert (dwfl != NULL);
470 /* We want to make sure the dwfl was properly attached. */
471 if (dwfl_pid (dwfl) < 0)
472 error (2, 0, "dwfl_pid: %s", dwfl_errmsg (-1));
478 #endif /* ! __linux__ */