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