LLVM API Documentation
00001 //===- PrettyStackTrace.cpp - Pretty Crash Handling -----------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines some helpful functions for dealing with the possibility of 00011 // Unix signals occurring while your program is running. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/Support/PrettyStackTrace.h" 00016 #include "llvm-c/Core.h" 00017 #include "llvm/ADT/SmallString.h" 00018 #include "llvm/Config/config.h" // Get autoconf configuration settings 00019 #include "llvm/Support/ManagedStatic.h" 00020 #include "llvm/Support/Signals.h" 00021 #include "llvm/Support/ThreadLocal.h" 00022 #include "llvm/Support/Watchdog.h" 00023 #include "llvm/Support/raw_ostream.h" 00024 00025 #ifdef HAVE_CRASHREPORTERCLIENT_H 00026 #include <CrashReporterClient.h> 00027 #endif 00028 00029 using namespace llvm; 00030 00031 static ManagedStatic<sys::ThreadLocal<const PrettyStackTraceEntry> > PrettyStackTraceHead; 00032 00033 static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){ 00034 unsigned NextID = 0; 00035 if (Entry->getNextEntry()) 00036 NextID = PrintStack(Entry->getNextEntry(), OS); 00037 OS << NextID << ".\t"; 00038 { 00039 sys::Watchdog W(5); 00040 Entry->print(OS); 00041 } 00042 00043 return NextID+1; 00044 } 00045 00046 /// PrintCurStackTrace - Print the current stack trace to the specified stream. 00047 static void PrintCurStackTrace(raw_ostream &OS) { 00048 // Don't print an empty trace. 00049 if (!PrettyStackTraceHead->get()) return; 00050 00051 // If there are pretty stack frames registered, walk and emit them. 00052 OS << "Stack dump:\n"; 00053 00054 PrintStack(PrettyStackTraceHead->get(), OS); 00055 OS.flush(); 00056 } 00057 00058 // Integrate with crash reporter libraries. 00059 #if defined (__APPLE__) && HAVE_CRASHREPORTERCLIENT_H 00060 // If any clients of llvm try to link to libCrashReporterClient.a themselves, 00061 // only one crash info struct will be used. 00062 extern "C" { 00063 CRASH_REPORTER_CLIENT_HIDDEN 00064 struct crashreporter_annotations_t gCRAnnotations 00065 __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION))) 00066 = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 }; 00067 } 00068 #elif defined (__APPLE__) && HAVE_CRASHREPORTER_INFO 00069 static const char *__crashreporter_info__ = 0; 00070 asm(".desc ___crashreporter_info__, 0x10"); 00071 #endif 00072 00073 00074 /// CrashHandler - This callback is run if a fatal signal is delivered to the 00075 /// process, it prints the pretty stack trace. 00076 static void CrashHandler(void *) { 00077 #ifndef __APPLE__ 00078 // On non-apple systems, just emit the crash stack trace to stderr. 00079 PrintCurStackTrace(errs()); 00080 #else 00081 // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also 00082 // put it into __crashreporter_info__. 00083 SmallString<2048> TmpStr; 00084 { 00085 raw_svector_ostream Stream(TmpStr); 00086 PrintCurStackTrace(Stream); 00087 } 00088 00089 if (!TmpStr.empty()) { 00090 #ifdef HAVE_CRASHREPORTERCLIENT_H 00091 // Cast to void to avoid warning. 00092 (void)CRSetCrashLogMessage(std::string(TmpStr.str()).c_str()); 00093 #elif HAVE_CRASHREPORTER_INFO 00094 __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str()); 00095 #endif 00096 errs() << TmpStr.str(); 00097 } 00098 00099 #endif 00100 } 00101 00102 PrettyStackTraceEntry::PrettyStackTraceEntry() { 00103 // Link ourselves. 00104 NextEntry = PrettyStackTraceHead->get(); 00105 PrettyStackTraceHead->set(this); 00106 } 00107 00108 PrettyStackTraceEntry::~PrettyStackTraceEntry() { 00109 // Do nothing if PrettyStackTraceHead is uninitialized. This can only happen 00110 // if a shutdown occurred after we created the PrettyStackTraceEntry. That 00111 // does occur in the following idiom: 00112 // 00113 // PrettyStackTraceProgram X(...); 00114 // llvm_shutdown_obj Y; 00115 // 00116 // Without this check, we may end up removing ourselves from the stack trace 00117 // after PrettyStackTraceHead has already been destroyed. 00118 if (!PrettyStackTraceHead.isConstructed()) 00119 return; 00120 00121 assert(PrettyStackTraceHead->get() == this && 00122 "Pretty stack trace entry destruction is out of order"); 00123 PrettyStackTraceHead->set(getNextEntry()); 00124 } 00125 00126 void PrettyStackTraceString::print(raw_ostream &OS) const { 00127 OS << Str << "\n"; 00128 } 00129 00130 void PrettyStackTraceProgram::print(raw_ostream &OS) const { 00131 OS << "Program arguments: "; 00132 // Print the argument list. 00133 for (unsigned i = 0, e = ArgC; i != e; ++i) 00134 OS << ArgV[i] << ' '; 00135 OS << '\n'; 00136 } 00137 00138 static bool RegisterCrashPrinter() { 00139 sys::AddSignalHandler(CrashHandler, nullptr); 00140 return false; 00141 } 00142 00143 void llvm::EnablePrettyStackTrace() { 00144 // The first time this is called, we register the crash printer. 00145 static bool HandlerRegistered = RegisterCrashPrinter(); 00146 (void)HandlerRegistered; 00147 } 00148 00149 void LLVMEnablePrettyStackTrace() { 00150 EnablePrettyStackTrace(); 00151 }