xref: /trueos/contrib/binutils/binutils/addr2line.c (revision 8fe640108653f13042f1b15213769e338aa524f6)
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>
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
20    Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
21 
22 /* Derived from objdump.c and nm.c by Ulrich.Lauther@mchp.siemens.de
23 
24    Usage:
25    addr2line [options] addr addr ...
26    or
27    addr2line [options]
28 
29    both forms write results to stdout, the second form reads addresses
30    to be converted from stdin.  */
31 
32 #include "sysdep.h"
33 #include "bfd.h"
34 #include "getopt.h"
35 #include "libiberty.h"
36 #include "demangle.h"
37 #include "bucomm.h"
38 
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.  */
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'},
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}
61 };
62 
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 *);
68 
69 /* Print a usage message to STREAM and exit with STATUS.  */
70 
71 static void
usage(FILE * stream,int status)72 usage (FILE *stream, int status)
73 {
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"));
89 
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);
94 }
95 
96 /* Read in the symbol table.  */
97 
98 static void
slurp_symtab(bfd * abfd)99 slurp_symtab (bfd *abfd)
100 {
101   long symcount;
102   unsigned int size;
103 
104   if ((bfd_get_file_flags (abfd) & HAS_SYMS) == 0)
105     return;
106 
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);
110 
111   if (symcount < 0)
112     bfd_fatal (bfd_get_filename (abfd));
113 }
114 
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;
122 static bfd_boolean found;
123 
124 /* Look for an address in a section.  This is called via
125    bfd_map_over_sections.  */
126 
127 static void
find_address_in_section(bfd * abfd,asection * section,void * data ATTRIBUTE_UNUSED)128 find_address_in_section (bfd *abfd, asection *section,
129 			 void *data ATTRIBUTE_UNUSED)
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 
144   size = bfd_get_section_size (section);
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 
152 /* Look for an offset in a section.  This is directly called.  */
153 
154 static void
find_offset_in_section(bfd * abfd,asection * section)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 
173 /* Read hexadecimal addresses from stdin, translate into
174    file_name:line_number and optionally function name.  */
175 
176 static void
translate_addresses(bfd * abfd,asection * section)177 translate_addresses (bfd *abfd, asection *section)
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 
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);
204 
205       if (! found)
206 	{
207 	  if (with_functions)
208 	    printf ("??\n");
209 	  printf ("??:0\n");
210 	}
211       else
212 	{
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 		  {
224 		    alloc = bfd_demangle (abfd, name, DMGL_ANSI | DMGL_PARAMS);
225 		    if (alloc != NULL)
226 		      name = alloc;
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);
250 
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 
261 /* Process a file.  Returns an exit value for main().  */
262 
263 static int
process_file(const char * file_name,const char * section_name,const char * target)264 process_file (const char *file_name, const char *section_name,
265 	      const char *target)
266 {
267   bfd *abfd;
268   asection *section;
269   char **matching;
270 
271   if (get_file_size (file_name) < 1)
272     return 1;
273 
274   abfd = bfd_openr (file_name, target);
275   if (abfd == NULL)
276     bfd_fatal (file_name);
277 
278   if (bfd_check_format (abfd, bfd_archive))
279     fatal (_("%s: cannot get addresses from archive"), file_name);
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 
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 
301   slurp_symtab (abfd);
302 
303   translate_addresses (abfd, section);
304 
305   if (syms != NULL)
306     {
307       free (syms);
308       syms = NULL;
309     }
310 
311   bfd_close (abfd);
312 
313   return 0;
314 }
315 
316 int
main(int argc,char ** argv)317 main (int argc, char **argv)
318 {
319   const char *file_name;
320   const char *section_name;
321   char *target;
322   int c;
323 
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);
332 
333   program_name = *argv;
334   xmalloc_set_program_name (program_name);
335 
336   expandargv (&argc, &argv);
337 
338   bfd_init ();
339   set_default_bfd_target ();
340 
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)
346     {
347       switch (c)
348 	{
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)
357 	    {
358 	      enum demangling_styles style;
359 
360 	      style = cplus_demangle_name_to_style (optarg);
361 	      if (style == unknown_demangling)
362 		fatal (_("unknown demangling style `%s'"),
363 		       optarg);
364 
365 	      cplus_demangle_set_style (style);
366 	    }
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;
394 	}
395     }
396 
397   if (file_name == NULL)
398     file_name = "a.out";
399 
400   addr = argv + optind;
401   naddr = argc - optind;
402 
403   return process_file (file_name, section_name, target);
404 }
405