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 | 661fc34 | 2009-03-23 05:42:29 +0000 | [diff] [blame^] | 60 | // Just call signal |
| 61 | static void RegisterHandler(int Signal) { |
| 62 | signal(Signal, SignalHandler); |
Chris Lattner | f74e6ef | 2009-03-07 08:15:47 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Chris Lattner | 661fc34 | 2009-03-23 05:42:29 +0000 | [diff] [blame^] | 65 | static void RegisterHandlers() { |
| 66 | std::for_each(IntSigs, IntSigsEnd, RegisterHandler); |
| 67 | std::for_each(KillSigs, KillSigsEnd, RegisterHandler); |
| 68 | } |
| 69 | |
| 70 | static void UnregisterHandler(int Signal) { |
| 71 | signal(Signal, SIG_DFL); |
| 72 | } |
| 73 | |
| 74 | static void UnregisterHandlers() { |
| 75 | std::for_each(KillSigs, KillSigsEnd, UnregisterHandler); |
| 76 | std::for_each(IntSigs, IntSigsEnd, UnregisterHandler); |
| 77 | } |
| 78 | |
| 79 | |
Chris Lattner | f74e6ef | 2009-03-07 08:15:47 +0000 | [diff] [blame] | 80 | |
| 81 | // SignalHandler - The signal handler that runs. |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 82 | static RETSIGTYPE SignalHandler(int Sig) { |
Chris Lattner | 52a5f39 | 2009-03-05 18:22:14 +0000 | [diff] [blame] | 83 | // Restore the signal behavior to default, so that the program actually |
| 84 | // crashes when we return and the signal reissues. This also ensures that if |
| 85 | // we crash in our signal handler that the program will terminate immediately |
| 86 | // instead of recursing in the signal handler. |
Chris Lattner | 661fc34 | 2009-03-23 05:42:29 +0000 | [diff] [blame^] | 87 | UnregisterHandlers(); |
Chris Lattner | f74e6ef | 2009-03-07 08:15:47 +0000 | [diff] [blame] | 88 | |
| 89 | // Unmask all potentially blocked kill signals. |
| 90 | sigset_t SigMask; |
| 91 | sigfillset(&SigMask); |
| 92 | sigprocmask(SIG_UNBLOCK, &SigMask, 0); |
Chris Lattner | 52a5f39 | 2009-03-05 18:22:14 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 94 | if (FilesToRemove != 0) |
| 95 | while (!FilesToRemove->empty()) { |
| 96 | FilesToRemove->back().eraseFromDisk(true); |
| 97 | FilesToRemove->pop_back(); |
| 98 | } |
| 99 | |
| 100 | if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd) { |
| 101 | if (InterruptFunction) { |
| 102 | void (*IF)() = InterruptFunction; |
| 103 | InterruptFunction = 0; |
| 104 | IF(); // run the interrupt function. |
| 105 | return; |
| 106 | } |
| 107 | exit(1); // If this is an interrupt signal, exit the program |
| 108 | } |
| 109 | |
| 110 | // Otherwise if it is a fault (like SEGV) run any handler. |
| 111 | if (CallBacksToRun) |
| 112 | for (unsigned i = 0, e = CallBacksToRun->size(); i != e; ++i) |
| 113 | (*CallBacksToRun)[i].first((*CallBacksToRun)[i].second); |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 116 | |
| 117 | |
Chris Lattner | afea1b7 | 2009-03-08 19:13:45 +0000 | [diff] [blame] | 118 | void llvm::sys::SetInterruptFunction(void (*IF)()) { |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 119 | InterruptFunction = IF; |
Chris Lattner | 661fc34 | 2009-03-23 05:42:29 +0000 | [diff] [blame^] | 120 | RegisterHandlers(); |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // RemoveFileOnSignal - The public API |
Chris Lattner | afea1b7 | 2009-03-08 19:13:45 +0000 | [diff] [blame] | 124 | bool llvm::sys::RemoveFileOnSignal(const sys::Path &Filename, |
| 125 | std::string* ErrMsg) { |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 126 | if (FilesToRemove == 0) |
| 127 | FilesToRemove = new std::vector<sys::Path>(); |
| 128 | |
| 129 | FilesToRemove->push_back(Filename); |
| 130 | |
Chris Lattner | 661fc34 | 2009-03-23 05:42:29 +0000 | [diff] [blame^] | 131 | RegisterHandlers(); |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 132 | return false; |
| 133 | } |
| 134 | |
| 135 | /// AddSignalHandler - Add a function to be called when a signal is delivered |
| 136 | /// to the process. The handler can have a cookie passed to it to identify |
| 137 | /// what instance of the handler it is. |
Chris Lattner | afea1b7 | 2009-03-08 19:13:45 +0000 | [diff] [blame] | 138 | void llvm::sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) { |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 139 | if (CallBacksToRun == 0) |
| 140 | CallBacksToRun = new std::vector<std::pair<void(*)(void*), void*> >(); |
| 141 | CallBacksToRun->push_back(std::make_pair(FnPtr, Cookie)); |
Chris Lattner | 661fc34 | 2009-03-23 05:42:29 +0000 | [diff] [blame^] | 142 | RegisterHandlers(); |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 145 | |
| 146 | // PrintStackTrace - In the case of a program crash or fault, print out a stack |
| 147 | // trace so that the user has an indication of why and where we died. |
| 148 | // |
| 149 | // 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] | 150 | // doesn't demangle symbols. |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 151 | static void PrintStackTrace(void *) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 152 | #ifdef HAVE_BACKTRACE |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 153 | static void* StackTrace[256]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 154 | // Use backtrace() to output a backtrace on Linux systems with glibc. |
Evan Cheng | 591bfc8 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 155 | int depth = backtrace(StackTrace, |
| 156 | static_cast<int>(array_lengthof(StackTrace))); |
Dan Gohman | 3d31323 | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 157 | #if HAVE_DLFCN_H && __GNUG__ |
| 158 | int width = 0; |
| 159 | for (int i = 0; i < depth; ++i) { |
| 160 | Dl_info dlinfo; |
| 161 | dladdr(StackTrace[i], &dlinfo); |
Dan Gohman | 2f75d52 | 2009-02-10 17:56:28 +0000 | [diff] [blame] | 162 | const char* name = strrchr(dlinfo.dli_fname, '/'); |
Dan Gohman | 3d31323 | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 163 | |
| 164 | int nwidth; |
| 165 | if (name == NULL) nwidth = strlen(dlinfo.dli_fname); |
| 166 | else nwidth = strlen(name) - 1; |
| 167 | |
| 168 | if (nwidth > width) width = nwidth; |
| 169 | } |
| 170 | |
| 171 | for (int i = 0; i < depth; ++i) { |
| 172 | Dl_info dlinfo; |
| 173 | dladdr(StackTrace[i], &dlinfo); |
| 174 | |
| 175 | fprintf(stderr, "%-3d", i); |
| 176 | |
Dan Gohman | 2f75d52 | 2009-02-10 17:56:28 +0000 | [diff] [blame] | 177 | const char* name = strrchr(dlinfo.dli_fname, '/'); |
Dan Gohman | 3d31323 | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 178 | if (name == NULL) fprintf(stderr, " %-*s", width, dlinfo.dli_fname); |
| 179 | else fprintf(stderr, " %-*s", width, name+1); |
| 180 | |
Dan Gohman | 0a48f14 | 2008-12-05 23:39:24 +0000 | [diff] [blame] | 181 | fprintf(stderr, " %#0*lx", |
| 182 | (int)(sizeof(void*) * 2) + 2, (unsigned long)StackTrace[i]); |
Dan Gohman | 3d31323 | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 183 | |
| 184 | if (dlinfo.dli_sname != NULL) { |
| 185 | int res; |
| 186 | fputc(' ', stderr); |
| 187 | char* d = abi::__cxa_demangle(dlinfo.dli_sname, NULL, NULL, &res); |
| 188 | if (d == NULL) fputs(dlinfo.dli_sname, stderr); |
| 189 | else fputs(d, stderr); |
| 190 | free(d); |
| 191 | |
| 192 | fprintf(stderr, " + %tu",(char*)StackTrace[i]-(char*)dlinfo.dli_saddr); |
| 193 | } |
| 194 | fputc('\n', stderr); |
| 195 | } |
| 196 | #else |
Lauro Ramos Venancio | 538db76 | 2008-02-15 18:05:54 +0000 | [diff] [blame] | 197 | backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 198 | #endif |
Dan Gohman | 3d31323 | 2008-12-05 20:12:48 +0000 | [diff] [blame] | 199 | #endif |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 202 | /// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or |
| 203 | /// 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] | 204 | void llvm::sys::PrintStackTraceOnErrorSignal() { |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 205 | AddSignalHandler(PrintStackTrace, 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 206 | } |
Chris Lattner | 199997b | 2009-03-04 21:21:36 +0000 | [diff] [blame] | 207 | |