blob: 758dfed6c9fc9d9179465c2790b7bba7f0dc0beb [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
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100263report_pid (Dwfl *dwfl, pid_t pid)
264{
265 int result = dwfl_linux_proc_report (dwfl, pid);
266 if (result < 0)
267 error (2, 0, "dwfl_linux_proc_report: %s", dwfl_errmsg (-1));
268 else if (result > 0)
269 error (2, result, "dwfl_linux_proc_report");
270
271 if (dwfl_report_end (dwfl, NULL, NULL) != 0)
272 error (2, 0, "dwfl_report_end: %s", dwfl_errmsg (-1));
Mark Wielaard19108012013-12-30 22:00:57 +0100273
Mark Wielaard70c3a532014-01-04 23:28:33 +0100274 result = dwfl_linux_proc_attach (dwfl, pid, true);
Mark Wielaard19108012013-12-30 22:00:57 +0100275 if (result < 0)
276 error (2, 0, "dwfl_linux_proc_attach: %s", dwfl_errmsg (-1));
277 else if (result > 0)
278 error (2, result, "dwfl_linux_proc_attach");
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100279}
280
281static Dwfl *
282pid_to_dwfl (pid_t pid)
283{
284 static char *debuginfo_path;
285 static const Dwfl_Callbacks proc_callbacks =
286 {
287 .find_debuginfo = dwfl_standard_find_debuginfo,
288 .debuginfo_path = &debuginfo_path,
289
290 .find_elf = dwfl_linux_proc_find_elf,
291 };
292 Dwfl *dwfl = dwfl_begin (&proc_callbacks);
293 if (dwfl == NULL)
294 error (2, 0, "dwfl_begin: %s", dwfl_errmsg (-1));
295 report_pid (dwfl, pid);
296 return dwfl;
297}
298
299static void
300exec_dump (const char *exec)
301{
302 pid_t pid = fork ();
303 switch (pid)
304 {
305 case -1:
306 abort ();
307 case 0:
308 execl (exec, exec, "--ptraceme", NULL);
309 abort ();
310 default:
311 break;
312 }
313
314 /* Catch the main thread. Catch it first otherwise the /proc evaluation of
315 PID may have caught still ourselves before executing execl above. */
316 errno = 0;
317 int status;
318 pid_t got = waitpid (pid, &status, 0);
319 assert_perror (errno);
320 assert (got == pid);
321 assert (WIFSTOPPED (status));
322 // Main thread will signal SIGUSR2. Other thread will signal SIGUSR1.
323 assert (WSTOPSIG (status) == SIGUSR2);
324
325 /* Catch the spawned thread. Do not use __WCLONE as we could get racy
326 __WCLONE, probably despite pthread_create already had to be called the new
327 task is not yet alive enough for waitpid. */
328 pid_t pid2 = waitpid (-1, &status, __WALL);
329 assert_perror (errno);
330 assert (pid2 > 0);
331 assert (pid2 != pid);
332 assert (WIFSTOPPED (status));
333 // Main thread will signal SIGUSR2. Other thread will signal SIGUSR1.
334 assert (WSTOPSIG (status) == SIGUSR1);
335
336 Dwfl *dwfl = pid_to_dwfl (pid);
337 char *selfpathname;
338 int i = asprintf (&selfpathname, "/proc/%ld/exe", (long) pid);
339 assert (i > 0);
340 struct see_exec_module data;
341 ssize_t ssize = readlink (selfpathname, data.selfpath,
342 sizeof (data.selfpath));
343 free (selfpathname);
344 assert (ssize > 0 && ssize < (ssize_t) sizeof (data.selfpath));
345 data.selfpath[ssize] = '\0';
346 data.mod = NULL;
347 ptrdiff_t ptrdiff = dwfl_getmodules (dwfl, see_exec_module, &data, 0);
348 assert (ptrdiff == 0);
349 assert (data.mod != NULL);
350 GElf_Addr loadbase;
351 Elf *elf = dwfl_module_getelf (data.mod, &loadbase);
352 GElf_Ehdr ehdr_mem, *ehdr = gelf_getehdr (elf, &ehdr_mem);
353 assert (ehdr != NULL);
354 /* It is false also on x86_64 with i386 inferior. */
355#ifndef __x86_64__
356 is_x86_64_native = false;
357#else /* __x86_64__ */
358 is_x86_64_native = ehdr->e_ident[EI_CLASS] == ELFCLASS64;
359#endif /* __x86_64__ */
360 void (*jmp) (void);
361 if (is_x86_64_native)
362 {
363 // Find inferior symbol named "jmp".
364 int nsym = dwfl_module_getsymtab (data.mod);
365 int symi;
366 for (symi = 1; symi < nsym; ++symi)
367 {
368 GElf_Sym symbol;
369 const char *symbol_name = dwfl_module_getsym (data.mod, symi, &symbol, NULL);
370 if (symbol_name == NULL)
371 continue;
372 switch (GELF_ST_TYPE (symbol.st_info))
373 {
374 case STT_SECTION:
375 case STT_FILE:
376 case STT_TLS:
377 continue;
378 default:
379 if (strcmp (symbol_name, "jmp") != 0)
380 continue;
381 break;
382 }
383 /* LOADBASE is already applied here. */
384 jmp = (void (*) (void)) (uintptr_t) symbol.st_value;
385 break;
386 }
387 assert (symi < nsym);
388 prepare_thread (pid2, jmp);
389 }
390 dwfl_end (dwfl);
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100391 check_tid = pid2;
392 dwfl = pid_to_dwfl (pid);
393 dump (dwfl);
394 dwfl_end (dwfl);
395}
396
397#define OPT_BACKTRACE_EXEC 0x100
398
399static const struct argp_option options[] =
400 {
401 { "backtrace-exec", OPT_BACKTRACE_EXEC, "EXEC", 0, N_("Run executable"), 0 },
402 { NULL, 0, NULL, 0, NULL, 0 }
403 };
404
405
406static error_t
407parse_opt (int key, char *arg, struct argp_state *state)
408{
409 switch (key)
410 {
411 case ARGP_KEY_INIT:
412 state->child_inputs[0] = state->input;
413 break;
414
415 case OPT_BACKTRACE_EXEC:
416 exec_dump (arg);
417 exit (0);
418
419 default:
420 return ARGP_ERR_UNKNOWN;
421 }
422 return 0;
423}
424
425int
426main (int argc __attribute__ ((unused)), char **argv)
427{
428 /* We use no threads here which can interfere with handling a stream. */
429 __fsetlocking (stdin, FSETLOCKING_BYCALLER);
430 __fsetlocking (stdout, FSETLOCKING_BYCALLER);
431 __fsetlocking (stderr, FSETLOCKING_BYCALLER);
432
433 /* Set locale. */
434 (void) setlocale (LC_ALL, "");
435
436 elf_version (EV_CURRENT);
437
438 Dwfl *dwfl = NULL;
439 const struct argp_child argp_children[] =
440 {
441 { .argp = dwfl_standard_argp () },
442 { .argp = NULL }
443 };
444 const struct argp argp =
445 {
446 options, parse_opt, NULL, NULL, argp_children, NULL, NULL
447 };
448 (void) argp_parse (&argp, argc, argv, 0, NULL, &dwfl);
449 assert (dwfl != NULL);
450 dump (dwfl);
451 dwfl_end (dwfl);
452 return 0;
453}