Back to index

cell-binutils  2.17cvs20070401
addr2line.c
Go to the documentation of this file.
00001 /* addr2line.c -- convert addresses to line number and function name
00002    Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006, 2007
00003    Free Software Foundation, Inc.
00004    Contributed by Ulrich Lauther <Ulrich.Lauther@mchp.siemens.de>
00005 
00006    This file is part of GNU Binutils.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2, or (at your option)
00011    any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00021 
00022 /* Derived from objdump.c and nm.c by Ulrich.Lauther@mchp.siemens.de
00023 
00024    Usage:
00025    addr2line [options] addr addr ...
00026    or
00027    addr2line [options]
00028 
00029    both forms write results to stdout, the second form reads addresses
00030    to be converted from stdin.  */
00031 
00032 #include "config.h"
00033 #include <string.h>
00034 
00035 #include "bfd.h"
00036 #include "getopt.h"
00037 #include "libiberty.h"
00038 #include "demangle.h"
00039 #include "bucomm.h"
00040 #include "budemang.h"
00041 
00042 static bfd_boolean unwind_inlines; /* -i, unwind inlined functions. */
00043 static bfd_boolean with_functions; /* -f, show function names.  */
00044 static bfd_boolean do_demangle;           /* -C, demangle names.  */
00045 static bfd_boolean base_names;            /* -s, strip directory names.  */
00046 
00047 static int naddr;           /* Number of addresses to process.  */
00048 static char **addr;         /* Hex addresses to process.  */
00049 
00050 static asymbol **syms;             /* Symbol table.  */
00051 
00052 static struct option long_options[] =
00053 {
00054   {"basenames", no_argument, NULL, 's'},
00055   {"demangle", optional_argument, NULL, 'C'},
00056   {"exe", required_argument, NULL, 'e'},
00057   {"functions", no_argument, NULL, 'f'},
00058   {"inlines", no_argument, NULL, 'i'},
00059   {"section", required_argument, NULL, 'j'},
00060   {"target", required_argument, NULL, 'b'},
00061   {"help", no_argument, NULL, 'H'},
00062   {"version", no_argument, NULL, 'V'},
00063   {0, no_argument, 0, 0}
00064 };
00065 
00066 static void usage (FILE *, int);
00067 static void slurp_symtab (bfd *);
00068 static void find_address_in_section (bfd *, asection *, void *);
00069 static void find_offset_in_section (bfd *, asection *);
00070 static void translate_addresses (bfd *, asection *);
00071 
00072 /* Print a usage message to STREAM and exit with STATUS.  */
00073 
00074 static void
00075 usage (FILE *stream, int status)
00076 {
00077   fprintf (stream, _("Usage: %s [option(s)] [addr(s)]\n"), program_name);
00078   fprintf (stream, _(" Convert addresses into line number/file name pairs.\n"));
00079   fprintf (stream, _(" If no addresses are specified on the command line, they will be read from stdin\n"));
00080   fprintf (stream, _(" The options are:\n\
00081   @<file>                Read options from <file>\n\
00082   -b --target=<bfdname>  Set the binary file format\n\
00083   -e --exe=<executable>  Set the input file name (default is a.out)\n\
00084   -i --inlines           Unwind inlined functions\n\
00085   -j --section=<name>    Read section-relative offsets instead of addresses\n\
00086   -s --basenames         Strip directory names\n\
00087   -f --functions         Show function names\n\
00088   -C --demangle[=style]  Demangle function names\n\
00089   -h --help              Display this information\n\
00090   -v --version           Display the program's version\n\
00091 \n"));
00092 
00093   list_supported_targets (program_name, stream);
00094   if (REPORT_BUGS_TO[0] && status == 0)
00095     fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
00096   exit (status);
00097 }
00098 
00099 /* Read in the symbol table.  */
00100 
00101 static void
00102 slurp_symtab (bfd *abfd)
00103 {
00104   long symcount;
00105   unsigned int size;
00106 
00107   if ((bfd_get_file_flags (abfd) & HAS_SYMS) == 0)
00108     return;
00109 
00110   symcount = bfd_read_minisymbols (abfd, FALSE, (void *) &syms, &size);
00111   if (symcount == 0)
00112     symcount = bfd_read_minisymbols (abfd, TRUE /* dynamic */, (void *) &syms, &size);
00113 
00114   if (symcount < 0)
00115     bfd_fatal (bfd_get_filename (abfd));
00116 }
00117 
00118 /* These global variables are used to pass information between
00119    translate_addresses and find_address_in_section.  */
00120 
00121 static bfd_vma pc;
00122 static const char *filename;
00123 static const char *functionname;
00124 static unsigned int line;
00125 static bfd_boolean found;
00126 
00127 /* Look for an address in a section.  This is called via
00128    bfd_map_over_sections.  */
00129 
00130 static void
00131 find_address_in_section (bfd *abfd, asection *section,
00132                       void *data ATTRIBUTE_UNUSED)
00133 {
00134   bfd_vma vma;
00135   bfd_size_type size;
00136 
00137   if (found)
00138     return;
00139 
00140   if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
00141     return;
00142 
00143   vma = bfd_get_section_vma (abfd, section);
00144   if (pc < vma)
00145     return;
00146 
00147   size = bfd_get_section_size (section);
00148   if (pc >= vma + size)
00149     return;
00150 
00151   found = bfd_find_nearest_line (abfd, section, syms, pc - vma,
00152                              &filename, &functionname, &line);
00153 }
00154 
00155 /* Look for an offset in a section.  This is directly called.  */
00156 
00157 static void
00158 find_offset_in_section (bfd *abfd, asection *section)
00159 {
00160   bfd_size_type size;
00161 
00162   if (found)
00163     return;
00164 
00165   if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
00166     return;
00167 
00168   size = bfd_get_section_size (section);
00169   if (pc >= size)
00170     return;
00171 
00172   found = bfd_find_nearest_line (abfd, section, syms, pc,
00173                              &filename, &functionname, &line);
00174 }
00175 
00176 /* Read hexadecimal addresses from stdin, translate into
00177    file_name:line_number and optionally function name.  */
00178 
00179 static void
00180 translate_addresses (bfd *abfd, asection *section)
00181 {
00182   int read_stdin = (naddr == 0);
00183 
00184   for (;;)
00185     {
00186       if (read_stdin)
00187        {
00188          char addr_hex[100];
00189 
00190          if (fgets (addr_hex, sizeof addr_hex, stdin) == NULL)
00191            break;
00192          pc = bfd_scan_vma (addr_hex, NULL, 16);
00193        }
00194       else
00195        {
00196          if (naddr <= 0)
00197            break;
00198          --naddr;
00199          pc = bfd_scan_vma (*addr++, NULL, 16);
00200        }
00201 
00202       found = FALSE;
00203       if (section)
00204        find_offset_in_section (abfd, section);
00205       else
00206        bfd_map_over_sections (abfd, find_address_in_section, NULL);
00207 
00208       if (! found)
00209        {
00210          if (with_functions)
00211            printf ("??\n");
00212          printf ("??:0\n");
00213        }
00214       else
00215        {
00216          do {
00217            if (with_functions)
00218              {
00219               const char *name;
00220               char *alloc = NULL;
00221 
00222               name = functionname;
00223               if (name == NULL || *name == '\0')
00224                 name = "??";
00225               else if (do_demangle)
00226                 {
00227                   alloc = demangle (abfd, name);
00228                   name = alloc;
00229                 }
00230 
00231               printf ("%s\n", name);
00232 
00233               if (alloc != NULL)
00234                 free (alloc);
00235              }
00236 
00237            if (base_names && filename != NULL)
00238              {
00239               char *h;
00240 
00241               h = strrchr (filename, '/');
00242               if (h != NULL)
00243                 filename = h + 1;
00244              }
00245 
00246            printf ("%s:%u\n", filename ? filename : "??", line);
00247            if (!unwind_inlines)
00248              found = FALSE;
00249            else
00250              found = bfd_find_inliner_info (abfd, &filename, &functionname, &line);
00251          } while (found);
00252 
00253        }
00254 
00255       /* fflush() is essential for using this command as a server
00256          child process that reads addresses from a pipe and responds
00257          with line number information, processing one address at a
00258          time.  */
00259       fflush (stdout);
00260     }
00261 }
00262 
00263 /* Process a file.  Returns an exit value for main().  */
00264 
00265 static int
00266 process_file (const char *file_name, const char *section_name,
00267              const char *target)
00268 {
00269   bfd *abfd;
00270   asection *section;
00271   char **matching;
00272 
00273   if (get_file_size (file_name) < 1)
00274     return 1;
00275 
00276   abfd = bfd_openr (file_name, target);
00277   if (abfd == NULL)
00278     bfd_fatal (file_name);
00279 
00280   if (bfd_check_format (abfd, bfd_archive))
00281     fatal (_("%s: cannot get addresses from archive"), file_name);
00282 
00283   if (! bfd_check_format_matches (abfd, bfd_object, &matching))
00284     {
00285       bfd_nonfatal (bfd_get_filename (abfd));
00286       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
00287        {
00288          list_matching_formats (matching);
00289          free (matching);
00290        }
00291       xexit (1);
00292     }
00293 
00294   if (section_name != NULL)
00295     {
00296       section = bfd_get_section_by_name (abfd, section_name);
00297       if (section == NULL)
00298        fatal (_("%s: cannot find section %s"), file_name, section_name);
00299     }
00300   else
00301     section = NULL;
00302 
00303   slurp_symtab (abfd);
00304 
00305   translate_addresses (abfd, section);
00306 
00307   if (syms != NULL)
00308     {
00309       free (syms);
00310       syms = NULL;
00311     }
00312 
00313   bfd_close (abfd);
00314 
00315   return 0;
00316 }
00317 
00318 int
00319 main (int argc, char **argv)
00320 {
00321   const char *file_name;
00322   const char *section_name;
00323   char *target;
00324   int c;
00325 
00326 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
00327   setlocale (LC_MESSAGES, "");
00328 #endif
00329 #if defined (HAVE_SETLOCALE)
00330   setlocale (LC_CTYPE, "");
00331 #endif
00332   bindtextdomain (PACKAGE, LOCALEDIR);
00333   textdomain (PACKAGE);
00334 
00335   program_name = *argv;
00336   xmalloc_set_program_name (program_name);
00337 
00338   expandargv (&argc, &argv);
00339 
00340   bfd_init ();
00341   set_default_bfd_target ();
00342 
00343   file_name = NULL;
00344   section_name = NULL;
00345   target = NULL;
00346   while ((c = getopt_long (argc, argv, "b:Ce:sfHhij:Vv", long_options, (int *) 0))
00347         != EOF)
00348     {
00349       switch (c)
00350        {
00351        case 0:
00352          break;             /* We've been given a long option.  */
00353        case 'b':
00354          target = optarg;
00355          break;
00356        case 'C':
00357          do_demangle = TRUE;
00358          if (optarg != NULL)
00359            {
00360              enum demangling_styles style;
00361 
00362              style = cplus_demangle_name_to_style (optarg);
00363              if (style == unknown_demangling)
00364               fatal (_("unknown demangling style `%s'"),
00365                      optarg);
00366 
00367              cplus_demangle_set_style (style);
00368            }
00369          break;
00370        case 'e':
00371          file_name = optarg;
00372          break;
00373        case 's':
00374          base_names = TRUE;
00375          break;
00376        case 'f':
00377          with_functions = TRUE;
00378          break;
00379        case 'v':
00380        case 'V':
00381          print_version ("addr2line");
00382          break;
00383        case 'h':
00384        case 'H':
00385          usage (stdout, 0);
00386          break;
00387        case 'i':
00388          unwind_inlines = TRUE;
00389          break;
00390        case 'j':
00391          section_name = optarg;
00392          break;
00393        default:
00394          usage (stderr, 1);
00395          break;
00396        }
00397     }
00398 
00399   if (file_name == NULL)
00400     file_name = "a.out";
00401 
00402   addr = argv + optind;
00403   naddr = argc - optind;
00404 
00405   return process_file (file_name, section_name, target);
00406 }