From 9e2483d78e09316a547368c133466ce167f3513c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Tue, 6 Mar 2018 10:59:23 +0100 Subject: [PATCH] Halt-And-Catch-Fire macro --- src/kernel/boot/kmain.c | 2 ++ src/kernel/include/debug.h | 14 ++++++++++++++ toolchain/gdbinit | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/src/kernel/boot/kmain.c b/src/kernel/boot/kmain.c index 50dd904..aaec318 100644 --- a/src/kernel/boot/kmain.c +++ b/src/kernel/boot/kmain.c @@ -14,6 +14,8 @@ void kmain() debug_ok("This thing worked well!\n"); debug_warning("Careful!\n"); debug_error("Something went wrong!\n"); + + PANIC("Reached end of kernel main function\n"); for(;;); } diff --git a/src/kernel/include/debug.h b/src/kernel/include/debug.h index 51a80ff..13bdf8d 100644 --- a/src/kernel/include/debug.h +++ b/src/kernel/include/debug.h @@ -22,3 +22,17 @@ void debug_printf(char *fmt, ...); void debug_puts(char *s); void debug_putsn(char *s, size_t n); void debug_putch(char c); + +#define S(x) #x +#define S_(x) S(x) +#define S__LINE__ S_(__LINE__) +#define PANIC(...) \ + do{ \ + debug("\n\nKernel panic!\n%s:%d\n", __FILE__, __LINE__); \ + debug(__VA_ARGS__); \ + volatile int _override = 0; \ + while(1){ \ + asm("panic_breakpoint_" S__LINE__ ":"); \ + if(_override) break; \ + } \ + }while(0) diff --git a/toolchain/gdbinit b/toolchain/gdbinit index 003a4c9..8f8ed31 100644 --- a/toolchain/gdbinit +++ b/toolchain/gdbinit @@ -11,6 +11,24 @@ target remote :1234 set height 0 set width 0 +# The PANIC() macro - defined in src/kernel/include/debug.h - creates +# a label of the form panic_breakpoint_xxx, where xxx is a number. +# Unfortunately, gdb can set breakpoints on FUNCTIONS based on regex, but +# not on LABELS. +# The following piece of python code runs objdump to extract all panic_breakpoint +# labels, and set breakpoints for each. +python +import subprocess +import os +dump = subprocess.Popen(("objdump", "-t", os.environ['BUILDROOT'] + "sysroot/kernel"), stdout=subprocess.PIPE) +lines = subprocess.check_output(('grep', 'panic_breakpoint'), stdin=dump.stdout) +dump.wait() +for line in lines.split('\n'): + name = line.split(' ')[-1] + if name: + gdb.execute('b ' + name, to_string=True) +end + define q monitor quit end