blob: 05b3e31644bdb85886fcdae2725ef69f86fdda63 [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"
Eric Christophera6b96002015-12-18 01:46:52 +000016#include "llvm-c/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/SmallString.h"
18#include "llvm/Config/config.h" // Get autoconf configuration settings
Chandler Carruthfb3139a2015-01-29 01:23:04 +000019#include "llvm/Support/Compiler.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000020#include "llvm/Support/Signals.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"
Eric Christophere9c1bb62010-06-22 21:01:04 +000023
24#ifdef HAVE_CRASHREPORTERCLIENT_H
25#include <CrashReporterClient.h>
26#endif
27
Chris Lattner68061d52009-03-04 21:40:23 +000028using namespace llvm;
29
Owen Anderson9253bb92015-01-29 07:35:31 +000030// If backtrace support is not enabled, compile out support for pretty stack
31// traces. This has the secondary effect of not requiring thread local storage
32// when backtrace support is disabled.
Owen Andersonc4d245c2015-01-29 07:53:13 +000033#if defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES)
Owen Anderson9253bb92015-01-29 07:35:31 +000034
Chandler Carruth16b670e2015-01-28 09:52:14 +000035// We need a thread local pointer to manage the stack of our stack trace
36// objects, but we *really* cannot tolerate destructors running and do not want
37// to pay any overhead of synchronizing. As a consequence, we use a raw
Chandler Carruthfb3139a2015-01-29 01:23:04 +000038// thread-local variable.
Chandler Carruthbe09eb72015-01-28 19:29:22 +000039static LLVM_THREAD_LOCAL const PrettyStackTraceEntry *PrettyStackTraceHead =
40 nullptr;
Chris Lattner68061d52009-03-04 21:40:23 +000041
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000042static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){
43 unsigned NextID = 0;
44 if (Entry->getNextEntry())
45 NextID = PrintStack(Entry->getNextEntry(), OS);
46 OS << NextID << ".\t";
Nick Lewycky4e06def2013-03-26 01:27:52 +000047 {
48 sys::Watchdog W(5);
49 Entry->print(OS);
50 }
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000051
52 return NextID+1;
53}
54
Chris Lattnere41a4342009-03-06 07:19:54 +000055/// PrintCurStackTrace - Print the current stack trace to the specified stream.
56static void PrintCurStackTrace(raw_ostream &OS) {
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000057 // Don't print an empty trace.
Chandler Carruth16b670e2015-01-28 09:52:14 +000058 if (!PrettyStackTraceHead) return;
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000059
Chris Lattner68061d52009-03-04 21:40:23 +000060 // If there are pretty stack frames registered, walk and emit them.
Chris Lattner68061d52009-03-04 21:40:23 +000061 OS << "Stack dump:\n";
62
Chandler Carruth16b670e2015-01-28 09:52:14 +000063 PrintStack(PrettyStackTraceHead, OS);
Chris Lattner68061d52009-03-04 21:40:23 +000064 OS.flush();
65}
66
Eric Christopher51f290832010-06-28 18:25:51 +000067// Integrate with crash reporter libraries.
Eric Christopherca466732010-12-03 07:45:22 +000068#if defined (__APPLE__) && HAVE_CRASHREPORTERCLIENT_H
Eric Christopher51f290832010-06-28 18:25:51 +000069// If any clients of llvm try to link to libCrashReporterClient.a themselves,
70// only one crash info struct will be used.
Eric Christopher7f103a22010-06-28 18:33:48 +000071extern "C" {
Eric Christopher51f290832010-06-28 18:25:51 +000072CRASH_REPORTER_CLIENT_HIDDEN
73struct crashreporter_annotations_t gCRAnnotations
74 __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
Eric Christopherc4b9b522011-10-05 05:00:26 +000075 = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
Eric Christopher7f103a22010-06-28 18:33:48 +000076}
Eric Christopherca466732010-12-03 07:45:22 +000077#elif defined (__APPLE__) && HAVE_CRASHREPORTER_INFO
Daniel Dunbarc120ffe2010-05-20 23:50:19 +000078static const char *__crashreporter_info__ = 0;
79asm(".desc ___crashreporter_info__, 0x10");
Chris Lattnere41a4342009-03-06 07:19:54 +000080#endif
81
82
83/// CrashHandler - This callback is run if a fatal signal is delivered to the
84/// process, it prints the pretty stack trace.
Eric Christopher87947f72010-08-08 00:00:34 +000085static void CrashHandler(void *) {
Chris Lattnere41a4342009-03-06 07:19:54 +000086#ifndef __APPLE__
87 // On non-apple systems, just emit the crash stack trace to stderr.
88 PrintCurStackTrace(errs());
89#else
90 // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
91 // put it into __crashreporter_info__.
92 SmallString<2048> TmpStr;
93 {
94 raw_svector_ostream Stream(TmpStr);
95 PrintCurStackTrace(Stream);
96 }
97
98 if (!TmpStr.empty()) {
Eric Christopherca466732010-12-03 07:45:22 +000099#ifdef HAVE_CRASHREPORTERCLIENT_H
Eric Christopher87947f72010-08-08 00:00:34 +0000100 // Cast to void to avoid warning.
101 (void)CRSetCrashLogMessage(std::string(TmpStr.str()).c_str());
Eric Christopherca466732010-12-03 07:45:22 +0000102#elif HAVE_CRASHREPORTER_INFO
103 __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str());
Eric Christophere9c1bb62010-06-22 21:01:04 +0000104#endif
Daniel Dunbar8b0b1152009-08-19 20:07:03 +0000105 errs() << TmpStr.str();
Chris Lattnere41a4342009-03-06 07:19:54 +0000106 }
107
108#endif
109}
110
Owen Andersonc4d245c2015-01-29 07:53:13 +0000111// defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES)
Owen Anderson9253bb92015-01-29 07:35:31 +0000112#endif
113
Chris Lattner68061d52009-03-04 21:40:23 +0000114PrettyStackTraceEntry::PrettyStackTraceEntry() {
Owen Andersonc4d245c2015-01-29 07:53:13 +0000115#if defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES)
Chris Lattner68061d52009-03-04 21:40:23 +0000116 // Link ourselves.
Chandler Carruth16b670e2015-01-28 09:52:14 +0000117 NextEntry = PrettyStackTraceHead;
118 PrettyStackTraceHead = this;
Owen Anderson9253bb92015-01-29 07:35:31 +0000119#endif
Chris Lattner68061d52009-03-04 21:40:23 +0000120}
121
122PrettyStackTraceEntry::~PrettyStackTraceEntry() {
Owen Andersonc4d245c2015-01-29 07:53:13 +0000123#if defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES)
Chandler Carruth16b670e2015-01-28 09:52:14 +0000124 assert(PrettyStackTraceHead == this &&
Chris Lattner68061d52009-03-04 21:40:23 +0000125 "Pretty stack trace entry destruction is out of order");
Chandler Carruth16b670e2015-01-28 09:52:14 +0000126 PrettyStackTraceHead = getNextEntry();
Owen Anderson9253bb92015-01-29 07:35:31 +0000127#endif
Chris Lattner68061d52009-03-04 21:40:23 +0000128}
129
130void PrettyStackTraceString::print(raw_ostream &OS) const {
131 OS << Str << "\n";
132}
133
134void PrettyStackTraceProgram::print(raw_ostream &OS) const {
Chris Lattner2674eb42009-03-05 06:51:42 +0000135 OS << "Program arguments: ";
Chris Lattner68061d52009-03-04 21:40:23 +0000136 // Print the argument list.
137 for (unsigned i = 0, e = ArgC; i != e; ++i)
138 OS << ArgV[i] << ' ';
139 OS << '\n';
Chris Lattner2674eb42009-03-05 06:51:42 +0000140}
Filip Pizlo9f50ccd2013-11-03 00:29:47 +0000141
Owen Andersonc4d245c2015-01-29 07:53:13 +0000142#if defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES)
Filip Pizloc10ca902013-11-04 02:22:25 +0000143static bool RegisterCrashPrinter() {
Craig Topperc10719f2014-04-07 04:17:22 +0000144 sys::AddSignalHandler(CrashHandler, nullptr);
Filip Pizloc10ca902013-11-04 02:22:25 +0000145 return false;
146}
Owen Anderson9253bb92015-01-29 07:35:31 +0000147#endif
Filip Pizloc10ca902013-11-04 02:22:25 +0000148
149void llvm::EnablePrettyStackTrace() {
Owen Andersonc4d245c2015-01-29 07:53:13 +0000150#if defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES)
Filip Pizloc10ca902013-11-04 02:22:25 +0000151 // The first time this is called, we register the crash printer.
152 static bool HandlerRegistered = RegisterCrashPrinter();
153 (void)HandlerRegistered;
Owen Anderson9253bb92015-01-29 07:35:31 +0000154#endif
Filip Pizloc10ca902013-11-04 02:22:25 +0000155}
156
Nico Weberaf3f2422015-08-07 17:47:03 +0000157const void* llvm::SavePrettyStackState() {
158#if defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES)
159 return PrettyStackTraceHead;
160#else
161 return nullptr;
162#endif
163}
164
165void llvm::RestorePrettyStackState(const void* Top) {
166#if defined(HAVE_BACKTRACE) && defined(ENABLE_BACKTRACES)
167 PrettyStackTraceHead = (const PrettyStackTraceEntry*)Top;
168#endif
169}
170
Filip Pizloc10ca902013-11-04 02:22:25 +0000171void LLVMEnablePrettyStackTrace() {
172 EnablePrettyStackTrace();
Filip Pizlo9f50ccd2013-11-03 00:29:47 +0000173}