d08abc8ce155c2eb227184743b1d5ed52bd9350e
[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/ADT/SmallString.h"
17 #include "llvm/Config/config.h"     // Get autoconf configuration settings
18 #include "llvm/Support/ManagedStatic.h"
19 #include "llvm/Support/Signals.h"
20 #include "llvm/Support/ThreadLocal.h"
21 #include "llvm/Support/Watchdog.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm-c/Core.h"
24
25 #ifdef HAVE_CRASHREPORTERCLIENT_H
26 #include <CrashReporterClient.h>
27 #endif
28
29 using namespace llvm;
30
31 namespace llvm {
32   bool DisablePrettyStackTrace = false;
33 }
34
35 static ManagedStatic<sys::ThreadLocal<const PrettyStackTraceEntry> > PrettyStackTraceHead;
36
37 static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){
38   unsigned NextID = 0;
39   if (Entry->getNextEntry())
40     NextID = PrintStack(Entry->getNextEntry(), OS);
41   OS << NextID << ".\t";
42   {
43     sys::Watchdog W(5);
44     Entry->print(OS);
45   }
46   
47   return NextID+1;
48 }
49
50 /// PrintCurStackTrace - Print the current stack trace to the specified stream.
51 static void PrintCurStackTrace(raw_ostream &OS) {
52   // Don't print an empty trace.
53   if (PrettyStackTraceHead->get() == 0) return;
54   
55   // If there are pretty stack frames registered, walk and emit them.
56   OS << "Stack dump:\n";
57   
58   PrintStack(PrettyStackTraceHead->get(), OS);
59   OS.flush();
60 }
61
62 // Integrate with crash reporter libraries.
63 #if defined (__APPLE__) && HAVE_CRASHREPORTERCLIENT_H
64 //  If any clients of llvm try to link to libCrashReporterClient.a themselves,
65 //  only one crash info struct will be used.
66 extern "C" {
67 CRASH_REPORTER_CLIENT_HIDDEN 
68 struct crashreporter_annotations_t gCRAnnotations 
69         __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION))) 
70         = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
71 }
72 #elif defined (__APPLE__) && HAVE_CRASHREPORTER_INFO
73 static const char *__crashreporter_info__ = 0;
74 asm(".desc ___crashreporter_info__, 0x10");
75 #endif
76
77
78 /// CrashHandler - This callback is run if a fatal signal is delivered to the
79 /// process, it prints the pretty stack trace.
80 static void CrashHandler(void *) {
81 #ifndef __APPLE__
82   // On non-apple systems, just emit the crash stack trace to stderr.
83   PrintCurStackTrace(errs());
84 #else
85   // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
86   // put it into __crashreporter_info__.
87   SmallString<2048> TmpStr;
88   {
89     raw_svector_ostream Stream(TmpStr);
90     PrintCurStackTrace(Stream);
91   }
92   
93   if (!TmpStr.empty()) {
94 #ifdef HAVE_CRASHREPORTERCLIENT_H
95     // Cast to void to avoid warning.
96     (void)CRSetCrashLogMessage(std::string(TmpStr.str()).c_str());
97 #elif HAVE_CRASHREPORTER_INFO 
98     __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str());
99 #endif
100     errs() << TmpStr.str();
101   }
102   
103 #endif
104 }
105
106 static bool RegisterCrashPrinter() {
107   if (!DisablePrettyStackTrace)
108     sys::AddSignalHandler(CrashHandler, 0);
109   return false;
110 }
111
112 PrettyStackTraceEntry::PrettyStackTraceEntry() {
113   // The first time this is called, we register the crash printer.
114   static bool HandlerRegistered = RegisterCrashPrinter();
115   (void)HandlerRegistered;
116     
117   // Link ourselves.
118   NextEntry = PrettyStackTraceHead->get();
119   PrettyStackTraceHead->set(this);
120 }
121
122 PrettyStackTraceEntry::~PrettyStackTraceEntry() {
123   // Do nothing if PrettyStackTraceHead is uninitialized. This can only happen
124   // if a shutdown occurred after we created the PrettyStackTraceEntry. That
125   // does occur in the following idiom:
126   //
127   // PrettyStackTraceProgram X(...);
128   // llvm_shutdown_obj Y;
129   //
130   // Without this check, we may end up removing ourselves from the stack trace
131   // after PrettyStackTraceHead has already been destroyed.
132   if (!PrettyStackTraceHead.isConstructed())
133     return;
134   
135   assert(PrettyStackTraceHead->get() == this &&
136          "Pretty stack trace entry destruction is out of order");
137   PrettyStackTraceHead->set(getNextEntry());
138 }
139
140 void PrettyStackTraceString::print(raw_ostream &OS) const {
141   OS << Str << "\n";
142 }
143
144 void PrettyStackTraceProgram::print(raw_ostream &OS) const {
145   OS << "Program arguments: ";
146   // Print the argument list.
147   for (unsigned i = 0, e = ArgC; i != e; ++i)
148     OS << ArgV[i] << ' ';
149   OS << '\n';
150 }
151
152 void LLVMDisablePrettyStackTrace() {
153   DisablePrettyStackTrace = true;
154 }