From: Mark Wielaard Date: Tue, 19 May 2015 14:21:27 +0000 (+0200) Subject: addr2line: Add -a, --address. Print address before for each entry. X-Git-Tag: elfutils-0.162~52 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=70a504d1e19fe14cf34ab3e7a0179aa421f548e0;p=thirdparty%2Felfutils.git addr2line: Add -a, --address. Print address before for each entry. Adds test cases with sample output. Signed-off-by: Mark Wielaard --- diff --git a/NEWS b/NEWS index 8cc184a2c..88d622ada 100644 --- 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 diff --git a/src/ChangeLog b/src/ChangeLog index 284b8860a..127e60132 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,11 @@ +2015-05-20 Mark Wielaard + + * 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 * addr2line.c (handle_address): Call strtoumax with base 16. Make diff --git a/src/addr2line.c b/src/addr2line.c index b1ff36804..720a087ee 100644 --- a/src/addr2line.c +++ b/src/addr2line.c @@ -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 , 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 diff --git a/tests/ChangeLog b/tests/ChangeLog index 0e310121a..d3a0e4b0a 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,3 +1,8 @@ +2015-05-20 Mark Wielaard + + * run-addr2line-test.sh: Add -a test variants. + * run-addr2line-i-test.sh: Likewise. + 2015-05-20 Mark Wielaard * run-addrname-test.sh: Make sure all input addresses are hex. diff --git a/tests/run-addr2line-i-test.sh b/tests/run-addr2line-i-test.sh index 183916c7d..e62aa201c 100755 --- a/tests/run-addr2line-i-test.sh +++ b/tests/run-addr2line-i-test.sh @@ -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 diff --git a/tests/run-addr2line-test.sh b/tests/run-addr2line-test.sh index 768006bf1..8d0606443 100755 --- a/tests/run-addr2line-test.sh +++ b/tests/run-addr2line-test.sh @@ -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