mirror of
https://codeberg.org/dnkl/foot.git
synced 2026-02-04 04:06:06 -05:00
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.
This commit is contained in:
parent
bcf46d9eab
commit
22f25a9e4f
23 changed files with 125 additions and 37 deletions
48
debug.c
Normal file
48
debug.c
Normal file
|
|
@ -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();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue