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> |
Brian Gaeke | 38ce7e5 | 2004-02-20 06:40:59 +0000 | [diff] [blame] | 20 | #include "Config/config.h" // Get the signal handler return type |
| 21 | #ifdef HAVE_EXECINFO_H |
| 22 | # include <execinfo.h> // For backtrace(). |
| 23 | #endif |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 24 | #include <signal.h> |
Alkis Evlogimenos | 280f9c9 | 2004-02-19 07:36:35 +0000 | [diff] [blame] | 25 | #include <unistd.h> |
Chris Lattner | b9632be | 2004-02-21 21:06:19 +0000 | [diff] [blame] | 26 | #include <sys/wait.h> |
| 27 | #include <cerrno> |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 01e770a | 2003-05-22 21:59:35 +0000 | [diff] [blame] | 30 | static std::vector<std::string> FilesToRemove; |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 31 | |
| 32 | // IntSigs - Signals that may interrupt the program at any time. |
| 33 | static const int IntSigs[] = { |
| 34 | SIGHUP, SIGINT, SIGQUIT, SIGKILL, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2 |
| 35 | }; |
| 36 | static const int *IntSigsEnd = IntSigs + sizeof(IntSigs)/sizeof(IntSigs[0]); |
| 37 | |
| 38 | // KillSigs - Signals that are synchronous with the program that will cause it |
| 39 | // to die. |
| 40 | static const int KillSigs[] = { |
Chris Lattner | 7c97cee | 2002-09-13 14:57:24 +0000 | [diff] [blame] | 41 | SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ |
| 42 | #ifdef SIGEMT |
| 43 | , SIGEMT |
| 44 | #endif |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 45 | }; |
| 46 | static const int *KillSigsEnd = KillSigs + sizeof(KillSigs)/sizeof(KillSigs[0]); |
| 47 | |
Chris Lattner | b9632be | 2004-02-21 21:06:19 +0000 | [diff] [blame] | 48 | #ifdef HAVE_BACKTRACE |
Alkis Evlogimenos | 280f9c9 | 2004-02-19 07:36:35 +0000 | [diff] [blame] | 49 | static void* StackTrace[256]; |
Chris Lattner | b9632be | 2004-02-21 21:06:19 +0000 | [diff] [blame] | 50 | #endif |
| 51 | |
| 52 | |
| 53 | // PrintStackTrace - In the case of a program crash or fault, print out a stack |
| 54 | // trace so that the user has an indication of why and where we died. |
| 55 | // |
| 56 | // On glibc systems we have the 'backtrace' function, which works nicely, but |
| 57 | // doesn't demangle symbols. In order to backtrace symbols, we fork and exec a |
| 58 | // 'c++filt' process to do the demangling. This seems like the simplest and |
| 59 | // most robust solution when we can't allocate memory (such as in a signal |
| 60 | // handler). If we can't find 'c++filt', we fallback to printing mangled names. |
| 61 | // |
| 62 | static void PrintStackTrace() { |
| 63 | #ifdef HAVE_BACKTRACE |
| 64 | // Use backtrace() to output a backtrace on Linux systems with glibc. |
| 65 | int depth = backtrace(StackTrace, sizeof(StackTrace)/sizeof(StackTrace[0])); |
| 66 | |
| 67 | // Create a one-way unix pipe. The backtracing process writes to PipeFDs[1], |
| 68 | // the c++filt process reads from PipeFDs[0]. |
| 69 | int PipeFDs[2]; |
| 70 | if (pipe(PipeFDs)) { |
| 71 | backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | switch (pid_t ChildPID = fork()) { |
| 76 | case -1: // Error forking, print mangled stack trace |
| 77 | close(PipeFDs[0]); |
| 78 | close(PipeFDs[1]); |
| 79 | backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO); |
| 80 | return; |
| 81 | default: // backtracing process |
| 82 | close(PipeFDs[0]); // Close the reader side. |
| 83 | |
| 84 | // Print the mangled backtrace into the pipe. |
| 85 | backtrace_symbols_fd(StackTrace, depth, PipeFDs[1]); |
| 86 | close(PipeFDs[1]); // We are done writing. |
| 87 | while (waitpid(ChildPID, 0, 0) == -1) |
| 88 | if (errno != EINTR) break; |
| 89 | return; |
| 90 | |
| 91 | case 0: // c++filt process |
| 92 | close(PipeFDs[1]); // Close the writer side. |
| 93 | dup2(PipeFDs[0], 0); // Read from standard input |
| 94 | close(PipeFDs[0]); // Close the old descriptor |
| 95 | dup2(2, 1); // Revector stdout -> stderr |
| 96 | |
| 97 | // Try to run c++filt or gc++filt. If neither is found, call back on 'cat' |
| 98 | // to print the mangled stack trace. If we can't find cat, just exit. |
| 99 | execlp("c++filt", "c++filt", 0); |
| 100 | execlp("gc++filt", "gc++filt", 0); |
| 101 | execlp("cat", "cat", 0); |
Misha Brukman | ac2ecd0 | 2004-02-21 21:51:41 +0000 | [diff] [blame^] | 102 | execlp("/bin/cat", "cat", 0); |
Chris Lattner | b9632be | 2004-02-21 21:06:19 +0000 | [diff] [blame] | 103 | exit(0); |
| 104 | } |
| 105 | #endif |
| 106 | } |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 107 | |
| 108 | // SignalHandler - The signal handler that runs... |
John Criswell | 7a73b80 | 2003-06-30 21:59:07 +0000 | [diff] [blame] | 109 | static RETSIGTYPE SignalHandler(int Sig) { |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 110 | while (!FilesToRemove.empty()) { |
| 111 | std::remove(FilesToRemove.back().c_str()); |
| 112 | FilesToRemove.pop_back(); |
| 113 | } |
| 114 | |
Anand Shukla | cfb22d3 | 2002-06-25 20:55:50 +0000 | [diff] [blame] | 115 | if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd) |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 116 | exit(1); // If this is an interrupt signal, exit the program |
| 117 | |
Alkis Evlogimenos | 280f9c9 | 2004-02-19 07:36:35 +0000 | [diff] [blame] | 118 | // Otherwise if it is a fault (like SEGV) output the stacktrace to |
Brian Gaeke | 38ce7e5 | 2004-02-20 06:40:59 +0000 | [diff] [blame] | 119 | // STDERR (if we can) and reissue the signal to die... |
Chris Lattner | b9632be | 2004-02-21 21:06:19 +0000 | [diff] [blame] | 120 | PrintStackTrace(); |
Chris Lattner | 39602b2 | 2003-05-27 16:25:04 +0000 | [diff] [blame] | 121 | signal(Sig, SIG_DFL); |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static void RegisterHandler(int Signal) { signal(Signal, SignalHandler); } |
| 125 | |
| 126 | // RemoveFileOnSignal - The public API |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 127 | void llvm::RemoveFileOnSignal(const std::string &Filename) { |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 128 | FilesToRemove.push_back(Filename); |
| 129 | |
Anand Shukla | cfb22d3 | 2002-06-25 20:55:50 +0000 | [diff] [blame] | 130 | std::for_each(IntSigs, IntSigsEnd, RegisterHandler); |
| 131 | std::for_each(KillSigs, KillSigsEnd, RegisterHandler); |
Chris Lattner | bac27a4 | 2002-04-18 19:53:53 +0000 | [diff] [blame] | 132 | } |
Chris Lattner | 982774c | 2004-02-19 20:03:14 +0000 | [diff] [blame] | 133 | |
| 134 | /// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or |
| 135 | /// SIGSEGV) is delivered to the process, print a stack trace and then exit. |
| 136 | void llvm::PrintStackTraceOnErrorSignal() { |
| 137 | std::for_each(KillSigs, KillSigsEnd, RegisterHandler); |
| 138 | } |