1 //===--- CrashRecoveryContext.cpp - Crash Recovery ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/CrashRecoveryContext.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/Config/config.h"
13 #include "llvm/System/ThreadLocal.h"
20 struct CrashRecoveryContextImpl;
22 static sys::ThreadLocal<const CrashRecoveryContextImpl> CurrentContext;
24 struct CrashRecoveryContextImpl {
25 std::string Backtrace;
27 volatile unsigned Failed : 1;
30 CrashRecoveryContextImpl() : Failed(false) {
31 CurrentContext.set(this);
33 ~CrashRecoveryContextImpl() {
34 CurrentContext.erase();
38 assert(!Failed && "Crash recovery context already failed!");
41 // FIXME: Stash the backtrace.
43 // Jump back to the RunSafely we were called under.
44 longjmp(JumpBuffer, 1);
50 static bool gCrashRecoveryEnabled = false;
52 CrashRecoveryContext::~CrashRecoveryContext() {
53 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
59 // FIXME: No real Win32 implementation currently.
61 void CrashRecoveryContext::Enable() {
62 if (gCrashRecoveryEnabled)
65 gCrashRecoveryEnabled = true;
68 void CrashRecoveryContext::Disable() {
69 if (!gCrashRecoveryEnabled)
72 gCrashRecoveryEnabled = false;
77 // Generic POSIX implementation.
79 // This implementation relies on synchronous signals being delivered to the
80 // current thread. We use a thread local object to keep track of the active
81 // crash recovery context, and install signal handlers to invoke HandleCrash on
84 // This implementation does not to attempt to chain signal handlers in any
85 // reliable fashion -- if we get a signal outside of a crash recovery context we
86 // simply disable crash recovery and raise the signal again.
90 static int Signals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP };
91 static const unsigned NumSignals = sizeof(Signals) / sizeof(Signals[0]);
92 static struct sigaction PrevActions[NumSignals];
94 static void CrashRecoverySignalHandler(int Signal) {
95 // Lookup the current thread local recovery object.
96 const CrashRecoveryContextImpl *CRCI = CurrentContext.get();
99 // We didn't find a crash recovery context -- this means either we got a
100 // signal on a thread we didn't expect it on, the application got a signal
101 // outside of a crash recovery context, or something else went horribly
104 // Disable crash recovery and raise the signal again. The assumption here is
105 // that the enclosing application will terminate soon, and we won't want to
106 // attempt crash recovery again.
108 // This call of Disable isn't thread safe, but it doesn't actually matter.
109 CrashRecoveryContext::Disable();
113 // Unblock the signal we received.
115 sigemptyset(&SigMask);
116 sigaddset(&SigMask, Signal);
117 sigprocmask(SIG_UNBLOCK, &SigMask, 0);
120 const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash();
123 void CrashRecoveryContext::Enable() {
124 if (gCrashRecoveryEnabled)
127 gCrashRecoveryEnabled = true;
129 // Setup the signal handler.
130 struct sigaction Handler;
131 Handler.sa_handler = CrashRecoverySignalHandler;
132 Handler.sa_flags = 0;
133 sigemptyset(&Handler.sa_mask);
135 for (unsigned i = 0; i != NumSignals; ++i) {
136 sigaction(Signals[i], &Handler, &PrevActions[i]);
140 void CrashRecoveryContext::Disable() {
141 if (!gCrashRecoveryEnabled)
144 gCrashRecoveryEnabled = false;
146 // Restore the previous signal handlers.
147 for (unsigned i = 0; i != NumSignals; ++i)
148 sigaction(Signals[i], &PrevActions[i], 0);
153 bool CrashRecoveryContext::RunSafely(void (*Fn)(void*), void *UserData) {
154 // If crash recovery is disabled, do nothing.
155 if (gCrashRecoveryEnabled) {
156 assert(!Impl && "Crash recovery context already initialized!");
157 CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl;
160 if (setjmp(CRCI->JumpBuffer) != 0) {
169 void CrashRecoveryContext::HandleCrash() {
170 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
171 assert(CRCI && "Crash recovery context never initialized!");
175 const std::string &CrashRecoveryContext::getBacktrace() const {
176 CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *) Impl;
177 assert(CRC && "Crash recovery context never initialized!");
178 assert(CRC->Failed && "No crash was detected!");
179 return CRC->Backtrace;