blob: 14290a1284fee36e1e7fc4fde72f48de11c9723f [file] [log] [blame]
Chris Lattnere97c7332009-03-04 21:40:23 +00001//===- 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/Support/PrettyStackTrace.h"
16#include "llvm/Support/raw_ostream.h"
17#include "llvm/System/Signals.h"
Owen Anderson0b49c322009-06-25 23:31:33 +000018#include "llvm/System/ThreadLocal.h"
Chris Lattner49155ff2009-03-06 07:19:54 +000019#include "llvm/ADT/SmallString.h"
Chris Lattnere97c7332009-03-04 21:40:23 +000020using namespace llvm;
21
22// FIXME: This should be thread local when llvm supports threads.
Owen Anderson0b49c322009-06-25 23:31:33 +000023static sys::ThreadLocal<const PrettyStackTraceEntry> PrettyStackTraceHead;
Chris Lattnere97c7332009-03-04 21:40:23 +000024
Chris Lattnerd56786f2009-03-05 07:03:49 +000025static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){
26 unsigned NextID = 0;
27 if (Entry->getNextEntry())
28 NextID = PrintStack(Entry->getNextEntry(), OS);
29 OS << NextID << ".\t";
30 Entry->print(OS);
31
32 return NextID+1;
33}
34
Chris Lattner49155ff2009-03-06 07:19:54 +000035/// PrintCurStackTrace - Print the current stack trace to the specified stream.
36static void PrintCurStackTrace(raw_ostream &OS) {
Chris Lattnerd56786f2009-03-05 07:03:49 +000037 // Don't print an empty trace.
Owen Anderson0b49c322009-06-25 23:31:33 +000038 if (PrettyStackTraceHead.get() == 0) return;
Chris Lattnerd56786f2009-03-05 07:03:49 +000039
Chris Lattnere97c7332009-03-04 21:40:23 +000040 // If there are pretty stack frames registered, walk and emit them.
Chris Lattnere97c7332009-03-04 21:40:23 +000041 OS << "Stack dump:\n";
42
Owen Anderson0b49c322009-06-25 23:31:33 +000043 PrintStack(PrettyStackTraceHead.get(), OS);
Chris Lattnere97c7332009-03-04 21:40:23 +000044 OS.flush();
45}
46
Chris Lattner49155ff2009-03-06 07:19:54 +000047// Integrate with crash reporter.
48#ifdef __APPLE__
49extern "C" const char *__crashreporter_info__;
50const char *__crashreporter_info__ = 0;
51#endif
52
53
54/// CrashHandler - This callback is run if a fatal signal is delivered to the
55/// process, it prints the pretty stack trace.
56static void CrashHandler(void *Cookie) {
57#ifndef __APPLE__
58 // On non-apple systems, just emit the crash stack trace to stderr.
59 PrintCurStackTrace(errs());
60#else
61 // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
62 // put it into __crashreporter_info__.
63 SmallString<2048> TmpStr;
64 {
65 raw_svector_ostream Stream(TmpStr);
66 PrintCurStackTrace(Stream);
67 }
68
69 if (!TmpStr.empty()) {
Dan Gohmanc7ffc9e2009-03-06 18:13:15 +000070 __crashreporter_info__ = strdup(TmpStr.c_str());
Chris Lattner49155ff2009-03-06 07:19:54 +000071 errs() << __crashreporter_info__;
Chris Lattner49155ff2009-03-06 07:19:54 +000072 }
73
74#endif
75}
76
Chris Lattnere97c7332009-03-04 21:40:23 +000077static bool RegisterCrashPrinter() {
78 sys::AddSignalHandler(CrashHandler, 0);
79 return false;
80}
81
82PrettyStackTraceEntry::PrettyStackTraceEntry() {
83 // The first time this is called, we register the crash printer.
84 static bool HandlerRegistered = RegisterCrashPrinter();
85 HandlerRegistered = HandlerRegistered;
86
87 // Link ourselves.
Owen Anderson0b49c322009-06-25 23:31:33 +000088 NextEntry = PrettyStackTraceHead.get();
89 PrettyStackTraceHead.set(this);
Chris Lattnere97c7332009-03-04 21:40:23 +000090}
91
92PrettyStackTraceEntry::~PrettyStackTraceEntry() {
Owen Anderson0b49c322009-06-25 23:31:33 +000093 assert(PrettyStackTraceHead.get() == this &&
Chris Lattnere97c7332009-03-04 21:40:23 +000094 "Pretty stack trace entry destruction is out of order");
Owen Anderson0b49c322009-06-25 23:31:33 +000095 PrettyStackTraceHead.set(getNextEntry());
Chris Lattnere97c7332009-03-04 21:40:23 +000096}
97
98void PrettyStackTraceString::print(raw_ostream &OS) const {
99 OS << Str << "\n";
100}
101
102void PrettyStackTraceProgram::print(raw_ostream &OS) const {
Chris Lattnerfcba7cd2009-03-05 06:51:42 +0000103 OS << "Program arguments: ";
Chris Lattnere97c7332009-03-04 21:40:23 +0000104 // Print the argument list.
105 for (unsigned i = 0, e = ArgC; i != e; ++i)
106 OS << ArgV[i] << ' ';
107 OS << '\n';
Chris Lattnerfcba7cd2009-03-05 06:51:42 +0000108}
109