dbg: map addresses to file+function+offset

Add support for mapping kernel .text addresses into file + function +
offset for debugging purposes. Make the backtrace use this new feature.
This commit is contained in:
Matheus
2026-10-09 01:46:15 -03:00
parent b52882f3f7
commit ac6addd527
3 changed files with 60 additions and 1 deletions
+8
View File
@@ -0,0 +1,8 @@
#ifndef _DBG_H
#define _DBG_H
#include <stddef.h>
void dbg_get_symbol(void *rip, char **fn_name, char **file_name, size_t *offset);
#endif
+45
View File
@@ -0,0 +1,45 @@
#include <dbg.h>
#include <stddef.h>
#include <stdint.h>
#ifdef __x86_64__
#define LOAD_BASE (uint64_t)0xffffffff80000000
#endif
#define SYM_TABLE_ENTRY_SIZE 3
extern uint32_t dbg_sym_table[];
extern uint32_t dbg_sym_table_end[];
extern char dbg_fn_name_table[];
extern char dbg_fn_name_table_end[];
extern char dbg_file_name_table[];
extern char dbg_file_name_table_end[];
void dbg_get_symbol(void *rip, char **fn_name, char **file_name, size_t *offset) {
*fn_name = "??";
*file_name = "??";
*offset = 0;
if ((uint64_t)rip < LOAD_BASE)
return;
uint64_t rip_offset = (uint64_t)rip - LOAD_BASE;
size_t low = 0;
size_t high = (dbg_sym_table_end - dbg_sym_table) / SYM_TABLE_ENTRY_SIZE;
while (low < high) {
size_t mid = low + (high - low) / 2;
if (dbg_sym_table[mid * SYM_TABLE_ENTRY_SIZE] <= rip_offset)
low = mid + 1;
else
high = mid;
}
if (low == 0)
return;
uint32_t *symbol = dbg_sym_table + (low - 1) * SYM_TABLE_ENTRY_SIZE;
*fn_name = dbg_fn_name_table + symbol[1];
*file_name = dbg_file_name_table + symbol[2];
*offset = rip_offset - symbol[0];
}
+7 -1
View File
@@ -4,11 +4,17 @@
#include <printf.h>
#include <kernel/interrupt.h>
#include <arch/backtrace.h>
#include <dbg.h>
static bool paniced = false;
static bool step_backtrace(void *, void *ip) {
printf("%p\n", ip);
char *fn, *file;
size_t off;
dbg_get_symbol(ip, &fn, &file, &off);
printf("%p: %s:%s+%x\n", ip, file, fn, off);
return true;
}