1 //===- Signals.cpp - Generic Unix Signals Implementation -----*- C++ -*-===//
\r
3 // The LLVM Compiler Infrastructure
\r
5 // This file is distributed under the University of Illinois Open Source
\r
6 // License. See LICENSE.TXT for details.
\r
8 //===----------------------------------------------------------------------===//
\r
10 // This file defines some helpful functions for dealing with the possibility of
\r
11 // Unix signals occuring while your program is running.
\r
13 //===----------------------------------------------------------------------===//
\r
16 #include "llvm/ADT/STLExtras.h"
\r
17 #include "llvm/System/Mutex.h"
\r
19 #include <algorithm>
\r
21 # include <execinfo.h> // For backtrace().
\r
27 #include <sys/stat.h>
\r
29 #if HAVE_DLFCN_H && __GNUG__
\r
31 #include <cxxabi.h>
\r
33 using namespace llvm;
\r
35 static RETSIGTYPE SignalHandler(int Sig); // defined below.
\r
37 static SmartMutex<true> SignalsMutex;
\r
39 /// InterruptFunction - The function to call if ctrl-c is pressed.
\r
40 static void (*InterruptFunction)() = 0;
\r
42 static std::vector<sys::Path> FilesToRemove;
\r
43 static std::vector<std::pair<void(*)(void*), void*> > CallBacksToRun;
\r
45 // IntSigs - Signals that may interrupt the program at any time.
\r
46 static const int IntSigs[] = {
\r
47 SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
\r
49 static const int *const IntSigsEnd =
\r
50 IntSigs + sizeof(IntSigs) / sizeof(IntSigs[0]);
\r
52 // KillSigs - Signals that are synchronous with the program that will cause it
\r
54 static const int KillSigs[] = {
\r
55 SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV
\r
69 static const int *const KillSigsEnd =
\r
70 KillSigs + sizeof(KillSigs) / sizeof(KillSigs[0]);
\r
72 static unsigned NumRegisteredSignals = 0;
\r
74 struct sigaction SA;
\r
76 } RegisteredSignalInfo[(sizeof(IntSigs)+sizeof(KillSigs))/sizeof(KillSigs[0])];
\r
79 static void RegisterHandler(int Signal) {
\r
80 assert(NumRegisteredSignals <
\r
81 sizeof(RegisteredSignalInfo)/sizeof(RegisteredSignalInfo[0]) &&
\r
82 "Out of space for signal handlers!");
\r
84 struct sigaction NewHandler;
\r
86 NewHandler.sa_handler = SignalHandler;
\r
87 NewHandler.sa_flags = SA_NODEFER|SA_RESETHAND;
\r
88 sigemptyset(&NewHandler.sa_mask);
\r
90 // Install the new handler, save the old one in RegisteredSignalInfo.
\r
91 sigaction(Signal, &NewHandler,
\r
92 &RegisteredSignalInfo[NumRegisteredSignals].SA);
\r
93 RegisteredSignalInfo[NumRegisteredSignals].SigNo = Signal;
\r
94 ++NumRegisteredSignals;
\r
97 static void RegisterHandlers() {
\r
98 // If the handlers are already registered, we're done.
\r
99 if (NumRegisteredSignals != 0) return;
\r
101 std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
\r
102 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
\r
105 static void UnregisterHandlers() {
\r
106 // Restore all of the signal handlers to how they were before we showed up.
\r
107 for (unsigned i = 0, e = NumRegisteredSignals; i != e; ++i)
\r
108 sigaction(RegisteredSignalInfo[i].SigNo,
\r
109 &RegisteredSignalInfo[i].SA, 0);
\r
110 NumRegisteredSignals = 0;
\r
114 /// RemoveFilesToRemove - Process the FilesToRemove list. This function
\r
115 /// should be called with the SignalsMutex lock held.
\r
116 static void RemoveFilesToRemove() {
\r
117 while (!FilesToRemove.empty()) {
\r
118 FilesToRemove.back().eraseFromDisk(true);
\r
119 FilesToRemove.pop_back();
\r
123 // SignalHandler - The signal handler that runs.
\r
124 static RETSIGTYPE SignalHandler(int Sig) {
\r
125 // Restore the signal behavior to default, so that the program actually
\r
126 // crashes when we return and the signal reissues. This also ensures that if
\r
127 // we crash in our signal handler that the program will terminate immediately
\r
128 // instead of recursing in the signal handler.
\r
129 UnregisterHandlers();
\r
131 // Unmask all potentially blocked kill signals.
\r
133 sigfillset(&SigMask);
\r
134 sigprocmask(SIG_UNBLOCK, &SigMask, 0);
\r
136 SignalsMutex.acquire();
\r
137 RemoveFilesToRemove();
\r
139 if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd) {
\r
140 if (InterruptFunction) {
\r
141 void (*IF)() = InterruptFunction;
\r
142 SignalsMutex.release();
\r
143 InterruptFunction = 0;
\r
144 IF(); // run the interrupt function.
\r
148 SignalsMutex.release();
\r
149 raise(Sig); // Execute the default handler.
\r
153 SignalsMutex.release();
\r
155 // Otherwise if it is a fault (like SEGV) run any handler.
\r
156 for (unsigned i = 0, e = CallBacksToRun.size(); i != e; ++i)
\r
157 CallBacksToRun[i].first(CallBacksToRun[i].second);
\r
160 void llvm::sys::RunInterruptHandlers() {
\r
161 SignalsMutex.acquire();
\r
162 RemoveFilesToRemove();
\r
163 SignalsMutex.release();
\r
166 void llvm::sys::SetInterruptFunction(void (*IF)()) {
\r
167 SignalsMutex.acquire();
\r
168 InterruptFunction = IF;
\r
169 SignalsMutex.release();
\r
170 RegisterHandlers();
\r
173 // RemoveFileOnSignal - The public API
\r
174 bool llvm::sys::RemoveFileOnSignal(const sys::Path &Filename,
\r
175 std::string* ErrMsg) {
\r
176 SignalsMutex.acquire();
\r
177 FilesToRemove.push_back(Filename);
\r
179 SignalsMutex.release();
\r
181 RegisterHandlers();
\r
185 /// AddSignalHandler - Add a function to be called when a signal is delivered
\r
186 /// to the process. The handler can have a cookie passed to it to identify
\r
187 /// what instance of the handler it is.
\r
188 void llvm::sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) {
\r
189 CallBacksToRun.push_back(std::make_pair(FnPtr, Cookie));
\r
190 RegisterHandlers();
\r
194 // PrintStackTrace - In the case of a program crash or fault, print out a stack
\r
195 // trace so that the user has an indication of why and where we died.
\r
197 // On glibc systems we have the 'backtrace' function, which works nicely, but
\r
198 // doesn't demangle symbols.
\r
199 static void PrintStackTrace(void *) {
\r
200 #ifdef HAVE_BACKTRACE
\r
201 static void* StackTrace[256];
\r
202 // Use backtrace() to output a backtrace on Linux systems with glibc.
\r
203 int depth = backtrace(StackTrace,
\r
204 static_cast<int>(array_lengthof(StackTrace)));
\r
205 #if HAVE_DLFCN_H && __GNUG__
\r
207 for (int i = 0; i < depth; ++i) {
\r
209 dladdr(StackTrace[i], &dlinfo);
\r
210 const char* name = strrchr(dlinfo.dli_fname, '/');
\r
213 if (name == NULL) nwidth = strlen(dlinfo.dli_fname);
\r
214 else nwidth = strlen(name) - 1;
\r
216 if (nwidth > width) width = nwidth;
\r
219 for (int i = 0; i < depth; ++i) {
\r
221 dladdr(StackTrace[i], &dlinfo);
\r
223 fprintf(stderr, "%-2d", i);
\r
225 const char* name = strrchr(dlinfo.dli_fname, '/');
\r
226 if (name == NULL) fprintf(stderr, " %-*s", width, dlinfo.dli_fname);
\r
227 else fprintf(stderr, " %-*s", width, name+1);
\r
229 fprintf(stderr, " %#0*lx",
\r
230 (int)(sizeof(void*) * 2) + 2, (unsigned long)StackTrace[i]);
\r
232 if (dlinfo.dli_sname != NULL) {
\r
234 fputc(' ', stderr);
\r
235 char* d = abi::__cxa_demangle(dlinfo.dli_sname, NULL, NULL, &res);
\r
236 if (d == NULL) fputs(dlinfo.dli_sname, stderr);
\r
237 else fputs(d, stderr);
\r
240 fprintf(stderr, " + %tu",(char*)StackTrace[i]-(char*)dlinfo.dli_saddr);
\r
242 fputc('\n', stderr);
\r
245 backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
\r
250 /// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
\r
251 /// SIGSEGV) is delivered to the process, print a stack trace and then exit.
\r
252 void llvm::sys::PrintStackTraceOnErrorSignal() {
\r
253 AddSignalHandler(PrintStackTrace, 0);
\r