Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 1 | //===- Signals.cpp - Generic Unix Signals Implementation -----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines some helpful functions for dealing with the possibility of |
| 11 | // Unix signals occuring while your program is running. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "Unix.h" |
Owen Anderson | 718cb66 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Owen Anderson | 6ae8c73 | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 17 | #include "llvm/System/Mutex.h" |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 18 | #include <vector> |
| 19 | #include <algorithm> |
Reid Spencer | cdf54d0 | 2004-12-27 06:16:52 +0000 | [diff] [blame] | 20 | #if HAVE_EXECINFO_H |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 21 | # include <execinfo.h> // For backtrace(). |
| 22 | #endif |
Reid Spencer | cdf54d0 | 2004-12-27 06:16:52 +0000 | [diff] [blame] | 23 | #if HAVE_SIGNAL_H |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 24 | #include <signal.h> |
Reid Spencer | cdf54d0 | 2004-12-27 06:16:52 +0000 | [diff] [blame] | 25 | #endif |
Reid Spencer | 2ae9d11 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 26 | #if HAVE_SYS_STAT_H |
| 27 | #include <sys/stat.h> |
| 28 | #endif |
Dan Gohman | 56d9b6d | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 29 | #if HAVE_DLFCN_H && __GNUG__ |
| 30 | #include <dlfcn.h> |
| 31 | #include <cxxabi.h> |
| 32 | #endif |
Chris Lattner | 8961501 | 2006-08-01 17:59:14 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 1c4d8a0 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 35 | static RETSIGTYPE SignalHandler(int Sig); // defined below. |
| 36 | |
Owen Anderson | 6ae8c73 | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 37 | static SmartMutex<true> SignalsMutex; |
| 38 | |
Chris Lattner | fa8c292 | 2005-08-02 02:14:22 +0000 | [diff] [blame] | 39 | /// InterruptFunction - The function to call if ctrl-c is pressed. |
Dan Gohman | 3bd659b | 2008-04-10 21:11:47 +0000 | [diff] [blame] | 40 | static void (*InterruptFunction)() = 0; |
Chris Lattner | fa8c292 | 2005-08-02 02:14:22 +0000 | [diff] [blame] | 41 | |
Jeffrey Yasskin | b7ccf75 | 2010-03-17 07:08:12 +0000 | [diff] [blame] | 42 | static std::vector<sys::Path> FilesToRemove; |
| 43 | static std::vector<std::pair<void(*)(void*), void*> > CallBacksToRun; |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 44 | |
| 45 | // IntSigs - Signals that may interrupt the program at any time. |
Dan Gohman | 3bd659b | 2008-04-10 21:11:47 +0000 | [diff] [blame] | 46 | static const int IntSigs[] = { |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 47 | SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2 |
| 48 | }; |
Dan Gohman | 4d9dd6b | 2008-05-14 00:42:30 +0000 | [diff] [blame] | 49 | static const int *const IntSigsEnd = |
| 50 | IntSigs + sizeof(IntSigs) / sizeof(IntSigs[0]); |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 51 | |
| 52 | // KillSigs - Signals that are synchronous with the program that will cause it |
| 53 | // to die. |
Dan Gohman | 3bd659b | 2008-04-10 21:11:47 +0000 | [diff] [blame] | 54 | static const int KillSigs[] = { |
Chris Lattner | 6ae7bbb | 2010-02-12 00:37:46 +0000 | [diff] [blame] | 55 | SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV |
| 56 | #ifdef SIGSYS |
| 57 | , SIGSYS |
| 58 | #endif |
| 59 | #ifdef SIGXCPU |
| 60 | , SIGXCPU |
| 61 | #endif |
Chris Lattner | 782ab58 | 2010-02-14 18:20:09 +0000 | [diff] [blame] | 62 | #ifdef SIGXFSZ |
Chris Lattner | 6ae7bbb | 2010-02-12 00:37:46 +0000 | [diff] [blame] | 63 | , SIGXFSZ |
| 64 | #endif |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 65 | #ifdef SIGEMT |
| 66 | , SIGEMT |
| 67 | #endif |
| 68 | }; |
Dan Gohman | 4d9dd6b | 2008-05-14 00:42:30 +0000 | [diff] [blame] | 69 | static const int *const KillSigsEnd = |
| 70 | KillSigs + sizeof(KillSigs) / sizeof(KillSigs[0]); |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 71 | |
Chris Lattner | eab5cb3 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 72 | static unsigned NumRegisteredSignals = 0; |
| 73 | static struct { |
| 74 | struct sigaction SA; |
| 75 | int SigNo; |
| 76 | } RegisteredSignalInfo[(sizeof(IntSigs)+sizeof(KillSigs))/sizeof(KillSigs[0])]; |
| 77 | |
| 78 | |
Chris Lattner | 1c4d8a0 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 79 | static void RegisterHandler(int Signal) { |
Chris Lattner | eab5cb3 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 80 | assert(NumRegisteredSignals < |
| 81 | sizeof(RegisteredSignalInfo)/sizeof(RegisteredSignalInfo[0]) && |
| 82 | "Out of space for signal handlers!"); |
| 83 | |
| 84 | struct sigaction NewHandler; |
| 85 | |
| 86 | NewHandler.sa_handler = SignalHandler; |
| 87 | NewHandler.sa_flags = SA_NODEFER|SA_RESETHAND; |
| 88 | sigemptyset(&NewHandler.sa_mask); |
| 89 | |
| 90 | // Install the new handler, save the old one in RegisteredSignalInfo. |
| 91 | sigaction(Signal, &NewHandler, |
| 92 | &RegisteredSignalInfo[NumRegisteredSignals].SA); |
| 93 | RegisteredSignalInfo[NumRegisteredSignals].SigNo = Signal; |
| 94 | ++NumRegisteredSignals; |
Chris Lattner | 922a881 | 2009-03-07 08:15:47 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Chris Lattner | 1c4d8a0 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 97 | static void RegisterHandlers() { |
Chris Lattner | eab5cb3 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 98 | // If the handlers are already registered, we're done. |
| 99 | if (NumRegisteredSignals != 0) return; |
| 100 | |
Chris Lattner | 1c4d8a0 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 101 | std::for_each(IntSigs, IntSigsEnd, RegisterHandler); |
| 102 | std::for_each(KillSigs, KillSigsEnd, RegisterHandler); |
| 103 | } |
| 104 | |
Chris Lattner | 1c4d8a0 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 105 | static void UnregisterHandlers() { |
Chris Lattner | eab5cb3 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 106 | // Restore all of the signal handlers to how they were before we showed up. |
| 107 | for (unsigned i = 0, e = NumRegisteredSignals; i != e; ++i) |
Chris Lattner | a8bd27f | 2009-03-23 06:46:20 +0000 | [diff] [blame] | 108 | sigaction(RegisteredSignalInfo[i].SigNo, |
| 109 | &RegisteredSignalInfo[i].SA, 0); |
Chris Lattner | eab5cb3 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 110 | NumRegisteredSignals = 0; |
Chris Lattner | 1c4d8a0 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | |
Dan Gohman | 39f76bb | 2010-05-27 23:11:55 +0000 | [diff] [blame] | 114 | /// RemoveFilesToRemove - Process the FilesToRemove list. This function |
| 115 | /// should be called with the SignalsMutex lock held. |
| 116 | static void RemoveFilesToRemove() { |
| 117 | while (!FilesToRemove.empty()) { |
| 118 | FilesToRemove.back().eraseFromDisk(true); |
| 119 | FilesToRemove.pop_back(); |
| 120 | } |
| 121 | } |
Chris Lattner | 922a881 | 2009-03-07 08:15:47 +0000 | [diff] [blame] | 122 | |
| 123 | // SignalHandler - The signal handler that runs. |
Chris Lattner | 35033a5 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 124 | static RETSIGTYPE SignalHandler(int Sig) { |
Chris Lattner | 0f6290d | 2009-03-05 18:22:14 +0000 | [diff] [blame] | 125 | // Restore the signal behavior to default, so that the program actually |
| 126 | // crashes when we return and the signal reissues. This also ensures that if |
| 127 | // we crash in our signal handler that the program will terminate immediately |
| 128 | // instead of recursing in the signal handler. |
Chris Lattner | 1c4d8a0 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 129 | UnregisterHandlers(); |
Chris Lattner | 922a881 | 2009-03-07 08:15:47 +0000 | [diff] [blame] | 130 | |
| 131 | // Unmask all potentially blocked kill signals. |
| 132 | sigset_t SigMask; |
| 133 | sigfillset(&SigMask); |
| 134 | sigprocmask(SIG_UNBLOCK, &SigMask, 0); |
Chris Lattner | 0f6290d | 2009-03-05 18:22:14 +0000 | [diff] [blame] | 135 | |
Owen Anderson | 6ae8c73 | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 136 | SignalsMutex.acquire(); |
Dan Gohman | 39f76bb | 2010-05-27 23:11:55 +0000 | [diff] [blame] | 137 | RemoveFilesToRemove(); |
Chris Lattner | 35033a5 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 138 | |
| 139 | if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd) { |
| 140 | if (InterruptFunction) { |
| 141 | void (*IF)() = InterruptFunction; |
Owen Anderson | 6ae8c73 | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 142 | SignalsMutex.release(); |
Chris Lattner | 35033a5 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 143 | InterruptFunction = 0; |
| 144 | IF(); // run the interrupt function. |
| 145 | return; |
| 146 | } |
Owen Anderson | 6ae8c73 | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 147 | |
| 148 | SignalsMutex.release(); |
Chris Lattner | b725315 | 2009-04-12 23:33:13 +0000 | [diff] [blame] | 149 | raise(Sig); // Execute the default handler. |
| 150 | return; |
Chris Lattner | 35033a5 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Owen Anderson | 6ae8c73 | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 153 | SignalsMutex.release(); |
| 154 | |
Chris Lattner | 35033a5 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 155 | // Otherwise if it is a fault (like SEGV) run any handler. |
Jeffrey Yasskin | b7ccf75 | 2010-03-17 07:08:12 +0000 | [diff] [blame] | 156 | for (unsigned i = 0, e = CallBacksToRun.size(); i != e; ++i) |
| 157 | CallBacksToRun[i].first(CallBacksToRun[i].second); |
Chris Lattner | 35033a5 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Daniel Dunbar | fb89e08 | 2010-05-08 02:10:34 +0000 | [diff] [blame] | 160 | void llvm::sys::RunInterruptHandlers() { |
Dan Gohman | 39f76bb | 2010-05-27 23:11:55 +0000 | [diff] [blame] | 161 | SignalsMutex.acquire(); |
| 162 | RemoveFilesToRemove(); |
| 163 | SignalsMutex.release(); |
Daniel Dunbar | fb89e08 | 2010-05-08 02:10:34 +0000 | [diff] [blame] | 164 | } |
Chris Lattner | 35033a5 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 165 | |
Chris Lattner | f48ba7b | 2009-03-08 19:13:45 +0000 | [diff] [blame] | 166 | void llvm::sys::SetInterruptFunction(void (*IF)()) { |
Owen Anderson | 6ae8c73 | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 167 | SignalsMutex.acquire(); |
Chris Lattner | 35033a5 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 168 | InterruptFunction = IF; |
Owen Anderson | 6ae8c73 | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 169 | SignalsMutex.release(); |
Chris Lattner | 1c4d8a0 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 170 | RegisterHandlers(); |
Chris Lattner | 35033a5 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // RemoveFileOnSignal - The public API |
Chris Lattner | f48ba7b | 2009-03-08 19:13:45 +0000 | [diff] [blame] | 174 | bool llvm::sys::RemoveFileOnSignal(const sys::Path &Filename, |
| 175 | std::string* ErrMsg) { |
Owen Anderson | 6ae8c73 | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 176 | SignalsMutex.acquire(); |
Jeffrey Yasskin | b7ccf75 | 2010-03-17 07:08:12 +0000 | [diff] [blame] | 177 | FilesToRemove.push_back(Filename); |
Chris Lattner | 35033a5 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 178 | |
Owen Anderson | 6ae8c73 | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 179 | SignalsMutex.release(); |
| 180 | |
Chris Lattner | 1c4d8a0 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 181 | RegisterHandlers(); |
Chris Lattner | 35033a5 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 182 | return false; |
| 183 | } |
| 184 | |
Dan Gohman | 4115411 | 2010-09-01 14:17:34 +0000 | [diff] [blame^] | 185 | // DontRemoveFileOnSignal - The public API |
| 186 | void llvm::sys::DontRemoveFileOnSignal(const sys::Path &Filename) { |
| 187 | SignalsMutex.acquire(); |
| 188 | std::vector<sys::Path>::reverse_iterator I = |
| 189 | std::find(FilesToRemove.rbegin(), FilesToRemove.rend(), Filename); |
| 190 | if (I != FilesToRemove.rend()) |
| 191 | FilesToRemove.erase(I.base()-1); |
| 192 | SignalsMutex.release(); |
| 193 | } |
| 194 | |
Chris Lattner | 35033a5 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 195 | /// AddSignalHandler - Add a function to be called when a signal is delivered |
| 196 | /// to the process. The handler can have a cookie passed to it to identify |
| 197 | /// what instance of the handler it is. |
Chris Lattner | f48ba7b | 2009-03-08 19:13:45 +0000 | [diff] [blame] | 198 | void llvm::sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) { |
Jeffrey Yasskin | b7ccf75 | 2010-03-17 07:08:12 +0000 | [diff] [blame] | 199 | CallBacksToRun.push_back(std::make_pair(FnPtr, Cookie)); |
Chris Lattner | 1c4d8a0 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 200 | RegisterHandlers(); |
Chris Lattner | 35033a5 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 203 | |
| 204 | // PrintStackTrace - In the case of a program crash or fault, print out a stack |
| 205 | // trace so that the user has an indication of why and where we died. |
| 206 | // |
| 207 | // On glibc systems we have the 'backtrace' function, which works nicely, but |
Lauro Ramos Venancio | 2e78b78 | 2008-02-15 18:05:54 +0000 | [diff] [blame] | 208 | // doesn't demangle symbols. |
Chris Lattner | 35033a5 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 209 | static void PrintStackTrace(void *) { |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 210 | #ifdef HAVE_BACKTRACE |
Chris Lattner | 35033a5 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 211 | static void* StackTrace[256]; |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 212 | // Use backtrace() to output a backtrace on Linux systems with glibc. |
Evan Cheng | 34cd4a4 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 213 | int depth = backtrace(StackTrace, |
| 214 | static_cast<int>(array_lengthof(StackTrace))); |
Dan Gohman | 56d9b6d | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 215 | #if HAVE_DLFCN_H && __GNUG__ |
| 216 | int width = 0; |
| 217 | for (int i = 0; i < depth; ++i) { |
| 218 | Dl_info dlinfo; |
| 219 | dladdr(StackTrace[i], &dlinfo); |
Dan Gohman | d4e1845 | 2009-02-10 17:56:28 +0000 | [diff] [blame] | 220 | const char* name = strrchr(dlinfo.dli_fname, '/'); |
Dan Gohman | 56d9b6d | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 221 | |
| 222 | int nwidth; |
| 223 | if (name == NULL) nwidth = strlen(dlinfo.dli_fname); |
| 224 | else nwidth = strlen(name) - 1; |
| 225 | |
| 226 | if (nwidth > width) width = nwidth; |
| 227 | } |
| 228 | |
| 229 | for (int i = 0; i < depth; ++i) { |
| 230 | Dl_info dlinfo; |
| 231 | dladdr(StackTrace[i], &dlinfo); |
| 232 | |
Dan Gohman | 0b81e19 | 2009-10-30 02:45:10 +0000 | [diff] [blame] | 233 | fprintf(stderr, "%-2d", i); |
Dan Gohman | 56d9b6d | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 234 | |
Dan Gohman | d4e1845 | 2009-02-10 17:56:28 +0000 | [diff] [blame] | 235 | const char* name = strrchr(dlinfo.dli_fname, '/'); |
Dan Gohman | 56d9b6d | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 236 | if (name == NULL) fprintf(stderr, " %-*s", width, dlinfo.dli_fname); |
| 237 | else fprintf(stderr, " %-*s", width, name+1); |
| 238 | |
Dan Gohman | dd17b25 | 2008-12-05 23:39:24 +0000 | [diff] [blame] | 239 | fprintf(stderr, " %#0*lx", |
| 240 | (int)(sizeof(void*) * 2) + 2, (unsigned long)StackTrace[i]); |
Dan Gohman | 56d9b6d | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 241 | |
| 242 | if (dlinfo.dli_sname != NULL) { |
| 243 | int res; |
| 244 | fputc(' ', stderr); |
| 245 | char* d = abi::__cxa_demangle(dlinfo.dli_sname, NULL, NULL, &res); |
| 246 | if (d == NULL) fputs(dlinfo.dli_sname, stderr); |
| 247 | else fputs(d, stderr); |
| 248 | free(d); |
| 249 | |
| 250 | fprintf(stderr, " + %tu",(char*)StackTrace[i]-(char*)dlinfo.dli_saddr); |
| 251 | } |
| 252 | fputc('\n', stderr); |
| 253 | } |
| 254 | #else |
Lauro Ramos Venancio | 2e78b78 | 2008-02-15 18:05:54 +0000 | [diff] [blame] | 255 | backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO); |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 256 | #endif |
Dan Gohman | 56d9b6d | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 257 | #endif |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 260 | /// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or |
| 261 | /// SIGSEGV) is delivered to the process, print a stack trace and then exit. |
Chris Lattner | f48ba7b | 2009-03-08 19:13:45 +0000 | [diff] [blame] | 262 | void llvm::sys::PrintStackTraceOnErrorSignal() { |
Chris Lattner | 35033a5 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 263 | AddSignalHandler(PrintStackTrace, 0); |
Reid Spencer | 496c277 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 264 | } |
Chris Lattner | 35033a5 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 265 | |
Daniel Dunbar | b08ceb8 | 2010-08-19 23:45:39 +0000 | [diff] [blame] | 266 | |
| 267 | /***/ |
| 268 | |
| 269 | // On Darwin, raise sends a signal to the main thread instead of the current |
| 270 | // thread. This has the unfortunate effect that assert() and abort() will end up |
| 271 | // bypassing our crash recovery attempts. We work around this for anything in |
| 272 | // the same linkage unit by just defining our own versions of the assert handler |
| 273 | // and abort. |
| 274 | |
| 275 | #ifdef __APPLE__ |
| 276 | |
| 277 | void __assert_rtn(const char *func, |
| 278 | const char *file, |
| 279 | int line, |
| 280 | const char *expr) { |
| 281 | if (func) |
| 282 | fprintf(stderr, "Assertion failed: (%s), function %s, file %s, line %d.\n", |
| 283 | expr, func, file, line); |
| 284 | else |
| 285 | fprintf(stderr, "Assertion failed: (%s), file %s, line %d.\n", |
| 286 | expr, file, line); |
| 287 | abort(); |
| 288 | } |
| 289 | |
| 290 | #include <signal.h> |
| 291 | #include <pthread.h> |
| 292 | |
| 293 | void abort() { |
| 294 | pthread_kill(pthread_self(), SIGABRT); |
| 295 | usleep(1000); |
| 296 | __builtin_trap(); |
| 297 | } |
| 298 | |
| 299 | #endif |