2020-11-15 19:56:10 +03:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <lib/trace.h>
|
2022-08-27 00:44:47 +03:00
|
|
|
#include <lib/misc.h>
|
2020-11-15 19:56:10 +03:00
|
|
|
#include <lib/config.h>
|
|
|
|
#include <lib/print.h>
|
|
|
|
#include <lib/uri.h>
|
|
|
|
#include <fs/file.h>
|
|
|
|
#include <mm/pmm.h>
|
|
|
|
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (BIOS)
|
2021-04-08 02:15:35 +03:00
|
|
|
extern symbol stage2_map;
|
2022-09-02 03:29:12 +03:00
|
|
|
#elif defined (UEFI)
|
2023-09-11 12:27:02 +03:00
|
|
|
extern symbol __slide;
|
2021-04-08 02:15:35 +03:00
|
|
|
#endif
|
2021-03-13 05:40:17 +03:00
|
|
|
|
2021-04-08 02:15:35 +03:00
|
|
|
extern symbol full_map;
|
2020-11-15 19:56:10 +03:00
|
|
|
|
2021-03-13 05:40:17 +03:00
|
|
|
static char *trace_address(size_t *off, size_t addr) {
|
2021-03-12 07:19:56 +03:00
|
|
|
char *limine_map;
|
|
|
|
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (BIOS)
|
2021-02-25 03:24:54 +03:00
|
|
|
if (!stage3_loaded)
|
2021-03-12 07:19:56 +03:00
|
|
|
limine_map = stage2_map;
|
|
|
|
else
|
|
|
|
limine_map = full_map;
|
2022-09-02 03:29:12 +03:00
|
|
|
#elif defined (UEFI)
|
2021-04-08 02:15:35 +03:00
|
|
|
limine_map = full_map;
|
|
|
|
|
2023-09-11 12:27:02 +03:00
|
|
|
addr -= (size_t)__slide;
|
2021-04-08 02:15:35 +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) {
|
|
|
|
if (base_ptr == NULL) {
|
|
|
|
asm volatile (
|
2021-07-20 14:35:43 +03:00
|
|
|
#if defined (__i386__)
|
2021-05-29 19:07:28 +03:00
|
|
|
"movl %%ebp, %0"
|
2021-07-20 14:35:43 +03:00
|
|
|
#elif defined (__x86_64__)
|
2021-05-29 19:07:28 +03:00
|
|
|
"movq %%rbp, %0"
|
2022-08-18 18:32:54 +03:00
|
|
|
#elif defined (__aarch64__)
|
|
|
|
"mov %0, x29"
|
2023-06-04 02:36:06 +03:00
|
|
|
#elif defined (__riscv64)
|
|
|
|
"mv %0, fp; addi %0, %0, -16"
|
2021-04-08 02:15:35 +03:00
|
|
|
#endif
|
2022-08-18 18:32:54 +03:00
|
|
|
: "=r"(base_ptr)
|
2020-11-15 19:56:10 +03:00
|
|
|
:: "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)
|
2021-12-10 05:18:19 +03:00
|
|
|
print(" [%p] <%s+%p>\n", ret_addr, name, off);
|
2020-11-15 19:56:10 +03:00
|
|
|
else
|
2021-12-10 05:18:19 +03:00
|
|
|
print(" [%p]\n", ret_addr);
|
2020-11-15 19:56:10 +03:00
|
|
|
if (!old_bp)
|
|
|
|
break;
|
2023-06-04 02:36:06 +03:00
|
|
|
#if defined (__riscv)
|
|
|
|
base_ptr = (void *)(old_bp - 16);
|
|
|
|
#else
|
2020-11-15 19:56:10 +03:00
|
|
|
base_ptr = (void*)old_bp;
|
2023-06-04 02:36:06 +03:00
|
|
|
#endif
|
2020-11-15 19:56:10 +03:00
|
|
|
}
|
2021-05-19 09:19:53 +03:00
|
|
|
print("End of trace. ");
|
2020-11-15 19:56:10 +03:00
|
|
|
}
|