blob: 296cc3e8f3bc1358bab4192bb6330e079842d7c1 [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() {
41 assert(!Failed && "Crash recovery context already failed!");
42 Failed = true;
43
44 // FIXME: Stash the backtrace.
45
46 // Jump back to the RunSafely we were called under.
47 longjmp(JumpBuffer, 1);
48 }
49};
50
51}
52
Daniel Dunbarc0c815e2010-08-17 22:32:34 +000053static sys::Mutex gCrashRecoveryContexMutex;
Daniel Dunbara309dac2010-07-28 15:40:20 +000054static bool gCrashRecoveryEnabled = false;
55
56CrashRecoveryContext::~CrashRecoveryContext() {
57 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
58 delete CRCI;
59}
60
Daniel Dunbara8fa7982010-08-17 22:32:37 +000061CrashRecoveryContext *CrashRecoveryContext::GetCurrent() {
62 const CrashRecoveryContextImpl *CRCI = CurrentContext.get();
63 if (!CRCI)
64 return 0;
65
66 return CRCI->CRC;
67}
68
Daniel Dunbard9082df2010-07-29 01:21:47 +000069#ifdef LLVM_ON_WIN32
70
71// FIXME: No real Win32 implementation currently.
72
Daniel Dunbara309dac2010-07-28 15:40:20 +000073void CrashRecoveryContext::Enable() {
Daniel Dunbarc0c815e2010-08-17 22:32:34 +000074 sys::ScopedLock L(gCrashRecoveryContexMutex);
75
Daniel Dunbara309dac2010-07-28 15:40:20 +000076 if (gCrashRecoveryEnabled)
77 return;
78
79 gCrashRecoveryEnabled = true;
80}
81
82void CrashRecoveryContext::Disable() {
Daniel Dunbarc0c815e2010-08-17 22:32:34 +000083 sys::ScopedLock L(gCrashRecoveryContexMutex);
84
Daniel Dunbara309dac2010-07-28 15:40:20 +000085 if (!gCrashRecoveryEnabled)
86 return;
87
88 gCrashRecoveryEnabled = false;
89}
90
Daniel Dunbard9082df2010-07-29 01:21:47 +000091#else
92
93// Generic POSIX implementation.
94//
95// This implementation relies on synchronous signals being delivered to the
96// current thread. We use a thread local object to keep track of the active
97// crash recovery context, and install signal handlers to invoke HandleCrash on
98// the active object.
99//
100// This implementation does not to attempt to chain signal handlers in any
101// reliable fashion -- if we get a signal outside of a crash recovery context we
102// simply disable crash recovery and raise the signal again.
103
104#include <signal.h>
105
Daniel Dunbar63cc2e12010-07-30 17:49:04 +0000106static int Signals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP };
107static const unsigned NumSignals = sizeof(Signals) / sizeof(Signals[0]);
108static struct sigaction PrevActions[NumSignals];
Daniel Dunbard9082df2010-07-29 01:21:47 +0000109
110static void CrashRecoverySignalHandler(int Signal) {
111 // Lookup the current thread local recovery object.
112 const CrashRecoveryContextImpl *CRCI = CurrentContext.get();
113
114 if (!CRCI) {
115 // We didn't find a crash recovery context -- this means either we got a
116 // signal on a thread we didn't expect it on, the application got a signal
117 // outside of a crash recovery context, or something else went horribly
118 // wrong.
119 //
120 // Disable crash recovery and raise the signal again. The assumption here is
121 // that the enclosing application will terminate soon, and we won't want to
122 // attempt crash recovery again.
123 //
124 // This call of Disable isn't thread safe, but it doesn't actually matter.
125 CrashRecoveryContext::Disable();
126 raise(Signal);
127 }
128
129 // Unblock the signal we received.
130 sigset_t SigMask;
131 sigemptyset(&SigMask);
132 sigaddset(&SigMask, Signal);
133 sigprocmask(SIG_UNBLOCK, &SigMask, 0);
134
135 if (CRCI)
136 const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash();
137}
138
139void CrashRecoveryContext::Enable() {
Daniel Dunbarc0c815e2010-08-17 22:32:34 +0000140 sys::ScopedLock L(gCrashRecoveryContexMutex);
141
Daniel Dunbard9082df2010-07-29 01:21:47 +0000142 if (gCrashRecoveryEnabled)
143 return;
144
145 gCrashRecoveryEnabled = true;
146
147 // Setup the signal handler.
148 struct sigaction Handler;
149 Handler.sa_handler = CrashRecoverySignalHandler;
150 Handler.sa_flags = 0;
151 sigemptyset(&Handler.sa_mask);
152
153 for (unsigned i = 0; i != NumSignals; ++i) {
Daniel Dunbar63cc2e12010-07-30 17:49:04 +0000154 sigaction(Signals[i], &Handler, &PrevActions[i]);
Daniel Dunbard9082df2010-07-29 01:21:47 +0000155 }
156}
157
158void CrashRecoveryContext::Disable() {
Daniel Dunbarc0c815e2010-08-17 22:32:34 +0000159 sys::ScopedLock L(gCrashRecoveryContexMutex);
160
Daniel Dunbard9082df2010-07-29 01:21:47 +0000161 if (!gCrashRecoveryEnabled)
162 return;
163
164 gCrashRecoveryEnabled = false;
165
166 // Restore the previous signal handlers.
167 for (unsigned i = 0; i != NumSignals; ++i)
Daniel Dunbar63cc2e12010-07-30 17:49:04 +0000168 sigaction(Signals[i], &PrevActions[i], 0);
Daniel Dunbard9082df2010-07-29 01:21:47 +0000169}
170
171#endif
172
Daniel Dunbara309dac2010-07-28 15:40:20 +0000173bool CrashRecoveryContext::RunSafely(void (*Fn)(void*), void *UserData) {
174 // If crash recovery is disabled, do nothing.
175 if (gCrashRecoveryEnabled) {
176 assert(!Impl && "Crash recovery context already initialized!");
Daniel Dunbara8fa7982010-08-17 22:32:37 +0000177 CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl(this);
Daniel Dunbara309dac2010-07-28 15:40:20 +0000178 Impl = CRCI;
179
180 if (setjmp(CRCI->JumpBuffer) != 0) {
181 return false;
182 }
183 }
184
185 Fn(UserData);
186 return true;
187}
188
189void CrashRecoveryContext::HandleCrash() {
190 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
191 assert(CRCI && "Crash recovery context never initialized!");
192 CRCI->HandleCrash();
193}
194
195const std::string &CrashRecoveryContext::getBacktrace() const {
196 CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *) Impl;
197 assert(CRC && "Crash recovery context never initialized!");
198 assert(CRC->Failed && "No crash was detected!");
199 return CRC->Backtrace;
200}