Skip to content

Commit 2b6283a

Browse files
committed
[exception] Print stack trace instruction pointer on crash
1 parent 445177c commit 2b6283a

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/kernel/interrupts/exceptions.asm

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
extern interrupt_handler_0x00_0x1F_exception
66

7+
global at_stack
78
global _interrupt_handler_0x00_exception
89
global _interrupt_handler_0x01_exception
910
global _interrupt_handler_0x02_exception
@@ -69,6 +70,22 @@ global _interrupt_handler_0x1F_exception
6970
iret
7071
%endmacro
7172

73+
at_stack:
74+
push ebp
75+
mov ebp, esp
76+
77+
push ds
78+
mov eax, ss
79+
mov ds, eax
80+
81+
mov esi, [ebp+8] ; stack pointer
82+
mov eax, [esi]
83+
84+
pop ds
85+
86+
pop ebp
87+
ret
88+
7289
create_low__interrupt_handler_xy_exception_nohup 0x00 ; divide-by-zero
7390
create_low__interrupt_handler_xy_exception 0x01
7491
create_low__interrupt_handler_xy_exception 0x02

src/kernel/interrupts/exceptions.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ struct exceptionContext {
5050
int ip, cs, eflag;
5151
};
5252

53+
int at_stack(int esp);
54+
5355
void interrupt_handler_0x00_0x1F_exception(struct exceptionContext context) {
5456
// written for handler with error code otherwise ip, cs and eflag will skew.
5557
const int gdte_size = sizeof(struct GDTEntry);
@@ -74,13 +76,14 @@ void interrupt_handler_0x00_0x1F_exception(struct exceptionContext context) {
7476
// stack trace
7577
{
7678
const int stack_trace_max_depth = 5;
77-
int last_ebp = context.ebp;
79+
int ebp = context.ebp;
7880
// stack[ebp] => previous_ebp, if previous_ebp == 0 then break;
79-
for(int i=0; i<stack_trace_max_depth; i++) {
80-
// to be implemented
81-
break;
81+
print_log("stack trace: \n - %x", context.ip);
82+
for(int i=0; ebp>0 && i<stack_trace_max_depth; i++) {
83+
int return_address = at_stack(ebp+4);
84+
print_log(" - %x (ni)", return_address);
85+
ebp = at_stack(ebp);
8286
}
83-
8487
}
8588

8689

0 commit comments

Comments
 (0)