blob: e14dbc1b56c25a32c9eff23aa7687d22ce3f5507 [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
Richard Smith3de58a52016-05-26 20:21:55 +000024#include <tuple>
25
Eric Christophere9c1bb62010-06-22 21:01:04 +000026#ifdef HAVE_CRASHREPORTERCLIENT_H
27#include <CrashReporterClient.h>
28#endif
29
Chris Lattner68061d52009-03-04 21:40:23 +000030using namespace llvm;
31
Owen Anderson9253bb92015-01-29 07:35:31 +000032// If backtrace support is not enabled, compile out support for pretty stack
33// traces. This has the secondary effect of not requiring thread local storage
34// when backtrace support is disabled.
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +000035#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Owen Anderson9253bb92015-01-29 07:35:31 +000036
Chandler Carruth16b670e2015-01-28 09:52:14 +000037// We need a thread local pointer to manage the stack of our stack trace
38// objects, but we *really* cannot tolerate destructors running and do not want
39// to pay any overhead of synchronizing. As a consequence, we use a raw
Chandler Carruthfb3139a2015-01-29 01:23:04 +000040// thread-local variable.
Richard Smith3de58a52016-05-26 20:21:55 +000041static LLVM_THREAD_LOCAL PrettyStackTraceEntry *PrettyStackTraceHead = nullptr;
Chris Lattner68061d52009-03-04 21:40:23 +000042
Richard Smith3de58a52016-05-26 20:21:55 +000043namespace llvm {
44PrettyStackTraceEntry *ReverseStackTrace(PrettyStackTraceEntry *Head) {
45 PrettyStackTraceEntry *Prev = nullptr;
46 while (Head)
47 std::tie(Prev, Head, Head->NextEntry) =
48 std::make_tuple(Head, Head->NextEntry, Prev);
49 return Prev;
50}
51}
52
53static void PrintStack(raw_ostream &OS) {
54 // Print out the stack in reverse order. To avoid recursion (which is likely
55 // to fail if we crashed due to stack overflow), we do an up-front pass to
56 // reverse the stack, then print it, then reverse it again.
57 unsigned ID = 0;
58 PrettyStackTraceEntry *ReversedStack =
59 llvm::ReverseStackTrace(PrettyStackTraceHead);
60 for (const PrettyStackTraceEntry *Entry = ReversedStack; Entry;
61 Entry = Entry->getNextEntry()) {
62 OS << ID++ << ".\t";
Nick Lewycky4e06def2013-03-26 01:27:52 +000063 sys::Watchdog W(5);
64 Entry->print(OS);
65 }
Richard Smith3de58a52016-05-26 20:21:55 +000066 llvm::ReverseStackTrace(ReversedStack);
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000067}
68
Chris Lattnere41a4342009-03-06 07:19:54 +000069/// PrintCurStackTrace - Print the current stack trace to the specified stream.
70static void PrintCurStackTrace(raw_ostream &OS) {
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000071 // Don't print an empty trace.
Chandler Carruth16b670e2015-01-28 09:52:14 +000072 if (!PrettyStackTraceHead) return;
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000073
Chris Lattner68061d52009-03-04 21:40:23 +000074 // If there are pretty stack frames registered, walk and emit them.
Chris Lattner68061d52009-03-04 21:40:23 +000075 OS << "Stack dump:\n";
76
Richard Smith3de58a52016-05-26 20:21:55 +000077 PrintStack(OS);
Chris Lattner68061d52009-03-04 21:40:23 +000078 OS.flush();
79}
80
Eric Christopher51f290832010-06-28 18:25:51 +000081// Integrate with crash reporter libraries.
Chris Bienemanbfff2542016-12-07 19:28:22 +000082#if defined (__APPLE__) && defined(HAVE_CRASHREPORTERCLIENT_H)
Eric Christopher51f290832010-06-28 18:25:51 +000083// If any clients of llvm try to link to libCrashReporterClient.a themselves,
84// only one crash info struct will be used.
Eric Christopher7f103a22010-06-28 18:33:48 +000085extern "C" {
Eric Christopher51f290832010-06-28 18:25:51 +000086CRASH_REPORTER_CLIENT_HIDDEN
87struct crashreporter_annotations_t gCRAnnotations
88 __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
Eric Christopherc4b9b522011-10-05 05:00:26 +000089 = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
Eric Christopher7f103a22010-06-28 18:33:48 +000090}
Sean Callanan032dbf92016-12-14 19:09:43 +000091#elif defined(__APPLE__) && HAVE_CRASHREPORTER_INFO
92extern "C" const char *__crashreporter_info__
93 __attribute__((visibility("hidden"))) = 0;
Daniel Dunbarc120ffe2010-05-20 23:50:19 +000094asm(".desc ___crashreporter_info__, 0x10");
Chris Lattnere41a4342009-03-06 07:19:54 +000095#endif
96
Chris Lattnere41a4342009-03-06 07:19:54 +000097/// CrashHandler - This callback is run if a fatal signal is delivered to the
98/// process, it prints the pretty stack trace.
Eric Christopher87947f72010-08-08 00:00:34 +000099static void CrashHandler(void *) {
Chris Lattnere41a4342009-03-06 07:19:54 +0000100#ifndef __APPLE__
101 // On non-apple systems, just emit the crash stack trace to stderr.
102 PrintCurStackTrace(errs());
103#else
104 // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
105 // put it into __crashreporter_info__.
106 SmallString<2048> TmpStr;
107 {
108 raw_svector_ostream Stream(TmpStr);
109 PrintCurStackTrace(Stream);
110 }
111
112 if (!TmpStr.empty()) {
Eric Christopherca466732010-12-03 07:45:22 +0000113#ifdef HAVE_CRASHREPORTERCLIENT_H
Eric Christopher87947f72010-08-08 00:00:34 +0000114 // Cast to void to avoid warning.
115 (void)CRSetCrashLogMessage(std::string(TmpStr.str()).c_str());
Eric Christopherca466732010-12-03 07:45:22 +0000116#elif HAVE_CRASHREPORTER_INFO
117 __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str());
Eric Christophere9c1bb62010-06-22 21:01:04 +0000118#endif
Daniel Dunbar8b0b1152009-08-19 20:07:03 +0000119 errs() << TmpStr.str();
Chris Lattnere41a4342009-03-06 07:19:54 +0000120 }
121
122#endif
123}
124
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000125// defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Owen Anderson9253bb92015-01-29 07:35:31 +0000126#endif
127
Chris Lattner68061d52009-03-04 21:40:23 +0000128PrettyStackTraceEntry::PrettyStackTraceEntry() {
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000129#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Chris Lattner68061d52009-03-04 21:40:23 +0000130 // Link ourselves.
Chandler Carruth16b670e2015-01-28 09:52:14 +0000131 NextEntry = PrettyStackTraceHead;
132 PrettyStackTraceHead = this;
Owen Anderson9253bb92015-01-29 07:35:31 +0000133#endif
Chris Lattner68061d52009-03-04 21:40:23 +0000134}
135
136PrettyStackTraceEntry::~PrettyStackTraceEntry() {
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000137#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Chandler Carruth16b670e2015-01-28 09:52:14 +0000138 assert(PrettyStackTraceHead == this &&
Chris Lattner68061d52009-03-04 21:40:23 +0000139 "Pretty stack trace entry destruction is out of order");
Richard Smith3de58a52016-05-26 20:21:55 +0000140 PrettyStackTraceHead = NextEntry;
Owen Anderson9253bb92015-01-29 07:35:31 +0000141#endif
Chris Lattner68061d52009-03-04 21:40:23 +0000142}
143
Sean Callanan032dbf92016-12-14 19:09:43 +0000144void PrettyStackTraceString::print(raw_ostream &OS) const { OS << Str << "\n"; }
145
146PrettyStackTraceFormat::PrettyStackTraceFormat(const char *Format, ...) {
147 va_list AP;
148 va_start(AP, Format);
149 const int SizeOrError = vsnprintf(nullptr, 0, Format, AP);
150 va_end(AP);
151 if (SizeOrError < 0) {
152 return;
153 }
154
155 const int Size = SizeOrError + 1; // '\0'
156 Str.resize(Size);
157 va_start(AP, Format);
158 vsnprintf(Str.data(), Size, Format, AP);
159 va_end(AP);
Chris Lattner68061d52009-03-04 21:40:23 +0000160}
161
Sean Callanan032dbf92016-12-14 19:09:43 +0000162void PrettyStackTraceFormat::print(raw_ostream &OS) const { OS << Str << "\n"; }
163
Chris Lattner68061d52009-03-04 21:40:23 +0000164void PrettyStackTraceProgram::print(raw_ostream &OS) const {
Chris Lattner2674eb42009-03-05 06:51:42 +0000165 OS << "Program arguments: ";
Chris Lattner68061d52009-03-04 21:40:23 +0000166 // Print the argument list.
167 for (unsigned i = 0, e = ArgC; i != e; ++i)
168 OS << ArgV[i] << ' ';
169 OS << '\n';
Chris Lattner2674eb42009-03-05 06:51:42 +0000170}
Filip Pizlo9f50ccd2013-11-03 00:29:47 +0000171
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000172#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Filip Pizloc10ca902013-11-04 02:22:25 +0000173static bool RegisterCrashPrinter() {
Craig Topperc10719f2014-04-07 04:17:22 +0000174 sys::AddSignalHandler(CrashHandler, nullptr);
Filip Pizloc10ca902013-11-04 02:22:25 +0000175 return false;
176}
Owen Anderson9253bb92015-01-29 07:35:31 +0000177#endif
Filip Pizloc10ca902013-11-04 02:22:25 +0000178
179void llvm::EnablePrettyStackTrace() {
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000180#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Filip Pizloc10ca902013-11-04 02:22:25 +0000181 // The first time this is called, we register the crash printer.
182 static bool HandlerRegistered = RegisterCrashPrinter();
183 (void)HandlerRegistered;
Owen Anderson9253bb92015-01-29 07:35:31 +0000184#endif
Filip Pizloc10ca902013-11-04 02:22:25 +0000185}
186
Richard Smith3de58a52016-05-26 20:21:55 +0000187const void *llvm::SavePrettyStackState() {
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000188#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Nico Weberaf3f2422015-08-07 17:47:03 +0000189 return PrettyStackTraceHead;
190#else
191 return nullptr;
192#endif
193}
194
Richard Smith3de58a52016-05-26 20:21:55 +0000195void llvm::RestorePrettyStackState(const void *Top) {
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000196#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Sanjoy Dasebeee962016-05-27 23:04:28 +0000197 PrettyStackTraceHead =
198 static_cast<PrettyStackTraceEntry *>(const_cast<void *>(Top));
Nico Weberaf3f2422015-08-07 17:47:03 +0000199#endif
200}
201
Filip Pizloc10ca902013-11-04 02:22:25 +0000202void LLVMEnablePrettyStackTrace() {
203 EnablePrettyStackTrace();
Filip Pizlo9f50ccd2013-11-03 00:29:47 +0000204}