diff options
| author | Mark Wielaard <[email protected]> | 2013-09-20 09:50:42 -0400 |
|---|---|---|
| committer | Mark Wielaard <[email protected]> | 2013-09-24 10:28:14 +0200 |
| commit | 1b734df17fca9f89a887b85ffe74616a87388f51 (patch) | |
| tree | 2dc0ba75b4c517261ad60a17f96b6f766722f9b5 /libdw | |
| parent | 21fc33fe191603b71e9e14d2d1a79a8aa48ad6e6 (diff) | |
libdw: Make dwarf_getfuncs find all (defining) DW_TAG_subprogram DIEs.
dwarf_getfuncs used to return only the DW_TAG_subprogram DIEs that were
direct children of the given CU. This is normally how GCC outputs the
subprogram DIEs. But not always. For nested functions the subprogram DIE
is placed under the code construct DIE where it is nested. Other compilers
might output the defining subprogram DIE of a C++ class function under
the DW_TAG_namespace DIE where it was defined. Both such constructs seem
allowed by the DWARF specification. So just searching the CU DIE children
was wrong.
To find all (defining) subprogram DIEs in a CU dwarf_getfuncs should
use __libdw_visit_scopes to walk the tree for all DIEs that can contain
subprograms as children. The only tricky part is making sure the offset
returned and used when the callback returns DWARF_CB_ABORT is correct
and the search continues at the right spot in the CU DIE tree. This
operation now needs to rewalk the whole tree.
Two new testcases were added that fail without this patch. And the
allfcts test was tweaked so that it always returns DWARF_CB_ABORT
from its callback to make sure the offset handling is correct.
Signed-off-by: Mark Wielaard <[email protected]>
Diffstat (limited to 'libdw')
| -rw-r--r-- | libdw/ChangeLog | 7 | ||||
| -rw-r--r-- | libdw/dwarf_getfuncs.c | 96 | ||||
| -rw-r--r-- | libdw/libdw.h | 11 |
3 files changed, 87 insertions, 27 deletions
diff --git a/libdw/ChangeLog b/libdw/ChangeLog index 1a851948..8e1dd92c 100644 --- a/libdw/ChangeLog +++ b/libdw/ChangeLog @@ -1,3 +1,10 @@ +2013-09-20 Mark Wielaard <[email protected]> + + * dwarf_getfuncs.c (visitor_info): New struct. + (tree_visitor): New function. + (dwarf_getfuncs): Use __libdw_visit_scopes with tree_visitor. + * libdw.h (dwarf_getfuncs): Expand function documentation. + 2013-09-12 Mark Wielaard <[email protected]> * fde.c (intern_fde): Free fde and set libdw errno when start diff --git a/libdw/dwarf_getfuncs.c b/libdw/dwarf_getfuncs.c index afc5b6eb..87e0341a 100644 --- a/libdw/dwarf_getfuncs.c +++ b/libdw/dwarf_getfuncs.c @@ -1,5 +1,5 @@ /* Get function information. - Copyright (C) 2005 Red Hat, Inc. + Copyright (C) 2005, 2013 Red Hat, Inc. This file is part of elfutils. Written by Ulrich Drepper <[email protected]>, 2005. @@ -35,6 +35,63 @@ #include "libdwP.h" +struct visitor_info +{ + /* The user callback of dwarf_getfuncs. */ + int (*callback) (Dwarf_Die *, void *); + + /* The user arg value to dwarf_getfuncs. */ + void *arg; + + /* The DIE offset where to (re)start the search. Zero for all. */ + Dwarf_Off start_offset; + + /* Last subprogram DIE offset seen. */ + Dwarf_Off last_offset; + + /* The CU only contains C functions. Allows pruning of most subtrees. */ + bool c_cu; +}; + +static int +tree_visitor (unsigned int depth __attribute__ ((unused)), + struct Dwarf_Die_Chain *chain, void *arg) +{ + struct visitor_info *const v = arg; + Dwarf_Die *die = &chain->die; + Dwarf_Off start_offset = v->start_offset; + Dwarf_Off die_offset = INTUSE(dwarf_dieoffset) (die); + + /* Pure C CUs can only contain defining subprogram DIEs as direct + children of the CU DIE or as nested function inside a normal C + code constructs. */ + int tag = INTUSE(dwarf_tag) (die); + if (v->c_cu + && tag != DW_TAG_subprogram + && tag != DW_TAG_lexical_block + && tag != DW_TAG_inlined_subroutine) + { + chain->prune = true; + return DWARF_CB_OK; + } + + /* Skip all DIEs till we found the (re)start offset. */ + if (start_offset != 0) + { + if (die_offset == start_offset) + v->start_offset = 0; + return DWARF_CB_OK; + } + + /* If this isn't a (defining) subprogram entity, skip DIE. */ + if (tag != DW_TAG_subprogram + || INTUSE(dwarf_hasattr) (die, DW_AT_declaration)) + return DWARF_CB_OK; + + v->last_offset = die_offset; + return (*v->callback) (die, v->arg); +} + ptrdiff_t dwarf_getfuncs (Dwarf_Die *cudie, int (*callback) (Dwarf_Die *, void *), void *arg, ptrdiff_t offset) @@ -43,31 +100,18 @@ dwarf_getfuncs (Dwarf_Die *cudie, int (*callback) (Dwarf_Die *, void *), || INTUSE(dwarf_tag) (cudie) != DW_TAG_compile_unit)) return -1; - Dwarf_Die die_mem; - Dwarf_Die *die; + int lang = INTUSE(dwarf_srclang) (cudie); + bool c_cu = (lang == DW_LANG_C89 + || lang == DW_LANG_C + || lang == DW_LANG_C99); - int res; - if (offset == 0) - res = INTUSE(dwarf_child) (cudie, &die_mem); - else - { - die = INTUSE(dwarf_offdie) (cudie->cu->dbg, offset, &die_mem); - res = INTUSE(dwarf_siblingof) (die, &die_mem); - } - die = res != 0 ? NULL : &die_mem; + struct visitor_info v = { callback, arg, offset, 0, c_cu }; + struct Dwarf_Die_Chain chain = { .die = CUDIE (cudie->cu), + .parent = NULL }; + int res = __libdw_visit_scopes (0, &chain, &tree_visitor, NULL, &v); - while (die != NULL) - { - if (INTUSE(dwarf_tag) (die) == DW_TAG_subprogram) - { - if (callback (die, arg) != DWARF_CB_OK) - return INTUSE(dwarf_dieoffset) (die); - } - - if (INTUSE(dwarf_siblingof) (die, &die_mem) != 0) - break; - } - - /* That's all. */ - return 0; + if (res == DWARF_CB_ABORT) + return v.last_offset; + else + return res; } diff --git a/libdw/libdw.h b/libdw/libdw.h index d1cd1775..0d94c526 100644 --- a/libdw/libdw.h +++ b/libdw/libdw.h @@ -747,7 +747,16 @@ extern Dwarf_Arange *dwarf_getarange_addr (Dwarf_Aranges *aranges, -/* Get functions in CUDIE. */ +/* Get functions in CUDIE. The given callback will be called for all + defining DW_TAG_subprograms in the CU DIE tree. If the callback + returns DWARF_CB_ABORT the return value can be used as offset argument + to resume the function to find all remaining functions (this is not + really recommended, since it needs to rewalk the CU DIE tree first till + that offset is found again). If the callback returns DWARF_CB_OK + dwarf_getfuncs will not return but keep calling the callback for each + function DIE it finds. Pass zero for offset on the first call to walk + the full CU DIE tree. If no more functions can be found and the callback + returned DWARF_CB_OK then the function returns zero. */ extern ptrdiff_t dwarf_getfuncs (Dwarf_Die *cudie, int (*callback) (Dwarf_Die *, void *), void *arg, ptrdiff_t offset); |
