diff options
| author | Craig Barnes <craigbarnes@protonmail.com> | 2021-01-15 20:39:45 +0000 |
|---|---|---|
| committer | Craig Barnes <craigbarnes@protonmail.com> | 2021-01-16 19:56:33 +0000 |
| commit | 22f25a9e4feddbc093ae01cd53bdda3c2c3a82e9 (patch) | |
| tree | 4d0c9bce90d0f0d04c22e9f88b52f910695cfaf6 /debug.c | |
| parent | Merge branch 'decset-1047-and-1048' (diff) | |
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.
Diffstat (limited to 'debug.c')
| -rw-r--r-- | debug.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/debug.c b/debug.c new file mode 100644 index 00000000..03ead23b --- /dev/null +++ b/debug.c @@ -0,0 +1,48 @@ +#include "debug.h" + +#include <errno.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <syslog.h> + +#if defined(__SANITIZE_ADDRESS__) || HAS_FEATURE(address_sanitizer) +#include <sanitizer/asan_interface.h> +#define ASAN_ENABLED 1 +#endif + +static void +print_stack_trace(void) +{ +#ifdef ASAN_ENABLED + fputs("\nStack trace:\n", stderr); + __sanitizer_print_stack_trace(); +#endif +} + +noreturn void +fatal_error(const char *msg, int err) +{ + syslog(LOG_ERR, "%s: %s", msg, strerror(err)); + errno = err; + perror(msg); + print_stack_trace(); + abort(); +} + +noreturn void +bug(const char *file, int line, const char *func, const char *fmt, ...) +{ + fprintf(stderr, "\n%s:%d: BUG in %s(): ", file, line, func); + + va_list ap; + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + fputc('\n', stderr); + print_stack_trace(); + fflush(stderr); + abort(); +} |
