blob: 23ee5ab105aec8316f598e42b261f2a4d4297bd2 [file] [log] [blame]
Chris Lattner68061d52009-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 Lattner0ab5e2c2011-04-15 05:18:47 +000011// Unix signals occurring while your program is running.
Chris Lattner68061d52009-03-04 21:40:23 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Support/PrettyStackTrace.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/SmallString.h"
17#include "llvm/Config/config.h" // Get autoconf configuration settings
Michael J. Spencer447762d2010-11-29 18:16:10 +000018#include "llvm/Support/Signals.h"
19#include "llvm/Support/ThreadLocal.h"
Nick Lewycky4e06def2013-03-26 01:27:52 +000020#include "llvm/Support/Watchdog.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/Support/raw_ostream.h"
Eric Christophere9c1bb62010-06-22 21:01:04 +000022
23#ifdef HAVE_CRASHREPORTERCLIENT_H
24#include <CrashReporterClient.h>
25#endif
26
Chris Lattner68061d52009-03-04 21:40:23 +000027using namespace llvm;
28
Chris Lattner5d485f72009-07-16 06:17:45 +000029namespace llvm {
30 bool DisablePrettyStackTrace = false;
31}
32
Chris Lattner68061d52009-03-04 21:40:23 +000033// FIXME: This should be thread local when llvm supports threads.
Owen Anderson76cfd202009-06-25 23:31:33 +000034static sys::ThreadLocal<const PrettyStackTraceEntry> PrettyStackTraceHead;
Chris Lattner68061d52009-03-04 21:40:23 +000035
Chris Lattner8d0fe8c2009-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 Lewycky4e06def2013-03-26 01:27:52 +000041 {
42 sys::Watchdog W(5);
43 Entry->print(OS);
44 }
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000045
46 return NextID+1;
47}
48
Chris Lattnere41a4342009-03-06 07:19:54 +000049/// PrintCurStackTrace - Print the current stack trace to the specified stream.
50static void PrintCurStackTrace(raw_ostream &OS) {
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000051 // Don't print an empty trace.
Owen Anderson76cfd202009-06-25 23:31:33 +000052 if (PrettyStackTraceHead.get() == 0) return;
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000053
Chris Lattner68061d52009-03-04 21:40:23 +000054 // If there are pretty stack frames registered, walk and emit them.
Chris Lattner68061d52009-03-04 21:40:23 +000055 OS << "Stack dump:\n";
56
Owen Anderson76cfd202009-06-25 23:31:33 +000057 PrintStack(PrettyStackTraceHead.get(), OS);
Chris Lattner68061d52009-03-04 21:40:23 +000058 OS.flush();
59}
60
Eric Christopher51f290832010-06-28 18:25:51 +000061// Integrate with crash reporter libraries.
Eric Christopherca466732010-12-03 07:45:22 +000062#if defined (__APPLE__) && HAVE_CRASHREPORTERCLIENT_H
Eric Christopher51f290832010-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 Christopher7f103a22010-06-28 18:33:48 +000065extern "C" {
Eric Christopher51f290832010-06-28 18:25:51 +000066CRASH_REPORTER_CLIENT_HIDDEN
67struct crashreporter_annotations_t gCRAnnotations
68 __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
Eric Christopherc4b9b522011-10-05 05:00:26 +000069 = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
Eric Christopher7f103a22010-06-28 18:33:48 +000070}
Eric Christopherca466732010-12-03 07:45:22 +000071#elif defined (__APPLE__) && HAVE_CRASHREPORTER_INFO
Daniel Dunbarc120ffe2010-05-20 23:50:19 +000072static const char *__crashreporter_info__ = 0;
73asm(".desc ___crashreporter_info__, 0x10");
Chris Lattnere41a4342009-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 Christopher87947f72010-08-08 00:00:34 +000079static void CrashHandler(void *) {
Chris Lattnere41a4342009-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 Christopherca466732010-12-03 07:45:22 +000093#ifdef HAVE_CRASHREPORTERCLIENT_H
Eric Christopher87947f72010-08-08 00:00:34 +000094 // Cast to void to avoid warning.
95 (void)CRSetCrashLogMessage(std::string(TmpStr.str()).c_str());
Eric Christopherca466732010-12-03 07:45:22 +000096#elif HAVE_CRASHREPORTER_INFO
97 __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str());
Eric Christophere9c1bb62010-06-22 21:01:04 +000098#endif
Daniel Dunbar8b0b1152009-08-19 20:07:03 +000099 errs() << TmpStr.str();
Chris Lattnere41a4342009-03-06 07:19:54 +0000100 }
101
102#endif
103}
104
Chris Lattner68061d52009-03-04 21:40:23 +0000105static bool RegisterCrashPrinter() {
Chris Lattner5d485f72009-07-16 06:17:45 +0000106 if (!DisablePrettyStackTrace)
107 sys::AddSignalHandler(CrashHandler, 0);
Chris Lattner68061d52009-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 Yasskin9b43f332010-12-23 00:58:24 +0000114 (void)HandlerRegistered;
Chris Lattner68061d52009-03-04 21:40:23 +0000115
116 // Link ourselves.
Owen Anderson76cfd202009-06-25 23:31:33 +0000117 NextEntry = PrettyStackTraceHead.get();
118 PrettyStackTraceHead.set(this);
Chris Lattner68061d52009-03-04 21:40:23 +0000119}
120
121PrettyStackTraceEntry::~PrettyStackTraceEntry() {
Owen Anderson76cfd202009-06-25 23:31:33 +0000122 assert(PrettyStackTraceHead.get() == this &&
Chris Lattner68061d52009-03-04 21:40:23 +0000123 "Pretty stack trace entry destruction is out of order");
Owen Anderson76cfd202009-06-25 23:31:33 +0000124 PrettyStackTraceHead.set(getNextEntry());
Chris Lattner68061d52009-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 Lattner2674eb42009-03-05 06:51:42 +0000132 OS << "Program arguments: ";
Chris Lattner68061d52009-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 Lattner2674eb42009-03-05 06:51:42 +0000137}