blob: 1d3948ebf3773d344a82ec9859ad24aa7a4e6cf1 [file] [log] [blame]
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +01001/* Test program for unwinding of frames.
Mark Wielaard14beac32014-06-11 15:14:23 +02002 Copyright (C) 2013, 2014 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>
30#include <sys/resource.h>
31#include <sys/ptrace.h>
32#include <signal.h>
33#include <sys/types.h>
34#include <sys/wait.h>
35#include <sys/user.h>
36#include <fcntl.h>
37#include <string.h>
38#include <argp.h>
39#include ELFUTILS_HEADER(dwfl)
40
Kurt Roeckx02cefda2014-04-22 21:46:22 +020041#ifndef __linux__
42
43int
44main (int argc __attribute__ ((unused)), char **argv)
45{
46 fprintf (stderr, "%s: Unwinding not supported for this architecture\n",
47 argv[0]);
48 return 77;
49}
50
51#else /* __linux__ */
52
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +010053static int
54dump_modules (Dwfl_Module *mod, void **userdata __attribute__ ((unused)),
55 const char *name, Dwarf_Addr start,
56 void *arg __attribute__ ((unused)))
57{
58 Dwarf_Addr end;
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,
61 name);
62 return DWARF_CB_OK;
63}
64
65static bool is_x86_64_native;
66static pid_t check_tid;
67
68static void
69callback_verify (pid_t tid, unsigned frameno, Dwarf_Addr pc,
70 const char *symname, Dwfl *dwfl)
71{
72 static bool seen_main = false;
73 if (symname && *symname == '.')
74 symname++;
75 if (symname && strcmp (symname, "main") == 0)
76 seen_main = true;
77 if (pc == 0)
78 {
79 assert (seen_main);
80 return;
81 }
82 if (check_tid == 0)
83 check_tid = tid;
84 if (tid != check_tid)
85 {
86 // For the main thread we are only interested if we can unwind till
87 // we see the "main" symbol.
88 return;
89 }
90 Dwfl_Module *mod;
91 static bool reduce_frameno = false;
92 if (reduce_frameno)
93 frameno--;
94 if (! is_x86_64_native && frameno >= 2)
95 frameno += 2;
96 const char *symname2 = NULL;
97 switch (frameno)
98 {
99 case 0:
100 if (! reduce_frameno && symname
Mark Wielaard1986c172014-06-14 01:09:17 +0200101 && (strcmp (symname, "__kernel_vsyscall") == 0
102 || strcmp (symname, "__libc_do_syscall") == 0))
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100103 reduce_frameno = true;
104 else
105 assert (symname && strcmp (symname, "raise") == 0);
106 break;
107 case 1:
108 assert (symname != NULL && strcmp (symname, "sigusr2") == 0);
109 break;
110 case 2: // x86_64 only
111 /* __restore_rt - glibc maybe does not have to have this symbol. */
112 break;
113 case 3: // x86_64 only
114 if (is_x86_64_native)
115 {
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);
119 if (mod)
120 symname2 = dwfl_module_addrname (mod, pc - 1);
121 assert (symname2 == NULL || strcmp (symname2, "jmp") != 0);
122 break;
123 }
124 /* PASSTHRU */
125 case 4:
126 assert (symname != NULL && strcmp (symname, "stdarg") == 0);
127 break;
128 case 5:
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);
132 if (mod)
133 symname2 = dwfl_module_addrname (mod, pc);
Mark Wielaardb6ef1ce2013-12-21 19:39:19 +0100134
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);
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100141 break;
142 }
143}
144
145static int
146frame_callback (Dwfl_Frame *state, void *frame_arg)
147{
148 int *framenop = frame_arg;
149 Dwarf_Addr pc;
150 bool isactivation;
151 if (! dwfl_frame_pc (state, &pc, &isactivation))
152 {
153 error (0, 0, "%s", dwfl_errmsg (-1));
154 return DWARF_CB_ABORT;
155 }
156 Dwarf_Addr pc_adjusted = pc - (isactivation ? 0 : 1);
157
158 /* Get PC->SYMNAME. */
159 Dwfl_Thread *thread = dwfl_frame_thread (state);
160 Dwfl *dwfl = dwfl_thread_dwfl (thread);
161 Dwfl_Module *mod = dwfl_addrmodule (dwfl, pc_adjusted);
162 const char *symname = NULL;
163 if (mod)
164 symname = dwfl_module_addrname (mod, pc_adjusted);
165
166 printf ("#%2d %#" PRIx64 "%4s\t%s\n", *framenop, (uint64_t) pc,
167 ! isactivation ? "- 1" : "", symname);
168 pid_t tid = dwfl_thread_tid (thread);
169 callback_verify (tid, *framenop, pc, symname, dwfl);
170 (*framenop)++;
171
172 return DWARF_CB_OK;
173}
174
175static int
176thread_callback (Dwfl_Thread *thread, void *thread_arg __attribute__((unused)))
177{
178 printf ("TID %ld:\n", (long) dwfl_thread_tid (thread));
179 int frameno = 0;
180 switch (dwfl_thread_getframes (thread, frame_callback, &frameno))
181 {
182 case 0:
183 break;
184 case DWARF_CB_ABORT:
185 return DWARF_CB_ABORT;
186 case -1:
187 error (0, 0, "dwfl_thread_getframes: %s", dwfl_errmsg (-1));
188 /* All platforms do not have yet proper unwind termination. */
189 break;
190 default:
191 abort ();
192 }
193 return DWARF_CB_OK;
194}
195
196static void
197dump (Dwfl *dwfl)
198{
199 ptrdiff_t ptrdiff = dwfl_getmodules (dwfl, dump_modules, NULL, 0);
200 assert (ptrdiff == 0);
201 bool err = false;
202 switch (dwfl_getthreads (dwfl, thread_callback, NULL))
203 {
204 case 0:
205 break;
206 case DWARF_CB_ABORT:
207 err = true;
208 break;
209 case -1:
210 error (0, 0, "dwfl_getthreads: %s", dwfl_errmsg (-1));
211 err = true;
212 break;
213 default:
214 abort ();
215 }
216 callback_verify (0, 0, 0, NULL, dwfl);
217 if (err)
218 exit (EXIT_FAILURE);
219}
220
221struct see_exec_module
222{
223 Dwfl_Module *mod;
224 char selfpath[PATH_MAX + 1];
225};
226
227static int
228see_exec_module (Dwfl_Module *mod, void **userdata __attribute__ ((unused)),
229 const char *name __attribute__ ((unused)),
230 Dwarf_Addr start __attribute__ ((unused)), void *arg)
231{
232 struct see_exec_module *data = arg;
233 if (strcmp (name, data->selfpath) != 0)
234 return DWARF_CB_OK;
235 assert (data->mod == NULL);
236 data->mod = mod;
237 return DWARF_CB_OK;
238}
239
240/* On x86_64 only:
241 PC will get changed to function 'jmp' by backtrace.c function
242 prepare_thread. Then SIGUSR2 will be signalled to backtrace-child
243 which will invoke function sigusr2.
244 This is all done so that signal interrupts execution of the very first
245 instruction of a function. Properly handled unwind should not slip into
246 the previous unrelated function. */
247
248static void
249prepare_thread (pid_t pid2 __attribute__ ((unused)),
250 void (*jmp) (void) __attribute__ ((unused)))
251{
252#ifndef __x86_64__
253 abort ();
254#else /* x86_64 */
255 long l;
256 errno = 0;
257 l = ptrace (PTRACE_POKEUSER, pid2,
258 (void *) (intptr_t) offsetof (struct user_regs_struct, rip), jmp);
259 assert_perror (errno);
260 assert (l == 0);
261 l = ptrace (PTRACE_CONT, pid2, NULL, (void *) (intptr_t) SIGUSR2);
262 int status;
263 pid_t got = waitpid (pid2, &status, __WALL);
264 assert_perror (errno);
265 assert (got == pid2);
266 assert (WIFSTOPPED (status));
267 assert (WSTOPSIG (status) == SIGUSR1);
268#endif /* __x86_64__ */
269}
270
271#include <asm/unistd.h>
272#include <unistd.h>
273#define tgkill(pid, tid, sig) syscall (__NR_tgkill, (pid), (tid), (sig))
274
275static void
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100276report_pid (Dwfl *dwfl, pid_t pid)
277{
278 int result = dwfl_linux_proc_report (dwfl, pid);
279 if (result < 0)
280 error (2, 0, "dwfl_linux_proc_report: %s", dwfl_errmsg (-1));
281 else if (result > 0)
282 error (2, result, "dwfl_linux_proc_report");
283
284 if (dwfl_report_end (dwfl, NULL, NULL) != 0)
285 error (2, 0, "dwfl_report_end: %s", dwfl_errmsg (-1));
Mark Wielaard19108012013-12-30 22:00:57 +0100286
Mark Wielaard70c3a532014-01-04 23:28:33 +0100287 result = dwfl_linux_proc_attach (dwfl, pid, true);
Mark Wielaard19108012013-12-30 22:00:57 +0100288 if (result < 0)
289 error (2, 0, "dwfl_linux_proc_attach: %s", dwfl_errmsg (-1));
290 else if (result > 0)
291 error (2, result, "dwfl_linux_proc_attach");
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100292}
293
294static Dwfl *
295pid_to_dwfl (pid_t pid)
296{
297 static char *debuginfo_path;
298 static const Dwfl_Callbacks proc_callbacks =
299 {
300 .find_debuginfo = dwfl_standard_find_debuginfo,
301 .debuginfo_path = &debuginfo_path,
302
303 .find_elf = dwfl_linux_proc_find_elf,
304 };
305 Dwfl *dwfl = dwfl_begin (&proc_callbacks);
306 if (dwfl == NULL)
307 error (2, 0, "dwfl_begin: %s", dwfl_errmsg (-1));
308 report_pid (dwfl, pid);
309 return dwfl;
310}
311
312static void
313exec_dump (const char *exec)
314{
315 pid_t pid = fork ();
316 switch (pid)
317 {
318 case -1:
319 abort ();
320 case 0:
321 execl (exec, exec, "--ptraceme", NULL);
322 abort ();
323 default:
324 break;
325 }
326
327 /* Catch the main thread. Catch it first otherwise the /proc evaluation of
328 PID may have caught still ourselves before executing execl above. */
329 errno = 0;
330 int status;
331 pid_t got = waitpid (pid, &status, 0);
332 assert_perror (errno);
333 assert (got == pid);
334 assert (WIFSTOPPED (status));
335 // Main thread will signal SIGUSR2. Other thread will signal SIGUSR1.
336 assert (WSTOPSIG (status) == SIGUSR2);
337
338 /* Catch the spawned thread. Do not use __WCLONE as we could get racy
339 __WCLONE, probably despite pthread_create already had to be called the new
340 task is not yet alive enough for waitpid. */
341 pid_t pid2 = waitpid (-1, &status, __WALL);
342 assert_perror (errno);
343 assert (pid2 > 0);
344 assert (pid2 != pid);
345 assert (WIFSTOPPED (status));
346 // Main thread will signal SIGUSR2. Other thread will signal SIGUSR1.
347 assert (WSTOPSIG (status) == SIGUSR1);
348
349 Dwfl *dwfl = pid_to_dwfl (pid);
350 char *selfpathname;
351 int i = asprintf (&selfpathname, "/proc/%ld/exe", (long) pid);
352 assert (i > 0);
353 struct see_exec_module data;
354 ssize_t ssize = readlink (selfpathname, data.selfpath,
355 sizeof (data.selfpath));
356 free (selfpathname);
357 assert (ssize > 0 && ssize < (ssize_t) sizeof (data.selfpath));
358 data.selfpath[ssize] = '\0';
359 data.mod = NULL;
360 ptrdiff_t ptrdiff = dwfl_getmodules (dwfl, see_exec_module, &data, 0);
361 assert (ptrdiff == 0);
362 assert (data.mod != NULL);
363 GElf_Addr loadbase;
364 Elf *elf = dwfl_module_getelf (data.mod, &loadbase);
365 GElf_Ehdr ehdr_mem, *ehdr = gelf_getehdr (elf, &ehdr_mem);
366 assert (ehdr != NULL);
367 /* It is false also on x86_64 with i386 inferior. */
368#ifndef __x86_64__
369 is_x86_64_native = false;
370#else /* __x86_64__ */
371 is_x86_64_native = ehdr->e_ident[EI_CLASS] == ELFCLASS64;
372#endif /* __x86_64__ */
373 void (*jmp) (void);
374 if (is_x86_64_native)
375 {
376 // Find inferior symbol named "jmp".
377 int nsym = dwfl_module_getsymtab (data.mod);
378 int symi;
379 for (symi = 1; symi < nsym; ++symi)
380 {
381 GElf_Sym symbol;
382 const char *symbol_name = dwfl_module_getsym (data.mod, symi, &symbol, NULL);
383 if (symbol_name == NULL)
384 continue;
385 switch (GELF_ST_TYPE (symbol.st_info))
386 {
387 case STT_SECTION:
388 case STT_FILE:
389 case STT_TLS:
390 continue;
391 default:
392 if (strcmp (symbol_name, "jmp") != 0)
393 continue;
394 break;
395 }
396 /* LOADBASE is already applied here. */
397 jmp = (void (*) (void)) (uintptr_t) symbol.st_value;
398 break;
399 }
400 assert (symi < nsym);
401 prepare_thread (pid2, jmp);
402 }
403 dwfl_end (dwfl);
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100404 check_tid = pid2;
405 dwfl = pid_to_dwfl (pid);
406 dump (dwfl);
407 dwfl_end (dwfl);
408}
409
410#define OPT_BACKTRACE_EXEC 0x100
411
412static const struct argp_option options[] =
413 {
414 { "backtrace-exec", OPT_BACKTRACE_EXEC, "EXEC", 0, N_("Run executable"), 0 },
415 { NULL, 0, NULL, 0, NULL, 0 }
416 };
417
418
419static error_t
420parse_opt (int key, char *arg, struct argp_state *state)
421{
422 switch (key)
423 {
424 case ARGP_KEY_INIT:
425 state->child_inputs[0] = state->input;
426 break;
427
428 case OPT_BACKTRACE_EXEC:
429 exec_dump (arg);
430 exit (0);
431
432 default:
433 return ARGP_ERR_UNKNOWN;
434 }
435 return 0;
436}
437
438int
439main (int argc __attribute__ ((unused)), char **argv)
440{
441 /* We use no threads here which can interfere with handling a stream. */
442 __fsetlocking (stdin, FSETLOCKING_BYCALLER);
443 __fsetlocking (stdout, FSETLOCKING_BYCALLER);
444 __fsetlocking (stderr, FSETLOCKING_BYCALLER);
445
446 /* Set locale. */
447 (void) setlocale (LC_ALL, "");
448
449 elf_version (EV_CURRENT);
450
451 Dwfl *dwfl = NULL;
452 const struct argp_child argp_children[] =
453 {
454 { .argp = dwfl_standard_argp () },
455 { .argp = NULL }
456 };
457 const struct argp argp =
458 {
459 options, parse_opt, NULL, NULL, argp_children, NULL, NULL
460 };
461 (void) argp_parse (&argp, argc, argv, 0, NULL, &dwfl);
462 assert (dwfl != NULL);
Mark Wielaard14beac32014-06-11 15:14:23 +0200463 /* We want to make sure the dwfl was properly attached. */
464 if (dwfl_pid (dwfl) < 0)
465 error (2, 0, "dwfl_pid: %s", dwfl_errmsg (-1));
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100466 dump (dwfl);
467 dwfl_end (dwfl);
468 return 0;
469}
Kurt Roeckx02cefda2014-04-22 21:46:22 +0200470
471#endif /* ! __linux__ */
472