]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
addr2line: Add -a, --address. Print address before for each entry.
authorMark Wielaard <mjw@redhat.com>
Tue, 19 May 2015 14:21:27 +0000 (16:21 +0200)
committerMark Wielaard <mjw@redhat.com>
Wed, 27 May 2015 15:17:51 +0000 (17:17 +0200)
Adds test cases with sample output.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
NEWS
src/ChangeLog
src/addr2line.c
tests/ChangeLog
tests/run-addr2line-i-test.sh
tests/run-addr2line-test.sh

diff --git a/NEWS b/NEWS
index 8cc184a2ca75085a5c13baf3cf9c7e26dfcfc0e1..88d622adad6d54bd62c58fd64bab17e610b63cc4 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,7 @@ libdw: Install new header elfutils/known-dwarf.h.
 
 addr2line: Input addresses are now always interpreted as hexadecimal
            numbers, never as octal or decimal numbers.
+           New option -a, --addresses to print address before each entry.
 
 Version 0.161
 
index 284b8860ae9f6f38c2e75768bf983367da8873b2..127e60132159343ffd5e3e19c83d6b702b3a4357 100644 (file)
@@ -1,3 +1,11 @@
+2015-05-20  Mark Wielaard  <mjw@redhat.com>
+
+       * addr2line.c (argp_option): Add "addresses", 'a'.
+       (print_addresses): New static bool.
+       (parse_opt): Set print_addresses.
+       (get_addr_width): New static function.
+       (handle_address): Print address if print_addresses is true.
+
 2015-05-20  Mark Wielaard  <mjw@redhat.com>
 
        * addr2line.c (handle_address): Call strtoumax with base 16. Make
index b1ff36804673ce698d951c7d568b1b436776471f..720a087eea3438c7844c59acb05feaa9e3aa4e7e 100644 (file)
@@ -1,5 +1,5 @@
 /* Locate source files and line information for given addresses
-   Copyright (C) 2005-2010, 2012, 2013 Red Hat, Inc.
+   Copyright (C) 2005-2010, 2012, 2013, 2015 Red Hat, Inc.
    This file is part of elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 2005.
 
@@ -59,6 +59,7 @@ static const struct argp_option options[] =
     N_("Treat addresses as offsets relative to NAME section."), 0 },
 
   { NULL, 0, NULL, 0, N_("Output format options:"), 3 },
+  { "addresses", 'a', NULL, 0, N_("Print address before each entry"), 0 },
   { "basenames", 's', NULL, 0, N_("Show only base names of source files"), 0 },
   { "absolute", 'A', NULL, 0,
     N_("Show absolute file names using compilation directory"), 0 },
@@ -100,6 +101,8 @@ static const struct argp argp =
 /* Handle ADDR.  */
 static int handle_address (const char *addr, Dwfl *dwfl);
 
+/* True when we should print the address for each entry.  */
+static bool print_addresses;
 
 /* True if only base names of files should be shown.  */
 static bool only_basenames;
@@ -210,6 +213,10 @@ parse_opt (int key, char *arg, struct argp_state *state)
       state->child_inputs[0] = state->input;
       break;
 
+    case 'a':
+      print_addresses = true;
+      break;
+
     case 'b':
     case 'C':
     case OPT_DEMANGLER:
@@ -528,6 +535,29 @@ print_src (const char *src, int lineno, int linecol, Dwarf_Die *cu)
            comp_dir, comp_dir_sep, src, lineno);
 }
 
+static int
+get_addr_width (Dwfl_Module *mod)
+{
+  // Try to find the address width if possible.
+  static int width = 0;
+  if (width == 0 && mod != NULL)
+    {
+      Dwarf_Addr bias;
+      Elf *elf = dwfl_module_getelf (mod, &bias);
+      if (elf != NULL)
+        {
+         GElf_Ehdr ehdr_mem;
+         GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
+         if (ehdr != NULL)
+           width = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16;
+       }
+    }
+  if (width == 0)
+    width = 16;
+
+  return width;
+}
+
 static int
 handle_address (const char *string, Dwfl *dwfl)
 {
@@ -582,6 +612,12 @@ handle_address (const char *string, Dwfl *dwfl)
 
   Dwfl_Module *mod = dwfl_addrmodule (dwfl, addr);
 
+  if (print_addresses)
+    {
+      int width = get_addr_width (mod);
+      printf ("0x%.*" PRIx64 "\n", width, addr);
+    }
+
   if (show_functions)
     {
       /* First determine the function name.  Use the DWARF information if
index 0e310121a0a9203520f454bbb8591c51376aa9d0..d3a0e4b0abfc5898a36fa34c4de1d1319554b12c 100644 (file)
@@ -1,3 +1,8 @@
+2015-05-20  Mark Wielaard  <mjw@redhat.com>
+
+       * run-addr2line-test.sh: Add -a test variants.
+       * run-addr2line-i-test.sh: Likewise.
+
 2015-05-20  Mark Wielaard  <mjw@redhat.com>
 
        * run-addrname-test.sh: Make sure all input addresses are hex.
index 183916c7dd30d0e8f64b1eeb0a492c88d1ecb195..e62aa201c40ff151f200779d99eb6d300fa37b9f 100755 (executable)
@@ -144,4 +144,57 @@ _Z2fuv
 /tmp/x.cpp:33
 EOF
 
+# All together now (plus function names plus addresses).
+testrun_compare ${abs_top_builddir}/src/addr2line -a -f -i -e testfile-inlines 0x00000000000005a0 0x00000000000005a1 0x00000000000005b0 0x00000000000005b1 0x00000000000005c0 0x00000000000005d0 0x00000000000005e0 0x00000000000005e1 0x00000000000005f0 0x00000000000005f1 0x00000000000005f2 <<\EOF
+0x00000000000005a0
+foobar
+/tmp/x.cpp:5
+0x00000000000005a1
+foobar
+/tmp/x.cpp:6
+0x00000000000005b0
+fubar
+/tmp/x.cpp:10
+0x00000000000005b1
+fubar
+/tmp/x.cpp:11
+0x00000000000005c0
+foobar inlined at /tmp/x.cpp:15 in _Z3barv
+/tmp/x.cpp:5
+bar
+/tmp/x.cpp:15
+0x00000000000005d0
+fubar inlined at /tmp/x.cpp:20 in _Z3bazv
+/tmp/x.cpp:10
+baz
+/tmp/x.cpp:20
+0x00000000000005e0
+foobar inlined at /tmp/x.cpp:15 in _Z3foov
+/tmp/x.cpp:5
+bar
+/tmp/x.cpp:15
+_Z3foov
+/tmp/x.cpp:25
+0x00000000000005e1
+fubar inlined at /tmp/x.cpp:20 in _Z3foov
+/tmp/x.cpp:10
+baz
+/tmp/x.cpp:20
+_Z3foov
+/tmp/x.cpp:26
+0x00000000000005f0
+_Z2fuv
+/tmp/x.cpp:31
+0x00000000000005f1
+fubar inlined at /tmp/x.cpp:32 in _Z2fuv
+/tmp/x.cpp:10
+_Z2fuv
+/tmp/x.cpp:32
+0x00000000000005f2
+foobar inlined at /tmp/x.cpp:33 in _Z2fuv
+/tmp/x.cpp:5
+_Z2fuv
+/tmp/x.cpp:33
+EOF
+
 exit 0
index 768006bf150ef8e476e022a19ad94d089f84f9bc..8d0606443810d50529af3ee37c908542f299c5b4 100755 (executable)
@@ -71,4 +71,40 @@ echo "# stdin without newline symbol, just EOF."
 echo -n "foo" | testrun ${abs_top_builddir}/src/addr2line -f -e testfile > stdin.nl.out || exit 1
 cmp foo.out stdin.nonl.out || exit 1
 
+tempfiles good.addr.out
+
+cat > good.addr.out <<\EOF
+0x08048468
+foo
+/home/drepper/gnu/new-bu/build/ttt/f.c:3
+0x0804845c
+bar
+/home/drepper/gnu/new-bu/build/ttt/b.c:4
+0x08048468
+foo
+/home/drepper/gnu/new-bu/build/ttt/f.c:3
+0x0804845c
+bar
+/home/drepper/gnu/new-bu/build/ttt/b.c:4
+0x08048468
+foo
+/home/drepper/gnu/new-bu/build/ttt/f.c:3
+0x0804845c
+bar
+/home/drepper/gnu/new-bu/build/ttt/b.c:4
+0x08048468
+foo
+/home/drepper/gnu/new-bu/build/ttt/f.c:3
+0x0804845c
+bar
+/home/drepper/gnu/new-bu/build/ttt/b.c:4
+EOF
+
+echo "# Everything on the command line with addresses"
+cat good.addr.out | testrun_compare ${abs_top_builddir}/src/addr2line -a -f -e testfile 0x08048468 0x0804845c foo bar foo+0x0 bar+0x0 foo-0x0 bar-0x0
+
+echo "# Everything from stdin (with newlines) with addresses."
+cat stdin.nl | testrun ${abs_top_builddir}/src/addr2line -a -f -e testfile > stdin.nl.out || exit 1
+cmp good.addr.out stdin.nl.out || exit 1
+
 exit 0