e01c491fb3ab85ee955c24a8e46641121e00bc7b
[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 occuring while your program is running.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
21
22 #ifdef HAVE_CRASHREPORTERCLIENT_H
23 #include <CrashReporterClient.h>
24 #endif
25
26 using namespace llvm;
27
28 namespace llvm {
29   bool DisablePrettyStackTrace = false;
30 }
31
32 // FIXME: This should be thread local when llvm supports threads.
33 static sys::ThreadLocal<const PrettyStackTraceEntry> PrettyStackTraceHead;
34
35 static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){
36   unsigned NextID = 0;
37   if (Entry->getNextEntry())
38     NextID = PrintStack(Entry->getNextEntry(), OS);
39   OS << NextID << ".\t";
40   Entry->print(OS);
41   
42   return NextID+1;
43 }
44
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;
49   
50   // If there are pretty stack frames registered, walk and emit them.
51   OS << "Stack dump:\n";
52   
53   PrintStack(PrettyStackTraceHead.get(), OS);
54   OS.flush();
55 }
56
57 // Integrate with crash reporter.
58 #if defined (__APPLE__) && !defined (HAVE_CRASHREPORTERCLIENT_H)
59 static const char *__crashreporter_info__ = 0;
60 asm(".desc ___crashreporter_info__, 0x10");
61 #endif
62
63
64 /// CrashHandler - This callback is run if a fatal signal is delivered to the
65 /// process, it prints the pretty stack trace.
66 static void CrashHandler(void *Cookie) {
67 #ifndef __APPLE__
68   // On non-apple systems, just emit the crash stack trace to stderr.
69   PrintCurStackTrace(errs());
70 #else
71   // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
72   // put it into __crashreporter_info__.
73   SmallString<2048> TmpStr;
74   {
75     raw_svector_ostream Stream(TmpStr);
76     PrintCurStackTrace(Stream);
77   }
78   
79   if (!TmpStr.empty()) {
80 #ifndef HAVE_CRASHREPORTERCLIENT_H
81     __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str());
82 #else
83     CRSetCrashLogMessage(std::string(TmpStr.str()).c_str());
84 #endif
85     errs() << TmpStr.str();
86   }
87   
88 #endif
89 }
90
91 static bool RegisterCrashPrinter() {
92   if (!DisablePrettyStackTrace)
93     sys::AddSignalHandler(CrashHandler, 0);
94   return false;
95 }
96
97 PrettyStackTraceEntry::PrettyStackTraceEntry() {
98   // The first time this is called, we register the crash printer.
99   static bool HandlerRegistered = RegisterCrashPrinter();
100   HandlerRegistered = HandlerRegistered;
101     
102   // Link ourselves.
103   NextEntry = PrettyStackTraceHead.get();
104   PrettyStackTraceHead.set(this);
105 }
106
107 PrettyStackTraceEntry::~PrettyStackTraceEntry() {
108   assert(PrettyStackTraceHead.get() == this &&
109          "Pretty stack trace entry destruction is out of order");
110   PrettyStackTraceHead.set(getNextEntry());
111 }
112
113 void PrettyStackTraceString::print(raw_ostream &OS) const {
114   OS << Str << "\n";
115 }
116
117 void PrettyStackTraceProgram::print(raw_ostream &OS) const {
118   OS << "Program arguments: ";
119   // Print the argument list.
120   for (unsigned i = 0, e = ArgC; i != e; ++i)
121     OS << ArgV[i] << ' ';
122   OS << '\n';
123 }
124