From c4437e19bdb28af3dc45c973fa46b4be3e2ab3ee Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Fri, 6 Aug 2021 00:35:36 +0430 Subject: [PATCH] LibDebug+Everywhere: Make DebugInfo not own the ELF image This is required to avoid copying the image where otherwise a reference would be enough. --- Userland/DevTools/UserspaceEmulator/Emulator.cpp | 5 +++-- Userland/DevTools/UserspaceEmulator/Emulator.h | 1 + Userland/Libraries/LibCoreDump/Backtrace.cpp | 3 ++- Userland/Libraries/LibCoreDump/Backtrace.h | 4 +++- Userland/Libraries/LibDebug/DebugInfo.cpp | 6 +++--- Userland/Libraries/LibDebug/DebugInfo.h | 6 +++--- Userland/Libraries/LibDebug/DebugSession.cpp | 5 +++-- Userland/Libraries/LibDebug/DebugSession.h | 4 +++- Userland/Libraries/LibSymbolication/Symbolication.cpp | 3 ++- 9 files changed, 23 insertions(+), 14 deletions(-) diff --git a/Userland/DevTools/UserspaceEmulator/Emulator.cpp b/Userland/DevTools/UserspaceEmulator/Emulator.cpp index 358a0605df..73daa6d6ff 100644 --- a/Userland/DevTools/UserspaceEmulator/Emulator.cpp +++ b/Userland/DevTools/UserspaceEmulator/Emulator.cpp @@ -419,8 +419,9 @@ MmapRegion const* Emulator::load_library_from_adress(FlatPtr address) if (file_or_error.is_error()) return {}; - auto debug_info = make(make(file_or_error.value()->bytes())); - m_dynamic_library_cache.set(lib_path, CachedELF { file_or_error.release_value(), move(debug_info) }); + auto image = make(file_or_error.value()->bytes()); + auto debug_info = make(*image); + m_dynamic_library_cache.set(lib_path, CachedELF { file_or_error.release_value(), move(debug_info), move(image) }); } return region; } diff --git a/Userland/DevTools/UserspaceEmulator/Emulator.h b/Userland/DevTools/UserspaceEmulator/Emulator.h index 8b4e7da099..df6bf32dba 100644 --- a/Userland/DevTools/UserspaceEmulator/Emulator.h +++ b/Userland/DevTools/UserspaceEmulator/Emulator.h @@ -257,6 +257,7 @@ private: struct CachedELF { NonnullRefPtr mapped_file; NonnullOwnPtr debug_info; + NonnullOwnPtr image; }; HashMap m_dynamic_library_cache; diff --git a/Userland/Libraries/LibCoreDump/Backtrace.cpp b/Userland/Libraries/LibCoreDump/Backtrace.cpp index 68f20e7467..0d0a361ab5 100644 --- a/Userland/Libraries/LibCoreDump/Backtrace.cpp +++ b/Userland/Libraries/LibCoreDump/Backtrace.cpp @@ -35,7 +35,8 @@ ELFObjectInfo const* Backtrace::object_info_for_region(ELF::Core::MemoryRegionIn return nullptr; auto image = make(file_or_error.value()->bytes()); - auto info = make(file_or_error.release_value(), make(move(image))); + auto& image_reference = *image; + auto info = make(file_or_error.release_value(), make(image_reference), move(image)); auto* info_ptr = info.ptr(); m_debug_info_cache.set(path, move(info)); return info_ptr; diff --git a/Userland/Libraries/LibCoreDump/Backtrace.h b/Userland/Libraries/LibCoreDump/Backtrace.h index 722b2d0a47..93f9813c56 100644 --- a/Userland/Libraries/LibCoreDump/Backtrace.h +++ b/Userland/Libraries/LibCoreDump/Backtrace.h @@ -14,14 +14,16 @@ namespace CoreDump { struct ELFObjectInfo { - ELFObjectInfo(NonnullRefPtr file, NonnullOwnPtr&& debug_info) + ELFObjectInfo(NonnullRefPtr file, NonnullOwnPtr&& debug_info, NonnullOwnPtr image) : file(move(file)) , debug_info(move(debug_info)) + , image(move(image)) { } NonnullRefPtr file; NonnullOwnPtr debug_info; + NonnullOwnPtr image; }; class Backtrace { diff --git a/Userland/Libraries/LibDebug/DebugInfo.cpp b/Userland/Libraries/LibDebug/DebugInfo.cpp index 3047bba9c9..35becb1e21 100644 --- a/Userland/Libraries/LibDebug/DebugInfo.cpp +++ b/Userland/Libraries/LibDebug/DebugInfo.cpp @@ -15,11 +15,11 @@ namespace Debug { -DebugInfo::DebugInfo(NonnullOwnPtr elf, String source_root, FlatPtr base_address) - : m_elf(move(elf)) +DebugInfo::DebugInfo(ELF::Image const& elf, String source_root, FlatPtr base_address) + : m_elf(elf) , m_source_root(move(source_root)) , m_base_address(base_address) - , m_dwarf_info(*m_elf) + , m_dwarf_info(m_elf) { prepare_variable_scopes(); prepare_lines(); diff --git a/Userland/Libraries/LibDebug/DebugInfo.h b/Userland/Libraries/LibDebug/DebugInfo.h index c238ff63cf..6a8d12b20d 100644 --- a/Userland/Libraries/LibDebug/DebugInfo.h +++ b/Userland/Libraries/LibDebug/DebugInfo.h @@ -24,9 +24,9 @@ class DebugInfo { AK_MAKE_NONMOVABLE(DebugInfo); public: - explicit DebugInfo(NonnullOwnPtr, String source_root = {}, FlatPtr base_address = 0); + explicit DebugInfo(ELF::Image const&, String source_root = {}, FlatPtr base_address = 0); - ELF::Image const& elf() const { return *m_elf; } + ELF::Image const& elf() const { return m_elf; } struct SourcePosition { FlyString file_path; @@ -124,7 +124,7 @@ private: Optional get_source_path_of_inline(const Dwarf::DIE&) const; Optional get_line_of_inline(const Dwarf::DIE&) const; - NonnullOwnPtr m_elf; + ELF::Image const& m_elf; String m_source_root; FlatPtr m_base_address { 0 }; Dwarf::DwarfInfo m_dwarf_info; diff --git a/Userland/Libraries/LibDebug/DebugSession.cpp b/Userland/Libraries/LibDebug/DebugSession.cpp index bae599dc76..ae7a32d01b 100644 --- a/Userland/Libraries/LibDebug/DebugSession.cpp +++ b/Userland/Libraries/LibDebug/DebugSession.cpp @@ -448,8 +448,9 @@ void DebugSession::update_loaded_libs() return IterationDecision::Continue; FlatPtr base_address = entry.as_object().get("address").to_addr(); - auto debug_info = make(make(file_or_error.value()->bytes()), m_source_root, base_address); - auto lib = make(lib_name, file_or_error.release_value(), move(debug_info), base_address); + auto image = make(file_or_error.value()->bytes()); + auto debug_info = make(*image, m_source_root, base_address); + auto lib = make(lib_name, file_or_error.release_value(), move(image), move(debug_info), base_address); m_loaded_libraries.set(lib_name, move(lib)); return IterationDecision::Continue; diff --git a/Userland/Libraries/LibDebug/DebugSession.h b/Userland/Libraries/LibDebug/DebugSession.h index a81dd70e32..c678917770 100644 --- a/Userland/Libraries/LibDebug/DebugSession.h +++ b/Userland/Libraries/LibDebug/DebugSession.h @@ -129,12 +129,14 @@ public: struct LoadedLibrary { String name; NonnullRefPtr file; + NonnullOwnPtr image; NonnullOwnPtr debug_info; FlatPtr base_address; - LoadedLibrary(String const& name, NonnullRefPtr file, NonnullOwnPtr&& debug_info, FlatPtr base_address) + LoadedLibrary(String const& name, NonnullRefPtr file, NonnullOwnPtr image, NonnullOwnPtr&& debug_info, FlatPtr base_address) : name(name) , file(move(file)) + , image(move(image)) , debug_info(move(debug_info)) , base_address(base_address) { diff --git a/Userland/Libraries/LibSymbolication/Symbolication.cpp b/Userland/Libraries/LibSymbolication/Symbolication.cpp index 13bc9840d4..f9adaa05e0 100644 --- a/Userland/Libraries/LibSymbolication/Symbolication.cpp +++ b/Userland/Libraries/LibSymbolication/Symbolication.cpp @@ -18,6 +18,7 @@ namespace Symbolication { struct CachedELF { NonnullRefPtr mapped_file; NonnullOwnPtr debug_info; + NonnullOwnPtr image; }; static HashMap> s_cache; @@ -73,7 +74,7 @@ Optional symbolicate(String const& path, FlatPtr address) s_cache.set(path, {}); {}; } - auto cached_elf = make(mapped_file.release_value(), make(move(elf))); + auto cached_elf = make(mapped_file.release_value(), make(*elf), move(elf)); s_cache.set(path, move(cached_elf)); }