blob: 592674e95dbcde1258f06d727411a00adade46f1 [file] [log] [blame]
Reid Spencer3d7a6142004-08-29 19:22:48 +00001//===- Signals.cpp - Generic Unix Signals Implementation -----*- C++ -*-===//
Michael J. Spencer447762d2010-11-29 18:16:10 +00002//
Reid Spencer3d7a6142004-08-29 19:22:48 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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. Spencer447762d2010-11-29 18:16:10 +00007//
Reid Spencer3d7a6142004-08-29 19:22:48 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines some helpful functions for dealing with the possibility of
Chris Lattner0ab5e2c2011-04-15 05:18:47 +000011// Unix signals occurring while your program is running.
Reid Spencer3d7a6142004-08-29 19:22:48 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "Unix.h"
Owen Andersone2f23a32007-09-07 04:06:50 +000016#include "llvm/ADT/STLExtras.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000017#include "llvm/Support/Mutex.h"
Dylan Noblesmithc4c51802014-08-23 23:07:14 +000018#include "llvm/Support/UniqueLock.h"
Chris Bieneman186e7d12014-09-02 23:48:13 +000019#include "llvm/Support/ManagedStatic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include <algorithm>
Chandler Carruthe6196eb2012-06-16 00:09:41 +000021#include <string>
Reid Spencer3d7a6142004-08-29 19:22:48 +000022#include <vector>
Reid Spencerd554bbc2004-12-27 06:16:52 +000023#if HAVE_EXECINFO_H
Reid Spencer3d7a6142004-08-29 19:22:48 +000024# include <execinfo.h> // For backtrace().
25#endif
Reid Spencerd554bbc2004-12-27 06:16:52 +000026#if HAVE_SIGNAL_H
Reid Spencer3d7a6142004-08-29 19:22:48 +000027#include <signal.h>
Reid Spencerd554bbc2004-12-27 06:16:52 +000028#endif
Reid Spencerceeb9182007-04-07 18:52:17 +000029#if HAVE_SYS_STAT_H
30#include <sys/stat.h>
31#endif
Joerg Sonnenberger66241832013-04-27 22:12:32 +000032#if HAVE_CXXABI_H
Joerg Sonnenberger44744092013-04-27 22:32:54 +000033#include <cxxabi.h>
Joerg Sonnenberger66241832013-04-27 22:12:32 +000034#endif
35#if HAVE_DLFCN_H
Dan Gohman7f079aa2008-12-05 20:12:48 +000036#include <dlfcn.h>
Dan Gohman7f079aa2008-12-05 20:12:48 +000037#endif
Argyrios Kyrtzidiscd8fe082012-01-11 20:53:25 +000038#if HAVE_MACH_MACH_H
39#include <mach/mach.h>
40#endif
41
Chris Lattner80df7c42006-08-01 17:59:14 +000042using namespace llvm;
Reid Spencer3d7a6142004-08-29 19:22:48 +000043
Chris Lattnerf299a682009-03-23 05:42:29 +000044static RETSIGTYPE SignalHandler(int Sig); // defined below.
45
Chris Bieneman186e7d12014-09-02 23:48:13 +000046static ManagedStatic<SmartMutex<true> > SignalsMutex;
Owen Anderson820739d2009-08-17 17:07:22 +000047
Chris Lattner6a5d6ec2005-08-02 02:14:22 +000048/// InterruptFunction - The function to call if ctrl-c is pressed.
Craig Toppere73658d2014-04-28 04:05:08 +000049static void (*InterruptFunction)() = nullptr;
Chris Lattner6a5d6ec2005-08-02 02:14:22 +000050
Chris Bieneman186e7d12014-09-02 23:48:13 +000051static ManagedStatic<std::vector<std::string>> FilesToRemove;
52static ManagedStatic<std::vector<std::pair<void (*)(void *), void *>>>
53 CallBacksToRun;
Reid Spencer3d7a6142004-08-29 19:22:48 +000054
Dan Gohmanf857cd72013-02-20 19:28:46 +000055// IntSigs - Signals that represent requested termination. There's no bug
56// or failure, or if there is, it's not our direct responsibility. For whatever
57// reason, our continued execution is no longer desirable.
Dan Gohmanf4bc7822008-04-10 21:11:47 +000058static const int IntSigs[] = {
Dan Gohman5cdb3452013-02-20 19:15:01 +000059 SIGHUP, SIGINT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
Reid Spencer3d7a6142004-08-29 19:22:48 +000060};
Reid Spencer3d7a6142004-08-29 19:22:48 +000061
Dan Gohmanf857cd72013-02-20 19:28:46 +000062// KillSigs - Signals that represent that we have a bug, and our prompt
63// termination has been ordered.
Dan Gohmanf4bc7822008-04-10 21:11:47 +000064static const int KillSigs[] = {
Dan Gohman5cdb3452013-02-20 19:15:01 +000065 SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGQUIT
Chris Lattner62f50da2010-02-12 00:37:46 +000066#ifdef SIGSYS
67 , SIGSYS
68#endif
69#ifdef SIGXCPU
70 , SIGXCPU
71#endif
Chris Lattner3b38fd62010-02-14 18:20:09 +000072#ifdef SIGXFSZ
Chris Lattner62f50da2010-02-12 00:37:46 +000073 , SIGXFSZ
74#endif
Reid Spencer3d7a6142004-08-29 19:22:48 +000075#ifdef SIGEMT
76 , SIGEMT
77#endif
78};
Reid Spencer3d7a6142004-08-29 19:22:48 +000079
Chris Lattnerfb954722009-03-23 05:55:36 +000080static unsigned NumRegisteredSignals = 0;
81static struct {
82 struct sigaction SA;
83 int SigNo;
84} RegisteredSignalInfo[(sizeof(IntSigs)+sizeof(KillSigs))/sizeof(KillSigs[0])];
85
86
Chris Lattnerf299a682009-03-23 05:42:29 +000087static void RegisterHandler(int Signal) {
Craig Topper26b45c22013-07-15 04:37:54 +000088 assert(NumRegisteredSignals <
89 sizeof(RegisteredSignalInfo)/sizeof(RegisteredSignalInfo[0]) &&
Chris Lattnerfb954722009-03-23 05:55:36 +000090 "Out of space for signal handlers!");
91
92 struct sigaction NewHandler;
Michael J. Spencer447762d2010-11-29 18:16:10 +000093
Chris Lattnerfb954722009-03-23 05:55:36 +000094 NewHandler.sa_handler = SignalHandler;
95 NewHandler.sa_flags = SA_NODEFER|SA_RESETHAND;
Michael J. Spencer447762d2010-11-29 18:16:10 +000096 sigemptyset(&NewHandler.sa_mask);
97
Chris Lattnerfb954722009-03-23 05:55:36 +000098 // Install the new handler, save the old one in RegisteredSignalInfo.
99 sigaction(Signal, &NewHandler,
100 &RegisteredSignalInfo[NumRegisteredSignals].SA);
101 RegisteredSignalInfo[NumRegisteredSignals].SigNo = Signal;
102 ++NumRegisteredSignals;
Chris Lattner6acb4d62009-03-07 08:15:47 +0000103}
104
Chris Lattnerf299a682009-03-23 05:42:29 +0000105static void RegisterHandlers() {
Chris Lattnerfb954722009-03-23 05:55:36 +0000106 // If the handlers are already registered, we're done.
107 if (NumRegisteredSignals != 0) return;
108
Chris Bienemanb1cd51e2014-08-29 01:05:16 +0000109 for (auto S : IntSigs) RegisterHandler(S);
110 for (auto S : KillSigs) RegisterHandler(S);
Chris Lattnerf299a682009-03-23 05:42:29 +0000111}
112
Chris Lattnerf299a682009-03-23 05:42:29 +0000113static void UnregisterHandlers() {
Chris Lattnerfb954722009-03-23 05:55:36 +0000114 // Restore all of the signal handlers to how they were before we showed up.
115 for (unsigned i = 0, e = NumRegisteredSignals; i != e; ++i)
Chris Lattnerf2b60652009-03-23 06:46:20 +0000116 sigaction(RegisteredSignalInfo[i].SigNo,
Craig Toppere73658d2014-04-28 04:05:08 +0000117 &RegisteredSignalInfo[i].SA, nullptr);
Chris Lattnerfb954722009-03-23 05:55:36 +0000118 NumRegisteredSignals = 0;
Chris Lattnerf299a682009-03-23 05:42:29 +0000119}
120
121
Dan Gohman288999b2010-05-27 23:11:55 +0000122/// RemoveFilesToRemove - Process the FilesToRemove list. This function
123/// should be called with the SignalsMutex lock held.
Chandler Carruthe6196eb2012-06-16 00:09:41 +0000124/// NB: This must be an async signal safe function. It cannot allocate or free
125/// memory, even in debug builds.
Dan Gohman288999b2010-05-27 23:11:55 +0000126static void RemoveFilesToRemove() {
Daniel Dunbar511479d2012-10-17 16:30:54 +0000127 // We avoid iterators in case of debug iterators that allocate or release
Chandler Carruthe6196eb2012-06-16 00:09:41 +0000128 // memory.
Chris Bieneman186e7d12014-09-02 23:48:13 +0000129 std::vector<std::string>& FilesToRemoveRef = *FilesToRemove;
130 for (unsigned i = 0, e = FilesToRemoveRef.size(); i != e; ++i) {
Daniel Dunbar511479d2012-10-17 16:30:54 +0000131 // We rely on a std::string implementation for which repeated calls to
132 // 'c_str()' don't allocate memory. We pre-call 'c_str()' on all of these
133 // strings to try to ensure this is safe.
Chris Bieneman186e7d12014-09-02 23:48:13 +0000134 const char *path = FilesToRemoveRef[i].c_str();
Daniel Dunbar511479d2012-10-17 16:30:54 +0000135
136 // Get the status so we can determine if it's a file or directory. If we
137 // can't stat the file, ignore it.
138 struct stat buf;
139 if (stat(path, &buf) != 0)
140 continue;
141
142 // If this is not a regular file, ignore it. We want to prevent removal of
143 // special files like /dev/null, even if the compiler is being run with the
144 // super-user permissions.
145 if (!S_ISREG(buf.st_mode))
146 continue;
147
148 // Otherwise, remove the file. We ignore any errors here as there is nothing
149 // else we can do.
150 unlink(path);
Dan Gohman288999b2010-05-27 23:11:55 +0000151 }
152}
Chris Lattner6acb4d62009-03-07 08:15:47 +0000153
154// SignalHandler - The signal handler that runs.
Chris Lattner4fdd0422009-03-04 21:21:36 +0000155static RETSIGTYPE SignalHandler(int Sig) {
Chris Lattnerbbbbbf32009-03-05 18:22:14 +0000156 // Restore the signal behavior to default, so that the program actually
157 // crashes when we return and the signal reissues. This also ensures that if
158 // we crash in our signal handler that the program will terminate immediately
159 // instead of recursing in the signal handler.
Chris Lattnerf299a682009-03-23 05:42:29 +0000160 UnregisterHandlers();
Chris Lattner6acb4d62009-03-07 08:15:47 +0000161
162 // Unmask all potentially blocked kill signals.
163 sigset_t SigMask;
164 sigfillset(&SigMask);
Craig Toppere73658d2014-04-28 04:05:08 +0000165 sigprocmask(SIG_UNBLOCK, &SigMask, nullptr);
Chris Lattnerbbbbbf32009-03-05 18:22:14 +0000166
Dylan Noblesmithc4c51802014-08-23 23:07:14 +0000167 {
Chris Bieneman186e7d12014-09-02 23:48:13 +0000168 unique_lock<SmartMutex<true>> Guard(*SignalsMutex);
Dylan Noblesmithc4c51802014-08-23 23:07:14 +0000169 RemoveFilesToRemove();
Chris Lattner4fdd0422009-03-04 21:21:36 +0000170
Chris Bienemanb1cd51e2014-08-29 01:05:16 +0000171 if (std::find(std::begin(IntSigs), std::end(IntSigs), Sig)
172 != std::end(IntSigs)) {
Dylan Noblesmithc4c51802014-08-23 23:07:14 +0000173 if (InterruptFunction) {
174 void (*IF)() = InterruptFunction;
175 Guard.unlock();
176 InterruptFunction = nullptr;
177 IF(); // run the interrupt function.
178 return;
179 }
180
181 Guard.unlock();
182 raise(Sig); // Execute the default handler.
Chris Lattner4fdd0422009-03-04 21:21:36 +0000183 return;
Dylan Noblesmithc4c51802014-08-23 23:07:14 +0000184 }
Chris Lattner4fdd0422009-03-04 21:21:36 +0000185 }
186
187 // Otherwise if it is a fault (like SEGV) run any handler.
Chris Bieneman186e7d12014-09-02 23:48:13 +0000188 std::vector<std::pair<void (*)(void *), void *>>& CallBacksToRunRef =
189 *CallBacksToRun;
190 for (unsigned i = 0, e = CallBacksToRun->size(); i != e; ++i)
191 CallBacksToRunRef[i].first(CallBacksToRunRef[i].second);
Ulrich Weigand90c9abd2013-05-03 12:22:11 +0000192
193#ifdef __s390__
194 // On S/390, certain signals are delivered with PSW Address pointing to
195 // *after* the faulting instruction. Simply returning from the signal
196 // handler would continue execution after that point, instead of
197 // re-raising the signal. Raise the signal manually in those cases.
198 if (Sig == SIGILL || Sig == SIGFPE || Sig == SIGTRAP)
199 raise(Sig);
200#endif
Chris Lattner4fdd0422009-03-04 21:21:36 +0000201}
202
Daniel Dunbar68272562010-05-08 02:10:34 +0000203void llvm::sys::RunInterruptHandlers() {
Chris Bieneman186e7d12014-09-02 23:48:13 +0000204 sys::SmartScopedLock<true> Guard(*SignalsMutex);
Dan Gohman288999b2010-05-27 23:11:55 +0000205 RemoveFilesToRemove();
Daniel Dunbar68272562010-05-08 02:10:34 +0000206}
Chris Lattner4fdd0422009-03-04 21:21:36 +0000207
Chris Lattnerbb1ba7b2009-03-08 19:13:45 +0000208void llvm::sys::SetInterruptFunction(void (*IF)()) {
Dylan Noblesmith4704ffe2014-08-23 22:49:17 +0000209 {
Chris Bieneman186e7d12014-09-02 23:48:13 +0000210 sys::SmartScopedLock<true> Guard(*SignalsMutex);
Dylan Noblesmith4704ffe2014-08-23 22:49:17 +0000211 InterruptFunction = IF;
212 }
Chris Lattnerf299a682009-03-23 05:42:29 +0000213 RegisterHandlers();
Chris Lattner4fdd0422009-03-04 21:21:36 +0000214}
215
216// RemoveFileOnSignal - The public API
Rafael Espindola4f35da72013-06-13 21:16:58 +0000217bool llvm::sys::RemoveFileOnSignal(StringRef Filename,
Chris Lattnerbb1ba7b2009-03-08 19:13:45 +0000218 std::string* ErrMsg) {
Dylan Noblesmith4704ffe2014-08-23 22:49:17 +0000219 {
Chris Bieneman186e7d12014-09-02 23:48:13 +0000220 sys::SmartScopedLock<true> Guard(*SignalsMutex);
221 std::vector<std::string>& FilesToRemoveRef = *FilesToRemove;
222 std::string *OldPtr =
223 FilesToRemoveRef.empty() ? nullptr : &FilesToRemoveRef[0];
224 FilesToRemoveRef.push_back(Filename);
Chandler Carruthe6196eb2012-06-16 00:09:41 +0000225
Dylan Noblesmith4704ffe2014-08-23 22:49:17 +0000226 // We want to call 'c_str()' on every std::string in this vector so that if
227 // the underlying implementation requires a re-allocation, it happens here
228 // rather than inside of the signal handler. If we see the vector grow, we
229 // have to call it on every entry. If it remains in place, we only need to
230 // call it on the latest one.
Chris Bieneman186e7d12014-09-02 23:48:13 +0000231 if (OldPtr == &FilesToRemoveRef[0])
232 FilesToRemoveRef.back().c_str();
Dylan Noblesmith4704ffe2014-08-23 22:49:17 +0000233 else
Chris Bieneman186e7d12014-09-02 23:48:13 +0000234 for (unsigned i = 0, e = FilesToRemoveRef.size(); i != e; ++i)
235 FilesToRemoveRef[i].c_str();
Dylan Noblesmith4704ffe2014-08-23 22:49:17 +0000236 }
Owen Anderson820739d2009-08-17 17:07:22 +0000237
Chris Lattnerf299a682009-03-23 05:42:29 +0000238 RegisterHandlers();
Chris Lattner4fdd0422009-03-04 21:21:36 +0000239 return false;
240}
241
Dan Gohmane201c072010-09-01 14:17:34 +0000242// DontRemoveFileOnSignal - The public API
Rafael Espindola4f35da72013-06-13 21:16:58 +0000243void llvm::sys::DontRemoveFileOnSignal(StringRef Filename) {
Chris Bieneman186e7d12014-09-02 23:48:13 +0000244 sys::SmartScopedLock<true> Guard(*SignalsMutex);
Chandler Carruthe6196eb2012-06-16 00:09:41 +0000245 std::vector<std::string>::reverse_iterator RI =
Chris Bieneman186e7d12014-09-02 23:48:13 +0000246 std::find(FilesToRemove->rbegin(), FilesToRemove->rend(), Filename);
247 std::vector<std::string>::iterator I = FilesToRemove->end();
248 if (RI != FilesToRemove->rend())
249 I = FilesToRemove->erase(RI.base()-1);
Chandler Carruthe6196eb2012-06-16 00:09:41 +0000250
251 // We need to call c_str() on every element which would have been moved by
252 // the erase. These elements, in a C++98 implementation where c_str()
253 // requires a reallocation on the first call may have had the call to c_str()
254 // made on insertion become invalid by being copied down an element.
Chris Bieneman186e7d12014-09-02 23:48:13 +0000255 for (std::vector<std::string>::iterator E = FilesToRemove->end(); I != E; ++I)
Chandler Carruthe6196eb2012-06-16 00:09:41 +0000256 I->c_str();
Dan Gohmane201c072010-09-01 14:17:34 +0000257}
258
Chris Lattner4fdd0422009-03-04 21:21:36 +0000259/// AddSignalHandler - Add a function to be called when a signal is delivered
260/// to the process. The handler can have a cookie passed to it to identify
261/// what instance of the handler it is.
Chris Lattnerbb1ba7b2009-03-08 19:13:45 +0000262void llvm::sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) {
Chris Bieneman186e7d12014-09-02 23:48:13 +0000263 CallBacksToRun->push_back(std::make_pair(FnPtr, Cookie));
Chris Lattnerf299a682009-03-23 05:42:29 +0000264 RegisterHandlers();
Chris Lattner4fdd0422009-03-04 21:21:36 +0000265}
266
Reid Spencer3d7a6142004-08-29 19:22:48 +0000267
268// PrintStackTrace - In the case of a program crash or fault, print out a stack
269// trace so that the user has an indication of why and where we died.
270//
271// On glibc systems we have the 'backtrace' function, which works nicely, but
Michael J. Spencer447762d2010-11-29 18:16:10 +0000272// doesn't demangle symbols.
Argyrios Kyrtzidiseb9ae762013-01-09 19:42:40 +0000273void llvm::sys::PrintStackTrace(FILE *FD) {
Benjamin Kramer5651cbd2012-09-28 10:10:46 +0000274#if defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES)
Chris Lattner4fdd0422009-03-04 21:21:36 +0000275 static void* StackTrace[256];
Reid Spencer3d7a6142004-08-29 19:22:48 +0000276 // Use backtrace() to output a backtrace on Linux systems with glibc.
Evan Cheng86cb3182008-05-05 18:30:58 +0000277 int depth = backtrace(StackTrace,
278 static_cast<int>(array_lengthof(StackTrace)));
Dan Gohman7f079aa2008-12-05 20:12:48 +0000279#if HAVE_DLFCN_H && __GNUG__
280 int width = 0;
281 for (int i = 0; i < depth; ++i) {
282 Dl_info dlinfo;
283 dladdr(StackTrace[i], &dlinfo);
Dan Gohman1f517dd2009-02-10 17:56:28 +0000284 const char* name = strrchr(dlinfo.dli_fname, '/');
Dan Gohman7f079aa2008-12-05 20:12:48 +0000285
286 int nwidth;
Craig Toppere73658d2014-04-28 04:05:08 +0000287 if (!name) nwidth = strlen(dlinfo.dli_fname);
288 else nwidth = strlen(name) - 1;
Dan Gohman7f079aa2008-12-05 20:12:48 +0000289
290 if (nwidth > width) width = nwidth;
291 }
292
293 for (int i = 0; i < depth; ++i) {
294 Dl_info dlinfo;
295 dladdr(StackTrace[i], &dlinfo);
296
Argyrios Kyrtzidiseb9ae762013-01-09 19:42:40 +0000297 fprintf(FD, "%-2d", i);
Dan Gohman7f079aa2008-12-05 20:12:48 +0000298
Dan Gohman1f517dd2009-02-10 17:56:28 +0000299 const char* name = strrchr(dlinfo.dli_fname, '/');
Craig Toppere73658d2014-04-28 04:05:08 +0000300 if (!name) fprintf(FD, " %-*s", width, dlinfo.dli_fname);
301 else fprintf(FD, " %-*s", width, name+1);
Dan Gohman7f079aa2008-12-05 20:12:48 +0000302
Argyrios Kyrtzidiseb9ae762013-01-09 19:42:40 +0000303 fprintf(FD, " %#0*lx",
Dan Gohman35e2cfc2008-12-05 23:39:24 +0000304 (int)(sizeof(void*) * 2) + 2, (unsigned long)StackTrace[i]);
Dan Gohman7f079aa2008-12-05 20:12:48 +0000305
Craig Toppere73658d2014-04-28 04:05:08 +0000306 if (dlinfo.dli_sname != nullptr) {
Argyrios Kyrtzidiseb9ae762013-01-09 19:42:40 +0000307 fputc(' ', FD);
Joerg Sonnenberger66241832013-04-27 22:12:32 +0000308# if HAVE_CXXABI_H
Benjamin Kramer83e2a442013-04-28 07:47:04 +0000309 int res;
Craig Toppere73658d2014-04-28 04:05:08 +0000310 char* d = abi::__cxa_demangle(dlinfo.dli_sname, nullptr, nullptr, &res);
Joerg Sonnenberger66241832013-04-27 22:12:32 +0000311# else
312 char* d = NULL;
313# endif
Craig Toppere73658d2014-04-28 04:05:08 +0000314 if (!d) fputs(dlinfo.dli_sname, FD);
315 else fputs(d, FD);
Dan Gohman7f079aa2008-12-05 20:12:48 +0000316 free(d);
317
Edwin Vane44338e02013-01-28 19:34:42 +0000318 // FIXME: When we move to C++11, use %t length modifier. It's not in
319 // C++03 and causes gcc to issue warnings. Losing the upper 32 bits of
320 // the stack offset for a stack dump isn't likely to cause any problems.
321 fprintf(FD, " + %u",(unsigned)((char*)StackTrace[i]-
322 (char*)dlinfo.dli_saddr));
Dan Gohman7f079aa2008-12-05 20:12:48 +0000323 }
Argyrios Kyrtzidiseb9ae762013-01-09 19:42:40 +0000324 fputc('\n', FD);
Dan Gohman7f079aa2008-12-05 20:12:48 +0000325 }
326#else
Lauro Ramos Venancioeab51d32008-02-15 18:05:54 +0000327 backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
Reid Spencer3d7a6142004-08-29 19:22:48 +0000328#endif
Dan Gohman7f079aa2008-12-05 20:12:48 +0000329#endif
Reid Spencer3d7a6142004-08-29 19:22:48 +0000330}
331
Argyrios Kyrtzidiseb9ae762013-01-09 19:42:40 +0000332static void PrintStackTraceSignalHandler(void *) {
333 PrintStackTrace(stderr);
334}
335
NAKAMURA Takumi8a54d812012-09-06 03:01:43 +0000336/// PrintStackTraceOnErrorSignal - When an error signal (such as SIGABRT or
Reid Spencer3d7a6142004-08-29 19:22:48 +0000337/// SIGSEGV) is delivered to the process, print a stack trace and then exit.
Chris Lattnerbb1ba7b2009-03-08 19:13:45 +0000338void llvm::sys::PrintStackTraceOnErrorSignal() {
Craig Toppere73658d2014-04-28 04:05:08 +0000339 AddSignalHandler(PrintStackTraceSignalHandler, nullptr);
Argyrios Kyrtzidiscd8fe082012-01-11 20:53:25 +0000340
Daniel Dunbareb6c7082013-08-30 20:39:21 +0000341#if defined(__APPLE__) && defined(ENABLE_CRASH_OVERRIDES)
Argyrios Kyrtzidiscd8fe082012-01-11 20:53:25 +0000342 // Environment variable to disable any kind of crash dialog.
343 if (getenv("LLVM_DISABLE_CRASH_REPORT")) {
344 mach_port_t self = mach_task_self();
345
346 exception_mask_t mask = EXC_MASK_CRASH;
347
NAKAMURA Takumiffa15712012-09-06 03:02:56 +0000348 kern_return_t ret = task_set_exception_ports(self,
Argyrios Kyrtzidiscd8fe082012-01-11 20:53:25 +0000349 mask,
Jean-Daniel Dupasa573b222012-03-24 22:17:50 +0000350 MACH_PORT_NULL,
NAKAMURA Takumiffa15712012-09-06 03:02:56 +0000351 EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES,
Jean-Daniel Dupasa573b222012-03-24 22:17:50 +0000352 THREAD_STATE_NONE);
Argyrios Kyrtzidiscd8fe082012-01-11 20:53:25 +0000353 (void)ret;
354 }
355#endif
Reid Spencer3d7a6142004-08-29 19:22:48 +0000356}
Chris Lattner4fdd0422009-03-04 21:21:36 +0000357
Daniel Dunbarf14d9462010-08-19 23:45:39 +0000358
359/***/
360
361// On Darwin, raise sends a signal to the main thread instead of the current
362// thread. This has the unfortunate effect that assert() and abort() will end up
363// bypassing our crash recovery attempts. We work around this for anything in
364// the same linkage unit by just defining our own versions of the assert handler
365// and abort.
366
Daniel Dunbareb6c7082013-08-30 20:39:21 +0000367#if defined(__APPLE__) && defined(ENABLE_CRASH_OVERRIDES)
Daniel Dunbarf14d9462010-08-19 23:45:39 +0000368
Douglas Gregor0e506822011-04-29 16:12:17 +0000369#include <signal.h>
370#include <pthread.h>
371
Daniel Dunbar18456f32010-09-22 17:46:10 +0000372int raise(int sig) {
Daniel Dunbarcc0e18d2010-10-08 18:31:34 +0000373 return pthread_kill(pthread_self(), sig);
Daniel Dunbar18456f32010-09-22 17:46:10 +0000374}
375
Daniel Dunbarf14d9462010-08-19 23:45:39 +0000376void __assert_rtn(const char *func,
377 const char *file,
378 int line,
379 const char *expr) {
380 if (func)
381 fprintf(stderr, "Assertion failed: (%s), function %s, file %s, line %d.\n",
382 expr, func, file, line);
383 else
384 fprintf(stderr, "Assertion failed: (%s), file %s, line %d.\n",
385 expr, file, line);
386 abort();
387}
388
Daniel Dunbarf14d9462010-08-19 23:45:39 +0000389void abort() {
Daniel Dunbar18456f32010-09-22 17:46:10 +0000390 raise(SIGABRT);
Daniel Dunbarf14d9462010-08-19 23:45:39 +0000391 usleep(1000);
392 __builtin_trap();
393}
394
395#endif