From 6b34691abe1a77e27f2898c919f9889c5231710b Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 13 Oct 2022 17:42:49 +0200 Subject: [PATCH] core/assert: print backtrace on failed assertion --- core/lib/assert.c | 10 ++++++++++ core/lib/include/assert.h | 3 +++ 2 files changed, 13 insertions(+) diff --git a/core/lib/assert.c b/core/lib/assert.c index bf5607bdaa..fcbed9743e 100644 --- a/core/lib/assert.c +++ b/core/lib/assert.c @@ -19,16 +19,26 @@ #include "architecture.h" #include "cpu.h" #include "panic.h" +#if IS_USED(MODULE_BACKTRACE) +#include "backtrace.h" +#endif __NORETURN void _assert_failure(const char *file, unsigned line) { printf("%s:%u => ", file, line); +#if IS_USED(MODULE_BACKTRACE) + printf("failed assertion. Backtrace:\n"); + backtrace_print(); +#endif core_panic(PANIC_ASSERT_FAIL, "FAILED ASSERTION."); } __NORETURN void _assert_panic(void) { printf("%" PRIxTXTPTR "\n", cpu_get_caller_pc()); +#if IS_USED(MODULE_BACKTRACE) + backtrace_print(); +#endif core_panic(PANIC_ASSERT_FAIL, "FAILED ASSERTION."); } diff --git a/core/lib/include/assert.h b/core/lib/include/assert.h index 1e328596df..35d7cbac37 100644 --- a/core/lib/include/assert.h +++ b/core/lib/include/assert.h @@ -108,6 +108,9 @@ __NORETURN void _assert_failure(const char *file, unsigned line); * or `gdb` (with the command `info line *(0x89abcdef)`) to identify the line * the assertion failed in. * + * If the `backtrace` module is enabled (and implemented for architecture in use) + * a backtrace will be printed in addition to the location of the failed assertion. + * * @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/assert.html */ #define assert(cond) ((cond) ? (void)0 : _assert_failure(RIOT_FILE_RELATIVE, \