[Dwarf-Discuss] the list of 'local' variables

Michael Eager eager at eagercon.com
Sat Jul 17 04:04:45 PDT 2010


Mathieu Lacage wrote:
> hi,
> 
> I am trying to figure out how I can, from a pc address, find the list
> of local variables which the pc address is able to access from its
> source language. I see that the 'Call Frame Information' appears to be
> just what I need but I fail to see how:
>   - I can get the list of local variables available _at this address_
>   - I can get from a local variable name a location expression valid
> _at this address_ which indicates either how to calculate the value of
> the variable or how to calculate its address

The CFI describes how to walk the call stack frames from any given
PC location.  It doesn't tell you anything about how the program
source is organized.

> The other option might be to parse the .debug_info section and
> identify the DW_TAG_variable and DW_TAG_lexical_block entities under
> the current function (which I guess I can find quickly from the
> aranges section) but I am worried that the location information I get
> from the lexical_block are not valid everywhere in the block depending
> on various compiler optimizations because I see no way to make that
> location information be pc-dependent.

The .debug_info section describes the scoping of the source program.
Each function and lexical scope is described, along with the range
of addresses it occupies.  Once you have found the place in the
program which corresponds to the PC address, you can see which local
variables are in scope at that location.

Each variable has a location attribute which describes where the
variable is located (in memory or registers) and the range of PC
addresses where this is valid.  If a variable is stored in different
locations at different PC addresses, a location list provides a mapping
between PC ranges and corresponding variable locations.

The .debug_aranges section is optional; many compilers do not generate it.

> The next step for me is digging in the gdb source tree to figure out
> how it implements this but maybe someone who knows better can save me
> a lot of time by giving me a rough outline of how this is expected to
> be done.

dwarf2read.c in GDB reads DWARF 2, 3, and 4 data.  You might also
look at readelf, which dumps most of the DWARF data.

-- 
Michael Eager	 eager at eagercon.com
1960 Park Blvd., Palo Alto, CA 94306  650-325-8077



More information about the Dwarf-Discuss mailing list