summaryrefslogtreecommitdiff
path: root/Applications
diff options
context:
space:
mode:
authorItamar <itamar8910@gmail.com>2020-12-03 18:09:04 +0200
committerAndreas Kling <kling@serenityos.org>2020-12-14 23:05:53 +0100
commit69b0fdd84a165b783235de56799e19c0c911192b (patch)
treeda0ccc5ffe8e8a7516dd406f5f34a3107ef3cf52 /Applications
parent72ca45e3004c96f0358866f6551e0b137c4cda38 (diff)
downloadserenity-69b0fdd84a165b783235de56799e19c0c911192b.zip
CrashDaemon: Show source position in backtrace
Diffstat (limited to 'Applications')
-rw-r--r--Applications/CrashDaemon/CMakeLists.txt2
-rw-r--r--Applications/CrashDaemon/main.cpp61
2 files changed, 53 insertions, 10 deletions
diff --git a/Applications/CrashDaemon/CMakeLists.txt b/Applications/CrashDaemon/CMakeLists.txt
index 5dcf13aacb..e5da5d5ada 100644
--- a/Applications/CrashDaemon/CMakeLists.txt
+++ b/Applications/CrashDaemon/CMakeLists.txt
@@ -3,4 +3,4 @@ set(SOURCES
)
serenity_bin(CrashDaemon)
-target_link_libraries(CrashDaemon LibC LibCore LibCoreDump)
+target_link_libraries(CrashDaemon LibC LibCore LibCoreDump LibDebug)
diff --git a/Applications/CrashDaemon/main.cpp b/Applications/CrashDaemon/main.cpp
index 761e46d529..a8a90f220b 100644
--- a/Applications/CrashDaemon/main.cpp
+++ b/Applications/CrashDaemon/main.cpp
@@ -25,10 +25,13 @@
*/
#include <AK/Assertions.h>
+#include <AK/HashMap.h>
+#include <AK/LexicalPath.h>
#include <AK/LogStream.h>
#include <AK/ScopeGuard.h>
#include <LibCore/DirectoryWatcher.h>
#include <LibCoreDump/CoreDumpReader.h>
+#include <LibDebug/DebugInfo.h>
#include <LibELF/Loader.h>
#include <fcntl.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();
}
-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 };
@@ -73,17 +94,29 @@ static String symbolicate(FlatPtr eip, const ELF::Core::MemoryRegionInfo* region
path = name;
}
+ auto cached_value = s_debug_info_cache.get(path);
+ if (cached_value.has_value())
+ return cached_value.value();
+
struct stat st;
if (stat(path.characters(), &st)) {
- return {};
+ return nullptr;
}
- auto mapped_file = make<MappedFile>(path);
- if (!mapped_file->is_valid())
- return {};
+ MappedFile object_file(path);
+ if (!object_file.is_valid())
+ return nullptr;
+
+ auto loader = ELF::Loader::create((const u8*)object_file.data(), object_file.size());
+ Debug::DebugInfo debug_info(loader);
- auto loader = ELF::Loader::create((const u8*)mapped_file->data(), mapped_file->size());
- return loader->symbolicate(eip - region->region_start);
+ 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)
@@ -97,9 +130,19 @@ static String backtrace_line(const CoreDumpReader& coredump, FlatPtr eip)
if (region_name.contains("Loader.so"))
return {};
- auto func_name = symbolicate(eip, region);
+ auto object_info = object_info_for_region(region);
+ if (object_info.is_null())
+ return {};
+
+ 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", eip, object_name(region_name).characters(), func_name.is_null() ? "???" : func_name.characters());
+ 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)