blob: 5b079ff211feadb92776525e04ac6331004dae56 [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
Sean Callanan62204ad2016-12-14 19:19:53 +000024#include <cstdarg>
Richard Smith3de58a52016-05-26 20:21:55 +000025#include <tuple>
26
Eric Christophere9c1bb62010-06-22 21:01:04 +000027#ifdef HAVE_CRASHREPORTERCLIENT_H
28#include <CrashReporterClient.h>
29#endif
30
Chris Lattner68061d52009-03-04 21:40:23 +000031using namespace llvm;
32
Owen Anderson9253bb92015-01-29 07:35:31 +000033// If backtrace support is not enabled, compile out support for pretty stack
34// traces. This has the secondary effect of not requiring thread local storage
35// when backtrace support is disabled.
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +000036#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Owen Anderson9253bb92015-01-29 07:35:31 +000037
Chandler Carruth16b670e2015-01-28 09:52:14 +000038// We need a thread local pointer to manage the stack of our stack trace
39// objects, but we *really* cannot tolerate destructors running and do not want
40// to pay any overhead of synchronizing. As a consequence, we use a raw
Chandler Carruthfb3139a2015-01-29 01:23:04 +000041// thread-local variable.
Richard Smith3de58a52016-05-26 20:21:55 +000042static LLVM_THREAD_LOCAL PrettyStackTraceEntry *PrettyStackTraceHead = nullptr;
Chris Lattner68061d52009-03-04 21:40:23 +000043
Richard Smith3de58a52016-05-26 20:21:55 +000044namespace llvm {
45PrettyStackTraceEntry *ReverseStackTrace(PrettyStackTraceEntry *Head) {
46 PrettyStackTraceEntry *Prev = nullptr;
47 while (Head)
48 std::tie(Prev, Head, Head->NextEntry) =
49 std::make_tuple(Head, Head->NextEntry, Prev);
50 return Prev;
51}
52}
53
54static void PrintStack(raw_ostream &OS) {
55 // Print out the stack in reverse order. To avoid recursion (which is likely
56 // to fail if we crashed due to stack overflow), we do an up-front pass to
57 // reverse the stack, then print it, then reverse it again.
58 unsigned ID = 0;
59 PrettyStackTraceEntry *ReversedStack =
60 llvm::ReverseStackTrace(PrettyStackTraceHead);
61 for (const PrettyStackTraceEntry *Entry = ReversedStack; Entry;
62 Entry = Entry->getNextEntry()) {
63 OS << ID++ << ".\t";
Nick Lewycky4e06def2013-03-26 01:27:52 +000064 sys::Watchdog W(5);
65 Entry->print(OS);
66 }
Richard Smith3de58a52016-05-26 20:21:55 +000067 llvm::ReverseStackTrace(ReversedStack);
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000068}
69
Chris Lattnere41a4342009-03-06 07:19:54 +000070/// PrintCurStackTrace - Print the current stack trace to the specified stream.
71static void PrintCurStackTrace(raw_ostream &OS) {
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000072 // Don't print an empty trace.
Chandler Carruth16b670e2015-01-28 09:52:14 +000073 if (!PrettyStackTraceHead) return;
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000074
Chris Lattner68061d52009-03-04 21:40:23 +000075 // If there are pretty stack frames registered, walk and emit them.
Chris Lattner68061d52009-03-04 21:40:23 +000076 OS << "Stack dump:\n";
77
Richard Smith3de58a52016-05-26 20:21:55 +000078 PrintStack(OS);
Chris Lattner68061d52009-03-04 21:40:23 +000079 OS.flush();
80}
81
Eric Christopher51f290832010-06-28 18:25:51 +000082// Integrate with crash reporter libraries.
Chris Bienemanbfff2542016-12-07 19:28:22 +000083#if defined (__APPLE__) && defined(HAVE_CRASHREPORTERCLIENT_H)
Eric Christopher51f290832010-06-28 18:25:51 +000084// If any clients of llvm try to link to libCrashReporterClient.a themselves,
85// only one crash info struct will be used.
Eric Christopher7f103a22010-06-28 18:33:48 +000086extern "C" {
Eric Christopher51f290832010-06-28 18:25:51 +000087CRASH_REPORTER_CLIENT_HIDDEN
88struct crashreporter_annotations_t gCRAnnotations
89 __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
Eric Christopherc4b9b522011-10-05 05:00:26 +000090 = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
Eric Christopher7f103a22010-06-28 18:33:48 +000091}
Sean Callanan032dbf92016-12-14 19:09:43 +000092#elif defined(__APPLE__) && HAVE_CRASHREPORTER_INFO
93extern "C" const char *__crashreporter_info__
94 __attribute__((visibility("hidden"))) = 0;
Daniel Dunbarc120ffe2010-05-20 23:50:19 +000095asm(".desc ___crashreporter_info__, 0x10");
Chris Lattnere41a4342009-03-06 07:19:54 +000096#endif
97
Chris Lattnere41a4342009-03-06 07:19:54 +000098/// CrashHandler - This callback is run if a fatal signal is delivered to the
99/// process, it prints the pretty stack trace.
Eric Christopher87947f72010-08-08 00:00:34 +0000100static void CrashHandler(void *) {
Chris Lattnere41a4342009-03-06 07:19:54 +0000101#ifndef __APPLE__
102 // On non-apple systems, just emit the crash stack trace to stderr.
103 PrintCurStackTrace(errs());
104#else
105 // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
106 // put it into __crashreporter_info__.
107 SmallString<2048> TmpStr;
108 {
109 raw_svector_ostream Stream(TmpStr);
110 PrintCurStackTrace(Stream);
111 }
112
113 if (!TmpStr.empty()) {
Eric Christopherca466732010-12-03 07:45:22 +0000114#ifdef HAVE_CRASHREPORTERCLIENT_H
Eric Christopher87947f72010-08-08 00:00:34 +0000115 // Cast to void to avoid warning.
116 (void)CRSetCrashLogMessage(std::string(TmpStr.str()).c_str());
Eric Christopherca466732010-12-03 07:45:22 +0000117#elif HAVE_CRASHREPORTER_INFO
118 __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str());
Eric Christophere9c1bb62010-06-22 21:01:04 +0000119#endif
Daniel Dunbar8b0b1152009-08-19 20:07:03 +0000120 errs() << TmpStr.str();
Chris Lattnere41a4342009-03-06 07:19:54 +0000121 }
122
123#endif
124}
125
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000126// defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Owen Anderson9253bb92015-01-29 07:35:31 +0000127#endif
128
Chris Lattner68061d52009-03-04 21:40:23 +0000129PrettyStackTraceEntry::PrettyStackTraceEntry() {
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000130#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Chris Lattner68061d52009-03-04 21:40:23 +0000131 // Link ourselves.
Chandler Carruth16b670e2015-01-28 09:52:14 +0000132 NextEntry = PrettyStackTraceHead;
133 PrettyStackTraceHead = this;
Owen Anderson9253bb92015-01-29 07:35:31 +0000134#endif
Chris Lattner68061d52009-03-04 21:40:23 +0000135}
136
137PrettyStackTraceEntry::~PrettyStackTraceEntry() {
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000138#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Chandler Carruth16b670e2015-01-28 09:52:14 +0000139 assert(PrettyStackTraceHead == this &&
Chris Lattner68061d52009-03-04 21:40:23 +0000140 "Pretty stack trace entry destruction is out of order");
Richard Smith3de58a52016-05-26 20:21:55 +0000141 PrettyStackTraceHead = NextEntry;
Owen Anderson9253bb92015-01-29 07:35:31 +0000142#endif
Chris Lattner68061d52009-03-04 21:40:23 +0000143}
144
Sean Callanan032dbf92016-12-14 19:09:43 +0000145void PrettyStackTraceString::print(raw_ostream &OS) const { OS << Str << "\n"; }
146
147PrettyStackTraceFormat::PrettyStackTraceFormat(const char *Format, ...) {
148 va_list AP;
149 va_start(AP, Format);
150 const int SizeOrError = vsnprintf(nullptr, 0, Format, AP);
151 va_end(AP);
152 if (SizeOrError < 0) {
153 return;
154 }
155
156 const int Size = SizeOrError + 1; // '\0'
157 Str.resize(Size);
158 va_start(AP, Format);
159 vsnprintf(Str.data(), Size, Format, AP);
160 va_end(AP);
Chris Lattner68061d52009-03-04 21:40:23 +0000161}
162
Sean Callanan032dbf92016-12-14 19:09:43 +0000163void PrettyStackTraceFormat::print(raw_ostream &OS) const { OS << Str << "\n"; }
164
Chris Lattner68061d52009-03-04 21:40:23 +0000165void PrettyStackTraceProgram::print(raw_ostream &OS) const {
Chris Lattner2674eb42009-03-05 06:51:42 +0000166 OS << "Program arguments: ";
Chris Lattner68061d52009-03-04 21:40:23 +0000167 // Print the argument list.
168 for (unsigned i = 0, e = ArgC; i != e; ++i)
169 OS << ArgV[i] << ' ';
170 OS << '\n';
Chris Lattner2674eb42009-03-05 06:51:42 +0000171}
Filip Pizlo9f50ccd2013-11-03 00:29:47 +0000172
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000173#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Filip Pizloc10ca902013-11-04 02:22:25 +0000174static bool RegisterCrashPrinter() {
Craig Topperc10719f2014-04-07 04:17:22 +0000175 sys::AddSignalHandler(CrashHandler, nullptr);
Filip Pizloc10ca902013-11-04 02:22:25 +0000176 return false;
177}
Owen Anderson9253bb92015-01-29 07:35:31 +0000178#endif
Filip Pizloc10ca902013-11-04 02:22:25 +0000179
180void llvm::EnablePrettyStackTrace() {
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000181#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Filip Pizloc10ca902013-11-04 02:22:25 +0000182 // The first time this is called, we register the crash printer.
183 static bool HandlerRegistered = RegisterCrashPrinter();
184 (void)HandlerRegistered;
Owen Anderson9253bb92015-01-29 07:35:31 +0000185#endif
Filip Pizloc10ca902013-11-04 02:22:25 +0000186}
187
Richard Smith3de58a52016-05-26 20:21:55 +0000188const void *llvm::SavePrettyStackState() {
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000189#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Nico Weberaf3f2422015-08-07 17:47:03 +0000190 return PrettyStackTraceHead;
191#else
192 return nullptr;
193#endif
194}
195
Richard Smith3de58a52016-05-26 20:21:55 +0000196void llvm::RestorePrettyStackState(const void *Top) {
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000197#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Sanjoy Dasebeee962016-05-27 23:04:28 +0000198 PrettyStackTraceHead =
199 static_cast<PrettyStackTraceEntry *>(const_cast<void *>(Top));
Nico Weberaf3f2422015-08-07 17:47:03 +0000200#endif
201}
202
Filip Pizloc10ca902013-11-04 02:22:25 +0000203void LLVMEnablePrettyStackTrace() {
204 EnablePrettyStackTrace();
Filip Pizlo9f50ccd2013-11-03 00:29:47 +0000205}