On Mon, 2020-06-15 at 09:50 -0700, Josh Stone via Elfutils-devel wrote: > On 6/13/20 10:34 AM, Milian Wolff wrote: > > can someone explain me the difference between dwarf_getscopes and > > dwarf_getscopes_die? Ideally, this should then be added to the > > documentation > > too. > > dwarf_getscopes_die just follows the DWARF structure of direct DIE > parents, whereas dwarf_getscopes is kind of a semantic view, most > notably following DW_TAG_inlined_subroutine+DW_AT_abstract_origin to > a different part of the DIE tree.
Yes, although the "just" might need a bit of explanation. The documentation could certainly be improved. The difference is given in the first sentence of description of each function: dwarf_getscopes (cu, addr, scopes): Return scope DIEs containing PC address. dwarf_getscopes_die (die, scopes): Return scope DIEs containing the given DIE. So the key difference is scopes containing address vs containing DIE. So when you go "up" dwarf_getscopes you comes across the lexical/source view, you probably "stop" at the first function like (subprogram/subroutine) DIE, because that is the "name" of the thing (symbol) where we are. When you go "up" the getscopes_die you come across the inlined call stack, this was inlined into/called from, etc. you are probably interested in all of the subroutines (inlined function) DIEs, till you see the function (subprogram) everything is inlined into. The "inlined call stack" is not a real call stack of course, but what the programmer intended. The programmer/source calls g() from f(), but instead of generating a call to g() the compiler instead just inlined the code into f(). So you often use them together. You use dwarf_getscopes for "where am I?" Then you use dwarf_getscopes_die for "how did I get here?" Another example use (of both calls) is eu-stack (src/stack.c), which combines the "real" call stack (use getscopes to tell where you are), with the inlined "call stack" (by using dwarf_getscopes_die). Hope that helps, Mark