blob: c9ec9fce9aa1bf341a954082f1dc5be64a1a894d [file] [log] [blame]
Reid Spencer496c2772004-08-29 19:22:48 +00001//===- Signals.cpp - Generic Unix Signals Implementation -----*- C++ -*-===//
Michael J. Spencer1f6efa32010-11-29 18:16:10 +00002//
Reid Spencer496c2772004-08-29 19:22:48 +00003// 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.
Michael J. Spencer1f6efa32010-11-29 18:16:10 +00007//
Reid Spencer496c2772004-08-29 19:22:48 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines some helpful functions for dealing with the possibility of
Chris Lattner7a2bdde2011-04-15 05:18:47 +000011// Unix signals occurring while your program is running.
Reid Spencer496c2772004-08-29 19:22:48 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "Unix.h"
Owen Anderson718cb662007-09-07 04:06:50 +000016#include "llvm/ADT/STLExtras.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000017#include "llvm/Support/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>
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000031#include <cxxabi.h>
Dan Gohman56d9b6d2008-12-05 20:12:48 +000032#endif
Argyrios Kyrtzidis08713b32012-01-11 20:53:25 +000033#if HAVE_MACH_MACH_H
34#include <mach/mach.h>
35#endif
36
Chris Lattner89615012006-08-01 17:59:14 +000037using namespace llvm;
Reid Spencer496c2772004-08-29 19:22:48 +000038
Chris Lattner1c4d8a02009-03-23 05:42:29 +000039static RETSIGTYPE SignalHandler(int Sig); // defined below.
40
Owen Anderson6ae8c732009-08-17 17:07:22 +000041static SmartMutex<true> SignalsMutex;
42
Chris Lattnerfa8c2922005-08-02 02:14:22 +000043/// InterruptFunction - The function to call if ctrl-c is pressed.
Dan Gohman3bd659b2008-04-10 21:11:47 +000044static void (*InterruptFunction)() = 0;
Chris Lattnerfa8c2922005-08-02 02:14:22 +000045
Jeffrey Yasskinb7ccf752010-03-17 07:08:12 +000046static std::vector<sys::Path> FilesToRemove;
47static std::vector<std::pair<void(*)(void*), void*> > CallBacksToRun;
Reid Spencer496c2772004-08-29 19:22:48 +000048
49// IntSigs - Signals that may interrupt the program at any time.
Dan Gohman3bd659b2008-04-10 21:11:47 +000050static const int IntSigs[] = {
Reid Spencer496c2772004-08-29 19:22:48 +000051 SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
52};
Dan Gohman4d9dd6b2008-05-14 00:42:30 +000053static const int *const IntSigsEnd =
54 IntSigs + sizeof(IntSigs) / sizeof(IntSigs[0]);
Reid Spencer496c2772004-08-29 19:22:48 +000055
56// KillSigs - Signals that are synchronous with the program that will cause it
57// to die.
Dan Gohman3bd659b2008-04-10 21:11:47 +000058static const int KillSigs[] = {
Chris Lattner6ae7bbb2010-02-12 00:37:46 +000059 SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV
60#ifdef SIGSYS
61 , SIGSYS
62#endif
63#ifdef SIGXCPU
64 , SIGXCPU
65#endif
Chris Lattner782ab582010-02-14 18:20:09 +000066#ifdef SIGXFSZ
Chris Lattner6ae7bbb2010-02-12 00:37:46 +000067 , SIGXFSZ
68#endif
Reid Spencer496c2772004-08-29 19:22:48 +000069#ifdef SIGEMT
70 , SIGEMT
71#endif
72};
Dan Gohman4d9dd6b2008-05-14 00:42:30 +000073static const int *const KillSigsEnd =
74 KillSigs + sizeof(KillSigs) / sizeof(KillSigs[0]);
Reid Spencer496c2772004-08-29 19:22:48 +000075
Chris Lattnereab5cb32009-03-23 05:55:36 +000076static unsigned NumRegisteredSignals = 0;
77static struct {
78 struct sigaction SA;
79 int SigNo;
80} RegisteredSignalInfo[(sizeof(IntSigs)+sizeof(KillSigs))/sizeof(KillSigs[0])];
81
82
Chris Lattner1c4d8a02009-03-23 05:42:29 +000083static void RegisterHandler(int Signal) {
Chris Lattnereab5cb32009-03-23 05:55:36 +000084 assert(NumRegisteredSignals <
85 sizeof(RegisteredSignalInfo)/sizeof(RegisteredSignalInfo[0]) &&
86 "Out of space for signal handlers!");
87
88 struct sigaction NewHandler;
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000089
Chris Lattnereab5cb32009-03-23 05:55:36 +000090 NewHandler.sa_handler = SignalHandler;
91 NewHandler.sa_flags = SA_NODEFER|SA_RESETHAND;
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000092 sigemptyset(&NewHandler.sa_mask);
93
Chris Lattnereab5cb32009-03-23 05:55:36 +000094 // Install the new handler, save the old one in RegisteredSignalInfo.
95 sigaction(Signal, &NewHandler,
96 &RegisteredSignalInfo[NumRegisteredSignals].SA);
97 RegisteredSignalInfo[NumRegisteredSignals].SigNo = Signal;
98 ++NumRegisteredSignals;
Chris Lattner922a8812009-03-07 08:15:47 +000099}
100
Chris Lattner1c4d8a02009-03-23 05:42:29 +0000101static void RegisterHandlers() {
Chris Lattnereab5cb32009-03-23 05:55:36 +0000102 // If the handlers are already registered, we're done.
103 if (NumRegisteredSignals != 0) return;
104
Chris Lattner1c4d8a02009-03-23 05:42:29 +0000105 std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
106 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
107}
108
Chris Lattner1c4d8a02009-03-23 05:42:29 +0000109static void UnregisterHandlers() {
Chris Lattnereab5cb32009-03-23 05:55:36 +0000110 // Restore all of the signal handlers to how they were before we showed up.
111 for (unsigned i = 0, e = NumRegisteredSignals; i != e; ++i)
Chris Lattnera8bd27f2009-03-23 06:46:20 +0000112 sigaction(RegisteredSignalInfo[i].SigNo,
113 &RegisteredSignalInfo[i].SA, 0);
Chris Lattnereab5cb32009-03-23 05:55:36 +0000114 NumRegisteredSignals = 0;
Chris Lattner1c4d8a02009-03-23 05:42:29 +0000115}
116
117
Dan Gohman39f76bb2010-05-27 23:11:55 +0000118/// RemoveFilesToRemove - Process the FilesToRemove list. This function
119/// should be called with the SignalsMutex lock held.
120static void RemoveFilesToRemove() {
121 while (!FilesToRemove.empty()) {
122 FilesToRemove.back().eraseFromDisk(true);
123 FilesToRemove.pop_back();
124 }
125}
Chris Lattner922a8812009-03-07 08:15:47 +0000126
127// SignalHandler - The signal handler that runs.
Chris Lattner35033a52009-03-04 21:21:36 +0000128static RETSIGTYPE SignalHandler(int Sig) {
Chris Lattner0f6290d2009-03-05 18:22:14 +0000129 // Restore the signal behavior to default, so that the program actually
130 // crashes when we return and the signal reissues. This also ensures that if
131 // we crash in our signal handler that the program will terminate immediately
132 // instead of recursing in the signal handler.
Chris Lattner1c4d8a02009-03-23 05:42:29 +0000133 UnregisterHandlers();
Chris Lattner922a8812009-03-07 08:15:47 +0000134
135 // Unmask all potentially blocked kill signals.
136 sigset_t SigMask;
137 sigfillset(&SigMask);
138 sigprocmask(SIG_UNBLOCK, &SigMask, 0);
Chris Lattner0f6290d2009-03-05 18:22:14 +0000139
Owen Anderson6ae8c732009-08-17 17:07:22 +0000140 SignalsMutex.acquire();
Dan Gohman39f76bb2010-05-27 23:11:55 +0000141 RemoveFilesToRemove();
Chris Lattner35033a52009-03-04 21:21:36 +0000142
143 if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd) {
144 if (InterruptFunction) {
145 void (*IF)() = InterruptFunction;
Owen Anderson6ae8c732009-08-17 17:07:22 +0000146 SignalsMutex.release();
Chris Lattner35033a52009-03-04 21:21:36 +0000147 InterruptFunction = 0;
148 IF(); // run the interrupt function.
149 return;
150 }
Michael J. Spencer1f6efa32010-11-29 18:16:10 +0000151
Owen Anderson6ae8c732009-08-17 17:07:22 +0000152 SignalsMutex.release();
Chris Lattnerb7253152009-04-12 23:33:13 +0000153 raise(Sig); // Execute the default handler.
154 return;
Chris Lattner35033a52009-03-04 21:21:36 +0000155 }
156
Owen Anderson6ae8c732009-08-17 17:07:22 +0000157 SignalsMutex.release();
158
Chris Lattner35033a52009-03-04 21:21:36 +0000159 // Otherwise if it is a fault (like SEGV) run any handler.
Jeffrey Yasskinb7ccf752010-03-17 07:08:12 +0000160 for (unsigned i = 0, e = CallBacksToRun.size(); i != e; ++i)
161 CallBacksToRun[i].first(CallBacksToRun[i].second);
Chris Lattner35033a52009-03-04 21:21:36 +0000162}
163
Daniel Dunbarfb89e082010-05-08 02:10:34 +0000164void llvm::sys::RunInterruptHandlers() {
Dan Gohman39f76bb2010-05-27 23:11:55 +0000165 SignalsMutex.acquire();
166 RemoveFilesToRemove();
167 SignalsMutex.release();
Daniel Dunbarfb89e082010-05-08 02:10:34 +0000168}
Chris Lattner35033a52009-03-04 21:21:36 +0000169
Chris Lattnerf48ba7b2009-03-08 19:13:45 +0000170void llvm::sys::SetInterruptFunction(void (*IF)()) {
Owen Anderson6ae8c732009-08-17 17:07:22 +0000171 SignalsMutex.acquire();
Chris Lattner35033a52009-03-04 21:21:36 +0000172 InterruptFunction = IF;
Owen Anderson6ae8c732009-08-17 17:07:22 +0000173 SignalsMutex.release();
Chris Lattner1c4d8a02009-03-23 05:42:29 +0000174 RegisterHandlers();
Chris Lattner35033a52009-03-04 21:21:36 +0000175}
176
177// RemoveFileOnSignal - The public API
Chris Lattnerf48ba7b2009-03-08 19:13:45 +0000178bool llvm::sys::RemoveFileOnSignal(const sys::Path &Filename,
179 std::string* ErrMsg) {
Owen Anderson6ae8c732009-08-17 17:07:22 +0000180 SignalsMutex.acquire();
Jeffrey Yasskinb7ccf752010-03-17 07:08:12 +0000181 FilesToRemove.push_back(Filename);
Chris Lattner35033a52009-03-04 21:21:36 +0000182
Owen Anderson6ae8c732009-08-17 17:07:22 +0000183 SignalsMutex.release();
184
Chris Lattner1c4d8a02009-03-23 05:42:29 +0000185 RegisterHandlers();
Chris Lattner35033a52009-03-04 21:21:36 +0000186 return false;
187}
188
Dan Gohman41154112010-09-01 14:17:34 +0000189// DontRemoveFileOnSignal - The public API
190void llvm::sys::DontRemoveFileOnSignal(const sys::Path &Filename) {
191 SignalsMutex.acquire();
192 std::vector<sys::Path>::reverse_iterator I =
193 std::find(FilesToRemove.rbegin(), FilesToRemove.rend(), Filename);
194 if (I != FilesToRemove.rend())
195 FilesToRemove.erase(I.base()-1);
196 SignalsMutex.release();
197}
198
Chris Lattner35033a52009-03-04 21:21:36 +0000199/// AddSignalHandler - Add a function to be called when a signal is delivered
200/// to the process. The handler can have a cookie passed to it to identify
201/// what instance of the handler it is.
Chris Lattnerf48ba7b2009-03-08 19:13:45 +0000202void llvm::sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) {
Jeffrey Yasskinb7ccf752010-03-17 07:08:12 +0000203 CallBacksToRun.push_back(std::make_pair(FnPtr, Cookie));
Chris Lattner1c4d8a02009-03-23 05:42:29 +0000204 RegisterHandlers();
Chris Lattner35033a52009-03-04 21:21:36 +0000205}
206
Reid Spencer496c2772004-08-29 19:22:48 +0000207
208// PrintStackTrace - In the case of a program crash or fault, print out a stack
209// trace so that the user has an indication of why and where we died.
210//
211// On glibc systems we have the 'backtrace' function, which works nicely, but
Michael J. Spencer1f6efa32010-11-29 18:16:10 +0000212// doesn't demangle symbols.
Chris Lattner35033a52009-03-04 21:21:36 +0000213static void PrintStackTrace(void *) {
Reid Spencer496c2772004-08-29 19:22:48 +0000214#ifdef HAVE_BACKTRACE
Chris Lattner35033a52009-03-04 21:21:36 +0000215 static void* StackTrace[256];
Reid Spencer496c2772004-08-29 19:22:48 +0000216 // Use backtrace() to output a backtrace on Linux systems with glibc.
Evan Cheng34cd4a42008-05-05 18:30:58 +0000217 int depth = backtrace(StackTrace,
218 static_cast<int>(array_lengthof(StackTrace)));
Dan Gohman56d9b6d2008-12-05 20:12:48 +0000219#if HAVE_DLFCN_H && __GNUG__
220 int width = 0;
221 for (int i = 0; i < depth; ++i) {
222 Dl_info dlinfo;
223 dladdr(StackTrace[i], &dlinfo);
Dan Gohmand4e18452009-02-10 17:56:28 +0000224 const char* name = strrchr(dlinfo.dli_fname, '/');
Dan Gohman56d9b6d2008-12-05 20:12:48 +0000225
226 int nwidth;
227 if (name == NULL) nwidth = strlen(dlinfo.dli_fname);
228 else nwidth = strlen(name) - 1;
229
230 if (nwidth > width) width = nwidth;
231 }
232
233 for (int i = 0; i < depth; ++i) {
234 Dl_info dlinfo;
235 dladdr(StackTrace[i], &dlinfo);
236
Dan Gohman0b81e192009-10-30 02:45:10 +0000237 fprintf(stderr, "%-2d", i);
Dan Gohman56d9b6d2008-12-05 20:12:48 +0000238
Dan Gohmand4e18452009-02-10 17:56:28 +0000239 const char* name = strrchr(dlinfo.dli_fname, '/');
Dan Gohman56d9b6d2008-12-05 20:12:48 +0000240 if (name == NULL) fprintf(stderr, " %-*s", width, dlinfo.dli_fname);
241 else fprintf(stderr, " %-*s", width, name+1);
242
Dan Gohmandd17b252008-12-05 23:39:24 +0000243 fprintf(stderr, " %#0*lx",
244 (int)(sizeof(void*) * 2) + 2, (unsigned long)StackTrace[i]);
Dan Gohman56d9b6d2008-12-05 20:12:48 +0000245
246 if (dlinfo.dli_sname != NULL) {
247 int res;
248 fputc(' ', stderr);
249 char* d = abi::__cxa_demangle(dlinfo.dli_sname, NULL, NULL, &res);
250 if (d == NULL) fputs(dlinfo.dli_sname, stderr);
251 else fputs(d, stderr);
252 free(d);
253
254 fprintf(stderr, " + %tu",(char*)StackTrace[i]-(char*)dlinfo.dli_saddr);
255 }
256 fputc('\n', stderr);
257 }
258#else
Lauro Ramos Venancio2e78b782008-02-15 18:05:54 +0000259 backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
Reid Spencer496c2772004-08-29 19:22:48 +0000260#endif
Dan Gohman56d9b6d2008-12-05 20:12:48 +0000261#endif
Reid Spencer496c2772004-08-29 19:22:48 +0000262}
263
Reid Spencer496c2772004-08-29 19:22:48 +0000264/// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
265/// SIGSEGV) is delivered to the process, print a stack trace and then exit.
Chris Lattnerf48ba7b2009-03-08 19:13:45 +0000266void llvm::sys::PrintStackTraceOnErrorSignal() {
Chris Lattner35033a52009-03-04 21:21:36 +0000267 AddSignalHandler(PrintStackTrace, 0);
Argyrios Kyrtzidis08713b32012-01-11 20:53:25 +0000268
269#if defined(__APPLE__)
270 // Environment variable to disable any kind of crash dialog.
271 if (getenv("LLVM_DISABLE_CRASH_REPORT")) {
272 mach_port_t self = mach_task_self();
273
274 exception_mask_t mask = EXC_MASK_CRASH;
275
276 kern_return_t ret = task_set_exception_ports(self,
277 mask,
Jean-Daniel Dupas300361a2012-03-24 22:17:50 +0000278 MACH_PORT_NULL,
Argyrios Kyrtzidis08713b32012-01-11 20:53:25 +0000279 EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES,
Jean-Daniel Dupas300361a2012-03-24 22:17:50 +0000280 THREAD_STATE_NONE);
Argyrios Kyrtzidis08713b32012-01-11 20:53:25 +0000281 (void)ret;
282 }
283#endif
Reid Spencer496c2772004-08-29 19:22:48 +0000284}
Chris Lattner35033a52009-03-04 21:21:36 +0000285
Daniel Dunbarb08ceb82010-08-19 23:45:39 +0000286
287/***/
288
289// On Darwin, raise sends a signal to the main thread instead of the current
290// thread. This has the unfortunate effect that assert() and abort() will end up
291// bypassing our crash recovery attempts. We work around this for anything in
292// the same linkage unit by just defining our own versions of the assert handler
293// and abort.
294
295#ifdef __APPLE__
296
Douglas Gregor72d30f62011-04-29 16:12:17 +0000297#include <signal.h>
298#include <pthread.h>
299
Daniel Dunbare4e06a82010-09-22 17:46:10 +0000300int raise(int sig) {
Daniel Dunbar95b46722010-10-08 18:31:34 +0000301 return pthread_kill(pthread_self(), sig);
Daniel Dunbare4e06a82010-09-22 17:46:10 +0000302}
303
Daniel Dunbarb08ceb82010-08-19 23:45:39 +0000304void __assert_rtn(const char *func,
305 const char *file,
306 int line,
307 const char *expr) {
308 if (func)
309 fprintf(stderr, "Assertion failed: (%s), function %s, file %s, line %d.\n",
310 expr, func, file, line);
311 else
312 fprintf(stderr, "Assertion failed: (%s), file %s, line %d.\n",
313 expr, file, line);
314 abort();
315}
316
Daniel Dunbarb08ceb82010-08-19 23:45:39 +0000317void abort() {
Daniel Dunbare4e06a82010-09-22 17:46:10 +0000318 raise(SIGABRT);
Daniel Dunbarb08ceb82010-08-19 23:45:39 +0000319 usleep(1000);
320 __builtin_trap();
321}
322
323#endif