blob: d39e1e99a0c5dd055e14de72f1307b7c7c9b08f7 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- Signals.cpp - Generic Unix Signals Implementation -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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 Anderson1636de92007-09-07 04:06:50 +000016#include "llvm/ADT/STLExtras.h"
Owen Anderson8e5fbcf2009-08-17 17:07:22 +000017#include "llvm/System/Mutex.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include <vector>
19#include <algorithm>
20#if HAVE_EXECINFO_H
21# include <execinfo.h> // For backtrace().
22#endif
23#if HAVE_SIGNAL_H
24#include <signal.h>
25#endif
26#if HAVE_SYS_STAT_H
27#include <sys/stat.h>
28#endif
Dan Gohman3d313232008-12-05 20:12:48 +000029#if HAVE_DLFCN_H && __GNUG__
30#include <dlfcn.h>
31#include <cxxabi.h>
32#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033using namespace llvm;
34
Chris Lattner661fc342009-03-23 05:42:29 +000035static RETSIGTYPE SignalHandler(int Sig); // defined below.
36
Owen Anderson8e5fbcf2009-08-17 17:07:22 +000037static SmartMutex<true> SignalsMutex;
38
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039/// InterruptFunction - The function to call if ctrl-c is pressed.
Dan Gohmanffc2f032008-04-10 21:11:47 +000040static void (*InterruptFunction)() = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041
Chris Lattner199997b2009-03-04 21:21:36 +000042static std::vector<sys::Path> *FilesToRemove = 0;
43static std::vector<std::pair<void(*)(void*), void*> > *CallBacksToRun = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044
45// IntSigs - Signals that may interrupt the program at any time.
Dan Gohmanffc2f032008-04-10 21:11:47 +000046static const int IntSigs[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
48};
Dan Gohmanc336a132008-05-14 00:42:30 +000049static const int *const IntSigsEnd =
50 IntSigs + sizeof(IntSigs) / sizeof(IntSigs[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051
52// KillSigs - Signals that are synchronous with the program that will cause it
53// to die.
Dan Gohmanffc2f032008-04-10 21:11:47 +000054static const int KillSigs[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ
56#ifdef SIGEMT
57 , SIGEMT
58#endif
59};
Dan Gohmanc336a132008-05-14 00:42:30 +000060static const int *const KillSigsEnd =
61 KillSigs + sizeof(KillSigs) / sizeof(KillSigs[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062
Chris Lattner48c6b8d2009-03-23 05:55:36 +000063static unsigned NumRegisteredSignals = 0;
64static struct {
65 struct sigaction SA;
66 int SigNo;
67} RegisteredSignalInfo[(sizeof(IntSigs)+sizeof(KillSigs))/sizeof(KillSigs[0])];
68
69
Chris Lattner661fc342009-03-23 05:42:29 +000070static void RegisterHandler(int Signal) {
Chris Lattner48c6b8d2009-03-23 05:55:36 +000071 assert(NumRegisteredSignals <
72 sizeof(RegisteredSignalInfo)/sizeof(RegisteredSignalInfo[0]) &&
73 "Out of space for signal handlers!");
74
75 struct sigaction NewHandler;
76
77 NewHandler.sa_handler = SignalHandler;
78 NewHandler.sa_flags = SA_NODEFER|SA_RESETHAND;
79 sigemptyset(&NewHandler.sa_mask);
80
81 // Install the new handler, save the old one in RegisteredSignalInfo.
82 sigaction(Signal, &NewHandler,
83 &RegisteredSignalInfo[NumRegisteredSignals].SA);
84 RegisteredSignalInfo[NumRegisteredSignals].SigNo = Signal;
85 ++NumRegisteredSignals;
Chris Lattnerf74e6ef2009-03-07 08:15:47 +000086}
87
Chris Lattner661fc342009-03-23 05:42:29 +000088static void RegisterHandlers() {
Chris Lattner48c6b8d2009-03-23 05:55:36 +000089 // If the handlers are already registered, we're done.
90 if (NumRegisteredSignals != 0) return;
91
Chris Lattner661fc342009-03-23 05:42:29 +000092 std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
93 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
94}
95
Chris Lattner661fc342009-03-23 05:42:29 +000096static void UnregisterHandlers() {
Chris Lattner48c6b8d2009-03-23 05:55:36 +000097 // Restore all of the signal handlers to how they were before we showed up.
98 for (unsigned i = 0, e = NumRegisteredSignals; i != e; ++i)
Chris Lattner9d5b4d92009-03-23 06:46:20 +000099 sigaction(RegisteredSignalInfo[i].SigNo,
100 &RegisteredSignalInfo[i].SA, 0);
Chris Lattner48c6b8d2009-03-23 05:55:36 +0000101 NumRegisteredSignals = 0;
Chris Lattner661fc342009-03-23 05:42:29 +0000102}
103
104
Chris Lattnerf74e6ef2009-03-07 08:15:47 +0000105
106// SignalHandler - The signal handler that runs.
Chris Lattner199997b2009-03-04 21:21:36 +0000107static RETSIGTYPE SignalHandler(int Sig) {
Chris Lattner52a5f392009-03-05 18:22:14 +0000108 // Restore the signal behavior to default, so that the program actually
109 // crashes when we return and the signal reissues. This also ensures that if
110 // we crash in our signal handler that the program will terminate immediately
111 // instead of recursing in the signal handler.
Chris Lattner661fc342009-03-23 05:42:29 +0000112 UnregisterHandlers();
Chris Lattnerf74e6ef2009-03-07 08:15:47 +0000113
114 // Unmask all potentially blocked kill signals.
115 sigset_t SigMask;
116 sigfillset(&SigMask);
117 sigprocmask(SIG_UNBLOCK, &SigMask, 0);
Chris Lattner52a5f392009-03-05 18:22:14 +0000118
Owen Anderson8e5fbcf2009-08-17 17:07:22 +0000119 SignalsMutex.acquire();
Chris Lattner199997b2009-03-04 21:21:36 +0000120 if (FilesToRemove != 0)
121 while (!FilesToRemove->empty()) {
122 FilesToRemove->back().eraseFromDisk(true);
123 FilesToRemove->pop_back();
124 }
125
126 if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd) {
127 if (InterruptFunction) {
128 void (*IF)() = InterruptFunction;
Owen Anderson8e5fbcf2009-08-17 17:07:22 +0000129 SignalsMutex.release();
Chris Lattner199997b2009-03-04 21:21:36 +0000130 InterruptFunction = 0;
131 IF(); // run the interrupt function.
132 return;
133 }
Owen Anderson8e5fbcf2009-08-17 17:07:22 +0000134
135 SignalsMutex.release();
Chris Lattner2f1b1992009-04-12 23:33:13 +0000136 raise(Sig); // Execute the default handler.
137 return;
Chris Lattner199997b2009-03-04 21:21:36 +0000138 }
139
Owen Anderson8e5fbcf2009-08-17 17:07:22 +0000140 SignalsMutex.release();
141
Chris Lattner199997b2009-03-04 21:21:36 +0000142 // Otherwise if it is a fault (like SEGV) run any handler.
143 if (CallBacksToRun)
144 for (unsigned i = 0, e = CallBacksToRun->size(); i != e; ++i)
145 (*CallBacksToRun)[i].first((*CallBacksToRun)[i].second);
Chris Lattner199997b2009-03-04 21:21:36 +0000146}
147
Chris Lattner199997b2009-03-04 21:21:36 +0000148
149
Chris Lattnerafea1b72009-03-08 19:13:45 +0000150void llvm::sys::SetInterruptFunction(void (*IF)()) {
Owen Anderson8e5fbcf2009-08-17 17:07:22 +0000151 SignalsMutex.acquire();
Chris Lattner199997b2009-03-04 21:21:36 +0000152 InterruptFunction = IF;
Owen Anderson8e5fbcf2009-08-17 17:07:22 +0000153 SignalsMutex.release();
Chris Lattner661fc342009-03-23 05:42:29 +0000154 RegisterHandlers();
Chris Lattner199997b2009-03-04 21:21:36 +0000155}
156
157// RemoveFileOnSignal - The public API
Chris Lattnerafea1b72009-03-08 19:13:45 +0000158bool llvm::sys::RemoveFileOnSignal(const sys::Path &Filename,
159 std::string* ErrMsg) {
Owen Anderson8e5fbcf2009-08-17 17:07:22 +0000160 SignalsMutex.acquire();
Chris Lattner199997b2009-03-04 21:21:36 +0000161 if (FilesToRemove == 0)
162 FilesToRemove = new std::vector<sys::Path>();
163
164 FilesToRemove->push_back(Filename);
165
Owen Anderson8e5fbcf2009-08-17 17:07:22 +0000166 SignalsMutex.release();
167
Chris Lattner661fc342009-03-23 05:42:29 +0000168 RegisterHandlers();
Chris Lattner199997b2009-03-04 21:21:36 +0000169 return false;
170}
171
172/// AddSignalHandler - Add a function to be called when a signal is delivered
173/// to the process. The handler can have a cookie passed to it to identify
174/// what instance of the handler it is.
Chris Lattnerafea1b72009-03-08 19:13:45 +0000175void llvm::sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) {
Chris Lattner199997b2009-03-04 21:21:36 +0000176 if (CallBacksToRun == 0)
177 CallBacksToRun = new std::vector<std::pair<void(*)(void*), void*> >();
178 CallBacksToRun->push_back(std::make_pair(FnPtr, Cookie));
Chris Lattner661fc342009-03-23 05:42:29 +0000179 RegisterHandlers();
Chris Lattner199997b2009-03-04 21:21:36 +0000180}
181
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182
183// PrintStackTrace - In the case of a program crash or fault, print out a stack
184// trace so that the user has an indication of why and where we died.
185//
186// On glibc systems we have the 'backtrace' function, which works nicely, but
Lauro Ramos Venancio538db762008-02-15 18:05:54 +0000187// doesn't demangle symbols.
Chris Lattner199997b2009-03-04 21:21:36 +0000188static void PrintStackTrace(void *) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189#ifdef HAVE_BACKTRACE
Chris Lattner199997b2009-03-04 21:21:36 +0000190 static void* StackTrace[256];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 // Use backtrace() to output a backtrace on Linux systems with glibc.
Evan Cheng591bfc82008-05-05 18:30:58 +0000192 int depth = backtrace(StackTrace,
193 static_cast<int>(array_lengthof(StackTrace)));
Dan Gohman3d313232008-12-05 20:12:48 +0000194#if HAVE_DLFCN_H && __GNUG__
195 int width = 0;
196 for (int i = 0; i < depth; ++i) {
197 Dl_info dlinfo;
198 dladdr(StackTrace[i], &dlinfo);
Dan Gohman2f75d522009-02-10 17:56:28 +0000199 const char* name = strrchr(dlinfo.dli_fname, '/');
Dan Gohman3d313232008-12-05 20:12:48 +0000200
201 int nwidth;
202 if (name == NULL) nwidth = strlen(dlinfo.dli_fname);
203 else nwidth = strlen(name) - 1;
204
205 if (nwidth > width) width = nwidth;
206 }
207
208 for (int i = 0; i < depth; ++i) {
209 Dl_info dlinfo;
210 dladdr(StackTrace[i], &dlinfo);
211
212 fprintf(stderr, "%-3d", i);
213
Dan Gohman2f75d522009-02-10 17:56:28 +0000214 const char* name = strrchr(dlinfo.dli_fname, '/');
Dan Gohman3d313232008-12-05 20:12:48 +0000215 if (name == NULL) fprintf(stderr, " %-*s", width, dlinfo.dli_fname);
216 else fprintf(stderr, " %-*s", width, name+1);
217
Dan Gohman0a48f142008-12-05 23:39:24 +0000218 fprintf(stderr, " %#0*lx",
219 (int)(sizeof(void*) * 2) + 2, (unsigned long)StackTrace[i]);
Dan Gohman3d313232008-12-05 20:12:48 +0000220
221 if (dlinfo.dli_sname != NULL) {
222 int res;
223 fputc(' ', stderr);
224 char* d = abi::__cxa_demangle(dlinfo.dli_sname, NULL, NULL, &res);
225 if (d == NULL) fputs(dlinfo.dli_sname, stderr);
226 else fputs(d, stderr);
227 free(d);
228
229 fprintf(stderr, " + %tu",(char*)StackTrace[i]-(char*)dlinfo.dli_saddr);
230 }
231 fputc('\n', stderr);
232 }
233#else
Lauro Ramos Venancio538db762008-02-15 18:05:54 +0000234 backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235#endif
Dan Gohman3d313232008-12-05 20:12:48 +0000236#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237}
238
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239/// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
240/// SIGSEGV) is delivered to the process, print a stack trace and then exit.
Chris Lattnerafea1b72009-03-08 19:13:45 +0000241void llvm::sys::PrintStackTraceOnErrorSignal() {
Chris Lattner199997b2009-03-04 21:21:36 +0000242 AddSignalHandler(PrintStackTrace, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243}
Chris Lattner199997b2009-03-04 21:21:36 +0000244