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/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"
25 #ifdef HAVE_CRASHREPORTERCLIENT_H
26 #include <CrashReporterClient.h>
31 static ManagedStatic<sys::ThreadLocal<const PrettyStackTraceEntry> > PrettyStackTraceHead;
33 static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){
35 if (Entry->getNextEntry())
36 NextID = PrintStack(Entry->getNextEntry(), OS);
37 OS << NextID << ".\t";
46 /// PrintCurStackTrace - Print the current stack trace to the specified stream.
47 static void PrintCurStackTrace(raw_ostream &OS) {
48 // Don't print an empty trace.
49 if (!PrettyStackTraceHead->get()) return;
51 // If there are pretty stack frames registered, walk and emit them.
52 OS << "Stack dump:\n";
54 PrintStack(PrettyStackTraceHead->get(), OS);
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.
63 CRASH_REPORTER_CLIENT_HIDDEN
64 struct crashreporter_annotations_t gCRAnnotations
65 __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
66 = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
68 #elif defined (__APPLE__) && HAVE_CRASHREPORTER_INFO
69 static const char *__crashreporter_info__ = 0;
70 asm(".desc ___crashreporter_info__, 0x10");
74 /// CrashHandler - This callback is run if a fatal signal is delivered to the
75 /// process, it prints the pretty stack trace.
76 static void CrashHandler(void *) {
78 // On non-apple systems, just emit the crash stack trace to stderr.
79 PrintCurStackTrace(errs());
81 // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
82 // put it into __crashreporter_info__.
83 SmallString<2048> TmpStr;
85 raw_svector_ostream Stream(TmpStr);
86 PrintCurStackTrace(Stream);
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());
96 errs() << TmpStr.str();
102 PrettyStackTraceEntry::PrettyStackTraceEntry() {
104 NextEntry = PrettyStackTraceHead->get();
105 PrettyStackTraceHead->set(this);
108 PrettyStackTraceEntry::~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:
113 // PrettyStackTraceProgram X(...);
114 // llvm_shutdown_obj Y;
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())
121 assert(PrettyStackTraceHead->get() == this &&
122 "Pretty stack trace entry destruction is out of order");
123 PrettyStackTraceHead->set(getNextEntry());
126 void PrettyStackTraceString::print(raw_ostream &OS) const {
130 void 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] << ' ';
138 static bool RegisterCrashPrinter() {
139 sys::AddSignalHandler(CrashHandler, nullptr);
143 void llvm::EnablePrettyStackTrace() {
144 // The first time this is called, we register the crash printer.
145 static bool HandlerRegistered = RegisterCrashPrinter();
146 (void)HandlerRegistered;
149 void LLVMEnablePrettyStackTrace() {
150 EnablePrettyStackTrace();