Home | History | Annotate | Download | only in binutils
      1 /* addr2line.c -- convert addresses to line number and function name
      2    Copyright (C) 1997-2014 Free Software Foundation, Inc.
      3    Contributed by Ulrich Lauther <Ulrich.Lauther (at) mchp.siemens.de>
      4 
      5    This file is part of GNU Binutils.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3, or (at your option)
     10    any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program; if not, write to the Free Software
     19    Foundation, 51 Franklin Street - Fifth Floor, Boston,
     20    MA 02110-1301, USA.  */
     21 
     22 
     23 /* Derived from objdump.c and nm.c by Ulrich.Lauther (at) mchp.siemens.de
     24 
     25    Usage:
     26    addr2line [options] addr addr ...
     27    or
     28    addr2line [options]
     29 
     30    both forms write results to stdout, the second form reads addresses
     31    to be converted from stdin.  */
     32 
     33 #include "sysdep.h"
     34 #include "bfd.h"
     35 #include "getopt.h"
     36 #include "libiberty.h"
     37 #include "demangle.h"
     38 #include "bucomm.h"
     39 #include "elf-bfd.h"
     40 
     41 static bfd_boolean unwind_inlines;	/* -i, unwind inlined functions. */
     42 static bfd_boolean with_addresses;	/* -a, show addresses.  */
     43 static bfd_boolean with_functions;	/* -f, show function names.  */
     44 static bfd_boolean do_demangle;		/* -C, demangle names.  */
     45 static bfd_boolean pretty_print;	/* -p, print on one line.  */
     46 static bfd_boolean base_names;		/* -s, strip directory names.  */
     47 
     48 static int naddr;		/* Number of addresses to process.  */
     49 static char **addr;		/* Hex addresses to process.  */
     50 
     51 static asymbol **syms;		/* Symbol table.  */
     52 
     53 static struct option long_options[] =
     54 {
     55   {"addresses", no_argument, NULL, 'a'},
     56   {"basenames", no_argument, NULL, 's'},
     57   {"demangle", optional_argument, NULL, 'C'},
     58   {"exe", required_argument, NULL, 'e'},
     59   {"functions", no_argument, NULL, 'f'},
     60   {"inlines", no_argument, NULL, 'i'},
     61   {"pretty-print", no_argument, NULL, 'p'},
     62   {"section", required_argument, NULL, 'j'},
     63   {"target", required_argument, NULL, 'b'},
     64   {"help", no_argument, NULL, 'H'},
     65   {"version", no_argument, NULL, 'V'},
     66   {0, no_argument, 0, 0}
     67 };
     68 
     69 static void usage (FILE *, int);
     70 static void slurp_symtab (bfd *);
     71 static void find_address_in_section (bfd *, asection *, void *);
     72 static void find_offset_in_section (bfd *, asection *);
     73 static void translate_addresses (bfd *, asection *);
     74 
     75 /* Print a usage message to STREAM and exit with STATUS.  */
     77 
     78 static void
     79 usage (FILE *stream, int status)
     80 {
     81   fprintf (stream, _("Usage: %s [option(s)] [addr(s)]\n"), program_name);
     82   fprintf (stream, _(" Convert addresses into line number/file name pairs.\n"));
     83   fprintf (stream, _(" If no addresses are specified on the command line, they will be read from stdin\n"));
     84   fprintf (stream, _(" The options are:\n\
     85   @<file>                Read options from <file>\n\
     86   -a --addresses         Show addresses\n\
     87   -b --target=<bfdname>  Set the binary file format\n\
     88   -e --exe=<executable>  Set the input file name (default is a.out)\n\
     89   -i --inlines           Unwind inlined functions\n\
     90   -j --section=<name>    Read section-relative offsets instead of addresses\n\
     91   -p --pretty-print      Make the output easier to read for humans\n\
     92   -s --basenames         Strip directory names\n\
     93   -f --functions         Show function names\n\
     94   -C --demangle[=style]  Demangle function names\n\
     95   -h --help              Display this information\n\
     96   -v --version           Display the program's version\n\
     97 \n"));
     98 
     99   list_supported_targets (program_name, stream);
    100   if (REPORT_BUGS_TO[0] && status == 0)
    101     fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
    102   exit (status);
    103 }
    104 
    105 /* Read in the symbol table.  */
    107 
    108 static void
    109 slurp_symtab (bfd *abfd)
    110 {
    111   long storage;
    112   long symcount;
    113   bfd_boolean dynamic = FALSE;
    114 
    115   if ((bfd_get_file_flags (abfd) & HAS_SYMS) == 0)
    116     return;
    117 
    118   storage = bfd_get_symtab_upper_bound (abfd);
    119   if (storage == 0)
    120     {
    121       storage = bfd_get_dynamic_symtab_upper_bound (abfd);
    122       dynamic = TRUE;
    123     }
    124   if (storage < 0)
    125     bfd_fatal (bfd_get_filename (abfd));
    126 
    127   syms = (asymbol **) xmalloc (storage);
    128   if (dynamic)
    129     symcount = bfd_canonicalize_dynamic_symtab (abfd, syms);
    130   else
    131     symcount = bfd_canonicalize_symtab (abfd, syms);
    132   if (symcount < 0)
    133     bfd_fatal (bfd_get_filename (abfd));
    134 
    135   /* If there are no symbols left after canonicalization and
    136      we have not tried the dynamic symbols then give them a go.  */
    137   if (symcount == 0
    138       && ! dynamic
    139       && (storage = bfd_get_dynamic_symtab_upper_bound (abfd)) > 0)
    140     {
    141       free (syms);
    142       syms = xmalloc (storage);
    143       symcount = bfd_canonicalize_dynamic_symtab (abfd, syms);
    144     }
    145 }
    146 
    147 /* These global variables are used to pass information between
    149    translate_addresses and find_address_in_section.  */
    150 
    151 static bfd_vma pc;
    152 static const char *filename;
    153 static const char *functionname;
    154 static unsigned int line;
    155 static unsigned int discriminator;
    156 static bfd_boolean found;
    157 
    158 /* Look for an address in a section.  This is called via
    159    bfd_map_over_sections.  */
    160 
    161 static void
    162 find_address_in_section (bfd *abfd, asection *section,
    163 			 void *data ATTRIBUTE_UNUSED)
    164 {
    165   bfd_vma vma;
    166   bfd_size_type size;
    167 
    168   if (found)
    169     return;
    170 
    171   if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
    172     return;
    173 
    174   vma = bfd_get_section_vma (abfd, section);
    175   if (pc < vma)
    176     return;
    177 
    178   size = bfd_get_section_size (section);
    179   if (pc >= vma + size)
    180     return;
    181 
    182   found = bfd_find_nearest_line_discriminator (abfd, section, syms, pc - vma,
    183                                                &filename, &functionname,
    184                                                &line, &discriminator);
    185 }
    186 
    187 /* Look for an offset in a section.  This is directly called.  */
    188 
    189 static void
    190 find_offset_in_section (bfd *abfd, asection *section)
    191 {
    192   bfd_size_type size;
    193 
    194   if (found)
    195     return;
    196 
    197   if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
    198     return;
    199 
    200   size = bfd_get_section_size (section);
    201   if (pc >= size)
    202     return;
    203 
    204   found = bfd_find_nearest_line_discriminator (abfd, section, syms, pc,
    205                                                &filename, &functionname,
    206                                                &line, &discriminator);
    207 }
    208 
    209 /* Read hexadecimal addresses from stdin, translate into
    210    file_name:line_number and optionally function name.  */
    211 
    212 static void
    213 translate_addresses (bfd *abfd, asection *section)
    214 {
    215   int read_stdin = (naddr == 0);
    216 
    217   for (;;)
    218     {
    219       if (read_stdin)
    220 	{
    221 	  char addr_hex[100];
    222 
    223 	  if (fgets (addr_hex, sizeof addr_hex, stdin) == NULL)
    224 	    break;
    225 	  pc = bfd_scan_vma (addr_hex, NULL, 16);
    226 	}
    227       else
    228 	{
    229 	  if (naddr <= 0)
    230 	    break;
    231 	  --naddr;
    232 	  pc = bfd_scan_vma (*addr++, NULL, 16);
    233 	}
    234 
    235       if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
    236 	{
    237 	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
    238 	  bfd_vma sign = (bfd_vma) 1 << (bed->s->arch_size - 1);
    239 
    240 	  pc &= (sign << 1) - 1;
    241 	  if (bed->sign_extend_vma)
    242 	    pc = (pc ^ sign) - sign;
    243 	}
    244 
    245       if (with_addresses)
    246         {
    247           printf ("0x");
    248           bfd_printf_vma (abfd, pc);
    249 
    250           if (pretty_print)
    251             printf (": ");
    252           else
    253             printf ("\n");
    254         }
    255 
    256       found = FALSE;
    257       if (section)
    258 	find_offset_in_section (abfd, section);
    259       else
    260 	bfd_map_over_sections (abfd, find_address_in_section, NULL);
    261 
    262       if (! found)
    263 	{
    264 	  if (with_functions)
    265 	    {
    266 	      if (pretty_print)
    267 		printf ("?? ");
    268 	      else
    269 		printf ("??\n");
    270 	    }
    271 	  printf ("??:0\n");
    272 	}
    273       else
    274 	{
    275 	  while (1)
    276             {
    277               if (with_functions)
    278                 {
    279                   const char *name;
    280                   char *alloc = NULL;
    281 
    282                   name = functionname;
    283                   if (name == NULL || *name == '\0')
    284                     name = "??";
    285                   else if (do_demangle)
    286                     {
    287                       alloc = bfd_demangle (abfd, name, DMGL_ANSI | DMGL_PARAMS);
    288                       if (alloc != NULL)
    289                         name = alloc;
    290                     }
    291 
    292                   printf ("%s", name);
    293                   if (pretty_print)
    294 		    /* Note for translators:  This printf is used to join the
    295 		       function name just printed above to the line number/
    296 		       file name pair that is about to be printed below.  Eg:
    297 
    298 		         foo at 123:bar.c  */
    299                     printf (_(" at "));
    300                   else
    301                     printf ("\n");
    302 
    303                   if (alloc != NULL)
    304                     free (alloc);
    305                 }
    306 
    307               if (base_names && filename != NULL)
    308                 {
    309                   char *h;
    310 
    311                   h = strrchr (filename, '/');
    312                   if (h != NULL)
    313                     filename = h + 1;
    314                 }
    315 
    316               printf ("%s:", filename ? filename : "??");
    317 	      if (line != 0)
    318                 {
    319                   if (discriminator != 0)
    320                     printf ("%u (discriminator %u)\n", line, discriminator);
    321                   else
    322                     printf ("%u\n", line);
    323                 }
    324 	      else
    325 		printf ("?\n");
    326               if (!unwind_inlines)
    327                 found = FALSE;
    328               else
    329                 found = bfd_find_inliner_info (abfd, &filename, &functionname,
    330 					       &line);
    331               if (! found)
    332                 break;
    333               if (pretty_print)
    334 		/* Note for translators: This printf is used to join the
    335 		   line number/file name pair that has just been printed with
    336 		   the line number/file name pair that is going to be printed
    337 		   by the next iteration of the while loop.  Eg:
    338 
    339 		     123:bar.c (inlined by) 456:main.c  */
    340                 printf (_(" (inlined by) "));
    341             }
    342 	}
    343 
    344       /* fflush() is essential for using this command as a server
    345          child process that reads addresses from a pipe and responds
    346          with line number information, processing one address at a
    347          time.  */
    348       fflush (stdout);
    349     }
    350 }
    351 
    352 /* Process a file.  Returns an exit value for main().  */
    353 
    354 static int
    355 process_file (const char *file_name, const char *section_name,
    356 	      const char *target)
    357 {
    358   bfd *abfd;
    359   asection *section;
    360   char **matching;
    361 
    362   if (get_file_size (file_name) < 1)
    363     return 1;
    364 
    365   abfd = bfd_openr (file_name, target);
    366   if (abfd == NULL)
    367     bfd_fatal (file_name);
    368 
    369   /* Decompress sections.  */
    370   abfd->flags |= BFD_DECOMPRESS;
    371 
    372   if (bfd_check_format (abfd, bfd_archive))
    373     fatal (_("%s: cannot get addresses from archive"), file_name);
    374 
    375   if (! bfd_check_format_matches (abfd, bfd_object, &matching))
    376     {
    377       bfd_nonfatal (bfd_get_filename (abfd));
    378       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
    379 	{
    380 	  list_matching_formats (matching);
    381 	  free (matching);
    382 	}
    383       xexit (1);
    384     }
    385 
    386   if (section_name != NULL)
    387     {
    388       section = bfd_get_section_by_name (abfd, section_name);
    389       if (section == NULL)
    390 	fatal (_("%s: cannot find section %s"), file_name, section_name);
    391     }
    392   else
    393     section = NULL;
    394 
    395   slurp_symtab (abfd);
    396 
    397   translate_addresses (abfd, section);
    398 
    399   if (syms != NULL)
    400     {
    401       free (syms);
    402       syms = NULL;
    403     }
    404 
    405   bfd_close (abfd);
    406 
    407   return 0;
    408 }
    409 
    410 int
    412 main (int argc, char **argv)
    413 {
    414   const char *file_name;
    415   const char *section_name;
    416   char *target;
    417   int c;
    418 
    419 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
    420   setlocale (LC_MESSAGES, "");
    421 #endif
    422 #if defined (HAVE_SETLOCALE)
    423   setlocale (LC_CTYPE, "");
    424 #endif
    425   bindtextdomain (PACKAGE, LOCALEDIR);
    426   textdomain (PACKAGE);
    427 
    428   program_name = *argv;
    429   xmalloc_set_program_name (program_name);
    430 
    431   expandargv (&argc, &argv);
    432 
    433   bfd_init ();
    434   set_default_bfd_target ();
    435 
    436   file_name = NULL;
    437   section_name = NULL;
    438   target = NULL;
    439   while ((c = getopt_long (argc, argv, "ab:Ce:sfHhij:pVv", long_options, (int *) 0))
    440 	 != EOF)
    441     {
    442       switch (c)
    443 	{
    444 	case 0:
    445 	  break;		/* We've been given a long option.  */
    446 	case 'a':
    447 	  with_addresses = TRUE;
    448 	  break;
    449 	case 'b':
    450 	  target = optarg;
    451 	  break;
    452 	case 'C':
    453 	  do_demangle = TRUE;
    454 	  if (optarg != NULL)
    455 	    {
    456 	      enum demangling_styles style;
    457 
    458 	      style = cplus_demangle_name_to_style (optarg);
    459 	      if (style == unknown_demangling)
    460 		fatal (_("unknown demangling style `%s'"),
    461 		       optarg);
    462 
    463 	      cplus_demangle_set_style (style);
    464 	    }
    465 	  break;
    466 	case 'e':
    467 	  file_name = optarg;
    468 	  break;
    469 	case 's':
    470 	  base_names = TRUE;
    471 	  break;
    472 	case 'f':
    473 	  with_functions = TRUE;
    474 	  break;
    475         case 'p':
    476           pretty_print = TRUE;
    477           break;
    478 	case 'v':
    479 	case 'V':
    480 	  print_version ("addr2line");
    481 	  break;
    482 	case 'h':
    483 	case 'H':
    484 	  usage (stdout, 0);
    485 	  break;
    486 	case 'i':
    487 	  unwind_inlines = TRUE;
    488 	  break;
    489 	case 'j':
    490 	  section_name = optarg;
    491 	  break;
    492 	default:
    493 	  usage (stderr, 1);
    494 	  break;
    495 	}
    496     }
    497 
    498   if (file_name == NULL)
    499     file_name = "a.out";
    500 
    501   addr = argv + optind;
    502   naddr = argc - optind;
    503 
    504   return process_file (file_name, section_name, target);
    505 }
    506