Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- Signals.cpp - Generic Unix Signals Implementation -----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 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. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines some helpful functions for dealing with the possibility of |
| 11 | // Unix signals occuring while your program is running. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "Unix.h" |
Owen Anderson | 1636de9 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 17 | #include <vector> |
| 18 | #include <algorithm> |
| 19 | #if HAVE_EXECINFO_H |
| 20 | # include <execinfo.h> // For backtrace(). |
| 21 | #endif |
| 22 | #if HAVE_SIGNAL_H |
| 23 | #include <signal.h> |
| 24 | #endif |
| 25 | #if HAVE_SYS_STAT_H |
| 26 | #include <sys/stat.h> |
| 27 | #endif |
Dan Gohman | 3d31323 | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 28 | #if HAVE_DLFCN_H && __GNUG__ |
| 29 | #include <dlfcn.h> |
| 30 | #include <cxxabi.h> |
| 31 | #endif |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
Chris Lattner | 661fc34 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 34 | static RETSIGTYPE SignalHandler(int Sig); // defined below. |
| 35 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 36 | /// InterruptFunction - The function to call if ctrl-c is pressed. |
Dan Gohman | ffc2f03 | 2008-04-10 21:11:47 +0000 | [diff] [blame] | 37 | static void (*InterruptFunction)() = 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 39 | static std::vector<sys::Path> *FilesToRemove = 0; |
| 40 | static std::vector<std::pair<void(*)(void*), void*> > *CallBacksToRun = 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 41 | |
| 42 | // IntSigs - Signals that may interrupt the program at any time. |
Dan Gohman | ffc2f03 | 2008-04-10 21:11:47 +0000 | [diff] [blame] | 43 | static const int IntSigs[] = { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 44 | SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2 |
| 45 | }; |
Dan Gohman | c336a13 | 2008-05-14 00:42:30 +0000 | [diff] [blame] | 46 | static const int *const IntSigsEnd = |
| 47 | IntSigs + sizeof(IntSigs) / sizeof(IntSigs[0]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 48 | |
| 49 | // KillSigs - Signals that are synchronous with the program that will cause it |
| 50 | // to die. |
Dan Gohman | ffc2f03 | 2008-04-10 21:11:47 +0000 | [diff] [blame] | 51 | static const int KillSigs[] = { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 52 | SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ |
| 53 | #ifdef SIGEMT |
| 54 | , SIGEMT |
| 55 | #endif |
| 56 | }; |
Dan Gohman | c336a13 | 2008-05-14 00:42:30 +0000 | [diff] [blame] | 57 | static const int *const KillSigsEnd = |
| 58 | KillSigs + sizeof(KillSigs) / sizeof(KillSigs[0]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 48c6b8d | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 60 | static unsigned NumRegisteredSignals = 0; |
| 61 | static struct { |
| 62 | struct sigaction SA; |
| 63 | int SigNo; |
| 64 | } RegisteredSignalInfo[(sizeof(IntSigs)+sizeof(KillSigs))/sizeof(KillSigs[0])]; |
| 65 | |
| 66 | |
Chris Lattner | 661fc34 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 67 | static void RegisterHandler(int Signal) { |
Chris Lattner | 48c6b8d | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 68 | assert(NumRegisteredSignals < |
| 69 | sizeof(RegisteredSignalInfo)/sizeof(RegisteredSignalInfo[0]) && |
| 70 | "Out of space for signal handlers!"); |
| 71 | |
| 72 | struct sigaction NewHandler; |
| 73 | |
| 74 | NewHandler.sa_handler = SignalHandler; |
| 75 | NewHandler.sa_flags = SA_NODEFER|SA_RESETHAND; |
| 76 | sigemptyset(&NewHandler.sa_mask); |
| 77 | |
| 78 | // Install the new handler, save the old one in RegisteredSignalInfo. |
| 79 | sigaction(Signal, &NewHandler, |
| 80 | &RegisteredSignalInfo[NumRegisteredSignals].SA); |
| 81 | RegisteredSignalInfo[NumRegisteredSignals].SigNo = Signal; |
| 82 | ++NumRegisteredSignals; |
Chris Lattner | f74e6ef | 2009-03-07 08:15:47 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Chris Lattner | 661fc34 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 85 | static void RegisterHandlers() { |
Chris Lattner | 48c6b8d | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 86 | // If the handlers are already registered, we're done. |
| 87 | if (NumRegisteredSignals != 0) return; |
| 88 | |
Chris Lattner | 661fc34 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 89 | std::for_each(IntSigs, IntSigsEnd, RegisterHandler); |
| 90 | std::for_each(KillSigs, KillSigsEnd, RegisterHandler); |
| 91 | } |
| 92 | |
Chris Lattner | 661fc34 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 93 | static void UnregisterHandlers() { |
Chris Lattner | 48c6b8d | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 94 | // Restore all of the signal handlers to how they were before we showed up. |
| 95 | for (unsigned i = 0, e = NumRegisteredSignals; i != e; ++i) |
Chris Lattner | 9d5b4d9 | 2009-03-23 06:46:20 +0000 | [diff] [blame] | 96 | sigaction(RegisteredSignalInfo[i].SigNo, |
| 97 | &RegisteredSignalInfo[i].SA, 0); |
Chris Lattner | 48c6b8d | 2009-03-23 05:55:36 +0000 | [diff] [blame] | 98 | NumRegisteredSignals = 0; |
Chris Lattner | 661fc34 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | |
Chris Lattner | f74e6ef | 2009-03-07 08:15:47 +0000 | [diff] [blame] | 102 | |
| 103 | // SignalHandler - The signal handler that runs. |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 104 | static RETSIGTYPE SignalHandler(int Sig) { |
Chris Lattner | 52a5f39 | 2009-03-05 18:22:14 +0000 | [diff] [blame] | 105 | // Restore the signal behavior to default, so that the program actually |
| 106 | // crashes when we return and the signal reissues. This also ensures that if |
| 107 | // we crash in our signal handler that the program will terminate immediately |
| 108 | // instead of recursing in the signal handler. |
Chris Lattner | 661fc34 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 109 | UnregisterHandlers(); |
Chris Lattner | f74e6ef | 2009-03-07 08:15:47 +0000 | [diff] [blame] | 110 | |
| 111 | // Unmask all potentially blocked kill signals. |
| 112 | sigset_t SigMask; |
| 113 | sigfillset(&SigMask); |
| 114 | sigprocmask(SIG_UNBLOCK, &SigMask, 0); |
Chris Lattner | 52a5f39 | 2009-03-05 18:22:14 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 116 | if (FilesToRemove != 0) |
| 117 | while (!FilesToRemove->empty()) { |
| 118 | FilesToRemove->back().eraseFromDisk(true); |
| 119 | FilesToRemove->pop_back(); |
| 120 | } |
| 121 | |
| 122 | if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd) { |
| 123 | if (InterruptFunction) { |
| 124 | void (*IF)() = InterruptFunction; |
| 125 | InterruptFunction = 0; |
| 126 | IF(); // run the interrupt function. |
| 127 | return; |
| 128 | } |
Chris Lattner | 2f1b199 | 2009-04-12 23:33:13 +0000 | [diff] [blame^] | 129 | raise(Sig); // Execute the default handler. |
| 130 | return; |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // Otherwise if it is a fault (like SEGV) run any handler. |
| 134 | if (CallBacksToRun) |
| 135 | for (unsigned i = 0, e = CallBacksToRun->size(); i != e; ++i) |
| 136 | (*CallBacksToRun)[i].first((*CallBacksToRun)[i].second); |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 139 | |
| 140 | |
Chris Lattner | afea1b7 | 2009-03-08 19:13:45 +0000 | [diff] [blame] | 141 | void llvm::sys::SetInterruptFunction(void (*IF)()) { |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 142 | InterruptFunction = IF; |
Chris Lattner | 661fc34 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 143 | RegisterHandlers(); |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // RemoveFileOnSignal - The public API |
Chris Lattner | afea1b7 | 2009-03-08 19:13:45 +0000 | [diff] [blame] | 147 | bool llvm::sys::RemoveFileOnSignal(const sys::Path &Filename, |
| 148 | std::string* ErrMsg) { |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 149 | if (FilesToRemove == 0) |
| 150 | FilesToRemove = new std::vector<sys::Path>(); |
| 151 | |
| 152 | FilesToRemove->push_back(Filename); |
| 153 | |
Chris Lattner | 661fc34 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 154 | RegisterHandlers(); |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 155 | return false; |
| 156 | } |
| 157 | |
| 158 | /// AddSignalHandler - Add a function to be called when a signal is delivered |
| 159 | /// to the process. The handler can have a cookie passed to it to identify |
| 160 | /// what instance of the handler it is. |
Chris Lattner | afea1b7 | 2009-03-08 19:13:45 +0000 | [diff] [blame] | 161 | void llvm::sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) { |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 162 | if (CallBacksToRun == 0) |
| 163 | CallBacksToRun = new std::vector<std::pair<void(*)(void*), void*> >(); |
| 164 | CallBacksToRun->push_back(std::make_pair(FnPtr, Cookie)); |
Chris Lattner | 661fc34 | 2009-03-23 05:42:29 +0000 | [diff] [blame] | 165 | RegisterHandlers(); |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 168 | |
| 169 | // PrintStackTrace - In the case of a program crash or fault, print out a stack |
| 170 | // trace so that the user has an indication of why and where we died. |
| 171 | // |
| 172 | // On glibc systems we have the 'backtrace' function, which works nicely, but |
Lauro Ramos Venancio | 538db76 | 2008-02-15 18:05:54 +0000 | [diff] [blame] | 173 | // doesn't demangle symbols. |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 174 | static void PrintStackTrace(void *) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 175 | #ifdef HAVE_BACKTRACE |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 176 | static void* StackTrace[256]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 177 | // Use backtrace() to output a backtrace on Linux systems with glibc. |
Evan Cheng | 591bfc8 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 178 | int depth = backtrace(StackTrace, |
| 179 | static_cast<int>(array_lengthof(StackTrace))); |
Dan Gohman | 3d31323 | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 180 | #if HAVE_DLFCN_H && __GNUG__ |
| 181 | int width = 0; |
| 182 | for (int i = 0; i < depth; ++i) { |
| 183 | Dl_info dlinfo; |
| 184 | dladdr(StackTrace[i], &dlinfo); |
Dan Gohman | 2f75d52 | 2009-02-10 17:56:28 +0000 | [diff] [blame] | 185 | const char* name = strrchr(dlinfo.dli_fname, '/'); |
Dan Gohman | 3d31323 | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 186 | |
| 187 | int nwidth; |
| 188 | if (name == NULL) nwidth = strlen(dlinfo.dli_fname); |
| 189 | else nwidth = strlen(name) - 1; |
| 190 | |
| 191 | if (nwidth > width) width = nwidth; |
| 192 | } |
| 193 | |
| 194 | for (int i = 0; i < depth; ++i) { |
| 195 | Dl_info dlinfo; |
| 196 | dladdr(StackTrace[i], &dlinfo); |
| 197 | |
| 198 | fprintf(stderr, "%-3d", i); |
| 199 | |
Dan Gohman | 2f75d52 | 2009-02-10 17:56:28 +0000 | [diff] [blame] | 200 | const char* name = strrchr(dlinfo.dli_fname, '/'); |
Dan Gohman | 3d31323 | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 201 | if (name == NULL) fprintf(stderr, " %-*s", width, dlinfo.dli_fname); |
| 202 | else fprintf(stderr, " %-*s", width, name+1); |
| 203 | |
Dan Gohman | 0a48f14 | 2008-12-05 23:39:24 +0000 | [diff] [blame] | 204 | fprintf(stderr, " %#0*lx", |
| 205 | (int)(sizeof(void*) * 2) + 2, (unsigned long)StackTrace[i]); |
Dan Gohman | 3d31323 | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 206 | |
| 207 | if (dlinfo.dli_sname != NULL) { |
| 208 | int res; |
| 209 | fputc(' ', stderr); |
| 210 | char* d = abi::__cxa_demangle(dlinfo.dli_sname, NULL, NULL, &res); |
| 211 | if (d == NULL) fputs(dlinfo.dli_sname, stderr); |
| 212 | else fputs(d, stderr); |
| 213 | free(d); |
| 214 | |
| 215 | fprintf(stderr, " + %tu",(char*)StackTrace[i]-(char*)dlinfo.dli_saddr); |
| 216 | } |
| 217 | fputc('\n', stderr); |
| 218 | } |
| 219 | #else |
Lauro Ramos Venancio | 538db76 | 2008-02-15 18:05:54 +0000 | [diff] [blame] | 220 | backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 221 | #endif |
Dan Gohman | 3d31323 | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 222 | #endif |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 225 | /// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or |
| 226 | /// SIGSEGV) is delivered to the process, print a stack trace and then exit. |
Chris Lattner | afea1b7 | 2009-03-08 19:13:45 +0000 | [diff] [blame] | 227 | void llvm::sys::PrintStackTraceOnErrorSignal() { |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 228 | AddSignalHandler(PrintStackTrace, 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 229 | } |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 230 | |