OSDN Git Service

Add SwiftShader source to repo
[android-x86/external-swiftshader.git] / src / LLVM / lib / System / Unix / Signals.inc
1 //===- Signals.cpp - Generic Unix Signals Implementation -----*- C++ -*-===//\r
2 // \r
3 //                     The LLVM Compiler Infrastructure\r
4 //\r
5 // This file is distributed under the University of Illinois Open Source\r
6 // License. See LICENSE.TXT for details.\r
7 // \r
8 //===----------------------------------------------------------------------===//\r
9 //\r
10 // This file defines some helpful functions for dealing with the possibility of\r
11 // Unix signals occuring while your program is running.\r
12 //\r
13 //===----------------------------------------------------------------------===//\r
14 \r
15 #include "Unix.h"\r
16 #include "llvm/ADT/STLExtras.h"\r
17 #include "llvm/System/Mutex.h"\r
18 #include <vector>\r
19 #include <algorithm>\r
20 #if HAVE_EXECINFO_H\r
21 # include <execinfo.h>         // For backtrace().\r
22 #endif\r
23 #if HAVE_SIGNAL_H\r
24 #include <signal.h>\r
25 #endif\r
26 #if HAVE_SYS_STAT_H\r
27 #include <sys/stat.h>\r
28 #endif\r
29 #if HAVE_DLFCN_H && __GNUG__\r
30 #include <dlfcn.h>\r
31 #include <cxxabi.h> \r
32 #endif\r
33 using namespace llvm;\r
34 \r
35 static RETSIGTYPE SignalHandler(int Sig);  // defined below.\r
36 \r
37 static SmartMutex<true> SignalsMutex;\r
38 \r
39 /// InterruptFunction - The function to call if ctrl-c is pressed.\r
40 static void (*InterruptFunction)() = 0;\r
41 \r
42 static std::vector<sys::Path> FilesToRemove;\r
43 static std::vector<std::pair<void(*)(void*), void*> > CallBacksToRun;\r
44 \r
45 // IntSigs - Signals that may interrupt the program at any time.\r
46 static const int IntSigs[] = {\r
47   SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2\r
48 };\r
49 static const int *const IntSigsEnd =\r
50   IntSigs + sizeof(IntSigs) / sizeof(IntSigs[0]);\r
51 \r
52 // KillSigs - Signals that are synchronous with the program that will cause it\r
53 // to die.\r
54 static const int KillSigs[] = {\r
55   SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV\r
56 #ifdef SIGSYS\r
57   , SIGSYS\r
58 #endif\r
59 #ifdef SIGXCPU\r
60   , SIGXCPU\r
61 #endif\r
62 #ifdef SIGXFSZ\r
63   , SIGXFSZ\r
64 #endif\r
65 #ifdef SIGEMT\r
66   , SIGEMT\r
67 #endif\r
68 };\r
69 static const int *const KillSigsEnd =\r
70   KillSigs + sizeof(KillSigs) / sizeof(KillSigs[0]);\r
71 \r
72 static unsigned NumRegisteredSignals = 0;\r
73 static struct {\r
74   struct sigaction SA;\r
75   int SigNo;\r
76 } RegisteredSignalInfo[(sizeof(IntSigs)+sizeof(KillSigs))/sizeof(KillSigs[0])];\r
77 \r
78 \r
79 static void RegisterHandler(int Signal) {\r
80   assert(NumRegisteredSignals <\r
81          sizeof(RegisteredSignalInfo)/sizeof(RegisteredSignalInfo[0]) &&\r
82          "Out of space for signal handlers!");\r
83 \r
84   struct sigaction NewHandler;\r
85   \r
86   NewHandler.sa_handler = SignalHandler;\r
87   NewHandler.sa_flags = SA_NODEFER|SA_RESETHAND;\r
88   sigemptyset(&NewHandler.sa_mask); \r
89   \r
90   // Install the new handler, save the old one in RegisteredSignalInfo.\r
91   sigaction(Signal, &NewHandler,\r
92             &RegisteredSignalInfo[NumRegisteredSignals].SA);\r
93   RegisteredSignalInfo[NumRegisteredSignals].SigNo = Signal;\r
94   ++NumRegisteredSignals;\r
95 }\r
96 \r
97 static void RegisterHandlers() {\r
98   // If the handlers are already registered, we're done.\r
99   if (NumRegisteredSignals != 0) return;\r
100 \r
101   std::for_each(IntSigs, IntSigsEnd, RegisterHandler);\r
102   std::for_each(KillSigs, KillSigsEnd, RegisterHandler);\r
103 }\r
104 \r
105 static void UnregisterHandlers() {\r
106   // Restore all of the signal handlers to how they were before we showed up.\r
107   for (unsigned i = 0, e = NumRegisteredSignals; i != e; ++i)\r
108     sigaction(RegisteredSignalInfo[i].SigNo,\r
109               &RegisteredSignalInfo[i].SA, 0);\r
110   NumRegisteredSignals = 0;\r
111 }\r
112 \r
113 \r
114 /// RemoveFilesToRemove - Process the FilesToRemove list. This function\r
115 /// should be called with the SignalsMutex lock held.\r
116 static void RemoveFilesToRemove() {\r
117   while (!FilesToRemove.empty()) {\r
118     FilesToRemove.back().eraseFromDisk(true);\r
119     FilesToRemove.pop_back();\r
120   }\r
121 }\r
122 \r
123 // SignalHandler - The signal handler that runs.\r
124 static RETSIGTYPE SignalHandler(int Sig) {\r
125   // Restore the signal behavior to default, so that the program actually\r
126   // crashes when we return and the signal reissues.  This also ensures that if\r
127   // we crash in our signal handler that the program will terminate immediately\r
128   // instead of recursing in the signal handler.\r
129   UnregisterHandlers();\r
130 \r
131   // Unmask all potentially blocked kill signals.\r
132   sigset_t SigMask;\r
133   sigfillset(&SigMask);\r
134   sigprocmask(SIG_UNBLOCK, &SigMask, 0);\r
135 \r
136   SignalsMutex.acquire();\r
137   RemoveFilesToRemove();\r
138 \r
139   if (std::find(IntSigs, IntSigsEnd, Sig) != IntSigsEnd) {\r
140     if (InterruptFunction) {\r
141       void (*IF)() = InterruptFunction;\r
142       SignalsMutex.release();\r
143       InterruptFunction = 0;\r
144       IF();        // run the interrupt function.\r
145       return;\r
146     }\r
147     \r
148     SignalsMutex.release();\r
149     raise(Sig);   // Execute the default handler.\r
150     return;\r
151   }\r
152 \r
153   SignalsMutex.release();\r
154 \r
155   // Otherwise if it is a fault (like SEGV) run any handler.\r
156   for (unsigned i = 0, e = CallBacksToRun.size(); i != e; ++i)\r
157     CallBacksToRun[i].first(CallBacksToRun[i].second);\r
158 }\r
159 \r
160 void llvm::sys::RunInterruptHandlers() {\r
161   SignalsMutex.acquire();\r
162   RemoveFilesToRemove();\r
163   SignalsMutex.release();\r
164 }\r
165 \r
166 void llvm::sys::SetInterruptFunction(void (*IF)()) {\r
167   SignalsMutex.acquire();\r
168   InterruptFunction = IF;\r
169   SignalsMutex.release();\r
170   RegisterHandlers();\r
171 }\r
172 \r
173 // RemoveFileOnSignal - The public API\r
174 bool llvm::sys::RemoveFileOnSignal(const sys::Path &Filename,\r
175                                    std::string* ErrMsg) {\r
176   SignalsMutex.acquire();\r
177   FilesToRemove.push_back(Filename);\r
178 \r
179   SignalsMutex.release();\r
180 \r
181   RegisterHandlers();\r
182   return false;\r
183 }\r
184 \r
185 /// AddSignalHandler - Add a function to be called when a signal is delivered\r
186 /// to the process.  The handler can have a cookie passed to it to identify\r
187 /// what instance of the handler it is.\r
188 void llvm::sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) {\r
189   CallBacksToRun.push_back(std::make_pair(FnPtr, Cookie));\r
190   RegisterHandlers();\r
191 }\r
192 \r
193 \r
194 // PrintStackTrace - In the case of a program crash or fault, print out a stack\r
195 // trace so that the user has an indication of why and where we died.\r
196 //\r
197 // On glibc systems we have the 'backtrace' function, which works nicely, but\r
198 // doesn't demangle symbols.  \r
199 static void PrintStackTrace(void *) {\r
200 #ifdef HAVE_BACKTRACE\r
201   static void* StackTrace[256];\r
202   // Use backtrace() to output a backtrace on Linux systems with glibc.\r
203   int depth = backtrace(StackTrace,\r
204                         static_cast<int>(array_lengthof(StackTrace)));\r
205 #if HAVE_DLFCN_H && __GNUG__\r
206   int width = 0;\r
207   for (int i = 0; i < depth; ++i) {\r
208     Dl_info dlinfo;\r
209     dladdr(StackTrace[i], &dlinfo);\r
210     const char* name = strrchr(dlinfo.dli_fname, '/');\r
211 \r
212     int nwidth;\r
213     if (name == NULL) nwidth = strlen(dlinfo.dli_fname);\r
214     else              nwidth = strlen(name) - 1;\r
215 \r
216     if (nwidth > width) width = nwidth;\r
217   }\r
218 \r
219   for (int i = 0; i < depth; ++i) {\r
220     Dl_info dlinfo;\r
221     dladdr(StackTrace[i], &dlinfo);\r
222 \r
223     fprintf(stderr, "%-2d", i);\r
224 \r
225     const char* name = strrchr(dlinfo.dli_fname, '/');\r
226     if (name == NULL) fprintf(stderr, " %-*s", width, dlinfo.dli_fname);\r
227     else              fprintf(stderr, " %-*s", width, name+1);\r
228 \r
229     fprintf(stderr, " %#0*lx",\r
230             (int)(sizeof(void*) * 2) + 2, (unsigned long)StackTrace[i]);\r
231 \r
232     if (dlinfo.dli_sname != NULL) {\r
233       int res;\r
234       fputc(' ', stderr);\r
235       char* d = abi::__cxa_demangle(dlinfo.dli_sname, NULL, NULL, &res);\r
236       if (d == NULL) fputs(dlinfo.dli_sname, stderr);\r
237       else           fputs(d, stderr);\r
238       free(d);\r
239 \r
240       fprintf(stderr, " + %tu",(char*)StackTrace[i]-(char*)dlinfo.dli_saddr);\r
241     }\r
242     fputc('\n', stderr);\r
243   }\r
244 #else\r
245   backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);\r
246 #endif\r
247 #endif\r
248 }\r
249 \r
250 /// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or\r
251 /// SIGSEGV) is delivered to the process, print a stack trace and then exit.\r
252 void llvm::sys::PrintStackTraceOnErrorSignal() {\r
253   AddSignalHandler(PrintStackTrace, 0);\r
254 }\r
255 \r