blob: 7ff826cd8e4e710f42f755f6f1f8215b7e360717 [file] [log] [blame]
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +01001/* Test program for unwinding of complicated DWARF expressions.
Mark Wielaard0a35e3a2015-03-13 23:51:40 +01002 Copyright (C) 2013, 2015 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>
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +010020#include <inttypes.h>
21#include <stdio_ext.h>
22#include <locale.h>
23#include <errno.h>
Mark Wielaard0a35e3a2015-03-13 23:51:40 +010024#include <error.h>
25#include <unistd.h>
Mark Wielaard0a35e3a2015-03-13 23:51:40 +010026#include <sys/types.h>
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +010027#include ELFUTILS_HEADER(dwfl)
28
Kurt Roeckx02cefda2014-04-22 21:46:22 +020029#ifndef __linux__
30
31int
32main (int argc __attribute__ ((unused)), char **argv)
33{
34 fprintf (stderr, "%s: Unwinding not supported for this architecture\n",
35 argv[0]);
36 return 77;
37}
38
39#else /* __linux__ */
Ulf Hermann9c1509a2017-04-20 15:37:04 +020040#include <sys/ptrace.h>
Mark Wielaard4482d002018-02-15 14:44:18 +010041#include <sys/wait.h>
42#include <signal.h>
Kurt Roeckx02cefda2014-04-22 21:46:22 +020043
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +010044#define main cleanup_13_main
45#include "cleanup-13.c"
46#undef main
47
48static void
49report_pid (Dwfl *dwfl, pid_t pid)
50{
51 int result = dwfl_linux_proc_report (dwfl, pid);
52 if (result < 0)
53 error (2, 0, "dwfl_linux_proc_report: %s", dwfl_errmsg (-1));
54 else if (result > 0)
55 error (2, result, "dwfl_linux_proc_report");
56
57 if (dwfl_report_end (dwfl, NULL, NULL) != 0)
58 error (2, 0, "dwfl_report_end: %s", dwfl_errmsg (-1));
Mark Wielaard19108012013-12-30 22:00:57 +010059
Mark Wielaard70c3a532014-01-04 23:28:33 +010060 result = dwfl_linux_proc_attach (dwfl, pid, true);
Mark Wielaard19108012013-12-30 22:00:57 +010061 if (result < 0)
62 error (2, 0, "dwfl_linux_proc_attach: %s", dwfl_errmsg (-1));
63 else if (result > 0)
64 error (2, result, "dwfl_linux_proc_attach");
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +010065}
66
67static Dwfl *
68pid_to_dwfl (pid_t pid)
69{
70 static char *debuginfo_path;
71 static const Dwfl_Callbacks proc_callbacks =
72 {
73 .find_debuginfo = dwfl_standard_find_debuginfo,
74 .debuginfo_path = &debuginfo_path,
75
76 .find_elf = dwfl_linux_proc_find_elf,
77 };
78 Dwfl *dwfl = dwfl_begin (&proc_callbacks);
79 if (dwfl == NULL)
80 error (2, 0, "dwfl_begin: %s", dwfl_errmsg (-1));
81 report_pid (dwfl, pid);
82 return dwfl;
83}
84
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +010085static int
86frame_callback (Dwfl_Frame *state, void *frame_arg)
87{
88 Dwarf_Addr pc;
89 bool isactivation;
90 if (! dwfl_frame_pc (state, &pc, &isactivation))
91 {
92 error (0, 0, "%s", dwfl_errmsg (-1));
93 return DWARF_CB_ABORT;
94 }
95 Dwarf_Addr pc_adjusted = pc - (isactivation ? 0 : 1);
96
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;
102 if (mod)
103 symname = dwfl_module_addrname (mod, pc_adjusted);
104
105 printf ("%#" PRIx64 "\t%s\n", (uint64_t) pc, symname);
106
107 if (symname && (strcmp (symname, "main") == 0
108 || strcmp (symname, ".main") == 0))
109 {
110 kill (dwfl_pid (dwfl), SIGKILL);
111 exit (0);
112 }
113
114 return DWARF_CB_OK;
115}
116
117static int
118thread_callback (Dwfl_Thread *thread, void *thread_arg)
119{
120 dwfl_thread_getframes (thread, frame_callback, NULL);
Mark Wielaard0a35e3a2015-03-13 23:51:40 +0100121 /* frame_callback shall exit (0) on success. */
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100122 error (1, 0, "dwfl_thread_getframes: %s", dwfl_errmsg (-1));
Mark Wielaard0a35e3a2015-03-13 23:51:40 +0100123 return DWARF_CB_ABORT;
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100124}
125
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100126int
127main (int argc __attribute__ ((unused)), char **argv)
128{
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);
133
134 /* Set locale. */
135 (void) setlocale (LC_ALL, "");
136
137 elf_version (EV_CURRENT);
138
139 pid_t pid = fork ();
140 switch (pid)
141 {
142 case -1:
143 abort ();
144 case 0:;
145 long l = ptrace (PTRACE_TRACEME, 0, NULL, NULL);
Max Filippovc801acf2015-05-04 20:17:52 +0300146 assert (errno == 0);
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100147 assert (l == 0);
148 cleanup_13_main ();
149 abort ();
150 default:
151 break;
152 }
153
154 errno = 0;
155 int status;
156 pid_t got = waitpid (pid, &status, 0);
Max Filippovc801acf2015-05-04 20:17:52 +0300157 assert (errno == 0);
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100158 assert (got == pid);
159 assert (WIFSTOPPED (status));
160 assert (WSTOPSIG (status) == SIGABRT);
161
Jan Kratochvil8ae9bc92013-12-02 20:54:28 +0100162 Dwfl *dwfl = pid_to_dwfl (pid);
163 dwfl_getthreads (dwfl, thread_callback, NULL);
164
165 /* There is an exit (0) call if we find the "main" frame, */
166 error (1, 0, "dwfl_getthreads: %s", dwfl_errmsg (-1));
167}
Kurt Roeckx02cefda2014-04-22 21:46:22 +0200168
169#endif /* ! __linux__ */
170