blob: d08abc8ce155c2eb227184743b1d5ed52bd9350e [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
Filip Pizlo67d97092013-09-13 22:59:47 +000018#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000019#include "llvm/Support/Signals.h"
20#include "llvm/Support/ThreadLocal.h"
Nick Lewycky4e06def2013-03-26 01:27:52 +000021#include "llvm/Support/Watchdog.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Support/raw_ostream.h"
Filip Pizlo9f50ccd2013-11-03 00:29:47 +000023#include "llvm-c/Core.h"
Eric Christophere9c1bb62010-06-22 21:01:04 +000024
25#ifdef HAVE_CRASHREPORTERCLIENT_H
26#include <CrashReporterClient.h>
27#endif
28
Chris Lattner68061d52009-03-04 21:40:23 +000029using namespace llvm;
30
Chris Lattner5d485f72009-07-16 06:17:45 +000031namespace llvm {
32 bool DisablePrettyStackTrace = false;
33}
34
Filip Pizlo67d97092013-09-13 22:59:47 +000035static ManagedStatic<sys::ThreadLocal<const PrettyStackTraceEntry> > PrettyStackTraceHead;
Chris Lattner68061d52009-03-04 21:40:23 +000036
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000037static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){
38 unsigned NextID = 0;
39 if (Entry->getNextEntry())
40 NextID = PrintStack(Entry->getNextEntry(), OS);
41 OS << NextID << ".\t";
Nick Lewycky4e06def2013-03-26 01:27:52 +000042 {
43 sys::Watchdog W(5);
44 Entry->print(OS);
45 }
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000046
47 return NextID+1;
48}
49
Chris Lattnere41a4342009-03-06 07:19:54 +000050/// PrintCurStackTrace - Print the current stack trace to the specified stream.
51static void PrintCurStackTrace(raw_ostream &OS) {
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000052 // Don't print an empty trace.
Filip Pizlo67d97092013-09-13 22:59:47 +000053 if (PrettyStackTraceHead->get() == 0) return;
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000054
Chris Lattner68061d52009-03-04 21:40:23 +000055 // If there are pretty stack frames registered, walk and emit them.
Chris Lattner68061d52009-03-04 21:40:23 +000056 OS << "Stack dump:\n";
57
Filip Pizlo67d97092013-09-13 22:59:47 +000058 PrintStack(PrettyStackTraceHead->get(), OS);
Chris Lattner68061d52009-03-04 21:40:23 +000059 OS.flush();
60}
61
Eric Christopher51f290832010-06-28 18:25:51 +000062// Integrate with crash reporter libraries.
Eric Christopherca466732010-12-03 07:45:22 +000063#if defined (__APPLE__) && HAVE_CRASHREPORTERCLIENT_H
Eric Christopher51f290832010-06-28 18:25:51 +000064// If any clients of llvm try to link to libCrashReporterClient.a themselves,
65// only one crash info struct will be used.
Eric Christopher7f103a22010-06-28 18:33:48 +000066extern "C" {
Eric Christopher51f290832010-06-28 18:25:51 +000067CRASH_REPORTER_CLIENT_HIDDEN
68struct crashreporter_annotations_t gCRAnnotations
69 __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
Eric Christopherc4b9b522011-10-05 05:00:26 +000070 = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
Eric Christopher7f103a22010-06-28 18:33:48 +000071}
Eric Christopherca466732010-12-03 07:45:22 +000072#elif defined (__APPLE__) && HAVE_CRASHREPORTER_INFO
Daniel Dunbarc120ffe2010-05-20 23:50:19 +000073static const char *__crashreporter_info__ = 0;
74asm(".desc ___crashreporter_info__, 0x10");
Chris Lattnere41a4342009-03-06 07:19:54 +000075#endif
76
77
78/// CrashHandler - This callback is run if a fatal signal is delivered to the
79/// process, it prints the pretty stack trace.
Eric Christopher87947f72010-08-08 00:00:34 +000080static void CrashHandler(void *) {
Chris Lattnere41a4342009-03-06 07:19:54 +000081#ifndef __APPLE__
82 // On non-apple systems, just emit the crash stack trace to stderr.
83 PrintCurStackTrace(errs());
84#else
85 // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
86 // put it into __crashreporter_info__.
87 SmallString<2048> TmpStr;
88 {
89 raw_svector_ostream Stream(TmpStr);
90 PrintCurStackTrace(Stream);
91 }
92
93 if (!TmpStr.empty()) {
Eric Christopherca466732010-12-03 07:45:22 +000094#ifdef HAVE_CRASHREPORTERCLIENT_H
Eric Christopher87947f72010-08-08 00:00:34 +000095 // Cast to void to avoid warning.
96 (void)CRSetCrashLogMessage(std::string(TmpStr.str()).c_str());
Eric Christopherca466732010-12-03 07:45:22 +000097#elif HAVE_CRASHREPORTER_INFO
98 __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str());
Eric Christophere9c1bb62010-06-22 21:01:04 +000099#endif
Daniel Dunbar8b0b1152009-08-19 20:07:03 +0000100 errs() << TmpStr.str();
Chris Lattnere41a4342009-03-06 07:19:54 +0000101 }
102
103#endif
104}
105
Chris Lattner68061d52009-03-04 21:40:23 +0000106static bool RegisterCrashPrinter() {
Chris Lattner5d485f72009-07-16 06:17:45 +0000107 if (!DisablePrettyStackTrace)
108 sys::AddSignalHandler(CrashHandler, 0);
Chris Lattner68061d52009-03-04 21:40:23 +0000109 return false;
110}
111
112PrettyStackTraceEntry::PrettyStackTraceEntry() {
113 // The first time this is called, we register the crash printer.
114 static bool HandlerRegistered = RegisterCrashPrinter();
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000115 (void)HandlerRegistered;
Chris Lattner68061d52009-03-04 21:40:23 +0000116
117 // Link ourselves.
Filip Pizlo67d97092013-09-13 22:59:47 +0000118 NextEntry = PrettyStackTraceHead->get();
119 PrettyStackTraceHead->set(this);
Chris Lattner68061d52009-03-04 21:40:23 +0000120}
121
122PrettyStackTraceEntry::~PrettyStackTraceEntry() {
Filip Pizlo67d97092013-09-13 22:59:47 +0000123 // Do nothing if PrettyStackTraceHead is uninitialized. This can only happen
124 // if a shutdown occurred after we created the PrettyStackTraceEntry. That
125 // does occur in the following idiom:
126 //
127 // PrettyStackTraceProgram X(...);
128 // llvm_shutdown_obj Y;
129 //
130 // Without this check, we may end up removing ourselves from the stack trace
131 // after PrettyStackTraceHead has already been destroyed.
132 if (!PrettyStackTraceHead.isConstructed())
133 return;
134
135 assert(PrettyStackTraceHead->get() == this &&
Chris Lattner68061d52009-03-04 21:40:23 +0000136 "Pretty stack trace entry destruction is out of order");
Filip Pizlo67d97092013-09-13 22:59:47 +0000137 PrettyStackTraceHead->set(getNextEntry());
Chris Lattner68061d52009-03-04 21:40:23 +0000138}
139
140void PrettyStackTraceString::print(raw_ostream &OS) const {
141 OS << Str << "\n";
142}
143
144void PrettyStackTraceProgram::print(raw_ostream &OS) const {
Chris Lattner2674eb42009-03-05 06:51:42 +0000145 OS << "Program arguments: ";
Chris Lattner68061d52009-03-04 21:40:23 +0000146 // Print the argument list.
147 for (unsigned i = 0, e = ArgC; i != e; ++i)
148 OS << ArgV[i] << ' ';
149 OS << '\n';
Chris Lattner2674eb42009-03-05 06:51:42 +0000150}
Filip Pizlo9f50ccd2013-11-03 00:29:47 +0000151
152void LLVMDisablePrettyStackTrace() {
153 DisablePrettyStackTrace = true;
154}