kernel: Lookup and print symbols for addresses in backtraces

This commit is contained in:
apio 2023-10-13 22:11:52 +02:00
parent 37e046d766
commit c75dbc0cbb
Signed by: apio
GPG Key ID: B8A7D06E42258954
7 changed files with 115 additions and 7 deletions

1
.gitignore vendored
View File

@ -2,6 +2,7 @@ Luna.iso
toolchain/
build/
initrd/boot/moon
initrd/ksyms
env-local.sh
initrd/bin/**
base/usr/bin/**

View File

@ -8,6 +8,7 @@ set(SOURCES
src/Log.cpp
src/Pledge.cpp
src/cxxabi.cpp
src/Symbols.cpp
src/video/Framebuffer.cpp
src/video/TextConsole.cpp
src/memory/MemoryManager.cpp
@ -150,4 +151,6 @@ target_include_directories(moon PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/gen)
target_link_options(moon PRIVATE LINKER:-T ${CMAKE_CURRENT_LIST_DIR}/moon.ld -nostdlib -nodefaultlibs)
add_custom_command(TARGET moon POST_BUILD COMMAND ${LUNA_ROOT}/tools/generate-symbols.sh)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/moon" DESTINATION ${LUNA_ROOT}/initrd/boot)

95
kernel/src/Symbols.cpp Normal file
View File

@ -0,0 +1,95 @@
#include "Symbols.h"
#include "Log.h"
#include "arch/MMU.h"
#include "boot/bootboot.h"
#include "fs/InitRD.h"
#include <luna/Scanf.h>
#include <luna/TarStream.h>
#include <luna/Vector.h>
extern const BOOTBOOT bootboot;
static bool g_symbols_loaded = false;
struct Symbol
{
u64 address;
char symbol[256];
};
Vector<Symbol> g_symbols;
extern const u8 kernel_start;
extern const u8 kernel_end;
static int sscanf(const char* str, const char* format, ...)
{
va_list ap;
va_start(ap, format);
int rc = scanf_impl(str, format, ap);
va_end(ap);
return rc;
}
namespace Symbols
{
Result<void> load()
{
const u64 virtual_initrd_address = MMU::translate_physical_address(bootboot.initrd_ptr);
TarStream stream;
stream.initialize((void*)virtual_initrd_address, bootboot.initrd_size);
TarStream::Entry file;
while (TRY(stream.read_next_entry(file)))
{
if (file.name.view() == "ksyms")
{
char* string = strtok(const_cast<char*>((const char*)file.data()), "\n");
if (!string) return {};
do {
Symbol symbol;
memset(symbol.symbol, 0, sizeof(symbol.symbol));
char unused;
int nread;
sscanf(string, "%lx %c %n", &symbol.address, &unused, &nread);
strlcpy(symbol.symbol, string + nread, sizeof(symbol));
TRY(g_symbols.try_append(symbol));
} while ((string = strtok(nullptr, "\n")));
kinfoln("Successfully loaded %zu kernel debug symbols", g_symbols.size());
g_symbols_loaded = true;
return {};
}
}
return {};
}
StringView lookup(u64 address)
{
if (!g_symbols_loaded) return StringView {};
if (address < (u64)&kernel_start) return StringView {};
if (address >= (u64)&kernel_end) return StringView {};
for (isize i = (isize)g_symbols.size() - 1; i >= 0; i--)
{
if (g_symbols[i].address < address)
{
usize index = i + 1;
if (index == g_symbols.size()) return StringView {};
return StringView { g_symbols[index].symbol };
}
}
return StringView {};
}
}

9
kernel/src/Symbols.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include <luna/StringView.h>
#include <luna/Types.h>
namespace Symbols
{
Result<void> load();
StringView lookup(u64 address);
}

View File

@ -1,5 +1,6 @@
#include "arch/CPU.h"
#include "Log.h"
#include "Symbols.h"
#include "api/Mouse.h"
#include "arch/Keyboard.h"
#include "arch/Timer.h"
@ -368,7 +369,7 @@ namespace CPU
rbp,
[](u64 instruction, void* arg) {
int* ptr = (int*)arg;
kinfoln("#%d at %p", *ptr, (void*)instruction);
kinfoln("#%d at %p in %s", *ptr, (void*)instruction, Symbols::lookup(instruction).chars());
(*ptr)++;
},
&frame_index);
@ -387,7 +388,7 @@ namespace CPU
regs,
[](u64 instruction, void* arg) {
int* ptr = (int*)arg;
kinfoln("#%d at %p", *ptr, (void*)instruction);
kinfoln("#%d at %p in %s", *ptr, (void*)instruction, Symbols::lookup(instruction).chars());
(*ptr)++;
},
&frame_index);

View File

@ -1,4 +1,5 @@
#include "Log.h"
#include "Symbols.h"
#include "arch/CPU.h"
#include "arch/Timer.h"
#include "binfmt/BinaryFormat.h"
@ -53,6 +54,8 @@ void oom_thread()
kinfoln("Current platform: %s", CPU::platform_string().chars());
kinfoln("Current processor: %s", CPU::identify().value_or("(unknown)"_sv).chars());
Symbols::load();
auto root = mark_critical(TmpFS::FileSystem::create(), "Failed to create initial ramfs");
mark_critical(VFS::mount_root(root), "Failed to mount the initial ramfs as the root filesystem");
mark_critical(InitRD::populate_vfs(), "Failed to load files from the initial ramdisk");

View File

@ -6,8 +6,4 @@ source $(dirname $0)/env.sh
cd $LUNA_ROOT
rm -f initrd/sys/moon.sym
nm -C -n initrd/boot/moon | grep -vE \\.Lubsan_data | awk '{ if ($2 != "a") print; }' | uniq > initrd/sys/moon.sym
chmod 400 initrd/sys/moon.sym
nm -C -n $LUNA_BUILD_DIR/kernel/moon | grep -vE \\.Lubsan_data | awk '{ if ($2 != "a") print; }' | uniq > initrd/ksyms