Просмотр исходного кода

LibDebug+Everywhere: Make DebugInfo not own the ELF image

This is required to avoid copying the image where otherwise a reference
would be enough.
Ali Mohammad Pur 3 лет назад
Родитель
Сommit
c4437e19bd

+ 3 - 2
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<Debug::DebugInfo>(make<ELF::Image>(file_or_error.value()->bytes()));
-        m_dynamic_library_cache.set(lib_path, CachedELF { file_or_error.release_value(), move(debug_info) });
+        auto image = make<ELF::Image>(file_or_error.value()->bytes());
+        auto debug_info = make<Debug::DebugInfo>(*image);
+        m_dynamic_library_cache.set(lib_path, CachedELF { file_or_error.release_value(), move(debug_info), move(image) });
     }
     return region;
 }

+ 1 - 0
Userland/DevTools/UserspaceEmulator/Emulator.h

@@ -257,6 +257,7 @@ private:
     struct CachedELF {
         NonnullRefPtr<MappedFile> mapped_file;
         NonnullOwnPtr<Debug::DebugInfo> debug_info;
+        NonnullOwnPtr<ELF::Image> image;
     };
 
     HashMap<String, CachedELF> m_dynamic_library_cache;

+ 2 - 1
Userland/Libraries/LibCoreDump/Backtrace.cpp

@@ -35,7 +35,8 @@ ELFObjectInfo const* Backtrace::object_info_for_region(ELF::Core::MemoryRegionIn
         return nullptr;
 
     auto image = make<ELF::Image>(file_or_error.value()->bytes());
-    auto info = make<ELFObjectInfo>(file_or_error.release_value(), make<Debug::DebugInfo>(move(image)));
+    auto& image_reference = *image;
+    auto info = make<ELFObjectInfo>(file_or_error.release_value(), make<Debug::DebugInfo>(image_reference), move(image));
     auto* info_ptr = info.ptr();
     m_debug_info_cache.set(path, move(info));
     return info_ptr;

+ 3 - 1
Userland/Libraries/LibCoreDump/Backtrace.h

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

+ 3 - 3
Userland/Libraries/LibDebug/DebugInfo.cpp

@@ -15,11 +15,11 @@
 
 namespace Debug {
 
-DebugInfo::DebugInfo(NonnullOwnPtr<const ELF::Image> 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();

+ 3 - 3
Userland/Libraries/LibDebug/DebugInfo.h

@@ -24,9 +24,9 @@ class DebugInfo {
     AK_MAKE_NONMOVABLE(DebugInfo);
 
 public:
-    explicit DebugInfo(NonnullOwnPtr<const ELF::Image>, 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<Dwarf::LineProgram::DirectoryAndFile> get_source_path_of_inline(const Dwarf::DIE&) const;
     Optional<uint32_t> get_line_of_inline(const Dwarf::DIE&) const;
 
-    NonnullOwnPtr<const ELF::Image> m_elf;
+    ELF::Image const& m_elf;
     String m_source_root;
     FlatPtr m_base_address { 0 };
     Dwarf::DwarfInfo m_dwarf_info;

+ 3 - 2
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<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);
+        auto image = make<ELF::Image>(file_or_error.value()->bytes());
+        auto debug_info = make<DebugInfo>(*image, m_source_root, base_address);
+        auto lib = make<LoadedLibrary>(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;

+ 3 - 1
Userland/Libraries/LibDebug/DebugSession.h

@@ -129,12 +129,14 @@ public:
     struct LoadedLibrary {
         String name;
         NonnullRefPtr<MappedFile> file;
+        NonnullOwnPtr<ELF::Image> image;
         NonnullOwnPtr<DebugInfo> debug_info;
         FlatPtr base_address;
 
-        LoadedLibrary(String const& name, NonnullRefPtr<MappedFile> file, NonnullOwnPtr<DebugInfo>&& debug_info, FlatPtr base_address)
+        LoadedLibrary(String const& name, NonnullRefPtr<MappedFile> file, NonnullOwnPtr<ELF::Image> image, NonnullOwnPtr<DebugInfo>&& debug_info, FlatPtr base_address)
             : name(name)
             , file(move(file))
+            , image(move(image))
             , debug_info(move(debug_info))
             , base_address(base_address)
         {

+ 2 - 1
Userland/Libraries/LibSymbolication/Symbolication.cpp

@@ -18,6 +18,7 @@ namespace Symbolication {
 struct CachedELF {
     NonnullRefPtr<MappedFile> mapped_file;
     NonnullOwnPtr<Debug::DebugInfo> debug_info;
+    NonnullOwnPtr<ELF::Image> image;
 };
 
 static HashMap<String, OwnPtr<CachedELF>> s_cache;
@@ -73,7 +74,7 @@ Optional<Symbol> symbolicate(String const& path, FlatPtr address)
             s_cache.set(path, {});
             {};
         }
-        auto cached_elf = make<CachedELF>(mapped_file.release_value(), make<Debug::DebugInfo>(move(elf)));
+        auto cached_elf = make<CachedELF>(mapped_file.release_value(), make<Debug::DebugInfo>(*elf), move(elf));
         s_cache.set(path, move(cached_elf));
     }