]> git.lizzy.rs Git - nothing.git/blob - src/system/stacktrace.c
Merge pull request #1113 from tsoding/1108
[nothing.git] / src / system / stacktrace.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #if defined(__GNUC__) && defined(__linux__)
4 #include <execinfo.h>
5 #include <unistd.h>
6 #define N 100
7 #endif
8
9 #include "./stacktrace.h"
10
11
12 void print_stacktrace(void)
13 {
14 #if defined(__GNUC__) && defined(__linux__)
15     void *array[N];
16     int size;
17
18     size = backtrace(array, N);
19
20     if (size <= 0) {
21         return;
22     }
23
24     fprintf(stderr, "Stacktrace: \n");
25     backtrace_symbols_fd(array + 1, size - 1, STDERR_FILENO);
26 #endif
27 }
28
29 void __trace_assert(const char *file, int line, const char *function, const char *message)
30 {
31     fprintf(
32         stderr,
33         "%s:%d: %s: Assertion `%s' failed\n",
34         file, line,
35         function,
36         message);
37     print_stacktrace();
38     abort();
39 }