blob: 978d3b7ccb2a97af3485b5703360dd4906eed3b5 [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>
Brian Gaeke38ce7e52004-02-20 06:40:59 +000020#include "Config/config.h" // Get the signal handler return type
21#ifdef HAVE_EXECINFO_H
22# include <execinfo.h> // For backtrace().
23#endif
Chris Lattnerbac27a42002-04-18 19:53:53 +000024#include <signal.h>
Alkis Evlogimenos280f9c92004-02-19 07:36:35 +000025#include <unistd.h>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattner01e770a2003-05-22 21:59:35 +000028static std::vector<std::string> FilesToRemove;
Chris Lattnerbac27a42002-04-18 19:53:53 +000029
30// IntSigs - Signals that may interrupt the program at any time.
31static const int IntSigs[] = {
32 SIGHUP, SIGINT, SIGQUIT, SIGKILL, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
33};
34static const int *IntSigsEnd = IntSigs + sizeof(IntSigs)/sizeof(IntSigs[0]);
35
36// KillSigs - Signals that are synchronous with the program that will cause it
37// to die.
38static const int KillSigs[] = {
Chris Lattner7c97cee2002-09-13 14:57:24 +000039 SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ
40#ifdef SIGEMT
41 , SIGEMT
42#endif
Chris Lattnerbac27a42002-04-18 19:53:53 +000043};
44static const int *KillSigsEnd = KillSigs + sizeof(KillSigs)/sizeof(KillSigs[0]);
45
Alkis Evlogimenos280f9c92004-02-19 07:36:35 +000046static void* StackTrace[256];
Chris Lattnerbac27a42002-04-18 19:53:53 +000047
48// SignalHandler - The signal handler that runs...
John Criswell7a73b802003-06-30 21:59:07 +000049static RETSIGTYPE SignalHandler(int Sig) {
Chris Lattnerbac27a42002-04-18 19:53:53 +000050 while (!FilesToRemove.empty()) {
51 std::remove(FilesToRemove.back().c_str());
52 FilesToRemove.pop_back();
53 }
54
Anand Shuklacfb22d32002-06-25 20:55:50 +000055 if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd)
Chris Lattnerbac27a42002-04-18 19:53:53 +000056 exit(1); // If this is an interrupt signal, exit the program
57
Alkis Evlogimenos280f9c92004-02-19 07:36:35 +000058 // Otherwise if it is a fault (like SEGV) output the stacktrace to
Brian Gaeke38ce7e52004-02-20 06:40:59 +000059 // STDERR (if we can) and reissue the signal to die...
60#ifdef HAVE_BACKTRACE
61 // Use backtrace() to output a backtrace on Linux systems with glibc.
62 int depth = backtrace(StackTrace, sizeof(StackTrace)/sizeof(StackTrace[0]));
63 backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
64#endif
Chris Lattner39602b22003-05-27 16:25:04 +000065 signal(Sig, SIG_DFL);
Chris Lattnerbac27a42002-04-18 19:53:53 +000066}
67
68static void RegisterHandler(int Signal) { signal(Signal, SignalHandler); }
69
70// RemoveFileOnSignal - The public API
Chris Lattner2cdd21c2003-12-14 21:35:53 +000071void llvm::RemoveFileOnSignal(const std::string &Filename) {
Chris Lattnerbac27a42002-04-18 19:53:53 +000072 FilesToRemove.push_back(Filename);
73
Anand Shuklacfb22d32002-06-25 20:55:50 +000074 std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
75 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
Chris Lattnerbac27a42002-04-18 19:53:53 +000076}
Chris Lattner982774c2004-02-19 20:03:14 +000077
78/// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
79/// SIGSEGV) is delivered to the process, print a stack trace and then exit.
80void llvm::PrintStackTraceOnErrorSignal() {
81 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
82}