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