1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:38:11 +00:00

LibDebug: Implement support for DWARF 5 line programs

This commit is contained in:
Gunnar Beutner 2021-04-28 22:19:46 +02:00 committed by Andreas Kling
parent 6b4448b623
commit d2f0984fef
3 changed files with 181 additions and 44 deletions

View file

@ -6,13 +6,15 @@
#include "LineProgram.h"
#include <AK/Debug.h>
#include <AK/Function.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
namespace Debug::Dwarf {
LineProgram::LineProgram(InputMemoryStream& stream)
: m_stream(stream)
LineProgram::LineProgram(DwarfInfo& dwarf_info, InputMemoryStream& stream)
: m_dwarf_info(dwarf_info)
, m_stream(stream)
{
m_unit_offset = m_stream.offset();
parse_unit_header();
@ -23,45 +25,100 @@ LineProgram::LineProgram(InputMemoryStream& stream)
void LineProgram::parse_unit_header()
{
m_stream >> Bytes { &m_unit_header, sizeof(m_unit_header) };
m_stream >> m_unit_header;
VERIFY(m_unit_header.version == DWARF_VERSION);
VERIFY(m_unit_header.opcode_base == SPECIAL_OPCODES_BASE);
VERIFY(m_unit_header.version() >= MIN_DWARF_VERSION && m_unit_header.version() <= MAX_DWARF_VERSION);
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());
}
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;
m_stream >> path_entry_format_count;
Vector<PathEntryFormat> format_descriptions;
for (u8 i = 0; i < path_entry_format_count; i++) {
size_t content_type = 0;
m_stream.read_LEB128_unsigned(content_type);
size_t data_form = 0;
m_stream.read_LEB128_unsigned(data_form);
format_descriptions.empend((ContentType)content_type, (AttributeDataForm)data_form);
}
size_t paths_count = 0;
m_stream.read_LEB128_unsigned(paths_count);
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, m_stream);
switch (format_description.type) {
case ContentType::Path:
entry.path = value.data.as_string;
break;
case ContentType::DirectoryIndex:
entry.directory_index = value.data.as_u32;
break;
default:
dbgln_if(DWARF_DEBUG, "Unhandled path list attribute: {}", (int)format_description.type);
}
}
callback(entry);
}
} else {
while (m_stream.peek_or_error()) {
String path;
m_stream >> path;
dbgln_if(DWARF_DEBUG, "path: {}", path);
PathEntry entry;
entry.path = path;
if (list_type == PathListType::Filenames) {
size_t directory_index = 0;
m_stream.read_LEB128_unsigned(directory_index);
size_t _unused = 0;
m_stream.read_LEB128_unsigned(_unused); // skip modification time
m_stream.read_LEB128_unsigned(_unused); // skip file size
entry.directory_index = directory_index;
dbgln_if(DWARF_DEBUG, "file: {}, directory index: {}", path, directory_index);
}
callback(entry);
}
m_stream.handle_recoverable_error();
m_stream.discard_or_error(1);
}
VERIFY(!m_stream.has_any_error());
}
void LineProgram::parse_source_directories()
{
m_source_directories.append(".");
while (m_stream.peek_or_error()) {
String directory;
m_stream >> directory;
dbgln_if(DWARF_DEBUG, "directory: {}", directory);
m_source_directories.append(move(directory));
if (m_unit_header.version() < 5) {
m_source_directories.append(".");
}
m_stream.handle_recoverable_error();
m_stream.discard_or_error(1);
VERIFY(!m_stream.has_any_error());
parse_path_entries([this](PathEntry& entry) {
m_source_directories.append(entry.path);
},
PathListType::Directories);
}
void LineProgram::parse_source_files()
{
m_source_files.append({ ".", 0 });
while (!m_stream.eof() && m_stream.peek_or_error()) {
String file_name;
m_stream >> file_name;
size_t directory_index = 0;
m_stream.read_LEB128_unsigned(directory_index);
size_t _unused = 0;
m_stream.read_LEB128_unsigned(_unused); // skip modification time
m_stream.read_LEB128_unsigned(_unused); // skip file size
dbgln_if(DWARF_DEBUG, "file: {}, directory index: {}", file_name, directory_index);
m_source_files.append({ file_name, directory_index });
if (m_unit_header.version() < 5) {
m_source_files.append({ ".", 0 });
}
m_stream.discard_or_error(1);
VERIFY(!m_stream.has_any_error());
parse_path_entries([this](PathEntry& entry) {
m_source_files.append({ entry.path, entry.directory_index });
},
PathListType::Filenames);
}
void LineProgram::append_to_line_info()
@ -88,7 +145,7 @@ void LineProgram::reset_registers()
m_address = 0;
m_line = 1;
m_file_index = 1;
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()
@ -131,7 +188,7 @@ void LineProgram::handle_standard_opcode(u8 opcode)
case StandardOpcodes::AdvancePc: {
size_t operand = 0;
m_stream.read_LEB128_unsigned(operand);
size_t delta = operand * m_unit_header.min_instruction_length;
size_t delta = operand * m_unit_header.min_instruction_length();
dbgln_if(DWARF_DEBUG, "AdvancePC by: {} to: {:p}", delta, m_address + delta);
m_address += delta;
break;
@ -165,9 +222,9 @@ void LineProgram::handle_standard_opcode(u8 opcode)
break;
}
case StandardOpcodes::ConstAddPc: {
u8 adjusted_opcode = 255 - SPECIAL_OPCODES_BASE;
ssize_t address_increment = (adjusted_opcode / m_unit_header.line_range) * m_unit_header.min_instruction_length;
address_increment *= m_unit_header.min_instruction_length;
u8 adjusted_opcode = 255 - m_unit_header.opcode_base();
ssize_t address_increment = (adjusted_opcode / m_unit_header.line_range()) * m_unit_header.min_instruction_length();
address_increment *= m_unit_header.min_instruction_length();
dbgln_if(DWARF_DEBUG, "ConstAddPc: advance pc by: {} to: {}", address_increment, (m_address + address_increment));
m_address += address_increment;
break;
@ -192,9 +249,9 @@ void LineProgram::handle_standard_opcode(u8 opcode)
}
void LineProgram::handle_special_opcode(u8 opcode)
{
u8 adjusted_opcode = opcode - SPECIAL_OPCODES_BASE;
ssize_t address_increment = (adjusted_opcode / m_unit_header.line_range) * m_unit_header.min_instruction_length;
ssize_t line_increment = m_unit_header.line_base + (adjusted_opcode % m_unit_header.line_range);
u8 adjusted_opcode = opcode - m_unit_header.opcode_base();
ssize_t address_increment = (adjusted_opcode / m_unit_header.line_range()) * m_unit_header.min_instruction_length();
ssize_t line_increment = m_unit_header.line_base() + (adjusted_opcode % m_unit_header.line_range());
m_address += address_increment;
m_line += line_increment;
@ -211,7 +268,7 @@ void LineProgram::run_program()
{
reset_registers();
while ((size_t)m_stream.offset() < m_unit_offset + sizeof(u32) + m_unit_header.length) {
while ((size_t)m_stream.offset() < m_unit_offset + sizeof(u32) + m_unit_header.length()) {
u8 opcode = 0;
m_stream >> opcode;