Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* addr2line.c -- convert addresses to line number and function name |
92f01d61 | 2 | Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006, 2007 |
2da42df6 | 3 | Free Software Foundation, Inc. |
c8c5888e | 4 | Contributed by Ulrich Lauther <Ulrich.Lauther@mchp.siemens.de> |
252b5132 RH |
5 | |
6 | This file is part of GNU Binutils. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
b43b5d5f | 20 | Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ |
252b5132 | 21 | |
c8c5888e | 22 | /* Derived from objdump.c and nm.c by Ulrich.Lauther@mchp.siemens.de |
252b5132 | 23 | |
f462a9ea | 24 | Usage: |
252b5132 RH |
25 | addr2line [options] addr addr ... |
26 | or | |
f462a9ea | 27 | addr2line [options] |
252b5132 RH |
28 | |
29 | both forms write results to stdout, the second form reads addresses | |
30 | to be converted from stdin. */ | |
31 | ||
3db64b00 | 32 | #include "sysdep.h" |
252b5132 RH |
33 | #include "bfd.h" |
34 | #include "getopt.h" | |
35 | #include "libiberty.h" | |
36 | #include "demangle.h" | |
37 | #include "bucomm.h" | |
38 | ||
0c552dc1 | 39 | static bfd_boolean unwind_inlines; /* -i, unwind inlined functions. */ |
b34976b6 AM |
40 | static bfd_boolean with_functions; /* -f, show function names. */ |
41 | static bfd_boolean do_demangle; /* -C, demangle names. */ | |
42 | static bfd_boolean base_names; /* -s, strip directory names. */ | |
252b5132 RH |
43 | |
44 | static int naddr; /* Number of addresses to process. */ | |
45 | static char **addr; /* Hex addresses to process. */ | |
46 | ||
47 | static asymbol **syms; /* Symbol table. */ | |
48 | ||
49 | static struct option long_options[] = | |
50 | { | |
51 | {"basenames", no_argument, NULL, 's'}, | |
28c309a2 | 52 | {"demangle", optional_argument, NULL, 'C'}, |
252b5132 RH |
53 | {"exe", required_argument, NULL, 'e'}, |
54 | {"functions", no_argument, NULL, 'f'}, | |
0c552dc1 | 55 | {"inlines", no_argument, NULL, 'i'}, |
c5f8c388 | 56 | {"section", required_argument, NULL, 'j'}, |
252b5132 RH |
57 | {"target", required_argument, NULL, 'b'}, |
58 | {"help", no_argument, NULL, 'H'}, | |
59 | {"version", no_argument, NULL, 'V'}, | |
60 | {0, no_argument, 0, 0} | |
61 | }; | |
62 | ||
2da42df6 AJ |
63 | static void usage (FILE *, int); |
64 | static void slurp_symtab (bfd *); | |
65 | static void find_address_in_section (bfd *, asection *, void *); | |
c5f8c388 EB |
66 | static void find_offset_in_section (bfd *, asection *); |
67 | static void translate_addresses (bfd *, asection *); | |
252b5132 RH |
68 | \f |
69 | /* Print a usage message to STREAM and exit with STATUS. */ | |
70 | ||
71 | static void | |
2da42df6 | 72 | usage (FILE *stream, int status) |
252b5132 | 73 | { |
8b53311e NC |
74 | fprintf (stream, _("Usage: %s [option(s)] [addr(s)]\n"), program_name); |
75 | fprintf (stream, _(" Convert addresses into line number/file name pairs.\n")); | |
76 | fprintf (stream, _(" If no addresses are specified on the command line, they will be read from stdin\n")); | |
77 | fprintf (stream, _(" The options are:\n\ | |
07012eee | 78 | @<file> Read options from <file>\n\ |
8b53311e NC |
79 | -b --target=<bfdname> Set the binary file format\n\ |
80 | -e --exe=<executable> Set the input file name (default is a.out)\n\ | |
c5f8c388 EB |
81 | -i --inlines Unwind inlined functions\n\ |
82 | -j --section=<name> Read section-relative offsets instead of addresses\n\ | |
8b53311e NC |
83 | -s --basenames Strip directory names\n\ |
84 | -f --functions Show function names\n\ | |
85 | -C --demangle[=style] Demangle function names\n\ | |
86 | -h --help Display this information\n\ | |
87 | -v --version Display the program's version\n\ | |
88 | \n")); | |
89 | ||
252b5132 | 90 | list_supported_targets (program_name, stream); |
92f01d61 | 91 | if (REPORT_BUGS_TO[0] && status == 0) |
8ad3436c | 92 | fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO); |
252b5132 RH |
93 | exit (status); |
94 | } | |
95 | \f | |
96 | /* Read in the symbol table. */ | |
97 | ||
98 | static void | |
2da42df6 | 99 | slurp_symtab (bfd *abfd) |
252b5132 | 100 | { |
252b5132 | 101 | long symcount; |
f309035a | 102 | unsigned int size; |
252b5132 RH |
103 | |
104 | if ((bfd_get_file_flags (abfd) & HAS_SYMS) == 0) | |
105 | return; | |
106 | ||
2da42df6 | 107 | symcount = bfd_read_minisymbols (abfd, FALSE, (void *) &syms, &size); |
f309035a | 108 | if (symcount == 0) |
2da42df6 | 109 | symcount = bfd_read_minisymbols (abfd, TRUE /* dynamic */, (void *) &syms, &size); |
252b5132 | 110 | |
252b5132 RH |
111 | if (symcount < 0) |
112 | bfd_fatal (bfd_get_filename (abfd)); | |
113 | } | |
114 | \f | |
115 | /* These global variables are used to pass information between | |
116 | translate_addresses and find_address_in_section. */ | |
117 | ||
118 | static bfd_vma pc; | |
119 | static const char *filename; | |
120 | static const char *functionname; | |
121 | static unsigned int line; | |
b34976b6 | 122 | static bfd_boolean found; |
252b5132 RH |
123 | |
124 | /* Look for an address in a section. This is called via | |
125 | bfd_map_over_sections. */ | |
126 | ||
127 | static void | |
2da42df6 AJ |
128 | find_address_in_section (bfd *abfd, asection *section, |
129 | void *data ATTRIBUTE_UNUSED) | |
252b5132 RH |
130 | { |
131 | bfd_vma vma; | |
132 | bfd_size_type size; | |
133 | ||
134 | if (found) | |
135 | return; | |
136 | ||
137 | if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0) | |
138 | return; | |
139 | ||
140 | vma = bfd_get_section_vma (abfd, section); | |
141 | if (pc < vma) | |
142 | return; | |
143 | ||
135dfb4a | 144 | size = bfd_get_section_size (section); |
252b5132 RH |
145 | if (pc >= vma + size) |
146 | return; | |
147 | ||
148 | found = bfd_find_nearest_line (abfd, section, syms, pc - vma, | |
149 | &filename, &functionname, &line); | |
150 | } | |
151 | ||
c5f8c388 EB |
152 | /* Look for an offset in a section. This is directly called. */ |
153 | ||
154 | static void | |
155 | find_offset_in_section (bfd *abfd, asection *section) | |
156 | { | |
157 | bfd_size_type size; | |
158 | ||
159 | if (found) | |
160 | return; | |
161 | ||
162 | if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0) | |
163 | return; | |
164 | ||
165 | size = bfd_get_section_size (section); | |
166 | if (pc >= size) | |
167 | return; | |
168 | ||
169 | found = bfd_find_nearest_line (abfd, section, syms, pc, | |
170 | &filename, &functionname, &line); | |
171 | } | |
172 | ||
252b5132 RH |
173 | /* Read hexadecimal addresses from stdin, translate into |
174 | file_name:line_number and optionally function name. */ | |
175 | ||
176 | static void | |
c5f8c388 | 177 | translate_addresses (bfd *abfd, asection *section) |
252b5132 RH |
178 | { |
179 | int read_stdin = (naddr == 0); | |
180 | ||
181 | for (;;) | |
182 | { | |
183 | if (read_stdin) | |
184 | { | |
185 | char addr_hex[100]; | |
186 | ||
187 | if (fgets (addr_hex, sizeof addr_hex, stdin) == NULL) | |
188 | break; | |
189 | pc = bfd_scan_vma (addr_hex, NULL, 16); | |
190 | } | |
191 | else | |
192 | { | |
193 | if (naddr <= 0) | |
194 | break; | |
195 | --naddr; | |
196 | pc = bfd_scan_vma (*addr++, NULL, 16); | |
197 | } | |
198 | ||
b34976b6 | 199 | found = FALSE; |
c5f8c388 EB |
200 | if (section) |
201 | find_offset_in_section (abfd, section); | |
202 | else | |
203 | bfd_map_over_sections (abfd, find_address_in_section, NULL); | |
252b5132 RH |
204 | |
205 | if (! found) | |
206 | { | |
207 | if (with_functions) | |
208 | printf ("??\n"); | |
209 | printf ("??:0\n"); | |
210 | } | |
211 | else | |
212 | { | |
0c552dc1 FF |
213 | do { |
214 | if (with_functions) | |
215 | { | |
216 | const char *name; | |
217 | char *alloc = NULL; | |
218 | ||
219 | name = functionname; | |
220 | if (name == NULL || *name == '\0') | |
221 | name = "??"; | |
222 | else if (do_demangle) | |
223 | { | |
ed180cc5 AM |
224 | alloc = bfd_demangle (abfd, name, DMGL_ANSI | DMGL_PARAMS); |
225 | if (alloc != NULL) | |
226 | name = alloc; | |
0c552dc1 FF |
227 | } |
228 | ||
229 | printf ("%s\n", name); | |
230 | ||
231 | if (alloc != NULL) | |
232 | free (alloc); | |
233 | } | |
234 | ||
235 | if (base_names && filename != NULL) | |
236 | { | |
237 | char *h; | |
238 | ||
239 | h = strrchr (filename, '/'); | |
240 | if (h != NULL) | |
241 | filename = h + 1; | |
242 | } | |
243 | ||
244 | printf ("%s:%u\n", filename ? filename : "??", line); | |
245 | if (!unwind_inlines) | |
246 | found = FALSE; | |
247 | else | |
248 | found = bfd_find_inliner_info (abfd, &filename, &functionname, &line); | |
249 | } while (found); | |
252b5132 | 250 | |
252b5132 RH |
251 | } |
252 | ||
253 | /* fflush() is essential for using this command as a server | |
254 | child process that reads addresses from a pipe and responds | |
255 | with line number information, processing one address at a | |
256 | time. */ | |
257 | fflush (stdout); | |
258 | } | |
259 | } | |
260 | ||
d68c385b | 261 | /* Process a file. Returns an exit value for main(). */ |
252b5132 | 262 | |
d68c385b | 263 | static int |
c5f8c388 EB |
264 | process_file (const char *file_name, const char *section_name, |
265 | const char *target) | |
252b5132 RH |
266 | { |
267 | bfd *abfd; | |
c5f8c388 | 268 | asection *section; |
252b5132 RH |
269 | char **matching; |
270 | ||
f24ddbdd | 271 | if (get_file_size (file_name) < 1) |
d68c385b | 272 | return 1; |
f24ddbdd | 273 | |
47badb7b | 274 | abfd = bfd_openr (file_name, target); |
252b5132 | 275 | if (abfd == NULL) |
47badb7b | 276 | bfd_fatal (file_name); |
252b5132 RH |
277 | |
278 | if (bfd_check_format (abfd, bfd_archive)) | |
c5f8c388 | 279 | fatal (_("%s: cannot get addresses from archive"), file_name); |
252b5132 RH |
280 | |
281 | if (! bfd_check_format_matches (abfd, bfd_object, &matching)) | |
282 | { | |
283 | bfd_nonfatal (bfd_get_filename (abfd)); | |
284 | if (bfd_get_error () == bfd_error_file_ambiguously_recognized) | |
285 | { | |
286 | list_matching_formats (matching); | |
287 | free (matching); | |
288 | } | |
289 | xexit (1); | |
290 | } | |
291 | ||
c5f8c388 EB |
292 | if (section_name != NULL) |
293 | { | |
294 | section = bfd_get_section_by_name (abfd, section_name); | |
295 | if (section == NULL) | |
296 | fatal (_("%s: cannot find section %s"), file_name, section_name); | |
297 | } | |
298 | else | |
299 | section = NULL; | |
300 | ||
252b5132 RH |
301 | slurp_symtab (abfd); |
302 | ||
c5f8c388 | 303 | translate_addresses (abfd, section); |
252b5132 RH |
304 | |
305 | if (syms != NULL) | |
306 | { | |
307 | free (syms); | |
308 | syms = NULL; | |
309 | } | |
310 | ||
311 | bfd_close (abfd); | |
d68c385b NC |
312 | |
313 | return 0; | |
252b5132 RH |
314 | } |
315 | \f | |
316 | int | |
2da42df6 | 317 | main (int argc, char **argv) |
252b5132 | 318 | { |
47badb7b | 319 | const char *file_name; |
c5f8c388 | 320 | const char *section_name; |
252b5132 RH |
321 | char *target; |
322 | int c; | |
323 | ||
324 | #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) | |
325 | setlocale (LC_MESSAGES, ""); | |
3882b010 L |
326 | #endif |
327 | #if defined (HAVE_SETLOCALE) | |
328 | setlocale (LC_CTYPE, ""); | |
252b5132 RH |
329 | #endif |
330 | bindtextdomain (PACKAGE, LOCALEDIR); | |
331 | textdomain (PACKAGE); | |
332 | ||
333 | program_name = *argv; | |
334 | xmalloc_set_program_name (program_name); | |
335 | ||
869b9d07 MM |
336 | expandargv (&argc, &argv); |
337 | ||
252b5132 RH |
338 | bfd_init (); |
339 | set_default_bfd_target (); | |
340 | ||
47badb7b | 341 | file_name = NULL; |
c5f8c388 | 342 | section_name = NULL; |
252b5132 | 343 | target = NULL; |
c5f8c388 | 344 | while ((c = getopt_long (argc, argv, "b:Ce:sfHhij:Vv", long_options, (int *) 0)) |
252b5132 RH |
345 | != EOF) |
346 | { | |
347 | switch (c) | |
348 | { | |
349 | case 0: | |
8b53311e | 350 | break; /* We've been given a long option. */ |
252b5132 RH |
351 | case 'b': |
352 | target = optarg; | |
353 | break; | |
354 | case 'C': | |
b34976b6 | 355 | do_demangle = TRUE; |
28c309a2 NC |
356 | if (optarg != NULL) |
357 | { | |
358 | enum demangling_styles style; | |
f462a9ea | 359 | |
28c309a2 | 360 | style = cplus_demangle_name_to_style (optarg); |
f462a9ea | 361 | if (style == unknown_demangling) |
28c309a2 NC |
362 | fatal (_("unknown demangling style `%s'"), |
363 | optarg); | |
f462a9ea | 364 | |
28c309a2 | 365 | cplus_demangle_set_style (style); |
f462a9ea | 366 | } |
252b5132 RH |
367 | break; |
368 | case 'e': | |
47badb7b | 369 | file_name = optarg; |
252b5132 RH |
370 | break; |
371 | case 's': | |
b34976b6 | 372 | base_names = TRUE; |
252b5132 RH |
373 | break; |
374 | case 'f': | |
b34976b6 | 375 | with_functions = TRUE; |
252b5132 | 376 | break; |
8b53311e | 377 | case 'v': |
252b5132 RH |
378 | case 'V': |
379 | print_version ("addr2line"); | |
380 | break; | |
8b53311e | 381 | case 'h': |
252b5132 RH |
382 | case 'H': |
383 | usage (stdout, 0); | |
384 | break; | |
0c552dc1 FF |
385 | case 'i': |
386 | unwind_inlines = TRUE; | |
387 | break; | |
c5f8c388 EB |
388 | case 'j': |
389 | section_name = optarg; | |
390 | break; | |
252b5132 RH |
391 | default: |
392 | usage (stderr, 1); | |
393 | break; | |
394 | } | |
395 | } | |
396 | ||
47badb7b NC |
397 | if (file_name == NULL) |
398 | file_name = "a.out"; | |
252b5132 RH |
399 | |
400 | addr = argv + optind; | |
401 | naddr = argc - optind; | |
402 | ||
d68c385b | 403 | return process_file (file_name, section_name, target); |
252b5132 | 404 | } |