diff --git a/kernel/src/arch/CPU.h b/kernel/src/arch/CPU.h index ac05ed6a..edf51cba 100644 --- a/kernel/src/arch/CPU.h +++ b/kernel/src/arch/CPU.h @@ -22,5 +22,7 @@ namespace CPU void wait_for_interrupt(); void get_stack_trace(void (*callback)(u64, void*), void* arg); + void print_stack_trace(); void get_stack_trace_at(Registers* regs, void (*callback)(u64, void*), void* arg); + void print_stack_trace_at(Registers* regs); } \ No newline at end of file diff --git a/kernel/src/arch/x86_64/CPU.cpp b/kernel/src/arch/x86_64/CPU.cpp index 8d8476c9..8654fe38 100644 --- a/kernel/src/arch/x86_64/CPU.cpp +++ b/kernel/src/arch/x86_64/CPU.cpp @@ -303,15 +303,7 @@ static void setup_idt() asm volatile("mov %%cr2, %0" : "=r"(cr2)); kerrorln("Page fault at RIP %lx while accessing %lx!", regs->rip, cr2); - int frame_index = 0; - CPU::get_stack_trace_at( - regs, - [](u64 instruction, void* arg) { - int* ptr = (int*)arg; - kinfoln("#%d at %p", *ptr, (void*)instruction); - (*ptr)++; - }, - &frame_index); + CPU::print_stack_trace_at(regs); CPU::efficient_halt(); } @@ -476,6 +468,20 @@ namespace CPU } } + void print_stack_trace() + { + u64 rbp; + int frame_index = 0; + 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 + while (current_frame && MemoryManager::validate_readable_page((u64)current_frame)) + { + kinfoln("#%d at %p", frame_index++, (void*)current_frame->instruction); + current_frame = current_frame->next; + } + } + void get_stack_trace_at(Registers* regs, void (*callback)(u64, void*), void* arg) { callback(regs->rip, arg); @@ -487,6 +493,19 @@ namespace CPU current_frame = current_frame->next; } } + + void print_stack_trace_at(Registers* regs) + { + int frame_index = 0; + get_stack_trace_at( + regs, + [](u64 instruction, void* arg) { + int* ptr = (int*)arg; + kinfoln("#%d at %p", *ptr, (void*)instruction); + (*ptr)++; + }, + &frame_index); + } } // called by kernel_yield