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