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