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