From 085857a900ce5a890ea06a35cd9f5e2aeea6d63c Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Fri, 16 Nov 2018 14:47:21 +0100 Subject: [PATCH] Add backtrace function --- Makefile | 6 ++++-- core/stack.c | 28 ++++++++++++++++++++++++++++ core/stack.h | 3 +++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 core/stack.c create mode 100644 core/stack.h diff --git a/Makefile b/Makefile index c64f685..6eb59de 100644 --- a/Makefile +++ b/Makefile @@ -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" diff --git a/core/stack.c b/core/stack.c new file mode 100644 index 0000000..b6a79ab --- /dev/null +++ b/core/stack.c @@ -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 +} diff --git a/core/stack.h b/core/stack.h new file mode 100644 index 0000000..a0265cd --- /dev/null +++ b/core/stack.h @@ -0,0 +1,3 @@ +#pragma once + +void printStackTrace(unsigned int maxFrame);