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/ADT/SmallString.h"
17 #include "llvm/Config/config.h" // Get autoconf configuration settings
18 #include "llvm/Support/Signals.h"
19 #include "llvm/Support/ThreadLocal.h"
20 #include "llvm/Support/Watchdog.h"
21 #include "llvm/Support/raw_ostream.h"
23 #ifdef HAVE_CRASHREPORTERCLIENT_H
24 #include <CrashReporterClient.h>
30 bool DisablePrettyStackTrace = false;
33 // FIXME: This should be thread local when llvm supports threads.
34 static sys::ThreadLocal<const PrettyStackTraceEntry> PrettyStackTraceHead;
36 static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){
38 if (Entry->getNextEntry())
39 NextID = PrintStack(Entry->getNextEntry(), OS);
40 OS << NextID << ".\t";
49 /// PrintCurStackTrace - Print the current stack trace to the specified stream.
50 static void PrintCurStackTrace(raw_ostream &OS) {
51 // Don't print an empty trace.
52 if (PrettyStackTraceHead.get() == 0) return;
54 // If there are pretty stack frames registered, walk and emit them.
55 OS << "Stack dump:\n";
57 PrintStack(PrettyStackTraceHead.get(), OS);
61 // Integrate with crash reporter libraries.
62 #if defined (__APPLE__) && HAVE_CRASHREPORTERCLIENT_H
63 // If any clients of llvm try to link to libCrashReporterClient.a themselves,
64 // only one crash info struct will be used.
66 CRASH_REPORTER_CLIENT_HIDDEN
67 struct crashreporter_annotations_t gCRAnnotations
68 __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
69 = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
71 #elif defined (__APPLE__) && HAVE_CRASHREPORTER_INFO
72 static const char *__crashreporter_info__ = 0;
73 asm(".desc ___crashreporter_info__, 0x10");
77 /// CrashHandler - This callback is run if a fatal signal is delivered to the
78 /// process, it prints the pretty stack trace.
79 static void CrashHandler(void *) {
81 // On non-apple systems, just emit the crash stack trace to stderr.
82 PrintCurStackTrace(errs());
84 // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
85 // put it into __crashreporter_info__.
86 SmallString<2048> TmpStr;
88 raw_svector_ostream Stream(TmpStr);
89 PrintCurStackTrace(Stream);
92 if (!TmpStr.empty()) {
93 #ifdef HAVE_CRASHREPORTERCLIENT_H
94 // Cast to void to avoid warning.
95 (void)CRSetCrashLogMessage(std::string(TmpStr.str()).c_str());
96 #elif HAVE_CRASHREPORTER_INFO
97 __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str());
99 errs() << TmpStr.str();
105 static bool RegisterCrashPrinter() {
106 if (!DisablePrettyStackTrace)
107 sys::AddSignalHandler(CrashHandler, 0);
111 PrettyStackTraceEntry::PrettyStackTraceEntry() {
112 // The first time this is called, we register the crash printer.
113 static bool HandlerRegistered = RegisterCrashPrinter();
114 (void)HandlerRegistered;
117 NextEntry = PrettyStackTraceHead.get();
118 PrettyStackTraceHead.set(this);
121 PrettyStackTraceEntry::~PrettyStackTraceEntry() {
122 assert(PrettyStackTraceHead.get() == this &&
123 "Pretty stack trace entry destruction is out of order");
124 PrettyStackTraceHead.set(getNextEntry());
127 void PrettyStackTraceString::print(raw_ostream &OS) const {
131 void PrettyStackTraceProgram::print(raw_ostream &OS) const {
132 OS << "Program arguments: ";
133 // Print the argument list.
134 for (unsigned i = 0, e = ArgC; i != e; ++i)
135 OS << ArgV[i] << ' ';