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"
20 // FIXME: This should be thread local when llvm supports threads.
21 static const PrettyStackTraceEntry *PrettyStackTraceHead = 0;
23 /// CrashHandler - This callback is run if a fatal signal is delivered to the
24 /// process, it prints the pretty stack trace.
25 static void CrashHandler(void *Cookie) {
26 // If there are pretty stack frames registered, walk and emit them.
27 raw_ostream &OS = errs();
28 OS << "Stack dump:\n";
31 for (const PrettyStackTraceEntry *Entry = PrettyStackTraceHead; Entry;
32 Entry = Entry->getNextEntry(), ++i) {
39 static bool RegisterCrashPrinter() {
40 sys::AddSignalHandler(CrashHandler, 0);
44 PrettyStackTraceEntry::PrettyStackTraceEntry() {
45 // The first time this is called, we register the crash printer.
46 static bool HandlerRegistered = RegisterCrashPrinter();
47 HandlerRegistered = HandlerRegistered;
50 NextEntry = PrettyStackTraceHead;
51 PrettyStackTraceHead = this;
54 PrettyStackTraceEntry::~PrettyStackTraceEntry() {
55 assert(PrettyStackTraceHead == this &&
56 "Pretty stack trace entry destruction is out of order");
57 PrettyStackTraceHead = getNextEntry();
60 void PrettyStackTraceString::print(raw_ostream &OS) const {
64 void PrettyStackTraceProgram::print(raw_ostream &OS) const {
65 OS << "Program arguments: ";
66 // Print the argument list.
67 for (unsigned i = 0, e = ArgC; i != e; ++i)