2004-08-29 19:22:48 +00:00
|
|
|
//===- Signals.cpp - Generic Unix Signals Implementation -----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2004-08-29 19:22:48 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines some helpful functions for dealing with the possibility of
|
|
|
|
// Unix signals occuring while your program is running.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Unix.h"
|
2007-09-07 04:06:50 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2009-08-17 17:07:22 +00:00
|
|
|
#include "llvm/System/Mutex.h"
|
2004-08-29 19:22:48 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
2004-12-27 06:16:52 +00:00
|
|
|
#if HAVE_EXECINFO_H
|
2004-08-29 19:22:48 +00:00
|
|
|
# include <execinfo.h> // For backtrace().
|
|
|
|
#endif
|
2004-12-27 06:16:52 +00:00
|
|
|
#if HAVE_SIGNAL_H
|
2004-08-29 19:22:48 +00:00
|
|
|
#include <signal.h>
|
2004-12-27 06:16:52 +00:00
|
|
|
#endif
|
2007-04-07 18:52:17 +00:00
|
|
|
#if HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
2008-12-05 20:12:48 +00:00
|
|
|
#if HAVE_DLFCN_H && __GNUG__
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <cxxabi.h>
|
|
|
|
#endif
|
2006-08-01 17:59:14 +00:00
|
|
|
using namespace llvm;
|
2004-08-29 19:22:48 +00:00
|
|
|
|
2009-03-23 05:42:29 +00:00
|
|
|
static RETSIGTYPE SignalHandler(int Sig); // defined below.
|
|
|
|
|
2009-08-17 17:07:22 +00:00
|
|
|
static SmartMutex<true> SignalsMutex;
|
|
|
|
|
2005-08-02 02:14:22 +00:00
|
|
|
/// InterruptFunction - The function to call if ctrl-c is pressed.
|
2008-04-10 21:11:47 +00:00
|
|
|
static void (*InterruptFunction)() = 0;
|
2005-08-02 02:14:22 +00:00
|
|
|
|
2010-03-17 07:08:12 +00:00
|
|
|
static std::vector<sys::Path> FilesToRemove;
|
|
|
|
static std::vector<std::pair<void(*)(void*), void*> > CallBacksToRun;
|
2004-08-29 19:22:48 +00:00
|
|
|
|
|
|
|
// IntSigs - Signals that may interrupt the program at any time.
|
2008-04-10 21:11:47 +00:00
|
|
|
static const int IntSigs[] = {
|
2004-08-29 19:22:48 +00:00
|
|
|
SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
|
|
|
|
};
|
2008-05-14 00:42:30 +00:00
|
|
|
static const int *const IntSigsEnd =
|
|
|
|
IntSigs + sizeof(IntSigs) / sizeof(IntSigs[0]);
|
2004-08-29 19:22:48 +00:00
|
|
|
|
|
|
|
// KillSigs - Signals that are synchronous with the program that will cause it
|
|
|
|
// to die.
|
2008-04-10 21:11:47 +00:00
|
|
|
static const int KillSigs[] = {
|
2010-02-12 00:37:46 +00:00
|
|
|
SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV
|
|
|
|
#ifdef SIGSYS
|
|
|
|
, SIGSYS
|
|
|
|
#endif
|
|
|
|
#ifdef SIGXCPU
|
|
|
|
, SIGXCPU
|
|
|
|
#endif
|
2010-02-14 18:20:09 +00:00
|
|
|
#ifdef SIGXFSZ
|
2010-02-12 00:37:46 +00:00
|
|
|
, SIGXFSZ
|
|
|
|
#endif
|
2004-08-29 19:22:48 +00:00
|
|
|
#ifdef SIGEMT
|
|
|
|
, SIGEMT
|
|
|
|
#endif
|
|
|
|
};
|
2008-05-14 00:42:30 +00:00
|
|
|
static const int *const KillSigsEnd =
|
|
|
|
KillSigs + sizeof(KillSigs) / sizeof(KillSigs[0]);
|
2004-08-29 19:22:48 +00:00
|
|
|
|
2009-03-23 05:55:36 +00:00
|
|
|
static unsigned NumRegisteredSignals = 0;
|
|
|
|
static struct {
|
|
|
|
struct sigaction SA;
|
|
|
|
int SigNo;
|
|
|
|
} RegisteredSignalInfo[(sizeof(IntSigs)+sizeof(KillSigs))/sizeof(KillSigs[0])];
|
|
|
|
|
|
|
|
|
2009-03-23 05:42:29 +00:00
|
|
|
static void RegisterHandler(int Signal) {
|
2009-03-23 05:55:36 +00:00
|
|
|
assert(NumRegisteredSignals <
|
|
|
|
sizeof(RegisteredSignalInfo)/sizeof(RegisteredSignalInfo[0]) &&
|
|
|
|
"Out of space for signal handlers!");
|
|
|
|
|
|
|
|
struct sigaction NewHandler;
|
|
|
|
|
|
|
|
NewHandler.sa_handler = SignalHandler;
|
|
|
|
NewHandler.sa_flags = SA_NODEFER|SA_RESETHAND;
|
|
|
|
sigemptyset(&NewHandler.sa_mask);
|
|
|
|
|
|
|
|
// Install the new handler, save the old one in RegisteredSignalInfo.
|
|
|
|
sigaction(Signal, &NewHandler,
|
|
|
|
&RegisteredSignalInfo[NumRegisteredSignals].SA);
|
|
|
|
RegisteredSignalInfo[NumRegisteredSignals].SigNo = Signal;
|
|
|
|
++NumRegisteredSignals;
|
2009-03-23 05:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void RegisterHandlers() {
|
2009-03-23 05:55:36 +00:00
|
|
|
// If the handlers are already registered, we're done.
|
|
|
|
if (NumRegisteredSignals != 0) return;
|
|
|
|
|
2009-03-23 05:42:29 +00:00
|
|
|
std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
|
|
|
|
std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void UnregisterHandlers() {
|
2009-03-23 05:55:36 +00: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 06:46:20 +00:00
|
|
|
sigaction(RegisteredSignalInfo[i].SigNo,
|
|
|
|
&RegisteredSignalInfo[i].SA, 0);
|
2009-03-23 05:55:36 +00:00
|
|
|
NumRegisteredSignals = 0;
|
2009-03-07 08:15:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-23 05:42:29 +00:00
|
|
|
|
2009-03-07 08:15:47 +00:00
|
|
|
// SignalHandler - The signal handler that runs.
|
2009-03-04 21:21:36 +00:00
|
|
|
static RETSIGTYPE SignalHandler(int Sig) {
|
2009-03-05 18:22:14 +00: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 05:42:29 +00:00
|
|
|
UnregisterHandlers();
|
2009-03-07 08:15:47 +00:00
|
|
|
|
|
|
|
// Unmask all potentially blocked kill signals.
|
|
|
|
sigset_t SigMask;
|
|
|
|
sigfillset(&SigMask);
|
|
|
|
sigprocmask(SIG_UNBLOCK, &SigMask, 0);
|
2009-03-05 18:22:14 +00:00
|
|
|
|
2009-08-17 17:07:22 +00:00
|
|
|
SignalsMutex.acquire();
|
2010-03-17 07:08:12 +00:00
|
|
|
while (!FilesToRemove.empty()) {
|
|
|
|
FilesToRemove.back().eraseFromDisk(true);
|
|
|
|
FilesToRemove.pop_back();
|
|
|
|
}
|
2009-03-04 21:21:36 +00:00
|
|
|
|
|
|
|
if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd) {
|
|
|
|
if (InterruptFunction) {
|
|
|
|
void (*IF)() = InterruptFunction;
|
2009-08-17 17:07:22 +00:00
|
|
|
SignalsMutex.release();
|
2009-03-04 21:21:36 +00:00
|
|
|
InterruptFunction = 0;
|
|
|
|
IF(); // run the interrupt function.
|
|
|
|
return;
|
|
|
|
}
|
2009-08-17 17:07:22 +00:00
|
|
|
|
|
|
|
SignalsMutex.release();
|
2009-04-12 23:33:13 +00:00
|
|
|
raise(Sig); // Execute the default handler.
|
|
|
|
return;
|
2009-03-04 21:21:36 +00:00
|
|
|
}
|
|
|
|
|
2009-08-17 17:07:22 +00:00
|
|
|
SignalsMutex.release();
|
|
|
|
|
2009-03-04 21:21:36 +00:00
|
|
|
// Otherwise if it is a fault (like SEGV) run any handler.
|
2010-03-17 07:08:12 +00:00
|
|
|
for (unsigned i = 0, e = CallBacksToRun.size(); i != e; ++i)
|
|
|
|
CallBacksToRun[i].first(CallBacksToRun[i].second);
|
2009-03-04 21:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-03-08 19:13:45 +00:00
|
|
|
void llvm::sys::SetInterruptFunction(void (*IF)()) {
|
2009-08-17 17:07:22 +00:00
|
|
|
SignalsMutex.acquire();
|
2009-03-04 21:21:36 +00:00
|
|
|
InterruptFunction = IF;
|
2009-08-17 17:07:22 +00:00
|
|
|
SignalsMutex.release();
|
2009-03-23 05:42:29 +00:00
|
|
|
RegisterHandlers();
|
2009-03-04 21:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveFileOnSignal - The public API
|
2009-03-08 19:13:45 +00:00
|
|
|
bool llvm::sys::RemoveFileOnSignal(const sys::Path &Filename,
|
|
|
|
std::string* ErrMsg) {
|
2009-08-17 17:07:22 +00:00
|
|
|
SignalsMutex.acquire();
|
2010-03-17 07:08:12 +00:00
|
|
|
FilesToRemove.push_back(Filename);
|
2009-03-04 21:21:36 +00:00
|
|
|
|
2009-08-17 17:07:22 +00:00
|
|
|
SignalsMutex.release();
|
|
|
|
|
2009-03-23 05:42:29 +00:00
|
|
|
RegisterHandlers();
|
2009-03-04 21:21:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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-08 19:13:45 +00:00
|
|
|
void llvm::sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) {
|
2010-03-17 07:08:12 +00:00
|
|
|
CallBacksToRun.push_back(std::make_pair(FnPtr, Cookie));
|
2009-03-23 05:42:29 +00:00
|
|
|
RegisterHandlers();
|
2009-03-04 21:21:36 +00:00
|
|
|
}
|
|
|
|
|
2004-08-29 19:22:48 +00: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
|
2008-02-15 18:05:54 +00:00
|
|
|
// doesn't demangle symbols.
|
2009-03-04 21:21:36 +00:00
|
|
|
static void PrintStackTrace(void *) {
|
2004-08-29 19:22:48 +00:00
|
|
|
#ifdef HAVE_BACKTRACE
|
2009-03-04 21:21:36 +00:00
|
|
|
static void* StackTrace[256];
|
2004-08-29 19:22:48 +00:00
|
|
|
// Use backtrace() to output a backtrace on Linux systems with glibc.
|
2008-05-05 18:30:58 +00:00
|
|
|
int depth = backtrace(StackTrace,
|
|
|
|
static_cast<int>(array_lengthof(StackTrace)));
|
2008-12-05 20:12:48 +00:00
|
|
|
#if HAVE_DLFCN_H && __GNUG__
|
|
|
|
int width = 0;
|
|
|
|
for (int i = 0; i < depth; ++i) {
|
|
|
|
Dl_info dlinfo;
|
|
|
|
dladdr(StackTrace[i], &dlinfo);
|
2009-02-10 17:56:28 +00:00
|
|
|
const char* name = strrchr(dlinfo.dli_fname, '/');
|
2008-12-05 20:12:48 +00:00
|
|
|
|
|
|
|
int nwidth;
|
|
|
|
if (name == NULL) nwidth = strlen(dlinfo.dli_fname);
|
|
|
|
else nwidth = strlen(name) - 1;
|
|
|
|
|
|
|
|
if (nwidth > width) width = nwidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < depth; ++i) {
|
|
|
|
Dl_info dlinfo;
|
|
|
|
dladdr(StackTrace[i], &dlinfo);
|
|
|
|
|
2009-10-30 02:45:10 +00:00
|
|
|
fprintf(stderr, "%-2d", i);
|
2008-12-05 20:12:48 +00:00
|
|
|
|
2009-02-10 17:56:28 +00:00
|
|
|
const char* name = strrchr(dlinfo.dli_fname, '/');
|
2008-12-05 20:12:48 +00:00
|
|
|
if (name == NULL) fprintf(stderr, " %-*s", width, dlinfo.dli_fname);
|
|
|
|
else fprintf(stderr, " %-*s", width, name+1);
|
|
|
|
|
2008-12-05 23:39:24 +00:00
|
|
|
fprintf(stderr, " %#0*lx",
|
|
|
|
(int)(sizeof(void*) * 2) + 2, (unsigned long)StackTrace[i]);
|
2008-12-05 20:12:48 +00:00
|
|
|
|
|
|
|
if (dlinfo.dli_sname != NULL) {
|
|
|
|
int res;
|
|
|
|
fputc(' ', stderr);
|
|
|
|
char* d = abi::__cxa_demangle(dlinfo.dli_sname, NULL, NULL, &res);
|
|
|
|
if (d == NULL) fputs(dlinfo.dli_sname, stderr);
|
|
|
|
else fputs(d, stderr);
|
|
|
|
free(d);
|
|
|
|
|
|
|
|
fprintf(stderr, " + %tu",(char*)StackTrace[i]-(char*)dlinfo.dli_saddr);
|
|
|
|
}
|
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
|
|
|
#else
|
2008-02-15 18:05:54 +00:00
|
|
|
backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
|
2004-08-29 19:22:48 +00:00
|
|
|
#endif
|
2008-12-05 20:12:48 +00:00
|
|
|
#endif
|
2004-08-29 19:22:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
|
|
|
|
/// SIGSEGV) is delivered to the process, print a stack trace and then exit.
|
2009-03-08 19:13:45 +00:00
|
|
|
void llvm::sys::PrintStackTraceOnErrorSignal() {
|
2009-03-04 21:21:36 +00:00
|
|
|
AddSignalHandler(PrintStackTrace, 0);
|
2004-08-29 19:22:48 +00:00
|
|
|
}
|
2009-03-04 21:21:36 +00:00
|
|
|
|