Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 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" |
Daniel Dunbar | 9789f81 | 2010-07-29 01:52:04 +0000 | [diff] [blame] | 11 | #include "llvm/Config/config.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 12 | #include "llvm/Support/ErrorHandling.h" |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 13 | #include "llvm/Support/ManagedStatic.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Mutex.h" |
| 15 | #include "llvm/Support/ThreadLocal.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include <setjmp.h> |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | struct CrashRecoveryContextImpl; |
| 22 | |
Zachary Turner | a40ccf6 | 2014-06-10 18:03:04 +0000 | [diff] [blame] | 23 | static ManagedStatic< |
| 24 | sys::ThreadLocal<const CrashRecoveryContextImpl> > CurrentContext; |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 25 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 26 | struct CrashRecoveryContextImpl { |
Nico Weber | 2692811 | 2015-08-07 17:32:06 +0000 | [diff] [blame] | 27 | // When threads are disabled, this links up all active |
| 28 | // CrashRecoveryContextImpls. When threads are enabled there's one thread |
| 29 | // per CrashRecoveryContext and CurrentContext is a thread-local, so only one |
| 30 | // CrashRecoveryContextImpl is active per thread and this is always null. |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 31 | const CrashRecoveryContextImpl *Next; |
| 32 | |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 33 | CrashRecoveryContext *CRC; |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 34 | std::string Backtrace; |
| 35 | ::jmp_buf JumpBuffer; |
| 36 | volatile unsigned Failed : 1; |
Argyrios Kyrtzidis | f1d8f52 | 2013-06-19 22:53:45 +0000 | [diff] [blame] | 37 | unsigned SwitchedThread : 1; |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 38 | |
| 39 | public: |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 40 | CrashRecoveryContextImpl(CrashRecoveryContext *CRC) : CRC(CRC), |
Argyrios Kyrtzidis | f1d8f52 | 2013-06-19 22:53:45 +0000 | [diff] [blame] | 41 | Failed(false), |
| 42 | SwitchedThread(false) { |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 43 | Next = CurrentContext->get(); |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 44 | CurrentContext->set(this); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 45 | } |
| 46 | ~CrashRecoveryContextImpl() { |
Argyrios Kyrtzidis | f1d8f52 | 2013-06-19 22:53:45 +0000 | [diff] [blame] | 47 | if (!SwitchedThread) |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 48 | CurrentContext->set(Next); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 49 | } |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 50 | |
Argyrios Kyrtzidis | f1d8f52 | 2013-06-19 22:53:45 +0000 | [diff] [blame] | 51 | /// \brief Called when the separate crash-recovery thread was finished, to |
| 52 | /// indicate that we don't need to clear the thread-local CurrentContext. |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 53 | void setSwitchedThread() { |
| 54 | #if defined(LLVM_ENABLE_THREADS) && LLVM_ENABLE_THREADS != 0 |
| 55 | SwitchedThread = true; |
| 56 | #endif |
| 57 | } |
Argyrios Kyrtzidis | f1d8f52 | 2013-06-19 22:53:45 +0000 | [diff] [blame] | 58 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 59 | void HandleCrash() { |
Daniel Dunbar | 09b0c78 | 2010-08-17 22:32:39 +0000 | [diff] [blame] | 60 | // Eliminate the current context entry, to avoid re-entering in case the |
| 61 | // cleanup code crashes. |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 62 | CurrentContext->set(Next); |
Daniel Dunbar | 09b0c78 | 2010-08-17 22:32:39 +0000 | [diff] [blame] | 63 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 64 | assert(!Failed && "Crash recovery context already failed!"); |
| 65 | Failed = true; |
| 66 | |
| 67 | // FIXME: Stash the backtrace. |
| 68 | |
| 69 | // Jump back to the RunSafely we were called under. |
| 70 | longjmp(JumpBuffer, 1); |
| 71 | } |
| 72 | }; |
| 73 | |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 74 | } |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 75 | |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 76 | static ManagedStatic<sys::Mutex> gCrashRecoveryContextMutex; |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 77 | static bool gCrashRecoveryEnabled = false; |
| 78 | |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 79 | static ManagedStatic<sys::ThreadLocal<const CrashRecoveryContext>> |
Ted Kremenek | ab1a242 | 2011-03-21 18:38:03 +0000 | [diff] [blame] | 80 | tlIsRecoveringFromCrash; |
| 81 | |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 82 | CrashRecoveryContextCleanup::~CrashRecoveryContextCleanup() {} |
| 83 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 84 | CrashRecoveryContext::~CrashRecoveryContext() { |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 85 | // Reclaim registered resources. |
| 86 | CrashRecoveryContextCleanup *i = head; |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 87 | const CrashRecoveryContext *PC = tlIsRecoveringFromCrash->get(); |
| 88 | tlIsRecoveringFromCrash->set(this); |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 89 | while (i) { |
| 90 | CrashRecoveryContextCleanup *tmp = i; |
| 91 | i = tmp->next; |
Ted Kremenek | 32aea2e | 2011-03-19 00:59:37 +0000 | [diff] [blame] | 92 | tmp->cleanupFired = true; |
Ted Kremenek | 857e535 | 2011-03-22 04:33:13 +0000 | [diff] [blame] | 93 | tmp->recoverResources(); |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 94 | delete tmp; |
| 95 | } |
Nico Weber | 28dc417 | 2015-08-06 19:21:25 +0000 | [diff] [blame] | 96 | tlIsRecoveringFromCrash->set(PC); |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 97 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 98 | CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl; |
| 99 | delete CRCI; |
| 100 | } |
| 101 | |
Ted Kremenek | ab1a242 | 2011-03-21 18:38:03 +0000 | [diff] [blame] | 102 | bool CrashRecoveryContext::isRecoveringFromCrash() { |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 103 | return tlIsRecoveringFromCrash->get() != nullptr; |
Ted Kremenek | ab1a242 | 2011-03-21 18:38:03 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 106 | CrashRecoveryContext *CrashRecoveryContext::GetCurrent() { |
Ted Kremenek | 794a071 | 2011-03-19 00:59:33 +0000 | [diff] [blame] | 107 | if (!gCrashRecoveryEnabled) |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 108 | return nullptr; |
Ted Kremenek | 794a071 | 2011-03-19 00:59:33 +0000 | [diff] [blame] | 109 | |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 110 | const CrashRecoveryContextImpl *CRCI = CurrentContext->get(); |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 111 | if (!CRCI) |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 112 | return nullptr; |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 113 | |
| 114 | return CRCI->CRC; |
| 115 | } |
| 116 | |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 117 | void CrashRecoveryContext::registerCleanup(CrashRecoveryContextCleanup *cleanup) |
| 118 | { |
| 119 | if (!cleanup) |
| 120 | return; |
| 121 | if (head) |
| 122 | head->prev = cleanup; |
| 123 | cleanup->next = head; |
| 124 | head = cleanup; |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | CrashRecoveryContext::unregisterCleanup(CrashRecoveryContextCleanup *cleanup) { |
| 129 | if (!cleanup) |
| 130 | return; |
| 131 | if (cleanup == head) { |
| 132 | head = cleanup->next; |
| 133 | if (head) |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 134 | head->prev = nullptr; |
Ted Kremenek | c44d3cf | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 135 | } |
| 136 | else { |
| 137 | cleanup->prev->next = cleanup->next; |
| 138 | if (cleanup->next) |
| 139 | cleanup->next->prev = cleanup->prev; |
| 140 | } |
| 141 | delete cleanup; |
| 142 | } |
| 143 | |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 144 | #ifdef LLVM_ON_WIN32 |
| 145 | |
Reid Kleckner | d59e2fa | 2014-02-12 21:26:20 +0000 | [diff] [blame] | 146 | #include "Windows/WindowsSupport.h" |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 147 | |
| 148 | // On Windows, we can make use of vectored exception handling to |
| 149 | // catch most crashing situations. Note that this does mean |
| 150 | // we will be alerted of exceptions *before* structured exception |
| 151 | // handling has the opportunity to catch it. But that isn't likely |
| 152 | // to cause problems because nowhere in the project is SEH being |
| 153 | // used. |
| 154 | // |
| 155 | // Vectored exception handling is built on top of SEH, and so it |
| 156 | // works on a per-thread basis. |
| 157 | // |
| 158 | // The vectored exception handler functionality was added in Windows |
| 159 | // XP, so if support for older versions of Windows is required, |
| 160 | // it will have to be added. |
| 161 | // |
| 162 | // If we want to support as far back as Win2k, we could use the |
| 163 | // SetUnhandledExceptionFilter API, but there's a risk of that |
| 164 | // being entirely overwritten (it's not a chain). |
| 165 | |
| 166 | static LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo) |
| 167 | { |
| 168 | // Lookup the current thread local recovery object. |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 169 | const CrashRecoveryContextImpl *CRCI = CurrentContext->get(); |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 170 | |
| 171 | if (!CRCI) { |
| 172 | // Something has gone horribly wrong, so let's just tell everyone |
| 173 | // to keep searching |
| 174 | CrashRecoveryContext::Disable(); |
| 175 | return EXCEPTION_CONTINUE_SEARCH; |
| 176 | } |
| 177 | |
| 178 | // TODO: We can capture the stack backtrace here and store it on the |
| 179 | // implementation if we so choose. |
| 180 | |
| 181 | // Handle the crash |
| 182 | const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash(); |
| 183 | |
| 184 | // Note that we don't actually get here because HandleCrash calls |
| 185 | // longjmp, which means the HandleCrash function never returns. |
| 186 | llvm_unreachable("Handled the crash, should have longjmp'ed out of here"); |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // Because the Enable and Disable calls are static, it means that |
| 190 | // there may not actually be an Impl available, or even a current |
| 191 | // CrashRecoveryContext at all. So we make use of a thread-local |
| 192 | // exception table. The handles contained in here will either be |
| 193 | // non-NULL, valid VEH handles, or NULL. |
| 194 | static sys::ThreadLocal<const void> sCurrentExceptionHandle; |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 195 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 196 | void CrashRecoveryContext::Enable() { |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 197 | sys::ScopedLock L(*gCrashRecoveryContextMutex); |
Daniel Dunbar | ff32994 | 2010-08-17 22:32:34 +0000 | [diff] [blame] | 198 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 199 | if (gCrashRecoveryEnabled) |
| 200 | return; |
| 201 | |
| 202 | gCrashRecoveryEnabled = true; |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 203 | |
| 204 | // We can set up vectored exception handling now. We will install our |
| 205 | // handler as the front of the list, though there's no assurances that |
| 206 | // it will remain at the front (another call could install itself before |
| 207 | // our handler). This 1) isn't likely, and 2) shouldn't cause problems. |
| 208 | PVOID handle = ::AddVectoredExceptionHandler(1, ExceptionHandler); |
| 209 | sCurrentExceptionHandle.set(handle); |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void CrashRecoveryContext::Disable() { |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 213 | sys::ScopedLock L(*gCrashRecoveryContextMutex); |
Daniel Dunbar | ff32994 | 2010-08-17 22:32:34 +0000 | [diff] [blame] | 214 | |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 215 | if (!gCrashRecoveryEnabled) |
| 216 | return; |
| 217 | |
| 218 | gCrashRecoveryEnabled = false; |
NAKAMURA Takumi | 1eae12c | 2011-08-20 06:35:36 +0000 | [diff] [blame] | 219 | |
| 220 | PVOID currentHandle = const_cast<PVOID>(sCurrentExceptionHandle.get()); |
| 221 | if (currentHandle) { |
| 222 | // Now we can remove the vectored exception handler from the chain |
| 223 | ::RemoveVectoredExceptionHandler(currentHandle); |
| 224 | |
| 225 | // Reset the handle in our thread-local set. |
| 226 | sCurrentExceptionHandle.set(NULL); |
| 227 | } |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 230 | #else |
| 231 | |
| 232 | // Generic POSIX implementation. |
| 233 | // |
| 234 | // This implementation relies on synchronous signals being delivered to the |
| 235 | // current thread. We use a thread local object to keep track of the active |
| 236 | // crash recovery context, and install signal handlers to invoke HandleCrash on |
| 237 | // the active object. |
| 238 | // |
| 239 | // This implementation does not to attempt to chain signal handlers in any |
| 240 | // reliable fashion -- if we get a signal outside of a crash recovery context we |
| 241 | // simply disable crash recovery and raise the signal again. |
| 242 | |
| 243 | #include <signal.h> |
| 244 | |
Zachary Turner | a40ccf6 | 2014-06-10 18:03:04 +0000 | [diff] [blame] | 245 | static const int Signals[] = |
| 246 | { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP }; |
Craig Topper | 1d37443 | 2015-10-18 05:15:38 +0000 | [diff] [blame] | 247 | static const unsigned NumSignals = array_lengthof(Signals); |
Daniel Dunbar | c90e82a | 2010-07-30 17:49:04 +0000 | [diff] [blame] | 248 | static struct sigaction PrevActions[NumSignals]; |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 249 | |
| 250 | static void CrashRecoverySignalHandler(int Signal) { |
| 251 | // Lookup the current thread local recovery object. |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 252 | const CrashRecoveryContextImpl *CRCI = CurrentContext->get(); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 253 | |
| 254 | if (!CRCI) { |
| 255 | // We didn't find a crash recovery context -- this means either we got a |
| 256 | // signal on a thread we didn't expect it on, the application got a signal |
| 257 | // outside of a crash recovery context, or something else went horribly |
| 258 | // wrong. |
| 259 | // |
| 260 | // Disable crash recovery and raise the signal again. The assumption here is |
| 261 | // that the enclosing application will terminate soon, and we won't want to |
| 262 | // attempt crash recovery again. |
| 263 | // |
| 264 | // This call of Disable isn't thread safe, but it doesn't actually matter. |
| 265 | CrashRecoveryContext::Disable(); |
| 266 | raise(Signal); |
Daniel Dunbar | 418e704 | 2010-10-18 21:55:18 +0000 | [diff] [blame] | 267 | |
| 268 | // The signal will be thrown once the signal mask is restored. |
| 269 | return; |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // Unblock the signal we received. |
| 273 | sigset_t SigMask; |
| 274 | sigemptyset(&SigMask); |
| 275 | sigaddset(&SigMask, Signal); |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 276 | sigprocmask(SIG_UNBLOCK, &SigMask, nullptr); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 277 | |
| 278 | if (CRCI) |
| 279 | const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash(); |
| 280 | } |
| 281 | |
| 282 | void CrashRecoveryContext::Enable() { |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 283 | sys::ScopedLock L(*gCrashRecoveryContextMutex); |
Daniel Dunbar | ff32994 | 2010-08-17 22:32:34 +0000 | [diff] [blame] | 284 | |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 285 | if (gCrashRecoveryEnabled) |
| 286 | return; |
| 287 | |
| 288 | gCrashRecoveryEnabled = true; |
| 289 | |
| 290 | // Setup the signal handler. |
| 291 | struct sigaction Handler; |
| 292 | Handler.sa_handler = CrashRecoverySignalHandler; |
| 293 | Handler.sa_flags = 0; |
| 294 | sigemptyset(&Handler.sa_mask); |
| 295 | |
| 296 | for (unsigned i = 0; i != NumSignals; ++i) { |
Daniel Dunbar | c90e82a | 2010-07-30 17:49:04 +0000 | [diff] [blame] | 297 | sigaction(Signals[i], &Handler, &PrevActions[i]); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | void CrashRecoveryContext::Disable() { |
Filip Pizlo | f2189bf | 2013-09-12 17:46:57 +0000 | [diff] [blame] | 302 | sys::ScopedLock L(*gCrashRecoveryContextMutex); |
Daniel Dunbar | ff32994 | 2010-08-17 22:32:34 +0000 | [diff] [blame] | 303 | |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 304 | if (!gCrashRecoveryEnabled) |
| 305 | return; |
| 306 | |
| 307 | gCrashRecoveryEnabled = false; |
| 308 | |
| 309 | // Restore the previous signal handlers. |
| 310 | for (unsigned i = 0; i != NumSignals; ++i) |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 311 | sigaction(Signals[i], &PrevActions[i], nullptr); |
Daniel Dunbar | af77e22 | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | #endif |
| 315 | |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 316 | bool CrashRecoveryContext::RunSafely(function_ref<void()> Fn) { |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 317 | // If crash recovery is disabled, do nothing. |
| 318 | if (gCrashRecoveryEnabled) { |
| 319 | assert(!Impl && "Crash recovery context already initialized!"); |
Daniel Dunbar | b30266e | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 320 | CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl(this); |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 321 | Impl = CRCI; |
| 322 | |
| 323 | if (setjmp(CRCI->JumpBuffer) != 0) { |
| 324 | return false; |
| 325 | } |
| 326 | } |
| 327 | |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 328 | Fn(); |
Daniel Dunbar | 19a3b37 | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 329 | return true; |
| 330 | } |
| 331 | |
| 332 | void CrashRecoveryContext::HandleCrash() { |
| 333 | CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl; |
| 334 | assert(CRCI && "Crash recovery context never initialized!"); |
| 335 | CRCI->HandleCrash(); |
| 336 | } |
| 337 | |
| 338 | const std::string &CrashRecoveryContext::getBacktrace() const { |
| 339 | CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *) Impl; |
| 340 | assert(CRC && "Crash recovery context never initialized!"); |
| 341 | assert(CRC->Failed && "No crash was detected!"); |
| 342 | return CRC->Backtrace; |
| 343 | } |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 344 | |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 345 | // FIXME: Portability. |
| 346 | static void setThreadBackgroundPriority() { |
| 347 | #ifdef __APPLE__ |
| 348 | setpriority(PRIO_DARWIN_THREAD, 0, PRIO_DARWIN_BG); |
| 349 | #endif |
| 350 | } |
| 351 | |
| 352 | static bool hasThreadBackgroundPriority() { |
| 353 | #ifdef __APPLE__ |
| 354 | return getpriority(PRIO_DARWIN_THREAD, 0) == 1; |
| 355 | #else |
| 356 | return false; |
| 357 | #endif |
| 358 | } |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 359 | |
| 360 | namespace { |
| 361 | struct RunSafelyOnThreadInfo { |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 362 | function_ref<void()> Fn; |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 363 | CrashRecoveryContext *CRC; |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 364 | bool UseBackgroundPriority; |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 365 | bool Result; |
| 366 | }; |
| 367 | } |
| 368 | |
| 369 | static void RunSafelyOnThread_Dispatch(void *UserData) { |
| 370 | RunSafelyOnThreadInfo *Info = |
| 371 | reinterpret_cast<RunSafelyOnThreadInfo*>(UserData); |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 372 | |
| 373 | if (Info->UseBackgroundPriority) |
| 374 | setThreadBackgroundPriority(); |
| 375 | |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 376 | Info->Result = Info->CRC->RunSafely(Info->Fn); |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 377 | } |
Richard Smith | c167d65 | 2014-05-06 01:44:26 +0000 | [diff] [blame] | 378 | bool CrashRecoveryContext::RunSafelyOnThread(function_ref<void()> Fn, |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 379 | unsigned RequestedStackSize) { |
Argyrios Kyrtzidis | e9012b0 | 2014-06-25 23:54:50 +0000 | [diff] [blame] | 380 | bool UseBackgroundPriority = hasThreadBackgroundPriority(); |
| 381 | RunSafelyOnThreadInfo Info = { Fn, this, UseBackgroundPriority, false }; |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 382 | llvm_execute_on_thread(RunSafelyOnThread_Dispatch, &Info, RequestedStackSize); |
Argyrios Kyrtzidis | f1d8f52 | 2013-06-19 22:53:45 +0000 | [diff] [blame] | 383 | if (CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *)Impl) |
| 384 | CRC->setSwitchedThread(); |
Daniel Dunbar | f4d90ba | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 385 | return Info.Result; |
| 386 | } |