Add backtrace function

This commit is contained in:
Mathieu Maret 2018-11-16 14:47:21 +01:00
parent 50fa9b7d24
commit 085857a900
3 changed files with 35 additions and 2 deletions

View File

@ -2,9 +2,11 @@
CPPFLAGS = -MMD
AS=nasm
ASFLAGS += -f elf32
#LDFLAGS += -m32 -nostdlib -mkernel -fno-stack-protector
LDFLAGS += -m32 -nostdlib -static -fno-common -fno-use-cxa-atexit -fno-exceptions -fno-non-call-exceptions -fno-weak -fno-rtti -fno-stack-protector
CFLAGS += -m32 -Wall -Wextra -Werror -ffreestanding -fno-exceptions -fno-pie -fno-stack-protector
CXXFLAGS += -m32 -Wall -Wextra -Werror -ffreestanding -fno-exceptions -fno-rtti -fno-pie
DEBUG_FLAGS += -g -Og -DDEBUG -fno-omit-frame-pointer
SUBDIRS := core drivers tests
@ -44,8 +46,8 @@ self_test: clean kernel
test:kernel
qemu-system-x86_64 -kernel $<
debug: CFLAGS += -g -Og
debug: CXXFLAGS += -g -Og
debug: CFLAGS += $(DEBUG_FLAGS)
debug: CXXFLAGS += $(DEBUG_FLAGS)
debug:kernel kernel.sym
qemu-system-x86_64 -s -S -kernel kernel&
gdb -s kernel.sym -ex "target remote localhost:1234"

28
core/stack.c Normal file
View File

@ -0,0 +1,28 @@
#include "stack.h"
#include "vga.h"
void printStackTrace(unsigned int maxFrames){
#ifdef DEBUG
// Now on Stack:
// ( potential second function argument )
// first function argument (maxFrames)
// return address from caller
// EBP (Extended Base Pointer) of calling function
unsigned int * ebp = &maxFrames - 2;
for (unsigned int frame = 0 ; frame < maxFrames; frame ++){
unsigned int eip = ebp [1];
if (eip == 0){
// No caller on stack
break;
}
ebp = (unsigned int *)(ebp[0]);
//unsigned int * arguments = &ebp[2];
printf(" 0x%x\n", eip);
}
#else
printf("Must be compiled with -fno-omit-frame-pointer for full stack\n");
unsigned int * ebp = &maxFrames - 2;
unsigned int eip = ebp [1];
printf("Caller: 0x%x\n", eip);
#endif
}

3
core/stack.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
void printStackTrace(unsigned int maxFrame);