arch/x86_64: Refactor the stack tracing code to remove duplicate code
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2022-12-21 17:21:01 +01:00
parent 7a2cb28475
commit 2e8ea724a0
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -184,11 +184,9 @@ namespace CPU
u64 instruction; u64 instruction;
}; };
void get_stack_trace(void (*callback)(u64, void*), void* arg) static void backtrace_impl(u64 base_pointer, void (*callback)(u64, void*), void* arg)
{ {
u64 rbp; StackFrame* current_frame = (StackFrame*)base_pointer;
asm volatile("mov %%rbp, %0" : "=r"(rbp));
StackFrame* current_frame = (StackFrame*)rbp;
// FIXME: Validate that the frame itself is readable, might span across multiple pages // FIXME: Validate that the frame itself is readable, might span across multiple pages
while (current_frame && MemoryManager::validate_readable_page((u64)current_frame)) while (current_frame && MemoryManager::validate_readable_page((u64)current_frame))
{ {
@ -197,30 +195,32 @@ namespace CPU
} }
} }
void get_stack_trace(void (*callback)(u64, void*), void* arg)
{
u64 rbp;
asm volatile("mov %%rbp, %0" : "=r"(rbp));
return backtrace_impl(rbp, callback, arg);
}
void print_stack_trace() void print_stack_trace()
{ {
u64 rbp; u64 rbp;
int frame_index = 0; int frame_index = 0;
asm volatile("mov %%rbp, %0" : "=r"(rbp)); asm volatile("mov %%rbp, %0" : "=r"(rbp));
StackFrame* current_frame = (StackFrame*)rbp; return backtrace_impl(
// FIXME: Validate that the frame itself is readable, might span across multiple pages rbp,
while (current_frame && MemoryManager::validate_readable_page((u64)current_frame)) [](u64 instruction, void* arg) {
{ int* ptr = (int*)arg;
kinfoln("#%d at %p", frame_index++, (void*)current_frame->instruction); kinfoln("#%d at %p", *ptr, (void*)instruction);
current_frame = current_frame->next; (*ptr)++;
} },
&frame_index);
} }
void get_stack_trace_at(Registers* regs, void (*callback)(u64, void*), void* arg) void get_stack_trace_at(Registers* regs, void (*callback)(u64, void*), void* arg)
{ {
callback(regs->rip, arg); callback(regs->rip, arg);
StackFrame* current_frame = (StackFrame*)regs->rbp; return backtrace_impl(regs->rbp, callback, arg);
// FIXME: Validate that the frame itself is readable, might span across multiple pages
while (current_frame && MemoryManager::validate_readable_page((u64)current_frame))
{
callback(current_frame->instruction, arg);
current_frame = current_frame->next;
}
} }
void print_stack_trace_at(Registers* regs) void print_stack_trace_at(Registers* regs)