Windows: Add support for unicode command lines
[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
24 #ifdef HAVE_CRASHREPORTERCLIENT_H
25 #include <CrashReporterClient.h>
26 #endif
27
28 using namespace llvm;
29
30 namespace llvm {
31   bool DisablePrettyStackTrace = false;
32 }
33
34 static ManagedStatic<sys::ThreadLocal<const PrettyStackTraceEntry> > PrettyStackTraceHead;
35
36 static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){
37   unsigned NextID = 0;
38   if (Entry->getNextEntry())
39     NextID = PrintStack(Entry->getNextEntry(), OS);
40   OS << NextID << ".\t";
41   {
42     sys::Watchdog W(5);
43     Entry->print(OS);
44   }
45   
46   return NextID+1;
47 }
48
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;
53   
54   // If there are pretty stack frames registered, walk and emit them.
55   OS << "Stack dump:\n";
56   
57   PrintStack(PrettyStackTraceHead->get(), OS);
58   OS.flush();
59 }
60
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.
65 extern "C" {
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 };
70 }
71 #elif defined (__APPLE__) && HAVE_CRASHREPORTER_INFO
72 static const char *__crashreporter_info__ = 0;
73 asm(".desc ___crashreporter_info__, 0x10");
74 #endif
75
76
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 *) {
80 #ifndef __APPLE__
81   // On non-apple systems, just emit the crash stack trace to stderr.
82   PrintCurStackTrace(errs());
83 #else
84   // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
85   // put it into __crashreporter_info__.
86   SmallString<2048> TmpStr;
87   {
88     raw_svector_ostream Stream(TmpStr);
89     PrintCurStackTrace(Stream);
90   }
91   
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());
98 #endif
99     errs() << TmpStr.str();
100   }
101   
102 #endif
103 }
104
105 static bool RegisterCrashPrinter() {
106   if (!DisablePrettyStackTrace)
107     sys::AddSignalHandler(CrashHandler, 0);
108   return false;
109 }
110
111 PrettyStackTraceEntry::PrettyStackTraceEntry() {
112   // The first time this is called, we register the crash printer.
113   static bool HandlerRegistered = RegisterCrashPrinter();
114   (void)HandlerRegistered;
115     
116   // Link ourselves.
117   NextEntry = PrettyStackTraceHead->get();
118   PrettyStackTraceHead->set(this);
119 }
120
121 PrettyStackTraceEntry::~PrettyStackTraceEntry() {
122   // Do nothing if PrettyStackTraceHead is uninitialized. This can only happen
123   // if a shutdown occurred after we created the PrettyStackTraceEntry. That
124   // does occur in the following idiom:
125   //
126   // PrettyStackTraceProgram X(...);
127   // llvm_shutdown_obj Y;
128   //
129   // Without this check, we may end up removing ourselves from the stack trace
130   // after PrettyStackTraceHead has already been destroyed.
131   if (!PrettyStackTraceHead.isConstructed())
132     return;
133   
134   assert(PrettyStackTraceHead->get() == this &&
135          "Pretty stack trace entry destruction is out of order");
136   PrettyStackTraceHead->set(getNextEntry());
137 }
138
139 void PrettyStackTraceString::print(raw_ostream &OS) const {
140   OS << Str << "\n";
141 }
142
143 void PrettyStackTraceProgram::print(raw_ostream &OS) const {
144   OS << "Program arguments: ";
145   // Print the argument list.
146   for (unsigned i = 0, e = ArgC; i != e; ++i)
147     OS << ArgV[i] << ' ';
148   OS << '\n';
149 }