1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 22:17:42 +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:
Tim Schumacher 2023-01-22 00:32:08 +01:00 committed by Andreas Kling
parent e235c42e4d
commit e62269650a
17 changed files with 243 additions and 204 deletions

View file

@ -168,7 +168,7 @@ RefPtr<VariablesModel> VariablesModel::create(Debug::ProcessInspector& inspector
auto lib = inspector.library_at(regs.ip()); auto lib = inspector.library_at(regs.ip());
if (!lib) if (!lib)
return nullptr; return nullptr;
auto variables = lib->debug_info->get_variables_in_current_scope(regs); auto variables = lib->debug_info->get_variables_in_current_scope(regs).release_value_but_fixme_should_propagate_errors();
return adopt_ref(*new VariablesModel(inspector, move(variables), regs)); return adopt_ref(*new VariablesModel(inspector, move(variables), regs));
} }

View file

@ -116,8 +116,9 @@ DisassemblyModel::DisassemblyModel(Profile& profile, ProfileNode& node)
StringView instruction_bytes = view.substring_view(offset_into_symbol, insn.value().length()); StringView instruction_bytes = view.substring_view(offset_into_symbol, insn.value().length());
u32 samples_at_this_instruction = m_node.events_per_address().get(address_in_profiled_program).value_or(0); u32 samples_at_this_instruction = m_node.events_per_address().get(address_in_profiled_program).value_or(0);
float percent = ((float)samples_at_this_instruction / (float)m_node.event_count()) * 100.0f; float percent = ((float)samples_at_this_instruction / (float)m_node.event_count()) * 100.0f;
auto source_position = debug_info->get_source_position_with_inlines(address_in_profiled_program - base_address).release_value_but_fixme_should_propagate_errors();
m_instructions.append({ insn.value(), disassembly, instruction_bytes, address_in_profiled_program, samples_at_this_instruction, percent, debug_info->get_source_position_with_inlines(address_in_profiled_program - base_address) }); m_instructions.append({ insn.value(), disassembly, instruction_bytes, address_in_profiled_program, samples_at_this_instruction, percent, source_position });
offset_into_symbol += insn.value().length(); offset_into_symbol += insn.value().length();
} }

View file

@ -121,7 +121,7 @@ void Backtrace::add_entry(Reader const& coredump, FlatPtr ip)
} }
auto function_name = object_info->debug_info->elf().symbolicate(ip - region->region_start); auto function_name = object_info->debug_info->elf().symbolicate(ip - region->region_start);
auto source_position = object_info->debug_info->get_source_position_with_inlines(ip - region->region_start); auto source_position = object_info->debug_info->get_source_position_with_inlines(ip - region->region_start).release_value_but_fixme_should_propagate_errors();
m_entries.append({ ip, object_name, function_name, source_position }); m_entries.append({ ip, object_name, function_name, source_position });
} }

View file

@ -21,70 +21,77 @@ DebugInfo::DebugInfo(ELF::Image const& elf, DeprecatedString source_root, FlatPt
, m_base_address(base_address) , m_base_address(base_address)
, m_dwarf_info(m_elf) , m_dwarf_info(m_elf)
{ {
prepare_variable_scopes(); prepare_variable_scopes().release_value_but_fixme_should_propagate_errors();
prepare_lines(); prepare_lines().release_value_but_fixme_should_propagate_errors();
} }
void DebugInfo::prepare_variable_scopes() ErrorOr<void> DebugInfo::prepare_variable_scopes()
{ {
m_dwarf_info.for_each_compilation_unit([&](Dwarf::CompilationUnit const& unit) { TRY(m_dwarf_info.for_each_compilation_unit([&](Dwarf::CompilationUnit const& unit) -> ErrorOr<void> {
auto root = unit.root_die(); auto root = unit.root_die();
parse_scopes_impl(root); TRY(parse_scopes_impl(root));
}); return {};
}));
return {};
} }
void DebugInfo::parse_scopes_impl(Dwarf::DIE const& die) ErrorOr<void> DebugInfo::parse_scopes_impl(Dwarf::DIE const& die)
{ {
die.for_each_child([&](Dwarf::DIE const& child) { TRY(die.for_each_child([&](Dwarf::DIE const& child) -> ErrorOr<void> {
if (child.is_null()) if (child.is_null())
return; return {};
if (!(child.tag() == Dwarf::EntryTag::SubProgram || child.tag() == Dwarf::EntryTag::LexicalBlock)) if (!(child.tag() == Dwarf::EntryTag::SubProgram || child.tag() == Dwarf::EntryTag::LexicalBlock))
return; return {};
if (child.get_attribute(Dwarf::Attribute::Inline).has_value()) { if (TRY(child.get_attribute(Dwarf::Attribute::Inline)).has_value()) {
dbgln_if(SPAM_DEBUG, "DWARF inlined functions are not supported"); dbgln_if(SPAM_DEBUG, "DWARF inlined functions are not supported");
return; return {};
} }
if (child.get_attribute(Dwarf::Attribute::Ranges).has_value()) { if (TRY(child.get_attribute(Dwarf::Attribute::Ranges)).has_value()) {
dbgln_if(SPAM_DEBUG, "DWARF ranges are not supported"); dbgln_if(SPAM_DEBUG, "DWARF ranges are not supported");
return; return {};
} }
auto name = child.get_attribute(Dwarf::Attribute::Name); auto name = TRY(child.get_attribute(Dwarf::Attribute::Name));
VariablesScope scope {}; VariablesScope scope {};
scope.is_function = (child.tag() == Dwarf::EntryTag::SubProgram); scope.is_function = (child.tag() == Dwarf::EntryTag::SubProgram);
if (name.has_value()) if (name.has_value())
scope.name = name.value().as_string(); scope.name = TRY(name.value().as_string());
if (!child.get_attribute(Dwarf::Attribute::LowPc).has_value()) { if (!TRY(child.get_attribute(Dwarf::Attribute::LowPc)).has_value()) {
dbgln_if(SPAM_DEBUG, "DWARF: Couldn't find attribute LowPc for scope"); dbgln_if(SPAM_DEBUG, "DWARF: Couldn't find attribute LowPc for scope");
return; return {};
} }
scope.address_low = child.get_attribute(Dwarf::Attribute::LowPc).value().as_addr(); scope.address_low = TRY(TRY(child.get_attribute(Dwarf::Attribute::LowPc)).value().as_addr());
auto high_pc = child.get_attribute(Dwarf::Attribute::HighPc); auto high_pc = TRY(child.get_attribute(Dwarf::Attribute::HighPc));
if (high_pc->type() == Dwarf::AttributeValue::Type::Address) if (high_pc->type() == Dwarf::AttributeValue::Type::Address)
scope.address_high = high_pc->as_addr(); scope.address_high = TRY(high_pc->as_addr());
else else
scope.address_high = scope.address_low + high_pc->as_unsigned(); scope.address_high = scope.address_low + high_pc->as_unsigned();
child.for_each_child([&](Dwarf::DIE const& variable_entry) { TRY(child.for_each_child([&](Dwarf::DIE const& variable_entry) -> ErrorOr<void> {
if (!(variable_entry.tag() == Dwarf::EntryTag::Variable if (!(variable_entry.tag() == Dwarf::EntryTag::Variable
|| variable_entry.tag() == Dwarf::EntryTag::FormalParameter)) || variable_entry.tag() == Dwarf::EntryTag::FormalParameter))
return; return {};
scope.dies_of_variables.append(variable_entry); scope.dies_of_variables.append(variable_entry);
}); return {};
}));
m_scopes.append(scope); m_scopes.append(scope);
parse_scopes_impl(child); TRY(parse_scopes_impl(child));
});
return {};
}));
return {};
} }
void DebugInfo::prepare_lines() ErrorOr<void> DebugInfo::prepare_lines()
{ {
Vector<Dwarf::LineProgram::LineInfo> all_lines; Vector<Dwarf::LineProgram::LineInfo> all_lines;
m_dwarf_info.for_each_compilation_unit([&all_lines](Dwarf::CompilationUnit const& unit) { TRY(m_dwarf_info.for_each_compilation_unit([&all_lines](Dwarf::CompilationUnit const& unit) -> ErrorOr<void> {
all_lines.extend(unit.line_program().lines()); all_lines.extend(unit.line_program().lines());
}); return {};
}));
HashMap<DeprecatedFlyString, Optional<DeprecatedString>> memoized_full_paths; HashMap<DeprecatedFlyString, Optional<DeprecatedString>> memoized_full_paths;
auto compute_full_path = [&](DeprecatedFlyString const& file_path) -> Optional<DeprecatedString> { auto compute_full_path = [&](DeprecatedFlyString const& file_path) -> Optional<DeprecatedString> {
@ -111,6 +118,7 @@ void DebugInfo::prepare_lines()
quick_sort(m_sorted_lines, [](auto& a, auto& b) { quick_sort(m_sorted_lines, [](auto& a, auto& b) {
return a.address < b.address; return a.address < b.address;
}); });
return {};
} }
Optional<DebugInfo::SourcePosition> DebugInfo::get_source_position(FlatPtr target_address) const Optional<DebugInfo::SourcePosition> DebugInfo::get_source_position(FlatPtr target_address) const
@ -160,7 +168,7 @@ Optional<DebugInfo::SourcePositionAndAddress> DebugInfo::get_address_from_source
return result; return result;
} }
NonnullOwnPtrVector<DebugInfo::VariableInfo> DebugInfo::get_variables_in_current_scope(PtraceRegisters const& regs) const ErrorOr<NonnullOwnPtrVector<DebugInfo::VariableInfo>> DebugInfo::get_variables_in_current_scope(PtraceRegisters const& regs) const
{ {
NonnullOwnPtrVector<DebugInfo::VariableInfo> variables; NonnullOwnPtrVector<DebugInfo::VariableInfo> variables;
@ -178,7 +186,7 @@ NonnullOwnPtrVector<DebugInfo::VariableInfo> DebugInfo::get_variables_in_current
continue; continue;
for (auto const& die_entry : scope.dies_of_variables) { for (auto const& die_entry : scope.dies_of_variables) {
auto variable_info = create_variable_info(die_entry, regs); auto variable_info = TRY(create_variable_info(die_entry, regs));
if (!variable_info) if (!variable_info)
continue; continue;
variables.append(variable_info.release_nonnull()); variables.append(variable_info.release_nonnull());
@ -187,18 +195,18 @@ NonnullOwnPtrVector<DebugInfo::VariableInfo> DebugInfo::get_variables_in_current
return variables; return variables;
} }
static Optional<Dwarf::DIE> parse_variable_type_die(Dwarf::DIE const& variable_die, DebugInfo::VariableInfo& variable_info) static ErrorOr<Optional<Dwarf::DIE>> parse_variable_type_die(Dwarf::DIE const& variable_die, DebugInfo::VariableInfo& variable_info)
{ {
auto type_die_offset = variable_die.get_attribute(Dwarf::Attribute::Type); auto type_die_offset = TRY(variable_die.get_attribute(Dwarf::Attribute::Type));
if (!type_die_offset.has_value()) if (!type_die_offset.has_value())
return {}; return Optional<Dwarf::DIE> {};
VERIFY(type_die_offset.value().type() == Dwarf::AttributeValue::Type::DieReference); VERIFY(type_die_offset.value().type() == Dwarf::AttributeValue::Type::DieReference);
auto type_die = variable_die.compilation_unit().get_die_at_offset(type_die_offset.value().as_unsigned()); auto type_die = variable_die.compilation_unit().get_die_at_offset(type_die_offset.value().as_unsigned());
auto type_name = type_die.get_attribute(Dwarf::Attribute::Name); auto type_name = TRY(type_die.get_attribute(Dwarf::Attribute::Name));
if (type_name.has_value()) { if (type_name.has_value()) {
variable_info.type_name = type_name.value().as_string(); variable_info.type_name = TRY(type_name.value().as_string());
} else { } else {
dbgln("Unnamed DWARF type at offset: {}", type_die.offset()); dbgln("Unnamed DWARF type at offset: {}", type_die.offset());
variable_info.type_name = "[Unnamed Type]"; variable_info.type_name = "[Unnamed Type]";
@ -207,15 +215,15 @@ static Optional<Dwarf::DIE> parse_variable_type_die(Dwarf::DIE const& variable_d
return type_die; return type_die;
} }
static void parse_variable_location(Dwarf::DIE const& variable_die, DebugInfo::VariableInfo& variable_info, PtraceRegisters const& regs) static ErrorOr<void> parse_variable_location(Dwarf::DIE const& variable_die, DebugInfo::VariableInfo& variable_info, PtraceRegisters const& regs)
{ {
auto location_info = variable_die.get_attribute(Dwarf::Attribute::Location); auto location_info = TRY(variable_die.get_attribute(Dwarf::Attribute::Location));
if (!location_info.has_value()) { if (!location_info.has_value()) {
location_info = variable_die.get_attribute(Dwarf::Attribute::MemberLocation); location_info = TRY(variable_die.get_attribute(Dwarf::Attribute::MemberLocation));
} }
if (!location_info.has_value()) if (!location_info.has_value())
return; return {};
switch (location_info.value().type()) { switch (location_info.value().type()) {
case Dwarf::AttributeValue::Type::UnsignedNumber: case Dwarf::AttributeValue::Type::UnsignedNumber:
@ -236,27 +244,29 @@ static void parse_variable_location(Dwarf::DIE const& variable_die, DebugInfo::V
default: default:
dbgln("Warning: unhandled Dwarf location type: {}", to_underlying(location_info.value().type())); dbgln("Warning: unhandled Dwarf location type: {}", to_underlying(location_info.value().type()));
} }
return {};
} }
OwnPtr<DebugInfo::VariableInfo> DebugInfo::create_variable_info(Dwarf::DIE const& variable_die, PtraceRegisters const& regs, u32 address_offset) const ErrorOr<OwnPtr<DebugInfo::VariableInfo>> DebugInfo::create_variable_info(Dwarf::DIE const& variable_die, PtraceRegisters const& regs, u32 address_offset) const
{ {
VERIFY(is_variable_tag_supported(variable_die.tag())); VERIFY(is_variable_tag_supported(variable_die.tag()));
if (variable_die.tag() == Dwarf::EntryTag::FormalParameter if (variable_die.tag() == Dwarf::EntryTag::FormalParameter
&& !variable_die.get_attribute(Dwarf::Attribute::Name).has_value()) { && !TRY(variable_die.get_attribute(Dwarf::Attribute::Name)).has_value()) {
// We don't want to display info for unused parameters // We don't want to display info for unused parameters
return {}; return OwnPtr<DebugInfo::VariableInfo> {};
} }
NonnullOwnPtr<VariableInfo> variable_info = make<VariableInfo>(); NonnullOwnPtr<VariableInfo> variable_info = make<VariableInfo>();
auto name_attribute = variable_die.get_attribute(Dwarf::Attribute::Name); auto name_attribute = TRY(variable_die.get_attribute(Dwarf::Attribute::Name));
if (name_attribute.has_value()) if (name_attribute.has_value())
variable_info->name = name_attribute.value().as_string(); variable_info->name = TRY(name_attribute.value().as_string());
auto type_die = parse_variable_type_die(variable_die, *variable_info); auto type_die = TRY(parse_variable_type_die(variable_die, *variable_info));
if (variable_die.tag() == Dwarf::EntryTag::Enumerator) { if (variable_die.tag() == Dwarf::EntryTag::Enumerator) {
auto constant = variable_die.get_attribute(Dwarf::Attribute::ConstValue); auto constant = TRY(variable_die.get_attribute(Dwarf::Attribute::ConstValue));
VERIFY(constant.has_value()); VERIFY(constant.has_value());
switch (constant.value().type()) { switch (constant.value().type()) {
case Dwarf::AttributeValue::Type::UnsignedNumber: case Dwarf::AttributeValue::Type::UnsignedNumber:
@ -266,23 +276,23 @@ OwnPtr<DebugInfo::VariableInfo> DebugInfo::create_variable_info(Dwarf::DIE const
variable_info->constant_data.as_i32 = constant.value().as_signed(); variable_info->constant_data.as_i32 = constant.value().as_signed();
break; break;
case Dwarf::AttributeValue::Type::String: case Dwarf::AttributeValue::Type::String:
variable_info->constant_data.as_string = constant.value().as_string(); variable_info->constant_data.as_string = TRY(constant.value().as_string());
break; break;
default: default:
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
} else { } else {
parse_variable_location(variable_die, *variable_info, regs); TRY(parse_variable_location(variable_die, *variable_info, regs));
variable_info->location_data.address += address_offset; variable_info->location_data.address += address_offset;
} }
if (type_die.has_value()) if (type_die.has_value())
add_type_info_to_variable(type_die.value(), regs, variable_info); TRY(add_type_info_to_variable(type_die.value(), regs, variable_info));
return variable_info; return variable_info;
} }
void DebugInfo::add_type_info_to_variable(Dwarf::DIE const& type_die, PtraceRegisters const& regs, DebugInfo::VariableInfo* parent_variable) const ErrorOr<void> DebugInfo::add_type_info_to_variable(Dwarf::DIE const& type_die, PtraceRegisters const& regs, DebugInfo::VariableInfo* parent_variable) const
{ {
OwnPtr<VariableInfo> type_info; OwnPtr<VariableInfo> type_info;
auto is_array_type = type_die.tag() == Dwarf::EntryTag::ArrayType; auto is_array_type = type_die.tag() == Dwarf::EntryTag::ArrayType;
@ -290,25 +300,25 @@ void DebugInfo::add_type_info_to_variable(Dwarf::DIE const& type_die, PtraceRegi
if (type_die.tag() == Dwarf::EntryTag::EnumerationType if (type_die.tag() == Dwarf::EntryTag::EnumerationType
|| type_die.tag() == Dwarf::EntryTag::StructureType || type_die.tag() == Dwarf::EntryTag::StructureType
|| is_array_type) { || is_array_type) {
type_info = create_variable_info(type_die, regs); type_info = TRY(create_variable_info(type_die, regs));
} }
type_die.for_each_child([&](Dwarf::DIE const& member) { TRY(type_die.for_each_child([&](Dwarf::DIE const& member) -> ErrorOr<void> {
if (member.is_null()) if (member.is_null())
return; return {};
if (is_array_type && member.tag() == Dwarf::EntryTag::SubRangeType) { if (is_array_type && member.tag() == Dwarf::EntryTag::SubRangeType) {
auto upper_bound = member.get_attribute(Dwarf::Attribute::UpperBound); auto upper_bound = TRY(member.get_attribute(Dwarf::Attribute::UpperBound));
VERIFY(upper_bound.has_value()); VERIFY(upper_bound.has_value());
auto size = upper_bound.value().as_unsigned() + 1; auto size = upper_bound.value().as_unsigned() + 1;
type_info->dimension_sizes.append(size); type_info->dimension_sizes.append(size);
return; return {};
} }
if (!is_variable_tag_supported(member.tag())) if (!is_variable_tag_supported(member.tag()))
return; return {};
auto member_variable = create_variable_info(member, regs, parent_variable->location_data.address); auto member_variable = TRY(create_variable_info(member, regs, parent_variable->location_data.address));
VERIFY(member_variable); VERIFY(member_variable);
if (type_die.tag() == Dwarf::EntryTag::EnumerationType) { if (type_die.tag() == Dwarf::EntryTag::EnumerationType) {
@ -316,12 +326,14 @@ void DebugInfo::add_type_info_to_variable(Dwarf::DIE const& type_die, PtraceRegi
type_info->members.append(member_variable.release_nonnull()); type_info->members.append(member_variable.release_nonnull());
} else { } else {
if (parent_variable->location_type != DebugInfo::VariableInfo::LocationType::Address) if (parent_variable->location_type != DebugInfo::VariableInfo::LocationType::Address)
return; return {};
member_variable->parent = parent_variable; member_variable->parent = parent_variable;
parent_variable->members.append(member_variable.release_nonnull()); parent_variable->members.append(member_variable.release_nonnull());
} }
});
return {};
}));
if (type_info) { if (type_info) {
if (is_array_type) { if (is_array_type) {
@ -337,6 +349,8 @@ void DebugInfo::add_type_info_to_variable(Dwarf::DIE const& type_die, PtraceRegi
parent_variable->type = move(type_info); parent_variable->type = move(type_info);
parent_variable->type->type_tag = type_die.tag(); parent_variable->type->type_tag = type_die.tag();
} }
return {};
} }
bool DebugInfo::is_variable_tag_supported(Dwarf::EntryTag const& tag) bool DebugInfo::is_variable_tag_supported(Dwarf::EntryTag const& tag)
@ -387,12 +401,12 @@ DebugInfo::SourcePosition DebugInfo::SourcePosition::from_line_info(Dwarf::LineP
return { line.file, line.line, line.address }; return { line.file, line.line, line.address };
} }
DebugInfo::SourcePositionWithInlines DebugInfo::get_source_position_with_inlines(FlatPtr address) const ErrorOr<DebugInfo::SourcePositionWithInlines> DebugInfo::get_source_position_with_inlines(FlatPtr address) const
{ {
// If the address is in an "inline chain", this is the inner-most inlined position. // If the address is in an "inline chain", this is the inner-most inlined position.
auto inner_source_position = get_source_position(address); auto inner_source_position = get_source_position(address);
auto die = m_dwarf_info.get_die_at_address(address); auto die = TRY(m_dwarf_info.get_die_at_address(address));
if (!die.has_value() || die->tag() == Dwarf::EntryTag::SubroutineType) { if (!die.has_value() || die->tag() == Dwarf::EntryTag::SubroutineType) {
// Inline chain is empty // Inline chain is empty
return SourcePositionWithInlines { inner_source_position, {} }; return SourcePositionWithInlines { inner_source_position, {} };
@ -400,25 +414,26 @@ DebugInfo::SourcePositionWithInlines DebugInfo::get_source_position_with_inlines
Vector<SourcePosition> inline_chain; Vector<SourcePosition> inline_chain;
auto insert_to_chain = [&](Dwarf::DIE const& die) { auto insert_to_chain = [&](Dwarf::DIE const& die) -> ErrorOr<void> {
auto caller_source_path = get_source_path_of_inline(die); auto caller_source_path = TRY(get_source_path_of_inline(die));
auto caller_line = get_line_of_inline(die); auto caller_line = TRY(get_line_of_inline(die));
if (!caller_source_path.has_value() || !caller_line.has_value()) { if (!caller_source_path.has_value() || !caller_line.has_value()) {
return; return {};
} }
inline_chain.append({ DeprecatedString::formatted("{}/{}", caller_source_path->directory, caller_source_path->filename), caller_line.value() }); inline_chain.append({ DeprecatedString::formatted("{}/{}", caller_source_path->directory, caller_source_path->filename), caller_line.value() });
return {};
}; };
while (die->tag() == Dwarf::EntryTag::InlinedSubroutine) { while (die->tag() == Dwarf::EntryTag::InlinedSubroutine) {
insert_to_chain(*die); TRY(insert_to_chain(*die));
if (!die->parent_offset().has_value()) { if (!die->parent_offset().has_value()) {
break; break;
} }
auto parent = die->compilation_unit().dwarf_info().get_cached_die_at_offset(die->parent_offset().value()); auto parent = TRY(die->compilation_unit().dwarf_info().get_cached_die_at_offset(die->parent_offset().value()));
if (!parent.has_value()) { if (!parent.has_value()) {
break; break;
} }
@ -428,9 +443,9 @@ DebugInfo::SourcePositionWithInlines DebugInfo::get_source_position_with_inlines
return SourcePositionWithInlines { inner_source_position, inline_chain }; return SourcePositionWithInlines { inner_source_position, inline_chain };
} }
Optional<Dwarf::LineProgram::DirectoryAndFile> DebugInfo::get_source_path_of_inline(Dwarf::DIE const& die) const ErrorOr<Optional<Dwarf::LineProgram::DirectoryAndFile>> DebugInfo::get_source_path_of_inline(Dwarf::DIE const& die) const
{ {
auto caller_file = die.get_attribute(Dwarf::Attribute::CallFile); auto caller_file = TRY(die.get_attribute(Dwarf::Attribute::CallFile));
if (caller_file.has_value()) { if (caller_file.has_value()) {
size_t file_index = 0; size_t file_index = 0;
@ -443,23 +458,23 @@ Optional<Dwarf::LineProgram::DirectoryAndFile> DebugInfo::get_source_path_of_inl
VERIFY(static_cast<u64>(signed_file_index) <= NumericLimits<size_t>::max()); VERIFY(static_cast<u64>(signed_file_index) <= NumericLimits<size_t>::max());
file_index = static_cast<size_t>(caller_file->as_signed()); file_index = static_cast<size_t>(caller_file->as_signed());
} else { } else {
return {}; return Optional<Dwarf::LineProgram::DirectoryAndFile> {};
} }
return die.compilation_unit().line_program().get_directory_and_file(file_index); return die.compilation_unit().line_program().get_directory_and_file(file_index);
} }
return {}; return Optional<Dwarf::LineProgram::DirectoryAndFile> {};
} }
Optional<uint32_t> DebugInfo::get_line_of_inline(Dwarf::DIE const& die) const ErrorOr<Optional<uint32_t>> DebugInfo::get_line_of_inline(Dwarf::DIE const& die) const
{ {
auto caller_line = die.get_attribute(Dwarf::Attribute::CallLine); auto caller_line = TRY(die.get_attribute(Dwarf::Attribute::CallLine));
if (!caller_line.has_value()) if (!caller_line.has_value())
return {}; return Optional<uint32_t> {};
if (caller_line->type() != Dwarf::AttributeValue::Type::UnsignedNumber) if (caller_line->type() != Dwarf::AttributeValue::Type::UnsignedNumber)
return {}; return Optional<uint32_t> {};
return caller_line.value().as_unsigned(); return Optional<uint32_t> { caller_line.value().as_unsigned() };
} }
} }

View file

@ -90,7 +90,7 @@ public:
Vector<Dwarf::DIE> dies_of_variables; Vector<Dwarf::DIE> dies_of_variables;
}; };
NonnullOwnPtrVector<VariableInfo> get_variables_in_current_scope(PtraceRegisters const&) const; ErrorOr<NonnullOwnPtrVector<VariableInfo>> get_variables_in_current_scope(PtraceRegisters const&) const;
Optional<SourcePosition> get_source_position(FlatPtr address) const; Optional<SourcePosition> get_source_position(FlatPtr address) const;
@ -98,7 +98,7 @@ public:
Optional<SourcePosition> source_position; Optional<SourcePosition> source_position;
Vector<SourcePosition> inline_chain; Vector<SourcePosition> inline_chain;
}; };
SourcePositionWithInlines get_source_position_with_inlines(FlatPtr address) const; ErrorOr<SourcePositionWithInlines> get_source_position_with_inlines(FlatPtr address) const;
struct SourcePositionAndAddress { struct SourcePositionAndAddress {
DeprecatedString file; DeprecatedString file;
@ -113,15 +113,15 @@ public:
Optional<VariablesScope> get_containing_function(FlatPtr address) const; Optional<VariablesScope> get_containing_function(FlatPtr address) const;
private: private:
void prepare_variable_scopes(); ErrorOr<void> prepare_variable_scopes();
void prepare_lines(); ErrorOr<void> prepare_lines();
void parse_scopes_impl(Dwarf::DIE const& die); ErrorOr<void> parse_scopes_impl(Dwarf::DIE const& die);
OwnPtr<VariableInfo> create_variable_info(Dwarf::DIE const& variable_die, PtraceRegisters const&, u32 address_offset = 0) const; ErrorOr<OwnPtr<VariableInfo>> create_variable_info(Dwarf::DIE const& variable_die, PtraceRegisters const&, u32 address_offset = 0) const;
static bool is_variable_tag_supported(Dwarf::EntryTag const& tag); static bool is_variable_tag_supported(Dwarf::EntryTag const& tag);
void add_type_info_to_variable(Dwarf::DIE const& type_die, PtraceRegisters const& regs, DebugInfo::VariableInfo* parent_variable) const; ErrorOr<void> add_type_info_to_variable(Dwarf::DIE const& type_die, PtraceRegisters const& regs, DebugInfo::VariableInfo* parent_variable) const;
Optional<Dwarf::LineProgram::DirectoryAndFile> get_source_path_of_inline(Dwarf::DIE const&) const; ErrorOr<Optional<Dwarf::LineProgram::DirectoryAndFile>> get_source_path_of_inline(Dwarf::DIE const&) const;
Optional<uint32_t> get_line_of_inline(Dwarf::DIE const&) const; ErrorOr<Optional<uint32_t>> get_line_of_inline(Dwarf::DIE const&) const;
ELF::Image const& m_elf; ELF::Image const& m_elf;
DeprecatedString m_source_root; DeprecatedString m_source_root;

View file

@ -34,13 +34,13 @@ ErrorOr<void> AddressRangesV5::for_each_range(Function<void(Range)> callback)
case RangeListEntryType::BaseAddressX: { case RangeListEntryType::BaseAddressX: {
FlatPtr index; FlatPtr index;
LEB128::read_unsigned(wrapped_range_lists_stream, 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; break;
} }
case RangeListEntryType::OffsetPair: { case RangeListEntryType::OffsetPair: {
Optional<FlatPtr> base_address = current_base_address; Optional<FlatPtr> base_address = current_base_address;
if (!base_address.has_value()) { 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()) if (!base_address.has_value())
@ -63,14 +63,14 @@ ErrorOr<void> AddressRangesV5::for_each_range(Function<void(Range)> callback)
size_t start, end; size_t start, end;
LEB128::read_unsigned(wrapped_range_lists_stream, start); LEB128::read_unsigned(wrapped_range_lists_stream, start);
LEB128::read_unsigned(wrapped_range_lists_stream, end); 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; break;
} }
case RangeListEntryType::StartXLength: { case RangeListEntryType::StartXLength: {
size_t start, length; size_t start, length;
LEB128::read_unsigned(wrapped_range_lists_stream, start); LEB128::read_unsigned(wrapped_range_lists_stream, start);
LEB128::read_unsigned(wrapped_range_lists_stream, length); 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 }); callback(Range { start_addr, start_addr + length });
break; break;
} }
@ -106,7 +106,7 @@ ErrorOr<void> AddressRangesV4::for_each_range(Function<void(Range)> callback)
} else if (begin == explode_byte(0xff)) { } else if (begin == explode_byte(0xff)) {
current_base_address = end; current_base_address = end;
} else { } 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 }); callback({ base + begin, base + end });
} }
} }

View file

@ -9,7 +9,7 @@
namespace Debug::Dwarf { namespace Debug::Dwarf {
FlatPtr AttributeValue::as_addr() const ErrorOr<FlatPtr> AttributeValue::as_addr() const
{ {
switch (m_form) { switch (m_form) {
case AttributeDataForm::Addr: 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) { switch (m_form) {
case AttributeDataForm::String: case AttributeDataForm::String:

View file

@ -33,10 +33,10 @@ public:
Type type() const { return m_type; } Type type() const { return m_type; }
AttributeDataForm form() const { return m_form; } AttributeDataForm form() const { return m_form; }
FlatPtr as_addr() const; ErrorOr<FlatPtr> as_addr() const;
u64 as_unsigned() const { return m_data.as_unsigned; } u64 as_unsigned() const { return m_data.as_unsigned; }
i64 as_signed() const { return m_data.as_signed; } 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; } bool as_bool() const { return m_data.as_bool; }
ReadonlyBytes as_raw_bytes() const { return m_data.as_raw_bytes; } ReadonlyBytes as_raw_bytes() const { return m_data.as_raw_bytes; }

View file

@ -40,27 +40,27 @@ LineProgram const& CompilationUnit::line_program() const
return *m_line_program; return *m_line_program;
} }
Optional<FlatPtr> CompilationUnit::base_address() const ErrorOr<Optional<FlatPtr>> CompilationUnit::base_address() const
{ {
if (m_has_cached_base_address) if (m_has_cached_base_address)
return m_cached_base_address; return m_cached_base_address;
auto die = root_die(); auto die = root_die();
auto res = die.get_attribute(Attribute::LowPc); auto res = TRY(die.get_attribute(Attribute::LowPc));
if (res.has_value()) { 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; m_has_cached_base_address = true;
return m_cached_base_address; 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) if (m_has_cached_address_table_base)
return m_cached_address_table_base; return m_cached_address_table_base;
auto die = root_die(); auto die = root_die();
auto res = die.get_attribute(Attribute::AddrBase); auto res = TRY(die.get_attribute(Attribute::AddrBase));
if (res.has_value()) { if (res.has_value()) {
VERIFY(res->form() == AttributeDataForm::SecOffset); VERIFY(res->form() == AttributeDataForm::SecOffset);
m_cached_address_table_base = res->as_unsigned(); m_cached_address_table_base = res->as_unsigned();
@ -69,13 +69,13 @@ u64 CompilationUnit::address_table_base() const
return m_cached_address_table_base; 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) if (m_has_cached_string_offsets_base)
return m_cached_string_offsets_base; return m_cached_string_offsets_base;
auto die = root_die(); auto die = root_die();
auto res = die.get_attribute(Attribute::StrOffsetsBase); auto res = TRY(die.get_attribute(Attribute::StrOffsetsBase));
if (res.has_value()) { if (res.has_value()) {
VERIFY(res->form() == AttributeDataForm::SecOffset); VERIFY(res->form() == AttributeDataForm::SecOffset);
m_cached_string_offsets_base = res->as_unsigned(); m_cached_string_offsets_base = res->as_unsigned();
@ -84,13 +84,13 @@ u64 CompilationUnit::string_offsets_base() const
return m_cached_string_offsets_base; 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) if (m_has_cached_range_lists_base)
return m_cached_range_lists_base; return m_cached_range_lists_base;
auto die = root_die(); auto die = root_die();
auto res = die.get_attribute(Attribute::RngListsBase); auto res = TRY(die.get_attribute(Attribute::RngListsBase));
if (res.has_value()) { if (res.has_value()) {
VERIFY(res->form() == AttributeDataForm::SecOffset); VERIFY(res->form() == AttributeDataForm::SecOffset);
m_cached_range_lists_base = res->as_unsigned(); m_cached_range_lists_base = res->as_unsigned();
@ -99,9 +99,9 @@ u64 CompilationUnit::range_lists_base() const
return m_cached_range_lists_base; 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(); auto debug_addr_data = dwarf_info().debug_addr_data();
VERIFY(base < debug_addr_data.size()); VERIFY(base < debug_addr_data.size());
auto addresses = debug_addr_data.slice(base); auto addresses = debug_addr_data.slice(base);
@ -111,9 +111,9 @@ FlatPtr CompilationUnit::get_address(size_t index) const
return value; 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(); auto debug_str_offsets_data = dwarf_info().debug_str_offsets_data();
VERIFY(base < debug_str_offsets_data.size()); VERIFY(base < debug_str_offsets_data.size());
// FIXME: This assumes DWARF32 // FIXME: This assumes DWARF32

View file

@ -31,22 +31,22 @@ public:
DIE root_die() const; DIE root_die() const;
DIE get_die_at_offset(u32 offset) const; DIE get_die_at_offset(u32 offset) const;
FlatPtr get_address(size_t index) const; ErrorOr<FlatPtr> get_address(size_t index) const;
char const* get_string(size_t index) const; ErrorOr<char const*> get_string(size_t index) const;
u8 dwarf_version() const { return m_header.version(); } u8 dwarf_version() const { return m_header.version(); }
DwarfInfo const& dwarf_info() const { return m_dwarf_info; } DwarfInfo const& dwarf_info() const { return m_dwarf_info; }
AbbreviationsMap const& abbreviations_map() const { return m_abbreviations; } AbbreviationsMap const& abbreviations_map() const { return m_abbreviations; }
LineProgram const& line_program() const; LineProgram const& line_program() const;
Optional<FlatPtr> base_address() const; ErrorOr<Optional<FlatPtr>> base_address() const;
// DW_AT_addr_base // DW_AT_addr_base
u64 address_table_base() const; ErrorOr<u64> address_table_base() const;
// DW_AT_str_offsets_base // DW_AT_str_offsets_base
u64 string_offsets_base() const; ErrorOr<u64> string_offsets_base() const;
// DW_AT_rnglists_base // DW_AT_rnglists_base
u64 range_lists_base() const; ErrorOr<u64> range_lists_base() const;
private: private:
DwarfInfo const& m_dwarf_info; DwarfInfo const& m_dwarf_info;

View file

@ -15,10 +15,10 @@ namespace Debug::Dwarf {
DIE::DIE(CompilationUnit const& unit, u32 offset, Optional<u32> parent_offset) DIE::DIE(CompilationUnit const& unit, u32 offset, Optional<u32> parent_offset)
: m_compilation_unit(unit) : 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; 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 // We iterate the attributes data only to calculate this DIE's size
for (auto& attribute_spec : abbreviation_info->attribute_specifications) { 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_size = stream.offset() - m_offset;
m_parent_offset = parent_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() }; InputMemoryStream stream { m_compilation_unit.dwarf_info().debug_info_data() };
stream.discard_or_error(m_data_offset); stream.discard_or_error(m_data_offset);
@ -55,42 +56,45 @@ Optional<AttributeValue> DIE::get_attribute(Attribute const& attribute) const
VERIFY(abbreviation_info); VERIFY(abbreviation_info);
for (auto const& attribute_spec : abbreviation_info->attribute_specifications) { 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) { if (attribute_spec.attribute == attribute) {
return value; 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) if (!m_has_children)
return; return {};
auto current_child = DIE(m_compilation_unit, m_offset + m_size, m_offset); auto current_child = DIE(m_compilation_unit, m_offset + m_size, m_offset);
while (true) { while (true) {
callback(current_child); TRY(callback(current_child));
if (current_child.is_null()) if (current_child.is_null())
break; break;
if (!current_child.has_children()) { 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; continue;
} }
auto sibling = current_child.get_attribute(Attribute::Sibling); auto sibling = TRY(current_child.get_attribute(Attribute::Sibling));
u32 sibling_offset = 0; u32 sibling_offset = 0;
if (sibling.has_value()) { if (sibling.has_value()) {
sibling_offset = sibling.value().as_unsigned(); sibling_offset = sibling.value().as_unsigned();
} else { } else {
// NOTE: According to the spec, the compiler doesn't have to supply the sibling information. // 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 // 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(); 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 {};
} }
} }

View file

@ -27,16 +27,16 @@ public:
bool has_children() const { return m_has_children; } bool has_children() const { return m_has_children; }
EntryTag tag() const { return m_tag; } 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; } bool is_null() const { return m_tag == EntryTag::None; }
CompilationUnit const& compilation_unit() const { return m_compilation_unit; } CompilationUnit const& compilation_unit() const { return m_compilation_unit; }
Optional<u32> parent_offset() const { return m_parent_offset; } Optional<u32> parent_offset() const { return m_parent_offset; }
private: 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; CompilationUnit const& m_compilation_unit;
u32 m_offset { 0 }; u32 m_offset { 0 };
u32 m_data_offset { 0 }; u32 m_data_offset { 0 };

View file

@ -29,7 +29,7 @@ DwarfInfo::DwarfInfo(ELF::Image const& elf)
m_debug_addr_data = section_data(".debug_addr"sv); m_debug_addr_data = section_data(".debug_addr"sv);
m_debug_ranges_data = section_data(".debug_ranges"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; DwarfInfo::~DwarfInfo() = default;
@ -42,10 +42,10 @@ ReadonlyBytes DwarfInfo::section_data(StringView section_name) const
return section->bytes(); return section->bytes();
} }
void DwarfInfo::populate_compilation_units() ErrorOr<void> DwarfInfo::populate_compilation_units()
{ {
if (!m_debug_info_data.data()) if (!m_debug_info_data.data())
return; return {};
InputMemoryStream debug_info_stream { m_debug_info_data }; InputMemoryStream debug_info_stream { m_debug_info_data };
InputMemoryStream line_info_stream { m_debug_line_data }; InputMemoryStream line_info_stream { m_debug_line_data };
@ -77,20 +77,22 @@ void DwarfInfo::populate_compilation_units()
} }
VERIFY(line_info_stream.eof()); 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 InputMemoryStream& debug_info_stream, CompilationUnit const* unit) const
{ {
AttributeValue value; AttributeValue value;
value.m_form = form; value.m_form = form;
value.m_compilation_unit = unit; 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 }; value.m_data.as_raw_bytes = { debug_info_data().offset_pointer(debug_info_stream.offset()), length };
debug_info_stream.discard_or_error(length); debug_info_stream.discard_or_error(length);
VERIFY(!debug_info_stream.has_any_error()); VERIFY(!debug_info_stream.has_any_error());
return {};
}; };
switch (form) { switch (form) {
@ -170,7 +172,7 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
} }
case AttributeDataForm::Data16: { case AttributeDataForm::Data16: {
value.m_type = AttributeValue::Type::RawBytes; value.m_type = AttributeValue::Type::RawBytes;
assign_raw_bytes_value(16); TRY(assign_raw_bytes_value(16));
VERIFY(!debug_info_stream.has_any_error()); VERIFY(!debug_info_stream.has_any_error());
break; break;
} }
@ -193,7 +195,7 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
debug_info_stream.read_LEB128_unsigned(length); debug_info_stream.read_LEB128_unsigned(length);
VERIFY(!debug_info_stream.has_any_error()); VERIFY(!debug_info_stream.has_any_error());
value.m_type = AttributeValue::Type::DwarfExpression; value.m_type = AttributeValue::Type::DwarfExpression;
assign_raw_bytes_value(length); TRY(assign_raw_bytes_value(length));
break; break;
} }
case AttributeDataForm::String: { case AttributeDataForm::String: {
@ -210,7 +212,7 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
u8 length; u8 length;
debug_info_stream >> length; debug_info_stream >> length;
VERIFY(!debug_info_stream.has_any_error()); VERIFY(!debug_info_stream.has_any_error());
assign_raw_bytes_value(length); TRY(assign_raw_bytes_value(length));
break; break;
} }
case AttributeDataForm::Block2: { case AttributeDataForm::Block2: {
@ -218,7 +220,7 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
u16 length; u16 length;
debug_info_stream >> length; debug_info_stream >> length;
VERIFY(!debug_info_stream.has_any_error()); VERIFY(!debug_info_stream.has_any_error());
assign_raw_bytes_value(length); TRY(assign_raw_bytes_value(length));
break; break;
} }
case AttributeDataForm::Block4: { case AttributeDataForm::Block4: {
@ -226,7 +228,7 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
u32 length; u32 length;
debug_info_stream >> length; debug_info_stream >> length;
VERIFY(!debug_info_stream.has_any_error()); VERIFY(!debug_info_stream.has_any_error());
assign_raw_bytes_value(length); TRY(assign_raw_bytes_value(length));
break; break;
} }
case AttributeDataForm::Block: { case AttributeDataForm::Block: {
@ -234,7 +236,7 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
size_t length; size_t length;
debug_info_stream.read_LEB128_unsigned(length); debug_info_stream.read_LEB128_unsigned(length);
VERIFY(!debug_info_stream.has_any_error()); VERIFY(!debug_info_stream.has_any_error());
assign_raw_bytes_value(length); TRY(assign_raw_bytes_value(length));
break; break;
} }
case AttributeDataForm::LineStrP: { case AttributeDataForm::LineStrP: {
@ -332,21 +334,21 @@ AttributeValue DwarfInfo::get_attribute_value(AttributeDataForm form, ssize_t im
return value; 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) { 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_range.insert(range.start_address, DIEAndRange { die, range });
m_cached_dies_by_offset.insert(die.offset(), die); m_cached_dies_by_offset.insert(die.offset(), die);
}; };
auto get_ranges_of_die = [this](DIE const& die) -> ErrorOr<Vector<DIERange>> { 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()) { if (ranges.has_value()) {
size_t offset; size_t offset;
if (ranges->form() == AttributeDataForm::SecOffset) { if (ranges->form() == AttributeDataForm::SecOffset) {
offset = ranges->as_unsigned(); offset = ranges->as_unsigned();
} else { } else {
auto index = ranges->as_unsigned(); 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 // FIXME: This assumes that the format is DWARf32
auto offsets = debug_range_lists_data().slice(base); auto offsets = debug_range_lists_data().slice(base);
offset = ByteReader::load32(offsets.offset_pointer(index * sizeof(u32))) + base; offset = ByteReader::load32(offsets.offset_pointer(index * sizeof(u32))) + base;
@ -371,8 +373,8 @@ void DwarfInfo::build_cached_dies() const
return entries; return entries;
} }
auto start = die.get_attribute(Attribute::LowPc); auto start = TRY(die.get_attribute(Attribute::LowPc));
auto end = die.get_attribute(Attribute::HighPc); auto end = TRY(die.get_attribute(Attribute::HighPc));
if (!start.has_value() || !end.has_value()) if (!start.has_value() || !end.has_value())
return Vector<DIERange> {}; return Vector<DIERange> {};
@ -384,40 +386,44 @@ void DwarfInfo::build_cached_dies() const
uint32_t range_end = 0; uint32_t range_end = 0;
if (end->form() == Dwarf::AttributeDataForm::Addr) if (end->form() == Dwarf::AttributeDataForm::Addr)
range_end = end->as_addr(); range_end = TRY(end->as_addr());
else 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. // If we simply use a lambda, type deduction fails because it's used recursively.
Function<void(DIE const& die)> insert_to_cache_recursively; Function<ErrorOr<void>(DIE const& die)> insert_to_cache_recursively;
insert_to_cache_recursively = [&](DIE const& die) { insert_to_cache_recursively = [&](DIE const& die) -> ErrorOr<void> {
if (die.offset() == 0 || die.parent_offset().has_value()) { 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) { for (auto& range : ranges) {
insert_to_cache(die, range); 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()) { 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) { TRY(for_each_compilation_unit([&](CompilationUnit const& compilation_unit) -> ErrorOr<void> {
insert_to_cache_recursively(compilation_unit.root_die()); TRY(insert_to_cache_recursively(compilation_unit.root_die()));
}); return {};
}));
m_built_cached_dies = true; 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) 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); 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) { 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()) if (iter.is_end())
return {}; return Optional<DIE> {};
if (iter->range.start_address > address || iter->range.end_address < address) { if (iter->range.start_address > address || iter->range.end_address < address) {
return {}; return Optional<DIE> {};
} }
return iter->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) if (!m_built_cached_dies)
build_cached_dies(); TRY(build_cached_dies());
auto* die = m_cached_dies_by_offset.find(offset); auto* die = m_cached_dies_by_offset.find(offset);
if (!die) if (!die)
return {}; return Optional<DIE> {};
return *die; return *die;
} }

View file

@ -39,23 +39,23 @@ public:
ReadonlyBytes debug_ranges_data() const { return m_debug_ranges_data; } ReadonlyBytes debug_ranges_data() const { return m_debug_ranges_data; }
template<typename Callback> 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; 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, // Note that even if there is a DIE at the given offset,
// but it does not exist in the DIE cache (because for example // 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. // 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, // To get any DIE object at a given offset in a compilation unit,
// use CompilationUnit::get_die_at_offset. // 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: private:
void populate_compilation_units(); ErrorOr<void> populate_compilation_units();
void build_cached_dies() const; ErrorOr<void> build_cached_dies() const;
ReadonlyBytes section_data(StringView section_name) const; ReadonlyBytes section_data(StringView section_name) const;
@ -90,11 +90,12 @@ private:
}; };
template<typename Callback> 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) { for (auto const& unit : m_compilation_units) {
callback(unit); TRY(callback(unit));
} }
return {};
} }
} }

View file

@ -18,13 +18,13 @@ LineProgram::LineProgram(DwarfInfo& dwarf_info, InputMemoryStream& stream)
, m_stream(stream) , m_stream(stream)
{ {
m_unit_offset = m_stream.offset(); m_unit_offset = m_stream.offset();
parse_unit_header(); parse_unit_header().release_value_but_fixme_should_propagate_errors();
parse_source_directories(); parse_source_directories().release_value_but_fixme_should_propagate_errors();
parse_source_files(); parse_source_files().release_value_but_fixme_should_propagate_errors();
run_program(); 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; 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); 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()); 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) { if (m_unit_header.version() >= 5) {
u8 path_entry_format_count = 0; 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++) { for (size_t i = 0; i < paths_count; i++) {
PathEntry entry; PathEntry entry;
for (auto& format_description : format_descriptions) { 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) { switch (format_description.type) {
case ContentType::Path: case ContentType::Path:
entry.path = value.as_string(); entry.path = TRY(value.as_string());
break; break;
case ContentType::DirectoryIndex: case ContentType::DirectoryIndex:
entry.directory_index = value.as_unsigned(); 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()); VERIFY(!m_stream.has_any_error());
return {};
} }
void LineProgram::parse_source_directories() ErrorOr<void> LineProgram::parse_source_directories()
{ {
if (m_unit_header.version() < 5) { if (m_unit_header.version() < 5) {
m_source_directories.append("."); m_source_directories.append(".");
} }
parse_path_entries([this](PathEntry& entry) { TRY(parse_path_entries([this](PathEntry& entry) {
m_source_directories.append(entry.path); 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) { if (m_unit_header.version() < 5) {
m_source_files.append({ ".", 0 }); 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 }); m_source_files.append({ entry.path, entry.directory_index });
}, },
PathListType::Filenames); PathListType::Filenames));
return {};
} }
void LineProgram::append_to_line_info() void LineProgram::append_to_line_info()
@ -149,7 +155,7 @@ void LineProgram::reset_registers()
m_is_statement = m_unit_header.default_is_stmt() == 1; 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; size_t length = 0;
m_stream.read_LEB128_unsigned(length); 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()); dbgln("Encountered unknown sub opcode {} at stream offset {:p}", sub_opcode, m_stream.offset());
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
return {};
} }
void LineProgram::handle_standard_opcode(u8 opcode) ErrorOr<void> LineProgram::handle_standard_opcode(u8 opcode)
{ {
switch (opcode) { switch (opcode) {
case StandardOpcodes::Copy: { case StandardOpcodes::Copy: {
@ -256,6 +264,8 @@ void LineProgram::handle_standard_opcode(u8 opcode)
dbgln("Unhandled LineProgram opcode {}", opcode); dbgln("Unhandled LineProgram opcode {}", opcode);
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
return {};
} }
void LineProgram::handle_special_opcode(u8 opcode) void LineProgram::handle_special_opcode(u8 opcode)
{ {
@ -277,7 +287,7 @@ void LineProgram::handle_special_opcode(u8 opcode)
m_prologue_end = false; m_prologue_end = false;
} }
void LineProgram::run_program() ErrorOr<void> LineProgram::run_program()
{ {
reset_registers(); reset_registers();
@ -288,13 +298,15 @@ void LineProgram::run_program()
dbgln_if(DWARF_DEBUG, "{:p}: opcode: {}", m_stream.offset() - 1, opcode); dbgln_if(DWARF_DEBUG, "{:p}: opcode: {}", m_stream.offset() - 1, opcode);
if (opcode == 0) { if (opcode == 0) {
handle_extended_opcode(); TRY(handle_extended_opcode());
} else if (opcode >= 1 && opcode <= 12) { } else if (opcode >= 1 && opcode <= 12) {
handle_standard_opcode(opcode); TRY(handle_standard_opcode(opcode));
} else { } else {
handle_special_opcode(opcode); handle_special_opcode(opcode);
} }
} }
return {};
} }
LineProgram::DirectoryAndFile LineProgram::get_directory_and_file(size_t file_index) const LineProgram::DirectoryAndFile LineProgram::get_directory_and_file(size_t file_index) const

View file

@ -133,19 +133,19 @@ public:
bool looks_like_embedded_resource() const; bool looks_like_embedded_resource() const;
private: private:
void parse_unit_header(); ErrorOr<void> parse_unit_header();
void parse_source_directories(); ErrorOr<void> parse_source_directories();
void parse_source_files(); ErrorOr<void> parse_source_files();
void run_program(); ErrorOr<void> run_program();
void append_to_line_info(); void append_to_line_info();
void reset_registers(); void reset_registers();
void handle_extended_opcode(); ErrorOr<void> handle_extended_opcode();
void handle_standard_opcode(u8 opcode); ErrorOr<void> handle_standard_opcode(u8 opcode);
void handle_special_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 { enum StandardOpcodes {
Copy = 1, Copy = 1,

View file

@ -105,7 +105,7 @@ Optional<Symbol> symbolicate(DeprecatedString const& path, FlatPtr address, Incl
Vector<Debug::DebugInfo::SourcePosition> positions; Vector<Debug::DebugInfo::SourcePosition> positions;
if (include_source_positions == IncludeSourcePosition::Yes) { if (include_source_positions == IncludeSourcePosition::Yes) {
auto source_position_with_inlines = cached_elf->debug_info->get_source_position_with_inlines(address); auto source_position_with_inlines = cached_elf->debug_info->get_source_position_with_inlines(address).release_value_but_fixme_should_propagate_errors();
for (auto& position : source_position_with_inlines.inline_chain) { for (auto& position : source_position_with_inlines.inline_chain) {
if (!positions.contains_slow(position)) if (!positions.contains_slow(position))