blob: b3baed066836221893eebc0b8c3c476fde8a3327 [file] [log] [blame]
Chris Lattnerbac27a42002-04-18 19:53:53 +00001//===- Signals.cpp - Signal Handling support ------------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerbac27a42002-04-18 19:53:53 +00009//
10// This file defines some helpful functions for dealing with the possibility of
Misha Brukman950971d2003-09-16 15:31:46 +000011// Unix signals occuring while your program is running.
Chris Lattnerbac27a42002-04-18 19:53:53 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "Support/Signals.h"
16#include <vector>
17#include <algorithm>
18#include <cstdlib>
19#include <cstdio>
Chris Lattner65596152004-02-19 21:21:23 +000020//#include <execinfo.h>
Chris Lattnerbac27a42002-04-18 19:53:53 +000021#include <signal.h>
Alkis Evlogimenos280f9c92004-02-19 07:36:35 +000022#include <unistd.h>
Chris Lattnerfafac122003-08-01 19:16:29 +000023#include "Config/config.h" // Get the signal handler return type
Chris Lattner2cdd21c2003-12-14 21:35:53 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattner01e770a2003-05-22 21:59:35 +000026static std::vector<std::string> FilesToRemove;
Chris Lattnerbac27a42002-04-18 19:53:53 +000027
28// IntSigs - Signals that may interrupt the program at any time.
29static const int IntSigs[] = {
30 SIGHUP, SIGINT, SIGQUIT, SIGKILL, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
31};
32static const int *IntSigsEnd = IntSigs + sizeof(IntSigs)/sizeof(IntSigs[0]);
33
34// KillSigs - Signals that are synchronous with the program that will cause it
35// to die.
36static const int KillSigs[] = {
Chris Lattner7c97cee2002-09-13 14:57:24 +000037 SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ
38#ifdef SIGEMT
39 , SIGEMT
40#endif
Chris Lattnerbac27a42002-04-18 19:53:53 +000041};
42static const int *KillSigsEnd = KillSigs + sizeof(KillSigs)/sizeof(KillSigs[0]);
43
Alkis Evlogimenos280f9c92004-02-19 07:36:35 +000044static void* StackTrace[256];
Chris Lattnerbac27a42002-04-18 19:53:53 +000045
46// SignalHandler - The signal handler that runs...
John Criswell7a73b802003-06-30 21:59:07 +000047static RETSIGTYPE SignalHandler(int Sig) {
Chris Lattnerbac27a42002-04-18 19:53:53 +000048 while (!FilesToRemove.empty()) {
49 std::remove(FilesToRemove.back().c_str());
50 FilesToRemove.pop_back();
51 }
52
Anand Shuklacfb22d32002-06-25 20:55:50 +000053 if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd)
Chris Lattnerbac27a42002-04-18 19:53:53 +000054 exit(1); // If this is an interrupt signal, exit the program
55
Alkis Evlogimenos280f9c92004-02-19 07:36:35 +000056 // Otherwise if it is a fault (like SEGV) output the stacktrace to
57 // STDERR and reissue the signal to die...
Chris Lattner65596152004-02-19 21:21:23 +000058 //int depth = backtrace(StackTrace, sizeof(StackTrace)/sizeof(StackTrace[0]));
59 //backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
Chris Lattner39602b22003-05-27 16:25:04 +000060 signal(Sig, SIG_DFL);
Chris Lattnerbac27a42002-04-18 19:53:53 +000061}
62
63static void RegisterHandler(int Signal) { signal(Signal, SignalHandler); }
64
65// RemoveFileOnSignal - The public API
Chris Lattner2cdd21c2003-12-14 21:35:53 +000066void llvm::RemoveFileOnSignal(const std::string &Filename) {
Chris Lattnerbac27a42002-04-18 19:53:53 +000067 FilesToRemove.push_back(Filename);
68
Anand Shuklacfb22d32002-06-25 20:55:50 +000069 std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
70 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
Chris Lattnerbac27a42002-04-18 19:53:53 +000071}
Chris Lattner982774c2004-02-19 20:03:14 +000072
73/// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
74/// SIGSEGV) is delivered to the process, print a stack trace and then exit.
75void llvm::PrintStackTraceOnErrorSignal() {
76 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
77}