add function to list global integer variables

This commit is contained in:
Gabriel Ionita 2025-10-24 18:50:00 +01:00
parent 95c179b107
commit 273a322f91
Signed by: gabi
SSH Key Fingerprint: SHA256:mrbYmB/SGtDvT3etRoS6pDrMYWxR0/B5GSF6rR3qhhc

View File

@ -115,3 +115,81 @@ Dwarf_Addr Symbol::get_global_var_addr(Dwarf_Debug dbg, Dwarf_Die die) {
dwarf_dealloc(dbg, loc_attr, DW_DLA_ATTR); dwarf_dealloc(dbg, loc_attr, DW_DLA_ATTR);
return addr; return addr;
} }
void Symbol::list_global_integer_vars(Dwarf_Debug dbg) {
Dwarf_Error err;
Dwarf_Unsigned cu_hdr_len, abbr_offset, next_cu_hdr;
Dwarf_Half ver_stamp, addr_sz;
Dwarf_Half length_size, extension_size;
Dwarf_Sig8 type_signature;
Dwarf_Unsigned typeoffset;
Dwarf_Half header_cu_type;
std::cout << "Global integer variables:" << std::endl;
std::cout << std::string(50, '-') << std::endl;
// Iterate through all compilation units
while (dwarf_next_cu_header_d(dbg, true, &cu_hdr_len, &ver_stamp, &abbr_offset,
&addr_sz, &length_size, &extension_size,
&type_signature, &typeoffset, &next_cu_hdr,
&header_cu_type, &err) == DW_DLV_OK) {
Dwarf_Die cu_die = nullptr;
// Get the compilation unit DIE
if (dwarf_siblingof_b(dbg, nullptr, true, &cu_die, &err) != DW_DLV_OK)
continue;
// Get the first child DIE
Dwarf_Die child_die = nullptr;
if (dwarf_child(cu_die, &child_die, &err) != DW_DLV_OK) {
dwarf_dealloc(dbg, cu_die, DW_DLA_DIE);
continue;
}
// Iterate through all child DIEs
while (child_die != nullptr) {
Dwarf_Half tag;
if (dwarf_tag(child_die, &tag, &err) == DW_DLV_OK && tag == DW_TAG_variable) {
// Check if it's a global variable (has DW_AT_external)
Dwarf_Attribute external_attr;
Dwarf_Bool is_external;
bool is_global = false;
if (dwarf_attr(child_die, DW_AT_external, &external_attr, &err) == DW_DLV_OK) {
if (dwarf_formflag(external_attr, &is_external, &err) == DW_DLV_OK && is_external) {
is_global = true;
}
dwarf_dealloc(dbg, external_attr, DW_DLA_ATTR);
}
// Process global integer variables
if (is_global && is_integer_type(dbg, child_die)) {
char *var_name = nullptr;
if (dwarf_diename(child_die, &var_name, &err) == DW_DLV_OK && var_name) {
Dwarf_Addr addr = get_global_var_addr(dbg, child_die);
std::cout << std::format("Variable: {:<30} Address: 0x{:x}",
var_name, addr) << std::endl;
dwarf_dealloc(dbg, var_name, DW_DLA_STRING);
}
}
}
// Move to the next sibling
Dwarf_Die next_die = nullptr;
int rc = dwarf_siblingof_c(child_die, &next_die, &err);
dwarf_dealloc(dbg, child_die, DW_DLA_DIE);
child_die = next_die;
if (rc != DW_DLV_OK)
break;
}
dwarf_dealloc(dbg, cu_die, DW_DLA_DIE);
}
}