Skip to content

Commit f56cdf4

Browse files
committed
added support for modules to stack trace
1 parent 85297d0 commit f56cdf4

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

mckrnl/core/include/module.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,18 @@ typedef struct module {
2121
elf_object_context_t ctx;
2222
} module_t;
2323

24+
#define MAX_MODULES 32
25+
26+
27+
extern module_t* loaded_modules[MAX_MODULES];
28+
extern int loaded_module_count;
29+
2430
#define define_module(name, init, stage_driver, stage_mount) module_t __module__ = { name, init, stage_driver, stage_mount };
2531

2632
module_t* load_object_file(void* image);
2733
void* resolve_symbol_address(elf_object_context_t* ctx, const char* symbol_name);
34+
char* resolve_symbol_name(elf_object_context_t* ctx, void* address);
35+
2836
void initrd_load_modules(void* saf_image, char* path);
2937

3038
void stage_driver();

mckrnl/core/module.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <stddef.h>
77
#include <stdio.h>
88

9-
module_t* loaded_modules[32] = { 0 };
9+
module_t* loaded_modules[MAX_MODULES] = { 0 };
1010
int loaded_module_count = 0;
1111

1212
void* resolve_symbol_address(elf_object_context_t* ctx, const char* symbol_name) {
@@ -29,6 +29,27 @@ void* resolve_symbol_address(elf_object_context_t* ctx, const char* symbol_name)
2929
return NULL;
3030
}
3131

32+
char* resolve_symbol_name(elf_object_context_t* ctx, void* address) {
33+
for (uint32_t i = 0; i < ctx->header->sh_entry_count; i++) {
34+
if (ctx->section_headers[i].type != SHT_SYMTAB) {
35+
continue;
36+
}
37+
38+
struct elf_symbol* symbol_table = (struct elf_symbol*)((uint8_t*)ctx->image + ctx->section_headers[i].offset);
39+
uint32_t symbol_count = ctx->section_headers[i].size / sizeof(struct elf_symbol);
40+
char* string_table = (char*)ctx->image + ctx->section_headers[ctx->section_headers[i].link].offset;
41+
42+
for (uint32_t j = 0; j < symbol_count; j++) {
43+
void* sym_addr = (uint8_t*)ctx->load_base + ctx->section_addresses[symbol_table[j].shndx] + symbol_table[j].value;
44+
if(address >= sym_addr && address < sym_addr + symbol_table[j].size) {
45+
return string_table + symbol_table[j].name;
46+
}
47+
}
48+
}
49+
50+
return NULL;
51+
}
52+
3253
module_t* load_object_file(void* image) {
3354
struct elf_header* header = image;
3455
if (header->magic != ELF_MAGIC || header->sh_entry_count == 0) {

mckrnl/core/utils/stdio.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <utils/trace.h>
1616
#include <gdb/gdb.h>
1717

18+
#include <module.h>
19+
1820
char_output_driver_t* debugf_driver = NULL;
1921
char_output_driver_t* printf_driver = NULL;
2022

@@ -86,6 +88,16 @@ void stacktrace_print(int frame_num, uint32_t eip) {
8688
uint32_t symbol_start = resolve_symbol_from_name(symbol);
8789
printf("[ 0x%x ] <%s + %d>\n", eip, symbol, eip - symbol_start);
8890
} else {
91+
for (int i = 0; i < loaded_module_count; i++) {
92+
module_t* module = loaded_modules[i];
93+
char* mod_symbol = resolve_symbol_name(&module->ctx, (void*)eip);
94+
if (mod_symbol) {
95+
uint32_t symbol_start = (uint32_t) resolve_symbol_address(&module->ctx, mod_symbol);
96+
printf("[ 0x%x (%s) ] <%s + %d>\n", eip, module->name, mod_symbol, eip - symbol_start);
97+
return;
98+
}
99+
}
100+
89101
printf("[ 0x%x ] <unknown>\n", eip);
90102
}
91103
}

0 commit comments

Comments
 (0)