Use llvm-symbolizer to symbolize LLVM/Clang crash dumps.
[oota-llvm.git] / lib / Support / Unix / Signals.inc
1 //===- Signals.cpp - Generic Unix Signals Implementation -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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 occurring while your program is running.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "Unix.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/FileUtilities.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/Mutex.h"
22 #include "llvm/Support/Program.h"
23 #include "llvm/Support/UniqueLock.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include <algorithm>
26 #include <string>
27 #include <vector>
28 #if HAVE_EXECINFO_H
29 # include <execinfo.h>         // For backtrace().
30 #endif
31 #if HAVE_SIGNAL_H
32 #include <signal.h>
33 #endif
34 #if HAVE_SYS_STAT_H
35 #include <sys/stat.h>
36 #endif
37 #if HAVE_CXXABI_H
38 #include <cxxabi.h>
39 #endif
40 #if HAVE_DLFCN_H
41 #include <dlfcn.h>
42 #endif
43 #if HAVE_MACH_MACH_H
44 #include <mach/mach.h>
45 #endif
46 #if HAVE_LINK_H
47 #include <link.h>
48 #endif
49
50 using namespace llvm;
51
52 static RETSIGTYPE SignalHandler(int Sig);  // defined below.
53
54 static ManagedStatic<SmartMutex<true> > SignalsMutex;
55
56 /// InterruptFunction - The function to call if ctrl-c is pressed.
57 static void (*InterruptFunction)() = nullptr;
58
59 static ManagedStatic<std::vector<std::string>> FilesToRemove;
60 static ManagedStatic<std::vector<std::pair<void (*)(void *), void *>>>
61     CallBacksToRun;
62
63 // IntSigs - Signals that represent requested termination. There's no bug
64 // or failure, or if there is, it's not our direct responsibility. For whatever
65 // reason, our continued execution is no longer desirable.
66 static const int IntSigs[] = {
67   SIGHUP, SIGINT, SIGPIPE, SIGTERM, SIGUSR1, SIGUSR2
68 };
69
70 // KillSigs - Signals that represent that we have a bug, and our prompt
71 // termination has been ordered.
72 static const int KillSigs[] = {
73   SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGBUS, SIGSEGV, SIGQUIT
74 #ifdef SIGSYS
75   , SIGSYS
76 #endif
77 #ifdef SIGXCPU
78   , SIGXCPU
79 #endif
80 #ifdef SIGXFSZ
81   , SIGXFSZ
82 #endif
83 #ifdef SIGEMT
84   , SIGEMT
85 #endif
86 };
87
88 static unsigned NumRegisteredSignals = 0;
89 static struct {
90   struct sigaction SA;
91   int SigNo;
92 } RegisteredSignalInfo[(sizeof(IntSigs)+sizeof(KillSigs))/sizeof(KillSigs[0])];
93
94
95 static void RegisterHandler(int Signal) {
96   assert(NumRegisteredSignals <
97          sizeof(RegisteredSignalInfo)/sizeof(RegisteredSignalInfo[0]) &&
98          "Out of space for signal handlers!");
99
100   struct sigaction NewHandler;
101
102   NewHandler.sa_handler = SignalHandler;
103   NewHandler.sa_flags = SA_NODEFER|SA_RESETHAND;
104   sigemptyset(&NewHandler.sa_mask);
105
106   // Install the new handler, save the old one in RegisteredSignalInfo.
107   sigaction(Signal, &NewHandler,
108             &RegisteredSignalInfo[NumRegisteredSignals].SA);
109   RegisteredSignalInfo[NumRegisteredSignals].SigNo = Signal;
110   ++NumRegisteredSignals;
111 }
112
113 static void RegisterHandlers() {
114   // If the handlers are already registered, we're done.
115   if (NumRegisteredSignals != 0) return;
116
117   for (auto S : IntSigs) RegisterHandler(S);
118   for (auto S : KillSigs) RegisterHandler(S);
119 }
120
121 static void UnregisterHandlers() {
122   // Restore all of the signal handlers to how they were before we showed up.
123   for (unsigned i = 0, e = NumRegisteredSignals; i != e; ++i)
124     sigaction(RegisteredSignalInfo[i].SigNo,
125               &RegisteredSignalInfo[i].SA, nullptr);
126   NumRegisteredSignals = 0;
127 }
128
129
130 /// RemoveFilesToRemove - Process the FilesToRemove list. This function
131 /// should be called with the SignalsMutex lock held.
132 /// NB: This must be an async signal safe function. It cannot allocate or free
133 /// memory, even in debug builds.
134 static void RemoveFilesToRemove() {
135   // We avoid iterators in case of debug iterators that allocate or release
136   // memory.
137   std::vector<std::string>& FilesToRemoveRef = *FilesToRemove;
138   for (unsigned i = 0, e = FilesToRemoveRef.size(); i != e; ++i) {
139     // We rely on a std::string implementation for which repeated calls to
140     // 'c_str()' don't allocate memory. We pre-call 'c_str()' on all of these
141     // strings to try to ensure this is safe.
142     const char *path = FilesToRemoveRef[i].c_str();
143
144     // Get the status so we can determine if it's a file or directory. If we
145     // can't stat the file, ignore it.
146     struct stat buf;
147     if (stat(path, &buf) != 0)
148       continue;
149
150     // If this is not a regular file, ignore it. We want to prevent removal of
151     // special files like /dev/null, even if the compiler is being run with the
152     // super-user permissions.
153     if (!S_ISREG(buf.st_mode))
154       continue;
155   
156     // Otherwise, remove the file. We ignore any errors here as there is nothing
157     // else we can do.
158     unlink(path);
159   }
160 }
161
162 // SignalHandler - The signal handler that runs.
163 static RETSIGTYPE SignalHandler(int Sig) {
164   // Restore the signal behavior to default, so that the program actually
165   // crashes when we return and the signal reissues.  This also ensures that if
166   // we crash in our signal handler that the program will terminate immediately
167   // instead of recursing in the signal handler.
168   UnregisterHandlers();
169
170   // Unmask all potentially blocked kill signals.
171   sigset_t SigMask;
172   sigfillset(&SigMask);
173   sigprocmask(SIG_UNBLOCK, &SigMask, nullptr);
174
175   {
176     unique_lock<SmartMutex<true>> Guard(*SignalsMutex);
177     RemoveFilesToRemove();
178
179     if (std::find(std::begin(IntSigs), std::end(IntSigs), Sig)
180         != std::end(IntSigs)) {
181       if (InterruptFunction) {
182         void (*IF)() = InterruptFunction;
183         Guard.unlock();
184         InterruptFunction = nullptr;
185         IF();        // run the interrupt function.
186         return;
187       }
188
189       Guard.unlock();
190       raise(Sig);   // Execute the default handler.
191       return;
192    }
193   }
194
195   // Otherwise if it is a fault (like SEGV) run any handler.
196   std::vector<std::pair<void (*)(void *), void *>>& CallBacksToRunRef =
197       *CallBacksToRun;
198   for (unsigned i = 0, e = CallBacksToRun->size(); i != e; ++i)
199     CallBacksToRunRef[i].first(CallBacksToRunRef[i].second);
200
201 #ifdef __s390__
202   // On S/390, certain signals are delivered with PSW Address pointing to
203   // *after* the faulting instruction.  Simply returning from the signal
204   // handler would continue execution after that point, instead of
205   // re-raising the signal.  Raise the signal manually in those cases.
206   if (Sig == SIGILL || Sig == SIGFPE || Sig == SIGTRAP)
207     raise(Sig);
208 #endif
209 }
210
211 void llvm::sys::RunInterruptHandlers() {
212   sys::SmartScopedLock<true> Guard(*SignalsMutex);
213   RemoveFilesToRemove();
214 }
215
216 void llvm::sys::SetInterruptFunction(void (*IF)()) {
217   {
218     sys::SmartScopedLock<true> Guard(*SignalsMutex);
219     InterruptFunction = IF;
220   }
221   RegisterHandlers();
222 }
223
224 // RemoveFileOnSignal - The public API
225 bool llvm::sys::RemoveFileOnSignal(StringRef Filename,
226                                    std::string* ErrMsg) {
227   {
228     sys::SmartScopedLock<true> Guard(*SignalsMutex);
229     std::vector<std::string>& FilesToRemoveRef = *FilesToRemove;
230     std::string *OldPtr =
231         FilesToRemoveRef.empty() ? nullptr : &FilesToRemoveRef[0];
232     FilesToRemoveRef.push_back(Filename);
233
234     // We want to call 'c_str()' on every std::string in this vector so that if
235     // the underlying implementation requires a re-allocation, it happens here
236     // rather than inside of the signal handler. If we see the vector grow, we
237     // have to call it on every entry. If it remains in place, we only need to
238     // call it on the latest one.
239     if (OldPtr == &FilesToRemoveRef[0])
240       FilesToRemoveRef.back().c_str();
241     else
242       for (unsigned i = 0, e = FilesToRemoveRef.size(); i != e; ++i)
243         FilesToRemoveRef[i].c_str();
244   }
245
246   RegisterHandlers();
247   return false;
248 }
249
250 // DontRemoveFileOnSignal - The public API
251 void llvm::sys::DontRemoveFileOnSignal(StringRef Filename) {
252   sys::SmartScopedLock<true> Guard(*SignalsMutex);
253   std::vector<std::string>::reverse_iterator RI =
254     std::find(FilesToRemove->rbegin(), FilesToRemove->rend(), Filename);
255   std::vector<std::string>::iterator I = FilesToRemove->end();
256   if (RI != FilesToRemove->rend())
257     I = FilesToRemove->erase(RI.base()-1);
258
259   // We need to call c_str() on every element which would have been moved by
260   // the erase. These elements, in a C++98 implementation where c_str()
261   // requires a reallocation on the first call may have had the call to c_str()
262   // made on insertion become invalid by being copied down an element.
263   for (std::vector<std::string>::iterator E = FilesToRemove->end(); I != E; ++I)
264     I->c_str();
265 }
266
267 /// AddSignalHandler - Add a function to be called when a signal is delivered
268 /// to the process.  The handler can have a cookie passed to it to identify
269 /// what instance of the handler it is.
270 void llvm::sys::AddSignalHandler(void (*FnPtr)(void *), void *Cookie) {
271   CallBacksToRun->push_back(std::make_pair(FnPtr, Cookie));
272   RegisterHandlers();
273 }
274
275 #if HAVE_LINK_H
276 struct DlIteratePhdrData {
277   void **StackTrace;
278   int depth;
279   bool first;
280   const char **modules;
281   intptr_t *offsets;
282   const char *main_exec_name;
283 };
284
285 static int dl_iterate_phdr_cb(dl_phdr_info *info, size_t size, void *arg) {
286   DlIteratePhdrData *data = (DlIteratePhdrData*)arg;
287   const char *name = data->first ? data->main_exec_name : info->dlpi_name;
288   data->first = false;
289   for (int i = 0; i < info->dlpi_phnum; i++) {
290     const ElfW(Phdr) *phdr = &info->dlpi_phdr[i];
291     if (phdr->p_type != PT_LOAD)
292       continue;
293     intptr_t beg = info->dlpi_addr + phdr->p_vaddr;
294     intptr_t end = beg + phdr->p_memsz;
295     for (int j = 0; j < data->depth; j++) {
296       if (data->modules[j])
297         continue;
298       intptr_t addr = (intptr_t)data->StackTrace[j];
299       if (beg <= addr && addr < end) {
300         data->modules[j] = name;
301         data->offsets[j] = addr - info->dlpi_addr;
302       }
303     }
304   }
305   return 0;
306 }
307
308 static bool findModulesAndOffsets(void **StackTrace, int Depth,
309                                   const char **Modules, intptr_t *Offsets,
310                                   const char *MainExecutableName) {
311   DlIteratePhdrData data = {StackTrace, Depth,   true,
312                             Modules,    Offsets, MainExecutableName};
313   dl_iterate_phdr(dl_iterate_phdr_cb, &data);
314   return true;
315 }
316 #else
317 static bool findModulesAndOffsets(void **StackTrace, int Depth,
318                                   const char **Modules, intptr_t *Offsets,
319                                   const char *MainExecutableName) {
320   return false;
321 }
322 #endif
323
324 static bool printSymbolizedStackTrace(void **StackTrace, int Depth, FILE *FD) {
325   // FIXME: Subtract necessary number from StackTrace entries to turn return addresses
326   // into actual instruction addresses.
327   // Use llvm-symbolizer tool to symbolize the stack traces.
328   std::string LLVMSymbolizerPath = sys::FindProgramByName("llvm-symbolizer");
329   if (LLVMSymbolizerPath.empty())
330     return false;
331   // We don't know argv0 or the address of main() at this point, but try
332   // to guess it anyway (it's possible on some platforms).
333   std::string MainExecutableName = sys::fs::getMainExecutable(nullptr, nullptr);
334   if (MainExecutableName.empty() ||
335       MainExecutableName.find("llvm-symbolizer") != std::string::npos)
336     return false;
337
338   std::vector<const char *> Modules(Depth, nullptr);
339   std::vector<intptr_t> Offsets(Depth, 0);
340   if (!findModulesAndOffsets(StackTrace, Depth, Modules.data(), Offsets.data(),
341                              MainExecutableName.c_str()))
342     return false;
343   int InputFD;
344   SmallString<32> InputFile, OutputFile;
345   sys::fs::createTemporaryFile("symbolizer-input", "", InputFD, InputFile);
346   sys::fs::createTemporaryFile("symbolizer-output", "", OutputFile);
347   FileRemover InputRemover(InputFile.c_str());
348   FileRemover OutputRemover(OutputFile.c_str());
349   std::vector<const StringRef *> Redirects(3, nullptr);
350   StringRef InputFileStr(InputFile);
351   StringRef OutputFileStr(OutputFile);
352   StringRef StderrFileStr;
353   Redirects[0] = &InputFileStr;
354   Redirects[1] = &OutputFileStr;
355   Redirects[2] = &StderrFileStr;
356
357   {
358     raw_fd_ostream Input(InputFD, true);
359     for (int i = 0; i < Depth; i++) {
360       if (Modules[i])
361         Input << Modules[i] << " " << (void*)Offsets[i] << "\n";
362     }
363   }
364
365   const char *args[] = {"llvm-symbolizer", nullptr};
366   int RunResult =
367       sys::ExecuteAndWait(LLVMSymbolizerPath, args, nullptr, Redirects.data());
368   if (RunResult != 0)
369     return false;
370
371   auto OutputBuf = MemoryBuffer::getFile(OutputFile.c_str());
372   if (std::error_code Err = OutputBuf.getError())
373     return false;
374   StringRef Output = OutputBuf.get()->getBuffer();
375   SmallVector<StringRef, 32> Lines;
376   Output.split(Lines, "\n");
377   auto CurLine = Lines.begin();
378   int frame_no = 0;
379   for (int i = 0; i < Depth; i++) {
380     if (!Modules[i]) {
381       fprintf(FD, "#%d %p\n", frame_no++, StackTrace[i]);
382       continue;
383     }
384     // Read pairs of lines (function name and file/line info) until we
385     // encounter empty line.
386     for (;;) {
387       StringRef FunctionName = *CurLine++;
388       if (FunctionName.empty())
389         break;
390       fprintf(FD, "#%d %p ", frame_no++, StackTrace[i]);
391       if (!FunctionName.startswith("??"))
392         fprintf(FD, "%s ", FunctionName.str().c_str());
393       StringRef FileLineInfo = *CurLine++;
394       if (!FileLineInfo.startswith("??"))
395         fprintf(FD, "%s", FileLineInfo.str().c_str());
396       else
397         fprintf(FD, "(%s+%p)", Modules[i], (void *)Offsets[i]);
398       fprintf(FD, "\n");
399     }
400   }
401   return true;
402 }
403
404 // PrintStackTrace - In the case of a program crash or fault, print out a stack
405 // trace so that the user has an indication of why and where we died.
406 //
407 // On glibc systems we have the 'backtrace' function, which works nicely, but
408 // doesn't demangle symbols.
409 void llvm::sys::PrintStackTrace(FILE *FD) {
410 #if defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES)
411   static void* StackTrace[256];
412   // Use backtrace() to output a backtrace on Linux systems with glibc.
413   int depth = backtrace(StackTrace,
414                         static_cast<int>(array_lengthof(StackTrace)));
415   if (printSymbolizedStackTrace(StackTrace, depth, FD))
416     return;
417 #if HAVE_DLFCN_H && __GNUG__
418   int width = 0;
419   for (int i = 0; i < depth; ++i) {
420     Dl_info dlinfo;
421     dladdr(StackTrace[i], &dlinfo);
422     const char* name = strrchr(dlinfo.dli_fname, '/');
423
424     int nwidth;
425     if (!name) nwidth = strlen(dlinfo.dli_fname);
426     else       nwidth = strlen(name) - 1;
427
428     if (nwidth > width) width = nwidth;
429   }
430
431   for (int i = 0; i < depth; ++i) {
432     Dl_info dlinfo;
433     dladdr(StackTrace[i], &dlinfo);
434
435     fprintf(FD, "%-2d", i);
436
437     const char* name = strrchr(dlinfo.dli_fname, '/');
438     if (!name) fprintf(FD, " %-*s", width, dlinfo.dli_fname);
439     else       fprintf(FD, " %-*s", width, name+1);
440
441     fprintf(FD, " %#0*lx",
442             (int)(sizeof(void*) * 2) + 2, (unsigned long)StackTrace[i]);
443
444     if (dlinfo.dli_sname != nullptr) {
445       fputc(' ', FD);
446 #  if HAVE_CXXABI_H
447       int res;
448       char* d = abi::__cxa_demangle(dlinfo.dli_sname, nullptr, nullptr, &res);
449 #  else
450       char* d = NULL;
451 #  endif
452       if (!d) fputs(dlinfo.dli_sname, FD);
453       else    fputs(d, FD);
454       free(d);
455
456       // FIXME: When we move to C++11, use %t length modifier. It's not in
457       // C++03 and causes gcc to issue warnings. Losing the upper 32 bits of
458       // the stack offset for a stack dump isn't likely to cause any problems.
459       fprintf(FD, " + %u",(unsigned)((char*)StackTrace[i]-
460                                      (char*)dlinfo.dli_saddr));
461     }
462     fputc('\n', FD);
463   }
464 #else
465   backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
466 #endif
467 #endif
468 }
469
470 static void PrintStackTraceSignalHandler(void *) {
471   PrintStackTrace(stderr);
472 }
473
474 /// PrintStackTraceOnErrorSignal - When an error signal (such as SIGABRT or
475 /// SIGSEGV) is delivered to the process, print a stack trace and then exit.
476 void llvm::sys::PrintStackTraceOnErrorSignal() {
477   AddSignalHandler(PrintStackTraceSignalHandler, nullptr);
478
479 #if defined(__APPLE__) && defined(ENABLE_CRASH_OVERRIDES)
480   // Environment variable to disable any kind of crash dialog.
481   if (getenv("LLVM_DISABLE_CRASH_REPORT")) {
482     mach_port_t self = mach_task_self();
483
484     exception_mask_t mask = EXC_MASK_CRASH;
485
486     kern_return_t ret = task_set_exception_ports(self,
487                              mask,
488                              MACH_PORT_NULL,
489                              EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES,
490                              THREAD_STATE_NONE);
491     (void)ret;
492   }
493 #endif
494 }
495
496
497 /***/
498
499 // On Darwin, raise sends a signal to the main thread instead of the current
500 // thread. This has the unfortunate effect that assert() and abort() will end up
501 // bypassing our crash recovery attempts. We work around this for anything in
502 // the same linkage unit by just defining our own versions of the assert handler
503 // and abort.
504
505 #if defined(__APPLE__) && defined(ENABLE_CRASH_OVERRIDES)
506
507 #include <signal.h>
508 #include <pthread.h>
509
510 int raise(int sig) {
511   return pthread_kill(pthread_self(), sig);
512 }
513
514 void __assert_rtn(const char *func,
515                   const char *file,
516                   int line,
517                   const char *expr) {
518   if (func)
519     fprintf(stderr, "Assertion failed: (%s), function %s, file %s, line %d.\n",
520             expr, func, file, line);
521   else
522     fprintf(stderr, "Assertion failed: (%s), file %s, line %d.\n",
523             expr, file, line);
524   abort();
525 }
526
527 void abort() {
528   raise(SIGABRT);
529   usleep(1000);
530   __builtin_trap();
531 }
532
533 #endif