Silence gcc-4.0.0 warnings.
[oota-llvm.git] / lib / System / Unix / Signals.inc
1 //===- Signals.cpp - Generic Unix Signals Implementation -----*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
9 //
10 // This file defines some helpful functions for dealing with the possibility of
11 // Unix signals occuring while your program is running.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "Unix.h"
16 #include <vector>
17 #include <algorithm>
18 #if HAVE_EXECINFO_H
19 # include <execinfo.h>         // For backtrace().
20 #endif
21 #if HAVE_SIGNAL_H
22 #include <signal.h>
23 #endif
24
25 namespace {
26
27 std::vector<std::string> *FilesToRemove = 0 ;
28 std::vector<llvm::sys::Path> *DirectoriesToRemove = 0;
29
30 // IntSigs - Signals that may interrupt the program at any time.
31 const int IntSigs[] = {
32   SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
33 };
34 const int *IntSigsEnd = IntSigs + sizeof(IntSigs)/sizeof(IntSigs[0]);
35
36 // KillSigs - Signals that are synchronous with the program that will cause it
37 // to die.
38 const int KillSigs[] = {
39   SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ
40 #ifdef SIGEMT
41   , SIGEMT
42 #endif
43 };
44 const int *KillSigsEnd = KillSigs + sizeof(KillSigs)/sizeof(KillSigs[0]);
45
46 #ifdef HAVE_BACKTRACE
47 void* StackTrace[256];
48 #endif
49
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.
52 //
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.
58 //
59 void PrintStackTrace() {
60 #ifdef HAVE_BACKTRACE
61   // Use backtrace() to output a backtrace on Linux systems with glibc.
62   int depth = backtrace(StackTrace, sizeof(StackTrace)/sizeof(StackTrace[0]));
63   
64   // Create a one-way unix pipe.  The backtracing process writes to PipeFDs[1],
65   // the c++filt process reads from PipeFDs[0].
66   int PipeFDs[2];
67   if (pipe(PipeFDs)) {
68     backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
69     return;
70   }
71
72   switch (pid_t ChildPID = fork()) {
73   case -1:        // Error forking, print mangled stack trace
74     close(PipeFDs[0]);
75     close(PipeFDs[1]);
76     backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
77     return;
78   default:        // backtracing process
79     close(PipeFDs[0]);  // Close the reader side.
80
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;
86     return;
87
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
93
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", (char*)NULL);
97     execlp("gc++filt", "gc++filt", (char*)NULL);
98     execlp("cat", "cat", (char*)NULL);
99     execlp("/bin/cat", "cat", (char*)NULL);
100     exit(0);
101   }
102 #endif
103 }
104
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();
111     }
112
113   if (DirectoriesToRemove != 0)
114     while (!DirectoriesToRemove->empty()) {
115       DirectoriesToRemove->back().destroyDirectory(true);
116       DirectoriesToRemove->pop_back();
117     }
118
119   if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd)
120     exit(1);   // If this is an interrupt signal, exit the program
121
122   // Otherwise if it is a fault (like SEGV) output the stacktrace to
123   // STDERR (if we can) and reissue the signal to die...
124   PrintStackTrace();
125   signal(Sig, SIG_DFL);
126 }
127
128 // Just call signal
129 void RegisterHandler(int Signal) { 
130   signal(Signal, SignalHandler); 
131 }
132
133 }
134
135 namespace llvm {
136
137 // RemoveFileOnSignal - The public API
138 void sys::RemoveFileOnSignal(const sys::Path &Filename) {
139   if (FilesToRemove == 0)
140     FilesToRemove = new std::vector<std::string>;
141
142   FilesToRemove->push_back(Filename.toString());
143
144   std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
145   std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
146 }
147
148 // RemoveDirectoryOnSignal - The public API
149 void sys::RemoveDirectoryOnSignal(const llvm::sys::Path& path) {
150   if (!path.isDirectory())
151     return;
152
153   if (DirectoriesToRemove == 0)
154     DirectoriesToRemove = new std::vector<sys::Path>;
155
156   DirectoriesToRemove->push_back(path);
157
158   std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
159   std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
160 }
161
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);
166 }
167
168 }
169
170 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab