blob: 3e0de66b5d71e0f2f104bf8fbe91c81f501f9e6a [file] [log] [blame]
Reid Spencer496c2772004-08-29 19:22:48 +00001//===- Signals.cpp - Generic Unix Signals Implementation -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer496c2772004-08-29 19:22:48 +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 Anderson718cb662007-09-07 04:06:50 +000016#include "llvm/ADT/STLExtras.h"
Owen Anderson6ae8c732009-08-17 17:07:22 +000017#include "llvm/System/Mutex.h"
Reid Spencer496c2772004-08-29 19:22:48 +000018#include <vector>
19#include <algorithm>
Reid Spencercdf54d02004-12-27 06:16:52 +000020#if HAVE_EXECINFO_H
Reid Spencer496c2772004-08-29 19:22:48 +000021# include <execinfo.h> // For backtrace().
22#endif
Reid Spencercdf54d02004-12-27 06:16:52 +000023#if HAVE_SIGNAL_H
Reid Spencer496c2772004-08-29 19:22:48 +000024#include <signal.h>
Reid Spencercdf54d02004-12-27 06:16:52 +000025#endif
Reid Spencer2ae9d112007-04-07 18:52:17 +000026#if HAVE_SYS_STAT_H
27#include <sys/stat.h>
28#endif
Dan Gohman56d9b6d2008-12-05 20:12:48 +000029#if HAVE_DLFCN_H && __GNUG__
30#include <dlfcn.h>
31#include <cxxabi.h>
32#endif
Chris Lattner89615012006-08-01 17:59:14 +000033using namespace llvm;
Reid Spencer496c2772004-08-29 19:22:48 +000034
Chris Lattner1c4d8a02009-03-23 05:42:29 +000035static RETSIGTYPE SignalHandler(int Sig); // defined below.
36
Owen Anderson6ae8c732009-08-17 17:07:22 +000037static SmartMutex<true> SignalsMutex;
38
Chris Lattnerfa8c2922005-08-02 02:14:22 +000039/// InterruptFunction - The function to call if ctrl-c is pressed.
Dan Gohman3bd659b2008-04-10 21:11:47 +000040static void (*InterruptFunction)() = 0;
Chris Lattnerfa8c2922005-08-02 02:14:22 +000041
Jeffrey Yasskinb7ccf752010-03-17 07:08:12 +000042static std::vector<sys::Path> FilesToRemove;
43static std::vector<std::pair<void(*)(void*), void*> > CallBacksToRun;
Reid Spencer496c2772004-08-29 19:22:48 +000044
45// IntSigs - Signals that may interrupt the program at any time.
Dan Gohman3bd659b2008-04-10 21:11:47 +000046static const int IntSigs[] = {
Reid Spencer496c2772004-08-29 19:22:48 +000047 SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
48};
Dan Gohman4d9dd6b2008-05-14 00:42:30 +000049static const int *const IntSigsEnd =
50 IntSigs + sizeof(IntSigs) / sizeof(IntSigs[0]);
Reid Spencer496c2772004-08-29 19:22:48 +000051
52// KillSigs - Signals that are synchronous with the program that will cause it
53// to die.
Dan Gohman3bd659b2008-04-10 21:11:47 +000054static const int KillSigs[] = {
Chris Lattner6ae7bbb2010-02-12 00:37:46 +000055 SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV
56#ifdef SIGSYS
57 , SIGSYS
58#endif
59#ifdef SIGXCPU
60 , SIGXCPU
61#endif
Chris Lattner782ab582010-02-14 18:20:09 +000062#ifdef SIGXFSZ
Chris Lattner6ae7bbb2010-02-12 00:37:46 +000063 , SIGXFSZ
64#endif
Reid Spencer496c2772004-08-29 19:22:48 +000065#ifdef SIGEMT
66 , SIGEMT
67#endif
68};
Dan Gohman4d9dd6b2008-05-14 00:42:30 +000069static const int *const KillSigsEnd =
70 KillSigs + sizeof(KillSigs) / sizeof(KillSigs[0]);
Reid Spencer496c2772004-08-29 19:22:48 +000071
Chris Lattnereab5cb32009-03-23 05:55:36 +000072static unsigned NumRegisteredSignals = 0;
73static struct {
74 struct sigaction SA;
75 int SigNo;
76} RegisteredSignalInfo[(sizeof(IntSigs)+sizeof(KillSigs))/sizeof(KillSigs[0])];
77
78
Chris Lattner1c4d8a02009-03-23 05:42:29 +000079static void RegisterHandler(int Signal) {
Chris Lattnereab5cb32009-03-23 05:55:36 +000080 assert(NumRegisteredSignals <
81 sizeof(RegisteredSignalInfo)/sizeof(RegisteredSignalInfo[0]) &&
82 "Out of space for signal handlers!");
83
84 struct sigaction NewHandler;
85
86 NewHandler.sa_handler = SignalHandler;
87 NewHandler.sa_flags = SA_NODEFER|SA_RESETHAND;
88 sigemptyset(&NewHandler.sa_mask);
89
90 // Install the new handler, save the old one in RegisteredSignalInfo.
91 sigaction(Signal, &NewHandler,
92 &RegisteredSignalInfo[NumRegisteredSignals].SA);
93 RegisteredSignalInfo[NumRegisteredSignals].SigNo = Signal;
94 ++NumRegisteredSignals;
Chris Lattner922a8812009-03-07 08:15:47 +000095}
96
Chris Lattner1c4d8a02009-03-23 05:42:29 +000097static void RegisterHandlers() {
Chris Lattnereab5cb32009-03-23 05:55:36 +000098 // If the handlers are already registered, we're done.
99 if (NumRegisteredSignals != 0) return;
100
Chris Lattner1c4d8a02009-03-23 05:42:29 +0000101 std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
102 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
103}
104
Chris Lattner1c4d8a02009-03-23 05:42:29 +0000105static void UnregisterHandlers() {
Chris Lattnereab5cb32009-03-23 05:55:36 +0000106 // Restore all of the signal handlers to how they were before we showed up.
107 for (unsigned i = 0, e = NumRegisteredSignals; i != e; ++i)
Chris Lattnera8bd27f2009-03-23 06:46:20 +0000108 sigaction(RegisteredSignalInfo[i].SigNo,
109 &RegisteredSignalInfo[i].SA, 0);
Chris Lattnereab5cb32009-03-23 05:55:36 +0000110 NumRegisteredSignals = 0;
Chris Lattner1c4d8a02009-03-23 05:42:29 +0000111}
112
113
Dan Gohman39f76bb2010-05-27 23:11:55 +0000114/// RemoveFilesToRemove - Process the FilesToRemove list. This function
115/// should be called with the SignalsMutex lock held.
116static void RemoveFilesToRemove() {
117 while (!FilesToRemove.empty()) {
118 FilesToRemove.back().eraseFromDisk(true);
119 FilesToRemove.pop_back();
120 }
121}
Chris Lattner922a8812009-03-07 08:15:47 +0000122
123// SignalHandler - The signal handler that runs.
Chris Lattner35033a52009-03-04 21:21:36 +0000124static RETSIGTYPE SignalHandler(int Sig) {
Chris Lattner0f6290d2009-03-05 18:22:14 +0000125 // Restore the signal behavior to default, so that the program actually
126 // crashes when we return and the signal reissues. This also ensures that if
127 // we crash in our signal handler that the program will terminate immediately
128 // instead of recursing in the signal handler.
Chris Lattner1c4d8a02009-03-23 05:42:29 +0000129 UnregisterHandlers();
Chris Lattner922a8812009-03-07 08:15:47 +0000130
131 // Unmask all potentially blocked kill signals.
132 sigset_t SigMask;
133 sigfillset(&SigMask);
134 sigprocmask(SIG_UNBLOCK, &SigMask, 0);
Chris Lattner0f6290d2009-03-05 18:22:14 +0000135
Owen Anderson6ae8c732009-08-17 17:07:22 +0000136 SignalsMutex.acquire();
Dan Gohman39f76bb2010-05-27 23:11:55 +0000137 RemoveFilesToRemove();
Chris Lattner35033a52009-03-04 21:21:36 +0000138
139 if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd) {
140 if (InterruptFunction) {
141 void (*IF)() = InterruptFunction;
Owen Anderson6ae8c732009-08-17 17:07:22 +0000142 SignalsMutex.release();
Chris Lattner35033a52009-03-04 21:21:36 +0000143 InterruptFunction = 0;
144 IF(); // run the interrupt function.
145 return;
146 }
Owen Anderson6ae8c732009-08-17 17:07:22 +0000147
148 SignalsMutex.release();
Chris Lattnerb7253152009-04-12 23:33:13 +0000149 raise(Sig); // Execute the default handler.
150 return;
Chris Lattner35033a52009-03-04 21:21:36 +0000151 }
152
Owen Anderson6ae8c732009-08-17 17:07:22 +0000153 SignalsMutex.release();
154
Chris Lattner35033a52009-03-04 21:21:36 +0000155 // Otherwise if it is a fault (like SEGV) run any handler.
Jeffrey Yasskinb7ccf752010-03-17 07:08:12 +0000156 for (unsigned i = 0, e = CallBacksToRun.size(); i != e; ++i)
157 CallBacksToRun[i].first(CallBacksToRun[i].second);
Chris Lattner35033a52009-03-04 21:21:36 +0000158}
159
Daniel Dunbarfb89e082010-05-08 02:10:34 +0000160void llvm::sys::RunInterruptHandlers() {
Dan Gohman39f76bb2010-05-27 23:11:55 +0000161 SignalsMutex.acquire();
162 RemoveFilesToRemove();
163 SignalsMutex.release();
Daniel Dunbarfb89e082010-05-08 02:10:34 +0000164}
Chris Lattner35033a52009-03-04 21:21:36 +0000165
Chris Lattnerf48ba7b2009-03-08 19:13:45 +0000166void llvm::sys::SetInterruptFunction(void (*IF)()) {
Owen Anderson6ae8c732009-08-17 17:07:22 +0000167 SignalsMutex.acquire();
Chris Lattner35033a52009-03-04 21:21:36 +0000168 InterruptFunction = IF;
Owen Anderson6ae8c732009-08-17 17:07:22 +0000169 SignalsMutex.release();
Chris Lattner1c4d8a02009-03-23 05:42:29 +0000170 RegisterHandlers();
Chris Lattner35033a52009-03-04 21:21:36 +0000171}
172
173// RemoveFileOnSignal - The public API
Chris Lattnerf48ba7b2009-03-08 19:13:45 +0000174bool llvm::sys::RemoveFileOnSignal(const sys::Path &Filename,
175 std::string* ErrMsg) {
Owen Anderson6ae8c732009-08-17 17:07:22 +0000176 SignalsMutex.acquire();
Jeffrey Yasskinb7ccf752010-03-17 07:08:12 +0000177 FilesToRemove.push_back(Filename);
Chris Lattner35033a52009-03-04 21:21:36 +0000178
Owen Anderson6ae8c732009-08-17 17:07:22 +0000179 SignalsMutex.release();
180
Chris Lattner1c4d8a02009-03-23 05:42:29 +0000181 RegisterHandlers();
Chris Lattner35033a52009-03-04 21:21:36 +0000182 return false;
183}
184
185/// AddSignalHandler - Add a function to be called when a signal is delivered
186/// to the process. The handler can have a cookie passed to it to identify
187/// what instance of the handler it is.
Chris Lattnerf48ba7b2009-03-08 19:13:45 +0000188void llvm::sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) {
Jeffrey Yasskinb7ccf752010-03-17 07:08:12 +0000189 CallBacksToRun.push_back(std::make_pair(FnPtr, Cookie));
Chris Lattner1c4d8a02009-03-23 05:42:29 +0000190 RegisterHandlers();
Chris Lattner35033a52009-03-04 21:21:36 +0000191}
192
Reid Spencer496c2772004-08-29 19:22:48 +0000193
194// PrintStackTrace - In the case of a program crash or fault, print out a stack
195// trace so that the user has an indication of why and where we died.
196//
197// On glibc systems we have the 'backtrace' function, which works nicely, but
Lauro Ramos Venancio2e78b782008-02-15 18:05:54 +0000198// doesn't demangle symbols.
Chris Lattner35033a52009-03-04 21:21:36 +0000199static void PrintStackTrace(void *) {
Reid Spencer496c2772004-08-29 19:22:48 +0000200#ifdef HAVE_BACKTRACE
Chris Lattner35033a52009-03-04 21:21:36 +0000201 static void* StackTrace[256];
Reid Spencer496c2772004-08-29 19:22:48 +0000202 // Use backtrace() to output a backtrace on Linux systems with glibc.
Evan Cheng34cd4a42008-05-05 18:30:58 +0000203 int depth = backtrace(StackTrace,
204 static_cast<int>(array_lengthof(StackTrace)));
Dan Gohman56d9b6d2008-12-05 20:12:48 +0000205#if HAVE_DLFCN_H && __GNUG__
206 int width = 0;
207 for (int i = 0; i < depth; ++i) {
208 Dl_info dlinfo;
209 dladdr(StackTrace[i], &dlinfo);
Dan Gohmand4e18452009-02-10 17:56:28 +0000210 const char* name = strrchr(dlinfo.dli_fname, '/');
Dan Gohman56d9b6d2008-12-05 20:12:48 +0000211
212 int nwidth;
213 if (name == NULL) nwidth = strlen(dlinfo.dli_fname);
214 else nwidth = strlen(name) - 1;
215
216 if (nwidth > width) width = nwidth;
217 }
218
219 for (int i = 0; i < depth; ++i) {
220 Dl_info dlinfo;
221 dladdr(StackTrace[i], &dlinfo);
222
Dan Gohman0b81e192009-10-30 02:45:10 +0000223 fprintf(stderr, "%-2d", i);
Dan Gohman56d9b6d2008-12-05 20:12:48 +0000224
Dan Gohmand4e18452009-02-10 17:56:28 +0000225 const char* name = strrchr(dlinfo.dli_fname, '/');
Dan Gohman56d9b6d2008-12-05 20:12:48 +0000226 if (name == NULL) fprintf(stderr, " %-*s", width, dlinfo.dli_fname);
227 else fprintf(stderr, " %-*s", width, name+1);
228
Dan Gohmandd17b252008-12-05 23:39:24 +0000229 fprintf(stderr, " %#0*lx",
230 (int)(sizeof(void*) * 2) + 2, (unsigned long)StackTrace[i]);
Dan Gohman56d9b6d2008-12-05 20:12:48 +0000231
232 if (dlinfo.dli_sname != NULL) {
233 int res;
234 fputc(' ', stderr);
235 char* d = abi::__cxa_demangle(dlinfo.dli_sname, NULL, NULL, &res);
236 if (d == NULL) fputs(dlinfo.dli_sname, stderr);
237 else fputs(d, stderr);
238 free(d);
239
240 fprintf(stderr, " + %tu",(char*)StackTrace[i]-(char*)dlinfo.dli_saddr);
241 }
242 fputc('\n', stderr);
243 }
244#else
Lauro Ramos Venancio2e78b782008-02-15 18:05:54 +0000245 backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
Reid Spencer496c2772004-08-29 19:22:48 +0000246#endif
Dan Gohman56d9b6d2008-12-05 20:12:48 +0000247#endif
Reid Spencer496c2772004-08-29 19:22:48 +0000248}
249
Reid Spencer496c2772004-08-29 19:22:48 +0000250/// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
251/// SIGSEGV) is delivered to the process, print a stack trace and then exit.
Chris Lattnerf48ba7b2009-03-08 19:13:45 +0000252void llvm::sys::PrintStackTraceOnErrorSignal() {
Chris Lattner35033a52009-03-04 21:21:36 +0000253 AddSignalHandler(PrintStackTrace, 0);
Reid Spencer496c2772004-08-29 19:22:48 +0000254}
Chris Lattner35033a52009-03-04 21:21:36 +0000255
Daniel Dunbarb08ceb82010-08-19 23:45:39 +0000256
257/***/
258
259// On Darwin, raise sends a signal to the main thread instead of the current
260// thread. This has the unfortunate effect that assert() and abort() will end up
261// bypassing our crash recovery attempts. We work around this for anything in
262// the same linkage unit by just defining our own versions of the assert handler
263// and abort.
264
265#ifdef __APPLE__
266
267void __assert_rtn(const char *func,
268 const char *file,
269 int line,
270 const char *expr) {
271 if (func)
272 fprintf(stderr, "Assertion failed: (%s), function %s, file %s, line %d.\n",
273 expr, func, file, line);
274 else
275 fprintf(stderr, "Assertion failed: (%s), file %s, line %d.\n",
276 expr, file, line);
277 abort();
278}
279
280#include <signal.h>
281#include <pthread.h>
282
283void abort() {
284 pthread_kill(pthread_self(), SIGABRT);
285 usleep(1000);
286 __builtin_trap();
287}
288
289#endif