1 /* Test program for unwinding of complicated DWARF expressions.
2 Copyright (C) 2013, 2015 Red Hat, Inc.
3 This file is part of elfutils.
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.
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.
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/>. */
21 #include <stdio_ext.h>
26 #include <sys/types.h>
27 #include ELFUTILS_HEADER(dwfl)
32 main (int argc __attribute__ ((unused)), char **argv)
34 fprintf (stderr, "%s: Unwinding not supported for this architecture\n",
40 #include <sys/ptrace.h>
44 #define main cleanup_13_main
45 #include "cleanup-13.c"
49 report_pid (Dwfl *dwfl, pid_t pid)
51 int result = dwfl_linux_proc_report (dwfl, pid);
53 error (2, 0, "dwfl_linux_proc_report: %s", dwfl_errmsg (-1));
55 error (2, result, "dwfl_linux_proc_report");
57 if (dwfl_report_end (dwfl, NULL, NULL) != 0)
58 error (2, 0, "dwfl_report_end: %s", dwfl_errmsg (-1));
60 result = dwfl_linux_proc_attach (dwfl, pid, true);
62 error (2, 0, "dwfl_linux_proc_attach: %s", dwfl_errmsg (-1));
64 error (2, result, "dwfl_linux_proc_attach");
68 pid_to_dwfl (pid_t pid)
70 static char *debuginfo_path;
71 static const Dwfl_Callbacks proc_callbacks =
73 .find_debuginfo = dwfl_standard_find_debuginfo,
74 .debuginfo_path = &debuginfo_path,
76 .find_elf = dwfl_linux_proc_find_elf,
78 Dwfl *dwfl = dwfl_begin (&proc_callbacks);
80 error (2, 0, "dwfl_begin: %s", dwfl_errmsg (-1));
81 report_pid (dwfl, pid);
86 frame_callback (Dwfl_Frame *state, void *frame_arg)
90 if (! dwfl_frame_pc (state, &pc, &isactivation))
92 error (0, 0, "%s", dwfl_errmsg (-1));
93 return DWARF_CB_ABORT;
95 Dwarf_Addr pc_adjusted = pc - (isactivation ? 0 : 1);
97 /* Get PC->SYMNAME. */
98 Dwfl_Thread *thread = dwfl_frame_thread (state);
99 Dwfl *dwfl = dwfl_thread_dwfl (thread);
100 Dwfl_Module *mod = dwfl_addrmodule (dwfl, pc_adjusted);
101 const char *symname = NULL;
103 symname = dwfl_module_addrname (mod, pc_adjusted);
105 printf ("%#" PRIx64 "\t%s\n", (uint64_t) pc, symname);
107 if (symname && (strcmp (symname, "main") == 0
108 || strcmp (symname, ".main") == 0))
110 kill (dwfl_pid (dwfl), SIGKILL);
118 thread_callback (Dwfl_Thread *thread, void *thread_arg)
120 dwfl_thread_getframes (thread, frame_callback, NULL);
121 /* frame_callback shall exit (0) on success. */
122 error (1, 0, "dwfl_thread_getframes: %s", dwfl_errmsg (-1));
123 return DWARF_CB_ABORT;
127 main (int argc __attribute__ ((unused)), char **argv)
129 /* We use no threads here which can interfere with handling a stream. */
130 __fsetlocking (stdin, FSETLOCKING_BYCALLER);
131 __fsetlocking (stdout, FSETLOCKING_BYCALLER);
132 __fsetlocking (stderr, FSETLOCKING_BYCALLER);
135 (void) setlocale (LC_ALL, "");
137 elf_version (EV_CURRENT);
145 long l = ptrace (PTRACE_TRACEME, 0, NULL, NULL);
156 pid_t got = waitpid (pid, &status, 0);
159 assert (WIFSTOPPED (status));
160 assert (WSTOPSIG (status) == SIGABRT);
162 Dwfl *dwfl = pid_to_dwfl (pid);
163 dwfl_getthreads (dwfl, thread_callback, NULL);
165 /* There is an exit (0) call if we find the "main" frame, */
166 error (1, 0, "dwfl_getthreads: %s", dwfl_errmsg (-1));
169 #endif /* ! __linux__ */