Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 1 | //===- Signals.cpp - Generic Unix Signals Implementation -----*- C++ -*-===// |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 3d7a614 | 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 | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 11 | // Unix signals occurring while your program is running. |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "Unix.h" |
Owen Anderson | e2f23a3 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Format.h" |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 18 | #include "llvm/Support/FileSystem.h" |
| 19 | #include "llvm/Support/FileUtilities.h" |
Alexey Samsonov | afe6707 | 2014-10-08 23:07:59 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ManagedStatic.h" |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MemoryBuffer.h" |
| 22 | #include "llvm/Support/Mutex.h" |
| 23 | #include "llvm/Support/Program.h" |
| 24 | #include "llvm/Support/UniqueLock.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Chandler Carruth | e6196eb | 2012-06-16 00:09:41 +0000 | [diff] [blame] | 27 | #include <string> |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 28 | #include <vector> |
Reid Spencer | d554bbc | 2004-12-27 06:16:52 +0000 | [diff] [blame] | 29 | #if HAVE_EXECINFO_H |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 30 | # include <execinfo.h> // For backtrace(). |
| 31 | #endif |
Reid Spencer | d554bbc | 2004-12-27 06:16:52 +0000 | [diff] [blame] | 32 | #if HAVE_SIGNAL_H |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 33 | #include <signal.h> |
Reid Spencer | d554bbc | 2004-12-27 06:16:52 +0000 | [diff] [blame] | 34 | #endif |
Reid Spencer | ceeb918 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 35 | #if HAVE_SYS_STAT_H |
| 36 | #include <sys/stat.h> |
| 37 | #endif |
Joerg Sonnenberger | 6624183 | 2013-04-27 22:12:32 +0000 | [diff] [blame] | 38 | #if HAVE_CXXABI_H |
Joerg Sonnenberger | 4474409 | 2013-04-27 22:32:54 +0000 | [diff] [blame] | 39 | #include <cxxabi.h> |
Joerg Sonnenberger | 6624183 | 2013-04-27 22:12:32 +0000 | [diff] [blame] | 40 | #endif |
| 41 | #if HAVE_DLFCN_H |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 42 | #include <dlfcn.h> |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 43 | #endif |
Argyrios Kyrtzidis | cd8fe08 | 2012-01-11 20:53:25 +0000 | [diff] [blame] | 44 | #if HAVE_MACH_MACH_H |
| 45 | #include <mach/mach.h> |
| 46 | #endif |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 47 | #if HAVE_LINK_H |
| 48 | #include <link.h> |
| 49 | #endif |
Argyrios Kyrtzidis | cd8fe08 | 2012-01-11 20:53:25 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 80df7c4 | 2006-08-01 17:59:14 +0000 | [diff] [blame] | 51 | using namespace llvm; |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 52 | |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 53 | static RETSIGTYPE SignalHandler(int Sig); // defined below. |
| 54 | |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 55 | static ManagedStatic<SmartMutex<true> > SignalsMutex; |
Owen Anderson | 820739d | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 6a5d6ec | 2005-08-02 02:14:22 +0000 | [diff] [blame] | 57 | /// InterruptFunction - The function to call if ctrl-c is pressed. |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 58 | static void (*InterruptFunction)() = nullptr; |
Chris Lattner | 6a5d6ec | 2005-08-02 02:14:22 +0000 | [diff] [blame] | 59 | |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 60 | static ManagedStatic<std::vector<std::string>> FilesToRemove; |
| 61 | static ManagedStatic<std::vector<std::pair<void (*)(void *), void *>>> |
| 62 | CallBacksToRun; |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 63 | |
Dan Gohman | f857cd7 | 2013-02-20 19:28:46 +0000 | [diff] [blame] | 64 | // IntSigs - Signals that represent requested termination. There's no bug |
| 65 | // or failure, or if there is, it's not our direct responsibility. For whatever |
| 66 | // reason, our continued execution is no longer desirable. |
Dan Gohman | f4bc782 | 2008-04-10 21:11:47 +0000 | [diff] [blame] | 67 | static const int IntSigs[] = { |
Dan Gohman | 5cdb345 | 2013-02-20 19:15:01 +0000 | [diff] [blame] | 68 | SIGHUP, SIGINT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2 |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 69 | }; |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 70 | |
Dan Gohman | f857cd7 | 2013-02-20 19:28:46 +0000 | [diff] [blame] | 71 | // KillSigs - Signals that represent that we have a bug, and our prompt |
| 72 | // termination has been ordered. |
Dan Gohman | f4bc782 | 2008-04-10 21:11:47 +0000 | [diff] [blame] | 73 | static const int KillSigs[] = { |
Dan Gohman | 5cdb345 | 2013-02-20 19:15:01 +0000 | [diff] [blame] | 74 | SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGQUIT |
Chris Lattner | 62f50da | 2010-02-12 00:37:46 +0000 | [diff] [blame] | 75 | #ifdef SIGSYS |
| 76 | , SIGSYS |
| 77 | #endif |
| 78 | #ifdef SIGXCPU |
| 79 | , SIGXCPU |
| 80 | #endif |
Chris Lattner | 3b38fd6 | 2010-02-14 18:20:09 +0000 | [diff] [blame] | 81 | #ifdef SIGXFSZ |
Chris Lattner | 62f50da | 2010-02-12 00:37:46 +0000 | [diff] [blame] | 82 | , SIGXFSZ |
| 83 | #endif |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 84 | #ifdef SIGEMT |
| 85 | , SIGEMT |
| 86 | #endif |
| 87 | }; |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 88 | |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 89 | static unsigned NumRegisteredSignals = 0; |
| 90 | static struct { |
| 91 | struct sigaction SA; |
| 92 | int SigNo; |
| 93 | } RegisteredSignalInfo[(sizeof(IntSigs)+sizeof(KillSigs))/sizeof(KillSigs[0])]; |
| 94 | |
| 95 | |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 96 | static void RegisterHandler(int Signal) { |
Craig Topper | 26b45c2 | 2013-07-15 04:37:54 +0000 | [diff] [blame] | 97 | assert(NumRegisteredSignals < |
| 98 | sizeof(RegisteredSignalInfo)/sizeof(RegisteredSignalInfo[0]) && |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 99 | "Out of space for signal handlers!"); |
| 100 | |
| 101 | struct sigaction NewHandler; |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 102 | |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 103 | NewHandler.sa_handler = SignalHandler; |
| 104 | NewHandler.sa_flags = SA_NODEFER|SA_RESETHAND; |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 105 | sigemptyset(&NewHandler.sa_mask); |
| 106 | |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 107 | // Install the new handler, save the old one in RegisteredSignalInfo. |
| 108 | sigaction(Signal, &NewHandler, |
| 109 | &RegisteredSignalInfo[NumRegisteredSignals].SA); |
| 110 | RegisteredSignalInfo[NumRegisteredSignals].SigNo = Signal; |
| 111 | ++NumRegisteredSignals; |
Chris Lattner | 6acb4d6 | 2009-03-07 08:15:47 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 114 | static void RegisterHandlers() { |
Chris Bieneman | 017ebf0 | 2015-04-27 20:45:35 +0000 | [diff] [blame] | 115 | // We need to dereference the signals mutex during handler registration so |
| 116 | // that we force its construction. This is to prevent the first use being |
| 117 | // during handling an actual signal because you can't safely call new in a |
| 118 | // signal handler. |
| 119 | *SignalsMutex; |
| 120 | |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 121 | // If the handlers are already registered, we're done. |
| 122 | if (NumRegisteredSignals != 0) return; |
| 123 | |
Chris Bieneman | b1cd51e | 2014-08-29 01:05:16 +0000 | [diff] [blame] | 124 | for (auto S : IntSigs) RegisterHandler(S); |
| 125 | for (auto S : KillSigs) RegisterHandler(S); |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 128 | static void UnregisterHandlers() { |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 129 | // Restore all of the signal handlers to how they were before we showed up. |
| 130 | for (unsigned i = 0, e = NumRegisteredSignals; i != e; ++i) |
Chris Lattner | f2b6065 | 2009-03-23 06:46:20 +0000 | [diff] [blame] | 131 | sigaction(RegisteredSignalInfo[i].SigNo, |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 132 | &RegisteredSignalInfo[i].SA, nullptr); |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 133 | NumRegisteredSignals = 0; |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | |
Dan Gohman | 288999b | 2010-05-27 23:11:55 +0000 | [diff] [blame] | 137 | /// RemoveFilesToRemove - Process the FilesToRemove list. This function |
| 138 | /// should be called with the SignalsMutex lock held. |
Chandler Carruth | e6196eb | 2012-06-16 00:09:41 +0000 | [diff] [blame] | 139 | /// NB: This must be an async signal safe function. It cannot allocate or free |
| 140 | /// memory, even in debug builds. |
Dan Gohman | 288999b | 2010-05-27 23:11:55 +0000 | [diff] [blame] | 141 | static void RemoveFilesToRemove() { |
Steven Wu | 9474669 | 2015-05-07 16:20:51 +0000 | [diff] [blame^] | 142 | // Avoid constructing ManagedStatic in the signal handler. |
| 143 | // If FilesToRemove is not constructed, there are no files to remove. |
| 144 | if (!FilesToRemove.isConstructed()) |
| 145 | return; |
| 146 | |
Daniel Dunbar | 511479d | 2012-10-17 16:30:54 +0000 | [diff] [blame] | 147 | // We avoid iterators in case of debug iterators that allocate or release |
Chandler Carruth | e6196eb | 2012-06-16 00:09:41 +0000 | [diff] [blame] | 148 | // memory. |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 149 | std::vector<std::string>& FilesToRemoveRef = *FilesToRemove; |
| 150 | for (unsigned i = 0, e = FilesToRemoveRef.size(); i != e; ++i) { |
Daniel Dunbar | 511479d | 2012-10-17 16:30:54 +0000 | [diff] [blame] | 151 | // We rely on a std::string implementation for which repeated calls to |
| 152 | // 'c_str()' don't allocate memory. We pre-call 'c_str()' on all of these |
| 153 | // strings to try to ensure this is safe. |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 154 | const char *path = FilesToRemoveRef[i].c_str(); |
Daniel Dunbar | 511479d | 2012-10-17 16:30:54 +0000 | [diff] [blame] | 155 | |
| 156 | // Get the status so we can determine if it's a file or directory. If we |
| 157 | // can't stat the file, ignore it. |
| 158 | struct stat buf; |
| 159 | if (stat(path, &buf) != 0) |
| 160 | continue; |
| 161 | |
| 162 | // If this is not a regular file, ignore it. We want to prevent removal of |
| 163 | // special files like /dev/null, even if the compiler is being run with the |
| 164 | // super-user permissions. |
| 165 | if (!S_ISREG(buf.st_mode)) |
| 166 | continue; |
| 167 | |
| 168 | // Otherwise, remove the file. We ignore any errors here as there is nothing |
| 169 | // else we can do. |
| 170 | unlink(path); |
Dan Gohman | 288999b | 2010-05-27 23:11:55 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
Chris Lattner | 6acb4d6 | 2009-03-07 08:15:47 +0000 | [diff] [blame] | 173 | |
| 174 | // SignalHandler - The signal handler that runs. |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 175 | static RETSIGTYPE SignalHandler(int Sig) { |
Chris Lattner | bbbbbf3 | 2009-03-05 18:22:14 +0000 | [diff] [blame] | 176 | // Restore the signal behavior to default, so that the program actually |
| 177 | // crashes when we return and the signal reissues. This also ensures that if |
| 178 | // we crash in our signal handler that the program will terminate immediately |
| 179 | // instead of recursing in the signal handler. |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 180 | UnregisterHandlers(); |
Chris Lattner | 6acb4d6 | 2009-03-07 08:15:47 +0000 | [diff] [blame] | 181 | |
| 182 | // Unmask all potentially blocked kill signals. |
| 183 | sigset_t SigMask; |
| 184 | sigfillset(&SigMask); |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 185 | sigprocmask(SIG_UNBLOCK, &SigMask, nullptr); |
Chris Lattner | bbbbbf3 | 2009-03-05 18:22:14 +0000 | [diff] [blame] | 186 | |
Dylan Noblesmith | c4c5180 | 2014-08-23 23:07:14 +0000 | [diff] [blame] | 187 | { |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 188 | unique_lock<SmartMutex<true>> Guard(*SignalsMutex); |
Dylan Noblesmith | c4c5180 | 2014-08-23 23:07:14 +0000 | [diff] [blame] | 189 | RemoveFilesToRemove(); |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 190 | |
Chris Bieneman | b1cd51e | 2014-08-29 01:05:16 +0000 | [diff] [blame] | 191 | if (std::find(std::begin(IntSigs), std::end(IntSigs), Sig) |
| 192 | != std::end(IntSigs)) { |
Dylan Noblesmith | c4c5180 | 2014-08-23 23:07:14 +0000 | [diff] [blame] | 193 | if (InterruptFunction) { |
| 194 | void (*IF)() = InterruptFunction; |
| 195 | Guard.unlock(); |
| 196 | InterruptFunction = nullptr; |
| 197 | IF(); // run the interrupt function. |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | Guard.unlock(); |
| 202 | raise(Sig); // Execute the default handler. |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 203 | return; |
Dylan Noblesmith | c4c5180 | 2014-08-23 23:07:14 +0000 | [diff] [blame] | 204 | } |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // Otherwise if it is a fault (like SEGV) run any handler. |
Steven Wu | 9474669 | 2015-05-07 16:20:51 +0000 | [diff] [blame^] | 208 | if (CallBacksToRun.isConstructed()) { |
| 209 | std::vector<std::pair<void (*)(void *), void *>>& CallBacksToRunRef = |
| 210 | *CallBacksToRun; |
| 211 | for (unsigned i = 0, e = CallBacksToRun->size(); i != e; ++i) |
| 212 | CallBacksToRunRef[i].first(CallBacksToRunRef[i].second); |
| 213 | } |
Ulrich Weigand | 90c9abd | 2013-05-03 12:22:11 +0000 | [diff] [blame] | 214 | |
| 215 | #ifdef __s390__ |
| 216 | // On S/390, certain signals are delivered with PSW Address pointing to |
| 217 | // *after* the faulting instruction. Simply returning from the signal |
| 218 | // handler would continue execution after that point, instead of |
| 219 | // re-raising the signal. Raise the signal manually in those cases. |
| 220 | if (Sig == SIGILL || Sig == SIGFPE || Sig == SIGTRAP) |
| 221 | raise(Sig); |
| 222 | #endif |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Daniel Dunbar | 6827256 | 2010-05-08 02:10:34 +0000 | [diff] [blame] | 225 | void llvm::sys::RunInterruptHandlers() { |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 226 | sys::SmartScopedLock<true> Guard(*SignalsMutex); |
Dan Gohman | 288999b | 2010-05-27 23:11:55 +0000 | [diff] [blame] | 227 | RemoveFilesToRemove(); |
Daniel Dunbar | 6827256 | 2010-05-08 02:10:34 +0000 | [diff] [blame] | 228 | } |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 229 | |
Chris Lattner | bb1ba7b | 2009-03-08 19:13:45 +0000 | [diff] [blame] | 230 | void llvm::sys::SetInterruptFunction(void (*IF)()) { |
Dylan Noblesmith | 4704ffe | 2014-08-23 22:49:17 +0000 | [diff] [blame] | 231 | { |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 232 | sys::SmartScopedLock<true> Guard(*SignalsMutex); |
Dylan Noblesmith | 4704ffe | 2014-08-23 22:49:17 +0000 | [diff] [blame] | 233 | InterruptFunction = IF; |
| 234 | } |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 235 | RegisterHandlers(); |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // RemoveFileOnSignal - The public API |
Rafael Espindola | 4f35da7 | 2013-06-13 21:16:58 +0000 | [diff] [blame] | 239 | bool llvm::sys::RemoveFileOnSignal(StringRef Filename, |
Chris Lattner | bb1ba7b | 2009-03-08 19:13:45 +0000 | [diff] [blame] | 240 | std::string* ErrMsg) { |
Dylan Noblesmith | 4704ffe | 2014-08-23 22:49:17 +0000 | [diff] [blame] | 241 | { |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 242 | sys::SmartScopedLock<true> Guard(*SignalsMutex); |
| 243 | std::vector<std::string>& FilesToRemoveRef = *FilesToRemove; |
| 244 | std::string *OldPtr = |
| 245 | FilesToRemoveRef.empty() ? nullptr : &FilesToRemoveRef[0]; |
| 246 | FilesToRemoveRef.push_back(Filename); |
Chandler Carruth | e6196eb | 2012-06-16 00:09:41 +0000 | [diff] [blame] | 247 | |
Dylan Noblesmith | 4704ffe | 2014-08-23 22:49:17 +0000 | [diff] [blame] | 248 | // We want to call 'c_str()' on every std::string in this vector so that if |
| 249 | // the underlying implementation requires a re-allocation, it happens here |
| 250 | // rather than inside of the signal handler. If we see the vector grow, we |
| 251 | // have to call it on every entry. If it remains in place, we only need to |
| 252 | // call it on the latest one. |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 253 | if (OldPtr == &FilesToRemoveRef[0]) |
| 254 | FilesToRemoveRef.back().c_str(); |
Dylan Noblesmith | 4704ffe | 2014-08-23 22:49:17 +0000 | [diff] [blame] | 255 | else |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 256 | for (unsigned i = 0, e = FilesToRemoveRef.size(); i != e; ++i) |
| 257 | FilesToRemoveRef[i].c_str(); |
Dylan Noblesmith | 4704ffe | 2014-08-23 22:49:17 +0000 | [diff] [blame] | 258 | } |
Owen Anderson | 820739d | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 259 | |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 260 | RegisterHandlers(); |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 261 | return false; |
| 262 | } |
| 263 | |
Dan Gohman | e201c07 | 2010-09-01 14:17:34 +0000 | [diff] [blame] | 264 | // DontRemoveFileOnSignal - The public API |
Rafael Espindola | 4f35da7 | 2013-06-13 21:16:58 +0000 | [diff] [blame] | 265 | void llvm::sys::DontRemoveFileOnSignal(StringRef Filename) { |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 266 | sys::SmartScopedLock<true> Guard(*SignalsMutex); |
Chandler Carruth | e6196eb | 2012-06-16 00:09:41 +0000 | [diff] [blame] | 267 | std::vector<std::string>::reverse_iterator RI = |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 268 | std::find(FilesToRemove->rbegin(), FilesToRemove->rend(), Filename); |
| 269 | std::vector<std::string>::iterator I = FilesToRemove->end(); |
| 270 | if (RI != FilesToRemove->rend()) |
| 271 | I = FilesToRemove->erase(RI.base()-1); |
Chandler Carruth | e6196eb | 2012-06-16 00:09:41 +0000 | [diff] [blame] | 272 | |
| 273 | // We need to call c_str() on every element which would have been moved by |
| 274 | // the erase. These elements, in a C++98 implementation where c_str() |
| 275 | // requires a reallocation on the first call may have had the call to c_str() |
| 276 | // made on insertion become invalid by being copied down an element. |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 277 | for (std::vector<std::string>::iterator E = FilesToRemove->end(); I != E; ++I) |
Chandler Carruth | e6196eb | 2012-06-16 00:09:41 +0000 | [diff] [blame] | 278 | I->c_str(); |
Dan Gohman | e201c07 | 2010-09-01 14:17:34 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 281 | /// AddSignalHandler - Add a function to be called when a signal is delivered |
| 282 | /// to the process. The handler can have a cookie passed to it to identify |
| 283 | /// what instance of the handler it is. |
Chris Lattner | bb1ba7b | 2009-03-08 19:13:45 +0000 | [diff] [blame] | 284 | void llvm::sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) { |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 285 | CallBacksToRun->push_back(std::make_pair(FnPtr, Cookie)); |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 286 | RegisterHandlers(); |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 287 | } |
| 288 | |
NAKAMURA Takumi | 59fe0d4 | 2014-10-13 04:32:43 +0000 | [diff] [blame] | 289 | #if defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES) |
| 290 | |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 291 | #if HAVE_LINK_H && (defined(__linux__) || defined(__FreeBSD__) || \ |
| 292 | defined(__FreeBSD_kernel__) || defined(__NetBSD__)) |
| 293 | struct DlIteratePhdrData { |
| 294 | void **StackTrace; |
| 295 | int depth; |
| 296 | bool first; |
| 297 | const char **modules; |
| 298 | intptr_t *offsets; |
| 299 | const char *main_exec_name; |
| 300 | }; |
| 301 | |
| 302 | static int dl_iterate_phdr_cb(dl_phdr_info *info, size_t size, void *arg) { |
| 303 | DlIteratePhdrData *data = (DlIteratePhdrData*)arg; |
| 304 | const char *name = data->first ? data->main_exec_name : info->dlpi_name; |
| 305 | data->first = false; |
| 306 | for (int i = 0; i < info->dlpi_phnum; i++) { |
| 307 | const auto *phdr = &info->dlpi_phdr[i]; |
| 308 | if (phdr->p_type != PT_LOAD) |
| 309 | continue; |
| 310 | intptr_t beg = info->dlpi_addr + phdr->p_vaddr; |
| 311 | intptr_t end = beg + phdr->p_memsz; |
| 312 | for (int j = 0; j < data->depth; j++) { |
| 313 | if (data->modules[j]) |
| 314 | continue; |
| 315 | intptr_t addr = (intptr_t)data->StackTrace[j]; |
| 316 | if (beg <= addr && addr < end) { |
| 317 | data->modules[j] = name; |
| 318 | data->offsets[j] = addr - info->dlpi_addr; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | static bool findModulesAndOffsets(void **StackTrace, int Depth, |
| 326 | const char **Modules, intptr_t *Offsets, |
| 327 | const char *MainExecutableName) { |
| 328 | DlIteratePhdrData data = {StackTrace, Depth, true, |
| 329 | Modules, Offsets, MainExecutableName}; |
| 330 | dl_iterate_phdr(dl_iterate_phdr_cb, &data); |
| 331 | return true; |
| 332 | } |
| 333 | #else |
| 334 | static bool findModulesAndOffsets(void **StackTrace, int Depth, |
| 335 | const char **Modules, intptr_t *Offsets, |
| 336 | const char *MainExecutableName) { |
| 337 | return false; |
| 338 | } |
| 339 | #endif |
| 340 | |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 341 | static bool printSymbolizedStackTrace(void **StackTrace, int Depth, |
| 342 | llvm::raw_ostream &OS) { |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 343 | // FIXME: Subtract necessary number from StackTrace entries to turn return addresses |
| 344 | // into actual instruction addresses. |
| 345 | // Use llvm-symbolizer tool to symbolize the stack traces. |
Rafael Espindola | c1f3087 | 2014-11-04 12:35:47 +0000 | [diff] [blame] | 346 | ErrorOr<std::string> LLVMSymbolizerPathOrErr = |
| 347 | sys::findProgramByName("llvm-symbolizer"); |
| 348 | if (!LLVMSymbolizerPathOrErr) |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 349 | return false; |
Rafael Espindola | c1f3087 | 2014-11-04 12:35:47 +0000 | [diff] [blame] | 350 | const std::string &LLVMSymbolizerPath = *LLVMSymbolizerPathOrErr; |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 351 | // We don't know argv0 or the address of main() at this point, but try |
| 352 | // to guess it anyway (it's possible on some platforms). |
| 353 | std::string MainExecutableName = sys::fs::getMainExecutable(nullptr, nullptr); |
| 354 | if (MainExecutableName.empty() || |
| 355 | MainExecutableName.find("llvm-symbolizer") != std::string::npos) |
| 356 | return false; |
| 357 | |
| 358 | std::vector<const char *> Modules(Depth, nullptr); |
| 359 | std::vector<intptr_t> Offsets(Depth, 0); |
| 360 | if (!findModulesAndOffsets(StackTrace, Depth, Modules.data(), Offsets.data(), |
| 361 | MainExecutableName.c_str())) |
| 362 | return false; |
| 363 | int InputFD; |
| 364 | SmallString<32> InputFile, OutputFile; |
| 365 | sys::fs::createTemporaryFile("symbolizer-input", "", InputFD, InputFile); |
| 366 | sys::fs::createTemporaryFile("symbolizer-output", "", OutputFile); |
| 367 | FileRemover InputRemover(InputFile.c_str()); |
| 368 | FileRemover OutputRemover(OutputFile.c_str()); |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 369 | |
| 370 | { |
| 371 | raw_fd_ostream Input(InputFD, true); |
| 372 | for (int i = 0; i < Depth; i++) { |
| 373 | if (Modules[i]) |
| 374 | Input << Modules[i] << " " << (void*)Offsets[i] << "\n"; |
| 375 | } |
| 376 | } |
| 377 | |
Benjamin Kramer | 7ad2240 | 2014-10-22 19:55:26 +0000 | [diff] [blame] | 378 | StringRef InputFileStr(InputFile); |
| 379 | StringRef OutputFileStr(OutputFile); |
| 380 | StringRef StderrFileStr; |
| 381 | const StringRef *Redirects[] = {&InputFileStr, &OutputFileStr, |
| 382 | &StderrFileStr}; |
Alexey Samsonov | 96983b8 | 2014-10-10 22:58:26 +0000 | [diff] [blame] | 383 | const char *Args[] = {"llvm-symbolizer", "--functions=linkage", "--inlining", |
| 384 | "--demangle", nullptr}; |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 385 | int RunResult = |
Benjamin Kramer | 7ad2240 | 2014-10-22 19:55:26 +0000 | [diff] [blame] | 386 | sys::ExecuteAndWait(LLVMSymbolizerPath, Args, nullptr, Redirects); |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 387 | if (RunResult != 0) |
| 388 | return false; |
| 389 | |
| 390 | auto OutputBuf = MemoryBuffer::getFile(OutputFile.c_str()); |
| 391 | if (!OutputBuf) |
| 392 | return false; |
| 393 | StringRef Output = OutputBuf.get()->getBuffer(); |
| 394 | SmallVector<StringRef, 32> Lines; |
| 395 | Output.split(Lines, "\n"); |
| 396 | auto CurLine = Lines.begin(); |
| 397 | int frame_no = 0; |
| 398 | for (int i = 0; i < Depth; i++) { |
| 399 | if (!Modules[i]) { |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 400 | OS << format("#%d %p\n", frame_no++, StackTrace[i]); |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 401 | continue; |
| 402 | } |
| 403 | // Read pairs of lines (function name and file/line info) until we |
| 404 | // encounter empty line. |
| 405 | for (;;) { |
Alexey Samsonov | 96983b8 | 2014-10-10 22:58:26 +0000 | [diff] [blame] | 406 | if (CurLine == Lines.end()) |
| 407 | return false; |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 408 | StringRef FunctionName = *CurLine++; |
| 409 | if (FunctionName.empty()) |
| 410 | break; |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 411 | OS << format("#%d %p ", frame_no++, StackTrace[i]); |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 412 | if (!FunctionName.startswith("??")) |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 413 | OS << format("%s ", FunctionName.str().c_str()); |
Alexey Samsonov | 96983b8 | 2014-10-10 22:58:26 +0000 | [diff] [blame] | 414 | if (CurLine == Lines.end()) |
| 415 | return false; |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 416 | StringRef FileLineInfo = *CurLine++; |
| 417 | if (!FileLineInfo.startswith("??")) |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 418 | OS << format("%s", FileLineInfo.str().c_str()); |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 419 | else |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 420 | OS << format("(%s+%p)", Modules[i], (void *)Offsets[i]); |
| 421 | OS << "\n"; |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | return true; |
| 425 | } |
NAKAMURA Takumi | 59fe0d4 | 2014-10-13 04:32:43 +0000 | [diff] [blame] | 426 | #endif // defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES) |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 427 | |
| 428 | // PrintStackTrace - In the case of a program crash or fault, print out a stack |
| 429 | // trace so that the user has an indication of why and where we died. |
| 430 | // |
| 431 | // On glibc systems we have the 'backtrace' function, which works nicely, but |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 432 | // doesn't demangle symbols. |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 433 | void llvm::sys::PrintStackTrace(raw_ostream &OS) { |
Benjamin Kramer | 5651cbd | 2012-09-28 10:10:46 +0000 | [diff] [blame] | 434 | #if defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES) |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 435 | static void* StackTrace[256]; |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 436 | // Use backtrace() to output a backtrace on Linux systems with glibc. |
Evan Cheng | 86cb318 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 437 | int depth = backtrace(StackTrace, |
| 438 | static_cast<int>(array_lengthof(StackTrace))); |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 439 | if (printSymbolizedStackTrace(StackTrace, depth, OS)) |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 440 | return; |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 441 | #if HAVE_DLFCN_H && __GNUG__ |
| 442 | int width = 0; |
| 443 | for (int i = 0; i < depth; ++i) { |
| 444 | Dl_info dlinfo; |
| 445 | dladdr(StackTrace[i], &dlinfo); |
Dan Gohman | 1f517dd | 2009-02-10 17:56:28 +0000 | [diff] [blame] | 446 | const char* name = strrchr(dlinfo.dli_fname, '/'); |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 447 | |
| 448 | int nwidth; |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 449 | if (!name) nwidth = strlen(dlinfo.dli_fname); |
| 450 | else nwidth = strlen(name) - 1; |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 451 | |
| 452 | if (nwidth > width) width = nwidth; |
| 453 | } |
| 454 | |
| 455 | for (int i = 0; i < depth; ++i) { |
| 456 | Dl_info dlinfo; |
| 457 | dladdr(StackTrace[i], &dlinfo); |
| 458 | |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 459 | OS << format("%-2d", i); |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 460 | |
Dan Gohman | 1f517dd | 2009-02-10 17:56:28 +0000 | [diff] [blame] | 461 | const char* name = strrchr(dlinfo.dli_fname, '/'); |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 462 | if (!name) OS << format(" %-*s", width, dlinfo.dli_fname); |
| 463 | else OS << format(" %-*s", width, name+1); |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 464 | |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 465 | OS << format(" %#0*lx", (int)(sizeof(void*) * 2) + 2, |
| 466 | (unsigned long)StackTrace[i]); |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 467 | |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 468 | if (dlinfo.dli_sname != nullptr) { |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 469 | OS << ' '; |
Joerg Sonnenberger | 6624183 | 2013-04-27 22:12:32 +0000 | [diff] [blame] | 470 | # if HAVE_CXXABI_H |
Benjamin Kramer | 83e2a44 | 2013-04-28 07:47:04 +0000 | [diff] [blame] | 471 | int res; |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 472 | char* d = abi::__cxa_demangle(dlinfo.dli_sname, nullptr, nullptr, &res); |
Joerg Sonnenberger | 6624183 | 2013-04-27 22:12:32 +0000 | [diff] [blame] | 473 | # else |
| 474 | char* d = NULL; |
| 475 | # endif |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 476 | if (!d) OS << dlinfo.dli_sname; |
| 477 | else OS << d; |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 478 | free(d); |
| 479 | |
Edwin Vane | 44338e0 | 2013-01-28 19:34:42 +0000 | [diff] [blame] | 480 | // FIXME: When we move to C++11, use %t length modifier. It's not in |
| 481 | // C++03 and causes gcc to issue warnings. Losing the upper 32 bits of |
| 482 | // the stack offset for a stack dump isn't likely to cause any problems. |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 483 | OS << format(" + %u",(unsigned)((char*)StackTrace[i]- |
| 484 | (char*)dlinfo.dli_saddr)); |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 485 | } |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 486 | OS << '\n'; |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 487 | } |
| 488 | #else |
Lauro Ramos Venancio | eab51d3 | 2008-02-15 18:05:54 +0000 | [diff] [blame] | 489 | backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO); |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 490 | #endif |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 491 | #endif |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Argyrios Kyrtzidis | eb9ae76 | 2013-01-09 19:42:40 +0000 | [diff] [blame] | 494 | static void PrintStackTraceSignalHandler(void *) { |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 495 | PrintStackTrace(llvm::errs()); |
Argyrios Kyrtzidis | eb9ae76 | 2013-01-09 19:42:40 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Michael J. Spencer | 89b0ad2 | 2015-01-29 17:20:29 +0000 | [diff] [blame] | 498 | void llvm::sys::DisableSystemDialogsOnCrash() {} |
| 499 | |
NAKAMURA Takumi | 8a54d81 | 2012-09-06 03:01:43 +0000 | [diff] [blame] | 500 | /// PrintStackTraceOnErrorSignal - When an error signal (such as SIGABRT or |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 501 | /// SIGSEGV) is delivered to the process, print a stack trace and then exit. |
Pete Cooper | 6bea2f4 | 2015-04-07 20:43:23 +0000 | [diff] [blame] | 502 | void llvm::sys::PrintStackTraceOnErrorSignal(bool DisableCrashReporting) { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 503 | AddSignalHandler(PrintStackTraceSignalHandler, nullptr); |
Argyrios Kyrtzidis | cd8fe08 | 2012-01-11 20:53:25 +0000 | [diff] [blame] | 504 | |
Daniel Dunbar | eb6c708 | 2013-08-30 20:39:21 +0000 | [diff] [blame] | 505 | #if defined(__APPLE__) && defined(ENABLE_CRASH_OVERRIDES) |
Argyrios Kyrtzidis | cd8fe08 | 2012-01-11 20:53:25 +0000 | [diff] [blame] | 506 | // Environment variable to disable any kind of crash dialog. |
Pete Cooper | 6bea2f4 | 2015-04-07 20:43:23 +0000 | [diff] [blame] | 507 | if (DisableCrashReporting || getenv("LLVM_DISABLE_CRASH_REPORT")) { |
Argyrios Kyrtzidis | cd8fe08 | 2012-01-11 20:53:25 +0000 | [diff] [blame] | 508 | mach_port_t self = mach_task_self(); |
| 509 | |
| 510 | exception_mask_t mask = EXC_MASK_CRASH; |
| 511 | |
NAKAMURA Takumi | ffa1571 | 2012-09-06 03:02:56 +0000 | [diff] [blame] | 512 | kern_return_t ret = task_set_exception_ports(self, |
Argyrios Kyrtzidis | cd8fe08 | 2012-01-11 20:53:25 +0000 | [diff] [blame] | 513 | mask, |
Jean-Daniel Dupas | a573b22 | 2012-03-24 22:17:50 +0000 | [diff] [blame] | 514 | MACH_PORT_NULL, |
NAKAMURA Takumi | ffa1571 | 2012-09-06 03:02:56 +0000 | [diff] [blame] | 515 | EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES, |
Jean-Daniel Dupas | a573b22 | 2012-03-24 22:17:50 +0000 | [diff] [blame] | 516 | THREAD_STATE_NONE); |
Argyrios Kyrtzidis | cd8fe08 | 2012-01-11 20:53:25 +0000 | [diff] [blame] | 517 | (void)ret; |
| 518 | } |
| 519 | #endif |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 520 | } |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 521 | |
Daniel Dunbar | f14d946 | 2010-08-19 23:45:39 +0000 | [diff] [blame] | 522 | |
| 523 | /***/ |
| 524 | |
| 525 | // On Darwin, raise sends a signal to the main thread instead of the current |
| 526 | // thread. This has the unfortunate effect that assert() and abort() will end up |
| 527 | // bypassing our crash recovery attempts. We work around this for anything in |
| 528 | // the same linkage unit by just defining our own versions of the assert handler |
| 529 | // and abort. |
| 530 | |
Daniel Dunbar | eb6c708 | 2013-08-30 20:39:21 +0000 | [diff] [blame] | 531 | #if defined(__APPLE__) && defined(ENABLE_CRASH_OVERRIDES) |
Daniel Dunbar | f14d946 | 2010-08-19 23:45:39 +0000 | [diff] [blame] | 532 | |
Douglas Gregor | 0e50682 | 2011-04-29 16:12:17 +0000 | [diff] [blame] | 533 | #include <signal.h> |
| 534 | #include <pthread.h> |
| 535 | |
Daniel Dunbar | 18456f3 | 2010-09-22 17:46:10 +0000 | [diff] [blame] | 536 | int raise(int sig) { |
Daniel Dunbar | cc0e18d | 2010-10-08 18:31:34 +0000 | [diff] [blame] | 537 | return pthread_kill(pthread_self(), sig); |
Daniel Dunbar | 18456f3 | 2010-09-22 17:46:10 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Daniel Dunbar | f14d946 | 2010-08-19 23:45:39 +0000 | [diff] [blame] | 540 | void __assert_rtn(const char *func, |
| 541 | const char *file, |
| 542 | int line, |
| 543 | const char *expr) { |
| 544 | if (func) |
| 545 | fprintf(stderr, "Assertion failed: (%s), function %s, file %s, line %d.\n", |
| 546 | expr, func, file, line); |
| 547 | else |
| 548 | fprintf(stderr, "Assertion failed: (%s), file %s, line %d.\n", |
| 549 | expr, file, line); |
| 550 | abort(); |
| 551 | } |
| 552 | |
Daniel Dunbar | f14d946 | 2010-08-19 23:45:39 +0000 | [diff] [blame] | 553 | void abort() { |
Daniel Dunbar | 18456f3 | 2010-09-22 17:46:10 +0000 | [diff] [blame] | 554 | raise(SIGABRT); |
Daniel Dunbar | f14d946 | 2010-08-19 23:45:39 +0000 | [diff] [blame] | 555 | usleep(1000); |
| 556 | __builtin_trap(); |
| 557 | } |
| 558 | |
| 559 | #endif |