1 //===- PrettyStackTrace.cpp - Pretty Crash Handling -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/System/ThreadLocal.h"
19 #include "llvm/ADT/SmallString.h"
22 // FIXME: This should be thread local when llvm supports threads.
23 static sys::ThreadLocal<const PrettyStackTraceEntry> PrettyStackTraceHead;
25 static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){
27 if (Entry->getNextEntry())
28 NextID = PrintStack(Entry->getNextEntry(), OS);
29 OS << NextID << ".\t";
35 /// PrintCurStackTrace - Print the current stack trace to the specified stream.
36 static void PrintCurStackTrace(raw_ostream &OS) {
37 // Don't print an empty trace.
38 if (PrettyStackTraceHead.get() == 0) return;
40 // If there are pretty stack frames registered, walk and emit them.
41 OS << "Stack dump:\n";
43 PrintStack(PrettyStackTraceHead.get(), OS);
47 // Integrate with crash reporter.
49 extern "C" const char *__crashreporter_info__;
50 const char *__crashreporter_info__ = 0;
54 /// CrashHandler - This callback is run if a fatal signal is delivered to the
55 /// process, it prints the pretty stack trace.
56 static void CrashHandler(void *Cookie) {
58 // On non-apple systems, just emit the crash stack trace to stderr.
59 PrintCurStackTrace(errs());
61 // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
62 // put it into __crashreporter_info__.
63 SmallString<2048> TmpStr;
65 raw_svector_ostream Stream(TmpStr);
66 PrintCurStackTrace(Stream);
69 if (!TmpStr.empty()) {
70 __crashreporter_info__ = strdup(TmpStr.c_str());
71 errs() << __crashreporter_info__;
77 static bool RegisterCrashPrinter() {
78 sys::AddSignalHandler(CrashHandler, 0);
82 PrettyStackTraceEntry::PrettyStackTraceEntry() {
83 // The first time this is called, we register the crash printer.
84 static bool HandlerRegistered = RegisterCrashPrinter();
85 HandlerRegistered = HandlerRegistered;
88 NextEntry = PrettyStackTraceHead.get();
89 PrettyStackTraceHead.set(this);
92 PrettyStackTraceEntry::~PrettyStackTraceEntry() {
93 assert(PrettyStackTraceHead.get() == this &&
94 "Pretty stack trace entry destruction is out of order");
95 PrettyStackTraceHead.set(getNextEntry());
98 void PrettyStackTraceString::print(raw_ostream &OS) const {
102 void PrettyStackTraceProgram::print(raw_ostream &OS) const {
103 OS << "Program arguments: ";
104 // Print the argument list.
105 for (unsigned i = 0, e = ArgC; i != e; ++i)
106 OS << ArgV[i] << ' ';