问题
I wanted to know the use of the attribute DW_AT_location for debugging. It is one of the attributes specified by dwarf for debugging, but could not really understand what exactly it represents. And also when should this attribute be emitted when we compile a code.
回答1:
From the DWARF 3 spec (http://dwarfstd.org/doc/Dwarf3.pdf):
2.16 Data Locations Any debugging information entry describing a data object, which includes variables, parameters, common blocks and the like, may have a DW_AT_location attribute, whose value is a location description (see Section 2.6).
The value of the DW_AT_location attribute is a location expression. Location expressions are fairly complex, I'd advise you to read the DWARF spec referenced above to learn more. In summary, a location expression can be a simple address with the location of the variable, or a mini-program that must be evaluated at run-time by the debugger to determine the location of the variable. Here are some example location expressions, from the DWARF spec:
Ideally, your compiler should emit a location list for a variable describing its location at all points in the program. Tracking a variable's location through registers is not trivial, that is why some compilers when producing debug information disable optimizations like moving variables into registers.
回答2:
Please check the chapter 7 of DWARF 3 spec, DATA REPRESENTATION.
For example, if the value of DW_AT_location is 0x91 0x68, from the table in chapter 7, we can know that the 0x91 indicates the DWARF Debugging Information Format which is DW_OP_fbreg, and 0x68 is a SLEB128 offset, we can get its real value: -24.
来源:https://stackoverflow.com/questions/9719266/use-and-meaning-of-dw-at-location