1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 17:57:35 +00:00

LibELF: Add find_demangled_function

Also, added AK::String::index_of and fixed a bug in ELF::Loader::symbol_ptr
This commit is contained in:
Itamar 2020-04-13 19:23:19 +03:00 committed by Andreas Kling
parent 34f0d98e67
commit e207de8449
7 changed files with 71 additions and 24 deletions

View file

@ -317,6 +317,18 @@ bool String::contains(const String& needle) const
return strstr(characters(), needle.characters()); return strstr(characters(), needle.characters());
} }
Optional<size_t> String::index_of(const String& needle) const
{
if (is_null() || needle.is_null())
return {};
const char* self_characters = characters();
const char* result = strstr(self_characters, needle.characters());
if (!result)
return {};
return Optional<size_t> { result - self_characters };
}
bool String::equals_ignoring_case(const StringView& other) const bool String::equals_ignoring_case(const StringView& other) const
{ {
return StringUtils::equals_ignoring_case(view(), other); return StringUtils::equals_ignoring_case(view(), other);

View file

@ -117,6 +117,7 @@ public:
bool equals_ignoring_case(const StringView&) const; bool equals_ignoring_case(const StringView&) const;
bool contains(const String&) const; bool contains(const String&) const;
Optional<size_t> index_of(const String&) const;
Vector<String> split_limit(char separator, size_t limit, bool keep_empty = false) const; Vector<String> split_limit(char separator, size_t limit, bool keep_empty = false) const;
Vector<String> split(char separator, bool keep_empty = false) const; Vector<String> split(char separator, bool keep_empty = false) const;

View file

@ -30,8 +30,8 @@
DebugSession::DebugSession(int pid) DebugSession::DebugSession(int pid)
: m_debugee_pid(pid) : m_debugee_pid(pid)
, m_executable(make<MappedFile>(String::format("/proc/%d/exe", pid))) , m_executable(String::format("/proc/%d/exe", pid))
, m_elf_image(make<ELF::Image>(reinterpret_cast<u8*>(m_executable->data()), m_executable->size())) , m_elf(reinterpret_cast<u8*>(m_executable.data()), m_executable.size())
{ {
} }
@ -176,8 +176,3 @@ void DebugSession::continue_debugee()
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
} }
VirtualAddress DebugSession::get_entry_point() const
{
return m_elf_image->entry();
}

View file

@ -26,13 +26,14 @@
#pragma once #pragma once
#include <AK/Demangle.h>
#include <AK/HashMap.h> #include <AK/HashMap.h>
#include <AK/MappedFile.h> #include <AK/MappedFile.h>
#include <AK/Optional.h> #include <AK/Optional.h>
#include <AK/OwnPtr.h> #include <AK/OwnPtr.h>
#include <AK/String.h> #include <AK/String.h>
#include <LibC/sys/arch/i386/regs.h> #include <LibC/sys/arch/i386/regs.h>
#include <LibELF/Image.h> #include <LibELF/Loader.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <sys/ptrace.h> #include <sys/ptrace.h>
@ -69,7 +70,7 @@ public:
template<typename Callback> template<typename Callback>
void run(Callback callback); void run(Callback callback);
VirtualAddress get_entry_point() const; const ELF::Loader& elf() const { return m_elf; }
enum DebugDecision { enum DebugDecision {
Continue, Continue,
@ -90,8 +91,8 @@ private:
int m_debugee_pid { -1 }; int m_debugee_pid { -1 };
bool m_is_debugee_dead { false }; bool m_is_debugee_dead { false };
NonnullOwnPtr<MappedFile> m_executable; MappedFile m_executable;
NonnullOwnPtr<ELF::Image> m_elf_image; ELF::Loader m_elf;
HashMap<void*, BreakPoint> m_breakpoints; HashMap<void*, BreakPoint> m_breakpoints;
}; };

View file

@ -27,6 +27,7 @@
#include "DebugSession.h" #include "DebugSession.h"
#include <AK/Assertions.h> #include <AK/Assertions.h>
#include <AK/ByteBuffer.h> #include <AK/ByteBuffer.h>
#include <AK/Demangle.h>
#include <AK/LogStream.h> #include <AK/LogStream.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <AK/kmalloc.h> #include <AK/kmalloc.h>
@ -137,14 +138,28 @@ bool handle_breakpoint_command(const String& command)
if (parts.size() != 2) if (parts.size() != 2)
return false; return false;
u32 breakpoint_address = strtoul(parts[1].characters(), nullptr, 16); auto argument = parts[1];
if (errno != 0) if (argument.is_empty())
return false; return false;
u32 breakpoint_address = 0;
if ((argument[0] >= '0' && argument[0] <= '9')) {
breakpoint_address = strtoul(argument.characters(), nullptr, 16);
} else {
auto symbol = g_debug_session->elf().find_demangled_function(argument);
if (!symbol.has_value()) {
printf("symbol %s not found\n", parts[1].characters());
return false;
}
breakpoint_address = reinterpret_cast<u32>(symbol.value().value());
}
bool success = g_debug_session->insert_breakpoint(reinterpret_cast<void*>(breakpoint_address)); bool success = g_debug_session->insert_breakpoint(reinterpret_cast<void*>(breakpoint_address));
if (!success) { if (!success) {
fprintf(stderr, "coult not insert breakpoint at: 0x%x\n", breakpoint_address); fprintf(stderr, "coult not insert breakpoint at: %08x\n", breakpoint_address);
return false; return false;
} }
printf("breakpoint insterted at: %08x\n", breakpoint_address);
return true; return true;
} }
@ -153,8 +168,8 @@ void print_help()
printf("Options:\n" printf("Options:\n"
"cont - Continue execution\n" "cont - Continue execution\n"
"regs - Print registers\n" "regs - Print registers\n"
"dis <number of instructions> - Print disassembly\n" "dis [number of instructions] - Print disassembly\n"
"bp <address> - Insert a breakpoint\n"); "bp <address/symbol> - Insert a breakpoint\n");
} }
int main(int argc, char** argv) int main(int argc, char** argv)
@ -185,7 +200,7 @@ int main(int argc, char** argv)
sa.sa_handler = handle_sigint; sa.sa_handler = handle_sigint;
sigaction(SIGINT, &sa, nullptr); sigaction(SIGINT, &sa, nullptr);
bool rc = g_debug_session->insert_breakpoint(g_debug_session->get_entry_point().as_ptr()); bool rc = g_debug_session->insert_breakpoint(g_debug_session->elf().entry().as_ptr());
ASSERT(rc); ASSERT(rc);
g_debug_session->run([&](DebugSession::DebugBreakReason reason, Optional<PtraceRegisters> optional_regs) { g_debug_session->run([&](DebugSession::DebugBreakReason reason, Optional<PtraceRegisters> optional_regs) {

View file

@ -136,13 +136,13 @@ bool Loader::layout()
return !failed; return !failed;
} }
char* Loader::symbol_ptr(const char* name) char* Loader::symbol_ptr(const char* name) const
{ {
char* found_ptr = nullptr; char* found_ptr = nullptr;
m_image.for_each_symbol([&](const Image::Symbol symbol) { m_image.for_each_symbol([&](const Image::Symbol symbol) {
if (symbol.type() != STT_FUNC) if (symbol.type() != STT_FUNC)
return IterationDecision::Continue; return IterationDecision::Continue;
if (symbol.name() == name) if (symbol.name() != name)
return IterationDecision::Continue; return IterationDecision::Continue;
if (m_image.is_executable()) if (m_image.is_executable())
found_ptr = (char*)(size_t)symbol.value(); found_ptr = (char*)(size_t)symbol.value();
@ -153,6 +153,25 @@ char* Loader::symbol_ptr(const char* name)
return found_ptr; return found_ptr;
} }
Optional<Image::Symbol> Loader::find_demangled_function(const String& name) const
{
Optional<Image::Symbol> found;
m_image.for_each_symbol([&](const Image::Symbol symbol) {
if (symbol.type() != STT_FUNC)
return IterationDecision::Continue;
auto demangled = demangle(symbol.name());
auto index_of_paren = demangled.index_of("(");
if (index_of_paren.has_value()) {
demangled = demangled.substring(0, index_of_paren.value());
}
if (demangled != name)
return IterationDecision::Continue;
found = symbol;
return IterationDecision::Break;
});
return found;
}
#ifndef KERNEL #ifndef KERNEL
Optional<Image::Symbol> Loader::find_symbol(u32 address, u32* out_offset) const Optional<Image::Symbol> Loader::find_symbol(u32 address, u32* out_offset) const
{ {
@ -160,7 +179,7 @@ Optional<Image::Symbol> Loader::find_symbol(u32 address, u32* out_offset) const
return {}; return {};
SortedSymbol* sorted_symbols = nullptr; SortedSymbol* sorted_symbols = nullptr;
#ifdef KERNEL # ifdef KERNEL
if (!m_sorted_symbols_region) { if (!m_sorted_symbols_region) {
m_sorted_symbols_region = MM.allocate_kernel_region(PAGE_ROUND_UP(m_symbol_count * sizeof(SortedSymbol)), "Sorted symbols", Kernel::Region::Access::Read | Kernel::Region::Access::Write); m_sorted_symbols_region = MM.allocate_kernel_region(PAGE_ROUND_UP(m_symbol_count * sizeof(SortedSymbol)), "Sorted symbols", Kernel::Region::Access::Read | Kernel::Region::Access::Write);
sorted_symbols = (SortedSymbol*)m_sorted_symbols_region->vaddr().as_ptr(); sorted_symbols = (SortedSymbol*)m_sorted_symbols_region->vaddr().as_ptr();
@ -175,7 +194,7 @@ Optional<Image::Symbol> Loader::find_symbol(u32 address, u32* out_offset) const
} else { } else {
sorted_symbols = (SortedSymbol*)m_sorted_symbols_region->vaddr().as_ptr(); sorted_symbols = (SortedSymbol*)m_sorted_symbols_region->vaddr().as_ptr();
} }
#else # else
if (m_sorted_symbols.is_empty()) { if (m_sorted_symbols.is_empty()) {
m_sorted_symbols.ensure_capacity(m_symbol_count); m_sorted_symbols.ensure_capacity(m_symbol_count);
m_image.for_each_symbol([this](auto& symbol) { m_image.for_each_symbol([this](auto& symbol) {
@ -187,7 +206,7 @@ Optional<Image::Symbol> Loader::find_symbol(u32 address, u32* out_offset) const
}); });
} }
sorted_symbols = m_sorted_symbols.data(); sorted_symbols = m_sorted_symbols.data();
#endif # endif
for (size_t i = 0; i < m_symbol_count; ++i) { for (size_t i = 0; i < m_symbol_count; ++i) {
if (sorted_symbols[i].address > address) { if (sorted_symbols[i].address > address) {

View file

@ -52,9 +52,13 @@ public:
Function<void*(VirtualAddress, size_t, size_t, bool, bool, const String&)> alloc_section_hook; Function<void*(VirtualAddress, size_t, size_t, bool, bool, const String&)> alloc_section_hook;
Function<void*(size_t, size_t)> tls_section_hook; Function<void*(size_t, size_t)> tls_section_hook;
Function<void*(VirtualAddress, size_t, size_t, size_t, bool r, bool w, bool x, const String&)> map_section_hook; Function<void*(VirtualAddress, size_t, size_t, size_t, bool r, bool w, bool x, const String&)> map_section_hook;
VirtualAddress entry() const { return m_image.entry(); }
#endif #endif
char* symbol_ptr(const char* name); VirtualAddress entry() const
{
return m_image.entry();
}
char* symbol_ptr(const char* name) const;
Optional<Image::Symbol> find_demangled_function(const String& name) const;
bool has_symbols() const { return m_symbol_count; } bool has_symbols() const { return m_symbol_count; }