From 373a595c567cb467aa1f1c627dcfb4fac94dbf86 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 3 Mar 2021 22:59:45 +0100 Subject: [PATCH] Profiler: Symbolicate addresses in non-PIE ELF objects This is a little bit messy, but basically if an ELF object is non-PIE, we have to account for the executable mapping being at a hard-coded offset and subtract that when doing symbolication. There's probably a nicer way to solve this, I just hacked this together so we can see "cc1plus" and friends in profiles. :^) --- Userland/DevTools/Profiler/Profile.cpp | 23 +++++++++++++++++------ Userland/DevTools/Profiler/Profile.h | 3 +++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Userland/DevTools/Profiler/Profile.cpp b/Userland/DevTools/Profiler/Profile.cpp index 1f61b1c3e36..b59b6d1a6fd 100644 --- a/Userland/DevTools/Profiler/Profile.cpp +++ b/Userland/DevTools/Profiler/Profile.cpp @@ -316,11 +316,7 @@ Result, String> Profile::load_from_perfcore_file(const St library_metadata = it->library_metadata.ptr(); if (auto* library = library_metadata ? library_metadata->library_containing(ptr) : nullptr) { object_name = library->name; - if (library->object) { - symbol = library->object->elf.symbolicate(ptr - library->base, &offset); - } else { - symbol = "??"; - } + symbol = library->symbolicate(ptr, &offset); } else { symbol = "??"; } @@ -455,10 +451,25 @@ LibraryMetadata::LibraryMetadata(JsonArray regions) if (!mapped_object) continue; - m_libraries.set(name, adopt_own(*new Library { base, size, name, mapped_object })); + FlatPtr text_base {}; + mapped_object->elf.for_each_program_header([&](const ELF::Image::ProgramHeader& ph) { + if (ph.is_executable()) + text_base = ph.vaddr().get(); + return IterationDecision::Continue; + }); + + m_libraries.set(name, adopt_own(*new Library { base, size, name, text_base, mapped_object })); } } +String LibraryMetadata::Library::symbolicate(FlatPtr ptr, u32* offset) const +{ + if (!object) + return "??"sv; + + return object->elf.symbolicate(ptr - base + text_base, offset); +} + const LibraryMetadata::Library* LibraryMetadata::library_containing(FlatPtr ptr) const { for (auto& it : m_libraries) { diff --git a/Userland/DevTools/Profiler/Profile.h b/Userland/DevTools/Profiler/Profile.h index 63728410379..8a76073487c 100644 --- a/Userland/DevTools/Profiler/Profile.h +++ b/Userland/DevTools/Profiler/Profile.h @@ -59,7 +59,10 @@ public: FlatPtr base; size_t size; String name; + FlatPtr text_base; MappedObject* object { nullptr }; + + String symbolicate(FlatPtr, u32* offset) const; }; const Library* library_containing(FlatPtr) const;