#include "llvm/Support/CrashRecoveryContext.h"
#include "llvm/ADT/SmallString.h"
+#include "llvm/Config/config.h"
+#include "llvm/System/Mutex.h"
#include "llvm/System/ThreadLocal.h"
#include <setjmp.h>
#include <cstdio>
static sys::ThreadLocal<const CrashRecoveryContextImpl> CurrentContext;
struct CrashRecoveryContextImpl {
+ CrashRecoveryContext *CRC;
std::string Backtrace;
::jmp_buf JumpBuffer;
volatile unsigned Failed : 1;
public:
- CrashRecoveryContextImpl() : Failed(false) {
+ CrashRecoveryContextImpl(CrashRecoveryContext *CRC) : CRC(CRC),
+ Failed(false) {
CurrentContext.set(this);
}
~CrashRecoveryContextImpl() {
- CurrentContext.set(0);
+ CurrentContext.erase();
}
void HandleCrash() {
+ // Eliminate the current context entry, to avoid re-entering in case the
+ // cleanup code crashes.
+ CurrentContext.erase();
+
assert(!Failed && "Crash recovery context already failed!");
Failed = true;
}
+static sys::Mutex gCrashRecoveryContexMutex;
static bool gCrashRecoveryEnabled = false;
CrashRecoveryContext::~CrashRecoveryContext() {
delete CRCI;
}
+CrashRecoveryContext *CrashRecoveryContext::GetCurrent() {
+ const CrashRecoveryContextImpl *CRCI = CurrentContext.get();
+ if (!CRCI)
+ return 0;
+
+ return CRCI->CRC;
+}
+
#ifdef LLVM_ON_WIN32
// FIXME: No real Win32 implementation currently.
void CrashRecoveryContext::Enable() {
+ sys::ScopedLock L(gCrashRecoveryContexMutex);
+
if (gCrashRecoveryEnabled)
return;
}
void CrashRecoveryContext::Disable() {
+ sys::ScopedLock L(gCrashRecoveryContexMutex);
+
if (!gCrashRecoveryEnabled)
return;
#include <signal.h>
-static struct {
- int Signal;
- struct sigaction PrevAction;
-} SignalInfo[] = {
- { SIGABRT, {} },
- { SIGBUS, {} },
- { SIGFPE, {} },
- { SIGILL, {} },
- { SIGSEGV, {} },
- { SIGTRAP, {} },
-};
-static const unsigned NumSignals = sizeof(SignalInfo) / sizeof(SignalInfo[0]);
+static int Signals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP };
+static const unsigned NumSignals = sizeof(Signals) / sizeof(Signals[0]);
+static struct sigaction PrevActions[NumSignals];
static void CrashRecoverySignalHandler(int Signal) {
// Lookup the current thread local recovery object.
// This call of Disable isn't thread safe, but it doesn't actually matter.
CrashRecoveryContext::Disable();
raise(Signal);
+
+ // The signal will be thrown once the signal mask is restored.
+ return;
}
// Unblock the signal we received.
}
void CrashRecoveryContext::Enable() {
+ sys::ScopedLock L(gCrashRecoveryContexMutex);
+
if (gCrashRecoveryEnabled)
return;
sigemptyset(&Handler.sa_mask);
for (unsigned i = 0; i != NumSignals; ++i) {
- sigaction(SignalInfo[i].Signal, &Handler,
- &SignalInfo[i].PrevAction);
+ sigaction(Signals[i], &Handler, &PrevActions[i]);
}
}
void CrashRecoveryContext::Disable() {
+ sys::ScopedLock L(gCrashRecoveryContexMutex);
+
if (!gCrashRecoveryEnabled)
return;
// Restore the previous signal handlers.
for (unsigned i = 0; i != NumSignals; ++i)
- sigaction(SignalInfo[i].Signal, &SignalInfo[i].PrevAction, 0);
+ sigaction(Signals[i], &PrevActions[i], 0);
}
#endif
// If crash recovery is disabled, do nothing.
if (gCrashRecoveryEnabled) {
assert(!Impl && "Crash recovery context already initialized!");
- CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl;
+ CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl(this);
Impl = CRCI;
if (setjmp(CRCI->JumpBuffer) != 0) {