diff options
Diffstat (limited to 'Userland/Libraries/LibDebug')
20 files changed, 2896 insertions, 0 deletions
diff --git a/Userland/Libraries/LibDebug/CMakeLists.txt b/Userland/Libraries/LibDebug/CMakeLists.txt new file mode 100644 index 0000000000..a2fa57a9a5 --- /dev/null +++ b/Userland/Libraries/LibDebug/CMakeLists.txt @@ -0,0 +1,14 @@ +set(SOURCES + DebugInfo.cpp + DebugSession.cpp + Dwarf/AbbreviationsMap.cpp + Dwarf/CompilationUnit.cpp + Dwarf/DIE.cpp + Dwarf/DwarfInfo.cpp + Dwarf/Expression.cpp + Dwarf/LineProgram.cpp + StackFrameUtils.cpp +) + +serenity_lib(LibDebug debug) +target_link_libraries(LibDebug LibC LibRegex) diff --git a/Userland/Libraries/LibDebug/DebugInfo.cpp b/Userland/Libraries/LibDebug/DebugInfo.cpp new file mode 100644 index 0000000000..c9e773d0ac --- /dev/null +++ b/Userland/Libraries/LibDebug/DebugInfo.cpp @@ -0,0 +1,371 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "DebugInfo.h" +#include <AK/LexicalPath.h> +#include <AK/MemoryStream.h> +#include <AK/QuickSort.h> +#include <LibDebug/Dwarf/CompilationUnit.h> +#include <LibDebug/Dwarf/DwarfInfo.h> +#include <LibDebug/Dwarf/Expression.h> + +//#define DEBUG_SPAM + +namespace Debug { + +DebugInfo::DebugInfo(NonnullOwnPtr<const ELF::Image> elf, String source_root, FlatPtr base_address) + : m_elf(move(elf)) + , m_source_root(source_root) + , m_base_address(base_address) + , m_dwarf_info(*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()) { +#ifdef DEBUG_SPAM + dbgln("DWARF inlined functions are not supported"); +#endif + return; + } + if (child.get_attribute(Dwarf::Attribute::Ranges).has_value()) { +#ifdef DEBUG_SPAM + dbgln("DWARF ranges are not supported"); +#endif + 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; + + if (!child.get_attribute(Dwarf::Attribute::LowPc).has_value()) { +#ifdef DEBUG_SPAM + dbgln("DWARF: Couldn't find attribute LowPc for scope"); +#endif + return; + } + 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 + || variable_entry.tag() == Dwarf::EntryTag::FormalParameter)) + return; + scope.dies_of_variables.append(variable_entry); + }); + m_scopes.append(scope); + + parse_scopes_impl(child); + }); +} + +void DebugInfo::prepare_lines() +{ + auto section = elf().lookup_section(".debug_line"); + if (section.is_undefined()) + return; + + InputMemoryStream stream { section.bytes() }; + + Vector<Dwarf::LineProgram::LineInfo> all_lines; + while (!stream.eof()) { + Dwarf::LineProgram program(stream); + all_lines.append(program.lines()); + } + + String serenity_slash("serenity/"); + + for (auto& line_info : all_lines) { + String file_path = line_info.file; + if (file_path.contains("Toolchain/") || file_path.contains("libgcc")) + continue; + if (file_path.contains(serenity_slash)) { + auto start_index = file_path.index_of(serenity_slash).value() + serenity_slash.length(); + file_path = file_path.substring(start_index, file_path.length() - start_index); + } + if (file_path.starts_with("./") && !m_source_root.is_null()) { + file_path = LexicalPath::canonicalized_path(String::formatted("{}/{}", m_source_root, file_path)); + } + m_sorted_lines.append({ line_info.address, file_path, line_info.line }); + } + quick_sort(m_sorted_lines, [](auto& a, auto& b) { + return a.address < b.address; + }); +} + +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) + return {}; + + // TODO: We can do a binray search here + for (size_t i = 0; i < m_sorted_lines.size() - 1; ++i) { + if (m_sorted_lines[i + 1].address > target_address) { + return SourcePosition::from_line_info(m_sorted_lines[i]); + } + } + return {}; +} + +Optional<DebugInfo::SourcePositionAndAddress> DebugInfo::get_address_from_source_position(const String& file, size_t line) const +{ + String file_path = file; + if (!file_path.starts_with("/")) + file_path = String::format("/%s", file_path.characters()); + + constexpr char SERENITY_LIBS_PREFIX[] = "/usr/src/serenity"; + if (file.starts_with(SERENITY_LIBS_PREFIX)) { + file_path = file.substring(sizeof(SERENITY_LIBS_PREFIX), file.length() - sizeof(SERENITY_LIBS_PREFIX)); + file_path = String::format("../%s", file_path.characters()); + } + + Optional<SourcePositionAndAddress> result; + for (const auto& line_entry : m_sorted_lines) { + if (!line_entry.file.ends_with(file_path)) + continue; + + if (line_entry.line > line) + continue; + + // We look for the source position that is closest to the desired position, and is not after it. + // For example, get_address_of_source_position("main.cpp", 73) could return the address for an instruction whose location is ("main.cpp", 72) + // as there might not be an instruction mapped for "main.cpp", 73. + if (!result.has_value() || (line_entry.line > result.value().line)) { + result = SourcePositionAndAddress { line_entry.file, line_entry.line, line_entry.address }; + } + } + return result; +} + +NonnullOwnPtrVector<DebugInfo::VariableInfo> DebugInfo::get_variables_in_current_scope(const PtraceRegisters& regs) const +{ + NonnullOwnPtrVector<DebugInfo::VariableInfo> variables; + + // TODO: We can store the scopes in a better data structure + for (const auto& scope : m_scopes) { + if (regs.eip - m_base_address < scope.address_low || regs.eip - m_base_address >= scope.address_high) + continue; + + for (const auto& die_entry : scope.dies_of_variables) { + auto variable_info = create_variable_info(die_entry, regs); + if (!variable_info) + continue; + variables.append(variable_info.release_nonnull()); + } + } + return variables; +} + +static Optional<Dwarf::DIE> parse_variable_type_die(const Dwarf::DIE& variable_die, DebugInfo::VariableInfo& variable_info) +{ + auto type_die_offset = variable_die.get_attribute(Dwarf::Attribute::Type); + if (!type_die_offset.has_value()) + return {}; + + 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_name = type_name.value().data.as_string; + } else { + dbgln("Unnamed DWARF type at offset: {}", type_die.offset()); + variable_info.name = "[Unnamed Type]"; + } + + return type_die; +} + +static void parse_variable_location(const Dwarf::DIE& variable_die, DebugInfo::VariableInfo& variable_info, const PtraceRegisters& regs) +{ + 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()) + return; + + switch (location_info.value().type) { + case Dwarf::DIE::AttributeValue::Type::UnsignedNumber: + variable_info.location_type = DebugInfo::VariableInfo::LocationType::Address; + variable_info.location_data.address = location_info.value().data.as_u32; + break; + case Dwarf::DIE::AttributeValue::Type::DwarfExpression: { + auto expression_bytes = ReadonlyBytes { location_info.value().data.as_raw_bytes.bytes, location_info.value().data.as_raw_bytes.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 = DebugInfo::VariableInfo::LocationType::Address; + variable_info.location_data.address = value.data.as_u32; + } + break; + } + default: + dbgln("Warninig: unhandled Dwarf location type: {}", (int)location_info.value().type); + } +} + +OwnPtr<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 + || variable_die.tag() == Dwarf::EntryTag::FormalParameter + || variable_die.tag() == Dwarf::EntryTag::EnumerationType + || variable_die.tag() == Dwarf::EntryTag::Enumerator + || variable_die.tag() == Dwarf::EntryTag::StructureType); + + if (variable_die.tag() == Dwarf::EntryTag::FormalParameter + && !variable_die.get_attribute(Dwarf::Attribute::Name).has_value()) { + // We don't want to display info for unused parameters + return {}; + } + + NonnullOwnPtr<VariableInfo> variable_info = make<VariableInfo>(); + variable_info->name = variable_die.get_attribute(Dwarf::Attribute::Name).value().data.as_string; + + auto type_die = parse_variable_type_die(variable_die, *variable_info); + + if (variable_die.tag() == Dwarf::EntryTag::Enumerator) { + auto constant = variable_die.get_attribute(Dwarf::Attribute::ConstValue); + ASSERT(constant.has_value()); + switch (constant.value().type) { + case Dwarf::DIE::AttributeValue::Type::UnsignedNumber: + variable_info->constant_data.as_u32 = constant.value().data.as_u32; + break; + case Dwarf::DIE::AttributeValue::Type::SignedNumber: + variable_info->constant_data.as_i32 = constant.value().data.as_i32; + break; + case Dwarf::DIE::AttributeValue::Type::String: + variable_info->constant_data.as_string = constant.value().data.as_string; + break; + default: + ASSERT_NOT_REACHED(); + } + } else { + parse_variable_location(variable_die, *variable_info, regs); + } + + if (type_die.has_value()) { + OwnPtr<VariableInfo> type_info; + if (type_die.value().tag() == Dwarf::EntryTag::EnumerationType || type_die.value().tag() == Dwarf::EntryTag::StructureType) { + type_info = create_variable_info(type_die.value(), regs); + } + + type_die.value().for_each_child([&](const Dwarf::DIE& member) { + if (member.is_null()) + return; + auto member_variable = create_variable_info(member, regs); + ASSERT(member_variable); + + if (type_die.value().tag() == Dwarf::EntryTag::EnumerationType) { + member_variable->parent = type_info.ptr(); + type_info->members.append(member_variable.release_nonnull()); + } else { + if (variable_info->location_type == DebugInfo::VariableInfo::LocationType::None) { + return; + } + ASSERT(variable_info->location_type == DebugInfo::VariableInfo::LocationType::Address); + + if (member_variable->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(member_variable.release_nonnull()); + } + }); + + if (type_info) { + variable_info->type = move(type_info); + variable_info->type->type_tag = type_die.value().tag(); + } + } + + return variable_info; +} + +String DebugInfo::name_of_containing_function(u32 address) const +{ + auto function = get_containing_function(address); + if (!function.has_value()) + return {}; + return function.value().name; +} + +Optional<DebugInfo::VariablesScope> DebugInfo::get_containing_function(u32 address) const +{ + for (const auto& scope : m_scopes) { + if (!scope.is_function || address < scope.address_low || address >= scope.address_high) + continue; + return scope; + } + return {}; +} + +Vector<DebugInfo::SourcePosition> DebugInfo::source_lines_in_scope(const VariablesScope& scope) const +{ + Vector<DebugInfo::SourcePosition> source_lines; + for (const auto& line : m_sorted_lines) { + if (line.address < scope.address_low) + continue; + + if (line.address >= scope.address_high) + break; + source_lines.append(SourcePosition::from_line_info(line)); + } + return source_lines; +} + +DebugInfo::SourcePosition DebugInfo::SourcePosition::from_line_info(const Dwarf::LineProgram::LineInfo& line) +{ + return { line.file, line.line, { line.address } }; +} + +} diff --git a/Userland/Libraries/LibDebug/DebugInfo.h b/Userland/Libraries/LibDebug/DebugInfo.h new file mode 100644 index 0000000000..8c73d7dee4 --- /dev/null +++ b/Userland/Libraries/LibDebug/DebugInfo.h @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include <AK/NonnullOwnPtrVector.h> +#include <AK/NonnullRefPtr.h> +#include <AK/Optional.h> +#include <AK/Vector.h> +#include <LibDebug/Dwarf/DIE.h> +#include <LibDebug/Dwarf/DwarfInfo.h> +#include <LibDebug/Dwarf/LineProgram.h> +#include <LibELF/Image.h> +#include <sys/arch/i386/regs.h> + +namespace Debug { + +class DebugInfo { +public: + explicit DebugInfo(NonnullOwnPtr<const ELF::Image>, String source_root = {}, FlatPtr base_address = 0); + + const ELF::Image& elf() const { return *m_elf; } + + struct SourcePosition { + FlyString file_path; + size_t line_number { 0 }; + Optional<u32> address_of_first_statement; + + SourcePosition() + : SourcePosition(String::empty(), 0) + { + } + SourcePosition(String file_path, size_t line_number) + : file_path(file_path) + , line_number(line_number) + { + } + SourcePosition(String file_path, size_t line_number, u32 address_of_first_statement) + : file_path(file_path) + , line_number(line_number) + , address_of_first_statement(address_of_first_statement) + { + } + + bool operator==(const SourcePosition& other) const { return file_path == other.file_path && line_number == other.line_number; } + bool operator!=(const SourcePosition& other) const { return !(*this == other); } + + static SourcePosition from_line_info(const Dwarf::LineProgram::LineInfo&); + }; + + struct VariableInfo { + enum class LocationType { + None, + Address, + Register, + }; + String name; + String type_name; + LocationType location_type { LocationType::None }; + union { + u32 address; + } location_data { 0 }; + + union { + u32 as_u32; + u32 as_i32; + const char* as_string; + } constant_data { 0 }; + + Dwarf::EntryTag type_tag; + OwnPtr<VariableInfo> type; + NonnullOwnPtrVector<VariableInfo> members; + VariableInfo* parent { nullptr }; + + bool is_enum_type() const { return type && type->type_tag == Dwarf::EntryTag::EnumerationType; } + }; + + struct VariablesScope { + bool is_function { false }; + String name; + u32 address_low { 0 }; + u32 address_high { 0 }; // Non-inclusive - the lowest address after address_low that's not in this scope + Vector<Dwarf::DIE> dies_of_variables; + }; + + NonnullOwnPtrVector<VariableInfo> get_variables_in_current_scope(const PtraceRegisters&) const; + + Optional<SourcePosition> get_source_position(u32 address) const; + + struct SourcePositionAndAddress { + String file; + size_t line; + FlatPtr address; + }; + + Optional<SourcePositionAndAddress> get_address_from_source_position(const String& file, size_t line) const; + + template<typename Callback> + void for_each_source_position(Callback callback) const + { + FlyString previous_file = ""; + size_t previous_line = 0; + for (const auto& line_info : m_sorted_lines) { + if (line_info.file == previous_file && line_info.line == previous_line) + continue; + previous_file = line_info.file; + previous_line = line_info.line; + callback({ line_info.file, line_info.line, line_info.address }); + } + } + + String name_of_containing_function(u32 address) const; + Vector<SourcePosition> source_lines_in_scope(const VariablesScope&) const; + Optional<VariablesScope> get_containing_function(u32 address) const; + +private: + void prepare_variable_scopes(); + void prepare_lines(); + void parse_scopes_impl(const Dwarf::DIE& die); + OwnPtr<VariableInfo> create_variable_info(const Dwarf::DIE& variable_die, const PtraceRegisters&) const; + + NonnullOwnPtr<const ELF::Image> m_elf; + String m_source_root; + FlatPtr m_base_address { 0 }; + Dwarf::DwarfInfo m_dwarf_info; + + Vector<VariablesScope> m_scopes; + Vector<Dwarf::LineProgram::LineInfo> m_sorted_lines; +}; + +} diff --git a/Userland/Libraries/LibDebug/DebugSession.cpp b/Userland/Libraries/LibDebug/DebugSession.cpp new file mode 100644 index 0000000000..82871d070c --- /dev/null +++ b/Userland/Libraries/LibDebug/DebugSession.cpp @@ -0,0 +1,432 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "DebugSession.h" +#include <AK/JsonObject.h> +#include <AK/JsonValue.h> +#include <AK/LexicalPath.h> +#include <AK/Optional.h> +#include <LibCore/File.h> +#include <LibRegex/Regex.h> +#include <stdlib.h> + +namespace Debug { + +DebugSession::DebugSession(pid_t pid, String source_root) + : m_debuggee_pid(pid) + , m_source_root(source_root) + +{ +} + +DebugSession::~DebugSession() +{ + if (m_is_debuggee_dead) + return; + + for (const auto& bp : m_breakpoints) { + disable_breakpoint(bp.key); + } + m_breakpoints.clear(); + + if (ptrace(PT_DETACH, m_debuggee_pid, 0, 0) < 0) { + perror("PT_DETACH"); + } +} + +OwnPtr<DebugSession> DebugSession::exec_and_attach(const String& command, String source_root) +{ + auto pid = fork(); + + if (pid < 0) { + perror("fork"); + exit(1); + } + + if (!pid) { + if (ptrace(PT_TRACE_ME, 0, 0, 0) < 0) { + perror("PT_TRACE_ME"); + exit(1); + } + + auto parts = command.split(' '); + ASSERT(!parts.is_empty()); + const char** args = (const char**)calloc(parts.size() + 1, sizeof(const char*)); + for (size_t i = 0; i < parts.size(); i++) { + args[i] = parts[i].characters(); + } + const char** envp = (const char**)calloc(2, sizeof(const char*)); + // This causes loader to stop on a breakpoint before jumping to the entry point of the program. + envp[0] = "_LOADER_BREAKPOINT=1"; + int rc = execvpe(args[0], const_cast<char**>(args), const_cast<char**>(envp)); + if (rc < 0) { + perror("execvp"); + } + ASSERT_NOT_REACHED(); + } + + if (waitpid(pid, nullptr, WSTOPPED) != pid) { + perror("waitpid"); + return {}; + } + + if (ptrace(PT_ATTACH, pid, 0, 0) < 0) { + perror("PT_ATTACH"); + return {}; + } + + // We want to continue until the exit from the 'execve' sycsall. + // This ensures that when we start debugging the process + // it executes the target image, and not the forked image of the tracing process. + // NOTE: we only need to do this when we are debugging a new process (i.e not attaching to a process that's already running!) + + if (waitpid(pid, nullptr, WSTOPPED) != pid) { + perror("wait_pid"); + return {}; + } + + auto debug_session = adopt_own(*new DebugSession(pid, source_root)); + + // Continue until breakpoint before entry point of main program + int wstatus = debug_session->continue_debuggee_and_wait(); + if (WSTOPSIG(wstatus) != SIGTRAP) { + dbgln("expected SIGTRAP"); + return {}; + } + + // At this point, libraries should have been loaded + debug_session->update_loaded_libs(); + + return move(debug_session); +} + +bool DebugSession::poke(u32* address, u32 data) +{ + if (ptrace(PT_POKE, m_debuggee_pid, (void*)address, data) < 0) { + perror("PT_POKE"); + return false; + } + return true; +} + +Optional<u32> DebugSession::peek(u32* address) const +{ + Optional<u32> result; + int rc = ptrace(PT_PEEK, m_debuggee_pid, (void*)address, 0); + if (errno == 0) + result = static_cast<u32>(rc); + return result; +} + +bool DebugSession::insert_breakpoint(void* address) +{ + // We insert a software breakpoint by + // patching the first byte of the instruction at 'address' + // with the breakpoint instruction (int3) + + if (m_breakpoints.contains(address)) + return false; + + auto original_bytes = peek(reinterpret_cast<u32*>(address)); + + if (!original_bytes.has_value()) + return false; + + ASSERT((original_bytes.value() & 0xff) != BREAKPOINT_INSTRUCTION); + + BreakPoint breakpoint { address, original_bytes.value(), BreakPointState::Disabled }; + + m_breakpoints.set(address, breakpoint); + + enable_breakpoint(breakpoint.address); + + return true; +} + +bool DebugSession::disable_breakpoint(void* address) +{ + auto breakpoint = m_breakpoints.get(address); + ASSERT(breakpoint.has_value()); + if (!poke(reinterpret_cast<u32*>(reinterpret_cast<char*>(breakpoint.value().address)), breakpoint.value().original_first_word)) + return false; + + auto bp = m_breakpoints.get(breakpoint.value().address).value(); + bp.state = BreakPointState::Disabled; + m_breakpoints.set(bp.address, bp); + return true; +} + +bool DebugSession::enable_breakpoint(void* address) +{ + auto breakpoint = m_breakpoints.get(address); + ASSERT(breakpoint.has_value()); + + ASSERT(breakpoint.value().state == BreakPointState::Disabled); + + if (!poke(reinterpret_cast<u32*>(breakpoint.value().address), (breakpoint.value().original_first_word & ~(uint32_t)0xff) | BREAKPOINT_INSTRUCTION)) + return false; + + auto bp = m_breakpoints.get(breakpoint.value().address).value(); + bp.state = BreakPointState::Enabled; + m_breakpoints.set(bp.address, bp); + return true; +} + +bool DebugSession::remove_breakpoint(void* address) +{ + if (!disable_breakpoint(address)) + return false; + + m_breakpoints.remove(address); + return true; +} + +bool DebugSession::breakpoint_exists(void* address) const +{ + return m_breakpoints.contains(address); +} + +PtraceRegisters DebugSession::get_registers() const +{ + PtraceRegisters regs; + if (ptrace(PT_GETREGS, m_debuggee_pid, ®s, 0) < 0) { + perror("PT_GETREGS"); + ASSERT_NOT_REACHED(); + } + return regs; +} + +void DebugSession::set_registers(const PtraceRegisters& regs) +{ + if (ptrace(PT_SETREGS, m_debuggee_pid, reinterpret_cast<void*>(&const_cast<PtraceRegisters&>(regs)), 0) < 0) { + perror("PT_SETREGS"); + ASSERT_NOT_REACHED(); + } +} + +void DebugSession::continue_debuggee(ContinueType type) +{ + int command = (type == ContinueType::FreeRun) ? PT_CONTINUE : PT_SYSCALL; + if (ptrace(command, m_debuggee_pid, 0, 0) < 0) { + perror("continue"); + ASSERT_NOT_REACHED(); + } +} + +int DebugSession::continue_debuggee_and_wait(ContinueType type) +{ + continue_debuggee(type); + int wstatus = 0; + if (waitpid(m_debuggee_pid, &wstatus, WSTOPPED | WEXITED) != m_debuggee_pid) { + perror("waitpid"); + ASSERT_NOT_REACHED(); + } + return wstatus; +} + +void* DebugSession::single_step() +{ + // Single stepping works by setting the x86 TRAP flag bit in the eflags register. + // This flag causes the cpu to enter single-stepping mode, which causes + // Interrupt 1 (debug interrupt) to be emitted after every instruction. + // To single step the program, we set the TRAP flag and continue the debuggee. + // After the debuggee has stopped, we clear the TRAP flag. + + auto regs = get_registers(); + constexpr u32 TRAP_FLAG = 0x100; + regs.eflags |= TRAP_FLAG; + set_registers(regs); + + continue_debuggee(); + + if (waitpid(m_debuggee_pid, 0, WSTOPPED) != m_debuggee_pid) { + perror("waitpid"); + ASSERT_NOT_REACHED(); + } + + regs = get_registers(); + regs.eflags &= ~(TRAP_FLAG); + set_registers(regs); + return (void*)regs.eip; +} + +void DebugSession::detach() +{ + for (auto& breakpoint : m_breakpoints.keys()) { + remove_breakpoint(breakpoint); + } + continue_debuggee(); +} + +Optional<DebugSession::InsertBreakpointAtSymbolResult> DebugSession::insert_breakpoint(const String& symbol_name) +{ + Optional<InsertBreakpointAtSymbolResult> result; + for_each_loaded_library([this, symbol_name, &result](auto& lib) { + // The loader contains its own definitions for LibC symbols, so we don't want to include it in the search. + if (lib.name == "Loader.so") + return IterationDecision::Continue; + + auto symbol = lib.debug_info->elf().find_demangled_function(symbol_name); + if (!symbol.has_value()) + return IterationDecision::Continue; + + auto breakpoint_address = symbol.value().value() + lib.base_address; + bool rc = this->insert_breakpoint(reinterpret_cast<void*>(breakpoint_address)); + if (!rc) + return IterationDecision::Break; + + result = InsertBreakpointAtSymbolResult { lib.name, breakpoint_address }; + return IterationDecision::Break; + }); + return result; +} + +Optional<DebugSession::InsertBreakpointAtSourcePositionResult> DebugSession::insert_breakpoint(const String& file_name, size_t line_number) +{ + auto address_and_source_position = get_address_from_source_position(file_name, line_number); + if (!address_and_source_position.has_value()) + return {}; + + auto address = address_and_source_position.value().address; + bool rc = this->insert_breakpoint(reinterpret_cast<void*>(address)); + if (!rc) + return {}; + + auto lib = library_at(address); + ASSERT(lib); + + return InsertBreakpointAtSourcePositionResult { lib->name, address_and_source_position.value().file, address_and_source_position.value().line, address }; +} + +void DebugSession::update_loaded_libs() +{ + auto file = Core::File::construct(String::format("/proc/%u/vm", m_debuggee_pid)); + bool rc = file->open(Core::IODevice::ReadOnly); + ASSERT(rc); + + auto file_contents = file->read_all(); + auto json = JsonValue::from_string(file_contents); + ASSERT(json.has_value()); + + auto vm_entries = json.value().as_array(); + Regex<PosixExtended> re("(.+): \\.text"); + + auto get_path_to_object = [&re](const String& vm_name) -> Optional<String> { + if (vm_name == "/usr/lib/Loader.so") + return vm_name; + RegexResult result; + auto rc = re.search(vm_name, result); + if (!rc) + return {}; + auto lib_name = result.capture_group_matches.at(0).at(0).view.u8view().to_string(); + if (lib_name.starts_with("/")) + return lib_name; + return String::format("/usr/lib/%s", lib_name.characters()); + }; + + vm_entries.for_each([&](auto& entry) { + // TODO: check that region is executable + auto vm_name = entry.as_object().get("name").as_string(); + + auto object_path = get_path_to_object(vm_name); + if (!object_path.has_value()) + return IterationDecision::Continue; + + String lib_name = object_path.value(); + if (lib_name.ends_with(".so")) + lib_name = LexicalPath(object_path.value()).basename(); + + // FIXME: DebugInfo currently cannot parse the debug information of libgcc_s.so + if (lib_name == "libgcc_s.so") + return IterationDecision::Continue; + + if (m_loaded_libraries.contains(lib_name)) + return IterationDecision::Continue; + + auto file_or_error = MappedFile ::map(object_path.value()); + if (file_or_error.is_error()) + return IterationDecision::Continue; + + FlatPtr base_address = entry.as_object().get("address").as_u32(); + auto debug_info = make<DebugInfo>(make<ELF::Image>(file_or_error.value()->bytes()), m_source_root, base_address); + auto lib = make<LoadedLibrary>(lib_name, file_or_error.release_value(), move(debug_info), base_address); + m_loaded_libraries.set(lib_name, move(lib)); + + return IterationDecision::Continue; + }); +} + +const DebugSession::LoadedLibrary* DebugSession::library_at(FlatPtr address) const +{ + const LoadedLibrary* result = nullptr; + for_each_loaded_library([&result, address](const auto& lib) { + if (address >= lib.base_address && address < lib.base_address + lib.debug_info->elf().size()) { + result = &lib; + return IterationDecision::Break; + } + return IterationDecision::Continue; + }); + return result; +} + +Optional<DebugSession::SymbolicationResult> DebugSession::symbolicate(FlatPtr address) const +{ + auto* lib = library_at(address); + if (!lib) + return {}; + //FIXME: ELF::Image symlicate() API should return String::empty() if symbol is not found (It currently returns ??) + auto symbol = lib->debug_info->elf().symbolicate(address - lib->base_address); + return { { lib->name, symbol } }; +} + +Optional<DebugInfo::SourcePositionAndAddress> DebugSession::get_address_from_source_position(const String& file, size_t line) const +{ + Optional<DebugInfo::SourcePositionAndAddress> result; + for_each_loaded_library([this, file, line, &result](auto& lib) { + // The loader contains its own definitions for LibC symbols, so we don't want to include it in the search. + if (lib.name == "Loader.so") + return IterationDecision::Continue; + + auto source_position_and_address = lib.debug_info->get_address_from_source_position(file, line); + if (!source_position_and_address.has_value()) + return IterationDecision::Continue; + + result = source_position_and_address; + result.value().address += lib.base_address; + return IterationDecision::Break; + }); + return result; +} + +Optional<DebugInfo::SourcePosition> DebugSession::get_source_position(FlatPtr address) const +{ + auto* lib = library_at(address); + if (!lib) + return {}; + return lib->debug_info->get_source_position(address - lib->base_address); +} + +} diff --git a/Userland/Libraries/LibDebug/DebugSession.h b/Userland/Libraries/LibDebug/DebugSession.h new file mode 100644 index 0000000000..f30174a740 --- /dev/null +++ b/Userland/Libraries/LibDebug/DebugSession.h @@ -0,0 +1,310 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include <AK/Demangle.h> +#include <AK/HashMap.h> +#include <AK/MappedFile.h> +#include <AK/NonnullRefPtr.h> +#include <AK/Optional.h> +#include <AK/OwnPtr.h> +#include <AK/String.h> +#include <LibC/sys/arch/i386/regs.h> +#include <LibDebug/DebugInfo.h> +#include <signal.h> +#include <stdio.h> +#include <sys/ptrace.h> +#include <sys/wait.h> +#include <unistd.h> + +namespace Debug { + +class DebugSession { +public: + static OwnPtr<DebugSession> exec_and_attach(const String& command, String source_root = {}); + + ~DebugSession(); + + int pid() const { return m_debuggee_pid; } + + bool poke(u32* address, u32 data); + Optional<u32> peek(u32* address) const; + + enum class BreakPointState { + Enabled, + Disabled, + }; + + struct BreakPoint { + void* address { nullptr }; + u32 original_first_word { 0 }; + BreakPointState state { BreakPointState::Disabled }; + }; + + struct InsertBreakpointAtSymbolResult { + String library_name; + FlatPtr address { 0 }; + }; + + Optional<InsertBreakpointAtSymbolResult> insert_breakpoint(const String& symbol_name); + + struct InsertBreakpointAtSourcePositionResult { + String library_name; + String file_name; + size_t line_number { 0 }; + FlatPtr address { 0 }; + }; + + Optional<InsertBreakpointAtSourcePositionResult> insert_breakpoint(const String& file_name, size_t line_number); + + bool insert_breakpoint(void* address); + bool disable_breakpoint(void* address); + bool enable_breakpoint(void* address); + bool remove_breakpoint(void* address); + bool breakpoint_exists(void* address) const; + + void dump_breakpoints() + { + for (auto addr : m_breakpoints.keys()) { + dbgln("{}", addr); + } + } + + PtraceRegisters get_registers() const; + void set_registers(const PtraceRegisters&); + + enum class ContinueType { + FreeRun, + Syscall, + }; + void continue_debuggee(ContinueType type = ContinueType::FreeRun); + + // Returns the wstatus result of waitpid() + int continue_debuggee_and_wait(ContinueType type = ContinueType::FreeRun); + + // Returns the new eip + void* single_step(); + + void detach(); + + enum DesiredInitialDebugeeState { + Running, + Stopped + }; + template<typename Callback> + void run(DesiredInitialDebugeeState, Callback); + + enum DebugDecision { + Continue, + SingleStep, + ContinueBreakAtSyscall, + Detach, + Kill, + }; + + enum DebugBreakReason { + Breakpoint, + Syscall, + Exited, + }; + + struct LoadedLibrary { + String name; + NonnullRefPtr<MappedFile> file; + NonnullOwnPtr<DebugInfo> debug_info; + FlatPtr base_address; + + LoadedLibrary(const String& name, NonnullRefPtr<MappedFile> file, NonnullOwnPtr<DebugInfo>&& debug_info, FlatPtr base_address) + : name(name) + , file(move(file)) + , debug_info(move(debug_info)) + , base_address(base_address) + { + } + }; + + template<typename Func> + void for_each_loaded_library(Func f) const + { + for (const auto& lib_name : m_loaded_libraries.keys()) { + const auto& lib = *m_loaded_libraries.get(lib_name).value(); + if (f(lib) == IterationDecision::Break) + break; + } + } + + const LoadedLibrary* library_at(FlatPtr address) const; + + struct SymbolicationResult { + String library_name; + String symbol; + }; + Optional<SymbolicationResult> symbolicate(FlatPtr address) const; + + Optional<DebugInfo::SourcePositionAndAddress> get_address_from_source_position(const String& file, size_t line) const; + + Optional<DebugInfo::SourcePosition> get_source_position(FlatPtr address) const; + +private: + explicit DebugSession(pid_t, String source_root); + + // x86 breakpoint instruction "int3" + static constexpr u8 BREAKPOINT_INSTRUCTION = 0xcc; + + void update_loaded_libs(); + + int m_debuggee_pid { -1 }; + String m_source_root; + bool m_is_debuggee_dead { false }; + + HashMap<void*, BreakPoint> m_breakpoints; + + // Maps from base address to loaded library + HashMap<String, NonnullOwnPtr<LoadedLibrary>> m_loaded_libraries; +}; + +template<typename Callback> +void DebugSession::run(DesiredInitialDebugeeState initial_debugee_state, Callback callback) +{ + + enum class State { + FirstIteration, + FreeRun, + Syscall, + ConsecutiveBreakpoint, + SingleStep, + }; + + State state { State::FirstIteration }; + + auto do_continue_and_wait = [&]() { + int wstatus = continue_debuggee_and_wait((state == State::Syscall) ? ContinueType::Syscall : ContinueType::FreeRun); + + // FIXME: This check actually only checks whether the debuggee + // stopped because it hit a breakpoint/syscall/is in single stepping mode or not + if (WSTOPSIG(wstatus) != SIGTRAP) { + callback(DebugBreakReason::Exited, Optional<PtraceRegisters>()); + m_is_debuggee_dead = true; + return true; + } + return false; + }; + + for (;;) { + if ((state == State::FirstIteration && initial_debugee_state == DesiredInitialDebugeeState::Running) || state == State::FreeRun || state == State::Syscall) { + if (do_continue_and_wait()) + break; + } + if (state == State::FirstIteration) + state = State::FreeRun; + + auto regs = get_registers(); + Optional<BreakPoint> current_breakpoint; + + if (state == State::FreeRun || state == State::Syscall) { + current_breakpoint = m_breakpoints.get((void*)((u32)regs.eip - 1)); + if (current_breakpoint.has_value()) + state = State::FreeRun; + } else { + current_breakpoint = m_breakpoints.get((void*)regs.eip); + } + + if (current_breakpoint.has_value()) { + // We want to make the breakpoint transparent to the user of the debugger. + // To achieive this, we perform two rollbacks: + // 1. Set regs.eip to point at the actual address of the instruction we breaked on. + // regs.eip currently points to one byte after the address of the original instruction, + // because the cpu has just executed the INT3 we patched into the instruction. + // 2. We restore the original first byte of the instruction, + // because it was patched with INT3. + regs.eip = reinterpret_cast<u32>(current_breakpoint.value().address); + set_registers(regs); + disable_breakpoint(current_breakpoint.value().address); + } + + DebugBreakReason reason = (state == State::Syscall && !current_breakpoint.has_value()) ? DebugBreakReason::Syscall : DebugBreakReason::Breakpoint; + + DebugDecision decision = callback(reason, regs); + + if (reason == DebugBreakReason::Syscall) { + // skip the exit from the syscall + if (do_continue_and_wait()) + break; + } + + if (decision == DebugDecision::Continue) { + state = State::FreeRun; + } else if (decision == DebugDecision::ContinueBreakAtSyscall) { + state = State::Syscall; + } + + bool did_single_step = false; + + // Re-enable the breakpoint if it wasn't removed by the user + if (current_breakpoint.has_value() && m_breakpoints.contains(current_breakpoint.value().address)) { + // The current breakpoint was removed to make it transparent to the user. + // We now want to re-enable it - the code execution flow could hit it again. + // To re-enable the breakpoint, we first perform a single step and execute the + // instruction of the breakpoint, and then redo the INT3 patch in its first byte. + + // If the user manually inserted a breakpoint at were we breaked at originally, + // we need to disable that breakpoint because we want to singlestep over it to execute the + // instruction we breaked on (we re-enable it again later anyways). + if (m_breakpoints.contains(current_breakpoint.value().address) && m_breakpoints.get(current_breakpoint.value().address).value().state == BreakPointState::Enabled) { + disable_breakpoint(current_breakpoint.value().address); + } + auto stopped_address = single_step(); + enable_breakpoint(current_breakpoint.value().address); + did_single_step = true; + // If there is another breakpoint after the current one, + // Then we are already on it (because of single_step) + auto breakpoint_at_next_instruction = m_breakpoints.get(stopped_address); + if (breakpoint_at_next_instruction.has_value() + && breakpoint_at_next_instruction.value().state == BreakPointState::Enabled) { + state = State::ConsecutiveBreakpoint; + } + } + + if (decision == DebugDecision::SingleStep) { + state = State::SingleStep; + } + + if (decision == DebugDecision::Detach) { + detach(); + break; + } + if (decision == DebugDecision::Kill) { + ASSERT_NOT_REACHED(); // TODO: implement + } + + if (state == State::SingleStep && !did_single_step) { + single_step(); + } + } +} + +} diff --git a/Userland/Libraries/LibDebug/Dwarf/AbbreviationsMap.cpp b/Userland/Libraries/LibDebug/Dwarf/AbbreviationsMap.cpp new file mode 100644 index 0000000000..865cf56943 --- /dev/null +++ b/Userland/Libraries/LibDebug/Dwarf/AbbreviationsMap.cpp @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "AbbreviationsMap.h" +#include "DwarfInfo.h" + +#include <AK/MemoryStream.h> + +namespace Debug::Dwarf { + +AbbreviationsMap::AbbreviationsMap(const DwarfInfo& dwarf_info, u32 offset) + : m_dwarf_info(dwarf_info) + , m_offset(offset) +{ + populate_map(); +} + +void AbbreviationsMap::populate_map() +{ + InputMemoryStream abbreviation_stream(m_dwarf_info.abbreviation_data()); + abbreviation_stream.discard_or_error(m_offset); + + while (!abbreviation_stream.eof()) { + size_t abbreviation_code = 0; + abbreviation_stream.read_LEB128_unsigned(abbreviation_code); + // An abbreviation code of 0 marks the end of the + // abbreviations for a given compilation unit + if (abbreviation_code == 0) + break; + + size_t tag {}; + abbreviation_stream.read_LEB128_unsigned(tag); + + u8 has_children = 0; + abbreviation_stream >> has_children; + + AbbreviationEntry abbrevation_entry {}; + abbrevation_entry.tag = static_cast<EntryTag>(tag); + abbrevation_entry.has_children = (has_children == 1); + + AttributeSpecification current_attribute_specification {}; + do { + size_t attribute_value = 0; + size_t form_value = 0; + abbreviation_stream.read_LEB128_unsigned(attribute_value); + abbreviation_stream.read_LEB128_unsigned(form_value); + + current_attribute_specification.attribute = static_cast<Attribute>(attribute_value); + current_attribute_specification.form = static_cast<AttributeDataForm>(form_value); + + if (current_attribute_specification.attribute != Attribute::None) { + abbrevation_entry.attribute_specifications.append(current_attribute_specification); + } + } while (current_attribute_specification.attribute != Attribute::None || current_attribute_specification.form != AttributeDataForm::None); + + m_entries.set((u32)abbreviation_code, move(abbrevation_entry)); + } +} + +Optional<AbbreviationsMap::AbbreviationEntry> AbbreviationsMap::get(u32 code) const +{ + return m_entries.get(code); +} + +} diff --git a/Userland/Libraries/LibDebug/Dwarf/AbbreviationsMap.h b/Userland/Libraries/LibDebug/Dwarf/AbbreviationsMap.h new file mode 100644 index 0000000000..ce5da7408a --- /dev/null +++ b/Userland/Libraries/LibDebug/Dwarf/AbbreviationsMap.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "DwarfTypes.h" +#include <AK/HashMap.h> +#include <AK/Optional.h> +#include <AK/Types.h> + +namespace Debug::Dwarf { + +class DwarfInfo; + +class AbbreviationsMap { +public: + AbbreviationsMap(const DwarfInfo& dwarf_info, u32 offset); + + struct AbbreviationEntry { + + EntryTag tag; + bool has_children; + + Vector<AttributeSpecification> attribute_specifications; + }; + + Optional<AbbreviationEntry> get(u32 code) const; + +private: + void populate_map(); + + const DwarfInfo& m_dwarf_info; + u32 m_offset { 0 }; + HashMap<u32, AbbreviationEntry> m_entries; +}; + +} diff --git a/Userland/Libraries/LibDebug/Dwarf/CompilationUnit.cpp b/Userland/Libraries/LibDebug/Dwarf/CompilationUnit.cpp new file mode 100644 index 0000000000..664d68e957 --- /dev/null +++ b/Userland/Libraries/LibDebug/Dwarf/CompilationUnit.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "CompilationUnit.h" +#include "DIE.h" + +namespace Debug::Dwarf { + +CompilationUnit::CompilationUnit(const DwarfInfo& dwarf_info, u32 offset, const CompilationUnitHeader& header) + : m_dwarf_info(dwarf_info) + , m_offset(offset) + , m_header(header) + , m_abbreviations(dwarf_info, header.abbrev_offset) +{ +} + +DIE CompilationUnit::root_die() const +{ + return DIE(*this, m_offset + sizeof(CompilationUnitHeader)); +} + +} diff --git a/Userland/Libraries/LibDebug/Dwarf/CompilationUnit.h b/Userland/Libraries/LibDebug/Dwarf/CompilationUnit.h new file mode 100644 index 0000000000..811ce3bc37 --- /dev/null +++ b/Userland/Libraries/LibDebug/Dwarf/CompilationUnit.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "AbbreviationsMap.h" +#include <AK/Types.h> + +namespace Debug::Dwarf { + +class DwarfInfo; +class DIE; + +class CompilationUnit { +public: + CompilationUnit(const DwarfInfo& dwarf_info, u32 offset, const CompilationUnitHeader&); + + u32 offset() const { return m_offset; } + u32 size() const { return m_header.length + sizeof(u32); } + + DIE root_die() const; + + const DwarfInfo& dwarf_info() const { return m_dwarf_info; } + const AbbreviationsMap& abbreviations_map() const { return m_abbreviations; } + +private: + const DwarfInfo& m_dwarf_info; + u32 m_offset { 0 }; + CompilationUnitHeader m_header; + AbbreviationsMap m_abbreviations; +}; + +} diff --git a/Userland/Libraries/LibDebug/Dwarf/DIE.cpp b/Userland/Libraries/LibDebug/Dwarf/DIE.cpp new file mode 100644 index 0000000000..96ad0875d6 --- /dev/null +++ b/Userland/Libraries/LibDebug/Dwarf/DIE.cpp @@ -0,0 +1,244 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "DIE.h" +#include "CompilationUnit.h" +#include "DwarfInfo.h" +#include <AK/ByteBuffer.h> +#include <AK/MemoryStream.h> + +namespace Debug::Dwarf { + +DIE::DIE(const CompilationUnit& unit, u32 offset) + : m_compilation_unit(unit) + , m_offset(offset) +{ + InputMemoryStream stream(m_compilation_unit.dwarf_info().debug_info_data()); + stream.discard_or_error(m_offset); + stream.read_LEB128_unsigned(m_abbreviation_code); + m_data_offset = stream.offset(); + + if (m_abbreviation_code == 0) { + // An abbreviation code of 0 ( = null DIE entry) means the end of a chain of siblings + m_tag = EntryTag::None; + } else { + auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code); + ASSERT(abbreviation_info.has_value()); + + m_tag = abbreviation_info.value().tag; + m_has_children = abbreviation_info.value().has_children; + + // We iterate the attributes data only to calculate this DIE's size + for (auto& attribute_spec : abbreviation_info.value().attribute_specifications) { + get_attribute_value(attribute_spec.form, stream); + } + } + m_size = stream.offset() - m_offset; +} + +DIE::AttributeValue DIE::get_attribute_value(AttributeDataForm form, + InputMemoryStream& debug_info_stream) const +{ + AttributeValue value; + + auto assign_raw_bytes_value = [&](size_t length) { + value.data.as_raw_bytes.length = length; + value.data.as_raw_bytes.bytes = reinterpret_cast<const u8*>(m_compilation_unit.dwarf_info().debug_info_data().data() + + debug_info_stream.offset()); + + debug_info_stream.discard_or_error(length); + }; + + switch (form) { + case AttributeDataForm::StringPointer: { + u32 offset; + debug_info_stream >> offset; + value.type = AttributeValue::Type::String; + + auto strings_data = m_compilation_unit.dwarf_info().debug_strings_data(); + value.data.as_string = reinterpret_cast<const char*>(strings_data.data() + offset); + break; + } + case AttributeDataForm::Data1: { + u8 data; + debug_info_stream >> data; + value.type = AttributeValue::Type::UnsignedNumber; + value.data.as_u32 = data; + break; + } + case AttributeDataForm::Data2: { + u16 data; + debug_info_stream >> data; + value.type = AttributeValue::Type::UnsignedNumber; + value.data.as_u32 = data; + break; + } + case AttributeDataForm::Addr: { + u32 address; + debug_info_stream >> address; + value.type = AttributeValue::Type::UnsignedNumber; + value.data.as_u32 = address; + break; + } + case AttributeDataForm::SData: { + ssize_t data; + debug_info_stream.read_LEB128_signed(data); + value.type = AttributeValue::Type::SignedNumber; + value.data.as_i32 = data; + break; + } + case AttributeDataForm::SecOffset: { + u32 data; + debug_info_stream >> data; + value.type = AttributeValue::Type::SecOffset; + value.data.as_u32 = data; + break; + } + case AttributeDataForm::Data4: { + u32 data; + debug_info_stream >> data; + value.type = AttributeValue::Type::UnsignedNumber; + value.data.as_u32 = data; + break; + } + case AttributeDataForm::Ref4: { + u32 data; + debug_info_stream >> data; + value.type = AttributeValue::Type::DieReference; + value.data.as_u32 = data + m_compilation_unit.offset(); + break; + } + case AttributeDataForm::FlagPresent: { + value.type = AttributeValue::Type::Boolean; + value.data.as_bool = true; + break; + } + case AttributeDataForm::ExprLoc: { + size_t length; + debug_info_stream.read_LEB128_unsigned(length); + value.type = AttributeValue::Type::DwarfExpression; + assign_raw_bytes_value(length); + break; + } + case AttributeDataForm::String: { + String str; + u32 str_offset = debug_info_stream.offset(); + debug_info_stream >> str; + value.type = AttributeValue::Type::String; + value.data.as_string = reinterpret_cast<const char*>(str_offset + m_compilation_unit.dwarf_info().debug_info_data().data()); + break; + } + case AttributeDataForm::Block1: { + value.type = AttributeValue::Type::RawBytes; + u8 length; + debug_info_stream >> length; + assign_raw_bytes_value(length); + break; + } + case AttributeDataForm::Block2: { + value.type = AttributeValue::Type::RawBytes; + u16 length; + debug_info_stream >> length; + assign_raw_bytes_value(length); + break; + } + case AttributeDataForm::Block4: { + value.type = AttributeValue::Type::RawBytes; + u32 length; + debug_info_stream >> length; + assign_raw_bytes_value(length); + break; + } + case AttributeDataForm::Block: { + value.type = AttributeValue::Type::RawBytes; + size_t length; + debug_info_stream.read_LEB128_unsigned(length); + assign_raw_bytes_value(length); + break; + } + default: + dbgln("Unimplemented AttributeDataForm: {}", (u32)form); + ASSERT_NOT_REACHED(); + } + return value; +} + +Optional<DIE::AttributeValue> DIE::get_attribute(const Attribute& attribute) const +{ + InputMemoryStream stream { m_compilation_unit.dwarf_info().debug_info_data() }; + stream.discard_or_error(m_data_offset); + + auto abbreviation_info = m_compilation_unit.abbreviations_map().get(m_abbreviation_code); + ASSERT(abbreviation_info.has_value()); + + for (const auto& attribute_spec : abbreviation_info.value().attribute_specifications) { + auto value = get_attribute_value(attribute_spec.form, stream); + if (attribute_spec.attribute == attribute) { + return value; + } + } + return {}; +} + +void DIE::for_each_child(Function<void(const DIE& child)> callback) const +{ + if (!m_has_children) + return; + + NonnullOwnPtr<DIE> current_child = make<DIE>(m_compilation_unit, m_offset + m_size); + while (true) { + callback(*current_child); + if (current_child->is_null()) + break; + if (!current_child->has_children()) { + current_child = make<DIE>(m_compilation_unit, current_child->offset() + current_child->size()); + continue; + } + + auto sibling = current_child->get_attribute(Attribute::Sibling); + u32 sibling_offset = 0; + if (sibling.has_value()) { + sibling_offset = sibling.value().data.as_u32; + } + + if (!sibling.has_value()) { + // 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([&](const DIE& sub_child) { + sibling_offset = sub_child.offset() + sub_child.size(); + }); + } + current_child = make<DIE>(m_compilation_unit, sibling_offset); + } +} + +DIE DIE::get_die_at_offset(u32 offset) const +{ + ASSERT(offset >= m_compilation_unit.offset() && offset < m_compilation_unit.offset() + m_compilation_unit.size()); + return DIE(m_compilation_unit, offset); +} + +} diff --git a/Userland/Libraries/LibDebug/Dwarf/DIE.h b/Userland/Libraries/LibDebug/Dwarf/DIE.h new file mode 100644 index 0000000000..3ba68eafa8 --- /dev/null +++ b/Userland/Libraries/LibDebug/Dwarf/DIE.h @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "CompilationUnit.h" +#include "DwarfTypes.h" +#include <AK/Function.h> +#include <AK/NonnullOwnPtr.h> +#include <AK/Optional.h> +#include <AK/Types.h> + +namespace Debug::Dwarf { + +class CompilationUnit; + +// DIE = Debugging Information Entry +class DIE { +public: + DIE(const CompilationUnit&, u32 offset); + + struct AttributeValue { + enum class Type : u8 { + UnsignedNumber, + SignedNumber, + String, + DieReference, // Reference to another DIE in the same compilation unit + Boolean, + DwarfExpression, + SecOffset, + RawBytes, + } type; + + union { + u32 as_u32; + i32 as_i32; + const char* as_string; // points to bytes in the memory mapped elf image + bool as_bool; + struct { + u32 length; + const u8* bytes; // points to bytes in the memory mapped elf image + } as_raw_bytes; + } data {}; + }; + + u32 offset() const { return m_offset; } + u32 size() const { return m_size; } + bool has_children() const { return m_has_children; } + EntryTag tag() const { return m_tag; } + + Optional<AttributeValue> get_attribute(const Attribute&) const; + + void for_each_child(Function<void(const DIE& child)> callback) const; + + bool is_null() const { return m_tag == EntryTag::None; } + + DIE get_die_at_offset(u32 offset) const; + +private: + AttributeValue get_attribute_value(AttributeDataForm form, + InputMemoryStream& debug_info_stream) const; + + const CompilationUnit& m_compilation_unit; + u32 m_offset { 0 }; + u32 m_data_offset { 0 }; + size_t m_abbreviation_code { 0 }; + EntryTag m_tag { EntryTag::None }; + bool m_has_children { false }; + u32 m_size { 0 }; +}; + +} diff --git a/Userland/Libraries/LibDebug/Dwarf/DwarfInfo.cpp b/Userland/Libraries/LibDebug/Dwarf/DwarfInfo.cpp new file mode 100644 index 0000000000..09717cfc71 --- /dev/null +++ b/Userland/Libraries/LibDebug/Dwarf/DwarfInfo.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "DwarfInfo.h" + +#include <AK/MemoryStream.h> + +namespace Debug::Dwarf { + +DwarfInfo::DwarfInfo(const ELF::Image& elf) + : m_elf(elf) +{ + m_debug_info_data = section_data(".debug_info"); + m_abbreviation_data = section_data(".debug_abbrev"); + m_debug_strings_data = section_data(".debug_str"); + + populate_compilation_units(); +} + +ReadonlyBytes DwarfInfo::section_data(const String& section_name) const +{ + auto section = m_elf.lookup_section(section_name); + if (section.is_undefined()) + return {}; + return section.bytes(); +} + +void DwarfInfo::populate_compilation_units() +{ + if (!m_debug_info_data.data()) + return; + + InputMemoryStream stream { m_debug_info_data }; + while (!stream.eof()) { + auto unit_offset = stream.offset(); + CompilationUnitHeader compilation_unit_header {}; + + stream >> Bytes { &compilation_unit_header, sizeof(compilation_unit_header) }; + ASSERT(compilation_unit_header.address_size == sizeof(u32)); + ASSERT(compilation_unit_header.version <= 4); + + u32 length_after_header = compilation_unit_header.length - (sizeof(CompilationUnitHeader) - offsetof(CompilationUnitHeader, version)); + m_compilation_units.empend(*this, unit_offset, compilation_unit_header); + stream.discard_or_error(length_after_header); + } +} + +} diff --git a/Userland/Libraries/LibDebug/Dwarf/DwarfInfo.h b/Userland/Libraries/LibDebug/Dwarf/DwarfInfo.h new file mode 100644 index 0000000000..59496e0e3b --- /dev/null +++ b/Userland/Libraries/LibDebug/Dwarf/DwarfInfo.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "CompilationUnit.h" +#include "DwarfTypes.h" +#include <AK/ByteBuffer.h> +#include <AK/NonnullRefPtr.h> +#include <AK/RefCounted.h> +#include <AK/String.h> +#include <LibELF/Image.h> + +namespace Debug::Dwarf { + +class DwarfInfo { +public: + explicit DwarfInfo(const ELF::Image&); + + ReadonlyBytes debug_info_data() const { return m_debug_info_data; } + ReadonlyBytes abbreviation_data() const { return m_abbreviation_data; } + ReadonlyBytes debug_strings_data() const { return m_debug_strings_data; } + + template<typename Callback> + void for_each_compilation_unit(Callback) const; + +private: + void populate_compilation_units(); + + ReadonlyBytes section_data(const String& section_name) const; + + const ELF::Image& m_elf; + ReadonlyBytes m_debug_info_data; + ReadonlyBytes m_abbreviation_data; + ReadonlyBytes m_debug_strings_data; + + Vector<Dwarf::CompilationUnit> m_compilation_units; +}; + +template<typename Callback> +void DwarfInfo::for_each_compilation_unit(Callback callback) const +{ + for (const auto& unit : m_compilation_units) { + callback(unit); + } +} + +} diff --git a/Userland/Libraries/LibDebug/Dwarf/DwarfTypes.h b/Userland/Libraries/LibDebug/Dwarf/DwarfTypes.h new file mode 100644 index 0000000000..7bda8f971a --- /dev/null +++ b/Userland/Libraries/LibDebug/Dwarf/DwarfTypes.h @@ -0,0 +1,294 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include <AK/Types.h> + +namespace Debug::Dwarf { + +struct [[gnu::packed]] CompilationUnitHeader { + u32 length; + u16 version; + u32 abbrev_offset; + u8 address_size; +}; + +enum class EntryTag : u16 { + None = 0, + ArrayType = 0x1, + ClassType = 0x2, + EntryPoint = 0x3, + EnumerationType = 0x4, + FormalParameter = 0x5, + ImportedDeclaration = 0x8, + Label = 0xa, + LexicalBlock = 0xb, + Member = 0xd, + PointerType = 0xf, + ReferenceType = 0x10, + CompileUnit = 0x11, + StringType = 0x12, + StructureType = 0x13, + SubroutineType = 0x15, + TypeDef = 0x16, + UnionType = 0x17, + UnspecifiedParameters = 0x18, + Variant = 0x19, + CommonBlock = 0x1a, + CommonInclusion = 0x1b, + Inheritance = 0x1c, + InlinedSubroutine = 0x1d, + Module = 0x1e, + PtrToMemberType = 0x1f, + SetType = 0x20, + SubRangeType = 0x21, + WithStmt = 0x22, + AccessDeclaration = 0x23, + BaseType = 0x24, + CatchBlock = 0x25, + ConstType = 0x26, + Constant = 0x27, + Enumerator = 0x28, + FileType = 0x29, + Friend = 0x2a, + NameList = 0x2b, + NameListItem = 0x2c, + PackedType = 0x2d, + SubProgram = 0x2e, + TemplateTypeParam = 0x2f, + TemplateValueParam = 0x30, + ThrownType = 0x31, + TryBlock = 0x32, + VariantPart = 0x33, + Variable = 0x34, + VolatileType = 0x35, + DwarfProcedure = 0x36, + RestrictType = 0x37, + InterfaceType = 0x38, + Namespace = 0x39, + ImportedModule = 0x3a, + UnspecifiedType = 0x3b, + PartialUnit = 0x3c, + ImportedUnit = 0x3d, + MutableType = 0x3e, + Condition = 0x3f, + SharedTyped = 0x40, + TypeUnit = 0x41, + RValueReferenceType = 0x42, + TemplateAlias = 0x43, + CoArrayType = 0x44, + GenericSubRange = 0x45, + DynamicType = 0x46, + AtomicType = 0x47, + CallSite = 0x48, + CallSiteParameter = 0x49, + SkeletonUnit = 0x4a, + ImmutableType = 0x4b, + LoUser = 0x4080, + HiUser = 0xffff, +}; + +enum class Attribute : u16 { + None = 0, + Sibling = 0x1, + Location = 0x2, + Name = 0x3, + Ordering = 0x9, + ByteSize = 0xb, + BitOffset = 0xc, + BitSize = 0xd, + StmtList = 0x10, + LowPc = 0x11, + HighPc = 0x12, + Language = 0x13, + Discr = 0x15, + DiscrValue = 0x16, + Visibility = 0x17, + Import = 0x18, + StringLength = 0x19, + CommonReference = 0x1a, + CompDir = 0x1b, + ConstValue = 0x1c, + ContainingType = 0x1d, + DefaultValue = 0x1e, + Inline = 0x20, + IsOptional = 0x21, + LowerBound = 0x22, + Producer = 0x25, + Prototyped = 0x27, + ReturnAddr = 0x2a, + StartScope = 0x2c, + BitStride = 0x2e, + UpperBound = 0x2f, + AbstractOrigin = 0x31, + Accessibility = 0x32, + AddressClass = 0x33, + Artificial = 0x34, + BaseTypes = 0x35, + CallingConvention = 0x36, + Count = 0x37, + MemberLocation = 0x38, + DeclColumn = 0x39, + DeclFile = 0x3a, + DeclLine = 0x3b, + Declaration = 0x3c, + DiscrList = 0x3d, + Encoding = 0x3e, + External = 0x3f, + FrameBase = 0x40, + Friend = 0x41, + IdentifierCase = 0x43, + MacroInfo = 0x43, + NameListItem = 0x44, + Priority = 0x45, + Segment = 0x46, + Specification = 0x47, + StaticLink = 0x48, + Type = 0x49, + UseLocation = 0x4a, + VariableParameter = 0x4b, + Virtuality = 0x4c, + VtableElemLocation = 0x4d, + Allocated = 0x4e, + Associated = 0x4f, + DataLocation = 0x50, + ByteStride = 0x51, + EntryPC = 0x52, + UseUTF8 = 0x53, + Extension = 0x54, + Ranges = 0x55, + Trampoline = 0x56, + CallColumn = 0x57, + CallFile = 0x58, + CallLine = 0x59, + Description = 0x5a, + BinaryScale = 0x5b, + DecimalScale = 0x5c, + Small = 0x5d, + DecimalSign = 0x5e, + DigitCount = 0x5f, + PictureString = 0x60, + Mutable = 0x61, + ThreadsScaled = 0x62, + Explicit = 0x63, + ObjectPointer = 0x64, + Endianity = 0x65, + Elemental = 0x66, + Pure = 0x67, + Recursive = 0x68, + Signature = 0x69, + MainSubprogram = 0x6a, + DataBitOffset = 0x6b, + ConstExpr = 0x6c, + EnumClass = 0x6d, + LinkageName = 0x6e, + StringLengthBitSize = 0x6f, + StringLengthByteSize = 0x70, + Rank = 0x71, + StrOffsetsBase = 0x72, + AddrBase = 0x73, + RngListsBase = 0x74, + DWOName = 0x76, + Reference = 0x77, + RValueReference = 0x78, + Macros = 0x79, + CallAllCalls = 0x7a, + CallAllSourceCalls = 0x7b, + CallAllTailCalls = 0x7c, + CallReturnPC = 0x7d, + CallValue = 0x7e, + CallOrigin = 0x7f, + CallParameter = 0x80, + CallPC = 0x81, + CallTailCall = 0x82, + CallTarget = 0x83, + CallTargetClobbered = 0x84, + CallDataLocation = 0x85, + CallDataValue = 0x86, + NoReturn = 0x87, + Alignment = 0x88, + ExportSymbols = 0x89, + Deleted = 0x8a, + Defaulted = 0x8b, + LocListsBase = 0x8c, + LoUser = 0x2000, + HiUser = 0x3fff, +}; + +enum class AttributeDataForm : u8 { + None = 0, + Addr = 0x1, + Block2 = 0x3, + Block4 = 0x4, + Data2 = 0x5, + Data4 = 0x6, + Data8 = 0x7, + String = 0x8, + Block = 0x9, + Block1 = 0xa, + Data1 = 0xb, + Flag = 0xc, + SData = 0xd, + StringPointer = 0xe, + UData = 0xf, + RefAddr = 0x10, + Ref1 = 0x11, + Ref2 = 0x12, + Ref4 = 0x13, + Ref8 = 0x14, + RefUData = 0x15, + Indirect = 0x16, + SecOffset = 0x17, + ExprLoc = 0x18, + FlagPresent = 0x19, + StrX = 0x1a, + AddrX = 0x1b, + RefSup4 = 0x1c, + StrPSup = 0x1d, + Data16 = 0x1e, + LineStrP = 0x1f, + RefSig8 = 0x20, + ImplicitConst = 0x21, + LocListX = 0x22, + RngListX = 0x23, + RefSup8 = 0x24, + StrX1 = 0x25, + StrX2 = 0x26, + StrX3 = 0x27, + StrX4 = 0x28, + AddrX1 = 0x29, + AddrX2 = 0x2a, + AddrX3 = 0x2b, + AddrX4 = 0x2c +}; + +struct [[gnu::packed]] AttributeSpecification { + Attribute attribute; + AttributeDataForm form; +}; + +} diff --git a/Userland/Libraries/LibDebug/Dwarf/Expression.cpp b/Userland/Libraries/LibDebug/Dwarf/Expression.cpp new file mode 100644 index 0000000000..fe50d1aed3 --- /dev/null +++ b/Userland/Libraries/LibDebug/Dwarf/Expression.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "Expression.h" + +#include <AK/MemoryStream.h> + +#include <sys/arch/i386/regs.h> + +namespace Debug::Dwarf::Expression { + +Value evaluate(ReadonlyBytes bytes, const PtraceRegisters& regs) +{ + InputMemoryStream stream(bytes); + + while (!stream.eof()) { + u8 opcode = 0; + stream >> opcode; + + switch (static_cast<Operations>(opcode)) { + case Operations::RegEbp: { + ssize_t offset = 0; + stream.read_LEB128_signed(offset); + return Value { Type::UnsignedIntetger, regs.ebp + offset }; + } + + case Operations::FbReg: { + ssize_t offset = 0; + stream.read_LEB128_signed(offset); + return Value { Type::UnsignedIntetger, regs.ebp + 2 * sizeof(size_t) + offset }; + } + + default: + dbgln("DWARF expr addr: {}", (const void*)bytes.data()); + dbgln("unsupported opcode: {}", (u8)opcode); + ASSERT_NOT_REACHED(); + } + } + ASSERT_NOT_REACHED(); +} + +} diff --git a/Userland/Libraries/LibDebug/Dwarf/Expression.h b/Userland/Libraries/LibDebug/Dwarf/Expression.h new file mode 100644 index 0000000000..ef13830353 --- /dev/null +++ b/Userland/Libraries/LibDebug/Dwarf/Expression.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "AK/ByteBuffer.h" +#include "AK/Types.h" + +struct PtraceRegisters; + +namespace Debug::Dwarf::Expression { + +enum class Type { + None, + UnsignedIntetger, + Register, +}; + +struct Value { + Type type; + union { + u32 as_u32; + } data { 0 }; +}; + +enum class Operations : u8 { + RegEbp = 0x75, + FbReg = 0x91, +}; + +Value evaluate(ReadonlyBytes, const PtraceRegisters&); + +} diff --git a/Userland/Libraries/LibDebug/Dwarf/LineProgram.cpp b/Userland/Libraries/LibDebug/Dwarf/LineProgram.cpp new file mode 100644 index 0000000000..2bc89a885f --- /dev/null +++ b/Userland/Libraries/LibDebug/Dwarf/LineProgram.cpp @@ -0,0 +1,269 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "LineProgram.h" +#include <AK/String.h> +#include <AK/StringBuilder.h> + +//#define DWARF_DEBUG + +namespace Debug::Dwarf { + +LineProgram::LineProgram(InputMemoryStream& stream) + : m_stream(stream) +{ + m_unit_offset = m_stream.offset(); + parse_unit_header(); + parse_source_directories(); + parse_source_files(); + run_program(); +} + +void LineProgram::parse_unit_header() +{ + m_stream >> Bytes { &m_unit_header, sizeof(m_unit_header) }; + + ASSERT(m_unit_header.version == DWARF_VERSION); + ASSERT(m_unit_header.opcode_base == SPECIAL_OPCODES_BASE); + +#ifdef DWARF_DEBUG + dbgln("unit length: {}", m_unit_header.length); +#endif +} + +void LineProgram::parse_source_directories() +{ + m_source_directories.append("."); + + while (m_stream.peek_or_error()) { + String directory; + m_stream >> directory; +#ifdef DWARF_DEBUG + dbgln("directory: {}", directory); +#endif + m_source_directories.append(move(directory)); + } + m_stream.handle_recoverable_error(); + m_stream.discard_or_error(1); + ASSERT(!m_stream.has_any_error()); +} + +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 +#ifdef DWARF_DEBUG + dbgln("file: {}, directory index: {}", file_name, directory_index); +#endif + m_source_files.append({ file_name, directory_index }); + } + m_stream.discard_or_error(1); + ASSERT(!m_stream.has_any_error()); +} + +void LineProgram::append_to_line_info() +{ +#ifdef DWARF_DEBUG + dbgln("appending line info: {:p}, {}:{}", m_address, m_source_files[m_file_index].name, m_line); +#endif + if (!m_is_statement) + return; + + String directory = m_source_directories[m_source_files[m_file_index].directory_index]; + + StringBuilder full_path(directory.length() + m_source_files[m_file_index].name.length() + 1); + full_path.append(directory); + full_path.append('/'); + full_path.append(m_source_files[m_file_index].name); + + m_lines.append({ m_address, full_path.to_string(), m_line }); +} + +void LineProgram::reset_registers() +{ + m_address = 0; + m_line = 1; + m_file_index = 1; + m_is_statement = m_unit_header.default_is_stmt == 1; +} + +void LineProgram::handle_extended_opcode() +{ + size_t length = 0; + m_stream.read_LEB128_unsigned(length); + + u8 sub_opcode = 0; + m_stream >> sub_opcode; + + switch (sub_opcode) { + case ExtendedOpcodes::EndSequence: { + append_to_line_info(); + reset_registers(); + break; + } + case ExtendedOpcodes::SetAddress: { + ASSERT(length == sizeof(size_t) + 1); + m_stream >> m_address; +#ifdef DWARF_DEBUG + dbgln("SetAddress: {:p}", m_address); +#endif + break; + } + case ExtendedOpcodes::SetDiscriminator: { +#ifdef DWARF_DEBUG + dbgln("SetDiscriminator"); +#endif + m_stream.discard_or_error(1); + break; + } + default: +#ifdef DWARF_DEBUG + dbgln("offset: {:p}", m_stream.offset()); +#endif + ASSERT_NOT_REACHED(); + } +} +void LineProgram::handle_standard_opcode(u8 opcode) +{ + switch (opcode) { + case StandardOpcodes::Copy: { + append_to_line_info(); + break; + } + case StandardOpcodes::AdvancePc: { + size_t operand = 0; + m_stream.read_LEB128_unsigned(operand); + size_t delta = operand * m_unit_header.min_instruction_length; +#ifdef DWARF_DEBUG + dbgln("AdvancePC by: {} to: {:p}", delta, m_address + delta); +#endif + m_address += delta; + break; + } + case StandardOpcodes::SetFile: { + size_t new_file_index = 0; + m_stream.read_LEB128_unsigned(new_file_index); +#ifdef DWARF_DEBUG + dbgln("SetFile: new file index: {}", new_file_index); +#endif + m_file_index = new_file_index; + break; + } + case StandardOpcodes::SetColumn: { + // not implemented +#ifdef DWARF_DEBUG + dbgln("SetColumn"); +#endif + size_t new_column; + m_stream.read_LEB128_unsigned(new_column); + + break; + } + case StandardOpcodes::AdvanceLine: { + ssize_t line_delta; + m_stream.read_LEB128_signed(line_delta); + ASSERT(line_delta >= 0 || m_line >= (size_t)(-line_delta)); + m_line += line_delta; +#ifdef DWARF_DEBUG + dbgln("AdvanceLine: {}", m_line); +#endif + break; + } + case StandardOpcodes::NegateStatement: { +#ifdef DWARF_DEBUG + dbgln("NegateStatement"); +#endif + m_is_statement = !m_is_statement; + 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; +#ifdef DWARF_DEBUG + dbgln("ConstAddPc: advance pc by: {} to: {}", address_increment, (m_address + address_increment)); +#endif + m_address += address_increment; + break; + } + case StandardOpcodes::SetIsa: { + size_t isa; + m_stream.read_LEB128_unsigned(isa); + dbgln("SetIsa: {}", isa); + break; + } + default: + dbgln("Unhandled LineProgram opcode {}", opcode); + ASSERT_NOT_REACHED(); + } +} +void LineProgram::handle_sepcial_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); + + m_address += address_increment; + m_line += line_increment; + +#ifdef DWARF_DEBUG + dbgln("Special adjusted_opcode: {}, address_increment: {}, line_increment: {}", adjusted_opcode, address_increment, line_increment); + dbg() << "Address is now:" << (void*)m_address << ", and line is: " << m_source_files[m_file_index].name << ":" << m_line; +#endif + + append_to_line_info(); +} + +void LineProgram::run_program() +{ + reset_registers(); + + while ((size_t)m_stream.offset() < m_unit_offset + sizeof(u32) + m_unit_header.length) { + u8 opcode = 0; + m_stream >> opcode; + +#ifdef DWARF_DEBUG + dbg() << (void*)(m_stream.offset() - 1) << ": opcode: " << opcode; +#endif + + if (opcode == 0) { + handle_extended_opcode(); + } else if (opcode >= 1 && opcode <= 12) { + handle_standard_opcode(opcode); + } else { + handle_sepcial_opcode(opcode); + } + } +} + +} diff --git a/Userland/Libraries/LibDebug/Dwarf/LineProgram.h b/Userland/Libraries/LibDebug/Dwarf/LineProgram.h new file mode 100644 index 0000000000..5599cae555 --- /dev/null +++ b/Userland/Libraries/LibDebug/Dwarf/LineProgram.h @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include <AK/FlyString.h> +#include <AK/MemoryStream.h> +#include <AK/Vector.h> + +namespace Debug::Dwarf { + +class LineProgram { +public: + explicit LineProgram(InputMemoryStream& stream); + + struct LineInfo { + u32 address { 0 }; + FlyString file; + size_t line { 0 }; + }; + + const Vector<LineInfo>& lines() const { return m_lines; } + +private: + void parse_unit_header(); + void parse_source_directories(); + void parse_source_files(); + void run_program(); + + void append_to_line_info(); + void reset_registers(); + + void handle_extended_opcode(); + void handle_standard_opcode(u8 opcode); + void handle_sepcial_opcode(u8 opcode); + + struct [[gnu::packed]] UnitHeader32 { + u32 length; + u16 version; + u32 header_length; + u8 min_instruction_length; + u8 default_is_stmt; + i8 line_base; + u8 line_range; + u8 opcode_base; + u8 std_opcode_lengths[12]; + }; + + enum StandardOpcodes { + Copy = 1, + AdvancePc, + AdvanceLine, + SetFile, + SetColumn, + NegateStatement, + SetBasicBlock, + ConstAddPc, + FixAdvancePc, + SetProlougeEnd, + SetEpilogueBegin, + SetIsa + }; + + enum ExtendedOpcodes { + EndSequence = 1, + SetAddress, + DefineFile, + SetDiscriminator, + }; + + struct FileEntry { + FlyString name; + size_t directory_index { 0 }; + }; + + static constexpr u16 DWARF_VERSION = 3; + static constexpr u8 SPECIAL_OPCODES_BASE = 13; + + InputMemoryStream& m_stream; + + size_t m_unit_offset { 0 }; + UnitHeader32 m_unit_header {}; + Vector<String> m_source_directories; + Vector<FileEntry> m_source_files; + + // The registers of the "line program" virtual machine + u32 m_address { 0 }; + size_t m_line { 0 }; + size_t m_file_index { 0 }; + bool m_is_statement { false }; + + Vector<LineInfo> m_lines; +}; + +} diff --git a/Userland/Libraries/LibDebug/StackFrameUtils.cpp b/Userland/Libraries/LibDebug/StackFrameUtils.cpp new file mode 100644 index 0000000000..6b68237924 --- /dev/null +++ b/Userland/Libraries/LibDebug/StackFrameUtils.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "StackFrameUtils.h" + +namespace Debug::StackFrameUtils { + +Optional<StackFrameInfo> get_info(const DebugSession& session, FlatPtr current_ebp) +{ + auto return_address = session.peek(reinterpret_cast<u32*>(current_ebp + sizeof(FlatPtr))); + auto next_ebp = session.peek(reinterpret_cast<u32*>(current_ebp)); + if (!return_address.has_value() || !next_ebp.has_value()) + return {}; + + StackFrameInfo info = { return_address.value(), next_ebp.value() }; + return info; +} + +} diff --git a/Userland/Libraries/LibDebug/StackFrameUtils.h b/Userland/Libraries/LibDebug/StackFrameUtils.h new file mode 100644 index 0000000000..557992d79e --- /dev/null +++ b/Userland/Libraries/LibDebug/StackFrameUtils.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include <AK/Optional.h> +#include <AK/Types.h> + +#include "LibDebug/DebugSession.h" + +namespace Debug::StackFrameUtils { + +struct StackFrameInfo { + FlatPtr return_address; + FlatPtr next_ebp; +}; + +Optional<StackFrameInfo> get_info(const DebugSession&, FlatPtr current_ebp); + +} |