zpu: wip - very crude stack slot pass
[llvm/zpu.git] / lib / Support / CrashRecoveryContext.cpp
blob93af79bc0f564df3d360b42c5b116cac557099f2
1 //===--- CrashRecoveryContext.cpp - Crash Recovery ------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/CrashRecoveryContext.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/Config/config.h"
13 #include "llvm/System/Mutex.h"
14 #include "llvm/System/ThreadLocal.h"
15 #include <setjmp.h>
16 #include <cstdio>
17 using namespace llvm;
19 namespace {
21 struct CrashRecoveryContextImpl;
23 static sys::ThreadLocal<const CrashRecoveryContextImpl> CurrentContext;
25 struct CrashRecoveryContextImpl {
26 CrashRecoveryContext *CRC;
27 std::string Backtrace;
28 ::jmp_buf JumpBuffer;
29 volatile unsigned Failed : 1;
31 public:
32 CrashRecoveryContextImpl(CrashRecoveryContext *CRC) : CRC(CRC),
33 Failed(false) {
34 CurrentContext.set(this);
36 ~CrashRecoveryContextImpl() {
37 CurrentContext.erase();
40 void HandleCrash() {
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!");
46 Failed = true;
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 CrashRecoveryContext::~CrashRecoveryContext() {
61 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
62 delete CRCI;
65 CrashRecoveryContext *CrashRecoveryContext::GetCurrent() {
66 const CrashRecoveryContextImpl *CRCI = CurrentContext.get();
67 if (!CRCI)
68 return 0;
70 return CRCI->CRC;
73 #ifdef LLVM_ON_WIN32
75 // FIXME: No real Win32 implementation currently.
77 void CrashRecoveryContext::Enable() {
78 sys::ScopedLock L(gCrashRecoveryContexMutex);
80 if (gCrashRecoveryEnabled)
81 return;
83 gCrashRecoveryEnabled = true;
86 void CrashRecoveryContext::Disable() {
87 sys::ScopedLock L(gCrashRecoveryContexMutex);
89 if (!gCrashRecoveryEnabled)
90 return;
92 gCrashRecoveryEnabled = false;
95 #else
97 // Generic POSIX implementation.
99 // This implementation relies on synchronous signals being delivered to the
100 // current thread. We use a thread local object to keep track of the active
101 // crash recovery context, and install signal handlers to invoke HandleCrash on
102 // the active object.
104 // This implementation does not to attempt to chain signal handlers in any
105 // reliable fashion -- if we get a signal outside of a crash recovery context we
106 // simply disable crash recovery and raise the signal again.
108 #include <signal.h>
110 static int Signals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP };
111 static const unsigned NumSignals = sizeof(Signals) / sizeof(Signals[0]);
112 static struct sigaction PrevActions[NumSignals];
114 static void CrashRecoverySignalHandler(int Signal) {
115 // Lookup the current thread local recovery object.
116 const CrashRecoveryContextImpl *CRCI = CurrentContext.get();
118 if (!CRCI) {
119 // We didn't find a crash recovery context -- this means either we got a
120 // signal on a thread we didn't expect it on, the application got a signal
121 // outside of a crash recovery context, or something else went horribly
122 // wrong.
124 // Disable crash recovery and raise the signal again. The assumption here is
125 // that the enclosing application will terminate soon, and we won't want to
126 // attempt crash recovery again.
128 // This call of Disable isn't thread safe, but it doesn't actually matter.
129 CrashRecoveryContext::Disable();
130 raise(Signal);
132 // The signal will be thrown once the signal mask is restored.
133 return;
136 // Unblock the signal we received.
137 sigset_t SigMask;
138 sigemptyset(&SigMask);
139 sigaddset(&SigMask, Signal);
140 sigprocmask(SIG_UNBLOCK, &SigMask, 0);
142 if (CRCI)
143 const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash();
146 void CrashRecoveryContext::Enable() {
147 sys::ScopedLock L(gCrashRecoveryContexMutex);
149 if (gCrashRecoveryEnabled)
150 return;
152 gCrashRecoveryEnabled = true;
154 // Setup the signal handler.
155 struct sigaction Handler;
156 Handler.sa_handler = CrashRecoverySignalHandler;
157 Handler.sa_flags = 0;
158 sigemptyset(&Handler.sa_mask);
160 for (unsigned i = 0; i != NumSignals; ++i) {
161 sigaction(Signals[i], &Handler, &PrevActions[i]);
165 void CrashRecoveryContext::Disable() {
166 sys::ScopedLock L(gCrashRecoveryContexMutex);
168 if (!gCrashRecoveryEnabled)
169 return;
171 gCrashRecoveryEnabled = false;
173 // Restore the previous signal handlers.
174 for (unsigned i = 0; i != NumSignals; ++i)
175 sigaction(Signals[i], &PrevActions[i], 0);
178 #endif
180 bool CrashRecoveryContext::RunSafely(void (*Fn)(void*), void *UserData) {
181 // If crash recovery is disabled, do nothing.
182 if (gCrashRecoveryEnabled) {
183 assert(!Impl && "Crash recovery context already initialized!");
184 CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl(this);
185 Impl = CRCI;
187 if (setjmp(CRCI->JumpBuffer) != 0) {
188 return false;
192 Fn(UserData);
193 return true;
196 void CrashRecoveryContext::HandleCrash() {
197 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
198 assert(CRCI && "Crash recovery context never initialized!");
199 CRCI->HandleCrash();
202 const std::string &CrashRecoveryContext::getBacktrace() const {
203 CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *) Impl;
204 assert(CRC && "Crash recovery context never initialized!");
205 assert(CRC->Failed && "No crash was detected!");
206 return CRC->Backtrace;