1 //===- PrettyStackTrace.cpp - Pretty Crash Handling -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines some helpful functions for dealing with the possibility of
11 // Unix signals occurring while your program is running.
13 //===----------------------------------------------------------------------===//
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/Compiler.h"
20 #include "llvm/Support/Signals.h"
21 #include "llvm/Support/Watchdog.h"
22 #include "llvm/Support/raw_ostream.h"
24 #ifdef HAVE_CRASHREPORTERCLIENT_H
25 #include <CrashReporterClient.h>
30 // We need a thread local pointer to manage the stack of our stack trace
31 // objects, but we *really* cannot tolerate destructors running and do not want
32 // to pay any overhead of synchronizing. As a consequence, we use a raw
33 // thread-local variable.
34 static LLVM_THREAD_LOCAL const PrettyStackTraceEntry *PrettyStackTraceHead =
37 static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){
39 if (Entry->getNextEntry())
40 NextID = PrintStack(Entry->getNextEntry(), OS);
41 OS << NextID << ".\t";
50 /// PrintCurStackTrace - Print the current stack trace to the specified stream.
51 static void PrintCurStackTrace(raw_ostream &OS) {
52 // Don't print an empty trace.
53 if (!PrettyStackTraceHead) return;
55 // If there are pretty stack frames registered, walk and emit them.
56 OS << "Stack dump:\n";
58 PrintStack(PrettyStackTraceHead, OS);
62 // Integrate with crash reporter libraries.
63 #if defined (__APPLE__) && HAVE_CRASHREPORTERCLIENT_H
64 // If any clients of llvm try to link to libCrashReporterClient.a themselves,
65 // only one crash info struct will be used.
67 CRASH_REPORTER_CLIENT_HIDDEN
68 struct crashreporter_annotations_t gCRAnnotations
69 __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
70 = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
72 #elif defined (__APPLE__) && HAVE_CRASHREPORTER_INFO
73 static const char *__crashreporter_info__ = 0;
74 asm(".desc ___crashreporter_info__, 0x10");
78 /// CrashHandler - This callback is run if a fatal signal is delivered to the
79 /// process, it prints the pretty stack trace.
80 static void CrashHandler(void *) {
82 // On non-apple systems, just emit the crash stack trace to stderr.
83 PrintCurStackTrace(errs());
85 // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
86 // put it into __crashreporter_info__.
87 SmallString<2048> TmpStr;
89 raw_svector_ostream Stream(TmpStr);
90 PrintCurStackTrace(Stream);
93 if (!TmpStr.empty()) {
94 #ifdef HAVE_CRASHREPORTERCLIENT_H
95 // Cast to void to avoid warning.
96 (void)CRSetCrashLogMessage(std::string(TmpStr.str()).c_str());
97 #elif HAVE_CRASHREPORTER_INFO
98 __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str());
100 errs() << TmpStr.str();
106 PrettyStackTraceEntry::PrettyStackTraceEntry() {
108 NextEntry = PrettyStackTraceHead;
109 PrettyStackTraceHead = this;
112 PrettyStackTraceEntry::~PrettyStackTraceEntry() {
113 assert(PrettyStackTraceHead == this &&
114 "Pretty stack trace entry destruction is out of order");
115 PrettyStackTraceHead = getNextEntry();
118 void PrettyStackTraceString::print(raw_ostream &OS) const {
122 void PrettyStackTraceProgram::print(raw_ostream &OS) const {
123 OS << "Program arguments: ";
124 // Print the argument list.
125 for (unsigned i = 0, e = ArgC; i != e; ++i)
126 OS << ArgV[i] << ' ';
130 static bool RegisterCrashPrinter() {
131 sys::AddSignalHandler(CrashHandler, nullptr);
135 void llvm::EnablePrettyStackTrace() {
136 // The first time this is called, we register the crash printer.
137 static bool HandlerRegistered = RegisterCrashPrinter();
138 (void)HandlerRegistered;
141 void LLVMEnablePrettyStackTrace() {
142 EnablePrettyStackTrace();