Update comments.
[llvm/msp430.git] / lib / Support / PrettyStackTrace.cpp
blobc111c5e6be24e0ec1c8b104bbc3f5cc46e0ec532
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.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Support/PrettyStackTrace.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include "llvm/System/Signals.h"
18 #include "llvm/ADT/SmallString.h"
19 using namespace llvm;
21 // FIXME: This should be thread local when llvm supports threads.
22 static const PrettyStackTraceEntry *PrettyStackTraceHead = 0;
24 static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){
25 unsigned NextID = 0;
26 if (Entry->getNextEntry())
27 NextID = PrintStack(Entry->getNextEntry(), OS);
28 OS << NextID << ".\t";
29 Entry->print(OS);
31 return NextID+1;
34 /// PrintCurStackTrace - Print the current stack trace to the specified stream.
35 static void PrintCurStackTrace(raw_ostream &OS) {
36 // Don't print an empty trace.
37 if (PrettyStackTraceHead == 0) return;
39 // If there are pretty stack frames registered, walk and emit them.
40 OS << "Stack dump:\n";
42 PrintStack(PrettyStackTraceHead, OS);
43 OS.flush();
46 // Integrate with crash reporter.
47 #ifdef __APPLE__
48 extern "C" const char *__crashreporter_info__;
49 const char *__crashreporter_info__ = 0;
50 #endif
53 /// CrashHandler - This callback is run if a fatal signal is delivered to the
54 /// process, it prints the pretty stack trace.
55 static void CrashHandler(void *Cookie) {
56 #ifndef __APPLE__
57 // On non-apple systems, just emit the crash stack trace to stderr.
58 PrintCurStackTrace(errs());
59 #else
60 // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
61 // put it into __crashreporter_info__.
62 SmallString<2048> TmpStr;
64 raw_svector_ostream Stream(TmpStr);
65 PrintCurStackTrace(Stream);
68 if (!TmpStr.empty()) {
69 __crashreporter_info__ = strdup(TmpStr.c_str());
70 errs() << __crashreporter_info__;
73 #endif
76 static bool RegisterCrashPrinter() {
77 sys::AddSignalHandler(CrashHandler, 0);
78 return false;
81 PrettyStackTraceEntry::PrettyStackTraceEntry() {
82 // The first time this is called, we register the crash printer.
83 static bool HandlerRegistered = RegisterCrashPrinter();
84 HandlerRegistered = HandlerRegistered;
86 // Link ourselves.
87 NextEntry = PrettyStackTraceHead;
88 PrettyStackTraceHead = this;
91 PrettyStackTraceEntry::~PrettyStackTraceEntry() {
92 assert(PrettyStackTraceHead == this &&
93 "Pretty stack trace entry destruction is out of order");
94 PrettyStackTraceHead = getNextEntry();
97 void PrettyStackTraceString::print(raw_ostream &OS) const {
98 OS << Str << "\n";
101 void PrettyStackTraceProgram::print(raw_ostream &OS) const {
102 OS << "Program arguments: ";
103 // Print the argument list.
104 for (unsigned i = 0, e = ArgC; i != e; ++i)
105 OS << ArgV[i] << ' ';
106 OS << '\n';