2016-04-06 04:45:04 +08:00
|
|
|
//===- Signals.cpp - Generic Unix Signals Implementation -----*- C++ -*-===//
|
2010-11-30 02:16:10 +08:00
|
|
|
//
|
2004-08-30 03:22:48 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2010-11-30 02:16:10 +08:00
|
|
|
//
|
2004-08-30 03:22:48 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines some helpful functions for dealing with the possibility of
|
2011-04-15 13:18:47 +08:00
|
|
|
// Unix signals occurring while your program is running.
|
2004-08-30 03:22:48 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Unix.h"
|
2007-09-07 12:06:50 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-09-07 03:16:48 +08:00
|
|
|
#include "llvm/Demangle/Demangle.h"
|
2015-03-06 03:10:52 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2014-10-11 06:06:59 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/FileUtilities.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "llvm/Support/Mutex.h"
|
2014-10-11 06:06:59 +08:00
|
|
|
#include "llvm/Support/Program.h"
|
2014-08-24 07:07:14 +08:00
|
|
|
#include "llvm/Support/UniqueLock.h"
|
2014-10-11 06:06:59 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include <algorithm>
|
2012-06-16 08:09:41 +08:00
|
|
|
#include <string>
|
2004-12-27 14:16:52 +08:00
|
|
|
#if HAVE_EXECINFO_H
|
2004-08-30 03:22:48 +08:00
|
|
|
# include <execinfo.h> // For backtrace().
|
|
|
|
#endif
|
2016-04-06 04:45:04 +08:00
|
|
|
#if HAVE_SIGNAL_H
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
2007-04-08 02:52:17 +08:00
|
|
|
#if HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
2013-04-28 06:12:32 +08:00
|
|
|
#if HAVE_DLFCN_H
|
2008-12-06 04:12:48 +08:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
2012-01-12 04:53:25 +08:00
|
|
|
#if HAVE_MACH_MACH_H
|
|
|
|
#include <mach/mach.h>
|
|
|
|
#endif
|
2014-10-11 06:06:59 +08:00
|
|
|
#if HAVE_LINK_H
|
|
|
|
#include <link.h>
|
|
|
|
#endif
|
2016-09-30 05:07:57 +08:00
|
|
|
#ifdef HAVE__UNWIND_BACKTRACE
|
2016-05-21 05:18:12 +08:00
|
|
|
// FIXME: We should be able to use <unwind.h> for any target that has an
|
|
|
|
// _Unwind_Backtrace function, but on FreeBSD the configure test passes
|
|
|
|
// despite the function not existing, and on Android, <unwind.h> conflicts
|
|
|
|
// with <link.h>.
|
2016-11-15 01:56:18 +08:00
|
|
|
#if defined(__GLIBC__) || defined(__APPLE__)
|
2016-05-21 05:18:12 +08:00
|
|
|
#include <unwind.h>
|
|
|
|
#else
|
2016-09-30 05:07:57 +08:00
|
|
|
#undef HAVE__UNWIND_BACKTRACE
|
2016-05-21 05:18:12 +08:00
|
|
|
#endif
|
|
|
|
#endif
|
2012-01-12 04:53:25 +08:00
|
|
|
|
2006-08-02 01:59:14 +08:00
|
|
|
using namespace llvm;
|
2004-08-30 03:22:48 +08:00
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static RETSIGTYPE SignalHandler(int Sig); // defined below.
|
2009-03-23 13:42:29 +08:00
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static ManagedStatic<SmartMutex<true> > SignalsMutex;
|
2009-08-18 01:07:22 +08:00
|
|
|
|
2005-08-02 10:14:22 +08:00
|
|
|
/// InterruptFunction - The function to call if ctrl-c is pressed.
|
2016-04-06 04:45:04 +08:00
|
|
|
static void (*InterruptFunction)() = nullptr;
|
2005-08-02 10:14:22 +08:00
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static ManagedStatic<std::vector<std::string>> FilesToRemove;
|
2004-08-30 03:22:48 +08:00
|
|
|
|
2016-06-09 08:53:21 +08:00
|
|
|
static StringRef Argv0;
|
|
|
|
|
2013-02-21 03:28:46 +08:00
|
|
|
// IntSigs - Signals that represent requested termination. There's no bug
|
|
|
|
// or failure, or if there is, it's not our direct responsibility. For whatever
|
|
|
|
// reason, our continued execution is no longer desirable.
|
2016-04-06 04:45:04 +08:00
|
|
|
static const int IntSigs[] = {
|
2013-02-21 03:15:01 +08:00
|
|
|
SIGHUP, SIGINT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
|
2004-08-30 03:22:48 +08:00
|
|
|
};
|
|
|
|
|
2013-02-21 03:28:46 +08:00
|
|
|
// KillSigs - Signals that represent that we have a bug, and our prompt
|
|
|
|
// termination has been ordered.
|
2016-04-06 04:45:04 +08:00
|
|
|
static const int KillSigs[] = {
|
2013-02-21 03:15:01 +08:00
|
|
|
SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGQUIT
|
2010-02-12 08:37:46 +08:00
|
|
|
#ifdef SIGSYS
|
|
|
|
, SIGSYS
|
|
|
|
#endif
|
|
|
|
#ifdef SIGXCPU
|
|
|
|
, SIGXCPU
|
|
|
|
#endif
|
2010-02-15 02:20:09 +08:00
|
|
|
#ifdef SIGXFSZ
|
2010-02-12 08:37:46 +08:00
|
|
|
, SIGXFSZ
|
|
|
|
#endif
|
2004-08-30 03:22:48 +08:00
|
|
|
#ifdef SIGEMT
|
|
|
|
, SIGEMT
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static unsigned NumRegisteredSignals = 0;
|
|
|
|
static struct {
|
2009-03-23 13:55:36 +08:00
|
|
|
struct sigaction SA;
|
|
|
|
int SigNo;
|
2015-12-01 14:12:59 +08:00
|
|
|
} RegisteredSignalInfo[array_lengthof(IntSigs) + array_lengthof(KillSigs)];
|
2009-03-23 13:55:36 +08:00
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
|
|
|
|
static void RegisterHandler(int Signal) {
|
2015-12-01 14:12:59 +08:00
|
|
|
assert(NumRegisteredSignals < array_lengthof(RegisteredSignalInfo) &&
|
2009-03-23 13:55:36 +08:00
|
|
|
"Out of space for signal handlers!");
|
|
|
|
|
|
|
|
struct sigaction NewHandler;
|
2010-11-30 02:16:10 +08:00
|
|
|
|
2009-03-23 13:55:36 +08:00
|
|
|
NewHandler.sa_handler = SignalHandler;
|
2016-05-23 14:47:37 +08:00
|
|
|
NewHandler.sa_flags = SA_NODEFER | SA_RESETHAND | SA_ONSTACK;
|
2010-11-30 02:16:10 +08:00
|
|
|
sigemptyset(&NewHandler.sa_mask);
|
|
|
|
|
2009-03-23 13:55:36 +08:00
|
|
|
// Install the new handler, save the old one in RegisteredSignalInfo.
|
|
|
|
sigaction(Signal, &NewHandler,
|
|
|
|
&RegisteredSignalInfo[NumRegisteredSignals].SA);
|
|
|
|
RegisteredSignalInfo[NumRegisteredSignals].SigNo = Signal;
|
|
|
|
++NumRegisteredSignals;
|
2009-03-23 13:42:29 +08:00
|
|
|
}
|
|
|
|
|
2016-05-21 05:26:00 +08:00
|
|
|
#if defined(HAVE_SIGALTSTACK)
|
2016-08-24 11:42:51 +08:00
|
|
|
// Hold onto both the old and new alternate signal stack so that it's not
|
|
|
|
// reported as a leak. We don't make any attempt to remove our alt signal
|
|
|
|
// stack if we remove our signal handlers; that can't be done reliably if
|
|
|
|
// someone else is also trying to do the same thing.
|
2016-05-21 05:38:15 +08:00
|
|
|
static stack_t OldAltStack;
|
2016-08-24 11:42:51 +08:00
|
|
|
static void* NewAltStackPointer;
|
2016-05-21 05:07:41 +08:00
|
|
|
|
|
|
|
static void CreateSigAltStack() {
|
2016-08-24 08:54:49 +08:00
|
|
|
const size_t AltStackSize = MINSIGSTKSZ + 64 * 1024;
|
2016-05-21 05:07:41 +08:00
|
|
|
|
|
|
|
// If we're executing on the alternate stack, or we already have an alternate
|
|
|
|
// signal stack that we're happy with, there's nothing for us to do. Don't
|
|
|
|
// reduce the size, some other part of the process might need a larger stack
|
|
|
|
// than we do.
|
|
|
|
if (sigaltstack(nullptr, &OldAltStack) != 0 ||
|
|
|
|
OldAltStack.ss_flags & SS_ONSTACK ||
|
|
|
|
(OldAltStack.ss_sp && OldAltStack.ss_size >= AltStackSize))
|
|
|
|
return;
|
|
|
|
|
2016-05-21 05:38:15 +08:00
|
|
|
stack_t AltStack = {};
|
2016-05-21 08:36:47 +08:00
|
|
|
AltStack.ss_sp = reinterpret_cast<char *>(malloc(AltStackSize));
|
2016-08-24 11:42:51 +08:00
|
|
|
NewAltStackPointer = AltStack.ss_sp; // Save to avoid reporting a leak.
|
2016-05-21 05:07:41 +08:00
|
|
|
AltStack.ss_size = AltStackSize;
|
|
|
|
if (sigaltstack(&AltStack, &OldAltStack) != 0)
|
|
|
|
free(AltStack.ss_sp);
|
|
|
|
}
|
2016-05-21 05:26:00 +08:00
|
|
|
#else
|
|
|
|
static void CreateSigAltStack() {}
|
|
|
|
#endif
|
2016-05-21 05:07:41 +08:00
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static void RegisterHandlers() {
|
2015-04-28 04:45:35 +08:00
|
|
|
// We need to dereference the signals mutex during handler registration so
|
|
|
|
// that we force its construction. This is to prevent the first use being
|
|
|
|
// during handling an actual signal because you can't safely call new in a
|
|
|
|
// signal handler.
|
|
|
|
*SignalsMutex;
|
2015-11-05 10:29:53 +08:00
|
|
|
|
2009-03-23 13:55:36 +08:00
|
|
|
// If the handlers are already registered, we're done.
|
|
|
|
if (NumRegisteredSignals != 0) return;
|
|
|
|
|
2016-05-21 05:07:41 +08:00
|
|
|
// Create an alternate stack for signal handling. This is necessary for us to
|
|
|
|
// be able to reliably handle signals due to stack overflow.
|
|
|
|
CreateSigAltStack();
|
|
|
|
|
2014-08-29 09:05:16 +08:00
|
|
|
for (auto S : IntSigs) RegisterHandler(S);
|
|
|
|
for (auto S : KillSigs) RegisterHandler(S);
|
2009-03-23 13:42:29 +08:00
|
|
|
}
|
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static void UnregisterHandlers() {
|
2009-03-23 13:55:36 +08:00
|
|
|
// Restore all of the signal handlers to how they were before we showed up.
|
|
|
|
for (unsigned i = 0, e = NumRegisteredSignals; i != e; ++i)
|
2009-03-23 14:46:20 +08:00
|
|
|
sigaction(RegisteredSignalInfo[i].SigNo,
|
2014-04-28 12:05:08 +08:00
|
|
|
&RegisteredSignalInfo[i].SA, nullptr);
|
2009-03-23 13:55:36 +08:00
|
|
|
NumRegisteredSignals = 0;
|
2009-03-07 16:15:47 +08:00
|
|
|
}
|
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
|
2010-05-28 07:11:55 +08:00
|
|
|
/// RemoveFilesToRemove - Process the FilesToRemove list. This function
|
|
|
|
/// should be called with the SignalsMutex lock held.
|
2012-06-16 08:09:41 +08:00
|
|
|
/// NB: This must be an async signal safe function. It cannot allocate or free
|
|
|
|
/// memory, even in debug builds.
|
2016-04-06 04:45:04 +08:00
|
|
|
static void RemoveFilesToRemove() {
|
2015-05-08 00:20:51 +08:00
|
|
|
// Avoid constructing ManagedStatic in the signal handler.
|
|
|
|
// If FilesToRemove is not constructed, there are no files to remove.
|
|
|
|
if (!FilesToRemove.isConstructed())
|
|
|
|
return;
|
|
|
|
|
2012-10-18 00:30:54 +08:00
|
|
|
// We avoid iterators in case of debug iterators that allocate or release
|
2012-06-16 08:09:41 +08:00
|
|
|
// memory.
|
2014-09-03 07:48:13 +08:00
|
|
|
std::vector<std::string>& FilesToRemoveRef = *FilesToRemove;
|
|
|
|
for (unsigned i = 0, e = FilesToRemoveRef.size(); i != e; ++i) {
|
|
|
|
const char *path = FilesToRemoveRef[i].c_str();
|
2012-10-18 00:30:54 +08:00
|
|
|
|
|
|
|
// Get the status so we can determine if it's a file or directory. If we
|
|
|
|
// can't stat the file, ignore it.
|
|
|
|
struct stat buf;
|
|
|
|
if (stat(path, &buf) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If this is not a regular file, ignore it. We want to prevent removal of
|
|
|
|
// special files like /dev/null, even if the compiler is being run with the
|
|
|
|
// super-user permissions.
|
|
|
|
if (!S_ISREG(buf.st_mode))
|
|
|
|
continue;
|
2015-11-05 10:29:53 +08:00
|
|
|
|
2012-10-18 00:30:54 +08:00
|
|
|
// Otherwise, remove the file. We ignore any errors here as there is nothing
|
|
|
|
// else we can do.
|
|
|
|
unlink(path);
|
2010-05-28 07:11:55 +08:00
|
|
|
}
|
|
|
|
}
|
2009-03-23 13:42:29 +08:00
|
|
|
|
2009-03-07 16:15:47 +08:00
|
|
|
// SignalHandler - The signal handler that runs.
|
2016-04-06 04:45:04 +08:00
|
|
|
static RETSIGTYPE SignalHandler(int Sig) {
|
2009-03-06 02:22:14 +08:00
|
|
|
// Restore the signal behavior to default, so that the program actually
|
|
|
|
// crashes when we return and the signal reissues. This also ensures that if
|
|
|
|
// we crash in our signal handler that the program will terminate immediately
|
|
|
|
// instead of recursing in the signal handler.
|
2009-03-23 13:42:29 +08:00
|
|
|
UnregisterHandlers();
|
2009-03-07 16:15:47 +08:00
|
|
|
|
|
|
|
// Unmask all potentially blocked kill signals.
|
|
|
|
sigset_t SigMask;
|
|
|
|
sigfillset(&SigMask);
|
2014-04-28 12:05:08 +08:00
|
|
|
sigprocmask(SIG_UNBLOCK, &SigMask, nullptr);
|
2009-03-06 02:22:14 +08:00
|
|
|
|
2014-08-24 07:07:14 +08:00
|
|
|
{
|
2014-09-03 07:48:13 +08:00
|
|
|
unique_lock<SmartMutex<true>> Guard(*SignalsMutex);
|
2014-08-24 07:07:14 +08:00
|
|
|
RemoveFilesToRemove();
|
|
|
|
|
2014-08-29 09:05:16 +08:00
|
|
|
if (std::find(std::begin(IntSigs), std::end(IntSigs), Sig)
|
|
|
|
!= std::end(IntSigs)) {
|
2014-08-24 07:07:14 +08:00
|
|
|
if (InterruptFunction) {
|
|
|
|
void (*IF)() = InterruptFunction;
|
|
|
|
Guard.unlock();
|
|
|
|
InterruptFunction = nullptr;
|
|
|
|
IF(); // run the interrupt function.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Guard.unlock();
|
|
|
|
raise(Sig); // Execute the default handler.
|
2009-03-05 05:21:36 +08:00
|
|
|
return;
|
2014-08-24 07:07:14 +08:00
|
|
|
}
|
2009-03-05 05:21:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise if it is a fault (like SEGV) run any handler.
|
2015-07-23 05:11:17 +08:00
|
|
|
llvm::sys::RunSignalHandlers();
|
[SystemZ] Support System Z as host architecture
The llvm::sys::AddSignalHandler function (as well as related routines) in
lib/Support/Unix/Signals.inc currently registers a signal handler routine
via "sigaction". When this handler is called due to a SIGSEGV, SIGILL or
similar signal, it will show a stack backtrace, deactivate the handler,
and then simply return to the operating system. The intent is that the
OS will now retry execution at the same location as before, which ought
to again trigger the same error condition and cause the same signal to be
delivered again. Since the hander is now deactivated, the OS will take
its default action (usually, terminate the program and possibly create
a core dump).
However, this method doesn't work reliably on System Z: With certain
signals (namely SIGILL, SIGFPE, and SIGTRAP), the program counter stored
by the kernel on the signal stack frame (which is the location where
execution will resume) is not the instruction that triggered the fault,
but then instruction *after it*. When the LLVM signal handler simply
returns to the kernel, execution will then resume at *that* address,
which will not trigger the problem again, but simply go on and execute
potentially unrelated code leading to random errors afterwards.
To fix this, the patch simply goes and re-raises the signal in question
directly from the handler instead of returning from it. This is done
only on System Z and only for those signals that have this particular
problem.
llvm-svn: 181010
2013-05-03 20:22:11 +08:00
|
|
|
|
|
|
|
#ifdef __s390__
|
|
|
|
// On S/390, certain signals are delivered with PSW Address pointing to
|
|
|
|
// *after* the faulting instruction. Simply returning from the signal
|
|
|
|
// handler would continue execution after that point, instead of
|
|
|
|
// re-raising the signal. Raise the signal manually in those cases.
|
|
|
|
if (Sig == SIGILL || Sig == SIGFPE || Sig == SIGTRAP)
|
|
|
|
raise(Sig);
|
|
|
|
#endif
|
2009-03-05 05:21:36 +08:00
|
|
|
}
|
|
|
|
|
2010-05-08 10:10:34 +08:00
|
|
|
void llvm::sys::RunInterruptHandlers() {
|
2014-09-03 07:48:13 +08:00
|
|
|
sys::SmartScopedLock<true> Guard(*SignalsMutex);
|
2010-05-28 07:11:55 +08:00
|
|
|
RemoveFilesToRemove();
|
2010-05-08 10:10:34 +08:00
|
|
|
}
|
2009-03-05 05:21:36 +08:00
|
|
|
|
2009-03-09 03:13:45 +08:00
|
|
|
void llvm::sys::SetInterruptFunction(void (*IF)()) {
|
2014-08-24 06:49:17 +08:00
|
|
|
{
|
2014-09-03 07:48:13 +08:00
|
|
|
sys::SmartScopedLock<true> Guard(*SignalsMutex);
|
2014-08-24 06:49:17 +08:00
|
|
|
InterruptFunction = IF;
|
|
|
|
}
|
2009-03-23 13:42:29 +08:00
|
|
|
RegisterHandlers();
|
2009-03-05 05:21:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveFileOnSignal - The public API
|
2013-06-14 05:16:58 +08:00
|
|
|
bool llvm::sys::RemoveFileOnSignal(StringRef Filename,
|
2009-03-09 03:13:45 +08:00
|
|
|
std::string* ErrMsg) {
|
2014-08-24 06:49:17 +08:00
|
|
|
{
|
2014-09-03 07:48:13 +08:00
|
|
|
sys::SmartScopedLock<true> Guard(*SignalsMutex);
|
2015-07-23 13:49:29 +08:00
|
|
|
FilesToRemove->push_back(Filename);
|
2014-08-24 06:49:17 +08:00
|
|
|
}
|
2009-08-18 01:07:22 +08:00
|
|
|
|
2009-03-23 13:42:29 +08:00
|
|
|
RegisterHandlers();
|
2009-03-05 05:21:36 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-01 22:17:34 +08:00
|
|
|
// DontRemoveFileOnSignal - The public API
|
2013-06-14 05:16:58 +08:00
|
|
|
void llvm::sys::DontRemoveFileOnSignal(StringRef Filename) {
|
2014-09-03 07:48:13 +08:00
|
|
|
sys::SmartScopedLock<true> Guard(*SignalsMutex);
|
2012-06-16 08:09:41 +08:00
|
|
|
std::vector<std::string>::reverse_iterator RI =
|
2016-08-12 11:55:06 +08:00
|
|
|
find(reverse(*FilesToRemove), Filename);
|
2014-09-03 07:48:13 +08:00
|
|
|
std::vector<std::string>::iterator I = FilesToRemove->end();
|
|
|
|
if (RI != FilesToRemove->rend())
|
|
|
|
I = FilesToRemove->erase(RI.base()-1);
|
2010-09-01 22:17:34 +08:00
|
|
|
}
|
|
|
|
|
2009-03-05 05:21:36 +08:00
|
|
|
/// AddSignalHandler - Add a function to be called when a signal is delivered
|
|
|
|
/// to the process. The handler can have a cookie passed to it to identify
|
|
|
|
/// what instance of the handler it is.
|
2009-03-09 03:13:45 +08:00
|
|
|
void llvm::sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) {
|
2014-09-03 07:48:13 +08:00
|
|
|
CallBacksToRun->push_back(std::make_pair(FnPtr, Cookie));
|
2009-03-23 13:42:29 +08:00
|
|
|
RegisterHandlers();
|
2009-03-05 05:21:36 +08:00
|
|
|
}
|
|
|
|
|
2016-10-01 04:04:24 +08:00
|
|
|
#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES && HAVE_LINK_H && \
|
2015-11-10 07:10:29 +08:00
|
|
|
(defined(__linux__) || defined(__FreeBSD__) || \
|
|
|
|
defined(__FreeBSD_kernel__) || defined(__NetBSD__))
|
2014-10-11 06:06:59 +08:00
|
|
|
struct DlIteratePhdrData {
|
|
|
|
void **StackTrace;
|
|
|
|
int depth;
|
|
|
|
bool first;
|
|
|
|
const char **modules;
|
|
|
|
intptr_t *offsets;
|
|
|
|
const char *main_exec_name;
|
|
|
|
};
|
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static int dl_iterate_phdr_cb(dl_phdr_info *info, size_t size, void *arg) {
|
2014-10-11 06:06:59 +08:00
|
|
|
DlIteratePhdrData *data = (DlIteratePhdrData*)arg;
|
|
|
|
const char *name = data->first ? data->main_exec_name : info->dlpi_name;
|
|
|
|
data->first = false;
|
|
|
|
for (int i = 0; i < info->dlpi_phnum; i++) {
|
|
|
|
const auto *phdr = &info->dlpi_phdr[i];
|
|
|
|
if (phdr->p_type != PT_LOAD)
|
|
|
|
continue;
|
|
|
|
intptr_t beg = info->dlpi_addr + phdr->p_vaddr;
|
|
|
|
intptr_t end = beg + phdr->p_memsz;
|
|
|
|
for (int j = 0; j < data->depth; j++) {
|
|
|
|
if (data->modules[j])
|
|
|
|
continue;
|
|
|
|
intptr_t addr = (intptr_t)data->StackTrace[j];
|
|
|
|
if (beg <= addr && addr < end) {
|
|
|
|
data->modules[j] = name;
|
|
|
|
data->offsets[j] = addr - info->dlpi_addr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-10 07:10:29 +08:00
|
|
|
/// If this is an ELF platform, we can find all loaded modules and their virtual
|
|
|
|
/// addresses with dl_iterate_phdr.
|
2014-10-11 06:06:59 +08:00
|
|
|
static bool findModulesAndOffsets(void **StackTrace, int Depth,
|
|
|
|
const char **Modules, intptr_t *Offsets,
|
2015-11-05 09:07:54 +08:00
|
|
|
const char *MainExecutableName,
|
|
|
|
StringSaver &StrPool) {
|
2014-10-11 06:06:59 +08:00
|
|
|
DlIteratePhdrData data = {StackTrace, Depth, true,
|
|
|
|
Modules, Offsets, MainExecutableName};
|
|
|
|
dl_iterate_phdr(dl_iterate_phdr_cb, &data);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#else
|
2015-11-10 07:10:29 +08:00
|
|
|
/// This platform does not have dl_iterate_phdr, so we do not yet know how to
|
|
|
|
/// find all loaded DSOs.
|
2014-10-11 06:06:59 +08:00
|
|
|
static bool findModulesAndOffsets(void **StackTrace, int Depth,
|
|
|
|
const char **Modules, intptr_t *Offsets,
|
2015-11-05 10:29:57 +08:00
|
|
|
const char *MainExecutableName,
|
|
|
|
StringSaver &StrPool) {
|
2014-10-11 06:06:59 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-10-01 04:04:24 +08:00
|
|
|
#endif // defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES && ...
|
2004-08-30 03:22:48 +08:00
|
|
|
|
2016-10-01 04:04:24 +08:00
|
|
|
#if ENABLE_BACKTRACES && defined(HAVE__UNWIND_BACKTRACE)
|
2016-05-21 05:18:12 +08:00
|
|
|
static int unwindBacktrace(void **StackTrace, int MaxEntries) {
|
|
|
|
if (MaxEntries < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Skip the first frame ('unwindBacktrace' itself).
|
|
|
|
int Entries = -1;
|
|
|
|
|
|
|
|
auto HandleFrame = [&](_Unwind_Context *Context) -> _Unwind_Reason_Code {
|
|
|
|
// Apparently we need to detect reaching the end of the stack ourselves.
|
|
|
|
void *IP = (void *)_Unwind_GetIP(Context);
|
|
|
|
if (!IP)
|
|
|
|
return _URC_END_OF_STACK;
|
|
|
|
|
|
|
|
assert(Entries < MaxEntries && "recursively called after END_OF_STACK?");
|
|
|
|
if (Entries >= 0)
|
|
|
|
StackTrace[Entries] = IP;
|
|
|
|
|
|
|
|
if (++Entries == MaxEntries)
|
|
|
|
return _URC_END_OF_STACK;
|
|
|
|
return _URC_NO_REASON;
|
|
|
|
};
|
|
|
|
|
|
|
|
_Unwind_Backtrace(
|
|
|
|
[](_Unwind_Context *Context, void *Handler) {
|
|
|
|
return (*static_cast<decltype(HandleFrame) *>(Handler))(Context);
|
|
|
|
},
|
|
|
|
static_cast<void *>(&HandleFrame));
|
|
|
|
return std::max(Entries, 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-08-30 03:22:48 +08:00
|
|
|
// PrintStackTrace - In the case of a program crash or fault, print out a stack
|
|
|
|
// trace so that the user has an indication of why and where we died.
|
|
|
|
//
|
|
|
|
// On glibc systems we have the 'backtrace' function, which works nicely, but
|
2010-11-30 02:16:10 +08:00
|
|
|
// doesn't demangle symbols.
|
2015-03-06 03:10:52 +08:00
|
|
|
void llvm::sys::PrintStackTrace(raw_ostream &OS) {
|
2016-10-01 04:04:24 +08:00
|
|
|
#if ENABLE_BACKTRACES
|
2016-05-21 05:18:12 +08:00
|
|
|
static void *StackTrace[256];
|
|
|
|
int depth = 0;
|
|
|
|
#if defined(HAVE_BACKTRACE)
|
2004-08-30 03:22:48 +08:00
|
|
|
// Use backtrace() to output a backtrace on Linux systems with glibc.
|
2016-05-21 05:18:12 +08:00
|
|
|
if (!depth)
|
|
|
|
depth = backtrace(StackTrace, static_cast<int>(array_lengthof(StackTrace)));
|
|
|
|
#endif
|
2016-09-30 05:07:57 +08:00
|
|
|
#if defined(HAVE__UNWIND_BACKTRACE)
|
2016-05-21 05:18:12 +08:00
|
|
|
// Try _Unwind_Backtrace() if backtrace() failed.
|
|
|
|
if (!depth)
|
|
|
|
depth = unwindBacktrace(StackTrace,
|
2008-05-06 02:30:58 +08:00
|
|
|
static_cast<int>(array_lengthof(StackTrace)));
|
2016-05-21 05:18:12 +08:00
|
|
|
#endif
|
|
|
|
if (!depth)
|
|
|
|
return;
|
|
|
|
|
2016-06-09 08:53:21 +08:00
|
|
|
if (printSymbolizedStackTrace(Argv0, StackTrace, depth, OS))
|
2014-10-11 06:06:59 +08:00
|
|
|
return;
|
2016-10-06 17:32:16 +08:00
|
|
|
#if HAVE_DLFCN_H && __GNUG__ && !defined(__CYGWIN__)
|
2008-12-06 04:12:48 +08:00
|
|
|
int width = 0;
|
|
|
|
for (int i = 0; i < depth; ++i) {
|
|
|
|
Dl_info dlinfo;
|
|
|
|
dladdr(StackTrace[i], &dlinfo);
|
2009-02-11 01:56:28 +08:00
|
|
|
const char* name = strrchr(dlinfo.dli_fname, '/');
|
2008-12-06 04:12:48 +08:00
|
|
|
|
|
|
|
int nwidth;
|
2014-04-28 12:05:08 +08:00
|
|
|
if (!name) nwidth = strlen(dlinfo.dli_fname);
|
|
|
|
else nwidth = strlen(name) - 1;
|
2008-12-06 04:12:48 +08:00
|
|
|
|
|
|
|
if (nwidth > width) width = nwidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < depth; ++i) {
|
|
|
|
Dl_info dlinfo;
|
|
|
|
dladdr(StackTrace[i], &dlinfo);
|
|
|
|
|
2015-03-06 03:10:52 +08:00
|
|
|
OS << format("%-2d", i);
|
2008-12-06 04:12:48 +08:00
|
|
|
|
2009-02-11 01:56:28 +08:00
|
|
|
const char* name = strrchr(dlinfo.dli_fname, '/');
|
2015-03-06 03:10:52 +08:00
|
|
|
if (!name) OS << format(" %-*s", width, dlinfo.dli_fname);
|
|
|
|
else OS << format(" %-*s", width, name+1);
|
2008-12-06 04:12:48 +08:00
|
|
|
|
2015-03-06 03:10:52 +08:00
|
|
|
OS << format(" %#0*lx", (int)(sizeof(void*) * 2) + 2,
|
|
|
|
(unsigned long)StackTrace[i]);
|
2008-12-06 04:12:48 +08:00
|
|
|
|
2014-04-28 12:05:08 +08:00
|
|
|
if (dlinfo.dli_sname != nullptr) {
|
2015-03-06 03:10:52 +08:00
|
|
|
OS << ' ';
|
2013-04-28 15:47:04 +08:00
|
|
|
int res;
|
2016-09-07 03:16:48 +08:00
|
|
|
char* d = itaniumDemangle(dlinfo.dli_sname, nullptr, nullptr, &res);
|
2015-03-06 03:10:52 +08:00
|
|
|
if (!d) OS << dlinfo.dli_sname;
|
|
|
|
else OS << d;
|
2008-12-06 04:12:48 +08:00
|
|
|
free(d);
|
|
|
|
|
2013-01-29 03:34:42 +08:00
|
|
|
// FIXME: When we move to C++11, use %t length modifier. It's not in
|
|
|
|
// C++03 and causes gcc to issue warnings. Losing the upper 32 bits of
|
|
|
|
// the stack offset for a stack dump isn't likely to cause any problems.
|
2015-03-06 03:10:52 +08:00
|
|
|
OS << format(" + %u",(unsigned)((char*)StackTrace[i]-
|
|
|
|
(char*)dlinfo.dli_saddr));
|
2008-12-06 04:12:48 +08:00
|
|
|
}
|
2015-03-06 03:10:52 +08:00
|
|
|
OS << '\n';
|
2008-12-06 04:12:48 +08:00
|
|
|
}
|
2016-05-21 05:18:12 +08:00
|
|
|
#elif defined(HAVE_BACKTRACE)
|
2008-02-16 02:05:54 +08:00
|
|
|
backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
|
2004-08-30 03:22:48 +08:00
|
|
|
#endif
|
2008-12-06 04:12:48 +08:00
|
|
|
#endif
|
2004-08-30 03:22:48 +08:00
|
|
|
}
|
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
static void PrintStackTraceSignalHandler(void *) {
|
2015-03-06 03:10:52 +08:00
|
|
|
PrintStackTrace(llvm::errs());
|
2013-01-10 03:42:40 +08:00
|
|
|
}
|
|
|
|
|
2015-01-30 01:20:29 +08:00
|
|
|
void llvm::sys::DisableSystemDialogsOnCrash() {}
|
|
|
|
|
2012-09-06 11:01:43 +08:00
|
|
|
/// PrintStackTraceOnErrorSignal - When an error signal (such as SIGABRT or
|
2004-08-30 03:22:48 +08:00
|
|
|
/// SIGSEGV) is delivered to the process, print a stack trace and then exit.
|
2016-06-09 08:53:21 +08:00
|
|
|
void llvm::sys::PrintStackTraceOnErrorSignal(StringRef Argv0,
|
|
|
|
bool DisableCrashReporting) {
|
|
|
|
::Argv0 = Argv0;
|
|
|
|
|
2014-04-28 12:05:08 +08:00
|
|
|
AddSignalHandler(PrintStackTraceSignalHandler, nullptr);
|
2012-01-12 04:53:25 +08:00
|
|
|
|
2016-10-01 04:06:19 +08:00
|
|
|
#if defined(__APPLE__) && ENABLE_CRASH_OVERRIDES
|
2012-01-12 04:53:25 +08:00
|
|
|
// Environment variable to disable any kind of crash dialog.
|
2015-04-08 04:43:23 +08:00
|
|
|
if (DisableCrashReporting || getenv("LLVM_DISABLE_CRASH_REPORT")) {
|
2012-01-12 04:53:25 +08:00
|
|
|
mach_port_t self = mach_task_self();
|
|
|
|
|
|
|
|
exception_mask_t mask = EXC_MASK_CRASH;
|
|
|
|
|
2012-09-06 11:02:56 +08:00
|
|
|
kern_return_t ret = task_set_exception_ports(self,
|
2012-01-12 04:53:25 +08:00
|
|
|
mask,
|
2012-03-25 06:17:50 +08:00
|
|
|
MACH_PORT_NULL,
|
2012-09-06 11:02:56 +08:00
|
|
|
EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES,
|
2012-03-25 06:17:50 +08:00
|
|
|
THREAD_STATE_NONE);
|
2012-01-12 04:53:25 +08:00
|
|
|
(void)ret;
|
|
|
|
}
|
|
|
|
#endif
|
2004-08-30 03:22:48 +08:00
|
|
|
}
|