• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Test program for unwinding of complicated DWARF expressions.
2    Copyright (C) 2013, 2015 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 <signal.h>
21 #include <inttypes.h>
22 #include <stdio_ext.h>
23 #include <locale.h>
24 #include <errno.h>
25 #include <error.h>
26 #include <unistd.h>
27 #include <sys/ptrace.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include ELFUTILS_HEADER(dwfl)
31 
32 #ifndef __linux__
33 
34 int
main(int argc,char ** argv)35 main (int argc __attribute__ ((unused)), char **argv)
36 {
37   fprintf (stderr, "%s: Unwinding not supported for this architecture\n",
38            argv[0]);
39   return 77;
40 }
41 
42 #else /* __linux__ */
43 
44 #define main cleanup_13_main
45 #include "cleanup-13.c"
46 #undef main
47 
48 static void
report_pid(Dwfl * dwfl,pid_t pid)49 report_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));
59 
60   result = dwfl_linux_proc_attach (dwfl, pid, true);
61   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");
65 }
66 
67 static Dwfl *
pid_to_dwfl(pid_t pid)68 pid_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 
85 static int
frame_callback(Dwfl_Frame * state,void * frame_arg)86 frame_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 
117 static int
thread_callback(Dwfl_Thread * thread,void * thread_arg)118 thread_callback (Dwfl_Thread *thread, void *thread_arg)
119 {
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;
124 }
125 
126 int
main(int argc,char ** argv)127 main (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);
146       assert (errno == 0);
147       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);
157   assert (errno == 0);
158   assert (got == pid);
159   assert (WIFSTOPPED (status));
160   assert (WSTOPSIG (status) == SIGABRT);
161 
162   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 }
168 
169 #endif /* ! __linux__ */
170 
171