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

CrashDaemon: Show source position in backtrace

This commit is contained in:
Itamar 2020-12-03 18:09:04 +02:00 committed by Andreas Kling
parent 72ca45e300
commit 69b0fdd84a
2 changed files with 53 additions and 10 deletions

View file

@ -3,4 +3,4 @@ set(SOURCES
) )
serenity_bin(CrashDaemon) serenity_bin(CrashDaemon)
target_link_libraries(CrashDaemon LibC LibCore LibCoreDump) target_link_libraries(CrashDaemon LibC LibCore LibCoreDump LibDebug)

View file

@ -25,10 +25,13 @@
*/ */
#include <AK/Assertions.h> #include <AK/Assertions.h>
#include <AK/HashMap.h>
#include <AK/LexicalPath.h>
#include <AK/LogStream.h> #include <AK/LogStream.h>
#include <AK/ScopeGuard.h> #include <AK/ScopeGuard.h>
#include <LibCore/DirectoryWatcher.h> #include <LibCore/DirectoryWatcher.h>
#include <LibCoreDump/CoreDumpReader.h> #include <LibCoreDump/CoreDumpReader.h>
#include <LibDebug/DebugInfo.h>
#include <LibELF/Loader.h> #include <LibELF/Loader.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
@ -60,7 +63,25 @@ static String object_name(StringView memory_region_name)
return memory_region_name.substring_view(0, memory_region_name.find_first_of(":").value()).to_string(); return memory_region_name.substring_view(0, memory_region_name.find_first_of(":").value()).to_string();
} }
static String symbolicate(FlatPtr eip, const ELF::Core::MemoryRegionInfo* region) struct ElfObjectInfo : public RefCounted<ElfObjectInfo> {
ElfObjectInfo(MappedFile&& file, NonnullRefPtr<ELF::Loader>&& loader, Debug::DebugInfo&& debug_info)
: file(move(file))
, loader(move(loader))
, debug_info(move(debug_info))
{
}
MappedFile file;
NonnullRefPtr<ELF::Loader> loader;
Debug::DebugInfo debug_info;
};
// FIXME: This cache has to be invalidated when libraries/programs are re-compiled.
// We can store the last-modified timestamp of the elf files in ElfObjectInfo to invalidate cache entries.
static HashMap<String, RefPtr<ElfObjectInfo>> s_debug_info_cache;
static RefPtr<ElfObjectInfo> object_info_for_region(const ELF::Core::MemoryRegionInfo* region)
{ {
StringView region_name { region->region_name }; StringView region_name { region->region_name };
@ -73,17 +94,29 @@ static String symbolicate(FlatPtr eip, const ELF::Core::MemoryRegionInfo* region
path = name; path = name;
} }
auto cached_value = s_debug_info_cache.get(path);
if (cached_value.has_value())
return cached_value.value();
struct stat st; struct stat st;
if (stat(path.characters(), &st)) { if (stat(path.characters(), &st)) {
return {}; return nullptr;
} }
auto mapped_file = make<MappedFile>(path); MappedFile object_file(path);
if (!mapped_file->is_valid()) if (!object_file.is_valid())
return {}; return nullptr;
auto loader = ELF::Loader::create((const u8*)mapped_file->data(), mapped_file->size()); auto loader = ELF::Loader::create((const u8*)object_file.data(), object_file.size());
return loader->symbolicate(eip - region->region_start); Debug::DebugInfo debug_info(loader);
RefPtr<ElfObjectInfo> info = adopt(*new ElfObjectInfo(
move(object_file),
move(loader),
move(debug_info)));
s_debug_info_cache.set(path, info);
return info;
} }
static String backtrace_line(const CoreDumpReader& coredump, FlatPtr eip) static String backtrace_line(const CoreDumpReader& coredump, FlatPtr eip)
@ -97,9 +130,19 @@ static String backtrace_line(const CoreDumpReader& coredump, FlatPtr eip)
if (region_name.contains("Loader.so")) if (region_name.contains("Loader.so"))
return {}; return {};
auto func_name = symbolicate(eip, region); auto object_info = object_info_for_region(region);
if (object_info.is_null())
return {};
return String::format("%p: [%s] %s", eip, object_name(region_name).characters(), func_name.is_null() ? "???" : func_name.characters()); auto func_name = object_info->loader->symbolicate(eip - region->region_start);
auto source_position = object_info->debug_info.get_source_position(eip - region->region_start);
auto source_position_string = String::empty();
if (source_position.has_value())
source_position_string = String::format(" (\033[34;1m%s\033[0m:%u)", LexicalPath(source_position.value().file_path).basename().characters(), source_position.value().line_number);
return String::format("%p: [%s] %s%s", eip, object_name(region_name).characters(), func_name.is_null() ? "???" : func_name.characters(), source_position_string.characters());
} }
static void backtrace(const String& coredump_path) static void backtrace(const String& coredump_path)