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/Support/Mutex.h"
14 #include "llvm/Support/ThreadLocal.h"
21 struct CrashRecoveryContextImpl;
23 static sys::ThreadLocal<const CrashRecoveryContextImpl> CurrentContext;
25 struct CrashRecoveryContextImpl {
26 CrashRecoveryContext *CRC;
27 std::string Backtrace;
29 volatile unsigned Failed : 1;
32 CrashRecoveryContextImpl(CrashRecoveryContext *CRC) : CRC(CRC),
34 CurrentContext.set(this);
36 ~CrashRecoveryContextImpl() {
37 CurrentContext.erase();
41 // Eliminate the current context entry, to avoid re-entering in case the
42 // cleanup code crashes.
43 CurrentContext.erase();
45 assert(!Failed && "Crash recovery context already failed!");
48 // FIXME: Stash the backtrace.
50 // Jump back to the RunSafely we were called under.
51 longjmp(JumpBuffer, 1);
57 static sys::Mutex gCrashRecoveryContexMutex;
58 static bool gCrashRecoveryEnabled = false;
60 CrashRecoveryContextCleanup::~CrashRecoveryContextCleanup() {}
62 CrashRecoveryContext::~CrashRecoveryContext() {
63 // Reclaim registered resources.
64 CrashRecoveryContextCleanup *i = head;
66 CrashRecoveryContextCleanup *tmp = i;
68 tmp->recoverResources();
72 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
76 CrashRecoveryContext *CrashRecoveryContext::GetCurrent() {
77 const CrashRecoveryContextImpl *CRCI = CurrentContext.get();
84 void CrashRecoveryContext::registerCleanup(CrashRecoveryContextCleanup *cleanup)
95 CrashRecoveryContext::unregisterCleanup(CrashRecoveryContextCleanup *cleanup) {
98 if (cleanup == head) {
104 cleanup->prev->next = cleanup->next;
106 cleanup->next->prev = cleanup->prev;
113 // FIXME: No real Win32 implementation currently.
115 void CrashRecoveryContext::Enable() {
116 sys::ScopedLock L(gCrashRecoveryContexMutex);
118 if (gCrashRecoveryEnabled)
121 gCrashRecoveryEnabled = true;
124 void CrashRecoveryContext::Disable() {
125 sys::ScopedLock L(gCrashRecoveryContexMutex);
127 if (!gCrashRecoveryEnabled)
130 gCrashRecoveryEnabled = false;
135 // Generic POSIX implementation.
137 // This implementation relies on synchronous signals being delivered to the
138 // current thread. We use a thread local object to keep track of the active
139 // crash recovery context, and install signal handlers to invoke HandleCrash on
140 // the active object.
142 // This implementation does not to attempt to chain signal handlers in any
143 // reliable fashion -- if we get a signal outside of a crash recovery context we
144 // simply disable crash recovery and raise the signal again.
148 static int Signals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP };
149 static const unsigned NumSignals = sizeof(Signals) / sizeof(Signals[0]);
150 static struct sigaction PrevActions[NumSignals];
152 static void CrashRecoverySignalHandler(int Signal) {
153 // Lookup the current thread local recovery object.
154 const CrashRecoveryContextImpl *CRCI = CurrentContext.get();
157 // We didn't find a crash recovery context -- this means either we got a
158 // signal on a thread we didn't expect it on, the application got a signal
159 // outside of a crash recovery context, or something else went horribly
162 // Disable crash recovery and raise the signal again. The assumption here is
163 // that the enclosing application will terminate soon, and we won't want to
164 // attempt crash recovery again.
166 // This call of Disable isn't thread safe, but it doesn't actually matter.
167 CrashRecoveryContext::Disable();
170 // The signal will be thrown once the signal mask is restored.
174 // Unblock the signal we received.
176 sigemptyset(&SigMask);
177 sigaddset(&SigMask, Signal);
178 sigprocmask(SIG_UNBLOCK, &SigMask, 0);
181 const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash();
184 void CrashRecoveryContext::Enable() {
185 sys::ScopedLock L(gCrashRecoveryContexMutex);
187 if (gCrashRecoveryEnabled)
190 gCrashRecoveryEnabled = true;
192 // Setup the signal handler.
193 struct sigaction Handler;
194 Handler.sa_handler = CrashRecoverySignalHandler;
195 Handler.sa_flags = 0;
196 sigemptyset(&Handler.sa_mask);
198 for (unsigned i = 0; i != NumSignals; ++i) {
199 sigaction(Signals[i], &Handler, &PrevActions[i]);
203 void CrashRecoveryContext::Disable() {
204 sys::ScopedLock L(gCrashRecoveryContexMutex);
206 if (!gCrashRecoveryEnabled)
209 gCrashRecoveryEnabled = false;
211 // Restore the previous signal handlers.
212 for (unsigned i = 0; i != NumSignals; ++i)
213 sigaction(Signals[i], &PrevActions[i], 0);
218 bool CrashRecoveryContext::RunSafely(void (*Fn)(void*), void *UserData) {
219 // If crash recovery is disabled, do nothing.
220 if (gCrashRecoveryEnabled) {
221 assert(!Impl && "Crash recovery context already initialized!");
222 CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl(this);
225 if (setjmp(CRCI->JumpBuffer) != 0) {
234 void CrashRecoveryContext::HandleCrash() {
235 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
236 assert(CRCI && "Crash recovery context never initialized!");
240 const std::string &CrashRecoveryContext::getBacktrace() const {
241 CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *) Impl;
242 assert(CRC && "Crash recovery context never initialized!");
243 assert(CRC->Failed && "No crash was detected!");
244 return CRC->Backtrace;
250 struct RunSafelyOnThreadInfo {
251 void (*UserFn)(void*);
253 CrashRecoveryContext *CRC;
258 static void RunSafelyOnThread_Dispatch(void *UserData) {
259 RunSafelyOnThreadInfo *Info =
260 reinterpret_cast<RunSafelyOnThreadInfo*>(UserData);
261 Info->Result = Info->CRC->RunSafely(Info->UserFn, Info->UserData);
263 bool CrashRecoveryContext::RunSafelyOnThread(void (*Fn)(void*), void *UserData,
264 unsigned RequestedStackSize) {
265 RunSafelyOnThreadInfo Info = { Fn, UserData, this, false };
266 llvm_execute_on_thread(RunSafelyOnThread_Dispatch, &Info, RequestedStackSize);