Make sure to include config.h, to pickup LLVM_ON_WIN32.
[oota-llvm.git] / lib / Support / CrashRecoveryContext.cpp
1 //===--- 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"
12 #include "llvm/Config/config.h"
13 #include "llvm/System/ThreadLocal.h"
14 #include <setjmp.h>
15 #include <cstdio>
16 using namespace llvm;
17
18 namespace {
19
20 struct CrashRecoveryContextImpl;
21
22 static sys::ThreadLocal<const CrashRecoveryContextImpl> CurrentContext;
23
24 struct CrashRecoveryContextImpl {
25   std::string Backtrace;
26   ::jmp_buf JumpBuffer;
27   volatile unsigned Failed : 1;
28
29 public:
30   CrashRecoveryContextImpl() : Failed(false) {
31     CurrentContext.set(this);
32   }
33   ~CrashRecoveryContextImpl() {
34     CurrentContext.set(0);
35   }
36
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
50 static bool gCrashRecoveryEnabled = false;
51
52 CrashRecoveryContext::~CrashRecoveryContext() {
53   CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
54   delete CRCI;
55 }
56
57 #ifdef LLVM_ON_WIN32
58
59 // FIXME: No real Win32 implementation currently.
60
61 void CrashRecoveryContext::Enable() {
62   if (gCrashRecoveryEnabled)
63     return;
64
65   gCrashRecoveryEnabled = true;
66 }
67
68 void CrashRecoveryContext::Disable() {
69   if (!gCrashRecoveryEnabled)
70     return;
71
72   gCrashRecoveryEnabled = false;
73 }
74
75 #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
90 static struct {
91   int Signal;
92   struct sigaction PrevAction;
93 } SignalInfo[] = {
94   { SIGABRT, {} },
95   { SIGBUS,  {} },
96   { SIGFPE,  {} },
97   { SIGILL,  {} },
98   { SIGSEGV, {} },
99   { SIGTRAP, {} },
100 };
101 static const unsigned NumSignals = sizeof(SignalInfo) / sizeof(SignalInfo[0]);
102
103 static void CrashRecoverySignalHandler(int Signal) {
104   // Lookup the current thread local recovery object.
105   const CrashRecoveryContextImpl *CRCI = CurrentContext.get();
106
107   if (!CRCI) {
108     // We didn't find a crash recovery context -- this means either we got a
109     // signal on a thread we didn't expect it on, the application got a signal
110     // outside of a crash recovery context, or something else went horribly
111     // wrong.
112     //
113     // Disable crash recovery and raise the signal again. The assumption here is
114     // that the enclosing application will terminate soon, and we won't want to
115     // attempt crash recovery again.
116     //
117     // This call of Disable isn't thread safe, but it doesn't actually matter.
118     CrashRecoveryContext::Disable();
119     raise(Signal);
120   }
121
122   // Unblock the signal we received.
123   sigset_t SigMask;
124   sigemptyset(&SigMask);
125   sigaddset(&SigMask, Signal);
126   sigprocmask(SIG_UNBLOCK, &SigMask, 0);
127
128   if (CRCI)
129     const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash();
130 }
131
132 void CrashRecoveryContext::Enable() {
133   if (gCrashRecoveryEnabled)
134     return;
135
136   gCrashRecoveryEnabled = true;
137
138   // Setup the signal handler.
139   struct sigaction Handler;
140   Handler.sa_handler = CrashRecoverySignalHandler;
141   Handler.sa_flags = 0;
142   sigemptyset(&Handler.sa_mask);
143
144   for (unsigned i = 0; i != NumSignals; ++i) {
145     sigaction(SignalInfo[i].Signal, &Handler,
146               &SignalInfo[i].PrevAction);
147   }
148 }
149
150 void CrashRecoveryContext::Disable() {
151   if (!gCrashRecoveryEnabled)
152     return;
153
154   gCrashRecoveryEnabled = false;
155
156   // Restore the previous signal handlers.
157   for (unsigned i = 0; i != NumSignals; ++i)
158     sigaction(SignalInfo[i].Signal, &SignalInfo[i].PrevAction, 0);
159 }
160
161 #endif
162
163 bool 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
179 void CrashRecoveryContext::HandleCrash() {
180   CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
181   assert(CRCI && "Crash recovery context never initialized!");
182   CRCI->HandleCrash();
183 }
184
185 const 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 }