mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 06:58:11 +00:00
LibDebug: Parse DWARF information entries
We can now iterate the tree structure of the DIEs, access attribute values and parse some very basic DWARF expressions.
This commit is contained in:
parent
2cafc36d9c
commit
c5eb20d0cc
14 changed files with 1067 additions and 3 deletions
|
@ -26,16 +26,66 @@
|
|||
|
||||
#include "DebugInfo.h"
|
||||
#include <AK/QuickSort.h>
|
||||
#include <LibDebug/Dwarf/CompilationUnit.h>
|
||||
#include <LibDebug/Dwarf/DwarfInfo.h>
|
||||
#include <LibDebug/Dwarf/Expression.h>
|
||||
|
||||
DebugInfo::DebugInfo(NonnullRefPtr<const ELF::Loader> elf)
|
||||
: m_elf(elf)
|
||||
, m_dwarf_info(Dwarf::DwarfInfo::create(m_elf))
|
||||
{
|
||||
prepare_variable_scopes();
|
||||
prepare_lines();
|
||||
}
|
||||
|
||||
void DebugInfo::prepare_variable_scopes()
|
||||
{
|
||||
m_dwarf_info->for_each_compilation_unit([&](const Dwarf::CompilationUnit& unit) {
|
||||
auto root = unit.root_die();
|
||||
parse_scopes_impl(root);
|
||||
});
|
||||
}
|
||||
|
||||
void DebugInfo::parse_scopes_impl(const Dwarf::DIE& die)
|
||||
{
|
||||
die.for_each_child([&](const Dwarf::DIE& child) {
|
||||
if (child.is_null())
|
||||
return;
|
||||
if (!(child.tag() == Dwarf::EntryTag::SubProgram || child.tag() == Dwarf::EntryTag::LexicalBlock))
|
||||
return;
|
||||
|
||||
if (child.get_attribute(Dwarf::Attribute::Inline).has_value()) {
|
||||
dbg() << "DWARF inlined functions are not supported";
|
||||
return;
|
||||
}
|
||||
if (child.get_attribute(Dwarf::Attribute::Ranges).has_value()) {
|
||||
dbg() << "DWARF ranges are not supported";
|
||||
return;
|
||||
}
|
||||
auto name = child.get_attribute(Dwarf::Attribute::Name);
|
||||
|
||||
VariablesScope scope {};
|
||||
scope.is_function = (child.tag() == Dwarf::EntryTag::SubProgram);
|
||||
if (name.has_value())
|
||||
scope.name = name.value().data.as_string;
|
||||
|
||||
scope.address_low = child.get_attribute(Dwarf::Attribute::LowPc).value().data.as_u32;
|
||||
// The attribute name HighPc is confusing. In this context, it seems to actually be a positive offset from LowPc
|
||||
scope.address_high = scope.address_low + child.get_attribute(Dwarf::Attribute::HighPc).value().data.as_u32;
|
||||
|
||||
child.for_each_child([&](const Dwarf::DIE& variable_entry) {
|
||||
if (variable_entry.tag() != Dwarf::EntryTag::Variable)
|
||||
return;
|
||||
scope.dies_of_variables.append(variable_entry);
|
||||
});
|
||||
m_scopes.append(scope);
|
||||
|
||||
parse_scopes_impl(child);
|
||||
});
|
||||
}
|
||||
|
||||
void DebugInfo::prepare_lines()
|
||||
{
|
||||
|
||||
auto section = m_elf->image().lookup_section(".debug_line");
|
||||
ASSERT(!section.is_undefined());
|
||||
|
||||
|
@ -45,7 +95,7 @@ void DebugInfo::prepare_lines()
|
|||
Vector<LineProgram::LineInfo> all_lines;
|
||||
while (!stream.at_end()) {
|
||||
LineProgram program(stream);
|
||||
all_lines.append(move(program.lines()));
|
||||
all_lines.append(program.lines());
|
||||
}
|
||||
|
||||
for (auto& line_info : all_lines) {
|
||||
|
@ -65,7 +115,6 @@ void DebugInfo::prepare_lines()
|
|||
|
||||
Optional<DebugInfo::SourcePosition> DebugInfo::get_source_position(u32 target_address) const
|
||||
{
|
||||
|
||||
if (m_sorted_lines.is_empty())
|
||||
return {};
|
||||
if (target_address < m_sorted_lines[0].address)
|
||||
|
@ -88,3 +137,94 @@ Optional<u32> DebugInfo::get_instruction_from_source(const String& file, size_t
|
|||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
NonnullOwnPtrVector<DebugInfo::VariableInfo> DebugInfo::get_variables_in_current_scope(const PtraceRegisters& regs) const
|
||||
{
|
||||
auto scope = get_scope(regs.eip);
|
||||
if (!scope.has_value())
|
||||
return {};
|
||||
|
||||
NonnullOwnPtrVector<DebugInfo::VariableInfo> variables;
|
||||
for (const auto& die_entry : scope.value().dies_of_variables) {
|
||||
variables.append(create_variable_info(die_entry, regs));
|
||||
}
|
||||
return variables;
|
||||
}
|
||||
|
||||
Optional<DebugInfo::VariablesScope> DebugInfo::get_scope(u32 instruction_pointer) const
|
||||
{
|
||||
Optional<VariablesScope> best_matching_scope;
|
||||
|
||||
// TODO: We can store the scopes in a better data strucutre
|
||||
for (const auto& scope : m_scopes) {
|
||||
if (instruction_pointer < scope.address_low || instruction_pointer >= scope.address_high)
|
||||
continue;
|
||||
|
||||
if (!best_matching_scope.has_value()) {
|
||||
best_matching_scope = scope;
|
||||
|
||||
} else if (scope.address_low > best_matching_scope.value().address_low || scope.address_high < best_matching_scope.value().address_high) {
|
||||
best_matching_scope = scope;
|
||||
}
|
||||
}
|
||||
return best_matching_scope;
|
||||
}
|
||||
|
||||
NonnullOwnPtr<DebugInfo::VariableInfo> DebugInfo::create_variable_info(const Dwarf::DIE& variable_die, const PtraceRegisters& regs) const
|
||||
{
|
||||
ASSERT(variable_die.tag() == Dwarf::EntryTag::Variable || variable_die.tag() == Dwarf::EntryTag::Member);
|
||||
|
||||
NonnullOwnPtr<VariableInfo> variable_info = make<VariableInfo>();
|
||||
|
||||
variable_info->name = variable_die.get_attribute(Dwarf::Attribute::Name).value().data.as_string;
|
||||
auto type_die_offset = variable_die.get_attribute(Dwarf::Attribute::Type);
|
||||
ASSERT(type_die_offset.has_value());
|
||||
ASSERT(type_die_offset.value().type == Dwarf::DIE::AttributeValue::Type::DieReference);
|
||||
|
||||
auto type_die = variable_die.get_die_at_offset(type_die_offset.value().data.as_u32);
|
||||
auto type_name = type_die.get_attribute(Dwarf::Attribute::Name);
|
||||
if (type_name.has_value()) {
|
||||
variable_info->type = type_name.value().data.as_string;
|
||||
} else {
|
||||
dbg() << "Unnamed DWRAF type at offset: " << type_die.offset();
|
||||
variable_info->name = "[Unnamed Type]";
|
||||
}
|
||||
|
||||
auto location_info = variable_die.get_attribute(Dwarf::Attribute::Location);
|
||||
if (!location_info.has_value()) {
|
||||
location_info = variable_die.get_attribute(Dwarf::Attribute::MemberLocation);
|
||||
}
|
||||
|
||||
if (location_info.has_value()) {
|
||||
if (location_info.value().type == Dwarf::DIE::AttributeValue::Type::UnsignedNumber) {
|
||||
variable_info->location_type = VariableInfo::LocationType::Address;
|
||||
variable_info->location_data.address = location_info.value().data.as_u32;
|
||||
}
|
||||
|
||||
if (location_info.value().type == Dwarf::DIE::AttributeValue::Type::DwarfExpression) {
|
||||
auto expression_bytes = ByteBuffer::wrap(location_info.value().data.as_dwarf_expression.bytes, location_info.value().data.as_dwarf_expression.length);
|
||||
auto value = Dwarf::Expression::evaluate(expression_bytes, regs);
|
||||
|
||||
if (value.type != Dwarf::Expression::Type::None) {
|
||||
ASSERT(value.type == Dwarf::Expression::Type::UnsignedIntetger);
|
||||
variable_info->location_type = VariableInfo::LocationType::Address;
|
||||
variable_info->location_data.address = value.data.as_u32;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type_die.for_each_child([&](const Dwarf::DIE& member) {
|
||||
if (member.is_null())
|
||||
return;
|
||||
auto member_variable = create_variable_info(member, regs);
|
||||
ASSERT(member_variable->location_type == DebugInfo::VariableInfo::LocationType::Address);
|
||||
ASSERT(variable_info->location_type == DebugInfo::VariableInfo::LocationType::Address);
|
||||
|
||||
member_variable->location_data.address += variable_info->location_data.address;
|
||||
member_variable->parent = variable_info.ptr();
|
||||
|
||||
variable_info->members.append(move(member_variable));
|
||||
});
|
||||
|
||||
return variable_info;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue