1 //===- Signals.cpp - Generic Unix Signals Implementation -----*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
18 #ifdef HAVE_EXECINFO_H
19 # include <execinfo.h> // For backtrace().
26 std::vector<std::string> *FilesToRemove = 0 ;
27 std::vector<llvm::sys::Path> *DirectoriesToRemove = 0;
29 // IntSigs - Signals that may interrupt the program at any time.
30 const int IntSigs[] = {
31 SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
33 const int *IntSigsEnd = IntSigs + sizeof(IntSigs)/sizeof(IntSigs[0]);
35 // KillSigs - Signals that are synchronous with the program that will cause it
37 const int KillSigs[] = {
38 SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ
43 const int *KillSigsEnd = KillSigs + sizeof(KillSigs)/sizeof(KillSigs[0]);
46 void* StackTrace[256];
49 // PrintStackTrace - In the case of a program crash or fault, print out a stack
50 // trace so that the user has an indication of why and where we died.
52 // On glibc systems we have the 'backtrace' function, which works nicely, but
53 // doesn't demangle symbols. In order to backtrace symbols, we fork and exec a
54 // 'c++filt' process to do the demangling. This seems like the simplest and
55 // most robust solution when we can't allocate memory (such as in a signal
56 // handler). If we can't find 'c++filt', we fallback to printing mangled names.
58 void PrintStackTrace() {
60 // Use backtrace() to output a backtrace on Linux systems with glibc.
61 int depth = backtrace(StackTrace, sizeof(StackTrace)/sizeof(StackTrace[0]));
63 // Create a one-way unix pipe. The backtracing process writes to PipeFDs[1],
64 // the c++filt process reads from PipeFDs[0].
67 backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
71 switch (pid_t ChildPID = fork()) {
72 case -1: // Error forking, print mangled stack trace
75 backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
77 default: // backtracing process
78 close(PipeFDs[0]); // Close the reader side.
80 // Print the mangled backtrace into the pipe.
81 backtrace_symbols_fd(StackTrace, depth, PipeFDs[1]);
82 close(PipeFDs[1]); // We are done writing.
83 while (waitpid(ChildPID, 0, 0) == -1)
84 if (errno != EINTR) break;
87 case 0: // c++filt process
88 close(PipeFDs[1]); // Close the writer side.
89 dup2(PipeFDs[0], 0); // Read from standard input
90 close(PipeFDs[0]); // Close the old descriptor
91 dup2(2, 1); // Revector stdout -> stderr
93 // Try to run c++filt or gc++filt. If neither is found, call back on 'cat'
94 // to print the mangled stack trace. If we can't find cat, just exit.
95 execlp("c++filt", "c++filt", 0);
96 execlp("gc++filt", "gc++filt", 0);
97 execlp("cat", "cat", 0);
98 execlp("/bin/cat", "cat", 0);
104 // SignalHandler - The signal handler that runs...
105 RETSIGTYPE SignalHandler(int Sig) {
106 if (FilesToRemove != 0)
107 while (!FilesToRemove->empty()) {
108 std::remove(FilesToRemove->back().c_str());
109 FilesToRemove->pop_back();
112 if (DirectoriesToRemove != 0)
113 while (!DirectoriesToRemove->empty()) {
114 DirectoriesToRemove->back().destroyDirectory(true);
115 DirectoriesToRemove->pop_back();
118 if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd)
119 exit(1); // If this is an interrupt signal, exit the program
121 // Otherwise if it is a fault (like SEGV) output the stacktrace to
122 // STDERR (if we can) and reissue the signal to die...
124 signal(Sig, SIG_DFL);
128 void RegisterHandler(int Signal) {
129 signal(Signal, SignalHandler);
136 // RemoveFileOnSignal - The public API
137 void sys::RemoveFileOnSignal(const sys::Path &Filename) {
138 if (FilesToRemove == 0)
139 FilesToRemove = new std::vector<std::string>;
141 FilesToRemove->push_back(Filename.get());
143 std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
144 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
147 // RemoveDirectoryOnSignal - The public API
148 void sys::RemoveDirectoryOnSignal(const llvm::sys::Path& path) {
149 if (!path.isDirectory())
152 if (DirectoriesToRemove == 0)
153 DirectoriesToRemove = new std::vector<sys::Path>;
155 DirectoriesToRemove->push_back(path);
157 std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
158 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
161 /// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
162 /// SIGSEGV) is delivered to the process, print a stack trace and then exit.
163 void sys::PrintStackTraceOnErrorSignal() {
164 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
169 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab