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 //===----------------------------------------------------------------------===//
19 # include <execinfo.h> // For backtrace().
27 std::vector<std::string> *FilesToRemove = 0 ;
28 std::vector<llvm::sys::Path> *DirectoriesToRemove = 0;
30 // IntSigs - Signals that may interrupt the program at any time.
31 const int IntSigs[] = {
32 SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
34 const int *IntSigsEnd = IntSigs + sizeof(IntSigs)/sizeof(IntSigs[0]);
36 // KillSigs - Signals that are synchronous with the program that will cause it
38 const int KillSigs[] = {
39 SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ
44 const int *KillSigsEnd = KillSigs + sizeof(KillSigs)/sizeof(KillSigs[0]);
47 void* StackTrace[256];
50 // PrintStackTrace - In the case of a program crash or fault, print out a stack
51 // trace so that the user has an indication of why and where we died.
53 // On glibc systems we have the 'backtrace' function, which works nicely, but
54 // doesn't demangle symbols. In order to backtrace symbols, we fork and exec a
55 // 'c++filt' process to do the demangling. This seems like the simplest and
56 // most robust solution when we can't allocate memory (such as in a signal
57 // handler). If we can't find 'c++filt', we fallback to printing mangled names.
59 void PrintStackTrace() {
61 // Use backtrace() to output a backtrace on Linux systems with glibc.
62 int depth = backtrace(StackTrace, sizeof(StackTrace)/sizeof(StackTrace[0]));
64 // Create a one-way unix pipe. The backtracing process writes to PipeFDs[1],
65 // the c++filt process reads from PipeFDs[0].
68 backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
72 switch (pid_t ChildPID = fork()) {
73 case -1: // Error forking, print mangled stack trace
76 backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
78 default: // backtracing process
79 close(PipeFDs[0]); // Close the reader side.
81 // Print the mangled backtrace into the pipe.
82 backtrace_symbols_fd(StackTrace, depth, PipeFDs[1]);
83 close(PipeFDs[1]); // We are done writing.
84 while (waitpid(ChildPID, 0, 0) == -1)
85 if (errno != EINTR) break;
88 case 0: // c++filt process
89 close(PipeFDs[1]); // Close the writer side.
90 dup2(PipeFDs[0], 0); // Read from standard input
91 close(PipeFDs[0]); // Close the old descriptor
92 dup2(2, 1); // Revector stdout -> stderr
94 // Try to run c++filt or gc++filt. If neither is found, call back on 'cat'
95 // to print the mangled stack trace. If we can't find cat, just exit.
96 execlp("c++filt", "c++filt", 0);
97 execlp("gc++filt", "gc++filt", 0);
98 execlp("cat", "cat", 0);
99 execlp("/bin/cat", "cat", 0);
105 // SignalHandler - The signal handler that runs...
106 RETSIGTYPE SignalHandler(int Sig) {
107 if (FilesToRemove != 0)
108 while (!FilesToRemove->empty()) {
109 std::remove(FilesToRemove->back().c_str());
110 FilesToRemove->pop_back();
113 if (DirectoriesToRemove != 0)
114 while (!DirectoriesToRemove->empty()) {
115 DirectoriesToRemove->back().destroyDirectory(true);
116 DirectoriesToRemove->pop_back();
119 if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd)
120 exit(1); // If this is an interrupt signal, exit the program
122 // Otherwise if it is a fault (like SEGV) output the stacktrace to
123 // STDERR (if we can) and reissue the signal to die...
125 signal(Sig, SIG_DFL);
129 void RegisterHandler(int Signal) {
130 signal(Signal, SignalHandler);
137 // RemoveFileOnSignal - The public API
138 void sys::RemoveFileOnSignal(const sys::Path &Filename) {
139 if (FilesToRemove == 0)
140 FilesToRemove = new std::vector<std::string>;
142 FilesToRemove->push_back(Filename.toString());
144 std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
145 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
148 // RemoveDirectoryOnSignal - The public API
149 void sys::RemoveDirectoryOnSignal(const llvm::sys::Path& path) {
150 if (!path.isDirectory())
153 if (DirectoriesToRemove == 0)
154 DirectoriesToRemove = new std::vector<sys::Path>;
156 DirectoriesToRemove->push_back(path);
158 std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
159 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
162 /// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
163 /// SIGSEGV) is delivered to the process, print a stack trace and then exit.
164 void sys::PrintStackTraceOnErrorSignal() {
165 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
170 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab