blob: e190051e6dbe1b6793da0de79c07c1d00920e773 [file] [log] [blame]
Daniel Dunbara309dac2010-07-28 15:40:20 +00001//===--- 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//===----------------------------------------------------------------------===//
9
10#include "llvm/Support/CrashRecoveryContext.h"
11#include "llvm/ADT/SmallString.h"
Daniel Dunbardade28e2010-07-29 01:52:04 +000012#include "llvm/Config/config.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000013#include "llvm/Support/Mutex.h"
14#include "llvm/Support/ThreadLocal.h"
Daniel Dunbara309dac2010-07-28 15:40:20 +000015#include <setjmp.h>
Daniel Dunbard9082df2010-07-29 01:21:47 +000016#include <cstdio>
Daniel Dunbara309dac2010-07-28 15:40:20 +000017using namespace llvm;
18
19namespace {
20
21struct CrashRecoveryContextImpl;
22
Daniel Dunbard9082df2010-07-29 01:21:47 +000023static sys::ThreadLocal<const CrashRecoveryContextImpl> CurrentContext;
24
Daniel Dunbara309dac2010-07-28 15:40:20 +000025struct CrashRecoveryContextImpl {
Daniel Dunbara8fa7982010-08-17 22:32:37 +000026 CrashRecoveryContext *CRC;
Daniel Dunbara309dac2010-07-28 15:40:20 +000027 std::string Backtrace;
28 ::jmp_buf JumpBuffer;
29 volatile unsigned Failed : 1;
30
31public:
Daniel Dunbara8fa7982010-08-17 22:32:37 +000032 CrashRecoveryContextImpl(CrashRecoveryContext *CRC) : CRC(CRC),
33 Failed(false) {
Daniel Dunbard9082df2010-07-29 01:21:47 +000034 CurrentContext.set(this);
35 }
36 ~CrashRecoveryContextImpl() {
Daniel Dunbara6855822010-07-29 15:24:21 +000037 CurrentContext.erase();
Daniel Dunbard9082df2010-07-29 01:21:47 +000038 }
Daniel Dunbara309dac2010-07-28 15:40:20 +000039
40 void HandleCrash() {
Daniel Dunbarebe7eb82010-08-17 22:32:39 +000041 // Eliminate the current context entry, to avoid re-entering in case the
42 // cleanup code crashes.
43 CurrentContext.erase();
44
Daniel Dunbara309dac2010-07-28 15:40:20 +000045 assert(!Failed && "Crash recovery context already failed!");
46 Failed = true;
47
48 // FIXME: Stash the backtrace.
49
50 // Jump back to the RunSafely we were called under.
51 longjmp(JumpBuffer, 1);
52 }
53};
54
55}
56
Daniel Dunbarc0c815e2010-08-17 22:32:34 +000057static sys::Mutex gCrashRecoveryContexMutex;
Daniel Dunbara309dac2010-07-28 15:40:20 +000058static bool gCrashRecoveryEnabled = false;
59
Ted Kremeneka4f98392011-03-18 02:05:11 +000060CrashRecoveryContextCleanup::~CrashRecoveryContextCleanup() {}
61
Daniel Dunbara309dac2010-07-28 15:40:20 +000062CrashRecoveryContext::~CrashRecoveryContext() {
Ted Kremeneka4f98392011-03-18 02:05:11 +000063 // Reclaim registered resources.
64 CrashRecoveryContextCleanup *i = head;
65 while (i) {
66 CrashRecoveryContextCleanup *tmp = i;
67 i = tmp->next;
68 tmp->recoverResources();
69 delete tmp;
70 }
71
Daniel Dunbara309dac2010-07-28 15:40:20 +000072 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
73 delete CRCI;
74}
75
Daniel Dunbara8fa7982010-08-17 22:32:37 +000076CrashRecoveryContext *CrashRecoveryContext::GetCurrent() {
77 const CrashRecoveryContextImpl *CRCI = CurrentContext.get();
78 if (!CRCI)
79 return 0;
80
81 return CRCI->CRC;
82}
83
Ted Kremeneka4f98392011-03-18 02:05:11 +000084void CrashRecoveryContext::registerCleanup(CrashRecoveryContextCleanup *cleanup)
85{
86 if (!cleanup)
87 return;
88 if (head)
89 head->prev = cleanup;
90 cleanup->next = head;
91 head = cleanup;
92}
93
94void
95CrashRecoveryContext::unregisterCleanup(CrashRecoveryContextCleanup *cleanup) {
96 if (!cleanup)
97 return;
98 if (cleanup == head) {
99 head = cleanup->next;
100 if (head)
101 head->prev = 0;
102 }
103 else {
104 cleanup->prev->next = cleanup->next;
105 if (cleanup->next)
106 cleanup->next->prev = cleanup->prev;
107 }
108 delete cleanup;
109}
110
Daniel Dunbard9082df2010-07-29 01:21:47 +0000111#ifdef LLVM_ON_WIN32
112
113// FIXME: No real Win32 implementation currently.
114
Daniel Dunbara309dac2010-07-28 15:40:20 +0000115void CrashRecoveryContext::Enable() {
Daniel Dunbarc0c815e2010-08-17 22:32:34 +0000116 sys::ScopedLock L(gCrashRecoveryContexMutex);
117
Daniel Dunbara309dac2010-07-28 15:40:20 +0000118 if (gCrashRecoveryEnabled)
119 return;
120
121 gCrashRecoveryEnabled = true;
122}
123
124void CrashRecoveryContext::Disable() {
Daniel Dunbarc0c815e2010-08-17 22:32:34 +0000125 sys::ScopedLock L(gCrashRecoveryContexMutex);
126
Daniel Dunbara309dac2010-07-28 15:40:20 +0000127 if (!gCrashRecoveryEnabled)
128 return;
129
130 gCrashRecoveryEnabled = false;
131}
132
Daniel Dunbard9082df2010-07-29 01:21:47 +0000133#else
134
135// Generic POSIX implementation.
136//
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.
141//
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.
145
146#include <signal.h>
147
Daniel Dunbar63cc2e12010-07-30 17:49:04 +0000148static int Signals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP };
149static const unsigned NumSignals = sizeof(Signals) / sizeof(Signals[0]);
150static struct sigaction PrevActions[NumSignals];
Daniel Dunbard9082df2010-07-29 01:21:47 +0000151
152static void CrashRecoverySignalHandler(int Signal) {
153 // Lookup the current thread local recovery object.
154 const CrashRecoveryContextImpl *CRCI = CurrentContext.get();
155
156 if (!CRCI) {
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
160 // wrong.
161 //
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.
165 //
166 // This call of Disable isn't thread safe, but it doesn't actually matter.
167 CrashRecoveryContext::Disable();
168 raise(Signal);
Daniel Dunbard49e2aa2010-10-18 21:55:18 +0000169
170 // The signal will be thrown once the signal mask is restored.
171 return;
Daniel Dunbard9082df2010-07-29 01:21:47 +0000172 }
173
174 // Unblock the signal we received.
175 sigset_t SigMask;
176 sigemptyset(&SigMask);
177 sigaddset(&SigMask, Signal);
178 sigprocmask(SIG_UNBLOCK, &SigMask, 0);
179
180 if (CRCI)
181 const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash();
182}
183
184void CrashRecoveryContext::Enable() {
Daniel Dunbarc0c815e2010-08-17 22:32:34 +0000185 sys::ScopedLock L(gCrashRecoveryContexMutex);
186
Daniel Dunbard9082df2010-07-29 01:21:47 +0000187 if (gCrashRecoveryEnabled)
188 return;
189
190 gCrashRecoveryEnabled = true;
191
192 // Setup the signal handler.
193 struct sigaction Handler;
194 Handler.sa_handler = CrashRecoverySignalHandler;
195 Handler.sa_flags = 0;
196 sigemptyset(&Handler.sa_mask);
197
198 for (unsigned i = 0; i != NumSignals; ++i) {
Daniel Dunbar63cc2e12010-07-30 17:49:04 +0000199 sigaction(Signals[i], &Handler, &PrevActions[i]);
Daniel Dunbard9082df2010-07-29 01:21:47 +0000200 }
201}
202
203void CrashRecoveryContext::Disable() {
Daniel Dunbarc0c815e2010-08-17 22:32:34 +0000204 sys::ScopedLock L(gCrashRecoveryContexMutex);
205
Daniel Dunbard9082df2010-07-29 01:21:47 +0000206 if (!gCrashRecoveryEnabled)
207 return;
208
209 gCrashRecoveryEnabled = false;
210
211 // Restore the previous signal handlers.
212 for (unsigned i = 0; i != NumSignals; ++i)
Daniel Dunbar63cc2e12010-07-30 17:49:04 +0000213 sigaction(Signals[i], &PrevActions[i], 0);
Daniel Dunbard9082df2010-07-29 01:21:47 +0000214}
215
216#endif
217
Daniel Dunbara309dac2010-07-28 15:40:20 +0000218bool 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!");
Daniel Dunbara8fa7982010-08-17 22:32:37 +0000222 CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl(this);
Daniel Dunbara309dac2010-07-28 15:40:20 +0000223 Impl = CRCI;
224
225 if (setjmp(CRCI->JumpBuffer) != 0) {
226 return false;
227 }
228 }
229
230 Fn(UserData);
231 return true;
232}
233
234void CrashRecoveryContext::HandleCrash() {
235 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
236 assert(CRCI && "Crash recovery context never initialized!");
237 CRCI->HandleCrash();
238}
239
240const 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;
245}
Daniel Dunbarf8254d642010-11-05 07:19:09 +0000246
247//
248
249namespace {
250struct RunSafelyOnThreadInfo {
251 void (*UserFn)(void*);
252 void *UserData;
253 CrashRecoveryContext *CRC;
254 bool Result;
255};
256}
257
258static void RunSafelyOnThread_Dispatch(void *UserData) {
259 RunSafelyOnThreadInfo *Info =
260 reinterpret_cast<RunSafelyOnThreadInfo*>(UserData);
261 Info->Result = Info->CRC->RunSafely(Info->UserFn, Info->UserData);
262}
263bool 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);
267 return Info.Result;
268}