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