Got a stack tracer working!!

This commit is contained in:
apio 2022-09-19 21:11:43 +02:00
parent 9d3030763b
commit ee8c2759a6
3 changed files with 54 additions and 2 deletions

View File

@ -0,0 +1,12 @@
#pragma once
#include <stdint.h>
struct StackTracer
{
StackTracer();
StackTracer(uintptr_t base_pointer);
void trace();
private:
uintptr_t m_base_pointer;
};

View File

@ -5,6 +5,7 @@
#include "log/Log.h"
#include "panic/hang.h"
#include "std/stdio.h"
#include "trace/StackTracer.h"
extern "C" void common_handler(SavedContext* context)
{
@ -15,14 +16,23 @@ extern "C" void common_handler(SavedContext* context)
}
if (context->number == 13)
{
kerrorln("General protection fault at RIP %lx, cs %ld, ss %ld, RSP %lx, error code %ld\n", context->rip,
kerrorln("General protection fault at RIP %lx, cs %ld, ss %ld, RSP %lx, error code %ld", context->rip,
context->cs, context->ss, context->rsp, context->error_code);
kinfoln("Stack trace:");
StackTracer tracer(context->rbp);
tracer.trace();
while (1) halt();
}
if (context->number == 14)
{
kerrorln("Page fault in %s (RIP %lx), while trying to access %lx, error code %ld\n",
kerrorln("Page fault in %s (RIP %lx), while trying to access %lx, error code %ld",
context->cs == 8 ? "ring 0" : "ring 3", context->rip, context->cr2, context->error_code);
kinfoln("Stack trace:");
StackTracer tracer(context->rbp);
tracer.trace();
hang();
}
if (context->number == 8)

View File

@ -0,0 +1,30 @@
#include "trace/StackTracer.h"
#include "std/stdio.h"
#include "trace/Resolve.h"
StackTracer::StackTracer()
{
asm("mov %%rbp, %0" : "=r"(m_base_pointer));
}
StackTracer::StackTracer(uintptr_t base_pointer) : m_base_pointer(base_pointer)
{
}
typedef struct stackframe
{
struct stackframe* next;
uintptr_t instruction;
} stackframe;
void StackTracer::trace()
{
stackframe* frame = (stackframe*)m_base_pointer;
while (frame)
{
char symbol_name[512];
get_symbol_name(frame->instruction, symbol_name);
printf("%lx: %s\n", frame->instruction, symbol_name);
frame = frame->next;
}
}