* doc/c-xtensa.texi (Xtensa Automatic Alignment): Remove statements
[binutils.git] / binutils / addr2line.c
bloba7c365ae7417000ce8a44c3e24e0be3564f2103b
1 /* addr2line.c -- convert addresses to line number and function name
2 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Ulrich Lauther <Ulrich.Lauther@mchp.siemens.de>
6 This file is part of GNU Binutils.
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.
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.
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
20 Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22 /* Derived from objdump.c and nm.c by Ulrich.Lauther@mchp.siemens.de
24 Usage:
25 addr2line [options] addr addr ...
27 addr2line [options]
29 both forms write results to stdout, the second form reads addresses
30 to be converted from stdin. */
32 #include "sysdep.h"
33 #include "bfd.h"
34 #include "getopt.h"
35 #include "libiberty.h"
36 #include "demangle.h"
37 #include "bucomm.h"
39 static bfd_boolean unwind_inlines; /* -i, unwind inlined functions. */
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. */
44 static int naddr; /* Number of addresses to process. */
45 static char **addr; /* Hex addresses to process. */
47 static asymbol **syms; /* Symbol table. */
49 static struct option long_options[] =
51 {"basenames", no_argument, NULL, 's'},
52 {"demangle", optional_argument, NULL, 'C'},
53 {"exe", required_argument, NULL, 'e'},
54 {"functions", no_argument, NULL, 'f'},
55 {"inlines", no_argument, NULL, 'i'},
56 {"section", required_argument, NULL, 'j'},
57 {"target", required_argument, NULL, 'b'},
58 {"help", no_argument, NULL, 'H'},
59 {"version", no_argument, NULL, 'V'},
60 {0, no_argument, 0, 0}
63 static void usage (FILE *, int);
64 static void slurp_symtab (bfd *);
65 static void find_address_in_section (bfd *, asection *, void *);
66 static void find_offset_in_section (bfd *, asection *);
67 static void translate_addresses (bfd *, asection *);
69 /* Print a usage message to STREAM and exit with STATUS. */
71 static void
72 usage (FILE *stream, int status)
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\
78 @<file> Read options from <file>\n\
79 -b --target=<bfdname> Set the binary file format\n\
80 -e --exe=<executable> Set the input file name (default is a.out)\n\
81 -i --inlines Unwind inlined functions\n\
82 -j --section=<name> Read section-relative offsets instead of addresses\n\
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"));
90 list_supported_targets (program_name, stream);
91 if (REPORT_BUGS_TO[0] && status == 0)
92 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
93 exit (status);
96 /* Read in the symbol table. */
98 static void
99 slurp_symtab (bfd *abfd)
101 long symcount;
102 unsigned int size;
104 if ((bfd_get_file_flags (abfd) & HAS_SYMS) == 0)
105 return;
107 symcount = bfd_read_minisymbols (abfd, FALSE, (void *) &syms, &size);
108 if (symcount == 0)
109 symcount = bfd_read_minisymbols (abfd, TRUE /* dynamic */, (void *) &syms, &size);
111 if (symcount < 0)
112 bfd_fatal (bfd_get_filename (abfd));
115 /* These global variables are used to pass information between
116 translate_addresses and find_address_in_section. */
118 static bfd_vma pc;
119 static const char *filename;
120 static const char *functionname;
121 static unsigned int line;
122 static bfd_boolean found;
124 /* Look for an address in a section. This is called via
125 bfd_map_over_sections. */
127 static void
128 find_address_in_section (bfd *abfd, asection *section,
129 void *data ATTRIBUTE_UNUSED)
131 bfd_vma vma;
132 bfd_size_type size;
134 if (found)
135 return;
137 if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
138 return;
140 vma = bfd_get_section_vma (abfd, section);
141 if (pc < vma)
142 return;
144 size = bfd_get_section_size (section);
145 if (pc >= vma + size)
146 return;
148 found = bfd_find_nearest_line (abfd, section, syms, pc - vma,
149 &filename, &functionname, &line);
152 /* Look for an offset in a section. This is directly called. */
154 static void
155 find_offset_in_section (bfd *abfd, asection *section)
157 bfd_size_type size;
159 if (found)
160 return;
162 if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
163 return;
165 size = bfd_get_section_size (section);
166 if (pc >= size)
167 return;
169 found = bfd_find_nearest_line (abfd, section, syms, pc,
170 &filename, &functionname, &line);
173 /* Read hexadecimal addresses from stdin, translate into
174 file_name:line_number and optionally function name. */
176 static void
177 translate_addresses (bfd *abfd, asection *section)
179 int read_stdin = (naddr == 0);
181 for (;;)
183 if (read_stdin)
185 char addr_hex[100];
187 if (fgets (addr_hex, sizeof addr_hex, stdin) == NULL)
188 break;
189 pc = bfd_scan_vma (addr_hex, NULL, 16);
191 else
193 if (naddr <= 0)
194 break;
195 --naddr;
196 pc = bfd_scan_vma (*addr++, NULL, 16);
199 found = FALSE;
200 if (section)
201 find_offset_in_section (abfd, section);
202 else
203 bfd_map_over_sections (abfd, find_address_in_section, NULL);
205 if (! found)
207 if (with_functions)
208 printf ("??\n");
209 printf ("??:0\n");
211 else
213 do {
214 if (with_functions)
216 const char *name;
217 char *alloc = NULL;
219 name = functionname;
220 if (name == NULL || *name == '\0')
221 name = "??";
222 else if (do_demangle)
224 alloc = bfd_demangle (abfd, name, DMGL_ANSI | DMGL_PARAMS);
225 if (alloc != NULL)
226 name = alloc;
229 printf ("%s\n", name);
231 if (alloc != NULL)
232 free (alloc);
235 if (base_names && filename != NULL)
237 char *h;
239 h = strrchr (filename, '/');
240 if (h != NULL)
241 filename = h + 1;
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);
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);
261 /* Process a file. Returns an exit value for main(). */
263 static int
264 process_file (const char *file_name, const char *section_name,
265 const char *target)
267 bfd *abfd;
268 asection *section;
269 char **matching;
271 if (get_file_size (file_name) < 1)
272 return 1;
274 abfd = bfd_openr (file_name, target);
275 if (abfd == NULL)
276 bfd_fatal (file_name);
278 if (bfd_check_format (abfd, bfd_archive))
279 fatal (_("%s: cannot get addresses from archive"), file_name);
281 if (! bfd_check_format_matches (abfd, bfd_object, &matching))
283 bfd_nonfatal (bfd_get_filename (abfd));
284 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
286 list_matching_formats (matching);
287 free (matching);
289 xexit (1);
292 if (section_name != NULL)
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);
298 else
299 section = NULL;
301 slurp_symtab (abfd);
303 translate_addresses (abfd, section);
305 if (syms != NULL)
307 free (syms);
308 syms = NULL;
311 bfd_close (abfd);
313 return 0;
317 main (int argc, char **argv)
319 const char *file_name;
320 const char *section_name;
321 char *target;
322 int c;
324 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
325 setlocale (LC_MESSAGES, "");
326 #endif
327 #if defined (HAVE_SETLOCALE)
328 setlocale (LC_CTYPE, "");
329 #endif
330 bindtextdomain (PACKAGE, LOCALEDIR);
331 textdomain (PACKAGE);
333 program_name = *argv;
334 xmalloc_set_program_name (program_name);
336 expandargv (&argc, &argv);
338 bfd_init ();
339 set_default_bfd_target ();
341 file_name = NULL;
342 section_name = NULL;
343 target = NULL;
344 while ((c = getopt_long (argc, argv, "b:Ce:sfHhij:Vv", long_options, (int *) 0))
345 != EOF)
347 switch (c)
349 case 0:
350 break; /* We've been given a long option. */
351 case 'b':
352 target = optarg;
353 break;
354 case 'C':
355 do_demangle = TRUE;
356 if (optarg != NULL)
358 enum demangling_styles style;
360 style = cplus_demangle_name_to_style (optarg);
361 if (style == unknown_demangling)
362 fatal (_("unknown demangling style `%s'"),
363 optarg);
365 cplus_demangle_set_style (style);
367 break;
368 case 'e':
369 file_name = optarg;
370 break;
371 case 's':
372 base_names = TRUE;
373 break;
374 case 'f':
375 with_functions = TRUE;
376 break;
377 case 'v':
378 case 'V':
379 print_version ("addr2line");
380 break;
381 case 'h':
382 case 'H':
383 usage (stdout, 0);
384 break;
385 case 'i':
386 unwind_inlines = TRUE;
387 break;
388 case 'j':
389 section_name = optarg;
390 break;
391 default:
392 usage (stderr, 1);
393 break;
397 if (file_name == NULL)
398 file_name = "a.out";
400 addr = argv + optind;
401 naddr = argc - optind;
403 return process_file (file_name, section_name, target);