2011-09-09 Pedro Alves <pedro@codesourcery.com>
[deliverable/binutils-gdb.git] / binutils / addr2line.c
index 187252151ff65dc998888abe14403dbf67029a08..1ece80aecd4ed6c0408fdb386497248f1440d589 100644 (file)
 #include "libiberty.h"
 #include "demangle.h"
 #include "bucomm.h"
+#include "elf-bfd.h"
 
 static bfd_boolean unwind_inlines;     /* -i, unwind inlined functions. */
+static bfd_boolean with_addresses;     /* -a, show addresses.  */
 static bfd_boolean with_functions;     /* -f, show function names.  */
 static bfd_boolean do_demangle;                /* -C, demangle names.  */
+static bfd_boolean pretty_print;       /* -p, print on one line.  */
 static bfd_boolean base_names;         /* -s, strip directory names.  */
 
 static int naddr;              /* Number of addresses to process.  */
@@ -50,11 +53,13 @@ static asymbol **syms;              /* Symbol table.  */
 
 static struct option long_options[] =
 {
+  {"addresses", no_argument, NULL, 'a'},
   {"basenames", no_argument, NULL, 's'},
   {"demangle", optional_argument, NULL, 'C'},
   {"exe", required_argument, NULL, 'e'},
   {"functions", no_argument, NULL, 'f'},
   {"inlines", no_argument, NULL, 'i'},
+  {"pretty-print", no_argument, NULL, 'p'},
   {"section", required_argument, NULL, 'j'},
   {"target", required_argument, NULL, 'b'},
   {"help", no_argument, NULL, 'H'},
@@ -78,10 +83,12 @@ usage (FILE *stream, int status)
   fprintf (stream, _(" If no addresses are specified on the command line, they will be read from stdin\n"));
   fprintf (stream, _(" The options are:\n\
   @<file>                Read options from <file>\n\
+  -a --addresses         Show addresses\n\
   -b --target=<bfdname>  Set the binary file format\n\
   -e --exe=<executable>  Set the input file name (default is a.out)\n\
   -i --inlines           Unwind inlined functions\n\
   -j --section=<name>    Read section-relative offsets instead of addresses\n\
+  -p --pretty-print      Make the output easier to read for humans\n\
   -s --basenames         Strip directory names\n\
   -f --functions         Show function names\n\
   -C --demangle[=style]  Demangle function names\n\
@@ -189,6 +196,8 @@ find_offset_in_section (bfd *abfd, asection *section)
 static void
 translate_addresses (bfd *abfd, asection *section)
 {
+  const struct elf_backend_data * bed;
+
   int read_stdin = (naddr == 0);
 
   for (;;)
@@ -209,6 +218,23 @@ translate_addresses (bfd *abfd, asection *section)
          pc = bfd_scan_vma (*addr++, NULL, 16);
        }
 
+      if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
+         && (bed = get_elf_backend_data (abfd)) != NULL
+         && bed->sign_extend_vma
+         && (pc & (bfd_vma) 1 << (bed->s->arch_size - 1)))
+       pc |= ((bfd_vma) -1) << bed->s->arch_size;
+
+      if (with_addresses)
+        {
+          printf ("0x");
+          bfd_printf_vma (abfd, pc);
+
+          if (pretty_print)
+            printf (": ");
+          else
+            printf ("\n");
+        }
+
       found = FALSE;
       if (section)
        find_offset_in_section (abfd, section);
@@ -223,44 +249,52 @@ translate_addresses (bfd *abfd, asection *section)
        }
       else
        {
-         do {
-           if (with_functions)
-             {
-               const char *name;
-               char *alloc = NULL;
-
-               name = functionname;
-               if (name == NULL || *name == '\0')
-                 name = "??";
-               else if (do_demangle)
-                 {
-                   alloc = bfd_demangle (abfd, name, DMGL_ANSI | DMGL_PARAMS);
-                   if (alloc != NULL)
-                     name = alloc;
-                 }
-
-               printf ("%s\n", name);
-
-               if (alloc != NULL)
-                 free (alloc);
-             }
-
-           if (base_names && filename != NULL)
-             {
-               char *h;
-
-               h = strrchr (filename, '/');
-               if (h != NULL)
-                 filename = h + 1;
-             }
-
-           printf ("%s:%u\n", filename ? filename : "??", line);
-           if (!unwind_inlines)
-             found = FALSE;
-           else
-             found = bfd_find_inliner_info (abfd, &filename, &functionname, &line);
-         } while (found);
-
+         while (1)
+            {
+              if (with_functions)
+                {
+                  const char *name;
+                  char *alloc = NULL;
+
+                  name = functionname;
+                  if (name == NULL || *name == '\0')
+                    name = "??";
+                  else if (do_demangle)
+                    {
+                      alloc = bfd_demangle (abfd, name, DMGL_ANSI | DMGL_PARAMS);
+                      if (alloc != NULL)
+                        name = alloc;
+                    }
+
+                  printf ("%s", name);
+                  if (pretty_print)
+                    printf (_(" at "));
+                  else
+                    printf ("\n");
+
+                  if (alloc != NULL)
+                    free (alloc);
+                }
+
+              if (base_names && filename != NULL)
+                {
+                  char *h;
+
+                  h = strrchr (filename, '/');
+                  if (h != NULL)
+                    filename = h + 1;
+                }
+
+              printf ("%s:%u\n", filename ? filename : "??", line);
+              if (!unwind_inlines)
+                found = FALSE;
+              else
+                found = bfd_find_inliner_info (abfd, &filename, &functionname, &line);
+              if (! found)
+                break;
+              if (pretty_print)
+                printf (_(" (inlined by) "));
+            }
        }
 
       /* fflush() is essential for using this command as a server
@@ -288,6 +322,9 @@ process_file (const char *file_name, const char *section_name,
   if (abfd == NULL)
     bfd_fatal (file_name);
 
+  /* Decompress sections.  */
+  abfd->flags |= BFD_DECOMPRESS;
+
   if (bfd_check_format (abfd, bfd_archive))
     fatal (_("%s: cannot get addresses from archive"), file_name);
 
@@ -354,13 +391,16 @@ main (int argc, char **argv)
   file_name = NULL;
   section_name = NULL;
   target = NULL;
-  while ((c = getopt_long (argc, argv, "b:Ce:sfHhij:Vv", long_options, (int *) 0))
+  while ((c = getopt_long (argc, argv, "ab:Ce:sfHhij:pVv", long_options, (int *) 0))
         != EOF)
     {
       switch (c)
        {
        case 0:
          break;                /* We've been given a long option.  */
+       case 'a':
+         with_addresses = TRUE;
+         break;
        case 'b':
          target = optarg;
          break;
@@ -387,6 +427,9 @@ main (int argc, char **argv)
        case 'f':
          with_functions = TRUE;
          break;
+        case 'p':
+          pretty_print = TRUE;
+          break;
        case 'v':
        case 'V':
          print_version ("addr2line");
This page took 0.025949 seconds and 4 git commands to generate.