(#594) Introduce stacktrace unit

master
rexim 2018-12-30 00:04:42 +07:00
parent cee8d11e65
commit 1b8f8b56f4
3 changed files with 46 additions and 0 deletions

View File

@ -119,6 +119,8 @@ add_executable(nothing
src/game/level_script.h
src/ebisp/std.h
src/ebisp/std.c
src/system/stacktrace.h
src/system/stacktrace.c
)
add_executable(repl

26
src/system/stacktrace.c Normal file
View File

@ -0,0 +1,26 @@
#include <stdio.h>
#ifdef __GNUC__
#include <execinfo.h>
#endif
#include <unistd.h>
#include "./stacktrace.h"
#define N 10
void print_stacktrace(void)
{
#ifdef __GNUC__
void *array[N];
int size;
size = backtrace(array, N);
if (size <= 0) {
return;
}
fprintf(stderr, "Stacktrace: \n");
backtrace_symbols_fd(array + 1, size - 1, STDERR_FILENO);
#endif
}

18
src/system/stacktrace.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef STACKTRACE_H_
#define STACKTRACE_H_
#define trace_assert(condition) \
if (!(condition)) { \
fprintf( \
stderr, \
"%s:%d: %s: Assertion `%s' failed\n", \
__FILE__, __LINE__, \
__func__, \
#condition); \
print_stacktrace(); \
exit(1); \
}
void print_stacktrace(void);
#endif // STACKTRACE_H_