mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:17:35 +00:00
LibDebug: Don't expose AttributeValue internals, use getters instead
This will be needed when we add `DW_FORM_strx*` and `DW_FORM_addrx*` support, which requires us to fetch `DW_AT_str_offsets_base` and `DW_AT_addr_base` attributes from the parent compilation unit. This can't be done as we read the values, because it would create infinite recursion (as we might try to parse the compilation unit's `DW_FORM_strx*` encoded name before we get to the attribute). Having getters ensures that we will perform lookups if they are needed.
This commit is contained in:
parent
1b63c8f3b0
commit
8e5b70a0ba
6 changed files with 101 additions and 80 deletions
|
@ -6,12 +6,18 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Span.h>
|
||||
#include <AK/Types.h>
|
||||
#include <LibDebug/Dwarf/DwarfTypes.h>
|
||||
|
||||
namespace Debug::Dwarf {
|
||||
|
||||
struct AttributeValue {
|
||||
class CompilationUnit;
|
||||
|
||||
class AttributeValue {
|
||||
friend class DwarfInfo;
|
||||
|
||||
public:
|
||||
enum class Type : u8 {
|
||||
UnsignedNumber,
|
||||
SignedNumber,
|
||||
|
@ -21,21 +27,33 @@ struct AttributeValue {
|
|||
DwarfExpression,
|
||||
SecOffset,
|
||||
RawBytes,
|
||||
} type;
|
||||
Address
|
||||
};
|
||||
|
||||
Type type() const { return m_type; }
|
||||
AttributeDataForm form() const { return m_form; }
|
||||
|
||||
FlatPtr as_addr() const { return m_data.as_addr; }
|
||||
u64 as_unsigned() const { return m_data.as_unsigned; }
|
||||
i64 as_signed() const { return m_data.as_signed; }
|
||||
const char* as_string() const { return m_data.as_string; }
|
||||
bool as_bool() const { return m_data.as_bool; }
|
||||
ReadonlyBytes as_raw_bytes() const { return m_data.as_raw_bytes; }
|
||||
|
||||
private:
|
||||
Type m_type;
|
||||
union {
|
||||
FlatPtr as_addr;
|
||||
u64 as_unsigned;
|
||||
i64 as_signed;
|
||||
const char* as_string; // points to bytes in the memory mapped elf image
|
||||
bool as_bool;
|
||||
struct {
|
||||
u32 length;
|
||||
const u8* bytes; // points to bytes in the memory mapped elf image
|
||||
} as_raw_bytes;
|
||||
} data {};
|
||||
ReadonlyBytes as_raw_bytes;
|
||||
} m_data {};
|
||||
|
||||
AttributeDataForm form {};
|
||||
AttributeDataForm m_form {};
|
||||
|
||||
CompilationUnit const* m_compilation_unit { nullptr };
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ Optional<FlatPtr> CompilationUnit::base_address() const
|
|||
auto die = root_die();
|
||||
auto res = die.get_attribute(Attribute::LowPc);
|
||||
if (res.has_value()) {
|
||||
m_cached_base_address = res->data.as_addr;
|
||||
m_cached_base_address = res->as_addr();
|
||||
}
|
||||
m_has_cached_base_address = true;
|
||||
return m_cached_base_address;
|
||||
|
|
|
@ -81,7 +81,7 @@ void DIE::for_each_child(Function<void(DIE const& child)> callback) const
|
|||
auto sibling = current_child.get_attribute(Attribute::Sibling);
|
||||
u32 sibling_offset = 0;
|
||||
if (sibling.has_value()) {
|
||||
sibling_offset = sibling.value().data.as_unsigned;
|
||||
sibling_offset = sibling.value().as_unsigned();
|
||||
} else {
|
||||
// NOTE: According to the spec, the compiler doesn't have to supply the sibling information.
|
||||
// When it doesn't, we have to recursively iterate the current child's children to find where they end
|
||||
|
|
|
@ -66,14 +66,14 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
|
|||
InputMemoryStream& debug_info_stream, const CompilationUnit* unit) const
|
||||
{
|
||||
AttributeValue value;
|
||||
value.form = form;
|
||||
value.m_form = form;
|
||||
value.m_compilation_unit = unit;
|
||||
|
||||
auto assign_raw_bytes_value = [&](size_t length) {
|
||||
value.data.as_raw_bytes.length = length;
|
||||
value.data.as_raw_bytes.bytes = reinterpret_cast<const u8*>(debug_info_data().data()
|
||||
+ debug_info_stream.offset());
|
||||
value.m_data.as_raw_bytes = { reinterpret_cast<const u8*>(debug_info_data().data() + debug_info_stream.offset()), length };
|
||||
|
||||
debug_info_stream.discard_or_error(length);
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
};
|
||||
|
||||
switch (form) {
|
||||
|
@ -81,95 +81,95 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
|
|||
u32 offset;
|
||||
debug_info_stream >> offset;
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
value.type = AttributeValue::Type::String;
|
||||
value.m_type = AttributeValue::Type::String;
|
||||
|
||||
auto strings_data = debug_strings_data();
|
||||
value.data.as_string = reinterpret_cast<const char*>(strings_data.data() + offset);
|
||||
value.m_data.as_string = reinterpret_cast<const char*>(strings_data.data() + offset);
|
||||
break;
|
||||
}
|
||||
case AttributeDataForm::Data1: {
|
||||
u8 data;
|
||||
debug_info_stream >> data;
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
value.type = AttributeValue::Type::UnsignedNumber;
|
||||
value.data.as_unsigned = data;
|
||||
value.m_type = AttributeValue::Type::UnsignedNumber;
|
||||
value.m_data.as_unsigned = data;
|
||||
break;
|
||||
}
|
||||
case AttributeDataForm::Data2: {
|
||||
u16 data;
|
||||
debug_info_stream >> data;
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
value.type = AttributeValue::Type::UnsignedNumber;
|
||||
value.data.as_signed = data;
|
||||
value.m_type = AttributeValue::Type::UnsignedNumber;
|
||||
value.m_data.as_signed = data;
|
||||
break;
|
||||
}
|
||||
case AttributeDataForm::Addr: {
|
||||
FlatPtr address;
|
||||
debug_info_stream >> address;
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
value.type = AttributeValue::Type::UnsignedNumber;
|
||||
value.data.as_addr = address;
|
||||
value.m_type = AttributeValue::Type::Address;
|
||||
value.m_data.as_addr = address;
|
||||
break;
|
||||
}
|
||||
case AttributeDataForm::SData: {
|
||||
i64 data;
|
||||
debug_info_stream.read_LEB128_signed(data);
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
value.type = AttributeValue::Type::SignedNumber;
|
||||
value.data.as_signed = data;
|
||||
value.m_type = AttributeValue::Type::SignedNumber;
|
||||
value.m_data.as_signed = data;
|
||||
break;
|
||||
}
|
||||
case AttributeDataForm::UData: {
|
||||
u64 data;
|
||||
debug_info_stream.read_LEB128_unsigned(data);
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
value.type = AttributeValue::Type::UnsignedNumber;
|
||||
value.data.as_unsigned = data;
|
||||
value.m_type = AttributeValue::Type::UnsignedNumber;
|
||||
value.m_data.as_unsigned = data;
|
||||
break;
|
||||
}
|
||||
case AttributeDataForm::SecOffset: {
|
||||
u32 data;
|
||||
debug_info_stream >> data;
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
value.type = AttributeValue::Type::SecOffset;
|
||||
value.data.as_unsigned = data;
|
||||
value.m_type = AttributeValue::Type::SecOffset;
|
||||
value.m_data.as_unsigned = data;
|
||||
break;
|
||||
}
|
||||
case AttributeDataForm::Data4: {
|
||||
u32 data;
|
||||
debug_info_stream >> data;
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
value.type = AttributeValue::Type::UnsignedNumber;
|
||||
value.data.as_unsigned = data;
|
||||
value.m_type = AttributeValue::Type::UnsignedNumber;
|
||||
value.m_data.as_unsigned = data;
|
||||
break;
|
||||
}
|
||||
case AttributeDataForm::Data8: {
|
||||
u64 data;
|
||||
debug_info_stream >> data;
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
value.type = AttributeValue::Type::UnsignedNumber;
|
||||
value.data.as_unsigned = data;
|
||||
value.m_type = AttributeValue::Type::UnsignedNumber;
|
||||
value.m_data.as_unsigned = data;
|
||||
break;
|
||||
}
|
||||
case AttributeDataForm::Ref4: {
|
||||
u32 data;
|
||||
debug_info_stream >> data;
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
value.type = AttributeValue::Type::DieReference;
|
||||
value.m_type = AttributeValue::Type::DieReference;
|
||||
VERIFY(unit);
|
||||
value.data.as_unsigned = data + unit->offset();
|
||||
value.m_data.as_unsigned = data + unit->offset();
|
||||
break;
|
||||
}
|
||||
case AttributeDataForm::FlagPresent: {
|
||||
value.type = AttributeValue::Type::Boolean;
|
||||
value.data.as_bool = true;
|
||||
value.m_type = AttributeValue::Type::Boolean;
|
||||
value.m_data.as_bool = true;
|
||||
break;
|
||||
}
|
||||
case AttributeDataForm::ExprLoc: {
|
||||
size_t length;
|
||||
debug_info_stream.read_LEB128_unsigned(length);
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
value.type = AttributeValue::Type::DwarfExpression;
|
||||
value.m_type = AttributeValue::Type::DwarfExpression;
|
||||
assign_raw_bytes_value(length);
|
||||
break;
|
||||
}
|
||||
|
@ -178,12 +178,12 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
|
|||
u32 str_offset = debug_info_stream.offset();
|
||||
debug_info_stream >> str;
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
value.type = AttributeValue::Type::String;
|
||||
value.data.as_string = reinterpret_cast<const char*>(str_offset + debug_info_data().data());
|
||||
value.m_type = AttributeValue::Type::String;
|
||||
value.m_data.as_string = reinterpret_cast<const char*>(str_offset + debug_info_data().data());
|
||||
break;
|
||||
}
|
||||
case AttributeDataForm::Block1: {
|
||||
value.type = AttributeValue::Type::RawBytes;
|
||||
value.m_type = AttributeValue::Type::RawBytes;
|
||||
u8 length;
|
||||
debug_info_stream >> length;
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
|
@ -191,7 +191,7 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
|
|||
break;
|
||||
}
|
||||
case AttributeDataForm::Block2: {
|
||||
value.type = AttributeValue::Type::RawBytes;
|
||||
value.m_type = AttributeValue::Type::RawBytes;
|
||||
u16 length;
|
||||
debug_info_stream >> length;
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
|
@ -199,7 +199,7 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
|
|||
break;
|
||||
}
|
||||
case AttributeDataForm::Block4: {
|
||||
value.type = AttributeValue::Type::RawBytes;
|
||||
value.m_type = AttributeValue::Type::RawBytes;
|
||||
u32 length;
|
||||
debug_info_stream >> length;
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
|
@ -207,7 +207,7 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
|
|||
break;
|
||||
}
|
||||
case AttributeDataForm::Block: {
|
||||
value.type = AttributeValue::Type::RawBytes;
|
||||
value.m_type = AttributeValue::Type::RawBytes;
|
||||
size_t length;
|
||||
debug_info_stream.read_LEB128_unsigned(length);
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
|
@ -218,16 +218,16 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
|
|||
u32 offset;
|
||||
debug_info_stream >> offset;
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
value.type = AttributeValue::Type::String;
|
||||
value.m_type = AttributeValue::Type::String;
|
||||
|
||||
auto strings_data = debug_line_strings_data();
|
||||
value.data.as_string = reinterpret_cast<const char*>(strings_data.data() + offset);
|
||||
value.m_data.as_string = reinterpret_cast<const char*>(strings_data.data() + offset);
|
||||
break;
|
||||
}
|
||||
case AttributeDataForm::ImplicitConst: {
|
||||
/* Value is part of the abbreviation record. */
|
||||
value.type = AttributeValue::Type::SignedNumber;
|
||||
value.data.as_signed = implicit_const_value;
|
||||
value.m_type = AttributeValue::Type::SignedNumber;
|
||||
value.m_data.as_signed = implicit_const_value;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
|
@ -247,9 +247,9 @@ void DwarfInfo::build_cached_dies() const
|
|||
auto ranges = die.get_attribute(Attribute::Ranges);
|
||||
if (ranges.has_value()) {
|
||||
// TODO Support DW_FORM_rnglistx
|
||||
if (ranges->form != AttributeDataForm::SecOffset)
|
||||
if (ranges->form() != AttributeDataForm::SecOffset)
|
||||
return {};
|
||||
auto offset = ranges->data.as_addr;
|
||||
auto offset = ranges->as_unsigned();
|
||||
AddressRanges address_ranges(debug_range_lists_data(), offset, die.compilation_unit());
|
||||
Vector<DIERange> entries;
|
||||
address_ranges.for_each_range([&entries](auto range) {
|
||||
|
@ -264,18 +264,18 @@ void DwarfInfo::build_cached_dies() const
|
|||
if (!start.has_value() || !end.has_value())
|
||||
return {};
|
||||
|
||||
VERIFY(start->form == Dwarf::AttributeDataForm::Addr);
|
||||
VERIFY(start->type() == Dwarf::AttributeValue::Type::Address);
|
||||
|
||||
// DW_AT_high_pc attribute can have different meanings depending on the attribute form.
|
||||
// (Dwarf version 5, section 2.17.2).
|
||||
|
||||
uint32_t range_end = 0;
|
||||
if (end->form == Dwarf::AttributeDataForm::Addr)
|
||||
range_end = end->data.as_addr;
|
||||
if (end->form() == Dwarf::AttributeDataForm::Addr)
|
||||
range_end = end->as_addr();
|
||||
else
|
||||
range_end = start->data.as_unsigned + end->data.as_unsigned;
|
||||
range_end = start->as_addr() + end->as_unsigned();
|
||||
|
||||
return { DIERange { (FlatPtr)start.value().data.as_addr, range_end } };
|
||||
return { DIERange { start.value().as_addr(), range_end } };
|
||||
};
|
||||
|
||||
// If we simply use a lambda, type deduction fails because it's used recursively.
|
||||
|
|
|
@ -60,10 +60,10 @@ void LineProgram::parse_path_entries(Function<void(PathEntry& entry)> callback,
|
|||
auto value = m_dwarf_info.get_attribute_value(format_description.form, 0, m_stream);
|
||||
switch (format_description.type) {
|
||||
case ContentType::Path:
|
||||
entry.path = value.data.as_string;
|
||||
entry.path = value.as_string();
|
||||
break;
|
||||
case ContentType::DirectoryIndex:
|
||||
entry.directory_index = value.data.as_unsigned;
|
||||
entry.directory_index = value.as_unsigned();
|
||||
break;
|
||||
default:
|
||||
dbgln_if(DWARF_DEBUG, "Unhandled path list attribute: {}", (int)format_description.type);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue