blob: d1ecbb74dacab4affa59bf973e80deddbde03f02 [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 {
26 std::string Backtrace;
27 ::jmp_buf JumpBuffer;
28 volatile unsigned Failed : 1;
29
30public:
Daniel Dunbard9082df2010-07-29 01:21:47 +000031 CrashRecoveryContextImpl() : Failed(false) {
32 CurrentContext.set(this);
33 }
34 ~CrashRecoveryContextImpl() {
Daniel Dunbara6855822010-07-29 15:24:21 +000035 CurrentContext.erase();
Daniel Dunbard9082df2010-07-29 01:21:47 +000036 }
Daniel Dunbara309dac2010-07-28 15:40:20 +000037
38 void HandleCrash() {
39 assert(!Failed && "Crash recovery context already failed!");
40 Failed = true;
41
42 // FIXME: Stash the backtrace.
43
44 // Jump back to the RunSafely we were called under.
45 longjmp(JumpBuffer, 1);
46 }
47};
48
49}
50
Daniel Dunbarc0c815e2010-08-17 22:32:34 +000051static sys::Mutex gCrashRecoveryContexMutex;
Daniel Dunbara309dac2010-07-28 15:40:20 +000052static bool gCrashRecoveryEnabled = false;
53
54CrashRecoveryContext::~CrashRecoveryContext() {
55 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
56 delete CRCI;
57}
58
Daniel Dunbard9082df2010-07-29 01:21:47 +000059#ifdef LLVM_ON_WIN32
60
61// FIXME: No real Win32 implementation currently.
62
Daniel Dunbara309dac2010-07-28 15:40:20 +000063void CrashRecoveryContext::Enable() {
Daniel Dunbarc0c815e2010-08-17 22:32:34 +000064 sys::ScopedLock L(gCrashRecoveryContexMutex);
65
Daniel Dunbara309dac2010-07-28 15:40:20 +000066 if (gCrashRecoveryEnabled)
67 return;
68
69 gCrashRecoveryEnabled = true;
70}
71
72void CrashRecoveryContext::Disable() {
Daniel Dunbarc0c815e2010-08-17 22:32:34 +000073 sys::ScopedLock L(gCrashRecoveryContexMutex);
74
Daniel Dunbara309dac2010-07-28 15:40:20 +000075 if (!gCrashRecoveryEnabled)
76 return;
77
78 gCrashRecoveryEnabled = false;
79}
80
Daniel Dunbard9082df2010-07-29 01:21:47 +000081#else
82
83// Generic POSIX implementation.
84//
85// This implementation relies on synchronous signals being delivered to the
86// current thread. We use a thread local object to keep track of the active
87// crash recovery context, and install signal handlers to invoke HandleCrash on
88// the active object.
89//
90// This implementation does not to attempt to chain signal handlers in any
91// reliable fashion -- if we get a signal outside of a crash recovery context we
92// simply disable crash recovery and raise the signal again.
93
94#include <signal.h>
95
Daniel Dunbar63cc2e12010-07-30 17:49:04 +000096static int Signals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP };
97static const unsigned NumSignals = sizeof(Signals) / sizeof(Signals[0]);
98static struct sigaction PrevActions[NumSignals];
Daniel Dunbard9082df2010-07-29 01:21:47 +000099
100static void CrashRecoverySignalHandler(int Signal) {
101 // Lookup the current thread local recovery object.
102 const CrashRecoveryContextImpl *CRCI = CurrentContext.get();
103
104 if (!CRCI) {
105 // We didn't find a crash recovery context -- this means either we got a
106 // signal on a thread we didn't expect it on, the application got a signal
107 // outside of a crash recovery context, or something else went horribly
108 // wrong.
109 //
110 // Disable crash recovery and raise the signal again. The assumption here is
111 // that the enclosing application will terminate soon, and we won't want to
112 // attempt crash recovery again.
113 //
114 // This call of Disable isn't thread safe, but it doesn't actually matter.
115 CrashRecoveryContext::Disable();
116 raise(Signal);
117 }
118
119 // Unblock the signal we received.
120 sigset_t SigMask;
121 sigemptyset(&SigMask);
122 sigaddset(&SigMask, Signal);
123 sigprocmask(SIG_UNBLOCK, &SigMask, 0);
124
125 if (CRCI)
126 const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash();
127}
128
129void CrashRecoveryContext::Enable() {
Daniel Dunbarc0c815e2010-08-17 22:32:34 +0000130 sys::ScopedLock L(gCrashRecoveryContexMutex);
131
Daniel Dunbard9082df2010-07-29 01:21:47 +0000132 if (gCrashRecoveryEnabled)
133 return;
134
135 gCrashRecoveryEnabled = true;
136
137 // Setup the signal handler.
138 struct sigaction Handler;
139 Handler.sa_handler = CrashRecoverySignalHandler;
140 Handler.sa_flags = 0;
141 sigemptyset(&Handler.sa_mask);
142
143 for (unsigned i = 0; i != NumSignals; ++i) {
Daniel Dunbar63cc2e12010-07-30 17:49:04 +0000144 sigaction(Signals[i], &Handler, &PrevActions[i]);
Daniel Dunbard9082df2010-07-29 01:21:47 +0000145 }
146}
147
148void CrashRecoveryContext::Disable() {
Daniel Dunbarc0c815e2010-08-17 22:32:34 +0000149 sys::ScopedLock L(gCrashRecoveryContexMutex);
150
Daniel Dunbard9082df2010-07-29 01:21:47 +0000151 if (!gCrashRecoveryEnabled)
152 return;
153
154 gCrashRecoveryEnabled = false;
155
156 // Restore the previous signal handlers.
157 for (unsigned i = 0; i != NumSignals; ++i)
Daniel Dunbar63cc2e12010-07-30 17:49:04 +0000158 sigaction(Signals[i], &PrevActions[i], 0);
Daniel Dunbard9082df2010-07-29 01:21:47 +0000159}
160
161#endif
162
Daniel Dunbara309dac2010-07-28 15:40:20 +0000163bool CrashRecoveryContext::RunSafely(void (*Fn)(void*), void *UserData) {
164 // If crash recovery is disabled, do nothing.
165 if (gCrashRecoveryEnabled) {
166 assert(!Impl && "Crash recovery context already initialized!");
167 CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl;
168 Impl = CRCI;
169
170 if (setjmp(CRCI->JumpBuffer) != 0) {
171 return false;
172 }
173 }
174
175 Fn(UserData);
176 return true;
177}
178
179void CrashRecoveryContext::HandleCrash() {
180 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
181 assert(CRCI && "Crash recovery context never initialized!");
182 CRCI->HandleCrash();
183}
184
185const std::string &CrashRecoveryContext::getBacktrace() const {
186 CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *) Impl;
187 assert(CRC && "Crash recovery context never initialized!");
188 assert(CRC->Failed && "No crash was detected!");
189 return CRC->Backtrace;
190}