Add convenience functions to print the stack trace directly
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2022-12-07 18:11:24 +01:00
parent 0d437cfcca
commit c8302a4fef
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 30 additions and 9 deletions

View File

@ -22,5 +22,7 @@ namespace CPU
void wait_for_interrupt(); void wait_for_interrupt();
void get_stack_trace(void (*callback)(u64, void*), void* arg); 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 get_stack_trace_at(Registers* regs, void (*callback)(u64, void*), void* arg);
void print_stack_trace_at(Registers* regs);
} }

View File

@ -303,15 +303,7 @@ static void setup_idt()
asm volatile("mov %%cr2, %0" : "=r"(cr2)); asm volatile("mov %%cr2, %0" : "=r"(cr2));
kerrorln("Page fault at RIP %lx while accessing %lx!", regs->rip, cr2); kerrorln("Page fault at RIP %lx while accessing %lx!", regs->rip, cr2);
int frame_index = 0; CPU::print_stack_trace_at(regs);
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::efficient_halt(); 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) void get_stack_trace_at(Registers* regs, void (*callback)(u64, void*), void* arg)
{ {
callback(regs->rip, arg); callback(regs->rip, arg);
@ -487,6 +493,19 @@ namespace CPU
current_frame = current_frame->next; 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 // called by kernel_yield