rulimine/stage23/lib/trace.s2.c

71 lines
1.6 KiB
C
Raw Normal View History

2020-11-15 19:56:10 +03:00
#include <stddef.h>
#include <stdint.h>
#include <lib/trace.h>
#include <lib/blib.h>
#include <lib/config.h>
#include <lib/print.h>
#include <lib/uri.h>
#include <fs/file.h>
#include <mm/pmm.h>
2021-02-25 03:24:54 +03:00
extern symbol limine_map;
2020-11-15 19:56:10 +03:00
char *trace_address(size_t *off, size_t addr) {
2021-03-02 12:23:43 +03:00
#if defined (bios)
2021-02-25 03:24:54 +03:00
if (!stage3_loaded)
2020-11-15 19:56:10 +03:00
return NULL;
2021-03-02 12:23:43 +03:00
#endif
2020-11-15 19:56:10 +03:00
2021-03-02 12:23:43 +03:00
uintptr_t prev_addr = 0;
char *prev_sym = NULL;
2020-11-15 19:56:10 +03:00
for (size_t i = 0;;) {
2021-03-02 12:23:43 +03:00
if (*((uintptr_t *)&limine_map[i]) >= addr) {
2020-11-15 19:56:10 +03:00
*off = addr - prev_addr;
return prev_sym;
}
2021-03-02 12:23:43 +03:00
prev_addr = *((uintptr_t *)&limine_map[i]);
i += sizeof(uintptr_t);
2021-02-25 03:24:54 +03:00
prev_sym = &limine_map[i];
while (limine_map[i++] != 0);
2020-11-15 19:56:10 +03:00
}
}
void print_stacktrace(size_t *base_ptr) {
2021-03-02 12:23:43 +03:00
#if defined (bios)
2021-02-25 03:24:54 +03:00
if (!stage3_loaded) {
print("trace: Stack trace omitted because stage 3 was not loaded yet.\n");
return;
}
2021-03-02 12:23:43 +03:00
#endif
2020-11-15 19:56:10 +03:00
if (base_ptr == NULL) {
asm volatile (
2021-03-02 12:23:43 +03:00
#if defined (bios)
2020-11-15 19:56:10 +03:00
"mov %0, ebp"
2021-03-02 12:23:43 +03:00
#elif defined (uefi)
"mov %0, rbp"
#endif
2020-11-15 19:56:10 +03:00
: "=g"(base_ptr)
:: "memory"
);
}
print("Stacktrace:\n");
for (;;) {
size_t old_bp = base_ptr[0];
size_t ret_addr = base_ptr[1];
if (!ret_addr)
break;
size_t off;
char *name = trace_address(&off, ret_addr);
if (name)
print(" [%x] <%s+%x>\n", ret_addr, name, off);
else
print(" [%x]\n", ret_addr);
if (!old_bp)
break;
base_ptr = (void*)old_bp;
}
print("End of trace.\n");
}