blob: f5b6e6f3652d69663fb13eeb552446bb860261a9 [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)))
Daniel Sanders8c345dc2018-01-30 16:02:32 +000091#if CRASHREPORTER_ANNOTATIONS_VERSION < 5
Eric Christopherc4b9b522011-10-05 05:00:26 +000092 = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
Daniel Sanders8c345dc2018-01-30 16:02:32 +000093#else
94 = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0, 0 };
95#endif
Eric Christopher7f103a22010-06-28 18:33:48 +000096}
Sean Callanan032dbf92016-12-14 19:09:43 +000097#elif defined(__APPLE__) && HAVE_CRASHREPORTER_INFO
98extern "C" const char *__crashreporter_info__
99 __attribute__((visibility("hidden"))) = 0;
Daniel Dunbarc120ffe2010-05-20 23:50:19 +0000100asm(".desc ___crashreporter_info__, 0x10");
Chris Lattnere41a4342009-03-06 07:19:54 +0000101#endif
102
Chris Lattnere41a4342009-03-06 07:19:54 +0000103/// CrashHandler - This callback is run if a fatal signal is delivered to the
104/// process, it prints the pretty stack trace.
Eric Christopher87947f72010-08-08 00:00:34 +0000105static void CrashHandler(void *) {
Chris Lattnere41a4342009-03-06 07:19:54 +0000106#ifndef __APPLE__
107 // On non-apple systems, just emit the crash stack trace to stderr.
108 PrintCurStackTrace(errs());
109#else
110 // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
111 // put it into __crashreporter_info__.
112 SmallString<2048> TmpStr;
113 {
114 raw_svector_ostream Stream(TmpStr);
115 PrintCurStackTrace(Stream);
116 }
117
118 if (!TmpStr.empty()) {
Eric Christopherca466732010-12-03 07:45:22 +0000119#ifdef HAVE_CRASHREPORTERCLIENT_H
Eric Christopher87947f72010-08-08 00:00:34 +0000120 // Cast to void to avoid warning.
Jordan Rose7e535bc2018-06-15 16:35:31 +0000121 (void)CRSetCrashLogMessage(TmpStr.c_str());
Eric Christopherca466732010-12-03 07:45:22 +0000122#elif HAVE_CRASHREPORTER_INFO
Jordan Rose7e535bc2018-06-15 16:35:31 +0000123 __crashreporter_info__ = strdup(TmpStr.c_str());
Eric Christophere9c1bb62010-06-22 21:01:04 +0000124#endif
Daniel Dunbar8b0b1152009-08-19 20:07:03 +0000125 errs() << TmpStr.str();
Chris Lattnere41a4342009-03-06 07:19:54 +0000126 }
127
128#endif
129}
130
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000131// defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Owen Anderson9253bb92015-01-29 07:35:31 +0000132#endif
133
Chris Lattner68061d52009-03-04 21:40:23 +0000134PrettyStackTraceEntry::PrettyStackTraceEntry() {
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000135#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Chris Lattner68061d52009-03-04 21:40:23 +0000136 // Link ourselves.
Chandler Carruth16b670e2015-01-28 09:52:14 +0000137 NextEntry = PrettyStackTraceHead;
138 PrettyStackTraceHead = this;
Owen Anderson9253bb92015-01-29 07:35:31 +0000139#endif
Chris Lattner68061d52009-03-04 21:40:23 +0000140}
141
142PrettyStackTraceEntry::~PrettyStackTraceEntry() {
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000143#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Chandler Carruth16b670e2015-01-28 09:52:14 +0000144 assert(PrettyStackTraceHead == this &&
Chris Lattner68061d52009-03-04 21:40:23 +0000145 "Pretty stack trace entry destruction is out of order");
Richard Smith3de58a52016-05-26 20:21:55 +0000146 PrettyStackTraceHead = NextEntry;
Owen Anderson9253bb92015-01-29 07:35:31 +0000147#endif
Chris Lattner68061d52009-03-04 21:40:23 +0000148}
149
Sean Callanan032dbf92016-12-14 19:09:43 +0000150void PrettyStackTraceString::print(raw_ostream &OS) const { OS << Str << "\n"; }
151
152PrettyStackTraceFormat::PrettyStackTraceFormat(const char *Format, ...) {
153 va_list AP;
154 va_start(AP, Format);
155 const int SizeOrError = vsnprintf(nullptr, 0, Format, AP);
156 va_end(AP);
157 if (SizeOrError < 0) {
158 return;
159 }
160
161 const int Size = SizeOrError + 1; // '\0'
162 Str.resize(Size);
163 va_start(AP, Format);
164 vsnprintf(Str.data(), Size, Format, AP);
165 va_end(AP);
Chris Lattner68061d52009-03-04 21:40:23 +0000166}
167
Sean Callanan032dbf92016-12-14 19:09:43 +0000168void PrettyStackTraceFormat::print(raw_ostream &OS) const { OS << Str << "\n"; }
169
Chris Lattner68061d52009-03-04 21:40:23 +0000170void PrettyStackTraceProgram::print(raw_ostream &OS) const {
Chris Lattner2674eb42009-03-05 06:51:42 +0000171 OS << "Program arguments: ";
Chris Lattner68061d52009-03-04 21:40:23 +0000172 // Print the argument list.
173 for (unsigned i = 0, e = ArgC; i != e; ++i)
174 OS << ArgV[i] << ' ';
175 OS << '\n';
Chris Lattner2674eb42009-03-05 06:51:42 +0000176}
Filip Pizlo9f50ccd2013-11-03 00:29:47 +0000177
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000178#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Filip Pizloc10ca902013-11-04 02:22:25 +0000179static bool RegisterCrashPrinter() {
Craig Topperc10719f2014-04-07 04:17:22 +0000180 sys::AddSignalHandler(CrashHandler, nullptr);
Filip Pizloc10ca902013-11-04 02:22:25 +0000181 return false;
182}
Owen Anderson9253bb92015-01-29 07:35:31 +0000183#endif
Filip Pizloc10ca902013-11-04 02:22:25 +0000184
185void llvm::EnablePrettyStackTrace() {
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000186#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Filip Pizloc10ca902013-11-04 02:22:25 +0000187 // The first time this is called, we register the crash printer.
188 static bool HandlerRegistered = RegisterCrashPrinter();
189 (void)HandlerRegistered;
Owen Anderson9253bb92015-01-29 07:35:31 +0000190#endif
Filip Pizloc10ca902013-11-04 02:22:25 +0000191}
192
Richard Smith3de58a52016-05-26 20:21:55 +0000193const void *llvm::SavePrettyStackState() {
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000194#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Nico Weberaf3f2422015-08-07 17:47:03 +0000195 return PrettyStackTraceHead;
196#else
197 return nullptr;
198#endif
199}
200
Richard Smith3de58a52016-05-26 20:21:55 +0000201void llvm::RestorePrettyStackState(const void *Top) {
Joerg Sonnenberger0e3cc3c2016-09-30 20:04:24 +0000202#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
Sanjoy Dasebeee962016-05-27 23:04:28 +0000203 PrettyStackTraceHead =
204 static_cast<PrettyStackTraceEntry *>(const_cast<void *>(Top));
Nico Weberaf3f2422015-08-07 17:47:03 +0000205#endif
206}
207
Filip Pizloc10ca902013-11-04 02:22:25 +0000208void LLVMEnablePrettyStackTrace() {
209 EnablePrettyStackTrace();
Filip Pizlo9f50ccd2013-11-03 00:29:47 +0000210}