blob: 23ee5ab105aec8316f598e42b261f2a4d4297bd2 [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
Chris Lattner7a2bdde2011-04-15 05:18:47 +000011// Unix signals occurring while your program is running.
Chris Lattnere97c7332009-03-04 21:40:23 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Support/PrettyStackTrace.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/SmallString.h"
17#include "llvm/Config/config.h" // Get autoconf configuration settings
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000018#include "llvm/Support/Signals.h"
19#include "llvm/Support/ThreadLocal.h"
Nick Lewyckyd5e1be02013-03-26 01:27:52 +000020#include "llvm/Support/Watchdog.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000021#include "llvm/Support/raw_ostream.h"
Eric Christopher654dee42010-06-22 21:01:04 +000022
23#ifdef HAVE_CRASHREPORTERCLIENT_H
24#include <CrashReporterClient.h>
25#endif
26
Chris Lattnere97c7332009-03-04 21:40:23 +000027using namespace llvm;
28
Chris Lattner90bb3f32009-07-16 06:17:45 +000029namespace llvm {
30 bool DisablePrettyStackTrace = false;
31}
32
Chris Lattnere97c7332009-03-04 21:40:23 +000033// FIXME: This should be thread local when llvm supports threads.
Owen Anderson0b49c322009-06-25 23:31:33 +000034static sys::ThreadLocal<const PrettyStackTraceEntry> PrettyStackTraceHead;
Chris Lattnere97c7332009-03-04 21:40:23 +000035
Chris Lattnerd56786f2009-03-05 07:03:49 +000036static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){
37 unsigned NextID = 0;
38 if (Entry->getNextEntry())
39 NextID = PrintStack(Entry->getNextEntry(), OS);
40 OS << NextID << ".\t";
Nick Lewyckyd5e1be02013-03-26 01:27:52 +000041 {
42 sys::Watchdog W(5);
43 Entry->print(OS);
44 }
Chris Lattnerd56786f2009-03-05 07:03:49 +000045
46 return NextID+1;
47}
48
Chris Lattner49155ff2009-03-06 07:19:54 +000049/// PrintCurStackTrace - Print the current stack trace to the specified stream.
50static void PrintCurStackTrace(raw_ostream &OS) {
Chris Lattnerd56786f2009-03-05 07:03:49 +000051 // Don't print an empty trace.
Owen Anderson0b49c322009-06-25 23:31:33 +000052 if (PrettyStackTraceHead.get() == 0) return;
Chris Lattnerd56786f2009-03-05 07:03:49 +000053
Chris Lattnere97c7332009-03-04 21:40:23 +000054 // If there are pretty stack frames registered, walk and emit them.
Chris Lattnere97c7332009-03-04 21:40:23 +000055 OS << "Stack dump:\n";
56
Owen Anderson0b49c322009-06-25 23:31:33 +000057 PrintStack(PrettyStackTraceHead.get(), OS);
Chris Lattnere97c7332009-03-04 21:40:23 +000058 OS.flush();
59}
60
Eric Christopher4411fbe2010-06-28 18:25:51 +000061// Integrate with crash reporter libraries.
Eric Christopher481d4022010-12-03 07:45:22 +000062#if defined (__APPLE__) && HAVE_CRASHREPORTERCLIENT_H
Eric Christopher4411fbe2010-06-28 18:25:51 +000063// If any clients of llvm try to link to libCrashReporterClient.a themselves,
64// only one crash info struct will be used.
Eric Christopher7d59a972010-06-28 18:33:48 +000065extern "C" {
Eric Christopher4411fbe2010-06-28 18:25:51 +000066CRASH_REPORTER_CLIENT_HIDDEN
67struct crashreporter_annotations_t gCRAnnotations
68 __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
Eric Christopherce4984f2011-10-05 05:00:26 +000069 = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
Eric Christopher7d59a972010-06-28 18:33:48 +000070}
Eric Christopher481d4022010-12-03 07:45:22 +000071#elif defined (__APPLE__) && HAVE_CRASHREPORTER_INFO
Daniel Dunbar4ef67302010-05-20 23:50:19 +000072static const char *__crashreporter_info__ = 0;
73asm(".desc ___crashreporter_info__, 0x10");
Chris Lattner49155ff2009-03-06 07:19:54 +000074#endif
75
76
77/// CrashHandler - This callback is run if a fatal signal is delivered to the
78/// process, it prints the pretty stack trace.
Eric Christopher371e5772010-08-08 00:00:34 +000079static void CrashHandler(void *) {
Chris Lattner49155ff2009-03-06 07:19:54 +000080#ifndef __APPLE__
81 // On non-apple systems, just emit the crash stack trace to stderr.
82 PrintCurStackTrace(errs());
83#else
84 // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
85 // put it into __crashreporter_info__.
86 SmallString<2048> TmpStr;
87 {
88 raw_svector_ostream Stream(TmpStr);
89 PrintCurStackTrace(Stream);
90 }
91
92 if (!TmpStr.empty()) {
Eric Christopher481d4022010-12-03 07:45:22 +000093#ifdef HAVE_CRASHREPORTERCLIENT_H
Eric Christopher371e5772010-08-08 00:00:34 +000094 // Cast to void to avoid warning.
95 (void)CRSetCrashLogMessage(std::string(TmpStr.str()).c_str());
Eric Christopher481d4022010-12-03 07:45:22 +000096#elif HAVE_CRASHREPORTER_INFO
97 __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str());
Eric Christopher654dee42010-06-22 21:01:04 +000098#endif
Daniel Dunbardddfd342009-08-19 20:07:03 +000099 errs() << TmpStr.str();
Chris Lattner49155ff2009-03-06 07:19:54 +0000100 }
101
102#endif
103}
104
Chris Lattnere97c7332009-03-04 21:40:23 +0000105static bool RegisterCrashPrinter() {
Chris Lattner90bb3f32009-07-16 06:17:45 +0000106 if (!DisablePrettyStackTrace)
107 sys::AddSignalHandler(CrashHandler, 0);
Chris Lattnere97c7332009-03-04 21:40:23 +0000108 return false;
109}
110
111PrettyStackTraceEntry::PrettyStackTraceEntry() {
112 // The first time this is called, we register the crash printer.
113 static bool HandlerRegistered = RegisterCrashPrinter();
Jeffrey Yasskin8e68c382010-12-23 00:58:24 +0000114 (void)HandlerRegistered;
Chris Lattnere97c7332009-03-04 21:40:23 +0000115
116 // Link ourselves.
Owen Anderson0b49c322009-06-25 23:31:33 +0000117 NextEntry = PrettyStackTraceHead.get();
118 PrettyStackTraceHead.set(this);
Chris Lattnere97c7332009-03-04 21:40:23 +0000119}
120
121PrettyStackTraceEntry::~PrettyStackTraceEntry() {
Owen Anderson0b49c322009-06-25 23:31:33 +0000122 assert(PrettyStackTraceHead.get() == this &&
Chris Lattnere97c7332009-03-04 21:40:23 +0000123 "Pretty stack trace entry destruction is out of order");
Owen Anderson0b49c322009-06-25 23:31:33 +0000124 PrettyStackTraceHead.set(getNextEntry());
Chris Lattnere97c7332009-03-04 21:40:23 +0000125}
126
127void PrettyStackTraceString::print(raw_ostream &OS) const {
128 OS << Str << "\n";
129}
130
131void PrettyStackTraceProgram::print(raw_ostream &OS) const {
Chris Lattnerfcba7cd2009-03-05 06:51:42 +0000132 OS << "Program arguments: ";
Chris Lattnere97c7332009-03-04 21:40:23 +0000133 // Print the argument list.
134 for (unsigned i = 0, e = ArgC; i != e; ++i)
135 OS << ArgV[i] << ' ';
136 OS << '\n';
Chris Lattnerfcba7cd2009-03-05 06:51:42 +0000137}