blob: 3c8a10849d149b1ee0d8ca2665dff9b3b790c501 [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
Eric Christopher654dee42010-06-22 21:01:04 +000015#include "llvm/Config/config.h" // Get autoconf configuration settings
Chris Lattnere97c7332009-03-04 21:40:23 +000016#include "llvm/Support/PrettyStackTrace.h"
17#include "llvm/Support/raw_ostream.h"
18#include "llvm/System/Signals.h"
Owen Anderson0b49c322009-06-25 23:31:33 +000019#include "llvm/System/ThreadLocal.h"
Chris Lattner49155ff2009-03-06 07:19:54 +000020#include "llvm/ADT/SmallString.h"
Eric Christopher654dee42010-06-22 21:01:04 +000021
22#ifdef HAVE_CRASHREPORTERCLIENT_H
23#include <CrashReporterClient.h>
24#endif
25
Chris Lattnere97c7332009-03-04 21:40:23 +000026using namespace llvm;
27
Chris Lattner90bb3f32009-07-16 06:17:45 +000028namespace llvm {
29 bool DisablePrettyStackTrace = false;
30}
31
Chris Lattnere97c7332009-03-04 21:40:23 +000032// FIXME: This should be thread local when llvm supports threads.
Owen Anderson0b49c322009-06-25 23:31:33 +000033static sys::ThreadLocal<const PrettyStackTraceEntry> PrettyStackTraceHead;
Chris Lattnere97c7332009-03-04 21:40:23 +000034
Chris Lattnerd56786f2009-03-05 07:03:49 +000035static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){
36 unsigned NextID = 0;
37 if (Entry->getNextEntry())
38 NextID = PrintStack(Entry->getNextEntry(), OS);
39 OS << NextID << ".\t";
40 Entry->print(OS);
41
42 return NextID+1;
43}
44
Chris Lattner49155ff2009-03-06 07:19:54 +000045/// PrintCurStackTrace - Print the current stack trace to the specified stream.
46static void PrintCurStackTrace(raw_ostream &OS) {
Chris Lattnerd56786f2009-03-05 07:03:49 +000047 // Don't print an empty trace.
Owen Anderson0b49c322009-06-25 23:31:33 +000048 if (PrettyStackTraceHead.get() == 0) return;
Chris Lattnerd56786f2009-03-05 07:03:49 +000049
Chris Lattnere97c7332009-03-04 21:40:23 +000050 // If there are pretty stack frames registered, walk and emit them.
Chris Lattnere97c7332009-03-04 21:40:23 +000051 OS << "Stack dump:\n";
52
Owen Anderson0b49c322009-06-25 23:31:33 +000053 PrintStack(PrettyStackTraceHead.get(), OS);
Chris Lattnere97c7332009-03-04 21:40:23 +000054 OS.flush();
55}
56
Eric Christopher4411fbe2010-06-28 18:25:51 +000057// Integrate with crash reporter libraries.
58#if defined (__APPLE__) && defined (HAVE_CRASHREPORTERCLIENT_H)
59// If any clients of llvm try to link to libCrashReporterClient.a themselves,
60// only one crash info struct will be used.
Eric Christopher7d59a972010-06-28 18:33:48 +000061extern "C" {
Eric Christopher4411fbe2010-06-28 18:25:51 +000062CRASH_REPORTER_CLIENT_HIDDEN
63struct crashreporter_annotations_t gCRAnnotations
64 __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
Eric Christopher7d59a972010-06-28 18:33:48 +000065 = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0 };
66}
67#elif defined (__APPLE__)
Daniel Dunbar4ef67302010-05-20 23:50:19 +000068static const char *__crashreporter_info__ = 0;
69asm(".desc ___crashreporter_info__, 0x10");
Chris Lattner49155ff2009-03-06 07:19:54 +000070#endif
71
72
73/// CrashHandler - This callback is run if a fatal signal is delivered to the
74/// process, it prints the pretty stack trace.
Eric Christopher371e5772010-08-08 00:00:34 +000075static void CrashHandler(void *) {
Chris Lattner49155ff2009-03-06 07:19:54 +000076#ifndef __APPLE__
77 // On non-apple systems, just emit the crash stack trace to stderr.
78 PrintCurStackTrace(errs());
79#else
80 // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
81 // put it into __crashreporter_info__.
82 SmallString<2048> TmpStr;
83 {
84 raw_svector_ostream Stream(TmpStr);
85 PrintCurStackTrace(Stream);
86 }
87
88 if (!TmpStr.empty()) {
Eric Christopher654dee42010-06-22 21:01:04 +000089#ifndef HAVE_CRASHREPORTERCLIENT_H
Daniel Dunbardddfd342009-08-19 20:07:03 +000090 __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str());
Eric Christopher654dee42010-06-22 21:01:04 +000091#else
Eric Christopher371e5772010-08-08 00:00:34 +000092 // Cast to void to avoid warning.
93 (void)CRSetCrashLogMessage(std::string(TmpStr.str()).c_str());
Eric Christopher654dee42010-06-22 21:01:04 +000094#endif
Daniel Dunbardddfd342009-08-19 20:07:03 +000095 errs() << TmpStr.str();
Chris Lattner49155ff2009-03-06 07:19:54 +000096 }
97
98#endif
99}
100
Chris Lattnere97c7332009-03-04 21:40:23 +0000101static bool RegisterCrashPrinter() {
Chris Lattner90bb3f32009-07-16 06:17:45 +0000102 if (!DisablePrettyStackTrace)
103 sys::AddSignalHandler(CrashHandler, 0);
Chris Lattnere97c7332009-03-04 21:40:23 +0000104 return false;
105}
106
107PrettyStackTraceEntry::PrettyStackTraceEntry() {
108 // The first time this is called, we register the crash printer.
109 static bool HandlerRegistered = RegisterCrashPrinter();
110 HandlerRegistered = HandlerRegistered;
111
112 // Link ourselves.
Owen Anderson0b49c322009-06-25 23:31:33 +0000113 NextEntry = PrettyStackTraceHead.get();
114 PrettyStackTraceHead.set(this);
Chris Lattnere97c7332009-03-04 21:40:23 +0000115}
116
117PrettyStackTraceEntry::~PrettyStackTraceEntry() {
Owen Anderson0b49c322009-06-25 23:31:33 +0000118 assert(PrettyStackTraceHead.get() == this &&
Chris Lattnere97c7332009-03-04 21:40:23 +0000119 "Pretty stack trace entry destruction is out of order");
Owen Anderson0b49c322009-06-25 23:31:33 +0000120 PrettyStackTraceHead.set(getNextEntry());
Chris Lattnere97c7332009-03-04 21:40:23 +0000121}
122
123void PrettyStackTraceString::print(raw_ostream &OS) const {
124 OS << Str << "\n";
125}
126
127void PrettyStackTraceProgram::print(raw_ostream &OS) const {
Chris Lattnerfcba7cd2009-03-05 06:51:42 +0000128 OS << "Program arguments: ";
Chris Lattnere97c7332009-03-04 21:40:23 +0000129 // Print the argument list.
130 for (unsigned i = 0, e = ArgC; i != e; ++i)
131 OS << ArgV[i] << ' ';
132 OS << '\n';
Chris Lattnerfcba7cd2009-03-05 06:51:42 +0000133}
134