blob: de98132539416fa49a11c23565aebd883281a19b [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 Dunbard9082df2010-07-29 01:21:47 +000013#include "llvm/System/ThreadLocal.h"
Daniel Dunbara309dac2010-07-28 15:40:20 +000014#include <setjmp.h>
Daniel Dunbard9082df2010-07-29 01:21:47 +000015#include <cstdio>
Daniel Dunbara309dac2010-07-28 15:40:20 +000016using namespace llvm;
17
18namespace {
19
20struct CrashRecoveryContextImpl;
21
Daniel Dunbard9082df2010-07-29 01:21:47 +000022static sys::ThreadLocal<const CrashRecoveryContextImpl> CurrentContext;
23
Daniel Dunbara309dac2010-07-28 15:40:20 +000024struct CrashRecoveryContextImpl {
25 std::string Backtrace;
26 ::jmp_buf JumpBuffer;
27 volatile unsigned Failed : 1;
28
29public:
Daniel Dunbard9082df2010-07-29 01:21:47 +000030 CrashRecoveryContextImpl() : Failed(false) {
31 CurrentContext.set(this);
32 }
33 ~CrashRecoveryContextImpl() {
Daniel Dunbara6855822010-07-29 15:24:21 +000034 CurrentContext.erase();
Daniel Dunbard9082df2010-07-29 01:21:47 +000035 }
Daniel Dunbara309dac2010-07-28 15:40:20 +000036
37 void HandleCrash() {
38 assert(!Failed && "Crash recovery context already failed!");
39 Failed = true;
40
41 // FIXME: Stash the backtrace.
42
43 // Jump back to the RunSafely we were called under.
44 longjmp(JumpBuffer, 1);
45 }
46};
47
48}
49
50static bool gCrashRecoveryEnabled = false;
51
52CrashRecoveryContext::~CrashRecoveryContext() {
53 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
54 delete CRCI;
55}
56
Daniel Dunbard9082df2010-07-29 01:21:47 +000057#ifdef LLVM_ON_WIN32
58
59// FIXME: No real Win32 implementation currently.
60
Daniel Dunbara309dac2010-07-28 15:40:20 +000061void CrashRecoveryContext::Enable() {
62 if (gCrashRecoveryEnabled)
63 return;
64
65 gCrashRecoveryEnabled = true;
66}
67
68void CrashRecoveryContext::Disable() {
69 if (!gCrashRecoveryEnabled)
70 return;
71
72 gCrashRecoveryEnabled = false;
73}
74
Daniel Dunbard9082df2010-07-29 01:21:47 +000075#else
76
77// Generic POSIX implementation.
78//
79// This implementation relies on synchronous signals being delivered to the
80// current thread. We use a thread local object to keep track of the active
81// crash recovery context, and install signal handlers to invoke HandleCrash on
82// the active object.
83//
84// This implementation does not to attempt to chain signal handlers in any
85// reliable fashion -- if we get a signal outside of a crash recovery context we
86// simply disable crash recovery and raise the signal again.
87
88#include <signal.h>
89
Daniel Dunbar63cc2e12010-07-30 17:49:04 +000090static int Signals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP };
91static const unsigned NumSignals = sizeof(Signals) / sizeof(Signals[0]);
92static struct sigaction PrevActions[NumSignals];
Daniel Dunbard9082df2010-07-29 01:21:47 +000093
94static void CrashRecoverySignalHandler(int Signal) {
95 // Lookup the current thread local recovery object.
96 const CrashRecoveryContextImpl *CRCI = CurrentContext.get();
97
98 if (!CRCI) {
99 // We didn't find a crash recovery context -- this means either we got a
100 // signal on a thread we didn't expect it on, the application got a signal
101 // outside of a crash recovery context, or something else went horribly
102 // wrong.
103 //
104 // Disable crash recovery and raise the signal again. The assumption here is
105 // that the enclosing application will terminate soon, and we won't want to
106 // attempt crash recovery again.
107 //
108 // This call of Disable isn't thread safe, but it doesn't actually matter.
109 CrashRecoveryContext::Disable();
110 raise(Signal);
111 }
112
113 // Unblock the signal we received.
114 sigset_t SigMask;
115 sigemptyset(&SigMask);
116 sigaddset(&SigMask, Signal);
117 sigprocmask(SIG_UNBLOCK, &SigMask, 0);
118
119 if (CRCI)
120 const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash();
121}
122
123void CrashRecoveryContext::Enable() {
124 if (gCrashRecoveryEnabled)
125 return;
126
127 gCrashRecoveryEnabled = true;
128
129 // Setup the signal handler.
130 struct sigaction Handler;
131 Handler.sa_handler = CrashRecoverySignalHandler;
132 Handler.sa_flags = 0;
133 sigemptyset(&Handler.sa_mask);
134
135 for (unsigned i = 0; i != NumSignals; ++i) {
Daniel Dunbar63cc2e12010-07-30 17:49:04 +0000136 sigaction(Signals[i], &Handler, &PrevActions[i]);
Daniel Dunbard9082df2010-07-29 01:21:47 +0000137 }
138}
139
140void CrashRecoveryContext::Disable() {
141 if (!gCrashRecoveryEnabled)
142 return;
143
144 gCrashRecoveryEnabled = false;
145
146 // Restore the previous signal handlers.
147 for (unsigned i = 0; i != NumSignals; ++i)
Daniel Dunbar63cc2e12010-07-30 17:49:04 +0000148 sigaction(Signals[i], &PrevActions[i], 0);
Daniel Dunbard9082df2010-07-29 01:21:47 +0000149}
150
151#endif
152
Daniel Dunbara309dac2010-07-28 15:40:20 +0000153bool CrashRecoveryContext::RunSafely(void (*Fn)(void*), void *UserData) {
154 // If crash recovery is disabled, do nothing.
155 if (gCrashRecoveryEnabled) {
156 assert(!Impl && "Crash recovery context already initialized!");
157 CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl;
158 Impl = CRCI;
159
160 if (setjmp(CRCI->JumpBuffer) != 0) {
161 return false;
162 }
163 }
164
165 Fn(UserData);
166 return true;
167}
168
169void CrashRecoveryContext::HandleCrash() {
170 CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
171 assert(CRCI && "Crash recovery context never initialized!");
172 CRCI->HandleCrash();
173}
174
175const std::string &CrashRecoveryContext::getBacktrace() const {
176 CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *) Impl;
177 assert(CRC && "Crash recovery context never initialized!");
178 assert(CRC->Failed && "No crash was detected!");
179 return CRC->Backtrace;
180}