File: debug.c

package info (click to toggle)
fuzzel 1.12.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 908 kB
  • sloc: ansic: 13,535; xml: 324; sh: 27; makefile: 9
file content (47 lines) | stat: -rw-r--r-- 1,047 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "debug.h"

#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "log.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 *file, int line, const char *msg, int err)
{
    log_msg(LOG_CLASS_ERROR, "debug", file, line, "%s: %s", msg, strerror(err));
    print_stack_trace();
    fflush(stderr);
    abort();
}

noreturn void
bug(const char *file, int line, const char *func, const char *fmt, ...)
{
    char buf[4096];
    va_list ap;
    va_start(ap, fmt);
    int n = vsnprintf(buf, sizeof(buf), fmt, ap);
    va_end(ap);

    const char *msg = likely(n >= 0) ? buf : "??";
    log_msg(LOG_CLASS_ERROR, "debug", file, line, "BUG in %s(): %s", func, msg);
    print_stack_trace();
    fflush(stderr);
    abort();
}