Home | History | Annotate | Download | only in Support
      1 //===- PrettyStackTrace.cpp - Pretty Crash Handling -----------------------===//
      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 "llvm/Support/PrettyStackTrace.h"
     16 #include "llvm/ADT/SmallString.h"
     17 #include "llvm/Config/config.h"     // Get autoconf configuration settings
     18 #include "llvm/Support/Signals.h"
     19 #include "llvm/Support/ThreadLocal.h"
     20 #include "llvm/Support/Watchdog.h"
     21 #include "llvm/Support/raw_ostream.h"
     22 
     23 #ifdef HAVE_CRASHREPORTERCLIENT_H
     24 #include <CrashReporterClient.h>
     25 #endif
     26 
     27 using namespace llvm;
     28 
     29 namespace llvm {
     30   bool DisablePrettyStackTrace = false;
     31 }
     32 
     33 // FIXME: This should be thread local when llvm supports threads.
     34 static sys::ThreadLocal<const PrettyStackTraceEntry> PrettyStackTraceHead;
     35 
     36 static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){
     37   unsigned NextID = 0;
     38   if (Entry->getNextEntry())
     39     NextID = PrintStack(Entry->getNextEntry(), OS);
     40   OS << NextID << ".\t";
     41   {
     42     sys::Watchdog W(5);
     43     Entry->print(OS);
     44   }
     45 
     46   return NextID+1;
     47 }
     48 
     49 /// PrintCurStackTrace - Print the current stack trace to the specified stream.
     50 static void PrintCurStackTrace(raw_ostream &OS) {
     51   // Don't print an empty trace.
     52   if (PrettyStackTraceHead.get() == 0) return;
     53 
     54   // If there are pretty stack frames registered, walk and emit them.
     55   OS << "Stack dump:\n";
     56 
     57   PrintStack(PrettyStackTraceHead.get(), OS);
     58   OS.flush();
     59 }
     60 
     61 // Integrate with crash reporter libraries.
     62 #if defined (__APPLE__) && HAVE_CRASHREPORTERCLIENT_H
     63 //  If any clients of llvm try to link to libCrashReporterClient.a themselves,
     64 //  only one crash info struct will be used.
     65 extern "C" {
     66 CRASH_REPORTER_CLIENT_HIDDEN
     67 struct crashreporter_annotations_t gCRAnnotations
     68         __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
     69         = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
     70 }
     71 #elif defined (__APPLE__) && HAVE_CRASHREPORTER_INFO
     72 static const char *__crashreporter_info__ = 0;
     73 asm(".desc ___crashreporter_info__, 0x10");
     74 #endif
     75 
     76 
     77 /// CrashHandler - This callback is run if a fatal signal is delivered to the
     78 /// process, it prints the pretty stack trace.
     79 static void CrashHandler(void *) {
     80 #ifndef __APPLE__
     81   // On non-apple systems, just emit the crash stack trace to stderr.
     82   PrintCurStackTrace(errs());
     83 #else
     84   // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
     85   // put it into __crashreporter_info__.
     86   SmallString<2048> TmpStr;
     87   {
     88     raw_svector_ostream Stream(TmpStr);
     89     PrintCurStackTrace(Stream);
     90   }
     91 
     92   if (!TmpStr.empty()) {
     93 #ifdef HAVE_CRASHREPORTERCLIENT_H
     94     // Cast to void to avoid warning.
     95     (void)CRSetCrashLogMessage(std::string(TmpStr.str()).c_str());
     96 #elif HAVE_CRASHREPORTER_INFO
     97     __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str());
     98 #endif
     99     errs() << TmpStr.str();
    100   }
    101 
    102 #endif
    103 }
    104 
    105 static bool RegisterCrashPrinter() {
    106   if (!DisablePrettyStackTrace)
    107     sys::AddSignalHandler(CrashHandler, 0);
    108   return false;
    109 }
    110 
    111 PrettyStackTraceEntry::PrettyStackTraceEntry() {
    112   // The first time this is called, we register the crash printer.
    113   static bool HandlerRegistered = RegisterCrashPrinter();
    114   (void)HandlerRegistered;
    115 
    116   // Link ourselves.
    117   NextEntry = PrettyStackTraceHead.get();
    118   PrettyStackTraceHead.set(this);
    119 }
    120 
    121 PrettyStackTraceEntry::~PrettyStackTraceEntry() {
    122   assert(PrettyStackTraceHead.get() == this &&
    123          "Pretty stack trace entry destruction is out of order");
    124   PrettyStackTraceHead.set(getNextEntry());
    125 }
    126 
    127 void PrettyStackTraceString::print(raw_ostream &OS) const {
    128   OS << Str << "\n";
    129 }
    130 
    131 void PrettyStackTraceProgram::print(raw_ostream &OS) const {
    132   OS << "Program arguments: ";
    133   // Print the argument list.
    134   for (unsigned i = 0, e = ArgC; i != e; ++i)
    135     OS << ArgV[i] << ' ';
    136   OS << '\n';
    137 }
    138