blob: 49258ede83c1df71b22ff45e7a3917e3574c2ac8 [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"
Daniel Dunbarc0c815e2010-08-17 22:32:34 +000013#include "llvm/System/Mutex.h"
Daniel Dunbard9082df2010-07-29 01:21:47 +000014#include "llvm/System/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
60CrashRecoveryContext::~CrashRecoveryContext() {
61 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
62 delete CRCI;
63}
64
Daniel Dunbara8fa7982010-08-17 22:32:37 +000065CrashRecoveryContext *CrashRecoveryContext::GetCurrent() {
66 const CrashRecoveryContextImpl *CRCI = CurrentContext.get();
67 if (!CRCI)
68 return 0;
69
70 return CRCI->CRC;
71}
72
Daniel Dunbard9082df2010-07-29 01:21:47 +000073#ifdef LLVM_ON_WIN32
74
75// FIXME: No real Win32 implementation currently.
76
Daniel Dunbara309dac2010-07-28 15:40:20 +000077void CrashRecoveryContext::Enable() {
Daniel Dunbarc0c815e2010-08-17 22:32:34 +000078 sys::ScopedLock L(gCrashRecoveryContexMutex);
79
Daniel Dunbara309dac2010-07-28 15:40:20 +000080 if (gCrashRecoveryEnabled)
81 return;
82
83 gCrashRecoveryEnabled = true;
84}
85
86void CrashRecoveryContext::Disable() {
Daniel Dunbarc0c815e2010-08-17 22:32:34 +000087 sys::ScopedLock L(gCrashRecoveryContexMutex);
88
Daniel Dunbara309dac2010-07-28 15:40:20 +000089 if (!gCrashRecoveryEnabled)
90 return;
91
92 gCrashRecoveryEnabled = false;
93}
94
Daniel Dunbard9082df2010-07-29 01:21:47 +000095#else
96
97// Generic POSIX implementation.
98//
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.
103//
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.
107
108#include <signal.h>
109
Daniel Dunbar63cc2e12010-07-30 17:49:04 +0000110static int Signals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP };
111static const unsigned NumSignals = sizeof(Signals) / sizeof(Signals[0]);
112static struct sigaction PrevActions[NumSignals];
Daniel Dunbard9082df2010-07-29 01:21:47 +0000113
114static void CrashRecoverySignalHandler(int Signal) {
115 // Lookup the current thread local recovery object.
116 const CrashRecoveryContextImpl *CRCI = CurrentContext.get();
117
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.
123 //
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.
127 //
128 // This call of Disable isn't thread safe, but it doesn't actually matter.
129 CrashRecoveryContext::Disable();
130 raise(Signal);
131 }
132
133 // Unblock the signal we received.
134 sigset_t SigMask;
135 sigemptyset(&SigMask);
136 sigaddset(&SigMask, Signal);
137 sigprocmask(SIG_UNBLOCK, &SigMask, 0);
138
139 if (CRCI)
140 const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash();
141}
142
143void CrashRecoveryContext::Enable() {
Daniel Dunbarc0c815e2010-08-17 22:32:34 +0000144 sys::ScopedLock L(gCrashRecoveryContexMutex);
145
Daniel Dunbard9082df2010-07-29 01:21:47 +0000146 if (gCrashRecoveryEnabled)
147 return;
148
149 gCrashRecoveryEnabled = true;
150
151 // Setup the signal handler.
152 struct sigaction Handler;
153 Handler.sa_handler = CrashRecoverySignalHandler;
154 Handler.sa_flags = 0;
155 sigemptyset(&Handler.sa_mask);
156
157 for (unsigned i = 0; i != NumSignals; ++i) {
Daniel Dunbar63cc2e12010-07-30 17:49:04 +0000158 sigaction(Signals[i], &Handler, &PrevActions[i]);
Daniel Dunbard9082df2010-07-29 01:21:47 +0000159 }
160}
161
162void CrashRecoveryContext::Disable() {
Daniel Dunbarc0c815e2010-08-17 22:32:34 +0000163 sys::ScopedLock L(gCrashRecoveryContexMutex);
164
Daniel Dunbard9082df2010-07-29 01:21:47 +0000165 if (!gCrashRecoveryEnabled)
166 return;
167
168 gCrashRecoveryEnabled = false;
169
170 // Restore the previous signal handlers.
171 for (unsigned i = 0; i != NumSignals; ++i)
Daniel Dunbar63cc2e12010-07-30 17:49:04 +0000172 sigaction(Signals[i], &PrevActions[i], 0);
Daniel Dunbard9082df2010-07-29 01:21:47 +0000173}
174
175#endif
176
Daniel Dunbara309dac2010-07-28 15:40:20 +0000177bool CrashRecoveryContext::RunSafely(void (*Fn)(void*), void *UserData) {
178 // If crash recovery is disabled, do nothing.
179 if (gCrashRecoveryEnabled) {
180 assert(!Impl && "Crash recovery context already initialized!");
Daniel Dunbara8fa7982010-08-17 22:32:37 +0000181 CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl(this);
Daniel Dunbara309dac2010-07-28 15:40:20 +0000182 Impl = CRCI;
183
184 if (setjmp(CRCI->JumpBuffer) != 0) {
185 return false;
186 }
187 }
188
189 Fn(UserData);
190 return true;
191}
192
193void CrashRecoveryContext::HandleCrash() {
194 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
195 assert(CRCI && "Crash recovery context never initialized!");
196 CRCI->HandleCrash();
197}
198
199const std::string &CrashRecoveryContext::getBacktrace() const {
200 CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *) Impl;
201 assert(CRC && "Crash recovery context never initialized!");
202 assert(CRC->Failed && "No crash was detected!");
203 return CRC->Backtrace;
204}