From 7f92690f84e6f4220430fb30267f44ec19ce9a97 Mon Sep 17 00:00:00 2001 From: fef Date: Sun, 21 Nov 2021 06:57:24 +0100 Subject: [PATCH] panic: print stack trace when panicking --- arch/x86/sys/amd64/CMakeLists.txt | 1 + arch/x86/sys/amd64/ktrace.c | 18 ++++++++++++++++++ include/gay/ktrace.h | 6 ++++++ include/gay/systm.h | 3 ++- kernel/panic.c | 3 +++ 5 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 arch/x86/sys/amd64/ktrace.c create mode 100644 include/gay/ktrace.h diff --git a/arch/x86/sys/amd64/CMakeLists.txt b/arch/x86/sys/amd64/CMakeLists.txt index 0ee3c06..9068a0e 100644 --- a/arch/x86/sys/amd64/CMakeLists.txt +++ b/arch/x86/sys/amd64/CMakeLists.txt @@ -3,6 +3,7 @@ target_sources(gay_arch PRIVATE idt.S irq.S + ktrace.c switch.S systm.c trap.S diff --git a/arch/x86/sys/amd64/ktrace.c b/arch/x86/sys/amd64/ktrace.c new file mode 100644 index 0000000..42e5c13 --- /dev/null +++ b/arch/x86/sys/amd64/ktrace.c @@ -0,0 +1,18 @@ +/* Copyright (C) 2021 fef . All rights reserved. */ + +#include + +#include +#include + +void ktrace_print(void) +{ + void **rbp; + __asm__ volatile("movq (%%rbp), %0" : "=r"(rbp)); + + kprintf("Stack trace:\n"); + while (rbp >= (void **)KERNBASE) { + kprintf(" %p\n", rbp[1]); + rbp = *rbp; + } +} diff --git a/include/gay/ktrace.h b/include/gay/ktrace.h new file mode 100644 index 0000000..2c4778c --- /dev/null +++ b/include/gay/ktrace.h @@ -0,0 +1,6 @@ +/* Copyright (C) 2021 fef . All rights reserved. */ + +#pragma once + +/** @brief Print a full stack trace to the kernel log, starting from the caller. */ +void ktrace_print(void); diff --git a/include/gay/systm.h b/include/gay/systm.h index d4b91f9..139017f 100644 --- a/include/gay/systm.h +++ b/include/gay/systm.h @@ -2,6 +2,7 @@ #pragma once +#include #include #include @@ -72,7 +73,7 @@ static inline void critical_leave(void) static inline bool in_critical(void) { - if (in_irq()) + if (in_irq() || !intr_enabled()) return true; return current->critnest > 0; } diff --git a/kernel/panic.c b/kernel/panic.c index 18a09ba..3ddc0a3 100644 --- a/kernel/panic.c +++ b/kernel/panic.c @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -22,6 +23,8 @@ void panic(const char *fmt, ...) else kvprintf(fmt, args); + ktrace_print(); + kprintf("\nKernel version: %s\nSystem halted", GAY_VERSION_STR); /* no need for va_end() here i guess */