Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 1 | //===- Signals.cpp - Signal Handling support ------------------------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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 Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines some helpful functions for dealing with the possibility of |
Misha Brukman | 950971d | 2003-09-16 15:31:46 +0000 | [diff] [blame] | 11 | // Unix signals occuring while your program is running. |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "Support/Signals.h" |
| 16 | #include <vector> |
| 17 | #include <algorithm> |
| 18 | #include <cstdlib> |
| 19 | #include <cstdio> |
Chris Lattner | 6559615 | 2004-02-19 21:21:23 +0000 | [diff] [blame] | 20 | //#include <execinfo.h> |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 21 | #include <signal.h> |
Alkis Evlogimenos | 280f9c9 | 2004-02-19 07:36:35 +0000 | [diff] [blame] | 22 | #include <unistd.h> |
Chris Lattner | fafac12 | 2003-08-01 19:16:29 +0000 | [diff] [blame] | 23 | #include "Config/config.h" // Get the signal handler return type |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 01e770a | 2003-05-22 21:59:35 +0000 | [diff] [blame] | 26 | static std::vector<std::string> FilesToRemove; |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 27 | |
| 28 | // IntSigs - Signals that may interrupt the program at any time. |
| 29 | static const int IntSigs[] = { |
| 30 | SIGHUP, SIGINT, SIGQUIT, SIGKILL, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2 |
| 31 | }; |
| 32 | static 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. |
| 36 | static const int KillSigs[] = { |
Chris Lattner | 7c97cee | 2002-09-13 14:57:24 +0000 | [diff] [blame] | 37 | SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ |
| 38 | #ifdef SIGEMT |
| 39 | , SIGEMT |
| 40 | #endif |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 41 | }; |
| 42 | static const int *KillSigsEnd = KillSigs + sizeof(KillSigs)/sizeof(KillSigs[0]); |
| 43 | |
Alkis Evlogimenos | 280f9c9 | 2004-02-19 07:36:35 +0000 | [diff] [blame] | 44 | static void* StackTrace[256]; |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 45 | |
| 46 | // SignalHandler - The signal handler that runs... |
John Criswell | 7a73b80 | 2003-06-30 21:59:07 +0000 | [diff] [blame] | 47 | static RETSIGTYPE SignalHandler(int Sig) { |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 48 | while (!FilesToRemove.empty()) { |
| 49 | std::remove(FilesToRemove.back().c_str()); |
| 50 | FilesToRemove.pop_back(); |
| 51 | } |
| 52 | |
Anand Shukla | cfb22d3 | 2002-06-25 20:55:50 +0000 | [diff] [blame] | 53 | if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd) |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 54 | exit(1); // If this is an interrupt signal, exit the program |
| 55 | |
Alkis Evlogimenos | 280f9c9 | 2004-02-19 07:36:35 +0000 | [diff] [blame] | 56 | // Otherwise if it is a fault (like SEGV) output the stacktrace to |
| 57 | // STDERR and reissue the signal to die... |
Chris Lattner | 6559615 | 2004-02-19 21:21:23 +0000 | [diff] [blame] | 58 | //int depth = backtrace(StackTrace, sizeof(StackTrace)/sizeof(StackTrace[0])); |
| 59 | //backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO); |
Chris Lattner | 39602b2 | 2003-05-27 16:25:04 +0000 | [diff] [blame] | 60 | signal(Sig, SIG_DFL); |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static void RegisterHandler(int Signal) { signal(Signal, SignalHandler); } |
| 64 | |
| 65 | // RemoveFileOnSignal - The public API |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 66 | void llvm::RemoveFileOnSignal(const std::string &Filename) { |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 67 | FilesToRemove.push_back(Filename); |
| 68 | |
Anand Shukla | cfb22d3 | 2002-06-25 20:55:50 +0000 | [diff] [blame] | 69 | std::for_each(IntSigs, IntSigsEnd, RegisterHandler); |
| 70 | std::for_each(KillSigs, KillSigsEnd, RegisterHandler); |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 71 | } |
Chris Lattner | 982774c | 2004-02-19 20:03:14 +0000 | [diff] [blame] | 72 | |
| 73 | /// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or |
| 74 | /// SIGSEGV) is delivered to the process, print a stack trace and then exit. |
| 75 | void llvm::PrintStackTraceOnErrorSignal() { |
| 76 | std::for_each(KillSigs, KillSigsEnd, RegisterHandler); |
| 77 | } |