LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Unix/Signals.cpp

Go to the documentation of this file.
00001 //===- Signals.cpp - Generic Unix Signals Implementation -----*- C++ -*-===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines some helpful functions for dealing with the possibility of
00011 // Unix signals occuring while your program is running.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "Unix.h"
00016 #include <vector>
00017 #include <algorithm>
00018 #ifdef HAVE_EXECINFO_H
00019 # include <execinfo.h>         // For backtrace().
00020 #endif
00021 #include <sys/wait.h>
00022 #include <signal.h>
00023 
00024 namespace {
00025 
00026 std::vector<std::string> *FilesToRemove = 0 ;
00027 std::vector<llvm::sys::Path> *DirectoriesToRemove = 0;
00028 
00029 // IntSigs - Signals that may interrupt the program at any time.
00030 const int IntSigs[] = {
00031   SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
00032 };
00033 const int *IntSigsEnd = IntSigs + sizeof(IntSigs)/sizeof(IntSigs[0]);
00034 
00035 // KillSigs - Signals that are synchronous with the program that will cause it
00036 // to die.
00037 const int KillSigs[] = {
00038   SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ
00039 #ifdef SIGEMT
00040   , SIGEMT
00041 #endif
00042 };
00043 const int *KillSigsEnd = KillSigs + sizeof(KillSigs)/sizeof(KillSigs[0]);
00044 
00045 #ifdef HAVE_BACKTRACE
00046 void* StackTrace[256];
00047 #endif
00048 
00049 // PrintStackTrace - In the case of a program crash or fault, print out a stack
00050 // trace so that the user has an indication of why and where we died.
00051 //
00052 // On glibc systems we have the 'backtrace' function, which works nicely, but
00053 // doesn't demangle symbols.  In order to backtrace symbols, we fork and exec a
00054 // 'c++filt' process to do the demangling.  This seems like the simplest and
00055 // most robust solution when we can't allocate memory (such as in a signal
00056 // handler).  If we can't find 'c++filt', we fallback to printing mangled names.
00057 //
00058 void PrintStackTrace() {
00059 #ifdef HAVE_BACKTRACE
00060   // Use backtrace() to output a backtrace on Linux systems with glibc.
00061   int depth = backtrace(StackTrace, sizeof(StackTrace)/sizeof(StackTrace[0]));
00062   
00063   // Create a one-way unix pipe.  The backtracing process writes to PipeFDs[1],
00064   // the c++filt process reads from PipeFDs[0].
00065   int PipeFDs[2];
00066   if (pipe(PipeFDs)) {
00067     backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
00068     return;
00069   }
00070 
00071   switch (pid_t ChildPID = fork()) {
00072   case -1:        // Error forking, print mangled stack trace
00073     close(PipeFDs[0]);
00074     close(PipeFDs[1]);
00075     backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
00076     return;
00077   default:        // backtracing process
00078     close(PipeFDs[0]);  // Close the reader side.
00079 
00080     // Print the mangled backtrace into the pipe.
00081     backtrace_symbols_fd(StackTrace, depth, PipeFDs[1]);
00082     close(PipeFDs[1]);   // We are done writing.
00083     while (waitpid(ChildPID, 0, 0) == -1)
00084       if (errno != EINTR) break;
00085     return;
00086 
00087   case 0:         // c++filt process
00088     close(PipeFDs[1]);    // Close the writer side.
00089     dup2(PipeFDs[0], 0);  // Read from standard input
00090     close(PipeFDs[0]);    // Close the old descriptor
00091     dup2(2, 1);           // Revector stdout -> stderr
00092 
00093     // Try to run c++filt or gc++filt.  If neither is found, call back on 'cat'
00094     // to print the mangled stack trace.  If we can't find cat, just exit.
00095     execlp("c++filt", "c++filt", 0);
00096     execlp("gc++filt", "gc++filt", 0);
00097     execlp("cat", "cat", 0);
00098     execlp("/bin/cat", "cat", 0);
00099     exit(0);
00100   }
00101 #endif
00102 }
00103 
00104 // SignalHandler - The signal handler that runs...
00105 RETSIGTYPE SignalHandler(int Sig) {
00106   if (FilesToRemove != 0)
00107     while (!FilesToRemove->empty()) {
00108       std::remove(FilesToRemove->back().c_str());
00109       FilesToRemove->pop_back();
00110     }
00111 
00112   if (DirectoriesToRemove != 0)
00113     while (!DirectoriesToRemove->empty()) {
00114       DirectoriesToRemove->back().destroyDirectory(true);
00115       DirectoriesToRemove->pop_back();
00116     }
00117 
00118   if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd)
00119     exit(1);   // If this is an interrupt signal, exit the program
00120 
00121   // Otherwise if it is a fault (like SEGV) output the stacktrace to
00122   // STDERR (if we can) and reissue the signal to die...
00123   PrintStackTrace();
00124   signal(Sig, SIG_DFL);
00125 }
00126 
00127 // Just call signal
00128 void RegisterHandler(int Signal) { 
00129   signal(Signal, SignalHandler); 
00130 }
00131 
00132 }
00133 
00134 namespace llvm {
00135 
00136 // RemoveFileOnSignal - The public API
00137 void sys::RemoveFileOnSignal(const sys::Path &Filename) {
00138   if (FilesToRemove == 0)
00139     FilesToRemove = new std::vector<std::string>;
00140 
00141   FilesToRemove->push_back(Filename.get());
00142 
00143   std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
00144   std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
00145 }
00146 
00147 // RemoveDirectoryOnSignal - The public API
00148 void sys::RemoveDirectoryOnSignal(const llvm::sys::Path& path) {
00149   if (!path.isDirectory())
00150     return;
00151 
00152   if (DirectoriesToRemove == 0)
00153     DirectoriesToRemove = new std::vector<sys::Path>;
00154 
00155   DirectoriesToRemove->push_back(path);
00156 
00157   std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
00158   std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
00159 }
00160 
00161 /// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
00162 /// SIGSEGV) is delivered to the process, print a stack trace and then exit.
00163 void sys::PrintStackTraceOnErrorSignal() {
00164   std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
00165 }
00166 
00167 }
00168 
00169 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab