blob: a9af969d5ba85785c5b35acea0bc8708a5c24167 [file] [log] [blame]
Reid Spencer496c2772004-08-29 19:22:48 +00001//===- 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>
Reid Spencercdf54d02004-12-27 06:16:52 +000018#if HAVE_EXECINFO_H
Reid Spencer496c2772004-08-29 19:22:48 +000019# include <execinfo.h> // For backtrace().
20#endif
Reid Spencercdf54d02004-12-27 06:16:52 +000021#if HAVE_SIGNAL_H
Reid Spencer496c2772004-08-29 19:22:48 +000022#include <signal.h>
Reid Spencercdf54d02004-12-27 06:16:52 +000023#endif
Reid Spencer496c2772004-08-29 19:22:48 +000024
25namespace {
26
27std::vector<std::string> *FilesToRemove = 0 ;
28std::vector<llvm::sys::Path> *DirectoriesToRemove = 0;
29
30// IntSigs - Signals that may interrupt the program at any time.
31const int IntSigs[] = {
32 SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
33};
34const 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.
38const int KillSigs[] = {
39 SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ
40#ifdef SIGEMT
41 , SIGEMT
42#endif
43};
44const int *KillSigsEnd = KillSigs + sizeof(KillSigs)/sizeof(KillSigs[0]);
45
46#ifdef HAVE_BACKTRACE
47void* 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//
59void 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.
Alkis Evlogimenos38da41c2005-04-22 17:56:01 +000096 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);
Reid Spencer496c2772004-08-29 19:22:48 +0000100 exit(0);
101 }
102#endif
103}
104
105// SignalHandler - The signal handler that runs...
106RETSIGTYPE 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()) {
Reid Spencera229c5c2005-07-08 03:08:58 +0000115 DirectoriesToRemove->back().eraseFromDisk(true);
Reid Spencer496c2772004-08-29 19:22:48 +0000116 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
129void RegisterHandler(int Signal) {
130 signal(Signal, SignalHandler);
131}
132
133}
134
135namespace llvm {
136
137// RemoveFileOnSignal - The public API
Reid Spencer94465592004-11-14 22:09:22 +0000138void sys::RemoveFileOnSignal(const sys::Path &Filename) {
Reid Spencer496c2772004-08-29 19:22:48 +0000139 if (FilesToRemove == 0)
140 FilesToRemove = new std::vector<std::string>;
141
Reid Spencer1fce0912004-12-11 00:14:15 +0000142 FilesToRemove->push_back(Filename.toString());
Reid Spencer496c2772004-08-29 19:22:48 +0000143
144 std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
145 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
146}
147
148// RemoveDirectoryOnSignal - The public API
149void sys::RemoveDirectoryOnSignal(const llvm::sys::Path& path) {
Reid Spencer07adb282004-11-05 22:15:36 +0000150 if (!path.isDirectory())
Reid Spencer496c2772004-08-29 19:22:48 +0000151 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.
164void sys::PrintStackTraceOnErrorSignal() {
165 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
166}
167
168}
169