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