1 | /* |
---|
2 | * Copyright (c) 2018, Chris Johns <chrisj@rtems.org> |
---|
3 | * |
---|
4 | * Permission to use, copy, modify, and/or distribute this software for any |
---|
5 | * purpose with or without fee is hereby granted, provided that the above |
---|
6 | * copyright notice and this permission notice appear in all copies. |
---|
7 | * |
---|
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
---|
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
---|
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
---|
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
15 | */ |
---|
16 | /** |
---|
17 | * @file |
---|
18 | * |
---|
19 | * @ingroup rtems_rld |
---|
20 | * |
---|
21 | * @brief RTEMS Address to Line is a version of the classic addr2line |
---|
22 | * utility to test the DWARF info support in the RTEMS Toolkit. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #if HAVE_CONFIG_H |
---|
27 | #include "config.h" |
---|
28 | #endif |
---|
29 | |
---|
30 | #include <iomanip> |
---|
31 | #include <iostream> |
---|
32 | |
---|
33 | #include <signal.h> |
---|
34 | #include <stdlib.h> |
---|
35 | #include <string.h> |
---|
36 | #include <unistd.h> |
---|
37 | |
---|
38 | #include <getopt.h> |
---|
39 | |
---|
40 | #include <rld.h> |
---|
41 | #include <rld-dwarf.h> |
---|
42 | #include <rld-files.h> |
---|
43 | #include <rld-rtems.h> |
---|
44 | |
---|
45 | #ifndef HAVE_KILL |
---|
46 | #define kill(p,s) raise(s) |
---|
47 | #endif |
---|
48 | |
---|
49 | /** |
---|
50 | * RTEMS Symbols options. |
---|
51 | */ |
---|
52 | static struct option rld_opts[] = { |
---|
53 | { "help", no_argument, NULL, 'h' }, |
---|
54 | { "version", no_argument, NULL, 'V' }, |
---|
55 | { "verbose", no_argument, NULL, 'v' }, |
---|
56 | { "executable", required_argument, NULL, 'e' }, |
---|
57 | { "functions" , no_argument, NULL, 'f' }, |
---|
58 | { "addresses", no_argument, NULL, 'a' }, |
---|
59 | { "pretty-print", no_argument, NULL, 'p' }, |
---|
60 | { "basenames", no_argument, NULL, 's' }, |
---|
61 | { NULL, 0, NULL, 0 } |
---|
62 | }; |
---|
63 | |
---|
64 | void |
---|
65 | usage (int exit_code) |
---|
66 | { |
---|
67 | std::cout << "rtems-addr2line [options] addresses" << std::endl |
---|
68 | << "Options and arguments:" << std::endl |
---|
69 | << " -h : help (also --help)" << std::endl |
---|
70 | << " -V : print version number and exit (also --version)" << std::endl |
---|
71 | << " -v : verbose (trace import parts), can supply multiple times" << std::endl |
---|
72 | << " to increase verbosity (also --verbose)" << std::endl |
---|
73 | << " -e : executable (also --executable)" << std::endl |
---|
74 | << " -f : show function names (also --functions)" << std::endl |
---|
75 | << " -a : show addresses (also --addresses)" << std::endl |
---|
76 | << " -p : human readable format (also --pretty-print)" << std::endl |
---|
77 | << " -s : Strip directory paths (also --basenames)" << std::endl; |
---|
78 | ::exit (exit_code); |
---|
79 | } |
---|
80 | |
---|
81 | static void |
---|
82 | fatal_signal (int signum) |
---|
83 | { |
---|
84 | signal (signum, SIG_DFL); |
---|
85 | |
---|
86 | /* |
---|
87 | * Get the same signal again, this time not handled, so its normal effect |
---|
88 | * occurs. |
---|
89 | */ |
---|
90 | kill (getpid (), signum); |
---|
91 | } |
---|
92 | |
---|
93 | static void |
---|
94 | setup_signals (void) |
---|
95 | { |
---|
96 | if (signal (SIGINT, SIG_IGN) != SIG_IGN) |
---|
97 | signal (SIGINT, fatal_signal); |
---|
98 | #ifdef SIGHUP |
---|
99 | if (signal (SIGHUP, SIG_IGN) != SIG_IGN) |
---|
100 | signal (SIGHUP, fatal_signal); |
---|
101 | #endif |
---|
102 | if (signal (SIGTERM, SIG_IGN) != SIG_IGN) |
---|
103 | signal (SIGTERM, fatal_signal); |
---|
104 | #ifdef SIGPIPE |
---|
105 | if (signal (SIGPIPE, SIG_IGN) != SIG_IGN) |
---|
106 | signal (SIGPIPE, fatal_signal); |
---|
107 | #endif |
---|
108 | #ifdef SIGCHLD |
---|
109 | signal (SIGCHLD, SIG_DFL); |
---|
110 | #endif |
---|
111 | } |
---|
112 | |
---|
113 | void |
---|
114 | unhandled_exception (void) |
---|
115 | { |
---|
116 | std::cerr << "error: exception handling error, please report" << std::endl; |
---|
117 | exit (13); |
---|
118 | } |
---|
119 | |
---|
120 | int |
---|
121 | main (int argc, char* argv[]) |
---|
122 | { |
---|
123 | int ec = 0; |
---|
124 | |
---|
125 | setup_signals (); |
---|
126 | |
---|
127 | std::set_terminate(unhandled_exception); |
---|
128 | |
---|
129 | try |
---|
130 | { |
---|
131 | std::string exe_name = "a.out"; |
---|
132 | bool show_functions = false; |
---|
133 | bool show_addresses = false; |
---|
134 | bool pretty_print = false; |
---|
135 | bool show_basenames = false; |
---|
136 | |
---|
137 | rld::set_cmdline (argc, argv); |
---|
138 | |
---|
139 | while (true) |
---|
140 | { |
---|
141 | int opt = ::getopt_long (argc, argv, "hvVe:faps", rld_opts, NULL); |
---|
142 | if (opt < 0) |
---|
143 | break; |
---|
144 | |
---|
145 | switch (opt) |
---|
146 | { |
---|
147 | case 'V': |
---|
148 | std::cout << "rtems-addr2line (RTEMS Address To Line) " << rld::version () |
---|
149 | << ", RTEMS revision " << rld::rtems::version () |
---|
150 | << std::endl; |
---|
151 | ::exit (0); |
---|
152 | break; |
---|
153 | |
---|
154 | case 'v': |
---|
155 | rld::verbose_inc (); |
---|
156 | break; |
---|
157 | |
---|
158 | case 'e': |
---|
159 | exe_name = optarg; |
---|
160 | break; |
---|
161 | |
---|
162 | case 'f': |
---|
163 | show_functions = true; |
---|
164 | break; |
---|
165 | |
---|
166 | case 'a': |
---|
167 | show_addresses = true; |
---|
168 | break; |
---|
169 | |
---|
170 | case 'p': |
---|
171 | pretty_print = true; |
---|
172 | break; |
---|
173 | |
---|
174 | case 's': |
---|
175 | show_basenames = true; |
---|
176 | break; |
---|
177 | |
---|
178 | case '?': |
---|
179 | usage (3); |
---|
180 | break; |
---|
181 | |
---|
182 | case 'h': |
---|
183 | usage (0); |
---|
184 | break; |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | /* |
---|
189 | * Set the program name. |
---|
190 | */ |
---|
191 | rld::set_progname (argv[0]); |
---|
192 | |
---|
193 | argc -= optind; |
---|
194 | argv += optind; |
---|
195 | |
---|
196 | if (rld::verbose ()) |
---|
197 | std::cout << "RTEMS Address To Line " << rld::version () << std::endl; |
---|
198 | |
---|
199 | /* |
---|
200 | * If there are no object files there is nothing to link. |
---|
201 | */ |
---|
202 | if (argc == 0) |
---|
203 | throw rld::error ("no addresses provided", "options"); |
---|
204 | |
---|
205 | if (rld::verbose ()) |
---|
206 | std::cout << "exe: " << exe_name << std::endl; |
---|
207 | |
---|
208 | /* |
---|
209 | * Load the executable's debug info. |
---|
210 | */ |
---|
211 | rld::files::object exe (exe_name); |
---|
212 | rld::dwarf::file debug; |
---|
213 | |
---|
214 | try |
---|
215 | { |
---|
216 | /* |
---|
217 | * Load the executable's ELF file debug info. |
---|
218 | */ |
---|
219 | exe.open (); |
---|
220 | exe.begin (); |
---|
221 | debug.begin (exe.elf ()); |
---|
222 | debug.load_debug (); |
---|
223 | debug.load_types (); |
---|
224 | debug.load_functions (); |
---|
225 | |
---|
226 | for (int arg = 0; arg < argc; ++arg) |
---|
227 | { |
---|
228 | rld::dwarf::dwarf_address location; |
---|
229 | |
---|
230 | if (rld::verbose ()) |
---|
231 | std::cout << "address: " << argv[arg] << std::endl; |
---|
232 | |
---|
233 | /* |
---|
234 | * Use the C routine as C++ does not have a way to automatically handle |
---|
235 | * different bases on the input. |
---|
236 | */ |
---|
237 | location = ::strtoul (argv[arg], 0, 0); |
---|
238 | |
---|
239 | std::string path; |
---|
240 | int line; |
---|
241 | |
---|
242 | debug.get_source (location, path, line); |
---|
243 | |
---|
244 | if (show_addresses) |
---|
245 | { |
---|
246 | std::cout << std::hex << std::setfill ('0') |
---|
247 | << "0x" << location |
---|
248 | << std::dec << std::setfill (' '); |
---|
249 | |
---|
250 | if (pretty_print) |
---|
251 | std::cout << ": "; |
---|
252 | else |
---|
253 | std::cout << std::endl; |
---|
254 | } |
---|
255 | |
---|
256 | if (show_functions) |
---|
257 | { |
---|
258 | std::string function; |
---|
259 | debug.get_function (location, function); |
---|
260 | std::cout << function << " at "; |
---|
261 | } |
---|
262 | |
---|
263 | if (show_basenames) |
---|
264 | std::cout << rld::path::basename (path); |
---|
265 | else |
---|
266 | std::cout << path; |
---|
267 | |
---|
268 | std::cout << ':' << line << std::endl; |
---|
269 | } |
---|
270 | |
---|
271 | debug.end (); |
---|
272 | exe.end (); |
---|
273 | exe.close (); |
---|
274 | } |
---|
275 | catch (...) |
---|
276 | { |
---|
277 | debug.end (); |
---|
278 | exe.end (); |
---|
279 | exe.close (); |
---|
280 | throw; |
---|
281 | } |
---|
282 | } |
---|
283 | catch (rld::error re) |
---|
284 | { |
---|
285 | std::cerr << "error: " |
---|
286 | << re.where << ": " << re.what |
---|
287 | << std::endl; |
---|
288 | ec = 10; |
---|
289 | } |
---|
290 | catch (std::exception& e) |
---|
291 | { |
---|
292 | rld::output_std_exception (e, std::cerr); |
---|
293 | ec = 11; |
---|
294 | } |
---|
295 | catch (...) |
---|
296 | { |
---|
297 | /* |
---|
298 | * Helps to know if this happens. |
---|
299 | */ |
---|
300 | std::cout << "error: unhandled exception" << std::endl; |
---|
301 | ec = 12; |
---|
302 | } |
---|
303 | |
---|
304 | return ec; |
---|
305 | } |
---|