blob: f5dd761f38d59ca9346b41a0fdfb2c0a736c31d2 [file] [log] [blame]
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +01001/* Test program for unwinding of frames.
Mark Wielaard90084992016-08-25 17:17:23 +02002 Copyright (C) 2013, 2014, 2016 Red Hat, Inc.
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +01003 This file is part of elfutils.
4
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.
9
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.
14
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/>. */
17
18#include <config.h>
19#include <assert.h>
20#include <inttypes.h>
21#include <stdio.h>
22#include <stdio_ext.h>
23#include <locale.h>
24#include <dirent.h>
25#include <stdlib.h>
26#include <errno.h>
27#include <error.h>
28#include <unistd.h>
29#include <dwarf.h>
Pino Toscano65251492015-06-26 20:36:01 +020030#ifdef __linux__
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +010031#include <sys/resource.h>
32#include <sys/ptrace.h>
33#include <signal.h>
34#include <sys/types.h>
35#include <sys/wait.h>
36#include <sys/user.h>
37#include <fcntl.h>
38#include <string.h>
39#include <argp.h>
40#include ELFUTILS_HEADER(dwfl)
Pino Toscano65251492015-06-26 20:36:01 +020041#endif
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +010042
Kurt Roeckx02cefda2014-04-22 21:46:22 +020043#ifndef __linux__
44
45int
46main (int argc __attribute__ ((unused)), char **argv)
47{
48 fprintf (stderr, "%s: Unwinding not supported for this architecture\n",
49 argv[0]);
50 return 77;
51}
52
53#else /* __linux__ */
54
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +010055static int
56dump_modules (Dwfl_Module *mod, void **userdata __attribute__ ((unused)),
57 const char *name, Dwarf_Addr start,
58 void *arg __attribute__ ((unused)))
59{
60 Dwarf_Addr end;
61 dwfl_module_info (mod, NULL, NULL, &end, NULL, NULL, NULL, NULL);
62 printf ("%#" PRIx64 "\t%#" PRIx64 "\t%s\n", (uint64_t) start, (uint64_t) end,
63 name);
64 return DWARF_CB_OK;
65}
66
Mark Wielaard90084992016-08-25 17:17:23 +020067static bool use_raise_jmp_patching;
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +010068static pid_t check_tid;
69
70static void
71callback_verify (pid_t tid, unsigned frameno, Dwarf_Addr pc,
72 const char *symname, Dwfl *dwfl)
73{
74 static bool seen_main = false;
75 if (symname && *symname == '.')
76 symname++;
77 if (symname && strcmp (symname, "main") == 0)
78 seen_main = true;
79 if (pc == 0)
80 {
Mark Wielaard727a90c2017-04-24 13:35:47 +020081 assert (seen_main);
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +010082 return;
83 }
84 if (check_tid == 0)
85 check_tid = tid;
86 if (tid != check_tid)
87 {
88 // For the main thread we are only interested if we can unwind till
89 // we see the "main" symbol.
90 return;
91 }
92 Dwfl_Module *mod;
Ulf Hermannd8437ed2017-04-20 14:41:35 +020093 /* See case 4. Special case to help out simple frame pointer unwinders. */
94 static bool duplicate_sigusr2 = false;
95 if (duplicate_sigusr2)
96 frameno--;
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +010097 static bool reduce_frameno = false;
98 if (reduce_frameno)
99 frameno--;
Mark Wielaard90084992016-08-25 17:17:23 +0200100 if (! use_raise_jmp_patching && frameno >= 2)
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100101 frameno += 2;
102 const char *symname2 = NULL;
103 switch (frameno)
104 {
105 case 0:
106 if (! reduce_frameno && symname
Mark Wielaard1986c172014-06-14 01:09:17 +0200107 && (strcmp (symname, "__kernel_vsyscall") == 0
108 || strcmp (symname, "__libc_do_syscall") == 0))
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100109 reduce_frameno = true;
Mark Wielaard727a90c2017-04-24 13:35:47 +0200110 else
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100111 assert (symname && strcmp (symname, "raise") == 0);
112 break;
113 case 1:
Mark Wielaard727a90c2017-04-24 13:35:47 +0200114 assert (symname != NULL && strcmp (symname, "sigusr2") == 0);
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100115 break;
116 case 2: // x86_64 only
117 /* __restore_rt - glibc maybe does not have to have this symbol. */
118 break;
Mark Wielaard90084992016-08-25 17:17:23 +0200119 case 3: // use_raise_jmp_patching
120 if (use_raise_jmp_patching)
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100121 {
122 /* Verify we trapped on the very first instruction of jmp. */
Mark Wielaard727a90c2017-04-24 13:35:47 +0200123 assert (symname != NULL && strcmp (symname, "jmp") == 0);
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100124 mod = dwfl_addrmodule (dwfl, pc - 1);
125 if (mod)
126 symname2 = dwfl_module_addrname (mod, pc - 1);
Mark Wielaard727a90c2017-04-24 13:35:47 +0200127 assert (symname2 == NULL || strcmp (symname2, "jmp") != 0);
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100128 break;
129 }
Joshua Watt555e15e2018-02-09 10:27:18 -0600130 FALLTHROUGH;
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100131 case 4:
Ulf Hermannd8437ed2017-04-20 14:41:35 +0200132 /* Some simple frame unwinders get this wrong and think sigusr2
133 is calling itself again. Allow it and just pretend there is
134 an extra sigusr2 frame. */
135 if (symname != NULL && strcmp (symname, "sigusr2") == 0)
136 {
137 duplicate_sigusr2 = true;
138 break;
139 }
Mark Wielaard727a90c2017-04-24 13:35:47 +0200140 assert (symname != NULL && strcmp (symname, "stdarg") == 0);
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100141 break;
142 case 5:
143 /* Verify we trapped on the very last instruction of child. */
Mark Wielaard727a90c2017-04-24 13:35:47 +0200144 assert (symname != NULL && strcmp (symname, "backtracegen") == 0);
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100145 mod = dwfl_addrmodule (dwfl, pc);
146 if (mod)
147 symname2 = dwfl_module_addrname (mod, pc);
Mark Wielaardb6ef1ce2013-12-21 19:39:19 +0100148
149 // Note that the following assert might in theory even fail on x86_64,
150 // there is no guarantee that the compiler doesn't reorder the
151 // instructions or even inserts some padding instructions at the end
152 // (which apparently happens on ppc64).
Mark Wielaard727a90c2017-04-24 13:35:47 +0200153 if (use_raise_jmp_patching)
Mark Wielaardb6ef1ce2013-12-21 19:39:19 +0100154 assert (symname2 == NULL || strcmp (symname2, "backtracegen") != 0);
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100155 break;
156 }
157}
158
159static int
160frame_callback (Dwfl_Frame *state, void *frame_arg)
161{
162 int *framenop = frame_arg;
163 Dwarf_Addr pc;
164 bool isactivation;
Mark Wielaard41362da2014-06-15 11:35:50 +0200165
166 if (*framenop > 16)
167 {
168 error (0, 0, "Too many frames: %d\n", *framenop);
169 return DWARF_CB_ABORT;
170 }
171
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100172 if (! dwfl_frame_pc (state, &pc, &isactivation))
173 {
174 error (0, 0, "%s", dwfl_errmsg (-1));
175 return DWARF_CB_ABORT;
176 }
177 Dwarf_Addr pc_adjusted = pc - (isactivation ? 0 : 1);
178
179 /* Get PC->SYMNAME. */
180 Dwfl_Thread *thread = dwfl_frame_thread (state);
181 Dwfl *dwfl = dwfl_thread_dwfl (thread);
182 Dwfl_Module *mod = dwfl_addrmodule (dwfl, pc_adjusted);
183 const char *symname = NULL;
184 if (mod)
185 symname = dwfl_module_addrname (mod, pc_adjusted);
186
187 printf ("#%2d %#" PRIx64 "%4s\t%s\n", *framenop, (uint64_t) pc,
188 ! isactivation ? "- 1" : "", symname);
189 pid_t tid = dwfl_thread_tid (thread);
190 callback_verify (tid, *framenop, pc, symname, dwfl);
191 (*framenop)++;
192
193 return DWARF_CB_OK;
194}
195
196static int
197thread_callback (Dwfl_Thread *thread, void *thread_arg __attribute__((unused)))
198{
199 printf ("TID %ld:\n", (long) dwfl_thread_tid (thread));
200 int frameno = 0;
201 switch (dwfl_thread_getframes (thread, frame_callback, &frameno))
202 {
203 case 0:
204 break;
205 case DWARF_CB_ABORT:
206 return DWARF_CB_ABORT;
207 case -1:
208 error (0, 0, "dwfl_thread_getframes: %s", dwfl_errmsg (-1));
209 /* All platforms do not have yet proper unwind termination. */
210 break;
211 default:
212 abort ();
213 }
214 return DWARF_CB_OK;
215}
216
217static void
218dump (Dwfl *dwfl)
219{
220 ptrdiff_t ptrdiff = dwfl_getmodules (dwfl, dump_modules, NULL, 0);
221 assert (ptrdiff == 0);
222 bool err = false;
223 switch (dwfl_getthreads (dwfl, thread_callback, NULL))
224 {
225 case 0:
226 break;
227 case DWARF_CB_ABORT:
228 err = true;
229 break;
230 case -1:
231 error (0, 0, "dwfl_getthreads: %s", dwfl_errmsg (-1));
232 err = true;
233 break;
234 default:
235 abort ();
236 }
237 callback_verify (0, 0, 0, NULL, dwfl);
238 if (err)
239 exit (EXIT_FAILURE);
240}
241
242struct see_exec_module
243{
244 Dwfl_Module *mod;
245 char selfpath[PATH_MAX + 1];
246};
247
248static int
249see_exec_module (Dwfl_Module *mod, void **userdata __attribute__ ((unused)),
250 const char *name __attribute__ ((unused)),
251 Dwarf_Addr start __attribute__ ((unused)), void *arg)
252{
253 struct see_exec_module *data = arg;
254 if (strcmp (name, data->selfpath) != 0)
255 return DWARF_CB_OK;
256 assert (data->mod == NULL);
257 data->mod = mod;
Mark Wielaard90084992016-08-25 17:17:23 +0200258 return DWARF_CB_ABORT;
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100259}
260
Mark Wielaard90084992016-08-25 17:17:23 +0200261/* We used to do this on x86_64 only (see backtrace-child why we now don't):
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100262 PC will get changed to function 'jmp' by backtrace.c function
263 prepare_thread. Then SIGUSR2 will be signalled to backtrace-child
264 which will invoke function sigusr2.
265 This is all done so that signal interrupts execution of the very first
266 instruction of a function. Properly handled unwind should not slip into
267 the previous unrelated function. */
268
Mark Wielaard90084992016-08-25 17:17:23 +0200269#ifdef __x86_64__
270/* #define RAISE_JMP_PATCHING 1 */
271#endif
272
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100273static void
274prepare_thread (pid_t pid2 __attribute__ ((unused)),
275 void (*jmp) (void) __attribute__ ((unused)))
276{
Mark Wielaard90084992016-08-25 17:17:23 +0200277#ifndef RAISE_JMP_PATCHING
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100278 abort ();
Mark Wielaard90084992016-08-25 17:17:23 +0200279#else /* RAISE_JMP_PATCHING */
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100280 long l;
H.J. Lube7ea702015-03-12 12:14:43 -0700281 struct user_regs_struct user_regs;
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100282 errno = 0;
H.J. Lube7ea702015-03-12 12:14:43 -0700283 l = ptrace (PTRACE_GETREGS, pid2, 0, (intptr_t) &user_regs);
Max Filippovc801acf2015-05-04 20:17:52 +0300284 assert (errno == 0);
H.J. Lube7ea702015-03-12 12:14:43 -0700285 assert (l == 0);
286 user_regs.rip = (intptr_t) jmp;
287 l = ptrace (PTRACE_SETREGS, pid2, 0, (intptr_t) &user_regs);
Max Filippovc801acf2015-05-04 20:17:52 +0300288 assert (errno == 0);
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100289 assert (l == 0);
290 l = ptrace (PTRACE_CONT, pid2, NULL, (void *) (intptr_t) SIGUSR2);
291 int status;
292 pid_t got = waitpid (pid2, &status, __WALL);
Max Filippovc801acf2015-05-04 20:17:52 +0300293 assert (errno == 0);
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100294 assert (got == pid2);
295 assert (WIFSTOPPED (status));
296 assert (WSTOPSIG (status) == SIGUSR1);
Mark Wielaard90084992016-08-25 17:17:23 +0200297#endif /* RAISE_JMP_PATCHING */
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100298}
299
300#include <asm/unistd.h>
301#include <unistd.h>
302#define tgkill(pid, tid, sig) syscall (__NR_tgkill, (pid), (tid), (sig))
303
304static void
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100305report_pid (Dwfl *dwfl, pid_t pid)
306{
307 int result = dwfl_linux_proc_report (dwfl, pid);
308 if (result < 0)
309 error (2, 0, "dwfl_linux_proc_report: %s", dwfl_errmsg (-1));
310 else if (result > 0)
311 error (2, result, "dwfl_linux_proc_report");
312
313 if (dwfl_report_end (dwfl, NULL, NULL) != 0)
314 error (2, 0, "dwfl_report_end: %s", dwfl_errmsg (-1));
Mark Wielaard19108012013-12-30 22:00:57 +0100315
Mark Wielaard70c3a532014-01-04 23:28:33 +0100316 result = dwfl_linux_proc_attach (dwfl, pid, true);
Mark Wielaard19108012013-12-30 22:00:57 +0100317 if (result < 0)
318 error (2, 0, "dwfl_linux_proc_attach: %s", dwfl_errmsg (-1));
319 else if (result > 0)
320 error (2, result, "dwfl_linux_proc_attach");
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100321}
322
323static Dwfl *
324pid_to_dwfl (pid_t pid)
325{
326 static char *debuginfo_path;
327 static const Dwfl_Callbacks proc_callbacks =
328 {
329 .find_debuginfo = dwfl_standard_find_debuginfo,
330 .debuginfo_path = &debuginfo_path,
331
332 .find_elf = dwfl_linux_proc_find_elf,
333 };
334 Dwfl *dwfl = dwfl_begin (&proc_callbacks);
335 if (dwfl == NULL)
336 error (2, 0, "dwfl_begin: %s", dwfl_errmsg (-1));
337 report_pid (dwfl, pid);
338 return dwfl;
339}
340
341static void
342exec_dump (const char *exec)
343{
344 pid_t pid = fork ();
345 switch (pid)
346 {
347 case -1:
348 abort ();
349 case 0:
350 execl (exec, exec, "--ptraceme", NULL);
351 abort ();
352 default:
353 break;
354 }
355
356 /* Catch the main thread. Catch it first otherwise the /proc evaluation of
357 PID may have caught still ourselves before executing execl above. */
358 errno = 0;
359 int status;
360 pid_t got = waitpid (pid, &status, 0);
Max Filippovc801acf2015-05-04 20:17:52 +0300361 assert (errno == 0);
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100362 assert (got == pid);
363 assert (WIFSTOPPED (status));
364 // Main thread will signal SIGUSR2. Other thread will signal SIGUSR1.
365 assert (WSTOPSIG (status) == SIGUSR2);
366
367 /* Catch the spawned thread. Do not use __WCLONE as we could get racy
368 __WCLONE, probably despite pthread_create already had to be called the new
369 task is not yet alive enough for waitpid. */
370 pid_t pid2 = waitpid (-1, &status, __WALL);
Max Filippovc801acf2015-05-04 20:17:52 +0300371 assert (errno == 0);
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100372 assert (pid2 > 0);
373 assert (pid2 != pid);
374 assert (WIFSTOPPED (status));
375 // Main thread will signal SIGUSR2. Other thread will signal SIGUSR1.
376 assert (WSTOPSIG (status) == SIGUSR1);
377
378 Dwfl *dwfl = pid_to_dwfl (pid);
379 char *selfpathname;
380 int i = asprintf (&selfpathname, "/proc/%ld/exe", (long) pid);
381 assert (i > 0);
382 struct see_exec_module data;
383 ssize_t ssize = readlink (selfpathname, data.selfpath,
384 sizeof (data.selfpath));
385 free (selfpathname);
386 assert (ssize > 0 && ssize < (ssize_t) sizeof (data.selfpath));
387 data.selfpath[ssize] = '\0';
388 data.mod = NULL;
Mark Wielaard90084992016-08-25 17:17:23 +0200389 dwfl_getmodules (dwfl, see_exec_module, &data, 0);
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100390 assert (data.mod != NULL);
391 GElf_Addr loadbase;
392 Elf *elf = dwfl_module_getelf (data.mod, &loadbase);
393 GElf_Ehdr ehdr_mem, *ehdr = gelf_getehdr (elf, &ehdr_mem);
394 assert (ehdr != NULL);
395 /* It is false also on x86_64 with i386 inferior. */
Mark Wielaard90084992016-08-25 17:17:23 +0200396#ifndef RAISE_JMP_PATCHING
397 use_raise_jmp_patching = false;
398#else /* RAISE_JMP_PATCHING_ */
399 use_raise_jmp_patching = ehdr->e_machine == EM_X86_64;
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100400#endif /* __x86_64__ */
Josh Stone3d114102015-02-11 14:25:37 -0800401 void (*jmp) (void) = 0;
Mark Wielaard90084992016-08-25 17:17:23 +0200402 if (use_raise_jmp_patching)
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100403 {
404 // Find inferior symbol named "jmp".
405 int nsym = dwfl_module_getsymtab (data.mod);
406 int symi;
407 for (symi = 1; symi < nsym; ++symi)
408 {
409 GElf_Sym symbol;
410 const char *symbol_name = dwfl_module_getsym (data.mod, symi, &symbol, NULL);
411 if (symbol_name == NULL)
412 continue;
413 switch (GELF_ST_TYPE (symbol.st_info))
414 {
415 case STT_SECTION:
416 case STT_FILE:
417 case STT_TLS:
418 continue;
419 default:
420 if (strcmp (symbol_name, "jmp") != 0)
421 continue;
422 break;
423 }
424 /* LOADBASE is already applied here. */
425 jmp = (void (*) (void)) (uintptr_t) symbol.st_value;
426 break;
427 }
428 assert (symi < nsym);
429 prepare_thread (pid2, jmp);
430 }
431 dwfl_end (dwfl);
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100432 check_tid = pid2;
433 dwfl = pid_to_dwfl (pid);
434 dump (dwfl);
435 dwfl_end (dwfl);
436}
437
438#define OPT_BACKTRACE_EXEC 0x100
439
440static const struct argp_option options[] =
441 {
442 { "backtrace-exec", OPT_BACKTRACE_EXEC, "EXEC", 0, N_("Run executable"), 0 },
443 { NULL, 0, NULL, 0, NULL, 0 }
444 };
445
446
447static error_t
448parse_opt (int key, char *arg, struct argp_state *state)
449{
450 switch (key)
451 {
452 case ARGP_KEY_INIT:
453 state->child_inputs[0] = state->input;
454 break;
455
456 case OPT_BACKTRACE_EXEC:
457 exec_dump (arg);
458 exit (0);
459
460 default:
461 return ARGP_ERR_UNKNOWN;
462 }
463 return 0;
464}
465
466int
467main (int argc __attribute__ ((unused)), char **argv)
468{
469 /* We use no threads here which can interfere with handling a stream. */
470 __fsetlocking (stdin, FSETLOCKING_BYCALLER);
471 __fsetlocking (stdout, FSETLOCKING_BYCALLER);
472 __fsetlocking (stderr, FSETLOCKING_BYCALLER);
473
474 /* Set locale. */
475 (void) setlocale (LC_ALL, "");
476
477 elf_version (EV_CURRENT);
478
479 Dwfl *dwfl = NULL;
480 const struct argp_child argp_children[] =
481 {
482 { .argp = dwfl_standard_argp () },
483 { .argp = NULL }
484 };
485 const struct argp argp =
486 {
487 options, parse_opt, NULL, NULL, argp_children, NULL, NULL
488 };
489 (void) argp_parse (&argp, argc, argv, 0, NULL, &dwfl);
490 assert (dwfl != NULL);
Mark Wielaard14beac32014-06-11 15:14:23 +0200491 /* We want to make sure the dwfl was properly attached. */
492 if (dwfl_pid (dwfl) < 0)
493 error (2, 0, "dwfl_pid: %s", dwfl_errmsg (-1));
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100494 dump (dwfl);
495 dwfl_end (dwfl);
496 return 0;
497}
Kurt Roeckx02cefda2014-04-22 21:46:22 +0200498
499#endif /* ! __linux__ */
500