2021-01-15 20:39:45 +00:00
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2021-02-09 12:32:33 +00:00
|
|
|
#include "log.h"
|
2021-01-15 20:39:45 +00:00
|
|
|
|
|
|
|
|
#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
|
2021-02-09 12:32:33 +00:00
|
|
|
fatal_error(const char *file, int line, const char *msg, int err)
|
2021-01-15 20:39:45 +00:00
|
|
|
{
|
2021-02-09 12:32:33 +00:00
|
|
|
log_msg(LOG_CLASS_ERROR, "debug", file, line, "%s: %s", msg, strerror(err));
|
2021-01-15 20:39:45 +00:00
|
|
|
print_stack_trace();
|
2021-02-09 12:32:33 +00:00
|
|
|
fflush(stderr);
|
2021-01-15 20:39:45 +00:00
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
noreturn void
|
|
|
|
|
bug(const char *file, int line, const char *func, const char *fmt, ...)
|
|
|
|
|
{
|
2021-02-09 12:32:33 +00:00
|
|
|
char buf[4096];
|
2021-01-15 20:39:45 +00:00
|
|
|
va_list ap;
|
|
|
|
|
va_start(ap, fmt);
|
2021-02-09 12:32:33 +00:00
|
|
|
int n = vsnprintf(buf, sizeof(buf), fmt, ap);
|
2021-01-15 20:39:45 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
|
2021-02-09 12:32:33 +00:00
|
|
|
const char *msg = likely(n >= 0) ? buf : "??";
|
|
|
|
|
log_msg(LOG_CLASS_ERROR, "debug", file, line, "BUG in %s(): %s", func, msg);
|
2021-01-15 20:39:45 +00:00
|
|
|
print_stack_trace();
|
|
|
|
|
fflush(stderr);
|
|
|
|
|
abort();
|
|
|
|
|
}
|