Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +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" |
Rafael Espindola | b940b66 | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 17 | #include "llvm/Demangle/Demangle.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" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Format.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> |
Ed Maste | e544379 | 2017-04-12 13:51:00 +0000 | [diff] [blame] | 28 | #ifdef HAVE_BACKTRACE |
| 29 | # include BACKTRACE_HEADER // For backtrace(). |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 30 | #endif |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 31 | #if HAVE_SIGNAL_H |
| 32 | #include <signal.h> |
| 33 | #endif |
Reid Spencer | ceeb918 | 2007-04-07 18:52:17 +0000 | [diff] [blame] | 34 | #if HAVE_SYS_STAT_H |
| 35 | #include <sys/stat.h> |
| 36 | #endif |
Joerg Sonnenberger | 6624183 | 2013-04-27 22:12:32 +0000 | [diff] [blame] | 37 | #if HAVE_DLFCN_H |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 38 | #include <dlfcn.h> |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 39 | #endif |
Argyrios Kyrtzidis | cd8fe08 | 2012-01-11 20:53:25 +0000 | [diff] [blame] | 40 | #if HAVE_MACH_MACH_H |
| 41 | #include <mach/mach.h> |
| 42 | #endif |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 43 | #if HAVE_LINK_H |
| 44 | #include <link.h> |
| 45 | #endif |
Joerg Sonnenberger | 5c50539 | 2016-09-29 21:07:57 +0000 | [diff] [blame] | 46 | #ifdef HAVE__UNWIND_BACKTRACE |
Richard Smith | 14d9651 | 2016-05-20 21:18:12 +0000 | [diff] [blame] | 47 | // FIXME: We should be able to use <unwind.h> for any target that has an |
| 48 | // _Unwind_Backtrace function, but on FreeBSD the configure test passes |
| 49 | // despite the function not existing, and on Android, <unwind.h> conflicts |
| 50 | // with <link.h>. |
Bob Wilson | 37df90a | 2017-01-06 02:26:33 +0000 | [diff] [blame] | 51 | #ifdef __GLIBC__ |
Richard Smith | 14d9651 | 2016-05-20 21:18:12 +0000 | [diff] [blame] | 52 | #include <unwind.h> |
| 53 | #else |
Joerg Sonnenberger | 5c50539 | 2016-09-29 21:07:57 +0000 | [diff] [blame] | 54 | #undef HAVE__UNWIND_BACKTRACE |
Richard Smith | 14d9651 | 2016-05-20 21:18:12 +0000 | [diff] [blame] | 55 | #endif |
| 56 | #endif |
Argyrios Kyrtzidis | cd8fe08 | 2012-01-11 20:53:25 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 80df7c4 | 2006-08-01 17:59:14 +0000 | [diff] [blame] | 58 | using namespace llvm; |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 59 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 60 | static RETSIGTYPE SignalHandler(int Sig); // defined below. |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 61 | |
Kristof Beyls | 7adf8c5 | 2017-03-31 14:58:52 +0000 | [diff] [blame] | 62 | static ManagedStatic<sys::SmartMutex<true> > SignalsMutex; |
Owen Anderson | 820739d | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 6a5d6ec | 2005-08-02 02:14:22 +0000 | [diff] [blame] | 64 | /// InterruptFunction - The function to call if ctrl-c is pressed. |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 65 | static void (*InterruptFunction)() = nullptr; |
Chris Lattner | 6a5d6ec | 2005-08-02 02:14:22 +0000 | [diff] [blame] | 66 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 67 | static ManagedStatic<std::vector<std::string>> FilesToRemove; |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 68 | |
Richard Smith | 2ad6d48 | 2016-06-09 00:53:21 +0000 | [diff] [blame] | 69 | static StringRef Argv0; |
| 70 | |
Dan Gohman | f857cd7 | 2013-02-20 19:28:46 +0000 | [diff] [blame] | 71 | // IntSigs - Signals that represent requested termination. There's no bug |
| 72 | // or failure, or if there is, it's not our direct responsibility. For whatever |
| 73 | // reason, our continued execution is no longer desirable. |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 74 | static const int IntSigs[] = { |
Dan Gohman | 5cdb345 | 2013-02-20 19:15:01 +0000 | [diff] [blame] | 75 | SIGHUP, SIGINT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2 |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 76 | }; |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 77 | |
Dan Gohman | f857cd7 | 2013-02-20 19:28:46 +0000 | [diff] [blame] | 78 | // KillSigs - Signals that represent that we have a bug, and our prompt |
| 79 | // termination has been ordered. |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 80 | static const int KillSigs[] = { |
Dan Gohman | 5cdb345 | 2013-02-20 19:15:01 +0000 | [diff] [blame] | 81 | SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGQUIT |
Chris Lattner | 62f50da | 2010-02-12 00:37:46 +0000 | [diff] [blame] | 82 | #ifdef SIGSYS |
| 83 | , SIGSYS |
| 84 | #endif |
| 85 | #ifdef SIGXCPU |
| 86 | , SIGXCPU |
| 87 | #endif |
Chris Lattner | 3b38fd6 | 2010-02-14 18:20:09 +0000 | [diff] [blame] | 88 | #ifdef SIGXFSZ |
Chris Lattner | 62f50da | 2010-02-12 00:37:46 +0000 | [diff] [blame] | 89 | , SIGXFSZ |
| 90 | #endif |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 91 | #ifdef SIGEMT |
| 92 | , SIGEMT |
| 93 | #endif |
| 94 | }; |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 95 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 96 | static unsigned NumRegisteredSignals = 0; |
| 97 | static struct { |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 98 | struct sigaction SA; |
| 99 | int SigNo; |
Craig Topper | fac9057 | 2015-12-01 06:12:59 +0000 | [diff] [blame] | 100 | } RegisteredSignalInfo[array_lengthof(IntSigs) + array_lengthof(KillSigs)]; |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 101 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 102 | |
| 103 | static void RegisterHandler(int Signal) { |
Craig Topper | fac9057 | 2015-12-01 06:12:59 +0000 | [diff] [blame] | 104 | assert(NumRegisteredSignals < array_lengthof(RegisteredSignalInfo) && |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 105 | "Out of space for signal handlers!"); |
| 106 | |
| 107 | struct sigaction NewHandler; |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 108 | |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 109 | NewHandler.sa_handler = SignalHandler; |
Richard Smith | b8b0be9 | 2016-05-23 06:47:37 +0000 | [diff] [blame] | 110 | NewHandler.sa_flags = SA_NODEFER | SA_RESETHAND | SA_ONSTACK; |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 111 | sigemptyset(&NewHandler.sa_mask); |
| 112 | |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 113 | // Install the new handler, save the old one in RegisteredSignalInfo. |
| 114 | sigaction(Signal, &NewHandler, |
| 115 | &RegisteredSignalInfo[NumRegisteredSignals].SA); |
| 116 | RegisteredSignalInfo[NumRegisteredSignals].SigNo = Signal; |
| 117 | ++NumRegisteredSignals; |
Chris Lattner | 6acb4d6 | 2009-03-07 08:15:47 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Richard Smith | abab5d2 | 2016-05-20 21:26:00 +0000 | [diff] [blame] | 120 | #if defined(HAVE_SIGALTSTACK) |
Chandler Carruth | eb232dc | 2016-08-24 03:42:51 +0000 | [diff] [blame] | 121 | // Hold onto both the old and new alternate signal stack so that it's not |
| 122 | // reported as a leak. We don't make any attempt to remove our alt signal |
| 123 | // stack if we remove our signal handlers; that can't be done reliably if |
| 124 | // someone else is also trying to do the same thing. |
Richard Smith | 4b735c5 | 2016-05-20 21:38:15 +0000 | [diff] [blame] | 125 | static stack_t OldAltStack; |
Chandler Carruth | eb232dc | 2016-08-24 03:42:51 +0000 | [diff] [blame] | 126 | static void* NewAltStackPointer; |
Richard Smith | e881134 | 2016-05-20 21:07:41 +0000 | [diff] [blame] | 127 | |
| 128 | static void CreateSigAltStack() { |
Richard Smith | b311631 | 2016-08-24 00:54:49 +0000 | [diff] [blame] | 129 | const size_t AltStackSize = MINSIGSTKSZ + 64 * 1024; |
Richard Smith | e881134 | 2016-05-20 21:07:41 +0000 | [diff] [blame] | 130 | |
| 131 | // If we're executing on the alternate stack, or we already have an alternate |
| 132 | // signal stack that we're happy with, there's nothing for us to do. Don't |
| 133 | // reduce the size, some other part of the process might need a larger stack |
| 134 | // than we do. |
| 135 | if (sigaltstack(nullptr, &OldAltStack) != 0 || |
| 136 | OldAltStack.ss_flags & SS_ONSTACK || |
| 137 | (OldAltStack.ss_sp && OldAltStack.ss_size >= AltStackSize)) |
| 138 | return; |
| 139 | |
Richard Smith | 4b735c5 | 2016-05-20 21:38:15 +0000 | [diff] [blame] | 140 | stack_t AltStack = {}; |
Serge Pavlov | 76d8cce | 2018-02-20 05:41:26 +0000 | [diff] [blame] | 141 | AltStack.ss_sp = static_cast<char *>(safe_malloc(AltStackSize)); |
Chandler Carruth | eb232dc | 2016-08-24 03:42:51 +0000 | [diff] [blame] | 142 | NewAltStackPointer = AltStack.ss_sp; // Save to avoid reporting a leak. |
Richard Smith | e881134 | 2016-05-20 21:07:41 +0000 | [diff] [blame] | 143 | AltStack.ss_size = AltStackSize; |
| 144 | if (sigaltstack(&AltStack, &OldAltStack) != 0) |
| 145 | free(AltStack.ss_sp); |
| 146 | } |
Richard Smith | abab5d2 | 2016-05-20 21:26:00 +0000 | [diff] [blame] | 147 | #else |
| 148 | static void CreateSigAltStack() {} |
| 149 | #endif |
Richard Smith | e881134 | 2016-05-20 21:07:41 +0000 | [diff] [blame] | 150 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 151 | static void RegisterHandlers() { |
Bruno Cardoso Lopes | 1856cee | 2017-03-27 18:21:31 +0000 | [diff] [blame] | 152 | sys::SmartScopedLock<true> Guard(*SignalsMutex); |
Mehdi Amini | 766d05b | 2015-11-05 02:29:53 +0000 | [diff] [blame] | 153 | |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 154 | // If the handlers are already registered, we're done. |
| 155 | if (NumRegisteredSignals != 0) return; |
| 156 | |
Richard Smith | e881134 | 2016-05-20 21:07:41 +0000 | [diff] [blame] | 157 | // Create an alternate stack for signal handling. This is necessary for us to |
| 158 | // be able to reliably handle signals due to stack overflow. |
| 159 | CreateSigAltStack(); |
| 160 | |
Chris Bieneman | b1cd51e | 2014-08-29 01:05:16 +0000 | [diff] [blame] | 161 | for (auto S : IntSigs) RegisterHandler(S); |
| 162 | for (auto S : KillSigs) RegisterHandler(S); |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 165 | static void UnregisterHandlers() { |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 166 | // Restore all of the signal handlers to how they were before we showed up. |
| 167 | for (unsigned i = 0, e = NumRegisteredSignals; i != e; ++i) |
Chris Lattner | f2b6065 | 2009-03-23 06:46:20 +0000 | [diff] [blame] | 168 | sigaction(RegisteredSignalInfo[i].SigNo, |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 169 | &RegisteredSignalInfo[i].SA, nullptr); |
Chris Lattner | fb95472 | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 170 | NumRegisteredSignals = 0; |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 173 | |
Dan Gohman | 288999b | 2010-05-27 23:11:55 +0000 | [diff] [blame] | 174 | /// RemoveFilesToRemove - Process the FilesToRemove list. This function |
| 175 | /// should be called with the SignalsMutex lock held. |
Chandler Carruth | e6196eb | 2012-06-16 00:09:41 +0000 | [diff] [blame] | 176 | /// NB: This must be an async signal safe function. It cannot allocate or free |
| 177 | /// memory, even in debug builds. |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 178 | static void RemoveFilesToRemove() { |
Steven Wu | 9474669 | 2015-05-07 16:20:51 +0000 | [diff] [blame] | 179 | // Avoid constructing ManagedStatic in the signal handler. |
| 180 | // If FilesToRemove is not constructed, there are no files to remove. |
| 181 | if (!FilesToRemove.isConstructed()) |
| 182 | return; |
| 183 | |
Daniel Dunbar | 511479d | 2012-10-17 16:30:54 +0000 | [diff] [blame] | 184 | // We avoid iterators in case of debug iterators that allocate or release |
Chandler Carruth | e6196eb | 2012-06-16 00:09:41 +0000 | [diff] [blame] | 185 | // memory. |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 186 | std::vector<std::string>& FilesToRemoveRef = *FilesToRemove; |
| 187 | for (unsigned i = 0, e = FilesToRemoveRef.size(); i != e; ++i) { |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 188 | const char *path = FilesToRemoveRef[i].c_str(); |
Daniel Dunbar | 511479d | 2012-10-17 16:30:54 +0000 | [diff] [blame] | 189 | |
| 190 | // Get the status so we can determine if it's a file or directory. If we |
| 191 | // can't stat the file, ignore it. |
| 192 | struct stat buf; |
| 193 | if (stat(path, &buf) != 0) |
| 194 | continue; |
| 195 | |
| 196 | // If this is not a regular file, ignore it. We want to prevent removal of |
| 197 | // special files like /dev/null, even if the compiler is being run with the |
| 198 | // super-user permissions. |
| 199 | if (!S_ISREG(buf.st_mode)) |
| 200 | continue; |
Mehdi Amini | 766d05b | 2015-11-05 02:29:53 +0000 | [diff] [blame] | 201 | |
Daniel Dunbar | 511479d | 2012-10-17 16:30:54 +0000 | [diff] [blame] | 202 | // Otherwise, remove the file. We ignore any errors here as there is nothing |
| 203 | // else we can do. |
| 204 | unlink(path); |
Dan Gohman | 288999b | 2010-05-27 23:11:55 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
Chris Lattner | 6acb4d6 | 2009-03-07 08:15:47 +0000 | [diff] [blame] | 207 | |
| 208 | // SignalHandler - The signal handler that runs. |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 209 | static RETSIGTYPE SignalHandler(int Sig) { |
Chris Lattner | bbbbbf3 | 2009-03-05 18:22:14 +0000 | [diff] [blame] | 210 | // Restore the signal behavior to default, so that the program actually |
| 211 | // crashes when we return and the signal reissues. This also ensures that if |
| 212 | // we crash in our signal handler that the program will terminate immediately |
| 213 | // instead of recursing in the signal handler. |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 214 | UnregisterHandlers(); |
Chris Lattner | 6acb4d6 | 2009-03-07 08:15:47 +0000 | [diff] [blame] | 215 | |
| 216 | // Unmask all potentially blocked kill signals. |
| 217 | sigset_t SigMask; |
| 218 | sigfillset(&SigMask); |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 219 | sigprocmask(SIG_UNBLOCK, &SigMask, nullptr); |
Chris Lattner | bbbbbf3 | 2009-03-05 18:22:14 +0000 | [diff] [blame] | 220 | |
Dylan Noblesmith | c4c5180 | 2014-08-23 23:07:14 +0000 | [diff] [blame] | 221 | { |
Kristof Beyls | 7adf8c5 | 2017-03-31 14:58:52 +0000 | [diff] [blame] | 222 | unique_lock<sys::SmartMutex<true>> Guard(*SignalsMutex); |
Dylan Noblesmith | c4c5180 | 2014-08-23 23:07:14 +0000 | [diff] [blame] | 223 | RemoveFilesToRemove(); |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 224 | |
Chris Bieneman | b1cd51e | 2014-08-29 01:05:16 +0000 | [diff] [blame] | 225 | if (std::find(std::begin(IntSigs), std::end(IntSigs), Sig) |
| 226 | != std::end(IntSigs)) { |
Dylan Noblesmith | c4c5180 | 2014-08-23 23:07:14 +0000 | [diff] [blame] | 227 | if (InterruptFunction) { |
| 228 | void (*IF)() = InterruptFunction; |
| 229 | Guard.unlock(); |
| 230 | InterruptFunction = nullptr; |
| 231 | IF(); // run the interrupt function. |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | Guard.unlock(); |
| 236 | raise(Sig); // Execute the default handler. |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 237 | return; |
Dylan Noblesmith | c4c5180 | 2014-08-23 23:07:14 +0000 | [diff] [blame] | 238 | } |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Otherwise if it is a fault (like SEGV) run any handler. |
Yaron Keren | 2873810 | 2015-07-22 21:11:17 +0000 | [diff] [blame] | 242 | llvm::sys::RunSignalHandlers(); |
Ulrich Weigand | 90c9abd | 2013-05-03 12:22:11 +0000 | [diff] [blame] | 243 | |
| 244 | #ifdef __s390__ |
| 245 | // On S/390, certain signals are delivered with PSW Address pointing to |
| 246 | // *after* the faulting instruction. Simply returning from the signal |
| 247 | // handler would continue execution after that point, instead of |
| 248 | // re-raising the signal. Raise the signal manually in those cases. |
| 249 | if (Sig == SIGILL || Sig == SIGFPE || Sig == SIGTRAP) |
| 250 | raise(Sig); |
| 251 | #endif |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Daniel Dunbar | 6827256 | 2010-05-08 02:10:34 +0000 | [diff] [blame] | 254 | void llvm::sys::RunInterruptHandlers() { |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 255 | sys::SmartScopedLock<true> Guard(*SignalsMutex); |
Dan Gohman | 288999b | 2010-05-27 23:11:55 +0000 | [diff] [blame] | 256 | RemoveFilesToRemove(); |
Daniel Dunbar | 6827256 | 2010-05-08 02:10:34 +0000 | [diff] [blame] | 257 | } |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 258 | |
Chris Lattner | bb1ba7b | 2009-03-08 19:13:45 +0000 | [diff] [blame] | 259 | void llvm::sys::SetInterruptFunction(void (*IF)()) { |
Dylan Noblesmith | 4704ffe | 2014-08-23 22:49:17 +0000 | [diff] [blame] | 260 | { |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 261 | sys::SmartScopedLock<true> Guard(*SignalsMutex); |
Dylan Noblesmith | 4704ffe | 2014-08-23 22:49:17 +0000 | [diff] [blame] | 262 | InterruptFunction = IF; |
| 263 | } |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 264 | RegisterHandlers(); |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // RemoveFileOnSignal - The public API |
Rafael Espindola | 4f35da7 | 2013-06-13 21:16:58 +0000 | [diff] [blame] | 268 | bool llvm::sys::RemoveFileOnSignal(StringRef Filename, |
Chris Lattner | bb1ba7b | 2009-03-08 19:13:45 +0000 | [diff] [blame] | 269 | std::string* ErrMsg) { |
Dylan Noblesmith | 4704ffe | 2014-08-23 22:49:17 +0000 | [diff] [blame] | 270 | { |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 271 | sys::SmartScopedLock<true> Guard(*SignalsMutex); |
Yaron Keren | 4135e4c | 2015-07-23 05:49:29 +0000 | [diff] [blame] | 272 | FilesToRemove->push_back(Filename); |
Dylan Noblesmith | 4704ffe | 2014-08-23 22:49:17 +0000 | [diff] [blame] | 273 | } |
Owen Anderson | 820739d | 2009-08-17 17:07:22 +0000 | [diff] [blame] | 274 | |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 275 | RegisterHandlers(); |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 276 | return false; |
| 277 | } |
| 278 | |
Dan Gohman | e201c07 | 2010-09-01 14:17:34 +0000 | [diff] [blame] | 279 | // DontRemoveFileOnSignal - The public API |
Rafael Espindola | 4f35da7 | 2013-06-13 21:16:58 +0000 | [diff] [blame] | 280 | void llvm::sys::DontRemoveFileOnSignal(StringRef Filename) { |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 281 | sys::SmartScopedLock<true> Guard(*SignalsMutex); |
Chandler Carruth | e6196eb | 2012-06-16 00:09:41 +0000 | [diff] [blame] | 282 | std::vector<std::string>::reverse_iterator RI = |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 283 | find(reverse(*FilesToRemove), Filename); |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 284 | std::vector<std::string>::iterator I = FilesToRemove->end(); |
| 285 | if (RI != FilesToRemove->rend()) |
| 286 | I = FilesToRemove->erase(RI.base()-1); |
Dan Gohman | e201c07 | 2010-09-01 14:17:34 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 289 | /// AddSignalHandler - Add a function to be called when a signal is delivered |
| 290 | /// to the process. The handler can have a cookie passed to it to identify |
| 291 | /// what instance of the handler it is. |
Chris Lattner | bb1ba7b | 2009-03-08 19:13:45 +0000 | [diff] [blame] | 292 | void llvm::sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) { |
Chris Bieneman | 186e7d1 | 2014-09-02 23:48:13 +0000 | [diff] [blame] | 293 | CallBacksToRun->push_back(std::make_pair(FnPtr, Cookie)); |
Chris Lattner | f299a68 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 294 | RegisterHandlers(); |
Chris Lattner | 4fdd042 | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Joerg Sonnenberger | 0e3cc3c | 2016-09-30 20:04:24 +0000 | [diff] [blame] | 297 | #if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES && HAVE_LINK_H && \ |
Reid Kleckner | 40aa9c6 | 2015-11-09 23:10:29 +0000 | [diff] [blame] | 298 | (defined(__linux__) || defined(__FreeBSD__) || \ |
| 299 | defined(__FreeBSD_kernel__) || defined(__NetBSD__)) |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 300 | struct DlIteratePhdrData { |
| 301 | void **StackTrace; |
| 302 | int depth; |
| 303 | bool first; |
| 304 | const char **modules; |
| 305 | intptr_t *offsets; |
| 306 | const char *main_exec_name; |
| 307 | }; |
| 308 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 309 | static int dl_iterate_phdr_cb(dl_phdr_info *info, size_t size, void *arg) { |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 310 | DlIteratePhdrData *data = (DlIteratePhdrData*)arg; |
| 311 | const char *name = data->first ? data->main_exec_name : info->dlpi_name; |
| 312 | data->first = false; |
| 313 | for (int i = 0; i < info->dlpi_phnum; i++) { |
| 314 | const auto *phdr = &info->dlpi_phdr[i]; |
| 315 | if (phdr->p_type != PT_LOAD) |
| 316 | continue; |
| 317 | intptr_t beg = info->dlpi_addr + phdr->p_vaddr; |
| 318 | intptr_t end = beg + phdr->p_memsz; |
| 319 | for (int j = 0; j < data->depth; j++) { |
| 320 | if (data->modules[j]) |
| 321 | continue; |
| 322 | intptr_t addr = (intptr_t)data->StackTrace[j]; |
| 323 | if (beg <= addr && addr < end) { |
| 324 | data->modules[j] = name; |
| 325 | data->offsets[j] = addr - info->dlpi_addr; |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | return 0; |
| 330 | } |
| 331 | |
Reid Kleckner | 40aa9c6 | 2015-11-09 23:10:29 +0000 | [diff] [blame] | 332 | /// If this is an ELF platform, we can find all loaded modules and their virtual |
| 333 | /// addresses with dl_iterate_phdr. |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 334 | static bool findModulesAndOffsets(void **StackTrace, int Depth, |
| 335 | const char **Modules, intptr_t *Offsets, |
Reid Kleckner | ba5757d | 2015-11-05 01:07:54 +0000 | [diff] [blame] | 336 | const char *MainExecutableName, |
| 337 | StringSaver &StrPool) { |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 338 | DlIteratePhdrData data = {StackTrace, Depth, true, |
| 339 | Modules, Offsets, MainExecutableName}; |
| 340 | dl_iterate_phdr(dl_iterate_phdr_cb, &data); |
| 341 | return true; |
| 342 | } |
| 343 | #else |
Reid Kleckner | 40aa9c6 | 2015-11-09 23:10:29 +0000 | [diff] [blame] | 344 | /// This platform does not have dl_iterate_phdr, so we do not yet know how to |
| 345 | /// find all loaded DSOs. |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 346 | static bool findModulesAndOffsets(void **StackTrace, int Depth, |
| 347 | const char **Modules, intptr_t *Offsets, |
Mehdi Amini | 7ae928e | 2015-11-05 02:29:57 +0000 | [diff] [blame] | 348 | const char *MainExecutableName, |
| 349 | StringSaver &StrPool) { |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 350 | return false; |
| 351 | } |
Joerg Sonnenberger | 0e3cc3c | 2016-09-30 20:04:24 +0000 | [diff] [blame] | 352 | #endif // defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES && ... |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 353 | |
Joerg Sonnenberger | 0e3cc3c | 2016-09-30 20:04:24 +0000 | [diff] [blame] | 354 | #if ENABLE_BACKTRACES && defined(HAVE__UNWIND_BACKTRACE) |
Richard Smith | 14d9651 | 2016-05-20 21:18:12 +0000 | [diff] [blame] | 355 | static int unwindBacktrace(void **StackTrace, int MaxEntries) { |
| 356 | if (MaxEntries < 0) |
| 357 | return 0; |
| 358 | |
| 359 | // Skip the first frame ('unwindBacktrace' itself). |
| 360 | int Entries = -1; |
| 361 | |
| 362 | auto HandleFrame = [&](_Unwind_Context *Context) -> _Unwind_Reason_Code { |
| 363 | // Apparently we need to detect reaching the end of the stack ourselves. |
| 364 | void *IP = (void *)_Unwind_GetIP(Context); |
| 365 | if (!IP) |
| 366 | return _URC_END_OF_STACK; |
| 367 | |
| 368 | assert(Entries < MaxEntries && "recursively called after END_OF_STACK?"); |
| 369 | if (Entries >= 0) |
| 370 | StackTrace[Entries] = IP; |
| 371 | |
| 372 | if (++Entries == MaxEntries) |
| 373 | return _URC_END_OF_STACK; |
| 374 | return _URC_NO_REASON; |
| 375 | }; |
| 376 | |
| 377 | _Unwind_Backtrace( |
| 378 | [](_Unwind_Context *Context, void *Handler) { |
| 379 | return (*static_cast<decltype(HandleFrame) *>(Handler))(Context); |
| 380 | }, |
| 381 | static_cast<void *>(&HandleFrame)); |
| 382 | return std::max(Entries, 0); |
| 383 | } |
| 384 | #endif |
| 385 | |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 386 | // PrintStackTrace - In the case of a program crash or fault, print out a stack |
| 387 | // trace so that the user has an indication of why and where we died. |
| 388 | // |
| 389 | // 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] | 390 | // doesn't demangle symbols. |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 391 | void llvm::sys::PrintStackTrace(raw_ostream &OS) { |
Joerg Sonnenberger | 0e3cc3c | 2016-09-30 20:04:24 +0000 | [diff] [blame] | 392 | #if ENABLE_BACKTRACES |
Richard Smith | 14d9651 | 2016-05-20 21:18:12 +0000 | [diff] [blame] | 393 | static void *StackTrace[256]; |
| 394 | int depth = 0; |
| 395 | #if defined(HAVE_BACKTRACE) |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 396 | // Use backtrace() to output a backtrace on Linux systems with glibc. |
Richard Smith | 14d9651 | 2016-05-20 21:18:12 +0000 | [diff] [blame] | 397 | if (!depth) |
| 398 | depth = backtrace(StackTrace, static_cast<int>(array_lengthof(StackTrace))); |
| 399 | #endif |
Joerg Sonnenberger | 5c50539 | 2016-09-29 21:07:57 +0000 | [diff] [blame] | 400 | #if defined(HAVE__UNWIND_BACKTRACE) |
Richard Smith | 14d9651 | 2016-05-20 21:18:12 +0000 | [diff] [blame] | 401 | // Try _Unwind_Backtrace() if backtrace() failed. |
| 402 | if (!depth) |
| 403 | depth = unwindBacktrace(StackTrace, |
Evan Cheng | 86cb318 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 404 | static_cast<int>(array_lengthof(StackTrace))); |
Richard Smith | 14d9651 | 2016-05-20 21:18:12 +0000 | [diff] [blame] | 405 | #endif |
| 406 | if (!depth) |
| 407 | return; |
| 408 | |
Richard Smith | 2ad6d48 | 2016-06-09 00:53:21 +0000 | [diff] [blame] | 409 | if (printSymbolizedStackTrace(Argv0, StackTrace, depth, OS)) |
Alexey Samsonov | 8a584bb | 2014-10-10 22:06:59 +0000 | [diff] [blame] | 410 | return; |
Omair Javaid | f5d560bc84 | 2017-02-02 01:17:49 +0000 | [diff] [blame] | 411 | #if HAVE_DLFCN_H && HAVE_DLADDR |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 412 | int width = 0; |
| 413 | for (int i = 0; i < depth; ++i) { |
| 414 | Dl_info dlinfo; |
| 415 | dladdr(StackTrace[i], &dlinfo); |
Dan Gohman | 1f517dd | 2009-02-10 17:56:28 +0000 | [diff] [blame] | 416 | const char* name = strrchr(dlinfo.dli_fname, '/'); |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 417 | |
| 418 | int nwidth; |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 419 | if (!name) nwidth = strlen(dlinfo.dli_fname); |
| 420 | else nwidth = strlen(name) - 1; |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 421 | |
| 422 | if (nwidth > width) width = nwidth; |
| 423 | } |
| 424 | |
| 425 | for (int i = 0; i < depth; ++i) { |
| 426 | Dl_info dlinfo; |
| 427 | dladdr(StackTrace[i], &dlinfo); |
| 428 | |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 429 | OS << format("%-2d", i); |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 430 | |
Dan Gohman | 1f517dd | 2009-02-10 17:56:28 +0000 | [diff] [blame] | 431 | const char* name = strrchr(dlinfo.dli_fname, '/'); |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 432 | if (!name) OS << format(" %-*s", width, dlinfo.dli_fname); |
| 433 | else OS << format(" %-*s", width, name+1); |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 434 | |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 435 | OS << format(" %#0*lx", (int)(sizeof(void*) * 2) + 2, |
| 436 | (unsigned long)StackTrace[i]); |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 437 | |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 438 | if (dlinfo.dli_sname != nullptr) { |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 439 | OS << ' '; |
Benjamin Kramer | 83e2a44 | 2013-04-28 07:47:04 +0000 | [diff] [blame] | 440 | int res; |
Rafael Espindola | b940b66 | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 441 | char* d = itaniumDemangle(dlinfo.dli_sname, nullptr, nullptr, &res); |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 442 | if (!d) OS << dlinfo.dli_sname; |
| 443 | else OS << d; |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 444 | free(d); |
| 445 | |
Edwin Vane | 44338e0 | 2013-01-28 19:34:42 +0000 | [diff] [blame] | 446 | // FIXME: When we move to C++11, use %t length modifier. It's not in |
| 447 | // C++03 and causes gcc to issue warnings. Losing the upper 32 bits of |
| 448 | // 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] | 449 | OS << format(" + %u",(unsigned)((char*)StackTrace[i]- |
| 450 | (char*)dlinfo.dli_saddr)); |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 451 | } |
Zachary Turner | cd132c9 | 2015-03-05 19:10:52 +0000 | [diff] [blame] | 452 | OS << '\n'; |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 453 | } |
Richard Smith | 14d9651 | 2016-05-20 21:18:12 +0000 | [diff] [blame] | 454 | #elif defined(HAVE_BACKTRACE) |
Lauro Ramos Venancio | eab51d3 | 2008-02-15 18:05:54 +0000 | [diff] [blame] | 455 | backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO); |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 456 | #endif |
Dan Gohman | 7f079aa | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 457 | #endif |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 460 | static void PrintStackTraceSignalHandler(void *) { |
Kristof Beyls | 7adf8c5 | 2017-03-31 14:58:52 +0000 | [diff] [blame] | 461 | sys::PrintStackTrace(llvm::errs()); |
Argyrios Kyrtzidis | eb9ae76 | 2013-01-09 19:42:40 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Michael J. Spencer | 89b0ad2 | 2015-01-29 17:20:29 +0000 | [diff] [blame] | 464 | void llvm::sys::DisableSystemDialogsOnCrash() {} |
| 465 | |
NAKAMURA Takumi | 8a54d81 | 2012-09-06 03:01:43 +0000 | [diff] [blame] | 466 | /// PrintStackTraceOnErrorSignal - When an error signal (such as SIGABRT or |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 467 | /// SIGSEGV) is delivered to the process, print a stack trace and then exit. |
Richard Smith | 2ad6d48 | 2016-06-09 00:53:21 +0000 | [diff] [blame] | 468 | void llvm::sys::PrintStackTraceOnErrorSignal(StringRef Argv0, |
| 469 | bool DisableCrashReporting) { |
| 470 | ::Argv0 = Argv0; |
| 471 | |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 472 | AddSignalHandler(PrintStackTraceSignalHandler, nullptr); |
Argyrios Kyrtzidis | cd8fe08 | 2012-01-11 20:53:25 +0000 | [diff] [blame] | 473 | |
Joerg Sonnenberger | 2cd87a0 | 2016-09-30 20:06:19 +0000 | [diff] [blame] | 474 | #if defined(__APPLE__) && ENABLE_CRASH_OVERRIDES |
Argyrios Kyrtzidis | cd8fe08 | 2012-01-11 20:53:25 +0000 | [diff] [blame] | 475 | // Environment variable to disable any kind of crash dialog. |
Pete Cooper | 6bea2f4 | 2015-04-07 20:43:23 +0000 | [diff] [blame] | 476 | if (DisableCrashReporting || getenv("LLVM_DISABLE_CRASH_REPORT")) { |
Argyrios Kyrtzidis | cd8fe08 | 2012-01-11 20:53:25 +0000 | [diff] [blame] | 477 | mach_port_t self = mach_task_self(); |
| 478 | |
| 479 | exception_mask_t mask = EXC_MASK_CRASH; |
| 480 | |
NAKAMURA Takumi | ffa1571 | 2012-09-06 03:02:56 +0000 | [diff] [blame] | 481 | kern_return_t ret = task_set_exception_ports(self, |
Argyrios Kyrtzidis | cd8fe08 | 2012-01-11 20:53:25 +0000 | [diff] [blame] | 482 | mask, |
Jean-Daniel Dupas | a573b22 | 2012-03-24 22:17:50 +0000 | [diff] [blame] | 483 | MACH_PORT_NULL, |
NAKAMURA Takumi | ffa1571 | 2012-09-06 03:02:56 +0000 | [diff] [blame] | 484 | EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES, |
Jean-Daniel Dupas | a573b22 | 2012-03-24 22:17:50 +0000 | [diff] [blame] | 485 | THREAD_STATE_NONE); |
Argyrios Kyrtzidis | cd8fe08 | 2012-01-11 20:53:25 +0000 | [diff] [blame] | 486 | (void)ret; |
| 487 | } |
| 488 | #endif |
Reid Spencer | 3d7a614 | 2004-08-29 19:22:48 +0000 | [diff] [blame] | 489 | } |