blob: ed576d5360099109f5af18f34fcd54ef7ecd67de [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"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm-c/Core.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/SmallString.h"
18#include "llvm/Config/config.h" // Get autoconf configuration settings
Michael J. Spencer447762d2010-11-29 18:16:10 +000019#include "llvm/Support/Signals.h"
Nick Lewycky4e06def2013-03-26 01:27:52 +000020#include "llvm/Support/Watchdog.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/Support/raw_ostream.h"
Eric Christophere9c1bb62010-06-22 21:01:04 +000022
23#ifdef HAVE_CRASHREPORTERCLIENT_H
24#include <CrashReporterClient.h>
25#endif
26
Chris Lattner68061d52009-03-04 21:40:23 +000027using namespace llvm;
28
Chandler Carruth16b670e2015-01-28 09:52:14 +000029// We need a thread local pointer to manage the stack of our stack trace
30// objects, but we *really* cannot tolerate destructors running and do not want
31// to pay any overhead of synchronizing. As a consequence, we use a raw
32// thread-local variable. Some day, we should be able to use a limited subset
33// of C++11's thread_local, but compilers aren't up for it today.
34// FIXME: This should be moved to a Compiler.h abstraction.
Chandler Carruthbe09eb72015-01-28 19:29:22 +000035#ifdef _MSC_VER
36// MSVC supports this with a __declspec.
37#define LLVM_THREAD_LOCAL __declspec(thread)
38#else
39// Clang, GCC, and all compatible compilers tend to use __thread. But we need
40// to work aronud a bug in the combination of Clang's compilation of
41// local-dynamic TLS and the ppc64 linker relocations which we do by forcing to
Chandler Carruthb2fe3e52015-01-28 23:10:57 +000042// global-dynamic (called in most documents "general dynamic").
Chandler Carruthbe09eb72015-01-28 19:29:22 +000043// FIXME: Make this conditional on the Clang version once this is fixed in
44// top-of-tree.
45#if defined(__clang__) && defined(__powerpc64__)
Chandler Carruthb2fe3e52015-01-28 23:10:57 +000046#define LLVM_THREAD_LOCAL __thread __attribute__((tls_model("global-dynamic")))
Chandler Carruthbe09eb72015-01-28 19:29:22 +000047#else
48#define LLVM_THREAD_LOCAL __thread
Chandler Carruth16b670e2015-01-28 09:52:14 +000049#endif
Chandler Carruthbe09eb72015-01-28 19:29:22 +000050#endif
51
52static LLVM_THREAD_LOCAL const PrettyStackTraceEntry *PrettyStackTraceHead =
53 nullptr;
Chris Lattner68061d52009-03-04 21:40:23 +000054
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000055static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){
56 unsigned NextID = 0;
57 if (Entry->getNextEntry())
58 NextID = PrintStack(Entry->getNextEntry(), OS);
59 OS << NextID << ".\t";
Nick Lewycky4e06def2013-03-26 01:27:52 +000060 {
61 sys::Watchdog W(5);
62 Entry->print(OS);
63 }
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000064
65 return NextID+1;
66}
67
Chris Lattnere41a4342009-03-06 07:19:54 +000068/// PrintCurStackTrace - Print the current stack trace to the specified stream.
69static void PrintCurStackTrace(raw_ostream &OS) {
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000070 // Don't print an empty trace.
Chandler Carruth16b670e2015-01-28 09:52:14 +000071 if (!PrettyStackTraceHead) return;
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000072
Chris Lattner68061d52009-03-04 21:40:23 +000073 // If there are pretty stack frames registered, walk and emit them.
Chris Lattner68061d52009-03-04 21:40:23 +000074 OS << "Stack dump:\n";
75
Chandler Carruth16b670e2015-01-28 09:52:14 +000076 PrintStack(PrettyStackTraceHead, OS);
Chris Lattner68061d52009-03-04 21:40:23 +000077 OS.flush();
78}
79
Eric Christopher51f290832010-06-28 18:25:51 +000080// Integrate with crash reporter libraries.
Eric Christopherca466732010-12-03 07:45:22 +000081#if defined (__APPLE__) && HAVE_CRASHREPORTERCLIENT_H
Eric Christopher51f290832010-06-28 18:25:51 +000082// If any clients of llvm try to link to libCrashReporterClient.a themselves,
83// only one crash info struct will be used.
Eric Christopher7f103a22010-06-28 18:33:48 +000084extern "C" {
Eric Christopher51f290832010-06-28 18:25:51 +000085CRASH_REPORTER_CLIENT_HIDDEN
86struct crashreporter_annotations_t gCRAnnotations
87 __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
Eric Christopherc4b9b522011-10-05 05:00:26 +000088 = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
Eric Christopher7f103a22010-06-28 18:33:48 +000089}
Eric Christopherca466732010-12-03 07:45:22 +000090#elif defined (__APPLE__) && HAVE_CRASHREPORTER_INFO
Daniel Dunbarc120ffe2010-05-20 23:50:19 +000091static const char *__crashreporter_info__ = 0;
92asm(".desc ___crashreporter_info__, 0x10");
Chris Lattnere41a4342009-03-06 07:19:54 +000093#endif
94
95
96/// CrashHandler - This callback is run if a fatal signal is delivered to the
97/// process, it prints the pretty stack trace.
Eric Christopher87947f72010-08-08 00:00:34 +000098static void CrashHandler(void *) {
Chris Lattnere41a4342009-03-06 07:19:54 +000099#ifndef __APPLE__
100 // On non-apple systems, just emit the crash stack trace to stderr.
101 PrintCurStackTrace(errs());
102#else
103 // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
104 // put it into __crashreporter_info__.
105 SmallString<2048> TmpStr;
106 {
107 raw_svector_ostream Stream(TmpStr);
108 PrintCurStackTrace(Stream);
109 }
110
111 if (!TmpStr.empty()) {
Eric Christopherca466732010-12-03 07:45:22 +0000112#ifdef HAVE_CRASHREPORTERCLIENT_H
Eric Christopher87947f72010-08-08 00:00:34 +0000113 // Cast to void to avoid warning.
114 (void)CRSetCrashLogMessage(std::string(TmpStr.str()).c_str());
Eric Christopherca466732010-12-03 07:45:22 +0000115#elif HAVE_CRASHREPORTER_INFO
116 __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str());
Eric Christophere9c1bb62010-06-22 21:01:04 +0000117#endif
Daniel Dunbar8b0b1152009-08-19 20:07:03 +0000118 errs() << TmpStr.str();
Chris Lattnere41a4342009-03-06 07:19:54 +0000119 }
120
121#endif
122}
123
Chris Lattner68061d52009-03-04 21:40:23 +0000124PrettyStackTraceEntry::PrettyStackTraceEntry() {
Chris Lattner68061d52009-03-04 21:40:23 +0000125 // Link ourselves.
Chandler Carruth16b670e2015-01-28 09:52:14 +0000126 NextEntry = PrettyStackTraceHead;
127 PrettyStackTraceHead = this;
Chris Lattner68061d52009-03-04 21:40:23 +0000128}
129
130PrettyStackTraceEntry::~PrettyStackTraceEntry() {
Chandler Carruth16b670e2015-01-28 09:52:14 +0000131 assert(PrettyStackTraceHead == this &&
Chris Lattner68061d52009-03-04 21:40:23 +0000132 "Pretty stack trace entry destruction is out of order");
Chandler Carruth16b670e2015-01-28 09:52:14 +0000133 PrettyStackTraceHead = getNextEntry();
Chris Lattner68061d52009-03-04 21:40:23 +0000134}
135
136void PrettyStackTraceString::print(raw_ostream &OS) const {
137 OS << Str << "\n";
138}
139
140void PrettyStackTraceProgram::print(raw_ostream &OS) const {
Chris Lattner2674eb42009-03-05 06:51:42 +0000141 OS << "Program arguments: ";
Chris Lattner68061d52009-03-04 21:40:23 +0000142 // Print the argument list.
143 for (unsigned i = 0, e = ArgC; i != e; ++i)
144 OS << ArgV[i] << ' ';
145 OS << '\n';
Chris Lattner2674eb42009-03-05 06:51:42 +0000146}
Filip Pizlo9f50ccd2013-11-03 00:29:47 +0000147
Filip Pizloc10ca902013-11-04 02:22:25 +0000148static bool RegisterCrashPrinter() {
Craig Topperc10719f2014-04-07 04:17:22 +0000149 sys::AddSignalHandler(CrashHandler, nullptr);
Filip Pizloc10ca902013-11-04 02:22:25 +0000150 return false;
151}
152
153void llvm::EnablePrettyStackTrace() {
154 // The first time this is called, we register the crash printer.
155 static bool HandlerRegistered = RegisterCrashPrinter();
156 (void)HandlerRegistered;
157}
158
159void LLVMEnablePrettyStackTrace() {
160 EnablePrettyStackTrace();
Filip Pizlo9f50ccd2013-11-03 00:29:47 +0000161}