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 occuring while your program is running. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/Support/PrettyStackTrace.h" 00016 #include "llvm/Support/raw_ostream.h" 00017 #include "llvm/System/Signals.h" 00018 #include "llvm/System/ThreadLocal.h" 00019 #include "llvm/ADT/SmallString.h" 00020 using namespace llvm; 00021 00022 namespace llvm { 00023 bool DisablePrettyStackTrace = false; 00024 } 00025 00026 // FIXME: This should be thread local when llvm supports threads. 00027 static sys::ThreadLocal<const PrettyStackTraceEntry> PrettyStackTraceHead; 00028 00029 static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){ 00030 unsigned NextID = 0; 00031 if (Entry->getNextEntry()) 00032 NextID = PrintStack(Entry->getNextEntry(), OS); 00033 OS << NextID << ".\t"; 00034 Entry->print(OS); 00035 00036 return NextID+1; 00037 } 00038 00039 /// PrintCurStackTrace - Print the current stack trace to the specified stream. 00040 static void PrintCurStackTrace(raw_ostream &OS) { 00041 // Don't print an empty trace. 00042 if (PrettyStackTraceHead.get() == 0) return; 00043 00044 // If there are pretty stack frames registered, walk and emit them. 00045 OS << "Stack dump:\n"; 00046 00047 PrintStack(PrettyStackTraceHead.get(), OS); 00048 OS.flush(); 00049 } 00050 00051 // Integrate with crash reporter. 00052 #ifdef __APPLE__ 00053 extern "C" const char *__crashreporter_info__; 00054 const char *__crashreporter_info__ = 0; 00055 #endif 00056 00057 00058 /// CrashHandler - This callback is run if a fatal signal is delivered to the 00059 /// process, it prints the pretty stack trace. 00060 static void CrashHandler(void *Cookie) { 00061 #ifndef __APPLE__ 00062 // On non-apple systems, just emit the crash stack trace to stderr. 00063 PrintCurStackTrace(errs()); 00064 #else 00065 // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also 00066 // put it into __crashreporter_info__. 00067 SmallString<2048> TmpStr; 00068 { 00069 raw_svector_ostream Stream(TmpStr); 00070 PrintCurStackTrace(Stream); 00071 } 00072 00073 if (!TmpStr.empty()) { 00074 __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str()); 00075 errs() << TmpStr.str(); 00076 } 00077 00078 #endif 00079 } 00080 00081 static bool RegisterCrashPrinter() { 00082 if (!DisablePrettyStackTrace) 00083 sys::AddSignalHandler(CrashHandler, 0); 00084 return false; 00085 } 00086 00087 PrettyStackTraceEntry::PrettyStackTraceEntry() { 00088 // The first time this is called, we register the crash printer. 00089 static bool HandlerRegistered = RegisterCrashPrinter(); 00090 HandlerRegistered = HandlerRegistered; 00091 00092 // Link ourselves. 00093 NextEntry = PrettyStackTraceHead.get(); 00094 PrettyStackTraceHead.set(this); 00095 } 00096 00097 PrettyStackTraceEntry::~PrettyStackTraceEntry() { 00098 assert(PrettyStackTraceHead.get() == this && 00099 "Pretty stack trace entry destruction is out of order"); 00100 PrettyStackTraceHead.set(getNextEntry()); 00101 } 00102 00103 void PrettyStackTraceString::print(raw_ostream &OS) const { 00104 OS << Str << "\n"; 00105 } 00106 00107 void PrettyStackTraceProgram::print(raw_ostream &OS) const { 00108 OS << "Program arguments: "; 00109 // Print the argument list. 00110 for (unsigned i = 0, e = ArgC; i != e; ++i) 00111 OS << ArgV[i] << ' '; 00112 OS << '\n'; 00113 } 00114