-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
crash_handler.d
51 lines (42 loc) · 1.37 KB
/
crash_handler.d
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
48
49
50
51
module crash_handler;
version (CRuntime_Musl)
enum BacktraceHandler = false;
else version (linux)
enum BacktraceHandler = true;
else version (OSX)
enum BacktraceHandler = true;
else
enum BacktraceHandler = false;
static if (BacktraceHandler)
{
extern (C) int backtrace(void** buffer, int size) nothrow @nogc @system;
extern (C) void backtrace_symbols_fd(const(void*)* buffer, int size, int fd) nothrow @nogc @system;
extern (C) void backtrace_handler(int sig) nothrow @nogc @system
{
import core.sys.posix.stdlib : exit;
import core.sys.posix.stdio : fprintf, fflush, stderr;
void*[100] buffer;
auto size = backtrace(buffer.ptr, cast(int) buffer.length);
fprintf(stderr, "Error: signal %d:\n", sig);
backtrace_symbols_fd(buffer.ptr, size, 2);
fflush(stderr);
exit(-sig);
}
void registerErrorHandlers()
{
import core.sys.posix.stdio : fprintf, stderr;
import core.sys.posix.signal : signal, SIGABRT, SIGALRM, SIGILL, SIGINT,
SIGKILL, SIGPIPE, SIGSEGV, SIGTRAP;
static foreach (sig; [
SIGABRT, SIGALRM, SIGILL, SIGINT, SIGKILL, SIGPIPE, SIGSEGV, SIGTRAP
])
signal(sig, &backtrace_handler);
// this is print on every invocation of serve-d, so we only print this in unittests and assume it works elsewhere
version (unittest)
fprintf(stderr, "Registered backtrace signal handlers\n");
}
shared static this()
{
registerErrorHandlers();
}
}