mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 18:17:45 +00:00
LibDebug: Propagate errors throughout DWARF parsing
Splitting this into a separate commit was an afterthought, so this does not yet feature any fallible operations.
This commit is contained in:
parent
e235c42e4d
commit
e62269650a
17 changed files with 243 additions and 204 deletions
|
@ -34,13 +34,13 @@ ErrorOr<void> AddressRangesV5::for_each_range(Function<void(Range)> callback)
|
|||
case RangeListEntryType::BaseAddressX: {
|
||||
FlatPtr index;
|
||||
LEB128::read_unsigned(wrapped_range_lists_stream, index);
|
||||
current_base_address = m_compilation_unit.get_address(index);
|
||||
current_base_address = TRY(m_compilation_unit.get_address(index));
|
||||
break;
|
||||
}
|
||||
case RangeListEntryType::OffsetPair: {
|
||||
Optional<FlatPtr> base_address = current_base_address;
|
||||
if (!base_address.has_value()) {
|
||||
base_address = m_compilation_unit.base_address();
|
||||
base_address = TRY(m_compilation_unit.base_address());
|
||||
}
|
||||
|
||||
if (!base_address.has_value())
|
||||
|
@ -63,14 +63,14 @@ ErrorOr<void> AddressRangesV5::for_each_range(Function<void(Range)> callback)
|
|||
size_t start, end;
|
||||
LEB128::read_unsigned(wrapped_range_lists_stream, start);
|
||||
LEB128::read_unsigned(wrapped_range_lists_stream, end);
|
||||
callback(Range { m_compilation_unit.get_address(start), m_compilation_unit.get_address(end) });
|
||||
callback(Range { TRY(m_compilation_unit.get_address(start)), TRY(m_compilation_unit.get_address(end)) });
|
||||
break;
|
||||
}
|
||||
case RangeListEntryType::StartXLength: {
|
||||
size_t start, length;
|
||||
LEB128::read_unsigned(wrapped_range_lists_stream, start);
|
||||
LEB128::read_unsigned(wrapped_range_lists_stream, length);
|
||||
auto start_addr = m_compilation_unit.get_address(start);
|
||||
auto start_addr = TRY(m_compilation_unit.get_address(start));
|
||||
callback(Range { start_addr, start_addr + length });
|
||||
break;
|
||||
}
|
||||
|
@ -106,7 +106,7 @@ ErrorOr<void> AddressRangesV4::for_each_range(Function<void(Range)> callback)
|
|||
} else if (begin == explode_byte(0xff)) {
|
||||
current_base_address = end;
|
||||
} else {
|
||||
FlatPtr base = current_base_address.value_or(m_compilation_unit.base_address().value_or(0));
|
||||
FlatPtr base = current_base_address.value_or(TRY(m_compilation_unit.base_address()).value_or(0));
|
||||
callback({ base + begin, base + end });
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
namespace Debug::Dwarf {
|
||||
|
||||
FlatPtr AttributeValue::as_addr() const
|
||||
ErrorOr<FlatPtr> AttributeValue::as_addr() const
|
||||
{
|
||||
switch (m_form) {
|
||||
case AttributeDataForm::Addr:
|
||||
|
@ -27,7 +27,7 @@ FlatPtr AttributeValue::as_addr() const
|
|||
}
|
||||
}
|
||||
|
||||
char const* AttributeValue::as_string() const
|
||||
ErrorOr<char const*> AttributeValue::as_string() const
|
||||
{
|
||||
switch (m_form) {
|
||||
case AttributeDataForm::String:
|
||||
|
|
|
@ -33,10 +33,10 @@ public:
|
|||
Type type() const { return m_type; }
|
||||
AttributeDataForm form() const { return m_form; }
|
||||
|
||||
FlatPtr as_addr() const;
|
||||
ErrorOr<FlatPtr> as_addr() const;
|
||||
u64 as_unsigned() const { return m_data.as_unsigned; }
|
||||
i64 as_signed() const { return m_data.as_signed; }
|
||||
char const* as_string() const;
|
||||
ErrorOr<char const*> as_string() const;
|
||||
bool as_bool() const { return m_data.as_bool; }
|
||||
ReadonlyBytes as_raw_bytes() const { return m_data.as_raw_bytes; }
|
||||
|
||||
|
|
|
@ -40,27 +40,27 @@ LineProgram const& CompilationUnit::line_program() const
|
|||
return *m_line_program;
|
||||
}
|
||||
|
||||
Optional<FlatPtr> CompilationUnit::base_address() const
|
||||
ErrorOr<Optional<FlatPtr>> CompilationUnit::base_address() const
|
||||
{
|
||||
if (m_has_cached_base_address)
|
||||
return m_cached_base_address;
|
||||
|
||||
auto die = root_die();
|
||||
auto res = die.get_attribute(Attribute::LowPc);
|
||||
auto res = TRY(die.get_attribute(Attribute::LowPc));
|
||||
if (res.has_value()) {
|
||||
m_cached_base_address = res->as_addr();
|
||||
m_cached_base_address = TRY(res->as_addr());
|
||||
}
|
||||
m_has_cached_base_address = true;
|
||||
return m_cached_base_address;
|
||||
}
|
||||
|
||||
u64 CompilationUnit::address_table_base() const
|
||||
ErrorOr<u64> CompilationUnit::address_table_base() const
|
||||
{
|
||||
if (m_has_cached_address_table_base)
|
||||
return m_cached_address_table_base;
|
||||
|
||||
auto die = root_die();
|
||||
auto res = die.get_attribute(Attribute::AddrBase);
|
||||
auto res = TRY(die.get_attribute(Attribute::AddrBase));
|
||||
if (res.has_value()) {
|
||||
VERIFY(res->form() == AttributeDataForm::SecOffset);
|
||||
m_cached_address_table_base = res->as_unsigned();
|
||||
|
@ -69,13 +69,13 @@ u64 CompilationUnit::address_table_base() const
|
|||
return m_cached_address_table_base;
|
||||
}
|
||||
|
||||
u64 CompilationUnit::string_offsets_base() const
|
||||
ErrorOr<u64> CompilationUnit::string_offsets_base() const
|
||||
{
|
||||
if (m_has_cached_string_offsets_base)
|
||||
return m_cached_string_offsets_base;
|
||||
|
||||
auto die = root_die();
|
||||
auto res = die.get_attribute(Attribute::StrOffsetsBase);
|
||||
auto res = TRY(die.get_attribute(Attribute::StrOffsetsBase));
|
||||
if (res.has_value()) {
|
||||
VERIFY(res->form() == AttributeDataForm::SecOffset);
|
||||
m_cached_string_offsets_base = res->as_unsigned();
|
||||
|
@ -84,13 +84,13 @@ u64 CompilationUnit::string_offsets_base() const
|
|||
return m_cached_string_offsets_base;
|
||||
}
|
||||
|
||||
u64 CompilationUnit::range_lists_base() const
|
||||
ErrorOr<u64> CompilationUnit::range_lists_base() const
|
||||
{
|
||||
if (m_has_cached_range_lists_base)
|
||||
return m_cached_range_lists_base;
|
||||
|
||||
auto die = root_die();
|
||||
auto res = die.get_attribute(Attribute::RngListsBase);
|
||||
auto res = TRY(die.get_attribute(Attribute::RngListsBase));
|
||||
if (res.has_value()) {
|
||||
VERIFY(res->form() == AttributeDataForm::SecOffset);
|
||||
m_cached_range_lists_base = res->as_unsigned();
|
||||
|
@ -99,9 +99,9 @@ u64 CompilationUnit::range_lists_base() const
|
|||
return m_cached_range_lists_base;
|
||||
}
|
||||
|
||||
FlatPtr CompilationUnit::get_address(size_t index) const
|
||||
ErrorOr<FlatPtr> CompilationUnit::get_address(size_t index) const
|
||||
{
|
||||
auto base = address_table_base();
|
||||
auto base = TRY(address_table_base());
|
||||
auto debug_addr_data = dwarf_info().debug_addr_data();
|
||||
VERIFY(base < debug_addr_data.size());
|
||||
auto addresses = debug_addr_data.slice(base);
|
||||
|
@ -111,9 +111,9 @@ FlatPtr CompilationUnit::get_address(size_t index) const
|
|||
return value;
|
||||
}
|
||||
|
||||
char const* CompilationUnit::get_string(size_t index) const
|
||||
ErrorOr<char const*> CompilationUnit::get_string(size_t index) const
|
||||
{
|
||||
auto base = string_offsets_base();
|
||||
auto base = TRY(string_offsets_base());
|
||||
auto debug_str_offsets_data = dwarf_info().debug_str_offsets_data();
|
||||
VERIFY(base < debug_str_offsets_data.size());
|
||||
// FIXME: This assumes DWARF32
|
||||
|
|
|
@ -31,22 +31,22 @@ public:
|
|||
DIE root_die() const;
|
||||
DIE get_die_at_offset(u32 offset) const;
|
||||
|
||||
FlatPtr get_address(size_t index) const;
|
||||
char const* get_string(size_t index) const;
|
||||
ErrorOr<FlatPtr> get_address(size_t index) const;
|
||||
ErrorOr<char const*> get_string(size_t index) const;
|
||||
|
||||
u8 dwarf_version() const { return m_header.version(); }
|
||||
|
||||
DwarfInfo const& dwarf_info() const { return m_dwarf_info; }
|
||||
AbbreviationsMap const& abbreviations_map() const { return m_abbreviations; }
|
||||
LineProgram const& line_program() const;
|
||||
Optional<FlatPtr> base_address() const;
|
||||
ErrorOr<Optional<FlatPtr>> base_address() const;
|
||||
|
||||
// DW_AT_addr_base
|
||||
u64 address_table_base() const;
|
||||
ErrorOr<u64> address_table_base() const;
|
||||
// DW_AT_str_offsets_base
|
||||
u64 string_offsets_base() const;
|
||||
ErrorOr<u64> string_offsets_base() const;
|
||||
// DW_AT_rnglists_base
|
||||
u64 range_lists_base() const;
|
||||
ErrorOr<u64> range_lists_base() const;
|
||||
|
||||
private:
|
||||
DwarfInfo const& m_dwarf_info;
|
||||
|
|
|
@ -15,10 +15,10 @@ namespace Debug::Dwarf {
|
|||
DIE::DIE(CompilationUnit const& unit, u32 offset, Optional<u32> parent_offset)
|
||||
: m_compilation_unit(unit)
|
||||
{
|
||||
rehydrate_from(offset, parent_offset);
|
||||
rehydrate_from(offset, parent_offset).release_value_but_fixme_should_propagate_errors();
|
||||
}
|
||||
|
||||
void DIE::rehydrate_from(u32 offset, Optional<u32> parent_offset)
|
||||
ErrorOr<void> DIE::rehydrate_from(u32 offset, Optional<u32> parent_offset)
|
||||
{
|
||||
m_offset = offset;
|
||||
|
||||
|
@ -39,14 +39,15 @@ void DIE::rehydrate_from(u32 offset, Optional<u32> parent_offset)
|
|||
|
||||
// We iterate the attributes data only to calculate this DIE's size
|
||||
for (auto& attribute_spec : abbreviation_info->attribute_specifications) {
|
||||
m_compilation_unit.dwarf_info().get_attribute_value(attribute_spec.form, attribute_spec.value, stream, &m_compilation_unit);
|
||||
TRY(m_compilation_unit.dwarf_info().get_attribute_value(attribute_spec.form, attribute_spec.value, stream, &m_compilation_unit));
|
||||
}
|
||||
}
|
||||
m_size = stream.offset() - m_offset;
|
||||
m_parent_offset = parent_offset;
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<AttributeValue> DIE::get_attribute(Attribute const& attribute) const
|
||||
ErrorOr<Optional<AttributeValue>> DIE::get_attribute(Attribute const& attribute) const
|
||||
{
|
||||
InputMemoryStream stream { m_compilation_unit.dwarf_info().debug_info_data() };
|
||||
stream.discard_or_error(m_data_offset);
|
||||
|
@ -55,42 +56,45 @@ Optional<AttributeValue> DIE::get_attribute(Attribute const& attribute) const
|
|||
VERIFY(abbreviation_info);
|
||||
|
||||
for (auto const& attribute_spec : abbreviation_info->attribute_specifications) {
|
||||
auto value = m_compilation_unit.dwarf_info().get_attribute_value(attribute_spec.form, attribute_spec.value, stream, &m_compilation_unit);
|
||||
auto value = TRY(m_compilation_unit.dwarf_info().get_attribute_value(attribute_spec.form, attribute_spec.value, stream, &m_compilation_unit));
|
||||
if (attribute_spec.attribute == attribute) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
return Optional<AttributeValue> {};
|
||||
}
|
||||
|
||||
void DIE::for_each_child(Function<void(DIE const& child)> callback) const
|
||||
ErrorOr<void> DIE::for_each_child(Function<ErrorOr<void>(DIE const& child)> callback) const
|
||||
{
|
||||
if (!m_has_children)
|
||||
return;
|
||||
return {};
|
||||
|
||||
auto current_child = DIE(m_compilation_unit, m_offset + m_size, m_offset);
|
||||
while (true) {
|
||||
callback(current_child);
|
||||
TRY(callback(current_child));
|
||||
if (current_child.is_null())
|
||||
break;
|
||||
if (!current_child.has_children()) {
|
||||
current_child.rehydrate_from(current_child.offset() + current_child.size(), m_offset);
|
||||
TRY(current_child.rehydrate_from(current_child.offset() + current_child.size(), m_offset));
|
||||
continue;
|
||||
}
|
||||
|
||||
auto sibling = current_child.get_attribute(Attribute::Sibling);
|
||||
auto sibling = TRY(current_child.get_attribute(Attribute::Sibling));
|
||||
u32 sibling_offset = 0;
|
||||
if (sibling.has_value()) {
|
||||
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
|
||||
current_child.for_each_child([&](DIE const& sub_child) {
|
||||
TRY(current_child.for_each_child([&](DIE const& sub_child) -> ErrorOr<void> {
|
||||
sibling_offset = sub_child.offset() + sub_child.size();
|
||||
});
|
||||
return {};
|
||||
}));
|
||||
}
|
||||
current_child.rehydrate_from(sibling_offset, m_offset);
|
||||
TRY(current_child.rehydrate_from(sibling_offset, m_offset));
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -27,16 +27,16 @@ public:
|
|||
bool has_children() const { return m_has_children; }
|
||||
EntryTag tag() const { return m_tag; }
|
||||
|
||||
Optional<AttributeValue> get_attribute(Attribute const&) const;
|
||||
ErrorOr<Optional<AttributeValue>> get_attribute(Attribute const&) const;
|
||||
|
||||
void for_each_child(Function<void(DIE const& child)> callback) const;
|
||||
ErrorOr<void> for_each_child(Function<ErrorOr<void>(DIE const& child)> callback) const;
|
||||
|
||||
bool is_null() const { return m_tag == EntryTag::None; }
|
||||
CompilationUnit const& compilation_unit() const { return m_compilation_unit; }
|
||||
Optional<u32> parent_offset() const { return m_parent_offset; }
|
||||
|
||||
private:
|
||||
void rehydrate_from(u32 offset, Optional<u32> parent_offset);
|
||||
ErrorOr<void> rehydrate_from(u32 offset, Optional<u32> parent_offset);
|
||||
CompilationUnit const& m_compilation_unit;
|
||||
u32 m_offset { 0 };
|
||||
u32 m_data_offset { 0 };
|
||||
|
|
|
@ -29,7 +29,7 @@ DwarfInfo::DwarfInfo(ELF::Image const& elf)
|
|||
m_debug_addr_data = section_data(".debug_addr"sv);
|
||||
m_debug_ranges_data = section_data(".debug_ranges"sv);
|
||||
|
||||
populate_compilation_units();
|
||||
populate_compilation_units().release_value_but_fixme_should_propagate_errors();
|
||||
}
|
||||
|
||||
DwarfInfo::~DwarfInfo() = default;
|
||||
|
@ -42,10 +42,10 @@ ReadonlyBytes DwarfInfo::section_data(StringView section_name) const
|
|||
return section->bytes();
|
||||
}
|
||||
|
||||
void DwarfInfo::populate_compilation_units()
|
||||
ErrorOr<void> DwarfInfo::populate_compilation_units()
|
||||
{
|
||||
if (!m_debug_info_data.data())
|
||||
return;
|
||||
return {};
|
||||
|
||||
InputMemoryStream debug_info_stream { m_debug_info_data };
|
||||
InputMemoryStream line_info_stream { m_debug_line_data };
|
||||
|
@ -77,20 +77,22 @@ void DwarfInfo::populate_compilation_units()
|
|||
}
|
||||
|
||||
VERIFY(line_info_stream.eof());
|
||||
return {};
|
||||
}
|
||||
|
||||
AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t implicit_const_value,
|
||||
ErrorOr<AttributeValue> DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t implicit_const_value,
|
||||
InputMemoryStream& debug_info_stream, CompilationUnit const* unit) const
|
||||
{
|
||||
AttributeValue value;
|
||||
value.m_form = form;
|
||||
value.m_compilation_unit = unit;
|
||||
|
||||
auto assign_raw_bytes_value = [&](size_t length) {
|
||||
auto assign_raw_bytes_value = [&](size_t length) -> ErrorOr<void> {
|
||||
value.m_data.as_raw_bytes = { debug_info_data().offset_pointer(debug_info_stream.offset()), length };
|
||||
|
||||
debug_info_stream.discard_or_error(length);
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
return {};
|
||||
};
|
||||
|
||||
switch (form) {
|
||||
|
@ -170,7 +172,7 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
|
|||
}
|
||||
case AttributeDataForm::Data16: {
|
||||
value.m_type = AttributeValue::Type::RawBytes;
|
||||
assign_raw_bytes_value(16);
|
||||
TRY(assign_raw_bytes_value(16));
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
break;
|
||||
}
|
||||
|
@ -193,7 +195,7 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
|
|||
debug_info_stream.read_LEB128_unsigned(length);
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
value.m_type = AttributeValue::Type::DwarfExpression;
|
||||
assign_raw_bytes_value(length);
|
||||
TRY(assign_raw_bytes_value(length));
|
||||
break;
|
||||
}
|
||||
case AttributeDataForm::String: {
|
||||
|
@ -210,7 +212,7 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
|
|||
u8 length;
|
||||
debug_info_stream >> length;
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
assign_raw_bytes_value(length);
|
||||
TRY(assign_raw_bytes_value(length));
|
||||
break;
|
||||
}
|
||||
case AttributeDataForm::Block2: {
|
||||
|
@ -218,7 +220,7 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
|
|||
u16 length;
|
||||
debug_info_stream >> length;
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
assign_raw_bytes_value(length);
|
||||
TRY(assign_raw_bytes_value(length));
|
||||
break;
|
||||
}
|
||||
case AttributeDataForm::Block4: {
|
||||
|
@ -226,7 +228,7 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
|
|||
u32 length;
|
||||
debug_info_stream >> length;
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
assign_raw_bytes_value(length);
|
||||
TRY(assign_raw_bytes_value(length));
|
||||
break;
|
||||
}
|
||||
case AttributeDataForm::Block: {
|
||||
|
@ -234,7 +236,7 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
|
|||
size_t length;
|
||||
debug_info_stream.read_LEB128_unsigned(length);
|
||||
VERIFY(!debug_info_stream.has_any_error());
|
||||
assign_raw_bytes_value(length);
|
||||
TRY(assign_raw_bytes_value(length));
|
||||
break;
|
||||
}
|
||||
case AttributeDataForm::LineStrP: {
|
||||
|
@ -332,21 +334,21 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
|
|||
return value;
|
||||
}
|
||||
|
||||
void DwarfInfo::build_cached_dies() const
|
||||
ErrorOr<void> DwarfInfo::build_cached_dies() const
|
||||
{
|
||||
auto insert_to_cache = [this](DIE const& die, DIERange const& range) {
|
||||
m_cached_dies_by_range.insert(range.start_address, DIEAndRange { die, range });
|
||||
m_cached_dies_by_offset.insert(die.offset(), die);
|
||||
};
|
||||
auto get_ranges_of_die = [this](DIE const& die) -> ErrorOr<Vector<DIERange>> {
|
||||
auto ranges = die.get_attribute(Attribute::Ranges);
|
||||
auto ranges = TRY(die.get_attribute(Attribute::Ranges));
|
||||
if (ranges.has_value()) {
|
||||
size_t offset;
|
||||
if (ranges->form() == AttributeDataForm::SecOffset) {
|
||||
offset = ranges->as_unsigned();
|
||||
} else {
|
||||
auto index = ranges->as_unsigned();
|
||||
auto base = die.compilation_unit().range_lists_base();
|
||||
auto base = TRY(die.compilation_unit().range_lists_base());
|
||||
// FIXME: This assumes that the format is DWARf32
|
||||
auto offsets = debug_range_lists_data().slice(base);
|
||||
offset = ByteReader::load32(offsets.offset_pointer(index * sizeof(u32))) + base;
|
||||
|
@ -371,8 +373,8 @@ void DwarfInfo::build_cached_dies() const
|
|||
return entries;
|
||||
}
|
||||
|
||||
auto start = die.get_attribute(Attribute::LowPc);
|
||||
auto end = die.get_attribute(Attribute::HighPc);
|
||||
auto start = TRY(die.get_attribute(Attribute::LowPc));
|
||||
auto end = TRY(die.get_attribute(Attribute::HighPc));
|
||||
|
||||
if (!start.has_value() || !end.has_value())
|
||||
return Vector<DIERange> {};
|
||||
|
@ -384,40 +386,44 @@ void DwarfInfo::build_cached_dies() const
|
|||
|
||||
uint32_t range_end = 0;
|
||||
if (end->form() == Dwarf::AttributeDataForm::Addr)
|
||||
range_end = end->as_addr();
|
||||
range_end = TRY(end->as_addr());
|
||||
else
|
||||
range_end = start->as_addr() + end->as_unsigned();
|
||||
range_end = TRY(start->as_addr()) + end->as_unsigned();
|
||||
|
||||
return Vector<DIERange> { DIERange { start.value().as_addr(), range_end } };
|
||||
return Vector<DIERange> { DIERange { TRY(start.value().as_addr()), range_end } };
|
||||
};
|
||||
|
||||
// If we simply use a lambda, type deduction fails because it's used recursively.
|
||||
Function<void(DIE const& die)> insert_to_cache_recursively;
|
||||
insert_to_cache_recursively = [&](DIE const& die) {
|
||||
Function<ErrorOr<void>(DIE const& die)> insert_to_cache_recursively;
|
||||
insert_to_cache_recursively = [&](DIE const& die) -> ErrorOr<void> {
|
||||
if (die.offset() == 0 || die.parent_offset().has_value()) {
|
||||
auto ranges = get_ranges_of_die(die).release_value_but_fixme_should_propagate_errors();
|
||||
auto ranges = TRY(get_ranges_of_die(die));
|
||||
for (auto& range : ranges) {
|
||||
insert_to_cache(die, range);
|
||||
}
|
||||
}
|
||||
die.for_each_child([&](DIE const& child) {
|
||||
TRY(die.for_each_child([&](DIE const& child) -> ErrorOr<void> {
|
||||
if (!child.is_null()) {
|
||||
insert_to_cache_recursively(child);
|
||||
TRY(insert_to_cache_recursively(child));
|
||||
}
|
||||
});
|
||||
return {};
|
||||
}));
|
||||
return {};
|
||||
};
|
||||
|
||||
for_each_compilation_unit([&](CompilationUnit const& compilation_unit) {
|
||||
insert_to_cache_recursively(compilation_unit.root_die());
|
||||
});
|
||||
TRY(for_each_compilation_unit([&](CompilationUnit const& compilation_unit) -> ErrorOr<void> {
|
||||
TRY(insert_to_cache_recursively(compilation_unit.root_die()));
|
||||
return {};
|
||||
}));
|
||||
|
||||
m_built_cached_dies = true;
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<DIE> DwarfInfo::get_die_at_address(FlatPtr address) const
|
||||
ErrorOr<Optional<DIE>> DwarfInfo::get_die_at_address(FlatPtr address) const
|
||||
{
|
||||
if (!m_built_cached_dies)
|
||||
build_cached_dies();
|
||||
TRY(build_cached_dies());
|
||||
|
||||
auto iter = m_cached_dies_by_range.find_largest_not_above_iterator(address);
|
||||
while (!iter.is_end() && !iter.is_begin() && iter->range.end_address < address) {
|
||||
|
@ -425,23 +431,23 @@ Optional<DIE> DwarfInfo::get_die_at_address(FlatPtr address) const
|
|||
}
|
||||
|
||||
if (iter.is_end())
|
||||
return {};
|
||||
return Optional<DIE> {};
|
||||
|
||||
if (iter->range.start_address > address || iter->range.end_address < address) {
|
||||
return {};
|
||||
return Optional<DIE> {};
|
||||
}
|
||||
|
||||
return iter->die;
|
||||
}
|
||||
|
||||
Optional<DIE> DwarfInfo::get_cached_die_at_offset(FlatPtr offset) const
|
||||
ErrorOr<Optional<DIE>> DwarfInfo::get_cached_die_at_offset(FlatPtr offset) const
|
||||
{
|
||||
if (!m_built_cached_dies)
|
||||
build_cached_dies();
|
||||
TRY(build_cached_dies());
|
||||
|
||||
auto* die = m_cached_dies_by_offset.find(offset);
|
||||
if (!die)
|
||||
return {};
|
||||
return Optional<DIE> {};
|
||||
return *die;
|
||||
}
|
||||
|
||||
|
|
|
@ -39,23 +39,23 @@ public:
|
|||
ReadonlyBytes debug_ranges_data() const { return m_debug_ranges_data; }
|
||||
|
||||
template<typename Callback>
|
||||
void for_each_compilation_unit(Callback) const;
|
||||
ErrorOr<void> for_each_compilation_unit(Callback) const;
|
||||
|
||||
AttributeValue get_attribute_value(AttributeDataForm form, ssize_t implicit_const_value,
|
||||
ErrorOr<AttributeValue> get_attribute_value(AttributeDataForm form, ssize_t implicit_const_value,
|
||||
InputMemoryStream& debug_info_stream, CompilationUnit const* unit = nullptr) const;
|
||||
|
||||
Optional<DIE> get_die_at_address(FlatPtr) const;
|
||||
ErrorOr<Optional<DIE>> get_die_at_address(FlatPtr) const;
|
||||
|
||||
// Note that even if there is a DIE at the given offset,
|
||||
// but it does not exist in the DIE cache (because for example
|
||||
// it does not contain an address range), then this function will not return it.
|
||||
// To get any DIE object at a given offset in a compilation unit,
|
||||
// use CompilationUnit::get_die_at_offset.
|
||||
Optional<DIE> get_cached_die_at_offset(FlatPtr) const;
|
||||
ErrorOr<Optional<DIE>> get_cached_die_at_offset(FlatPtr) const;
|
||||
|
||||
private:
|
||||
void populate_compilation_units();
|
||||
void build_cached_dies() const;
|
||||
ErrorOr<void> populate_compilation_units();
|
||||
ErrorOr<void> build_cached_dies() const;
|
||||
|
||||
ReadonlyBytes section_data(StringView section_name) const;
|
||||
|
||||
|
@ -90,11 +90,12 @@ private:
|
|||
};
|
||||
|
||||
template<typename Callback>
|
||||
void DwarfInfo::for_each_compilation_unit(Callback callback) const
|
||||
ErrorOr<void> DwarfInfo::for_each_compilation_unit(Callback callback) const
|
||||
{
|
||||
for (auto const& unit : m_compilation_units) {
|
||||
callback(unit);
|
||||
TRY(callback(unit));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -18,13 +18,13 @@ LineProgram::LineProgram(DwarfInfo& dwarf_info, InputMemoryStream& stream)
|
|||
, m_stream(stream)
|
||||
{
|
||||
m_unit_offset = m_stream.offset();
|
||||
parse_unit_header();
|
||||
parse_source_directories();
|
||||
parse_source_files();
|
||||
run_program();
|
||||
parse_unit_header().release_value_but_fixme_should_propagate_errors();
|
||||
parse_source_directories().release_value_but_fixme_should_propagate_errors();
|
||||
parse_source_files().release_value_but_fixme_should_propagate_errors();
|
||||
run_program().release_value_but_fixme_should_propagate_errors();
|
||||
}
|
||||
|
||||
void LineProgram::parse_unit_header()
|
||||
ErrorOr<void> LineProgram::parse_unit_header()
|
||||
{
|
||||
m_stream >> m_unit_header;
|
||||
|
||||
|
@ -32,9 +32,10 @@ void LineProgram::parse_unit_header()
|
|||
VERIFY(m_unit_header.opcode_base() <= sizeof(m_unit_header.std_opcode_lengths) / sizeof(m_unit_header.std_opcode_lengths[0]) + 1);
|
||||
|
||||
dbgln_if(DWARF_DEBUG, "unit length: {}", m_unit_header.length());
|
||||
return {};
|
||||
}
|
||||
|
||||
void LineProgram::parse_path_entries(Function<void(PathEntry& entry)> callback, PathListType list_type)
|
||||
ErrorOr<void> LineProgram::parse_path_entries(Function<void(PathEntry& entry)> callback, PathListType list_type)
|
||||
{
|
||||
if (m_unit_header.version() >= 5) {
|
||||
u8 path_entry_format_count = 0;
|
||||
|
@ -58,10 +59,10 @@ void LineProgram::parse_path_entries(Function<void(PathEntry& entry)> callback,
|
|||
for (size_t i = 0; i < paths_count; i++) {
|
||||
PathEntry entry;
|
||||
for (auto& format_description : format_descriptions) {
|
||||
auto value = m_dwarf_info.get_attribute_value(format_description.form, 0, m_stream);
|
||||
auto value = TRY(m_dwarf_info.get_attribute_value(format_description.form, 0, m_stream));
|
||||
switch (format_description.type) {
|
||||
case ContentType::Path:
|
||||
entry.path = value.as_string();
|
||||
entry.path = TRY(value.as_string());
|
||||
break;
|
||||
case ContentType::DirectoryIndex:
|
||||
entry.directory_index = value.as_unsigned();
|
||||
|
@ -96,30 +97,35 @@ void LineProgram::parse_path_entries(Function<void(PathEntry& entry)> callback,
|
|||
}
|
||||
|
||||
VERIFY(!m_stream.has_any_error());
|
||||
return {};
|
||||
}
|
||||
|
||||
void LineProgram::parse_source_directories()
|
||||
ErrorOr<void> LineProgram::parse_source_directories()
|
||||
{
|
||||
if (m_unit_header.version() < 5) {
|
||||
m_source_directories.append(".");
|
||||
}
|
||||
|
||||
parse_path_entries([this](PathEntry& entry) {
|
||||
TRY(parse_path_entries([this](PathEntry& entry) {
|
||||
m_source_directories.append(entry.path);
|
||||
},
|
||||
PathListType::Directories);
|
||||
PathListType::Directories));
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void LineProgram::parse_source_files()
|
||||
ErrorOr<void> LineProgram::parse_source_files()
|
||||
{
|
||||
if (m_unit_header.version() < 5) {
|
||||
m_source_files.append({ ".", 0 });
|
||||
}
|
||||
|
||||
parse_path_entries([this](PathEntry& entry) {
|
||||
TRY(parse_path_entries([this](PathEntry& entry) {
|
||||
m_source_files.append({ entry.path, entry.directory_index });
|
||||
},
|
||||
PathListType::Filenames);
|
||||
PathListType::Filenames));
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void LineProgram::append_to_line_info()
|
||||
|
@ -149,7 +155,7 @@ void LineProgram::reset_registers()
|
|||
m_is_statement = m_unit_header.default_is_stmt() == 1;
|
||||
}
|
||||
|
||||
void LineProgram::handle_extended_opcode()
|
||||
ErrorOr<void> LineProgram::handle_extended_opcode()
|
||||
{
|
||||
size_t length = 0;
|
||||
m_stream.read_LEB128_unsigned(length);
|
||||
|
@ -179,8 +185,10 @@ void LineProgram::handle_extended_opcode()
|
|||
dbgln("Encountered unknown sub opcode {} at stream offset {:p}", sub_opcode, m_stream.offset());
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
void LineProgram::handle_standard_opcode(u8 opcode)
|
||||
ErrorOr<void> LineProgram::handle_standard_opcode(u8 opcode)
|
||||
{
|
||||
switch (opcode) {
|
||||
case StandardOpcodes::Copy: {
|
||||
|
@ -256,6 +264,8 @@ void LineProgram::handle_standard_opcode(u8 opcode)
|
|||
dbgln("Unhandled LineProgram opcode {}", opcode);
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
void LineProgram::handle_special_opcode(u8 opcode)
|
||||
{
|
||||
|
@ -277,7 +287,7 @@ void LineProgram::handle_special_opcode(u8 opcode)
|
|||
m_prologue_end = false;
|
||||
}
|
||||
|
||||
void LineProgram::run_program()
|
||||
ErrorOr<void> LineProgram::run_program()
|
||||
{
|
||||
reset_registers();
|
||||
|
||||
|
@ -288,13 +298,15 @@ void LineProgram::run_program()
|
|||
dbgln_if(DWARF_DEBUG, "{:p}: opcode: {}", m_stream.offset() - 1, opcode);
|
||||
|
||||
if (opcode == 0) {
|
||||
handle_extended_opcode();
|
||||
TRY(handle_extended_opcode());
|
||||
} else if (opcode >= 1 && opcode <= 12) {
|
||||
handle_standard_opcode(opcode);
|
||||
TRY(handle_standard_opcode(opcode));
|
||||
} else {
|
||||
handle_special_opcode(opcode);
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
LineProgram::DirectoryAndFile LineProgram::get_directory_and_file(size_t file_index) const
|
||||
|
|
|
@ -133,19 +133,19 @@ public:
|
|||
bool looks_like_embedded_resource() const;
|
||||
|
||||
private:
|
||||
void parse_unit_header();
|
||||
void parse_source_directories();
|
||||
void parse_source_files();
|
||||
void run_program();
|
||||
ErrorOr<void> parse_unit_header();
|
||||
ErrorOr<void> parse_source_directories();
|
||||
ErrorOr<void> parse_source_files();
|
||||
ErrorOr<void> run_program();
|
||||
|
||||
void append_to_line_info();
|
||||
void reset_registers();
|
||||
|
||||
void handle_extended_opcode();
|
||||
void handle_standard_opcode(u8 opcode);
|
||||
ErrorOr<void> handle_extended_opcode();
|
||||
ErrorOr<void> handle_standard_opcode(u8 opcode);
|
||||
void handle_special_opcode(u8 opcode);
|
||||
|
||||
void parse_path_entries(Function<void(PathEntry& entry)> callback, PathListType list_type);
|
||||
ErrorOr<void> parse_path_entries(Function<void(PathEntry& entry)> callback, PathListType list_type);
|
||||
|
||||
enum StandardOpcodes {
|
||||
Copy = 1,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue