[LPM] Rip all of ManagedStatic and ThreadLocal out of the pretty stack
[oota-llvm.git] / lib / Support / PrettyStackTrace.cpp
1 //===- PrettyStackTrace.cpp - Pretty Crash Handling -----------------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines some helpful functions for dealing with the possibility of
11 // Unix signals occurring while your program is running.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
22
23 #ifdef HAVE_CRASHREPORTERCLIENT_H
24 #include <CrashReporterClient.h>
25 #endif
26
27 using namespace llvm;
28
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.
35 #ifdef _MSC_VER // MSVC supports this with a __declspec.
36 static __declspec(thread) const PrettyStackTraceEntry
37     *PrettyStackTraceHead = nullptr;
38 #else // Clang, GCC, and all compatible compilers tend to use __thread.
39 static __thread const PrettyStackTraceEntry *PrettyStackTraceHead = nullptr;
40 #endif
41
42 static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){
43   unsigned NextID = 0;
44   if (Entry->getNextEntry())
45     NextID = PrintStack(Entry->getNextEntry(), OS);
46   OS << NextID << ".\t";
47   {
48     sys::Watchdog W(5);
49     Entry->print(OS);
50   }
51   
52   return NextID+1;
53 }
54
55 /// PrintCurStackTrace - Print the current stack trace to the specified stream.
56 static void PrintCurStackTrace(raw_ostream &OS) {
57   // Don't print an empty trace.
58   if (!PrettyStackTraceHead) return;
59   
60   // If there are pretty stack frames registered, walk and emit them.
61   OS << "Stack dump:\n";
62   
63   PrintStack(PrettyStackTraceHead, OS);
64   OS.flush();
65 }
66
67 // Integrate with crash reporter libraries.
68 #if defined (__APPLE__) && HAVE_CRASHREPORTERCLIENT_H
69 //  If any clients of llvm try to link to libCrashReporterClient.a themselves,
70 //  only one crash info struct will be used.
71 extern "C" {
72 CRASH_REPORTER_CLIENT_HIDDEN 
73 struct crashreporter_annotations_t gCRAnnotations 
74         __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION))) 
75         = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
76 }
77 #elif defined (__APPLE__) && HAVE_CRASHREPORTER_INFO
78 static const char *__crashreporter_info__ = 0;
79 asm(".desc ___crashreporter_info__, 0x10");
80 #endif
81
82
83 /// CrashHandler - This callback is run if a fatal signal is delivered to the
84 /// process, it prints the pretty stack trace.
85 static void CrashHandler(void *) {
86 #ifndef __APPLE__
87   // On non-apple systems, just emit the crash stack trace to stderr.
88   PrintCurStackTrace(errs());
89 #else
90   // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
91   // put it into __crashreporter_info__.
92   SmallString<2048> TmpStr;
93   {
94     raw_svector_ostream Stream(TmpStr);
95     PrintCurStackTrace(Stream);
96   }
97   
98   if (!TmpStr.empty()) {
99 #ifdef HAVE_CRASHREPORTERCLIENT_H
100     // Cast to void to avoid warning.
101     (void)CRSetCrashLogMessage(std::string(TmpStr.str()).c_str());
102 #elif HAVE_CRASHREPORTER_INFO 
103     __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str());
104 #endif
105     errs() << TmpStr.str();
106   }
107   
108 #endif
109 }
110
111 PrettyStackTraceEntry::PrettyStackTraceEntry() {
112   // Link ourselves.
113   NextEntry = PrettyStackTraceHead;
114   PrettyStackTraceHead = this;
115 }
116
117 PrettyStackTraceEntry::~PrettyStackTraceEntry() {
118   assert(PrettyStackTraceHead == this &&
119          "Pretty stack trace entry destruction is out of order");
120   PrettyStackTraceHead = getNextEntry();
121 }
122
123 void PrettyStackTraceString::print(raw_ostream &OS) const {
124   OS << Str << "\n";
125 }
126
127 void PrettyStackTraceProgram::print(raw_ostream &OS) const {
128   OS << "Program arguments: ";
129   // Print the argument list.
130   for (unsigned i = 0, e = ArgC; i != e; ++i)
131     OS << ArgV[i] << ' ';
132   OS << '\n';
133 }
134
135 static bool RegisterCrashPrinter() {
136   sys::AddSignalHandler(CrashHandler, nullptr);
137   return false;
138 }
139
140 void llvm::EnablePrettyStackTrace() {
141   // The first time this is called, we register the crash printer.
142   static bool HandlerRegistered = RegisterCrashPrinter();
143   (void)HandlerRegistered;
144 }
145
146 void LLVMEnablePrettyStackTrace() {
147   EnablePrettyStackTrace();
148 }