Adds test cases with sample output. Signed-off-by: Mark Wielaard --- NEWS | 2 ++ src/ChangeLog | 8 +++++++ src/addr2line.c | 38 ++++++++++++++++++++++++++++++- tests/ChangeLog | 5 ++++ tests/run-addr2line-i-test.sh | 53 +++++++++++++++++++++++++++++++++++++++++++ tests/run-addr2line-test.sh | 36 +++++++++++++++++++++++++++++ 6 files changed, 141 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 60aa995..6d81e59 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,8 @@ Version 0.162 libdw: Install new header elfutils/known-dwarf.h. +addr2line: New option -a, --addresses to print address before each entry. + Version 0.161 libdw: New function dwarf_peel_type. dwarf_aggregate_size now uses diff --git a/src/ChangeLog b/src/ChangeLog index 284b886..127e601 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,5 +1,13 @@ 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 sure all input has been processed. diff --git a/src/addr2line.c b/src/addr2line.c index b1ff368..4416c7a 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: @@ -529,6 +536,29 @@ print_src (const char *src, int lineno, int linecol, Dwarf_Die *cu) } static int +get_addr_width (Dwfl_Module *mod) +{ + // Try to find the address wide if possible. + static int width = 0; + if (width == 0 && mod) + { + Dwarf_Addr bias; + Elf *elf = dwfl_module_getelf (mod, &bias); + if (elf) + { + GElf_Ehdr ehdr_mem; + GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem); + if (ehdr) + 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) { char *endp; @@ -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%0*" 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 0e31012..d3a0e4b 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,5 +1,10 @@ 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. 2015-05-04 Max Filippov diff --git a/tests/run-addr2line-i-test.sh b/tests/run-addr2line-i-test.sh index 183916c..e62aa20 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 768006b..8d06064 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 -- 1.8.3.1