blob: d4e21a3a82672b82175ad91fad1b89a246079b03 [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() {
Ted Kremenekfb200e32011-03-19 00:59:33 +000077 if (!gCrashRecoveryEnabled)
78 return 0;
79
Daniel Dunbara8fa7982010-08-17 22:32:37 +000080 const CrashRecoveryContextImpl *CRCI = CurrentContext.get();
81 if (!CRCI)
82 return 0;
83
84 return CRCI->CRC;
85}
86
Ted Kremeneka4f98392011-03-18 02:05:11 +000087void CrashRecoveryContext::registerCleanup(CrashRecoveryContextCleanup *cleanup)
88{
89 if (!cleanup)
90 return;
91 if (head)
92 head->prev = cleanup;
93 cleanup->next = head;
94 head = cleanup;
95}
96
97void
98CrashRecoveryContext::unregisterCleanup(CrashRecoveryContextCleanup *cleanup) {
99 if (!cleanup)
100 return;
101 if (cleanup == head) {
102 head = cleanup->next;
103 if (head)
104 head->prev = 0;
105 }
106 else {
107 cleanup->prev->next = cleanup->next;
108 if (cleanup->next)
109 cleanup->next->prev = cleanup->prev;
110 }
111 delete cleanup;
112}
113
Daniel Dunbard9082df2010-07-29 01:21:47 +0000114#ifdef LLVM_ON_WIN32
115
116// FIXME: No real Win32 implementation currently.
117
Daniel Dunbara309dac2010-07-28 15:40:20 +0000118void CrashRecoveryContext::Enable() {
Daniel Dunbarc0c815e2010-08-17 22:32:34 +0000119 sys::ScopedLock L(gCrashRecoveryContexMutex);
120
Daniel Dunbara309dac2010-07-28 15:40:20 +0000121 if (gCrashRecoveryEnabled)
122 return;
123
124 gCrashRecoveryEnabled = true;
125}
126
127void CrashRecoveryContext::Disable() {
Daniel Dunbarc0c815e2010-08-17 22:32:34 +0000128 sys::ScopedLock L(gCrashRecoveryContexMutex);
129
Daniel Dunbara309dac2010-07-28 15:40:20 +0000130 if (!gCrashRecoveryEnabled)
131 return;
132
133 gCrashRecoveryEnabled = false;
134}
135
Daniel Dunbard9082df2010-07-29 01:21:47 +0000136#else
137
138// Generic POSIX implementation.
139//
140// This implementation relies on synchronous signals being delivered to the
141// current thread. We use a thread local object to keep track of the active
142// crash recovery context, and install signal handlers to invoke HandleCrash on
143// the active object.
144//
145// This implementation does not to attempt to chain signal handlers in any
146// reliable fashion -- if we get a signal outside of a crash recovery context we
147// simply disable crash recovery and raise the signal again.
148
149#include <signal.h>
150
Daniel Dunbar63cc2e12010-07-30 17:49:04 +0000151static int Signals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP };
152static const unsigned NumSignals = sizeof(Signals) / sizeof(Signals[0]);
153static struct sigaction PrevActions[NumSignals];
Daniel Dunbard9082df2010-07-29 01:21:47 +0000154
155static void CrashRecoverySignalHandler(int Signal) {
156 // Lookup the current thread local recovery object.
157 const CrashRecoveryContextImpl *CRCI = CurrentContext.get();
158
159 if (!CRCI) {
160 // We didn't find a crash recovery context -- this means either we got a
161 // signal on a thread we didn't expect it on, the application got a signal
162 // outside of a crash recovery context, or something else went horribly
163 // wrong.
164 //
165 // Disable crash recovery and raise the signal again. The assumption here is
166 // that the enclosing application will terminate soon, and we won't want to
167 // attempt crash recovery again.
168 //
169 // This call of Disable isn't thread safe, but it doesn't actually matter.
170 CrashRecoveryContext::Disable();
171 raise(Signal);
Daniel Dunbard49e2aa2010-10-18 21:55:18 +0000172
173 // The signal will be thrown once the signal mask is restored.
174 return;
Daniel Dunbard9082df2010-07-29 01:21:47 +0000175 }
176
177 // Unblock the signal we received.
178 sigset_t SigMask;
179 sigemptyset(&SigMask);
180 sigaddset(&SigMask, Signal);
181 sigprocmask(SIG_UNBLOCK, &SigMask, 0);
182
183 if (CRCI)
184 const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash();
185}
186
187void CrashRecoveryContext::Enable() {
Daniel Dunbarc0c815e2010-08-17 22:32:34 +0000188 sys::ScopedLock L(gCrashRecoveryContexMutex);
189
Daniel Dunbard9082df2010-07-29 01:21:47 +0000190 if (gCrashRecoveryEnabled)
191 return;
192
193 gCrashRecoveryEnabled = true;
194
195 // Setup the signal handler.
196 struct sigaction Handler;
197 Handler.sa_handler = CrashRecoverySignalHandler;
198 Handler.sa_flags = 0;
199 sigemptyset(&Handler.sa_mask);
200
201 for (unsigned i = 0; i != NumSignals; ++i) {
Daniel Dunbar63cc2e12010-07-30 17:49:04 +0000202 sigaction(Signals[i], &Handler, &PrevActions[i]);
Daniel Dunbard9082df2010-07-29 01:21:47 +0000203 }
204}
205
206void CrashRecoveryContext::Disable() {
Daniel Dunbarc0c815e2010-08-17 22:32:34 +0000207 sys::ScopedLock L(gCrashRecoveryContexMutex);
208
Daniel Dunbard9082df2010-07-29 01:21:47 +0000209 if (!gCrashRecoveryEnabled)
210 return;
211
212 gCrashRecoveryEnabled = false;
213
214 // Restore the previous signal handlers.
215 for (unsigned i = 0; i != NumSignals; ++i)
Daniel Dunbar63cc2e12010-07-30 17:49:04 +0000216 sigaction(Signals[i], &PrevActions[i], 0);
Daniel Dunbard9082df2010-07-29 01:21:47 +0000217}
218
219#endif
220
Daniel Dunbara309dac2010-07-28 15:40:20 +0000221bool CrashRecoveryContext::RunSafely(void (*Fn)(void*), void *UserData) {
222 // If crash recovery is disabled, do nothing.
223 if (gCrashRecoveryEnabled) {
224 assert(!Impl && "Crash recovery context already initialized!");
Daniel Dunbara8fa7982010-08-17 22:32:37 +0000225 CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl(this);
Daniel Dunbara309dac2010-07-28 15:40:20 +0000226 Impl = CRCI;
227
228 if (setjmp(CRCI->JumpBuffer) != 0) {
229 return false;
230 }
231 }
232
233 Fn(UserData);
234 return true;
235}
236
237void CrashRecoveryContext::HandleCrash() {
238 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
239 assert(CRCI && "Crash recovery context never initialized!");
240 CRCI->HandleCrash();
241}
242
243const std::string &CrashRecoveryContext::getBacktrace() const {
244 CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *) Impl;
245 assert(CRC && "Crash recovery context never initialized!");
246 assert(CRC->Failed && "No crash was detected!");
247 return CRC->Backtrace;
248}
Daniel Dunbarf8254d642010-11-05 07:19:09 +0000249
250//
251
252namespace {
253struct RunSafelyOnThreadInfo {
254 void (*UserFn)(void*);
255 void *UserData;
256 CrashRecoveryContext *CRC;
257 bool Result;
258};
259}
260
261static void RunSafelyOnThread_Dispatch(void *UserData) {
262 RunSafelyOnThreadInfo *Info =
263 reinterpret_cast<RunSafelyOnThreadInfo*>(UserData);
264 Info->Result = Info->CRC->RunSafely(Info->UserFn, Info->UserData);
265}
266bool CrashRecoveryContext::RunSafelyOnThread(void (*Fn)(void*), void *UserData,
267 unsigned RequestedStackSize) {
268 RunSafelyOnThreadInfo Info = { Fn, UserData, this, false };
269 llvm_execute_on_thread(RunSafelyOnThread_Dispatch, &Info, RequestedStackSize);
270 return Info.Result;
271}