Daniel Dunbar | a309dac | 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 | dade28e | 2010-07-29 01:52:04 +0000 | [diff] [blame] | 12 | #include "llvm/Config/config.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Mutex.h" |
| 14 | #include "llvm/Support/ThreadLocal.h" |
Daniel Dunbar | a309dac | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 15 | #include <setjmp.h> |
Daniel Dunbar | d9082df | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 16 | #include <cstdio> |
Daniel Dunbar | a309dac | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | struct CrashRecoveryContextImpl; |
| 22 | |
Daniel Dunbar | d9082df | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 23 | static sys::ThreadLocal<const CrashRecoveryContextImpl> CurrentContext; |
| 24 | |
Daniel Dunbar | a309dac | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 25 | struct CrashRecoveryContextImpl { |
Daniel Dunbar | a8fa798 | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 26 | CrashRecoveryContext *CRC; |
Daniel Dunbar | a309dac | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 27 | std::string Backtrace; |
| 28 | ::jmp_buf JumpBuffer; |
| 29 | volatile unsigned Failed : 1; |
| 30 | |
| 31 | public: |
Daniel Dunbar | a8fa798 | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 32 | CrashRecoveryContextImpl(CrashRecoveryContext *CRC) : CRC(CRC), |
| 33 | Failed(false) { |
Daniel Dunbar | d9082df | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 34 | CurrentContext.set(this); |
| 35 | } |
| 36 | ~CrashRecoveryContextImpl() { |
Daniel Dunbar | a685582 | 2010-07-29 15:24:21 +0000 | [diff] [blame] | 37 | CurrentContext.erase(); |
Daniel Dunbar | d9082df | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 38 | } |
Daniel Dunbar | a309dac | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 39 | |
| 40 | void HandleCrash() { |
Daniel Dunbar | ebe7eb8 | 2010-08-17 22:32:39 +0000 | [diff] [blame] | 41 | // Eliminate the current context entry, to avoid re-entering in case the |
| 42 | // cleanup code crashes. |
| 43 | CurrentContext.erase(); |
| 44 | |
Daniel Dunbar | a309dac | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 45 | assert(!Failed && "Crash recovery context already failed!"); |
| 46 | Failed = true; |
| 47 | |
| 48 | // FIXME: Stash the backtrace. |
| 49 | |
| 50 | // Jump back to the RunSafely we were called under. |
| 51 | longjmp(JumpBuffer, 1); |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | } |
| 56 | |
Daniel Dunbar | c0c815e | 2010-08-17 22:32:34 +0000 | [diff] [blame] | 57 | static sys::Mutex gCrashRecoveryContexMutex; |
Daniel Dunbar | a309dac | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 58 | static bool gCrashRecoveryEnabled = false; |
| 59 | |
Ted Kremenek | b52fde4 | 2011-03-21 18:38:03 +0000 | [diff] [blame] | 60 | static sys::ThreadLocal<const CrashRecoveryContextCleanup> |
| 61 | tlIsRecoveringFromCrash; |
| 62 | |
Ted Kremenek | a4f9839 | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 63 | CrashRecoveryContextCleanup::~CrashRecoveryContextCleanup() {} |
| 64 | |
Daniel Dunbar | a309dac | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 65 | CrashRecoveryContext::~CrashRecoveryContext() { |
Ted Kremenek | a4f9839 | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 66 | // Reclaim registered resources. |
| 67 | CrashRecoveryContextCleanup *i = head; |
Ted Kremenek | b52fde4 | 2011-03-21 18:38:03 +0000 | [diff] [blame] | 68 | tlIsRecoveringFromCrash.set(head); |
Ted Kremenek | a4f9839 | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 69 | while (i) { |
| 70 | CrashRecoveryContextCleanup *tmp = i; |
| 71 | i = tmp->next; |
Ted Kremenek | 1a06d57 | 2011-03-19 00:59:37 +0000 | [diff] [blame] | 72 | tmp->cleanupFired = true; |
Ted Kremenek | 3311d95 | 2011-03-22 04:33:13 +0000 | [diff] [blame^] | 73 | tmp->recoverResources(); |
Ted Kremenek | a4f9839 | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 74 | delete tmp; |
| 75 | } |
Ted Kremenek | b52fde4 | 2011-03-21 18:38:03 +0000 | [diff] [blame] | 76 | tlIsRecoveringFromCrash.erase(); |
Ted Kremenek | a4f9839 | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 77 | |
Daniel Dunbar | a309dac | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 78 | CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl; |
| 79 | delete CRCI; |
| 80 | } |
| 81 | |
Ted Kremenek | b52fde4 | 2011-03-21 18:38:03 +0000 | [diff] [blame] | 82 | bool CrashRecoveryContext::isRecoveringFromCrash() { |
| 83 | return tlIsRecoveringFromCrash.get() != 0; |
| 84 | } |
| 85 | |
Daniel Dunbar | a8fa798 | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 86 | CrashRecoveryContext *CrashRecoveryContext::GetCurrent() { |
Ted Kremenek | fb200e3 | 2011-03-19 00:59:33 +0000 | [diff] [blame] | 87 | if (!gCrashRecoveryEnabled) |
| 88 | return 0; |
| 89 | |
Daniel Dunbar | a8fa798 | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 90 | const CrashRecoveryContextImpl *CRCI = CurrentContext.get(); |
| 91 | if (!CRCI) |
| 92 | return 0; |
| 93 | |
| 94 | return CRCI->CRC; |
| 95 | } |
| 96 | |
Ted Kremenek | a4f9839 | 2011-03-18 02:05:11 +0000 | [diff] [blame] | 97 | void CrashRecoveryContext::registerCleanup(CrashRecoveryContextCleanup *cleanup) |
| 98 | { |
| 99 | if (!cleanup) |
| 100 | return; |
| 101 | if (head) |
| 102 | head->prev = cleanup; |
| 103 | cleanup->next = head; |
| 104 | head = cleanup; |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | CrashRecoveryContext::unregisterCleanup(CrashRecoveryContextCleanup *cleanup) { |
| 109 | if (!cleanup) |
| 110 | return; |
| 111 | if (cleanup == head) { |
| 112 | head = cleanup->next; |
| 113 | if (head) |
| 114 | head->prev = 0; |
| 115 | } |
| 116 | else { |
| 117 | cleanup->prev->next = cleanup->next; |
| 118 | if (cleanup->next) |
| 119 | cleanup->next->prev = cleanup->prev; |
| 120 | } |
| 121 | delete cleanup; |
| 122 | } |
| 123 | |
Daniel Dunbar | d9082df | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 124 | #ifdef LLVM_ON_WIN32 |
| 125 | |
| 126 | // FIXME: No real Win32 implementation currently. |
| 127 | |
Daniel Dunbar | a309dac | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 128 | void CrashRecoveryContext::Enable() { |
Daniel Dunbar | c0c815e | 2010-08-17 22:32:34 +0000 | [diff] [blame] | 129 | sys::ScopedLock L(gCrashRecoveryContexMutex); |
| 130 | |
Daniel Dunbar | a309dac | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 131 | if (gCrashRecoveryEnabled) |
| 132 | return; |
| 133 | |
| 134 | gCrashRecoveryEnabled = true; |
| 135 | } |
| 136 | |
| 137 | void CrashRecoveryContext::Disable() { |
Daniel Dunbar | c0c815e | 2010-08-17 22:32:34 +0000 | [diff] [blame] | 138 | sys::ScopedLock L(gCrashRecoveryContexMutex); |
| 139 | |
Daniel Dunbar | a309dac | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 140 | if (!gCrashRecoveryEnabled) |
| 141 | return; |
| 142 | |
| 143 | gCrashRecoveryEnabled = false; |
| 144 | } |
| 145 | |
Daniel Dunbar | d9082df | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 146 | #else |
| 147 | |
| 148 | // Generic POSIX implementation. |
| 149 | // |
| 150 | // This implementation relies on synchronous signals being delivered to the |
| 151 | // current thread. We use a thread local object to keep track of the active |
| 152 | // crash recovery context, and install signal handlers to invoke HandleCrash on |
| 153 | // the active object. |
| 154 | // |
| 155 | // This implementation does not to attempt to chain signal handlers in any |
| 156 | // reliable fashion -- if we get a signal outside of a crash recovery context we |
| 157 | // simply disable crash recovery and raise the signal again. |
| 158 | |
| 159 | #include <signal.h> |
| 160 | |
Daniel Dunbar | 63cc2e1 | 2010-07-30 17:49:04 +0000 | [diff] [blame] | 161 | static int Signals[] = { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP }; |
| 162 | static const unsigned NumSignals = sizeof(Signals) / sizeof(Signals[0]); |
| 163 | static struct sigaction PrevActions[NumSignals]; |
Daniel Dunbar | d9082df | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 164 | |
| 165 | static void CrashRecoverySignalHandler(int Signal) { |
| 166 | // Lookup the current thread local recovery object. |
| 167 | const CrashRecoveryContextImpl *CRCI = CurrentContext.get(); |
| 168 | |
| 169 | if (!CRCI) { |
| 170 | // We didn't find a crash recovery context -- this means either we got a |
| 171 | // signal on a thread we didn't expect it on, the application got a signal |
| 172 | // outside of a crash recovery context, or something else went horribly |
| 173 | // wrong. |
| 174 | // |
| 175 | // Disable crash recovery and raise the signal again. The assumption here is |
| 176 | // that the enclosing application will terminate soon, and we won't want to |
| 177 | // attempt crash recovery again. |
| 178 | // |
| 179 | // This call of Disable isn't thread safe, but it doesn't actually matter. |
| 180 | CrashRecoveryContext::Disable(); |
| 181 | raise(Signal); |
Daniel Dunbar | d49e2aa | 2010-10-18 21:55:18 +0000 | [diff] [blame] | 182 | |
| 183 | // The signal will be thrown once the signal mask is restored. |
| 184 | return; |
Daniel Dunbar | d9082df | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // Unblock the signal we received. |
| 188 | sigset_t SigMask; |
| 189 | sigemptyset(&SigMask); |
| 190 | sigaddset(&SigMask, Signal); |
| 191 | sigprocmask(SIG_UNBLOCK, &SigMask, 0); |
| 192 | |
| 193 | if (CRCI) |
| 194 | const_cast<CrashRecoveryContextImpl*>(CRCI)->HandleCrash(); |
| 195 | } |
| 196 | |
| 197 | void CrashRecoveryContext::Enable() { |
Daniel Dunbar | c0c815e | 2010-08-17 22:32:34 +0000 | [diff] [blame] | 198 | sys::ScopedLock L(gCrashRecoveryContexMutex); |
| 199 | |
Daniel Dunbar | d9082df | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 200 | if (gCrashRecoveryEnabled) |
| 201 | return; |
| 202 | |
| 203 | gCrashRecoveryEnabled = true; |
| 204 | |
| 205 | // Setup the signal handler. |
| 206 | struct sigaction Handler; |
| 207 | Handler.sa_handler = CrashRecoverySignalHandler; |
| 208 | Handler.sa_flags = 0; |
| 209 | sigemptyset(&Handler.sa_mask); |
| 210 | |
| 211 | for (unsigned i = 0; i != NumSignals; ++i) { |
Daniel Dunbar | 63cc2e1 | 2010-07-30 17:49:04 +0000 | [diff] [blame] | 212 | sigaction(Signals[i], &Handler, &PrevActions[i]); |
Daniel Dunbar | d9082df | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | void CrashRecoveryContext::Disable() { |
Daniel Dunbar | c0c815e | 2010-08-17 22:32:34 +0000 | [diff] [blame] | 217 | sys::ScopedLock L(gCrashRecoveryContexMutex); |
| 218 | |
Daniel Dunbar | d9082df | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 219 | if (!gCrashRecoveryEnabled) |
| 220 | return; |
| 221 | |
| 222 | gCrashRecoveryEnabled = false; |
| 223 | |
| 224 | // Restore the previous signal handlers. |
| 225 | for (unsigned i = 0; i != NumSignals; ++i) |
Daniel Dunbar | 63cc2e1 | 2010-07-30 17:49:04 +0000 | [diff] [blame] | 226 | sigaction(Signals[i], &PrevActions[i], 0); |
Daniel Dunbar | d9082df | 2010-07-29 01:21:47 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | #endif |
| 230 | |
Daniel Dunbar | a309dac | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 231 | bool CrashRecoveryContext::RunSafely(void (*Fn)(void*), void *UserData) { |
| 232 | // If crash recovery is disabled, do nothing. |
| 233 | if (gCrashRecoveryEnabled) { |
| 234 | assert(!Impl && "Crash recovery context already initialized!"); |
Daniel Dunbar | a8fa798 | 2010-08-17 22:32:37 +0000 | [diff] [blame] | 235 | CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl(this); |
Daniel Dunbar | a309dac | 2010-07-28 15:40:20 +0000 | [diff] [blame] | 236 | Impl = CRCI; |
| 237 | |
| 238 | if (setjmp(CRCI->JumpBuffer) != 0) { |
| 239 | return false; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | Fn(UserData); |
| 244 | return true; |
| 245 | } |
| 246 | |
| 247 | void CrashRecoveryContext::HandleCrash() { |
| 248 | CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl; |
| 249 | assert(CRCI && "Crash recovery context never initialized!"); |
| 250 | CRCI->HandleCrash(); |
| 251 | } |
| 252 | |
| 253 | const std::string &CrashRecoveryContext::getBacktrace() const { |
| 254 | CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *) Impl; |
| 255 | assert(CRC && "Crash recovery context never initialized!"); |
| 256 | assert(CRC->Failed && "No crash was detected!"); |
| 257 | return CRC->Backtrace; |
| 258 | } |
Daniel Dunbar | f8254d64 | 2010-11-05 07:19:09 +0000 | [diff] [blame] | 259 | |
| 260 | // |
| 261 | |
| 262 | namespace { |
| 263 | struct RunSafelyOnThreadInfo { |
| 264 | void (*UserFn)(void*); |
| 265 | void *UserData; |
| 266 | CrashRecoveryContext *CRC; |
| 267 | bool Result; |
| 268 | }; |
| 269 | } |
| 270 | |
| 271 | static void RunSafelyOnThread_Dispatch(void *UserData) { |
| 272 | RunSafelyOnThreadInfo *Info = |
| 273 | reinterpret_cast<RunSafelyOnThreadInfo*>(UserData); |
| 274 | Info->Result = Info->CRC->RunSafely(Info->UserFn, Info->UserData); |
| 275 | } |
| 276 | bool CrashRecoveryContext::RunSafelyOnThread(void (*Fn)(void*), void *UserData, |
| 277 | unsigned RequestedStackSize) { |
| 278 | RunSafelyOnThreadInfo Info = { Fn, UserData, this, false }; |
| 279 | llvm_execute_on_thread(RunSafelyOnThread_Dispatch, &Info, RequestedStackSize); |
| 280 | return Info.Result; |
| 281 | } |