From 22f25a9e4feddbc093ae01cd53bdda3c2c3a82e9 Mon Sep 17 00:00:00 2001 From: Craig Barnes Date: Fri, 15 Jan 2021 20:39:45 +0000 Subject: Print stack trace on assert() failure or when calling fatal_error() Note: this uses the __sanitizer_print_stack_trace() function from the AddressSanitizer runtime, so it only works when AddressSanitizer is in use. --- debug.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 debug.h (limited to 'debug.h') diff --git a/debug.h b/debug.h new file mode 100644 index 00000000..ba71c047 --- /dev/null +++ b/debug.h @@ -0,0 +1,32 @@ +#pragma once + +#include "macros.h" + +#ifdef NDEBUG + #define BUG(...) UNREACHABLE() +#else + #define BUG(...) bug(__FILE__, __LINE__, __func__, __VA_ARGS__) +#endif + +#undef assert + +#define assert(x) do { \ + IGNORE_WARNING("-Wtautological-compare") \ + if (unlikely(!(x))) { \ + BUG("assertion failed: '%s'", #x); \ + } \ + UNIGNORE_WARNINGS \ +} while (0) + +#ifndef static_assert + #if __STDC_VERSION__ >= 201112L + #define static_assert(x, msg) _Static_assert((x), msg) + #elif GNUC_AT_LEAST(4, 6) || HAS_EXTENSION(c_static_assert) + #define static_assert(x, msg) __extension__ _Static_assert((x), msg) + #else + #define static_assert(x, msg) + #endif +#endif + +noreturn void fatal_error(const char *msg, int err) COLD; +noreturn void bug(const char *file, int line, const char *func, const char *fmt, ...) PRINTF(4) COLD; -- cgit v1.3