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