libdwarf
Location/expression access

Example using DWARF2-5 loclists and loc-expressions.

Valid for DWARF2 and later DWARF.

This example simply assumes the attribute has a form which relates to location lists or location expressions. Use dwarf_get_form_class() to determine if this attribute fits. Use dwarf_get_version_of_die() to help get the data you need.

See also
dwarf_get_form_class
dwarf_get_version_of_die
Reading a location expression
*/
int example_loclistcv5(Dwarf_Attribute someattr,
Dwarf_Error *error)
{
Dwarf_Unsigned lcount = 0;
Dwarf_Loc_Head_c loclist_head = 0;
int lres = 0;
lres = dwarf_get_loclist_c(someattr,&loclist_head,
&lcount,error);
if (lres == DW_DLV_OK) {
/* Before any return remember to call
dwarf_loc_head_c_dealloc(loclist_head); */
for (i = 0; i < lcount; ++i) {
Dwarf_Small loclist_lkind = 0;
Dwarf_Small lle_value = 0;
Dwarf_Unsigned rawval1 = 0;
Dwarf_Unsigned rawval2 = 0;
Dwarf_Bool debug_addr_unavailable = FALSE;
Dwarf_Addr lopc = 0;
Dwarf_Addr hipc = 0;
Dwarf_Unsigned loclist_expr_op_count = 0;
Dwarf_Locdesc_c locdesc_entry = 0;
Dwarf_Unsigned expression_offset = 0;
Dwarf_Unsigned locdesc_offset = 0;
lres = dwarf_get_locdesc_entry_d(loclist_head,
i,
&lle_value,
&rawval1,&rawval2,
&debug_addr_unavailable,
&lopc,&hipc,
&loclist_expr_op_count,
&locdesc_entry,
&loclist_lkind,
&expression_offset,
&locdesc_offset,
error);
if (lres == DW_DLV_OK) {
int opres = 0;
Dwarf_Small op = 0;
for (j = 0; j < loclist_expr_op_count; ++j) {
Dwarf_Unsigned opd1 = 0;
Dwarf_Unsigned opd2 = 0;
Dwarf_Unsigned opd3 = 0;
Dwarf_Unsigned offsetforbranch = 0;
locdesc_entry, j,&op,
&opd1,&opd2,&opd3,
&offsetforbranch,
error);
if (opres == DW_DLV_OK) {
/* Do something with the operators.
Usually you want to use opd1,2,3
as appropriate. Calculations
involving base addresses etc
have already been incorporated
in opd1,2,3. */
} else {
dwarf_dealloc_loc_head_c(loclist_head);
/*Something is wrong. */
return opres;
}
}
} else {
/* Something is wrong. Do something. */
dwarf_dealloc_loc_head_c(loclist_head);
return lres;
}
}
}
/* Always call dwarf_loc_head_c_dealloc()
to free all the memory associated with loclist_head. */
dwarf_dealloc_loc_head_c(loclist_head);
loclist_head = 0;
return lres;
}
struct Dwarf_Error_s * Dwarf_Error
Definition: libdwarf.h:597
struct Dwarf_Locdesc_c_s * Dwarf_Locdesc_c
Definition: libdwarf.h:341
struct Dwarf_Loc_Head_c_s * Dwarf_Loc_Head_c
Definition: libdwarf.h:347
struct Dwarf_Attribute_s * Dwarf_Attribute
Definition: libdwarf.h:658
unsigned char Dwarf_Small
Definition: libdwarf.h:204
unsigned long long Dwarf_Unsigned
Definition: libdwarf.h:196
int Dwarf_Bool
Definition: libdwarf.h:202
unsigned long long Dwarf_Addr
Definition: libdwarf.h:199
int dwarf_get_location_op_value_c(Dwarf_Locdesc_c dw_locdesc, Dwarf_Unsigned dw_index, Dwarf_Small *dw_operator_out, Dwarf_Unsigned *dw_operand1, Dwarf_Unsigned *dw_operand2, Dwarf_Unsigned *dw_operand3, Dwarf_Unsigned *dw_offset_for_branch, Dwarf_Error *dw_error)
Get the raw values from a single location operation.
int dwarf_get_locdesc_entry_d(Dwarf_Loc_Head_c dw_loclist_head, Dwarf_Unsigned dw_index, Dwarf_Small *dw_lle_value_out, Dwarf_Unsigned *dw_rawlowpc, Dwarf_Unsigned *dw_rawhipc, Dwarf_Bool *dw_debug_addr_unavailable, Dwarf_Addr *dw_lowpc_cooked, Dwarf_Addr *dw_hipc_cooked, Dwarf_Unsigned *dw_locexpr_op_count_out, Dwarf_Locdesc_c *dw_locentry_out, Dwarf_Small *dw_loclist_source_out, Dwarf_Unsigned *dw_expression_offset_out, Dwarf_Unsigned *dw_locdesc_offset_out, Dwarf_Error *dw_error)
Retrieve the details(_d) of a location expression.
int dwarf_get_loclist_c(Dwarf_Attribute dw_attr, Dwarf_Loc_Head_c *dw_loclist_head, Dwarf_Unsigned *dw_locentry_count, Dwarf_Error *dw_error)
Location Lists and Expressions.
void dwarf_dealloc_loc_head_c(Dwarf_Loc_Head_c dw_head)
Dealloc (free) all memory allocated for Dwarf_Loc_Head_c.