Signals.cpp revision a5648f2ea2a845f54c5691764ac4689313fdab05
1//===- Signals.cpp - Signal Handling support ------------------------------===//
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 "llvm/System/Signals.h"
16#include <vector>
17#include <algorithm>
18#include <cstdlib>
19#include <cstdio>
20#include "Config/config.h"     // Get the signal handler return type
21#ifdef HAVE_EXECINFO_H
22# include <execinfo.h>         // For backtrace().
23#endif
24#include <signal.h>
25#include <unistd.h>
26#include <sys/wait.h>
27#include <cerrno>
28using namespace llvm;
29
30static std::vector<std::string> FilesToRemove;
31
32// IntSigs - Signals that may interrupt the program at any time.
33static const int IntSigs[] = {
34  SIGHUP, SIGINT, SIGQUIT, SIGKILL, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
35};
36static const int *IntSigsEnd = IntSigs + sizeof(IntSigs)/sizeof(IntSigs[0]);
37
38// KillSigs - Signals that are synchronous with the program that will cause it
39// to die.
40static const int KillSigs[] = {
41  SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ
42#ifdef SIGEMT
43  , SIGEMT
44#endif
45};
46static const int *KillSigsEnd = KillSigs + sizeof(KillSigs)/sizeof(KillSigs[0]);
47
48#ifdef HAVE_BACKTRACE
49static void* StackTrace[256];
50#endif
51
52
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.
55//
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.
61//
62static void PrintStackTrace() {
63#ifdef HAVE_BACKTRACE
64  // Use backtrace() to output a backtrace on Linux systems with glibc.
65  int depth = backtrace(StackTrace, sizeof(StackTrace)/sizeof(StackTrace[0]));
66
67  // Create a one-way unix pipe.  The backtracing process writes to PipeFDs[1],
68  // the c++filt process reads from PipeFDs[0].
69  int PipeFDs[2];
70  if (pipe(PipeFDs)) {
71    backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
72    return;
73  }
74
75  switch (pid_t ChildPID = fork()) {
76  case -1:        // Error forking, print mangled stack trace
77    close(PipeFDs[0]);
78    close(PipeFDs[1]);
79    backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
80    return;
81  default:        // backtracing process
82    close(PipeFDs[0]);  // Close the reader side.
83
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;
89    return;
90
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
96
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);
103    exit(0);
104  }
105#endif
106}
107
108// SignalHandler - The signal handler that runs...
109static RETSIGTYPE SignalHandler(int Sig) {
110  while (!FilesToRemove.empty()) {
111    std::remove(FilesToRemove.back().c_str());
112    FilesToRemove.pop_back();
113  }
114
115  if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd)
116    exit(1);   // If this is an interrupt signal, exit the program
117
118  // Otherwise if it is a fault (like SEGV) output the stacktrace to
119  // STDERR (if we can) and reissue the signal to die...
120  PrintStackTrace();
121  signal(Sig, SIG_DFL);
122}
123
124static void RegisterHandler(int Signal) { signal(Signal, SignalHandler); }
125
126// RemoveFileOnSignal - The public API
127void llvm::RemoveFileOnSignal(const std::string &Filename) {
128  FilesToRemove.push_back(Filename);
129
130  std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
131  std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
132}
133
134/// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
135/// SIGSEGV) is delivered to the process, print a stack trace and then exit.
136void llvm::PrintStackTraceOnErrorSignal() {
137  std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
138}
139