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 occuring while your program is running.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Config/config.h" // Get autoconf configuration settings
16 #include "llvm/Support/PrettyStackTrace.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "llvm/System/Signals.h"
19 #include "llvm/System/ThreadLocal.h"
20 #include "llvm/ADT/SmallString.h"
22 #ifdef HAVE_CRASHREPORTERCLIENT_H
23 #include <CrashReporterClient.h>
29 bool DisablePrettyStackTrace = false;
32 // FIXME: This should be thread local when llvm supports threads.
33 static sys::ThreadLocal<const PrettyStackTraceEntry> PrettyStackTraceHead;
35 static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){
37 if (Entry->getNextEntry())
38 NextID = PrintStack(Entry->getNextEntry(), OS);
39 OS << NextID << ".\t";
45 /// PrintCurStackTrace - Print the current stack trace to the specified stream.
46 static void PrintCurStackTrace(raw_ostream &OS) {
47 // Don't print an empty trace.
48 if (PrettyStackTraceHead.get() == 0) return;
50 // If there are pretty stack frames registered, walk and emit them.
51 OS << "Stack dump:\n";
53 PrintStack(PrettyStackTraceHead.get(), OS);
57 // Integrate with crash reporter libraries.
58 #if defined (__APPLE__) && defined (HAVE_CRASHREPORTERCLIENT_H)
59 // If any clients of llvm try to link to libCrashReporterClient.a themselves,
60 // only one crash info struct will be used.
61 CRASH_REPORTER_CLIENT_HIDDEN
62 struct crashreporter_annotations_t gCRAnnotations
63 __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
64 = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0 };
65 #else if defined (__APPLE__)
66 static const char *__crashreporter_info__ = 0;
67 asm(".desc ___crashreporter_info__, 0x10");
71 /// CrashHandler - This callback is run if a fatal signal is delivered to the
72 /// process, it prints the pretty stack trace.
73 static void CrashHandler(void *Cookie) {
75 // On non-apple systems, just emit the crash stack trace to stderr.
76 PrintCurStackTrace(errs());
78 // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
79 // put it into __crashreporter_info__.
80 SmallString<2048> TmpStr;
82 raw_svector_ostream Stream(TmpStr);
83 PrintCurStackTrace(Stream);
86 if (!TmpStr.empty()) {
87 #ifndef HAVE_CRASHREPORTERCLIENT_H
88 __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str());
90 CRSetCrashLogMessage(std::string(TmpStr.str()).c_str());
92 errs() << TmpStr.str();
98 static bool RegisterCrashPrinter() {
99 if (!DisablePrettyStackTrace)
100 sys::AddSignalHandler(CrashHandler, 0);
104 PrettyStackTraceEntry::PrettyStackTraceEntry() {
105 // The first time this is called, we register the crash printer.
106 static bool HandlerRegistered = RegisterCrashPrinter();
107 HandlerRegistered = HandlerRegistered;
110 NextEntry = PrettyStackTraceHead.get();
111 PrettyStackTraceHead.set(this);
114 PrettyStackTraceEntry::~PrettyStackTraceEntry() {
115 assert(PrettyStackTraceHead.get() == this &&
116 "Pretty stack trace entry destruction is out of order");
117 PrettyStackTraceHead.set(getNextEntry());
120 void PrettyStackTraceString::print(raw_ostream &OS) const {
124 void PrettyStackTraceProgram::print(raw_ostream &OS) const {
125 OS << "Program arguments: ";
126 // Print the argument list.
127 for (unsigned i = 0, e = ArgC; i != e; ++i)
128 OS << ArgV[i] << ' ';