blob: 061cdb3da216e4aaa08c415f95c99948683c3bd2 [file] [log] [blame]
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +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"
Zachary Turnercd132c92015-03-05 19:10:52 +000017#include "llvm/Support/Format.h"
Alexey Samsonov8a584bb2014-10-10 22:06:59 +000018#include "llvm/Support/FileSystem.h"
19#include "llvm/Support/FileUtilities.h"
Alexey Samsonov8a584bb2014-10-10 22:06:59 +000020#include "llvm/Support/MemoryBuffer.h"
21#include "llvm/Support/Mutex.h"
22#include "llvm/Support/Program.h"
23#include "llvm/Support/UniqueLock.h"
24#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include <algorithm>
Chandler Carruthe6196eb2012-06-16 00:09:41 +000026#include <string>
Reid Spencerd554bbc2004-12-27 06:16:52 +000027#if HAVE_EXECINFO_H
Reid Spencer3d7a6142004-08-29 19:22:48 +000028# include <execinfo.h> // For backtrace().
29#endif
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000030#if HAVE_SIGNAL_H
31#include <signal.h>
32#endif
Reid Spencerceeb9182007-04-07 18:52:17 +000033#if HAVE_SYS_STAT_H
34#include <sys/stat.h>
35#endif
Joerg Sonnenberger66241832013-04-27 22:12:32 +000036#if HAVE_CXXABI_H
Joerg Sonnenberger44744092013-04-27 22:32:54 +000037#include <cxxabi.h>
Joerg Sonnenberger66241832013-04-27 22:12:32 +000038#endif
39#if HAVE_DLFCN_H
Dan Gohman7f079aa2008-12-05 20:12:48 +000040#include <dlfcn.h>
Dan Gohman7f079aa2008-12-05 20:12:48 +000041#endif
Argyrios Kyrtzidiscd8fe082012-01-11 20:53:25 +000042#if HAVE_MACH_MACH_H
43#include <mach/mach.h>
44#endif
Alexey Samsonov8a584bb2014-10-10 22:06:59 +000045#if HAVE_LINK_H
46#include <link.h>
47#endif
Argyrios Kyrtzidiscd8fe082012-01-11 20:53:25 +000048
Chris Lattner80df7c42006-08-01 17:59:14 +000049using namespace llvm;
Reid Spencer3d7a6142004-08-29 19:22:48 +000050
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000051static RETSIGTYPE SignalHandler(int Sig); // defined below.
Chris Lattnerf299a682009-03-23 05:42:29 +000052
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000053static ManagedStatic<SmartMutex<true> > SignalsMutex;
Owen Anderson820739d2009-08-17 17:07:22 +000054
Chris Lattner6a5d6ec2005-08-02 02:14:22 +000055/// InterruptFunction - The function to call if ctrl-c is pressed.
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000056static void (*InterruptFunction)() = nullptr;
Chris Lattner6a5d6ec2005-08-02 02:14:22 +000057
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000058static ManagedStatic<std::vector<std::string>> FilesToRemove;
Reid Spencer3d7a6142004-08-29 19:22:48 +000059
Dan Gohmanf857cd72013-02-20 19:28:46 +000060// IntSigs - Signals that represent requested termination. There's no bug
61// or failure, or if there is, it's not our direct responsibility. For whatever
62// reason, our continued execution is no longer desirable.
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000063static const int IntSigs[] = {
Dan Gohman5cdb3452013-02-20 19:15:01 +000064 SIGHUP, SIGINT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
Reid Spencer3d7a6142004-08-29 19:22:48 +000065};
Reid Spencer3d7a6142004-08-29 19:22:48 +000066
Dan Gohmanf857cd72013-02-20 19:28:46 +000067// KillSigs - Signals that represent that we have a bug, and our prompt
68// termination has been ordered.
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000069static const int KillSigs[] = {
Dan Gohman5cdb3452013-02-20 19:15:01 +000070 SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGQUIT
Chris Lattner62f50da2010-02-12 00:37:46 +000071#ifdef SIGSYS
72 , SIGSYS
73#endif
74#ifdef SIGXCPU
75 , SIGXCPU
76#endif
Chris Lattner3b38fd62010-02-14 18:20:09 +000077#ifdef SIGXFSZ
Chris Lattner62f50da2010-02-12 00:37:46 +000078 , SIGXFSZ
79#endif
Reid Spencer3d7a6142004-08-29 19:22:48 +000080#ifdef SIGEMT
81 , SIGEMT
82#endif
83};
Reid Spencer3d7a6142004-08-29 19:22:48 +000084
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000085static unsigned NumRegisteredSignals = 0;
86static struct {
Chris Lattnerfb954722009-03-23 05:55:36 +000087 struct sigaction SA;
88 int SigNo;
Craig Topperfac90572015-12-01 06:12:59 +000089} RegisteredSignalInfo[array_lengthof(IntSigs) + array_lengthof(KillSigs)];
Chris Lattnerfb954722009-03-23 05:55:36 +000090
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000091
92static void RegisterHandler(int Signal) {
Craig Topperfac90572015-12-01 06:12:59 +000093 assert(NumRegisteredSignals < array_lengthof(RegisteredSignalInfo) &&
Chris Lattnerfb954722009-03-23 05:55:36 +000094 "Out of space for signal handlers!");
95
96 struct sigaction NewHandler;
Michael J. Spencer447762d2010-11-29 18:16:10 +000097
Chris Lattnerfb954722009-03-23 05:55:36 +000098 NewHandler.sa_handler = SignalHandler;
99 NewHandler.sa_flags = SA_NODEFER|SA_RESETHAND;
Michael J. Spencer447762d2010-11-29 18:16:10 +0000100 sigemptyset(&NewHandler.sa_mask);
101
Chris Lattnerfb954722009-03-23 05:55:36 +0000102 // Install the new handler, save the old one in RegisteredSignalInfo.
103 sigaction(Signal, &NewHandler,
104 &RegisteredSignalInfo[NumRegisteredSignals].SA);
105 RegisteredSignalInfo[NumRegisteredSignals].SigNo = Signal;
106 ++NumRegisteredSignals;
Chris Lattner6acb4d62009-03-07 08:15:47 +0000107}
108
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000109static void RegisterHandlers() {
Chris Bieneman017ebf02015-04-27 20:45:35 +0000110 // We need to dereference the signals mutex during handler registration so
111 // that we force its construction. This is to prevent the first use being
112 // during handling an actual signal because you can't safely call new in a
113 // signal handler.
114 *SignalsMutex;
Mehdi Amini766d05b2015-11-05 02:29:53 +0000115
Chris Lattnerfb954722009-03-23 05:55:36 +0000116 // If the handlers are already registered, we're done.
117 if (NumRegisteredSignals != 0) return;
118
Chris Bienemanb1cd51e2014-08-29 01:05:16 +0000119 for (auto S : IntSigs) RegisterHandler(S);
120 for (auto S : KillSigs) RegisterHandler(S);
Chris Lattnerf299a682009-03-23 05:42:29 +0000121}
122
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000123static void UnregisterHandlers() {
Chris Lattnerfb954722009-03-23 05:55:36 +0000124 // Restore all of the signal handlers to how they were before we showed up.
125 for (unsigned i = 0, e = NumRegisteredSignals; i != e; ++i)
Chris Lattnerf2b60652009-03-23 06:46:20 +0000126 sigaction(RegisteredSignalInfo[i].SigNo,
Craig Toppere73658d2014-04-28 04:05:08 +0000127 &RegisteredSignalInfo[i].SA, nullptr);
Chris Lattnerfb954722009-03-23 05:55:36 +0000128 NumRegisteredSignals = 0;
Chris Lattnerf299a682009-03-23 05:42:29 +0000129}
130
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000131
Dan Gohman288999b2010-05-27 23:11:55 +0000132/// RemoveFilesToRemove - Process the FilesToRemove list. This function
133/// should be called with the SignalsMutex lock held.
Chandler Carruthe6196eb2012-06-16 00:09:41 +0000134/// NB: This must be an async signal safe function. It cannot allocate or free
135/// memory, even in debug builds.
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000136static void RemoveFilesToRemove() {
Steven Wu94746692015-05-07 16:20:51 +0000137 // Avoid constructing ManagedStatic in the signal handler.
138 // If FilesToRemove is not constructed, there are no files to remove.
139 if (!FilesToRemove.isConstructed())
140 return;
141
Daniel Dunbar511479d2012-10-17 16:30:54 +0000142 // We avoid iterators in case of debug iterators that allocate or release
Chandler Carruthe6196eb2012-06-16 00:09:41 +0000143 // memory.
Chris Bieneman186e7d12014-09-02 23:48:13 +0000144 std::vector<std::string>& FilesToRemoveRef = *FilesToRemove;
145 for (unsigned i = 0, e = FilesToRemoveRef.size(); i != e; ++i) {
Chris Bieneman186e7d12014-09-02 23:48:13 +0000146 const char *path = FilesToRemoveRef[i].c_str();
Daniel Dunbar511479d2012-10-17 16:30:54 +0000147
148 // Get the status so we can determine if it's a file or directory. If we
149 // can't stat the file, ignore it.
150 struct stat buf;
151 if (stat(path, &buf) != 0)
152 continue;
153
154 // If this is not a regular file, ignore it. We want to prevent removal of
155 // special files like /dev/null, even if the compiler is being run with the
156 // super-user permissions.
157 if (!S_ISREG(buf.st_mode))
158 continue;
Mehdi Amini766d05b2015-11-05 02:29:53 +0000159
Daniel Dunbar511479d2012-10-17 16:30:54 +0000160 // Otherwise, remove the file. We ignore any errors here as there is nothing
161 // else we can do.
162 unlink(path);
Dan Gohman288999b2010-05-27 23:11:55 +0000163 }
164}
Chris Lattner6acb4d62009-03-07 08:15:47 +0000165
166// SignalHandler - The signal handler that runs.
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000167static RETSIGTYPE SignalHandler(int Sig) {
Chris Lattnerbbbbbf32009-03-05 18:22:14 +0000168 // Restore the signal behavior to default, so that the program actually
169 // crashes when we return and the signal reissues. This also ensures that if
170 // we crash in our signal handler that the program will terminate immediately
171 // instead of recursing in the signal handler.
Chris Lattnerf299a682009-03-23 05:42:29 +0000172 UnregisterHandlers();
Chris Lattner6acb4d62009-03-07 08:15:47 +0000173
174 // Unmask all potentially blocked kill signals.
175 sigset_t SigMask;
176 sigfillset(&SigMask);
Craig Toppere73658d2014-04-28 04:05:08 +0000177 sigprocmask(SIG_UNBLOCK, &SigMask, nullptr);
Chris Lattnerbbbbbf32009-03-05 18:22:14 +0000178
Dylan Noblesmithc4c51802014-08-23 23:07:14 +0000179 {
Chris Bieneman186e7d12014-09-02 23:48:13 +0000180 unique_lock<SmartMutex<true>> Guard(*SignalsMutex);
Dylan Noblesmithc4c51802014-08-23 23:07:14 +0000181 RemoveFilesToRemove();
Chris Lattner4fdd0422009-03-04 21:21:36 +0000182
Chris Bienemanb1cd51e2014-08-29 01:05:16 +0000183 if (std::find(std::begin(IntSigs), std::end(IntSigs), Sig)
184 != std::end(IntSigs)) {
Dylan Noblesmithc4c51802014-08-23 23:07:14 +0000185 if (InterruptFunction) {
186 void (*IF)() = InterruptFunction;
187 Guard.unlock();
188 InterruptFunction = nullptr;
189 IF(); // run the interrupt function.
190 return;
191 }
192
193 Guard.unlock();
194 raise(Sig); // Execute the default handler.
Chris Lattner4fdd0422009-03-04 21:21:36 +0000195 return;
Dylan Noblesmithc4c51802014-08-23 23:07:14 +0000196 }
Chris Lattner4fdd0422009-03-04 21:21:36 +0000197 }
198
199 // Otherwise if it is a fault (like SEGV) run any handler.
Yaron Keren28738102015-07-22 21:11:17 +0000200 llvm::sys::RunSignalHandlers();
Ulrich Weigand90c9abd2013-05-03 12:22:11 +0000201
202#ifdef __s390__
203 // On S/390, certain signals are delivered with PSW Address pointing to
204 // *after* the faulting instruction. Simply returning from the signal
205 // handler would continue execution after that point, instead of
206 // re-raising the signal. Raise the signal manually in those cases.
207 if (Sig == SIGILL || Sig == SIGFPE || Sig == SIGTRAP)
208 raise(Sig);
209#endif
Chris Lattner4fdd0422009-03-04 21:21:36 +0000210}
211
Daniel Dunbar68272562010-05-08 02:10:34 +0000212void llvm::sys::RunInterruptHandlers() {
Chris Bieneman186e7d12014-09-02 23:48:13 +0000213 sys::SmartScopedLock<true> Guard(*SignalsMutex);
Dan Gohman288999b2010-05-27 23:11:55 +0000214 RemoveFilesToRemove();
Daniel Dunbar68272562010-05-08 02:10:34 +0000215}
Chris Lattner4fdd0422009-03-04 21:21:36 +0000216
Chris Lattnerbb1ba7b2009-03-08 19:13:45 +0000217void llvm::sys::SetInterruptFunction(void (*IF)()) {
Dylan Noblesmith4704ffe2014-08-23 22:49:17 +0000218 {
Chris Bieneman186e7d12014-09-02 23:48:13 +0000219 sys::SmartScopedLock<true> Guard(*SignalsMutex);
Dylan Noblesmith4704ffe2014-08-23 22:49:17 +0000220 InterruptFunction = IF;
221 }
Chris Lattnerf299a682009-03-23 05:42:29 +0000222 RegisterHandlers();
Chris Lattner4fdd0422009-03-04 21:21:36 +0000223}
224
225// RemoveFileOnSignal - The public API
Rafael Espindola4f35da72013-06-13 21:16:58 +0000226bool llvm::sys::RemoveFileOnSignal(StringRef Filename,
Chris Lattnerbb1ba7b2009-03-08 19:13:45 +0000227 std::string* ErrMsg) {
Dylan Noblesmith4704ffe2014-08-23 22:49:17 +0000228 {
Chris Bieneman186e7d12014-09-02 23:48:13 +0000229 sys::SmartScopedLock<true> Guard(*SignalsMutex);
Yaron Keren4135e4c2015-07-23 05:49:29 +0000230 FilesToRemove->push_back(Filename);
Dylan Noblesmith4704ffe2014-08-23 22:49:17 +0000231 }
Owen Anderson820739d2009-08-17 17:07:22 +0000232
Chris Lattnerf299a682009-03-23 05:42:29 +0000233 RegisterHandlers();
Chris Lattner4fdd0422009-03-04 21:21:36 +0000234 return false;
235}
236
Dan Gohmane201c072010-09-01 14:17:34 +0000237// DontRemoveFileOnSignal - The public API
Rafael Espindola4f35da72013-06-13 21:16:58 +0000238void llvm::sys::DontRemoveFileOnSignal(StringRef Filename) {
Chris Bieneman186e7d12014-09-02 23:48:13 +0000239 sys::SmartScopedLock<true> Guard(*SignalsMutex);
Chandler Carruthe6196eb2012-06-16 00:09:41 +0000240 std::vector<std::string>::reverse_iterator RI =
Chris Bieneman186e7d12014-09-02 23:48:13 +0000241 std::find(FilesToRemove->rbegin(), FilesToRemove->rend(), Filename);
242 std::vector<std::string>::iterator I = FilesToRemove->end();
243 if (RI != FilesToRemove->rend())
244 I = FilesToRemove->erase(RI.base()-1);
Dan Gohmane201c072010-09-01 14:17:34 +0000245}
246
Chris Lattner4fdd0422009-03-04 21:21:36 +0000247/// AddSignalHandler - Add a function to be called when a signal is delivered
248/// to the process. The handler can have a cookie passed to it to identify
249/// what instance of the handler it is.
Chris Lattnerbb1ba7b2009-03-08 19:13:45 +0000250void llvm::sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) {
Chris Bieneman186e7d12014-09-02 23:48:13 +0000251 CallBacksToRun->push_back(std::make_pair(FnPtr, Cookie));
Chris Lattnerf299a682009-03-23 05:42:29 +0000252 RegisterHandlers();
Chris Lattner4fdd0422009-03-04 21:21:36 +0000253}
254
Reid Kleckner40aa9c62015-11-09 23:10:29 +0000255#if defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES) && HAVE_LINK_H && \
256 (defined(__linux__) || defined(__FreeBSD__) || \
257 defined(__FreeBSD_kernel__) || defined(__NetBSD__))
Alexey Samsonov8a584bb2014-10-10 22:06:59 +0000258struct DlIteratePhdrData {
259 void **StackTrace;
260 int depth;
261 bool first;
262 const char **modules;
263 intptr_t *offsets;
264 const char *main_exec_name;
265};
266
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000267static int dl_iterate_phdr_cb(dl_phdr_info *info, size_t size, void *arg) {
Alexey Samsonov8a584bb2014-10-10 22:06:59 +0000268 DlIteratePhdrData *data = (DlIteratePhdrData*)arg;
269 const char *name = data->first ? data->main_exec_name : info->dlpi_name;
270 data->first = false;
271 for (int i = 0; i < info->dlpi_phnum; i++) {
272 const auto *phdr = &info->dlpi_phdr[i];
273 if (phdr->p_type != PT_LOAD)
274 continue;
275 intptr_t beg = info->dlpi_addr + phdr->p_vaddr;
276 intptr_t end = beg + phdr->p_memsz;
277 for (int j = 0; j < data->depth; j++) {
278 if (data->modules[j])
279 continue;
280 intptr_t addr = (intptr_t)data->StackTrace[j];
281 if (beg <= addr && addr < end) {
282 data->modules[j] = name;
283 data->offsets[j] = addr - info->dlpi_addr;
284 }
285 }
286 }
287 return 0;
288}
289
Reid Kleckner40aa9c62015-11-09 23:10:29 +0000290/// If this is an ELF platform, we can find all loaded modules and their virtual
291/// addresses with dl_iterate_phdr.
Alexey Samsonov8a584bb2014-10-10 22:06:59 +0000292static bool findModulesAndOffsets(void **StackTrace, int Depth,
293 const char **Modules, intptr_t *Offsets,
Reid Klecknerba5757d2015-11-05 01:07:54 +0000294 const char *MainExecutableName,
295 StringSaver &StrPool) {
Alexey Samsonov8a584bb2014-10-10 22:06:59 +0000296 DlIteratePhdrData data = {StackTrace, Depth, true,
297 Modules, Offsets, MainExecutableName};
298 dl_iterate_phdr(dl_iterate_phdr_cb, &data);
299 return true;
300}
301#else
Reid Kleckner40aa9c62015-11-09 23:10:29 +0000302/// This platform does not have dl_iterate_phdr, so we do not yet know how to
303/// find all loaded DSOs.
Alexey Samsonov8a584bb2014-10-10 22:06:59 +0000304static bool findModulesAndOffsets(void **StackTrace, int Depth,
305 const char **Modules, intptr_t *Offsets,
Mehdi Amini7ae928e2015-11-05 02:29:57 +0000306 const char *MainExecutableName,
307 StringSaver &StrPool) {
Alexey Samsonov8a584bb2014-10-10 22:06:59 +0000308 return false;
309}
Reid Kleckner40aa9c62015-11-09 23:10:29 +0000310#endif // defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES) && ...
Reid Spencer3d7a6142004-08-29 19:22:48 +0000311
312// PrintStackTrace - In the case of a program crash or fault, print out a stack
313// trace so that the user has an indication of why and where we died.
314//
315// On glibc systems we have the 'backtrace' function, which works nicely, but
Michael J. Spencer447762d2010-11-29 18:16:10 +0000316// doesn't demangle symbols.
Zachary Turnercd132c92015-03-05 19:10:52 +0000317void llvm::sys::PrintStackTrace(raw_ostream &OS) {
Benjamin Kramer5651cbd2012-09-28 10:10:46 +0000318#if defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES)
Chris Lattner4fdd0422009-03-04 21:21:36 +0000319 static void* StackTrace[256];
Reid Spencer3d7a6142004-08-29 19:22:48 +0000320 // Use backtrace() to output a backtrace on Linux systems with glibc.
Evan Cheng86cb3182008-05-05 18:30:58 +0000321 int depth = backtrace(StackTrace,
322 static_cast<int>(array_lengthof(StackTrace)));
Zachary Turnercd132c92015-03-05 19:10:52 +0000323 if (printSymbolizedStackTrace(StackTrace, depth, OS))
Alexey Samsonov8a584bb2014-10-10 22:06:59 +0000324 return;
Dan Gohman7f079aa2008-12-05 20:12:48 +0000325#if HAVE_DLFCN_H && __GNUG__
326 int width = 0;
327 for (int i = 0; i < depth; ++i) {
328 Dl_info dlinfo;
329 dladdr(StackTrace[i], &dlinfo);
Dan Gohman1f517dd2009-02-10 17:56:28 +0000330 const char* name = strrchr(dlinfo.dli_fname, '/');
Dan Gohman7f079aa2008-12-05 20:12:48 +0000331
332 int nwidth;
Craig Toppere73658d2014-04-28 04:05:08 +0000333 if (!name) nwidth = strlen(dlinfo.dli_fname);
334 else nwidth = strlen(name) - 1;
Dan Gohman7f079aa2008-12-05 20:12:48 +0000335
336 if (nwidth > width) width = nwidth;
337 }
338
339 for (int i = 0; i < depth; ++i) {
340 Dl_info dlinfo;
341 dladdr(StackTrace[i], &dlinfo);
342
Zachary Turnercd132c92015-03-05 19:10:52 +0000343 OS << format("%-2d", i);
Dan Gohman7f079aa2008-12-05 20:12:48 +0000344
Dan Gohman1f517dd2009-02-10 17:56:28 +0000345 const char* name = strrchr(dlinfo.dli_fname, '/');
Zachary Turnercd132c92015-03-05 19:10:52 +0000346 if (!name) OS << format(" %-*s", width, dlinfo.dli_fname);
347 else OS << format(" %-*s", width, name+1);
Dan Gohman7f079aa2008-12-05 20:12:48 +0000348
Zachary Turnercd132c92015-03-05 19:10:52 +0000349 OS << format(" %#0*lx", (int)(sizeof(void*) * 2) + 2,
350 (unsigned long)StackTrace[i]);
Dan Gohman7f079aa2008-12-05 20:12:48 +0000351
Craig Toppere73658d2014-04-28 04:05:08 +0000352 if (dlinfo.dli_sname != nullptr) {
Zachary Turnercd132c92015-03-05 19:10:52 +0000353 OS << ' ';
Joerg Sonnenberger66241832013-04-27 22:12:32 +0000354# if HAVE_CXXABI_H
Benjamin Kramer83e2a442013-04-28 07:47:04 +0000355 int res;
Craig Toppere73658d2014-04-28 04:05:08 +0000356 char* d = abi::__cxa_demangle(dlinfo.dli_sname, nullptr, nullptr, &res);
Joerg Sonnenberger66241832013-04-27 22:12:32 +0000357# else
358 char* d = NULL;
359# endif
Zachary Turnercd132c92015-03-05 19:10:52 +0000360 if (!d) OS << dlinfo.dli_sname;
361 else OS << d;
Dan Gohman7f079aa2008-12-05 20:12:48 +0000362 free(d);
363
Edwin Vane44338e02013-01-28 19:34:42 +0000364 // FIXME: When we move to C++11, use %t length modifier. It's not in
365 // C++03 and causes gcc to issue warnings. Losing the upper 32 bits of
366 // the stack offset for a stack dump isn't likely to cause any problems.
Zachary Turnercd132c92015-03-05 19:10:52 +0000367 OS << format(" + %u",(unsigned)((char*)StackTrace[i]-
368 (char*)dlinfo.dli_saddr));
Dan Gohman7f079aa2008-12-05 20:12:48 +0000369 }
Zachary Turnercd132c92015-03-05 19:10:52 +0000370 OS << '\n';
Dan Gohman7f079aa2008-12-05 20:12:48 +0000371 }
372#else
Lauro Ramos Venancioeab51d32008-02-15 18:05:54 +0000373 backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
Reid Spencer3d7a6142004-08-29 19:22:48 +0000374#endif
Dan Gohman7f079aa2008-12-05 20:12:48 +0000375#endif
Reid Spencer3d7a6142004-08-29 19:22:48 +0000376}
377
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000378static void PrintStackTraceSignalHandler(void *) {
Zachary Turnercd132c92015-03-05 19:10:52 +0000379 PrintStackTrace(llvm::errs());
Argyrios Kyrtzidiseb9ae762013-01-09 19:42:40 +0000380}
381
Michael J. Spencer89b0ad22015-01-29 17:20:29 +0000382void llvm::sys::DisableSystemDialogsOnCrash() {}
383
NAKAMURA Takumi8a54d812012-09-06 03:01:43 +0000384/// PrintStackTraceOnErrorSignal - When an error signal (such as SIGABRT or
Reid Spencer3d7a6142004-08-29 19:22:48 +0000385/// SIGSEGV) is delivered to the process, print a stack trace and then exit.
Pete Cooper6bea2f42015-04-07 20:43:23 +0000386void llvm::sys::PrintStackTraceOnErrorSignal(bool DisableCrashReporting) {
Craig Toppere73658d2014-04-28 04:05:08 +0000387 AddSignalHandler(PrintStackTraceSignalHandler, nullptr);
Argyrios Kyrtzidiscd8fe082012-01-11 20:53:25 +0000388
Daniel Dunbareb6c7082013-08-30 20:39:21 +0000389#if defined(__APPLE__) && defined(ENABLE_CRASH_OVERRIDES)
Argyrios Kyrtzidiscd8fe082012-01-11 20:53:25 +0000390 // Environment variable to disable any kind of crash dialog.
Pete Cooper6bea2f42015-04-07 20:43:23 +0000391 if (DisableCrashReporting || getenv("LLVM_DISABLE_CRASH_REPORT")) {
Argyrios Kyrtzidiscd8fe082012-01-11 20:53:25 +0000392 mach_port_t self = mach_task_self();
393
394 exception_mask_t mask = EXC_MASK_CRASH;
395
NAKAMURA Takumiffa15712012-09-06 03:02:56 +0000396 kern_return_t ret = task_set_exception_ports(self,
Argyrios Kyrtzidiscd8fe082012-01-11 20:53:25 +0000397 mask,
Jean-Daniel Dupasa573b222012-03-24 22:17:50 +0000398 MACH_PORT_NULL,
NAKAMURA Takumiffa15712012-09-06 03:02:56 +0000399 EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES,
Jean-Daniel Dupasa573b222012-03-24 22:17:50 +0000400 THREAD_STATE_NONE);
Argyrios Kyrtzidiscd8fe082012-01-11 20:53:25 +0000401 (void)ret;
402 }
403#endif
Reid Spencer3d7a6142004-08-29 19:22:48 +0000404}
Chris Lattner4fdd0422009-03-04 21:21:36 +0000405
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000406
407/***/
408
Daniel Dunbarf14d9462010-08-19 23:45:39 +0000409// On Darwin, raise sends a signal to the main thread instead of the current
410// thread. This has the unfortunate effect that assert() and abort() will end up
411// bypassing our crash recovery attempts. We work around this for anything in
412// the same linkage unit by just defining our own versions of the assert handler
413// and abort.
414
Daniel Dunbareb6c7082013-08-30 20:39:21 +0000415#if defined(__APPLE__) && defined(ENABLE_CRASH_OVERRIDES)
Daniel Dunbarf14d9462010-08-19 23:45:39 +0000416
Douglas Gregor0e506822011-04-29 16:12:17 +0000417#include <signal.h>
418#include <pthread.h>
419
Daniel Dunbar18456f32010-09-22 17:46:10 +0000420int raise(int sig) {
Daniel Dunbarcc0e18d2010-10-08 18:31:34 +0000421 return pthread_kill(pthread_self(), sig);
Daniel Dunbar18456f32010-09-22 17:46:10 +0000422}
423
Daniel Dunbarf14d9462010-08-19 23:45:39 +0000424void __assert_rtn(const char *func,
425 const char *file,
426 int line,
427 const char *expr) {
428 if (func)
429 fprintf(stderr, "Assertion failed: (%s), function %s, file %s, line %d.\n",
430 expr, func, file, line);
431 else
432 fprintf(stderr, "Assertion failed: (%s), file %s, line %d.\n",
433 expr, file, line);
434 abort();
435}
436
Daniel Dunbarf14d9462010-08-19 23:45:39 +0000437void abort() {
Daniel Dunbar18456f32010-09-22 17:46:10 +0000438 raise(SIGABRT);
Daniel Dunbarf14d9462010-08-19 23:45:39 +0000439 usleep(1000);
440 __builtin_trap();
441}
442
443#endif