1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:57:44 +00:00

LibDebug: Store LibDebug objects on the heap & make them non-copyable

This fixes an issue were some LibDebug objects (for example,
Dwarf::CompilationUnit) held a reference to their parent
Dwarf::DwarfInfo object, which was constructed on the stack and later
moved to the heap.
This commit is contained in:
Itamar 2021-06-18 14:31:03 +03:00 committed by Andreas Kling
parent edd79ddd00
commit e9e4358a93
7 changed files with 19 additions and 10 deletions

View file

@ -42,7 +42,7 @@ static const ELFObjectInfo* object_info_for_region(const ELF::Core::MemoryRegion
return nullptr; return nullptr;
auto image = make<ELF::Image>(file_or_error.value()->bytes()); auto image = make<ELF::Image>(file_or_error.value()->bytes());
auto info = make<ELFObjectInfo>(file_or_error.release_value(), Debug::DebugInfo { move(image) }); auto info = make<ELFObjectInfo>(file_or_error.release_value(), make<Debug::DebugInfo>(move(image)));
auto* info_ptr = info.ptr(); auto* info_ptr = info.ptr();
s_debug_info_cache.set(path, move(info)); s_debug_info_cache.set(path, move(info));
return info_ptr; return info_ptr;

View file

@ -14,14 +14,14 @@
namespace CoreDump { namespace CoreDump {
struct ELFObjectInfo { struct ELFObjectInfo {
ELFObjectInfo(NonnullRefPtr<MappedFile> file, Debug::DebugInfo&& debug_info) ELFObjectInfo(NonnullRefPtr<MappedFile> file, NonnullOwnPtr<Debug::DebugInfo>&& debug_info)
: file(move(file)) : file(move(file))
, debug_info(move(debug_info)) , debug_info(move(debug_info))
{ {
} }
NonnullRefPtr<MappedFile> file; NonnullRefPtr<MappedFile> file;
Debug::DebugInfo debug_info; NonnullOwnPtr<Debug::DebugInfo> debug_info;
}; };
class Backtrace { class Backtrace {

View file

@ -20,6 +20,9 @@
namespace Debug { namespace Debug {
class DebugInfo { class DebugInfo {
AK_MAKE_NONCOPYABLE(DebugInfo);
AK_MAKE_NONMOVABLE(DebugInfo);
public: public:
explicit DebugInfo(NonnullOwnPtr<const ELF::Image>, String source_root = {}, FlatPtr base_address = 0); explicit DebugInfo(NonnullOwnPtr<const ELF::Image>, String source_root = {}, FlatPtr base_address = 0);

View file

@ -7,6 +7,7 @@
#pragma once #pragma once
#include "AbbreviationsMap.h" #include "AbbreviationsMap.h"
#include <AK/Noncopyable.h>
#include <AK/Types.h> #include <AK/Types.h>
namespace Debug::Dwarf { namespace Debug::Dwarf {
@ -15,6 +16,9 @@ class DwarfInfo;
class DIE; class DIE;
class CompilationUnit { class CompilationUnit {
AK_MAKE_NONCOPYABLE(CompilationUnit);
AK_MAKE_NONMOVABLE(CompilationUnit);
public: public:
CompilationUnit(const DwarfInfo& dwarf_info, u32 offset, const CompilationUnitHeader&); CompilationUnit(const DwarfInfo& dwarf_info, u32 offset, const CompilationUnitHeader&);

View file

@ -44,7 +44,7 @@ void DwarfInfo::populate_compilation_units()
VERIFY(compilation_unit_header.address_size() == sizeof(u32)); VERIFY(compilation_unit_header.address_size() == sizeof(u32));
u32 length_after_header = compilation_unit_header.length() - (compilation_unit_header.header_size() - offsetof(CompilationUnitHeader, common.version)); u32 length_after_header = compilation_unit_header.length() - (compilation_unit_header.header_size() - offsetof(CompilationUnitHeader, common.version));
m_compilation_units.empend(*this, unit_offset, compilation_unit_header); m_compilation_units.append(make<CompilationUnit>(*this, unit_offset, compilation_unit_header));
stream.discard_or_error(length_after_header); stream.discard_or_error(length_after_header);
} }
} }

View file

@ -19,6 +19,9 @@
namespace Debug::Dwarf { namespace Debug::Dwarf {
class DwarfInfo { class DwarfInfo {
AK_MAKE_NONCOPYABLE(DwarfInfo);
AK_MAKE_NONMOVABLE(DwarfInfo);
public: public:
explicit DwarfInfo(const ELF::Image&); explicit DwarfInfo(const ELF::Image&);
@ -44,7 +47,7 @@ private:
ReadonlyBytes m_debug_strings_data; ReadonlyBytes m_debug_strings_data;
ReadonlyBytes m_debug_line_strings_data; ReadonlyBytes m_debug_line_strings_data;
Vector<Dwarf::CompilationUnit> m_compilation_units; NonnullOwnPtrVector<Dwarf::CompilationUnit> m_compilation_units;
}; };
template<typename Callback> template<typename Callback>

View file

@ -16,7 +16,7 @@ namespace Symbolication {
struct CachedELF { struct CachedELF {
NonnullRefPtr<MappedFile> mapped_file; NonnullRefPtr<MappedFile> mapped_file;
Debug::DebugInfo debug_info; NonnullOwnPtr<Debug::DebugInfo> debug_info;
}; };
static HashMap<String, OwnPtr<CachedELF>> s_cache; static HashMap<String, OwnPtr<CachedELF>> s_cache;
@ -36,8 +36,7 @@ Optional<Symbol> symbolicate(String const& path, u32 address)
s_cache.set(path, {}); s_cache.set(path, {});
{}; {};
} }
Debug::DebugInfo debug_info(move(elf)); auto cached_elf = make<CachedELF>(mapped_file.release_value(), make<Debug::DebugInfo>(move(elf)));
auto cached_elf = make<CachedELF>(mapped_file.release_value(), move(debug_info));
s_cache.set(path, move(cached_elf)); s_cache.set(path, move(cached_elf));
} }
@ -49,8 +48,8 @@ Optional<Symbol> symbolicate(String const& path, u32 address)
return {}; return {};
u32 offset = 0; u32 offset = 0;
auto symbol = cached_elf->debug_info.elf().symbolicate(address, &offset); auto symbol = cached_elf->debug_info->elf().symbolicate(address, &offset);
auto source_position = cached_elf->debug_info.get_source_position(address); auto source_position = cached_elf->debug_info->get_source_position(address);
String filename; String filename;
u32 line_number = 0; u32 line_number = 0;
if (source_position.has_value()) { if (source_position.has_value()) {