forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal.cpp
191 lines (181 loc) · 5 KB
/
signal.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* Copyright (c) Facebook, Inc. and its affiliates.
* Licensed under the Apache License, Version 2.0. */
#include "watchman.h"
#include <folly/Conv.h>
#include "Logging.h"
using namespace watchman;
#ifndef _WIN32
static void crash_handler(int signo, siginfo_t* si, void*) {
const char* reason = "";
if (si) {
switch (si->si_signo) {
case SIGILL:
switch (si->si_code) {
case ILL_ILLOPC:
reason = "illegal opcode";
break;
case ILL_ILLOPN:
reason = "illegal operand";
break;
case ILL_ILLADR:
reason = "illegal addressing mode";
break;
case ILL_ILLTRP:
reason = "illegal trap";
break;
case ILL_PRVOPC:
reason = "privileged opcode";
break;
case ILL_PRVREG:
reason = "privileged register";
break;
case ILL_COPROC:
reason = "co-processor error";
break;
case ILL_BADSTK:
reason = "internal stack error";
break;
}
break;
case SIGFPE:
switch (si->si_code) {
case FPE_INTDIV:
reason = "integer divide by zero";
break;
case FPE_INTOVF:
reason = "integer overflow";
break;
case FPE_FLTDIV:
reason = "floating point divide by zero";
break;
case FPE_FLTOVF:
reason = "floating point overflow";
break;
case FPE_FLTUND:
reason = "floating point underflow";
break;
case FPE_FLTRES:
reason = "floating point inexact result";
break;
case FPE_FLTINV:
reason = "invalid floating point operation";
break;
case FPE_FLTSUB:
reason = "subscript out of range";
break;
}
break;
case SIGSEGV:
switch (si->si_code) {
case SEGV_MAPERR:
reason = "address not mapped to object";
break;
case SEGV_ACCERR:
reason = "invalid permissions for mapped object";
break;
}
break;
#ifdef SIGBUS
case SIGBUS:
switch (si->si_code) {
case BUS_ADRALN:
reason = "invalid address alignment";
break;
case BUS_ADRERR:
reason = "non-existent physical address";
break;
}
break;
#endif
}
}
if (si) {
auto msg = folly::to<std::string>(
"Terminating due to signal ",
signo,
" ",
w_strsignal(signo),
" generated by pid=",
si->si_pid,
" uid=",
si->si_uid,
" ",
reason,
" (",
uintptr_t(si->si_value.sival_ptr),
")\n");
ignore_result(write(STDERR_FILENO, msg.data(), msg.size()));
} else {
auto msg = folly::to<std::string>(
"Terminating due to signal ",
signo,
" ",
w_strsignal(signo),
" ",
reason,
"\n");
ignore_result(write(STDERR_FILENO, msg.data(), msg.size()));
}
#if defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS_FD)
{
void* array[24];
size_t size = backtrace(array, sizeof(array) / sizeof(array[0]));
backtrace_symbols_fd(array, size, STDERR_FILENO);
}
#endif
if (signo == SIGTERM) {
w_request_shutdown();
return;
}
// Resend the signal to terminate ourselves with it.
// We register crash_handler with SA_RESETHAND so it will
// not be invoked a second time.
kill(getpid(), signo);
}
#endif
namespace {
void terminationHandler() {
auto eptr = std::current_exception();
if (eptr) {
try {
std::rethrow_exception(eptr);
} catch (const std::exception& exc) {
log(ABORT, "std::terminate was called. Exception: ", exc.what(), ".\n");
} catch (...) {
log(ABORT,
"std::terminate was called. Exception is not a std::exception.\n");
}
} else {
log(ABORT, "std::terminate was called. There is no current exception.\n");
}
}
} // namespace
void w_setup_signal_handlers(void) {
#ifndef _WIN32
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_sigaction = crash_handler;
sa.sa_flags = SA_SIGINFO | SA_RESETHAND;
sigaction(SIGSEGV, &sa, NULL);
#ifdef SIGBUS
sigaction(SIGBUS, &sa, NULL);
#endif
sigaction(SIGFPE, &sa, NULL);
sigaction(SIGILL, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
#else
// Don't show error dialogs for background service failures
SetErrorMode(SEM_FAILCRITICALERRORS);
// also tell the C runtime that we should just abort when
// we abort; don't do the crash reporting dialog.
_set_abort_behavior(_WRITE_ABORT_MSG, ~0);
// Force error output to stderr, don't use a msgbox.
_set_error_mode(_OUT_TO_STDERR);
// bridge OS exceptions into our FATAL logger so that we can
// capture a stack trace.
SetUnhandledExceptionFilter(exception_filter);
#endif
std::set_terminate(terminationHandler);
}
/* vim:ts=2:sw=2:et:
*/