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" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Mutex.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include <algorithm> |
Chandler Carruth | e6196eb | 2012-06-16 00:09:41 +0000 | [diff] [blame] | 19 | #include <string> |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 20 | #include <vector> |
Reid Spencer | d554bbc | 2004-12-27 06:16:52 +0000 | [diff] [blame] | 21 | #if HAVE_EXECINFO_H |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 22 | # include <execinfo.h> // For backtrace(). |
| 23 | #endif |
Reid Spencer | d554bbc | 2004-12-27 06:16:52 +0000 | [diff] [blame] | 24 | #if HAVE_SIGNAL_H |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 25 | #include <signal.h> |
Reid Spencer | d554bbc | 2004-12-27 06:16:52 +0000 | [diff] [blame] | 26 | #endif |
Reid Spencer | ceeb918 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 27 | #if HAVE_SYS_STAT_H |
| 28 | #include <sys/stat.h> |
| 29 | #endif |
Joerg Sonnenberger | 6624183 | 2013-04-27 22:12:32 +0000 | [diff] [blame] | 30 | #if HAVE_CXXABI_H |
Joerg Sonnenberger | 4474409 | 2013-04-27 22:32:54 +0000 | [diff] [blame] | 31 | #include <cxxabi.h> |
Joerg Sonnenberger | 6624183 | 2013-04-27 22:12:32 +0000 | [diff] [blame] | 32 | #endif |
| 33 | #if HAVE_DLFCN_H |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 34 | #include <dlfcn.h> |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 35 | #endif |
Argyrios Kyrtzidis | cd8fe08 | 2012-01-11 20:53:25 +0000 | [diff] [blame] | 36 | #if HAVE_MACH_MACH_H |
| 37 | #include <mach/mach.h> |
| 38 | #endif |
| 39 | |
Chris Lattner | 80df7c4 | 2006-08-01 17:59:14 +0000 | [diff] [blame] | 40 | using namespace llvm; |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 41 | |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 42 | static RETSIGTYPE SignalHandler(int Sig); // defined below. |
| 43 | |
Owen Anderson | 820739d | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 44 | static SmartMutex<true> SignalsMutex; |
| 45 | |
Chris Lattner | 6a5d6ec | 2005-08-02 02:14:22 +0000 | [diff] [blame] | 46 | /// InterruptFunction - The function to call if ctrl-c is pressed. |
Dan Gohman | f4bc782 | 2008-04-10 21:11:47 +0000 | [diff] [blame] | 47 | static void (*InterruptFunction)() = 0; |
Chris Lattner | 6a5d6ec | 2005-08-02 02:14:22 +0000 | [diff] [blame] | 48 | |
Chandler Carruth | e6196eb | 2012-06-16 00:09:41 +0000 | [diff] [blame] | 49 | static std::vector<std::string> FilesToRemove; |
Jeffrey Yasskin | c4e3f05 | 2010-03-17 07:08:12 +0000 | [diff] [blame] | 50 | static std::vector<std::pair<void(*)(void*), void*> > CallBacksToRun; |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 51 | |
Dan Gohman | f857cd7 | 2013-02-20 19:28:46 +0000 | [diff] [blame] | 52 | // IntSigs - Signals that represent requested termination. There's no bug |
| 53 | // or failure, or if there is, it's not our direct responsibility. For whatever |
| 54 | // reason, our continued execution is no longer desirable. |
Dan Gohman | f4bc782 | 2008-04-10 21:11:47 +0000 | [diff] [blame] | 55 | static const int IntSigs[] = { |
Dan Gohman | 5cdb345 | 2013-02-20 19:15:01 +0000 | [diff] [blame] | 56 | SIGHUP, SIGINT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2 |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 57 | }; |
Craig Topper | 5871321 | 2013-07-15 04:27:47 +0000 | [diff] [blame] | 58 | static const int *const IntSigsEnd = array_endof(IntSigs); |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 59 | |
Dan Gohman | f857cd7 | 2013-02-20 19:28:46 +0000 | [diff] [blame] | 60 | // KillSigs - Signals that represent that we have a bug, and our prompt |
| 61 | // termination has been ordered. |
Dan Gohman | f4bc782 | 2008-04-10 21:11:47 +0000 | [diff] [blame] | 62 | static const int KillSigs[] = { |
Dan Gohman | 5cdb345 | 2013-02-20 19:15:01 +0000 | [diff] [blame] | 63 | SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGQUIT |
Chris Lattner | 62f50da | 2010-02-12 00:37:46 +0000 | [diff] [blame] | 64 | #ifdef SIGSYS |
| 65 | , SIGSYS |
| 66 | #endif |
| 67 | #ifdef SIGXCPU |
| 68 | , SIGXCPU |
| 69 | #endif |
Chris Lattner | 3b38fd6 | 2010-02-14 18:20:09 +0000 | [diff] [blame] | 70 | #ifdef SIGXFSZ |
Chris Lattner | 62f50da | 2010-02-12 00:37:46 +0000 | [diff] [blame] | 71 | , SIGXFSZ |
| 72 | #endif |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 73 | #ifdef SIGEMT |
| 74 | , SIGEMT |
| 75 | #endif |
| 76 | }; |
Craig Topper | 5871321 | 2013-07-15 04:27:47 +0000 | [diff] [blame] | 77 | static const int *const KillSigsEnd = array_endof(KillSigs); |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 78 | |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 79 | static unsigned NumRegisteredSignals = 0; |
| 80 | static struct { |
| 81 | struct sigaction SA; |
| 82 | int SigNo; |
| 83 | } RegisteredSignalInfo[(sizeof(IntSigs)+sizeof(KillSigs))/sizeof(KillSigs[0])]; |
| 84 | |
| 85 | |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 86 | static void RegisterHandler(int Signal) { |
Craig Topper | 26b45c2 | 2013-07-15 04:37:54 +0000 | [diff] [blame^] | 87 | assert(NumRegisteredSignals < |
| 88 | sizeof(RegisteredSignalInfo)/sizeof(RegisteredSignalInfo[0]) && |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 89 | "Out of space for signal handlers!"); |
| 90 | |
| 91 | struct sigaction NewHandler; |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 92 | |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 93 | NewHandler.sa_handler = SignalHandler; |
| 94 | NewHandler.sa_flags = SA_NODEFER|SA_RESETHAND; |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 95 | sigemptyset(&NewHandler.sa_mask); |
| 96 | |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 97 | // Install the new handler, save the old one in RegisteredSignalInfo. |
| 98 | sigaction(Signal, &NewHandler, |
| 99 | &RegisteredSignalInfo[NumRegisteredSignals].SA); |
| 100 | RegisteredSignalInfo[NumRegisteredSignals].SigNo = Signal; |
| 101 | ++NumRegisteredSignals; |
Chris Lattner | 6acb4d6 | 2009-03-07 08:15:47 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 104 | static void RegisterHandlers() { |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 105 | // If the handlers are already registered, we're done. |
| 106 | if (NumRegisteredSignals != 0) return; |
| 107 | |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 108 | std::for_each(IntSigs, IntSigsEnd, RegisterHandler); |
| 109 | std::for_each(KillSigs, KillSigsEnd, RegisterHandler); |
| 110 | } |
| 111 | |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 112 | static void UnregisterHandlers() { |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 113 | // Restore all of the signal handlers to how they were before we showed up. |
| 114 | for (unsigned i = 0, e = NumRegisteredSignals; i != e; ++i) |
Chris Lattner | f2b6065 | 2009-03-23 06:46:20 +0000 | [diff] [blame] | 115 | sigaction(RegisteredSignalInfo[i].SigNo, |
| 116 | &RegisteredSignalInfo[i].SA, 0); |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 117 | NumRegisteredSignals = 0; |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | |
Dan Gohman | 288999b | 2010-05-27 23:11:55 +0000 | [diff] [blame] | 121 | /// RemoveFilesToRemove - Process the FilesToRemove list. This function |
| 122 | /// should be called with the SignalsMutex lock held. |
Chandler Carruth | e6196eb | 2012-06-16 00:09:41 +0000 | [diff] [blame] | 123 | /// NB: This must be an async signal safe function. It cannot allocate or free |
| 124 | /// memory, even in debug builds. |
Dan Gohman | 288999b | 2010-05-27 23:11:55 +0000 | [diff] [blame] | 125 | static void RemoveFilesToRemove() { |
Daniel Dunbar | 511479d | 2012-10-17 16:30:54 +0000 | [diff] [blame] | 126 | // We avoid iterators in case of debug iterators that allocate or release |
Chandler Carruth | e6196eb | 2012-06-16 00:09:41 +0000 | [diff] [blame] | 127 | // memory. |
| 128 | for (unsigned i = 0, e = FilesToRemove.size(); i != e; ++i) { |
Daniel Dunbar | 511479d | 2012-10-17 16:30:54 +0000 | [diff] [blame] | 129 | // We rely on a std::string implementation for which repeated calls to |
| 130 | // 'c_str()' don't allocate memory. We pre-call 'c_str()' on all of these |
| 131 | // strings to try to ensure this is safe. |
| 132 | const char *path = FilesToRemove[i].c_str(); |
| 133 | |
| 134 | // Get the status so we can determine if it's a file or directory. If we |
| 135 | // can't stat the file, ignore it. |
| 136 | struct stat buf; |
| 137 | if (stat(path, &buf) != 0) |
| 138 | continue; |
| 139 | |
| 140 | // If this is not a regular file, ignore it. We want to prevent removal of |
| 141 | // special files like /dev/null, even if the compiler is being run with the |
| 142 | // super-user permissions. |
| 143 | if (!S_ISREG(buf.st_mode)) |
| 144 | continue; |
| 145 | |
| 146 | // Otherwise, remove the file. We ignore any errors here as there is nothing |
| 147 | // else we can do. |
| 148 | unlink(path); |
Dan Gohman | 288999b | 2010-05-27 23:11:55 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
Chris Lattner | 6acb4d6 | 2009-03-07 08:15:47 +0000 | [diff] [blame] | 151 | |
| 152 | // SignalHandler - The signal handler that runs. |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 153 | static RETSIGTYPE SignalHandler(int Sig) { |
Chris Lattner | bbbbbf3 | 2009-03-05 18:22:14 +0000 | [diff] [blame] | 154 | // Restore the signal behavior to default, so that the program actually |
| 155 | // crashes when we return and the signal reissues. This also ensures that if |
| 156 | // we crash in our signal handler that the program will terminate immediately |
| 157 | // instead of recursing in the signal handler. |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 158 | UnregisterHandlers(); |
Chris Lattner | 6acb4d6 | 2009-03-07 08:15:47 +0000 | [diff] [blame] | 159 | |
| 160 | // Unmask all potentially blocked kill signals. |
| 161 | sigset_t SigMask; |
| 162 | sigfillset(&SigMask); |
| 163 | sigprocmask(SIG_UNBLOCK, &SigMask, 0); |
Chris Lattner | bbbbbf3 | 2009-03-05 18:22:14 +0000 | [diff] [blame] | 164 | |
Owen Anderson | 820739d | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 165 | SignalsMutex.acquire(); |
Dan Gohman | 288999b | 2010-05-27 23:11:55 +0000 | [diff] [blame] | 166 | RemoveFilesToRemove(); |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 167 | |
| 168 | if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd) { |
| 169 | if (InterruptFunction) { |
| 170 | void (*IF)() = InterruptFunction; |
Owen Anderson | 820739d | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 171 | SignalsMutex.release(); |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 172 | InterruptFunction = 0; |
| 173 | IF(); // run the interrupt function. |
| 174 | return; |
| 175 | } |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 176 | |
Owen Anderson | 820739d | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 177 | SignalsMutex.release(); |
Chris Lattner | ce8b7d2 | 2009-04-12 23:33:13 +0000 | [diff] [blame] | 178 | raise(Sig); // Execute the default handler. |
| 179 | return; |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Owen Anderson | 820739d | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 182 | SignalsMutex.release(); |
| 183 | |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 184 | // Otherwise if it is a fault (like SEGV) run any handler. |
Jeffrey Yasskin | c4e3f05 | 2010-03-17 07:08:12 +0000 | [diff] [blame] | 185 | for (unsigned i = 0, e = CallBacksToRun.size(); i != e; ++i) |
| 186 | CallBacksToRun[i].first(CallBacksToRun[i].second); |
Ulrich Weigand | 90c9abd | 2013-05-03 12:22:11 +0000 | [diff] [blame] | 187 | |
| 188 | #ifdef __s390__ |
| 189 | // On S/390, certain signals are delivered with PSW Address pointing to |
| 190 | // *after* the faulting instruction. Simply returning from the signal |
| 191 | // handler would continue execution after that point, instead of |
| 192 | // re-raising the signal. Raise the signal manually in those cases. |
| 193 | if (Sig == SIGILL || Sig == SIGFPE || Sig == SIGTRAP) |
| 194 | raise(Sig); |
| 195 | #endif |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Daniel Dunbar | 6827256 | 2010-05-08 02:10:34 +0000 | [diff] [blame] | 198 | void llvm::sys::RunInterruptHandlers() { |
Dan Gohman | 288999b | 2010-05-27 23:11:55 +0000 | [diff] [blame] | 199 | SignalsMutex.acquire(); |
| 200 | RemoveFilesToRemove(); |
| 201 | SignalsMutex.release(); |
Daniel Dunbar | 6827256 | 2010-05-08 02:10:34 +0000 | [diff] [blame] | 202 | } |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 203 | |
Chris Lattner | bb1ba7b | 2009-03-08 19:13:45 +0000 | [diff] [blame] | 204 | void llvm::sys::SetInterruptFunction(void (*IF)()) { |
Owen Anderson | 820739d | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 205 | SignalsMutex.acquire(); |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 206 | InterruptFunction = IF; |
Owen Anderson | 820739d | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 207 | SignalsMutex.release(); |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 208 | RegisterHandlers(); |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | // RemoveFileOnSignal - The public API |
Rafael Espindola | 4f35da7 | 2013-06-13 21:16:58 +0000 | [diff] [blame] | 212 | bool llvm::sys::RemoveFileOnSignal(StringRef Filename, |
Chris Lattner | bb1ba7b | 2009-03-08 19:13:45 +0000 | [diff] [blame] | 213 | std::string* ErrMsg) { |
Owen Anderson | 820739d | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 214 | SignalsMutex.acquire(); |
Chandler Carruth | 52de271 | 2012-06-16 00:44:07 +0000 | [diff] [blame] | 215 | std::string *OldPtr = FilesToRemove.empty() ? 0 : &FilesToRemove[0]; |
Rafael Espindola | 4f35da7 | 2013-06-13 21:16:58 +0000 | [diff] [blame] | 216 | FilesToRemove.push_back(Filename); |
Chandler Carruth | e6196eb | 2012-06-16 00:09:41 +0000 | [diff] [blame] | 217 | |
| 218 | // We want to call 'c_str()' on every std::string in this vector so that if |
| 219 | // the underlying implementation requires a re-allocation, it happens here |
| 220 | // rather than inside of the signal handler. If we see the vector grow, we |
| 221 | // have to call it on every entry. If it remains in place, we only need to |
| 222 | // call it on the latest one. |
| 223 | if (OldPtr == &FilesToRemove[0]) |
| 224 | FilesToRemove.back().c_str(); |
| 225 | else |
| 226 | for (unsigned i = 0, e = FilesToRemove.size(); i != e; ++i) |
| 227 | FilesToRemove[i].c_str(); |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 228 | |
Owen Anderson | 820739d | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 229 | SignalsMutex.release(); |
| 230 | |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 231 | RegisterHandlers(); |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 232 | return false; |
| 233 | } |
| 234 | |
Dan Gohman | e201c07 | 2010-09-01 14:17:34 +0000 | [diff] [blame] | 235 | // DontRemoveFileOnSignal - The public API |
Rafael Espindola | 4f35da7 | 2013-06-13 21:16:58 +0000 | [diff] [blame] | 236 | void llvm::sys::DontRemoveFileOnSignal(StringRef Filename) { |
Dan Gohman | e201c07 | 2010-09-01 14:17:34 +0000 | [diff] [blame] | 237 | SignalsMutex.acquire(); |
Chandler Carruth | e6196eb | 2012-06-16 00:09:41 +0000 | [diff] [blame] | 238 | std::vector<std::string>::reverse_iterator RI = |
Rafael Espindola | 4f35da7 | 2013-06-13 21:16:58 +0000 | [diff] [blame] | 239 | std::find(FilesToRemove.rbegin(), FilesToRemove.rend(), Filename); |
Chandler Carruth | e6196eb | 2012-06-16 00:09:41 +0000 | [diff] [blame] | 240 | std::vector<std::string>::iterator I = FilesToRemove.end(); |
| 241 | if (RI != FilesToRemove.rend()) |
| 242 | I = FilesToRemove.erase(RI.base()-1); |
| 243 | |
| 244 | // We need to call c_str() on every element which would have been moved by |
| 245 | // the erase. These elements, in a C++98 implementation where c_str() |
| 246 | // requires a reallocation on the first call may have had the call to c_str() |
| 247 | // made on insertion become invalid by being copied down an element. |
| 248 | for (std::vector<std::string>::iterator E = FilesToRemove.end(); I != E; ++I) |
| 249 | I->c_str(); |
| 250 | |
Dan Gohman | e201c07 | 2010-09-01 14:17:34 +0000 | [diff] [blame] | 251 | SignalsMutex.release(); |
| 252 | } |
| 253 | |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 254 | /// AddSignalHandler - Add a function to be called when a signal is delivered |
| 255 | /// to the process. The handler can have a cookie passed to it to identify |
| 256 | /// what instance of the handler it is. |
Chris Lattner | bb1ba7b | 2009-03-08 19:13:45 +0000 | [diff] [blame] | 257 | void llvm::sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) { |
Jeffrey Yasskin | c4e3f05 | 2010-03-17 07:08:12 +0000 | [diff] [blame] | 258 | CallBacksToRun.push_back(std::make_pair(FnPtr, Cookie)); |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 259 | RegisterHandlers(); |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 262 | |
| 263 | // PrintStackTrace - In the case of a program crash or fault, print out a stack |
| 264 | // trace so that the user has an indication of why and where we died. |
| 265 | // |
| 266 | // 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] | 267 | // doesn't demangle symbols. |
Argyrios Kyrtzidis | eb9ae76 | 2013-01-09 19:42:40 +0000 | [diff] [blame] | 268 | void llvm::sys::PrintStackTrace(FILE *FD) { |
Benjamin Kramer | 5651cbd | 2012-09-28 10:10:46 +0000 | [diff] [blame] | 269 | #if defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES) |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 270 | static void* StackTrace[256]; |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 271 | // Use backtrace() to output a backtrace on Linux systems with glibc. |
Evan Cheng | 86cb318 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 272 | int depth = backtrace(StackTrace, |
| 273 | static_cast<int>(array_lengthof(StackTrace))); |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 274 | #if HAVE_DLFCN_H && __GNUG__ |
| 275 | int width = 0; |
| 276 | for (int i = 0; i < depth; ++i) { |
| 277 | Dl_info dlinfo; |
| 278 | dladdr(StackTrace[i], &dlinfo); |
Dan Gohman | 1f517dd | 2009-02-10 17:56:28 +0000 | [diff] [blame] | 279 | const char* name = strrchr(dlinfo.dli_fname, '/'); |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 280 | |
| 281 | int nwidth; |
| 282 | if (name == NULL) nwidth = strlen(dlinfo.dli_fname); |
| 283 | else nwidth = strlen(name) - 1; |
| 284 | |
| 285 | if (nwidth > width) width = nwidth; |
| 286 | } |
| 287 | |
| 288 | for (int i = 0; i < depth; ++i) { |
| 289 | Dl_info dlinfo; |
| 290 | dladdr(StackTrace[i], &dlinfo); |
| 291 | |
Argyrios Kyrtzidis | eb9ae76 | 2013-01-09 19:42:40 +0000 | [diff] [blame] | 292 | fprintf(FD, "%-2d", i); |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 293 | |
Dan Gohman | 1f517dd | 2009-02-10 17:56:28 +0000 | [diff] [blame] | 294 | const char* name = strrchr(dlinfo.dli_fname, '/'); |
Argyrios Kyrtzidis | eb9ae76 | 2013-01-09 19:42:40 +0000 | [diff] [blame] | 295 | if (name == NULL) fprintf(FD, " %-*s", width, dlinfo.dli_fname); |
| 296 | else fprintf(FD, " %-*s", width, name+1); |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 297 | |
Argyrios Kyrtzidis | eb9ae76 | 2013-01-09 19:42:40 +0000 | [diff] [blame] | 298 | fprintf(FD, " %#0*lx", |
Dan Gohman | 35e2cfc | 2008-12-05 23:39:24 +0000 | [diff] [blame] | 299 | (int)(sizeof(void*) * 2) + 2, (unsigned long)StackTrace[i]); |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 300 | |
| 301 | if (dlinfo.dli_sname != NULL) { |
Argyrios Kyrtzidis | eb9ae76 | 2013-01-09 19:42:40 +0000 | [diff] [blame] | 302 | fputc(' ', FD); |
Joerg Sonnenberger | 6624183 | 2013-04-27 22:12:32 +0000 | [diff] [blame] | 303 | # if HAVE_CXXABI_H |
Benjamin Kramer | 83e2a44 | 2013-04-28 07:47:04 +0000 | [diff] [blame] | 304 | int res; |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 305 | char* d = abi::__cxa_demangle(dlinfo.dli_sname, NULL, NULL, &res); |
Joerg Sonnenberger | 6624183 | 2013-04-27 22:12:32 +0000 | [diff] [blame] | 306 | # else |
| 307 | char* d = NULL; |
| 308 | # endif |
Argyrios Kyrtzidis | eb9ae76 | 2013-01-09 19:42:40 +0000 | [diff] [blame] | 309 | if (d == NULL) fputs(dlinfo.dli_sname, FD); |
| 310 | else fputs(d, FD); |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 311 | free(d); |
| 312 | |
Edwin Vane | 44338e0 | 2013-01-28 19:34:42 +0000 | [diff] [blame] | 313 | // FIXME: When we move to C++11, use %t length modifier. It's not in |
| 314 | // C++03 and causes gcc to issue warnings. Losing the upper 32 bits of |
| 315 | // the stack offset for a stack dump isn't likely to cause any problems. |
| 316 | fprintf(FD, " + %u",(unsigned)((char*)StackTrace[i]- |
| 317 | (char*)dlinfo.dli_saddr)); |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 318 | } |
Argyrios Kyrtzidis | eb9ae76 | 2013-01-09 19:42:40 +0000 | [diff] [blame] | 319 | fputc('\n', FD); |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 320 | } |
| 321 | #else |
Lauro Ramos Venancio | eab51d3 | 2008-02-15 18:05:54 +0000 | [diff] [blame] | 322 | backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO); |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 323 | #endif |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 324 | #endif |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Argyrios Kyrtzidis | eb9ae76 | 2013-01-09 19:42:40 +0000 | [diff] [blame] | 327 | static void PrintStackTraceSignalHandler(void *) { |
| 328 | PrintStackTrace(stderr); |
| 329 | } |
| 330 | |
NAKAMURA Takumi | 8a54d81 | 2012-09-06 03:01:43 +0000 | [diff] [blame] | 331 | /// PrintStackTraceOnErrorSignal - When an error signal (such as SIGABRT or |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 332 | /// SIGSEGV) is delivered to the process, print a stack trace and then exit. |
Chris Lattner | bb1ba7b | 2009-03-08 19:13:45 +0000 | [diff] [blame] | 333 | void llvm::sys::PrintStackTraceOnErrorSignal() { |
Argyrios Kyrtzidis | eb9ae76 | 2013-01-09 19:42:40 +0000 | [diff] [blame] | 334 | AddSignalHandler(PrintStackTraceSignalHandler, 0); |
Argyrios Kyrtzidis | cd8fe08 | 2012-01-11 20:53:25 +0000 | [diff] [blame] | 335 | |
| 336 | #if defined(__APPLE__) |
| 337 | // Environment variable to disable any kind of crash dialog. |
| 338 | if (getenv("LLVM_DISABLE_CRASH_REPORT")) { |
| 339 | mach_port_t self = mach_task_self(); |
| 340 | |
| 341 | exception_mask_t mask = EXC_MASK_CRASH; |
| 342 | |
NAKAMURA Takumi | ffa1571 | 2012-09-06 03:02:56 +0000 | [diff] [blame] | 343 | kern_return_t ret = task_set_exception_ports(self, |
Argyrios Kyrtzidis | cd8fe08 | 2012-01-11 20:53:25 +0000 | [diff] [blame] | 344 | mask, |
Jean-Daniel Dupas | a573b22 | 2012-03-24 22:17:50 +0000 | [diff] [blame] | 345 | MACH_PORT_NULL, |
NAKAMURA Takumi | ffa1571 | 2012-09-06 03:02:56 +0000 | [diff] [blame] | 346 | EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES, |
Jean-Daniel Dupas | a573b22 | 2012-03-24 22:17:50 +0000 | [diff] [blame] | 347 | THREAD_STATE_NONE); |
Argyrios Kyrtzidis | cd8fe08 | 2012-01-11 20:53:25 +0000 | [diff] [blame] | 348 | (void)ret; |
| 349 | } |
| 350 | #endif |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 351 | } |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 352 | |
Daniel Dunbar | f14d946 | 2010-08-19 23:45:39 +0000 | [diff] [blame] | 353 | |
| 354 | /***/ |
| 355 | |
| 356 | // On Darwin, raise sends a signal to the main thread instead of the current |
| 357 | // thread. This has the unfortunate effect that assert() and abort() will end up |
| 358 | // bypassing our crash recovery attempts. We work around this for anything in |
| 359 | // the same linkage unit by just defining our own versions of the assert handler |
| 360 | // and abort. |
| 361 | |
| 362 | #ifdef __APPLE__ |
| 363 | |
Douglas Gregor | 0e50682 | 2011-04-29 16:12:17 +0000 | [diff] [blame] | 364 | #include <signal.h> |
| 365 | #include <pthread.h> |
| 366 | |
Daniel Dunbar | 18456f3 | 2010-09-22 17:46:10 +0000 | [diff] [blame] | 367 | int raise(int sig) { |
Daniel Dunbar | cc0e18d | 2010-10-08 18:31:34 +0000 | [diff] [blame] | 368 | return pthread_kill(pthread_self(), sig); |
Daniel Dunbar | 18456f3 | 2010-09-22 17:46:10 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Daniel Dunbar | f14d946 | 2010-08-19 23:45:39 +0000 | [diff] [blame] | 371 | void __assert_rtn(const char *func, |
| 372 | const char *file, |
| 373 | int line, |
| 374 | const char *expr) { |
| 375 | if (func) |
| 376 | fprintf(stderr, "Assertion failed: (%s), function %s, file %s, line %d.\n", |
| 377 | expr, func, file, line); |
| 378 | else |
| 379 | fprintf(stderr, "Assertion failed: (%s), file %s, line %d.\n", |
| 380 | expr, file, line); |
| 381 | abort(); |
| 382 | } |
| 383 | |
Daniel Dunbar | f14d946 | 2010-08-19 23:45:39 +0000 | [diff] [blame] | 384 | void abort() { |
Daniel Dunbar | 18456f3 | 2010-09-22 17:46:10 +0000 | [diff] [blame] | 385 | raise(SIGABRT); |
Daniel Dunbar | f14d946 | 2010-08-19 23:45:39 +0000 | [diff] [blame] | 386 | usleep(1000); |
| 387 | __builtin_trap(); |
| 388 | } |
| 389 | |
| 390 | #endif |