1 //===- Signals.cpp - Signal Handling support ------------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines some helpful functions for dealing with the possibility of
11 // Unix signals occuring while your program is running.
13 //===----------------------------------------------------------------------===//
15 #include "Support/Signals.h"
20 #include "Config/config.h" // Get the signal handler return type
21 #ifdef HAVE_EXECINFO_H
22 # include <execinfo.h> // For backtrace().
30 static std::vector<std::string> FilesToRemove;
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
36 static const int *IntSigsEnd = IntSigs + sizeof(IntSigs)/sizeof(IntSigs[0]);
38 // KillSigs - Signals that are synchronous with the program that will cause it
40 static const int KillSigs[] = {
41 SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ
46 static const int *KillSigsEnd = KillSigs + sizeof(KillSigs)/sizeof(KillSigs[0]);
49 static void* StackTrace[256];
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.
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.
62 static void PrintStackTrace() {
64 // Use backtrace() to output a backtrace on Linux systems with glibc.
65 int depth = backtrace(StackTrace, sizeof(StackTrace)/sizeof(StackTrace[0]));
67 // Create a one-way unix pipe. The backtracing process writes to PipeFDs[1],
68 // the c++filt process reads from PipeFDs[0].
71 backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
75 switch (pid_t ChildPID = fork()) {
76 case -1: // Error forking, print mangled stack trace
79 backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
81 default: // backtracing process
82 close(PipeFDs[0]); // Close the reader side.
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;
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
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);
102 execlp("/bin/cat", "cat", 0);
108 // SignalHandler - The signal handler that runs...
109 static RETSIGTYPE SignalHandler(int Sig) {
110 while (!FilesToRemove.empty()) {
111 std::remove(FilesToRemove.back().c_str());
112 FilesToRemove.pop_back();
115 if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd)
116 exit(1); // If this is an interrupt signal, exit the program
118 // Otherwise if it is a fault (like SEGV) output the stacktrace to
119 // STDERR (if we can) and reissue the signal to die...
121 signal(Sig, SIG_DFL);
124 static void RegisterHandler(int Signal) { signal(Signal, SignalHandler); }
126 // RemoveFileOnSignal - The public API
127 void llvm::RemoveFileOnSignal(const std::string &Filename) {
128 FilesToRemove.push_back(Filename);
130 std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
131 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
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);