blob: 112cd783e436ced3fa6123c69f7031c8c58ac68d [file] [log] [blame]
Chris Lattnerbac27a42002-04-18 19:53:53 +00001//===- Signals.cpp - Signal Handling support ------------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattnerbac27a42002-04-18 19:53:53 +00009//
10// This file defines some helpful functions for dealing with the possibility of
Misha Brukman950971d2003-09-16 15:31:46 +000011// Unix signals occuring while your program is running.
Chris Lattnerbac27a42002-04-18 19:53:53 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "Support/Signals.h"
16#include <vector>
17#include <algorithm>
18#include <cstdlib>
19#include <cstdio>
Brian Gaeke38ce7e52004-02-20 06:40:59 +000020#include "Config/config.h" // Get the signal handler return type
21#ifdef HAVE_EXECINFO_H
22# include <execinfo.h> // For backtrace().
23#endif
Chris Lattnerbac27a42002-04-18 19:53:53 +000024#include <signal.h>
Alkis Evlogimenos280f9c92004-02-19 07:36:35 +000025#include <unistd.h>
Chris Lattnerb9632be2004-02-21 21:06:19 +000026#include <sys/wait.h>
27#include <cerrno>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner01e770a2003-05-22 21:59:35 +000030static std::vector<std::string> FilesToRemove;
Chris Lattnerbac27a42002-04-18 19:53:53 +000031
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[] = {
Chris Lattner7c97cee2002-09-13 14:57:24 +000041 SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGSYS, SIGXCPU, SIGXFSZ
42#ifdef SIGEMT
43 , SIGEMT
44#endif
Chris Lattnerbac27a42002-04-18 19:53:53 +000045};
46static const int *KillSigsEnd = KillSigs + sizeof(KillSigs)/sizeof(KillSigs[0]);
47
Chris Lattnerb9632be2004-02-21 21:06:19 +000048#ifdef HAVE_BACKTRACE
Alkis Evlogimenos280f9c92004-02-19 07:36:35 +000049static void* StackTrace[256];
Chris Lattnerb9632be2004-02-21 21:06:19 +000050#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);
Misha Brukmanac2ecd02004-02-21 21:51:41 +0000102 execlp("/bin/cat", "cat", 0);
Chris Lattnerb9632be2004-02-21 21:06:19 +0000103 exit(0);
104 }
105#endif
106}
Chris Lattnerbac27a42002-04-18 19:53:53 +0000107
108// SignalHandler - The signal handler that runs...
John Criswell7a73b802003-06-30 21:59:07 +0000109static RETSIGTYPE SignalHandler(int Sig) {
Chris Lattnerbac27a42002-04-18 19:53:53 +0000110 while (!FilesToRemove.empty()) {
111 std::remove(FilesToRemove.back().c_str());
112 FilesToRemove.pop_back();
113 }
114
Anand Shuklacfb22d32002-06-25 20:55:50 +0000115 if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd)
Chris Lattnerbac27a42002-04-18 19:53:53 +0000116 exit(1); // If this is an interrupt signal, exit the program
117
Alkis Evlogimenos280f9c92004-02-19 07:36:35 +0000118 // Otherwise if it is a fault (like SEGV) output the stacktrace to
Brian Gaeke38ce7e52004-02-20 06:40:59 +0000119 // STDERR (if we can) and reissue the signal to die...
Chris Lattnerb9632be2004-02-21 21:06:19 +0000120 PrintStackTrace();
Chris Lattner39602b22003-05-27 16:25:04 +0000121 signal(Sig, SIG_DFL);
Chris Lattnerbac27a42002-04-18 19:53:53 +0000122}
123
124static void RegisterHandler(int Signal) { signal(Signal, SignalHandler); }
125
126// RemoveFileOnSignal - The public API
Chris Lattner2cdd21c2003-12-14 21:35:53 +0000127void llvm::RemoveFileOnSignal(const std::string &Filename) {
Chris Lattnerbac27a42002-04-18 19:53:53 +0000128 FilesToRemove.push_back(Filename);
129
Anand Shuklacfb22d32002-06-25 20:55:50 +0000130 std::for_each(IntSigs, IntSigsEnd, RegisterHandler);
131 std::for_each(KillSigs, KillSigsEnd, RegisterHandler);
Chris Lattnerbac27a42002-04-18 19:53:53 +0000132}
Chris Lattner982774c2004-02-19 20:03:14 +0000133
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}