1 /* addr2line.c -- convert addresses to line number and function name
2 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
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)
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 addr2line [options] addr addr ...
29 both forms write results to stdout, the second form reads addresses
30 to be converted from stdin. */
36 #include "libiberty.h"
41 static bfd_boolean with_functions; /* -f, show function names. */
42 static bfd_boolean do_demangle; /* -C, demangle names. */
43 static bfd_boolean base_names; /* -s, strip directory names. */
45 static int naddr; /* Number of addresses to process. */
46 static char **addr; /* Hex addresses to process. */
48 static asymbol **syms; /* Symbol table. */
50 static struct option long_options[] =
52 {"basenames", no_argument, NULL, 's'},
53 {"demangle", optional_argument, NULL, 'C'},
54 {"exe", required_argument, NULL, 'e'},
55 {"functions", no_argument, NULL, 'f'},
56 {"target", required_argument, NULL, 'b'},
57 {"help", no_argument, NULL, 'H'},
58 {"version", no_argument, NULL, 'V'},
59 {0, no_argument, 0, 0}
62 static void usage (FILE *, int);
63 static void slurp_symtab (bfd *);
64 static void find_address_in_section (bfd *, asection *, void *);
65 static void translate_addresses (bfd *);
66 static void process_file (const char *, const char *);
68 /* Print a usage message to STREAM and exit with STATUS. */
71 usage (FILE *stream, int status)
73 fprintf (stream, _("Usage: %s [option(s)] [addr(s)]\n"), program_name);
74 fprintf (stream, _(" Convert addresses into line number/file name pairs.\n"));
75 fprintf (stream, _(" If no addresses are specified on the command line, they will be read from stdin\n"));
76 fprintf (stream, _(" The options are:\n\
77 -b --target=<bfdname> Set the binary file format\n\
78 -e --exe=<executable> Set the input file name (default is a.out)\n\
79 -s --basenames Strip directory names\n\
80 -f --functions Show function names\n\
81 -C --demangle[=style] Demangle function names\n\
82 -h --help Display this information\n\
83 -v --version Display the program's version\n\
86 list_supported_targets (program_name, stream);
88 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
92 /* Read in the symbol table. */
95 slurp_symtab (bfd *abfd)
100 if ((bfd_get_file_flags (abfd) & HAS_SYMS) == 0)
103 symcount = bfd_read_minisymbols (abfd, FALSE, (void *) &syms, &size);
105 symcount = bfd_read_minisymbols (abfd, TRUE /* dynamic */, (void *) &syms, &size);
108 bfd_fatal (bfd_get_filename (abfd));
111 /* These global variables are used to pass information between
112 translate_addresses and find_address_in_section. */
115 static const char *filename;
116 static const char *functionname;
117 static unsigned int line;
118 static bfd_boolean found;
120 /* Look for an address in a section. This is called via
121 bfd_map_over_sections. */
124 find_address_in_section (bfd *abfd, asection *section,
125 void *data ATTRIBUTE_UNUSED)
133 if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
136 vma = bfd_get_section_vma (abfd, section);
140 size = bfd_get_section_size_before_reloc (section);
141 if (pc >= vma + size)
144 found = bfd_find_nearest_line (abfd, section, syms, pc - vma,
145 &filename, &functionname, &line);
148 /* Read hexadecimal addresses from stdin, translate into
149 file_name:line_number and optionally function name. */
152 translate_addresses (bfd *abfd)
154 int read_stdin = (naddr == 0);
162 if (fgets (addr_hex, sizeof addr_hex, stdin) == NULL)
164 pc = bfd_scan_vma (addr_hex, NULL, 16);
171 pc = bfd_scan_vma (*addr++, NULL, 16);
175 bfd_map_over_sections (abfd, find_address_in_section, NULL);
191 if (name == NULL || *name == '\0')
193 else if (do_demangle)
195 alloc = demangle (abfd, name);
199 printf ("%s\n", name);
205 if (base_names && filename != NULL)
209 h = strrchr (filename, '/');
214 printf ("%s:%u\n", filename ? filename : "??", line);
217 /* fflush() is essential for using this command as a server
218 child process that reads addresses from a pipe and responds
219 with line number information, processing one address at a
225 /* Process a file. */
228 process_file (const char *file_name, const char *target)
233 if (get_file_size (file_name) < 1)
236 abfd = bfd_openr (file_name, target);
238 bfd_fatal (file_name);
240 if (bfd_check_format (abfd, bfd_archive))
241 fatal (_("%s: can not get addresses from archive"), file_name);
243 if (! bfd_check_format_matches (abfd, bfd_object, &matching))
245 bfd_nonfatal (bfd_get_filename (abfd));
246 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
248 list_matching_formats (matching);
256 translate_addresses (abfd);
267 int main (int, char **);
270 main (int argc, char **argv)
272 const char *file_name;
276 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
277 setlocale (LC_MESSAGES, "");
279 #if defined (HAVE_SETLOCALE)
280 setlocale (LC_CTYPE, "");
282 bindtextdomain (PACKAGE, LOCALEDIR);
283 textdomain (PACKAGE);
285 program_name = *argv;
286 xmalloc_set_program_name (program_name);
289 set_default_bfd_target ();
293 while ((c = getopt_long (argc, argv, "b:Ce:sfHhVv", long_options, (int *) 0))
299 break; /* We've been given a long option. */
307 enum demangling_styles style;
309 style = cplus_demangle_name_to_style (optarg);
310 if (style == unknown_demangling)
311 fatal (_("unknown demangling style `%s'"),
314 cplus_demangle_set_style (style);
324 with_functions = TRUE;
328 print_version ("addr2line");
340 if (file_name == NULL)
343 addr = argv + optind;
344 naddr = argc - optind;
346 process_file (file_name, target);