2 * Copyright 2015 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // This is heavily inspired by the signal handler from google-glog
19 #include <folly/experimental/symbolizer/SignalHandler.h>
21 #include <sys/types.h>
22 #include <sys/syscall.h>
31 #include <glog/logging.h>
33 #include <folly/Conv.h>
34 #include <folly/FileUtil.h>
35 #include <folly/Portability.h>
36 #include <folly/ScopeGuard.h>
37 #include <folly/experimental/symbolizer/Symbolizer.h>
39 namespace folly { namespace symbolizer {
44 * Fatal signal handler registry.
46 class FatalSignalCallbackRegistry {
48 FatalSignalCallbackRegistry();
50 void add(SignalCallback func);
55 std::atomic<bool> installed_;
57 std::vector<SignalCallback> handlers_;
60 FatalSignalCallbackRegistry::FatalSignalCallbackRegistry()
64 void FatalSignalCallbackRegistry::add(SignalCallback func) {
65 std::lock_guard<std::mutex> lock(mutex_);
67 << "FatalSignalCallbackRegistry::add may not be used "
68 "after installing the signal handlers.";
69 handlers_.push_back(func);
72 void FatalSignalCallbackRegistry::markInstalled() {
73 std::lock_guard<std::mutex> lock(mutex_);
74 CHECK(!installed_.exchange(true))
75 << "FatalSignalCallbackRegistry::markInstalled must be called "
79 void FatalSignalCallbackRegistry::run() {
84 for (auto& fn : handlers_) {
89 // Leak it so we don't have to worry about destruction order
90 FatalSignalCallbackRegistry* gFatalSignalCallbackRegistry =
91 new FatalSignalCallbackRegistry;
96 struct sigaction oldAction;
98 { SIGSEGV, "SIGSEGV" },
100 { SIGFPE, "SIGFPE" },
101 { SIGABRT, "SIGABRT" },
102 { SIGBUS, "SIGBUS" },
103 { SIGTERM, "SIGTERM" },
107 void callPreviousSignalHandler(int signum) {
108 // Restore disposition to old disposition, then kill ourselves with the same
109 // signal. The signal will be blocked until we return from our handler,
110 // then it will invoke the default handler and abort.
111 for (auto p = kFatalSignals; p->name; ++p) {
112 if (p->number == signum) {
113 sigaction(signum, &p->oldAction, nullptr);
119 // Not one of the signals we know about. Oh well. Reset to default.
121 memset(&sa, 0, sizeof(sa));
122 sa.sa_handler = SIG_DFL;
123 sigaction(signum, &sa, nullptr);
127 constexpr size_t kDefaultCapacity = 500;
129 // Note: not thread-safe, but that's okay, as we only let one thread
130 // in our signal handler at a time.
132 // Leak it so we don't have to worry about destruction order
133 auto gSignalSafeElfCache = new SignalSafeElfCache(kDefaultCapacity);
135 // Buffered writer (using a fixed-size buffer). We try to write only once
136 // to prevent interleaving with messages written from other threads.
138 // Leak it so we don't have to worry about destruction order.
139 auto gPrinter = new FDSymbolizePrinter(STDERR_FILENO,
140 SymbolizePrinter::COLOR_IF_TTY,
141 size_t(64) << 10); // 64KiB
143 // Flush gPrinter, also fsync, in case we're about to crash again...
146 fsyncNoInt(STDERR_FILENO);
149 void printDec(uint64_t val) {
151 uint32_t n = uint64ToBufferUnsafe(val, buf);
152 gPrinter->print(StringPiece(buf, n));
155 const char kHexChars[] = "0123456789abcdef";
156 void printHex(uint64_t val) {
157 // TODO(tudorb): Add this to folly/Conv.h
158 char buf[2 + 2 * sizeof(uint64_t)]; // "0x" prefix, 2 digits for each byte
160 char* end = buf + sizeof(buf);
163 *--p = kHexChars[val & 0x0f];
169 gPrinter->print(StringPiece(p, end));
172 void print(StringPiece sp) {
176 void dumpTimeInfo() {
177 SCOPE_EXIT { flush(); };
178 time_t now = time(nullptr);
179 print("*** Aborted at ");
181 print(" (Unix time, try 'date -d @");
186 const char* sigill_reason(int si_code) {
189 return "illegal opcode";
191 return "illegal operand";
193 return "illegal addressing mode";
195 return "illegal trap";
197 return "privileged opcode";
199 return "privileged register";
201 return "coprocessor error";
203 return "internal stack error";
210 const char* sigfpe_reason(int si_code) {
213 return "integer divide by zero";
215 return "integer overflow";
217 return "floating-point divide by zero";
219 return "floating-point overflow";
221 return "floating-point underflow";
223 return "floating-point inexact result";
225 return "floating-point invalid operation";
227 return "subscript out of range";
234 const char* sigsegv_reason(int si_code) {
237 return "address not mapped to object";
239 return "invalid permissions for mapped object";
246 const char* sigbus_reason(int si_code) {
249 return "invalid address alignment";
251 return "nonexistent physical address";
253 return "object-specific hardware error";
255 // MCEERR_AR and MCEERR_AO: in sigaction(2) but not in headers.
262 const char* sigtrap_reason(int si_code) {
265 return "process breakpoint";
267 return "process trace trap";
269 // TRAP_BRANCH and TRAP_HWBKPT: in sigaction(2) but not in headers.
276 const char* sigchld_reason(int si_code) {
279 return "child has exited";
281 return "child was killed";
283 return "child terminated abnormally";
285 return "traced child has trapped";
287 return "child has stopped";
289 return "stopped child has continued";
296 const char* sigio_reason(int si_code) {
299 return "data input available";
301 return "output buffers available";
303 return "input message available";
307 return "high priority input available";
309 return "device disconnected";
316 const char* signal_reason(int signum, int si_code) {
319 return sigill_reason(si_code);
321 return sigfpe_reason(si_code);
323 return sigsegv_reason(si_code);
325 return sigbus_reason(si_code);
327 return sigtrap_reason(si_code);
329 return sigchld_reason(si_code);
331 return sigio_reason(si_code); // aka SIGPOLL
338 void dumpSignalInfo(int signum, siginfo_t* siginfo) {
339 SCOPE_EXIT { flush(); };
340 // Get the signal name, if possible.
341 const char* name = nullptr;
342 for (auto p = kFatalSignals; p->name; ++p) {
343 if (p->number == signum) {
349 print("*** Signal ");
358 printHex(reinterpret_cast<uint64_t>(siginfo->si_addr));
359 print(") received by PID ");
361 print(" (pthread TID ");
362 printHex((uint64_t)pthread_self());
363 print(") (linux TID ");
364 printDec(syscall(__NR_gettid));
366 // Kernel-sourced signals don't give us useful info for pid/uid.
367 if (siginfo->si_code != SI_KERNEL) {
368 print(") (maybe from PID ");
369 printDec(siginfo->si_pid);
371 printDec(siginfo->si_uid);
374 auto reason = signal_reason(signum, siginfo->si_code);
376 if (reason != nullptr) {
381 print("), stack trace: ***\n");
384 FOLLY_NOINLINE void dumpStackTrace(bool symbolize);
386 void dumpStackTrace(bool symbolize) {
387 SCOPE_EXIT { flush(); };
388 // Get and symbolize stack trace
389 constexpr size_t kMaxStackTraceDepth = 100;
390 FrameArray<kMaxStackTraceDepth> addresses;
392 // Skip the getStackTrace frame
393 if (!getStackTraceSafe(addresses)) {
394 print("(error retrieving stack trace)\n");
395 } else if (symbolize) {
396 Symbolizer symbolizer(gSignalSafeElfCache);
397 symbolizer.symbolize(addresses);
399 // Skip the top 2 frames:
401 // dumpStackTrace (here)
403 // Leaving signalHandler on the stack for clarity, I think.
404 gPrinter->println(addresses, 2);
406 print("(safe mode, symbolizer not available)\n");
407 AddressFormatter formatter;
408 for (size_t i = 0; i < addresses.frameCount; ++i) {
409 print(formatter.format(addresses.addresses[i]));
415 // On Linux, pthread_t is a pointer, so 0 is an invalid value, which we
416 // take to indicate "no thread in the signal handler".
418 // POSIX defines PTHREAD_NULL for this purpose, but that's not available.
419 constexpr pthread_t kInvalidThreadId = 0;
421 std::atomic<pthread_t> gSignalThread(kInvalidThreadId);
422 std::atomic<bool> gInRecursiveSignalHandler(false);
425 void innerSignalHandler(int signum, siginfo_t* info, void* /* uctx */) {
426 // First, let's only let one thread in here at a time.
427 pthread_t myId = pthread_self();
429 pthread_t prevSignalThread = kInvalidThreadId;
430 while (!gSignalThread.compare_exchange_strong(prevSignalThread, myId)) {
431 if (pthread_equal(prevSignalThread, myId)) {
432 // First time here. Try to dump the stack trace without symbolization.
433 // If we still fail, well, we're mightily screwed, so we do nothing the
435 if (!gInRecursiveSignalHandler.exchange(true)) {
436 print("Entered fatal signal handler recursively. We're in trouble.\n");
437 dumpStackTrace(false); // no symbolization
442 // Wait a while, try again.
445 ts.tv_nsec = 100L * 1000 * 1000; // 100ms
446 nanosleep(&ts, nullptr);
448 prevSignalThread = kInvalidThreadId;
452 dumpSignalInfo(signum, info);
453 dumpStackTrace(true); // with symbolization
455 // Run user callbacks
456 gFatalSignalCallbackRegistry->run();
459 void signalHandler(int signum, siginfo_t* info, void* uctx) {
460 SCOPE_EXIT { flush(); };
461 innerSignalHandler(signum, info, uctx);
463 gSignalThread = kInvalidThreadId;
464 // Kill ourselves with the previous handler.
465 callPreviousSignalHandler(signum);
470 void addFatalSignalCallback(SignalCallback cb) {
471 gFatalSignalCallbackRegistry->add(cb);
474 void installFatalSignalCallbacks() {
475 gFatalSignalCallbackRegistry->markInstalled();
480 std::atomic<bool> gAlreadyInstalled;
484 void installFatalSignalHandler() {
485 if (gAlreadyInstalled.exchange(true)) {
491 memset(&sa, 0, sizeof(sa));
492 sigemptyset(&sa.sa_mask);
493 // By default signal handlers are run on the signaled thread's stack.
494 // In case of stack overflow running the SIGSEGV signal handler on
495 // the same stack leads to another SIGSEGV and crashes the program.
496 // Use SA_ONSTACK, so alternate stack is used (only if configured via
498 sa.sa_flags |= SA_SIGINFO | SA_ONSTACK;
499 sa.sa_sigaction = &signalHandler;
501 for (auto p = kFatalSignals; p->name; ++p) {
502 CHECK_ERR(sigaction(p->number, &sa, &p->oldAction));