blob: a18e9cc50040fa3d317a0a195565e60521260616 [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"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000018#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>
Dimitry Andricc12f10f2017-04-29 23:45:30 +000025#include <cstdio>
Richard Smith3de58a52016-05-26 20:21:55 +000026#include <tuple>
27
Eric Christophere9c1bb62010-06-22 21:01:04 +000028#ifdef HAVE_CRASHREPORTERCLIENT_H
29#include <CrashReporterClient.h>
30#endif
31
Chris Lattner68061d52009-03-04 21:40:23 +000032using namespace llvm;
33
Owen Anderson9253bb92015-01-29 07:35:31 +000034// If backtrace support is not enabled, compile out support for pretty stack
35// traces. This has the secondary effect of not requiring thread local storage
36// when backtrace support is disabled.
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +000037#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Owen Anderson9253bb92015-01-29 07:35:31 +000038
Chandler Carruth16b670e2015-01-28 09:52:14 +000039// We need a thread local pointer to manage the stack of our stack trace
40// objects, but we *really* cannot tolerate destructors running and do not want
41// to pay any overhead of synchronizing. As a consequence, we use a raw
Chandler Carruthfb3139a2015-01-29 01:23:04 +000042// thread-local variable.
Richard Smith3de58a52016-05-26 20:21:55 +000043static LLVM_THREAD_LOCAL PrettyStackTraceEntry *PrettyStackTraceHead = nullptr;
Chris Lattner68061d52009-03-04 21:40:23 +000044
Richard Smith3de58a52016-05-26 20:21:55 +000045namespace llvm {
46PrettyStackTraceEntry *ReverseStackTrace(PrettyStackTraceEntry *Head) {
47 PrettyStackTraceEntry *Prev = nullptr;
48 while (Head)
49 std::tie(Prev, Head, Head->NextEntry) =
50 std::make_tuple(Head, Head->NextEntry, Prev);
51 return Prev;
52}
53}
54
55static void PrintStack(raw_ostream &OS) {
56 // Print out the stack in reverse order. To avoid recursion (which is likely
57 // to fail if we crashed due to stack overflow), we do an up-front pass to
58 // reverse the stack, then print it, then reverse it again.
59 unsigned ID = 0;
60 PrettyStackTraceEntry *ReversedStack =
61 llvm::ReverseStackTrace(PrettyStackTraceHead);
62 for (const PrettyStackTraceEntry *Entry = ReversedStack; Entry;
63 Entry = Entry->getNextEntry()) {
64 OS << ID++ << ".\t";
Nick Lewycky4e06def2013-03-26 01:27:52 +000065 sys::Watchdog W(5);
66 Entry->print(OS);
67 }
Richard Smith3de58a52016-05-26 20:21:55 +000068 llvm::ReverseStackTrace(ReversedStack);
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000069}
70
Chris Lattnere41a4342009-03-06 07:19:54 +000071/// PrintCurStackTrace - Print the current stack trace to the specified stream.
72static void PrintCurStackTrace(raw_ostream &OS) {
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000073 // Don't print an empty trace.
Chandler Carruth16b670e2015-01-28 09:52:14 +000074 if (!PrettyStackTraceHead) return;
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000075
Chris Lattner68061d52009-03-04 21:40:23 +000076 // If there are pretty stack frames registered, walk and emit them.
Chris Lattner68061d52009-03-04 21:40:23 +000077 OS << "Stack dump:\n";
78
Richard Smith3de58a52016-05-26 20:21:55 +000079 PrintStack(OS);
Chris Lattner68061d52009-03-04 21:40:23 +000080 OS.flush();
81}
82
Eric Christopher51f290832010-06-28 18:25:51 +000083// Integrate with crash reporter libraries.
Chris Bienemanbfff2542016-12-07 19:28:22 +000084#if defined (__APPLE__) && defined(HAVE_CRASHREPORTERCLIENT_H)
Eric Christopher51f290832010-06-28 18:25:51 +000085// If any clients of llvm try to link to libCrashReporterClient.a themselves,
86// only one crash info struct will be used.
Eric Christopher7f103a22010-06-28 18:33:48 +000087extern "C" {
Eric Christopher51f290832010-06-28 18:25:51 +000088CRASH_REPORTER_CLIENT_HIDDEN
89struct crashreporter_annotations_t gCRAnnotations
90 __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
Eric Christopherc4b9b522011-10-05 05:00:26 +000091 = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
Eric Christopher7f103a22010-06-28 18:33:48 +000092}
Sean Callanan032dbf92016-12-14 19:09:43 +000093#elif defined(__APPLE__) && HAVE_CRASHREPORTER_INFO
94extern "C" const char *__crashreporter_info__
95 __attribute__((visibility("hidden"))) = 0;
Daniel Dunbarc120ffe2010-05-20 23:50:19 +000096asm(".desc ___crashreporter_info__, 0x10");
Chris Lattnere41a4342009-03-06 07:19:54 +000097#endif
98
Chris Lattnere41a4342009-03-06 07:19:54 +000099/// CrashHandler - This callback is run if a fatal signal is delivered to the
100/// process, it prints the pretty stack trace.
Eric Christopher87947f72010-08-08 00:00:34 +0000101static void CrashHandler(void *) {
Chris Lattnere41a4342009-03-06 07:19:54 +0000102#ifndef __APPLE__
103 // On non-apple systems, just emit the crash stack trace to stderr.
104 PrintCurStackTrace(errs());
105#else
106 // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
107 // put it into __crashreporter_info__.
108 SmallString<2048> TmpStr;
109 {
110 raw_svector_ostream Stream(TmpStr);
111 PrintCurStackTrace(Stream);
112 }
113
114 if (!TmpStr.empty()) {
Eric Christopherca466732010-12-03 07:45:22 +0000115#ifdef HAVE_CRASHREPORTERCLIENT_H
Eric Christopher87947f72010-08-08 00:00:34 +0000116 // Cast to void to avoid warning.
117 (void)CRSetCrashLogMessage(std::string(TmpStr.str()).c_str());
Eric Christopherca466732010-12-03 07:45:22 +0000118#elif HAVE_CRASHREPORTER_INFO
119 __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str());
Eric Christophere9c1bb62010-06-22 21:01:04 +0000120#endif
Daniel Dunbar8b0b1152009-08-19 20:07:03 +0000121 errs() << TmpStr.str();
Chris Lattnere41a4342009-03-06 07:19:54 +0000122 }
123
124#endif
125}
126
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000127// defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Owen Anderson9253bb92015-01-29 07:35:31 +0000128#endif
129
Chris Lattner68061d52009-03-04 21:40:23 +0000130PrettyStackTraceEntry::PrettyStackTraceEntry() {
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000131#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Chris Lattner68061d52009-03-04 21:40:23 +0000132 // Link ourselves.
Chandler Carruth16b670e2015-01-28 09:52:14 +0000133 NextEntry = PrettyStackTraceHead;
134 PrettyStackTraceHead = this;
Owen Anderson9253bb92015-01-29 07:35:31 +0000135#endif
Chris Lattner68061d52009-03-04 21:40:23 +0000136}
137
138PrettyStackTraceEntry::~PrettyStackTraceEntry() {
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000139#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Chandler Carruth16b670e2015-01-28 09:52:14 +0000140 assert(PrettyStackTraceHead == this &&
Chris Lattner68061d52009-03-04 21:40:23 +0000141 "Pretty stack trace entry destruction is out of order");
Richard Smith3de58a52016-05-26 20:21:55 +0000142 PrettyStackTraceHead = NextEntry;
Owen Anderson9253bb92015-01-29 07:35:31 +0000143#endif
Chris Lattner68061d52009-03-04 21:40:23 +0000144}
145
Sean Callanan032dbf92016-12-14 19:09:43 +0000146void PrettyStackTraceString::print(raw_ostream &OS) const { OS << Str << "\n"; }
147
148PrettyStackTraceFormat::PrettyStackTraceFormat(const char *Format, ...) {
149 va_list AP;
150 va_start(AP, Format);
151 const int SizeOrError = vsnprintf(nullptr, 0, Format, AP);
152 va_end(AP);
153 if (SizeOrError < 0) {
154 return;
155 }
156
157 const int Size = SizeOrError + 1; // '\0'
158 Str.resize(Size);
159 va_start(AP, Format);
160 vsnprintf(Str.data(), Size, Format, AP);
161 va_end(AP);
Chris Lattner68061d52009-03-04 21:40:23 +0000162}
163
Sean Callanan032dbf92016-12-14 19:09:43 +0000164void PrettyStackTraceFormat::print(raw_ostream &OS) const { OS << Str << "\n"; }
165
Chris Lattner68061d52009-03-04 21:40:23 +0000166void PrettyStackTraceProgram::print(raw_ostream &OS) const {
Chris Lattner2674eb42009-03-05 06:51:42 +0000167 OS << "Program arguments: ";
Chris Lattner68061d52009-03-04 21:40:23 +0000168 // Print the argument list.
169 for (unsigned i = 0, e = ArgC; i != e; ++i)
170 OS << ArgV[i] << ' ';
171 OS << '\n';
Chris Lattner2674eb42009-03-05 06:51:42 +0000172}
Filip Pizlo9f50ccd2013-11-03 00:29:47 +0000173
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000174#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Filip Pizloc10ca902013-11-04 02:22:25 +0000175static bool RegisterCrashPrinter() {
Craig Topperc10719f2014-04-07 04:17:22 +0000176 sys::AddSignalHandler(CrashHandler, nullptr);
Filip Pizloc10ca902013-11-04 02:22:25 +0000177 return false;
178}
Owen Anderson9253bb92015-01-29 07:35:31 +0000179#endif
Filip Pizloc10ca902013-11-04 02:22:25 +0000180
181void llvm::EnablePrettyStackTrace() {
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000182#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Filip Pizloc10ca902013-11-04 02:22:25 +0000183 // The first time this is called, we register the crash printer.
184 static bool HandlerRegistered = RegisterCrashPrinter();
185 (void)HandlerRegistered;
Owen Anderson9253bb92015-01-29 07:35:31 +0000186#endif
Filip Pizloc10ca902013-11-04 02:22:25 +0000187}
188
Richard Smith3de58a52016-05-26 20:21:55 +0000189const void *llvm::SavePrettyStackState() {
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000190#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Nico Weberaf3f2422015-08-07 17:47:03 +0000191 return PrettyStackTraceHead;
192#else
193 return nullptr;
194#endif
195}
196
Richard Smith3de58a52016-05-26 20:21:55 +0000197void llvm::RestorePrettyStackState(const void *Top) {
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000198#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Sanjoy Dasebeee962016-05-27 23:04:28 +0000199 PrettyStackTraceHead =
200 static_cast<PrettyStackTraceEntry *>(const_cast<void *>(Top));
Nico Weberaf3f2422015-08-07 17:47:03 +0000201#endif
202}
203
Filip Pizloc10ca902013-11-04 02:22:25 +0000204void LLVMEnablePrettyStackTrace() {
205 EnablePrettyStackTrace();
Filip Pizlo9f50ccd2013-11-03 00:29:47 +0000206}