x86_64: print stack trace during panic

This commit is contained in:
2023-05-06 22:18:34 +01:00
parent 90afd997e6
commit 6cf8f4234a
4 changed files with 75 additions and 3 deletions

View File

@@ -1,3 +1,5 @@
#include "socks/machine/panic.h"
#include "socks/vm.h"
#include <socks/printk.h>
#include <socks/libc/stdio.h>
#include <arch/irq.h>
@@ -19,6 +21,11 @@
#define R_ID 21
#define R_MAX 21
struct stack_frame {
uintptr_t rbp;
uintptr_t rip;
} __attribute__((packed));
const char *pf_rfl_name(int bit)
{
switch (bit) {
@@ -112,7 +119,61 @@ void ml_print_cpu_state(struct cpu_context *ctx)
printk(" cr3 %016llx cr4 %016llx", cr3, cr4);
}
void ml_print_stack_trace(struct cpu_context *ctx)
static void print_stack_item(uintptr_t addr)
{
if (!addr) {
return;
}
char buf[64];
size_t i = 0;
i += snprintf(buf, sizeof(buf), " [<%p>] ", addr);
size_t offset = 0;
char name[128];
int found = -1;
if (found == 0 && name[0] != '\0') {
i += snprintf(buf + i, sizeof(buf) - i, "%s+0x%lx", name, offset);
} else {
i += snprintf(buf + i, sizeof(buf) - i, "?");
}
printk("%s", buf);
}
static void print_stack_trace(uintptr_t ip, uintptr_t *bp)
{
struct stack_frame *stk = (struct stack_frame *)bp;
printk("call trace:");
print_stack_item(ip);
int max_frames = 10, current_frame = 0;
while (1) {
if (!vm_virt_to_phys(stk) ||
bp == NULL ||
current_frame > max_frames) {
break;
}
uintptr_t addr = stk->rip;
print_stack_item(addr);
stk = (struct stack_frame *)stk->rbp;
current_frame++;
}
}
void ml_print_stack_trace(uintptr_t ip)
{
uintptr_t *bp;
asm volatile("mov %%rbp, %0" : "=r" (bp));
print_stack_trace(ip, bp);
}
void ml_print_stack_trace_irq(struct cpu_context *ctx)
{
print_stack_trace(ctx->rip, (uintptr_t *)ctx->rbp);
}