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/Signals.h"
20 #include "llvm/Support/Watchdog.h"
21 #include "llvm/Support/raw_ostream.h"
23 #ifdef HAVE_CRASHREPORTERCLIENT_H
24 #include <CrashReporterClient.h>
29 // We need a thread local pointer to manage the stack of our stack trace
30 // objects, but we *really* cannot tolerate destructors running and do not want
31 // to pay any overhead of synchronizing. As a consequence, we use a raw
32 // thread-local variable. Some day, we should be able to use a limited subset
33 // of C++11's thread_local, but compilers aren't up for it today.
34 // FIXME: This should be moved to a Compiler.h abstraction.
36 // MSVC supports this with a __declspec.
37 #define LLVM_THREAD_LOCAL __declspec(thread)
39 // Clang, GCC, and all compatible compilers tend to use __thread. But we need
40 // to work aronud a bug in the combination of Clang's compilation of
41 // local-dynamic TLS and the ppc64 linker relocations which we do by forcing to
43 // FIXME: Make this conditional on the Clang version once this is fixed in
45 #if defined(__clang__) && defined(__powerpc64__)
46 #define LLVM_THREAD_LOCAL __thread __attribute__((tls_model("general-dynamic")))
48 #define LLVM_THREAD_LOCAL __thread
52 static LLVM_THREAD_LOCAL const PrettyStackTraceEntry *PrettyStackTraceHead =
55 static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){
57 if (Entry->getNextEntry())
58 NextID = PrintStack(Entry->getNextEntry(), OS);
59 OS << NextID << ".\t";
68 /// PrintCurStackTrace - Print the current stack trace to the specified stream.
69 static void PrintCurStackTrace(raw_ostream &OS) {
70 // Don't print an empty trace.
71 if (!PrettyStackTraceHead) return;
73 // If there are pretty stack frames registered, walk and emit them.
74 OS << "Stack dump:\n";
76 PrintStack(PrettyStackTraceHead, OS);
80 // Integrate with crash reporter libraries.
81 #if defined (__APPLE__) && HAVE_CRASHREPORTERCLIENT_H
82 // If any clients of llvm try to link to libCrashReporterClient.a themselves,
83 // only one crash info struct will be used.
85 CRASH_REPORTER_CLIENT_HIDDEN
86 struct crashreporter_annotations_t gCRAnnotations
87 __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
88 = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
90 #elif defined (__APPLE__) && HAVE_CRASHREPORTER_INFO
91 static const char *__crashreporter_info__ = 0;
92 asm(".desc ___crashreporter_info__, 0x10");
96 /// CrashHandler - This callback is run if a fatal signal is delivered to the
97 /// process, it prints the pretty stack trace.
98 static void CrashHandler(void *) {
100 // On non-apple systems, just emit the crash stack trace to stderr.
101 PrintCurStackTrace(errs());
103 // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
104 // put it into __crashreporter_info__.
105 SmallString<2048> TmpStr;
107 raw_svector_ostream Stream(TmpStr);
108 PrintCurStackTrace(Stream);
111 if (!TmpStr.empty()) {
112 #ifdef HAVE_CRASHREPORTERCLIENT_H
113 // Cast to void to avoid warning.
114 (void)CRSetCrashLogMessage(std::string(TmpStr.str()).c_str());
115 #elif HAVE_CRASHREPORTER_INFO
116 __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str());
118 errs() << TmpStr.str();
124 PrettyStackTraceEntry::PrettyStackTraceEntry() {
126 NextEntry = PrettyStackTraceHead;
127 PrettyStackTraceHead = this;
130 PrettyStackTraceEntry::~PrettyStackTraceEntry() {
131 assert(PrettyStackTraceHead == this &&
132 "Pretty stack trace entry destruction is out of order");
133 PrettyStackTraceHead = getNextEntry();
136 void PrettyStackTraceString::print(raw_ostream &OS) const {
140 void PrettyStackTraceProgram::print(raw_ostream &OS) const {
141 OS << "Program arguments: ";
142 // Print the argument list.
143 for (unsigned i = 0, e = ArgC; i != e; ++i)
144 OS << ArgV[i] << ' ';
148 static bool RegisterCrashPrinter() {
149 sys::AddSignalHandler(CrashHandler, nullptr);
153 void llvm::EnablePrettyStackTrace() {
154 // The first time this is called, we register the crash printer.
155 static bool HandlerRegistered = RegisterCrashPrinter();
156 (void)HandlerRegistered;
159 void LLVMEnablePrettyStackTrace() {
160 EnablePrettyStackTrace();