blob: d4e205cdb3ce921a9bef5d95dc518e05f2f48ce6 [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
Filip Pizlo67d97092013-09-13 22:59:47 +000019#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000020#include "llvm/Support/Signals.h"
21#include "llvm/Support/ThreadLocal.h"
Nick Lewycky4e06def2013-03-26 01:27:52 +000022#include "llvm/Support/Watchdog.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/Support/raw_ostream.h"
Eric Christophere9c1bb62010-06-22 21:01:04 +000024
25#ifdef HAVE_CRASHREPORTERCLIENT_H
26#include <CrashReporterClient.h>
27#endif
28
Chris Lattner68061d52009-03-04 21:40:23 +000029using namespace llvm;
30
Filip Pizlo67d97092013-09-13 22:59:47 +000031static ManagedStatic<sys::ThreadLocal<const PrettyStackTraceEntry> > PrettyStackTraceHead;
Chris Lattner68061d52009-03-04 21:40:23 +000032
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000033static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){
34 unsigned NextID = 0;
35 if (Entry->getNextEntry())
36 NextID = PrintStack(Entry->getNextEntry(), OS);
37 OS << NextID << ".\t";
Nick Lewycky4e06def2013-03-26 01:27:52 +000038 {
39 sys::Watchdog W(5);
40 Entry->print(OS);
41 }
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000042
43 return NextID+1;
44}
45
Chris Lattnere41a4342009-03-06 07:19:54 +000046/// PrintCurStackTrace - Print the current stack trace to the specified stream.
47static void PrintCurStackTrace(raw_ostream &OS) {
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000048 // Don't print an empty trace.
Filip Pizlo67d97092013-09-13 22:59:47 +000049 if (PrettyStackTraceHead->get() == 0) return;
Chris Lattner8d0fe8c2009-03-05 07:03:49 +000050
Chris Lattner68061d52009-03-04 21:40:23 +000051 // If there are pretty stack frames registered, walk and emit them.
Chris Lattner68061d52009-03-04 21:40:23 +000052 OS << "Stack dump:\n";
53
Filip Pizlo67d97092013-09-13 22:59:47 +000054 PrintStack(PrettyStackTraceHead->get(), OS);
Chris Lattner68061d52009-03-04 21:40:23 +000055 OS.flush();
56}
57
Eric Christopher51f290832010-06-28 18:25:51 +000058// Integrate with crash reporter libraries.
Eric Christopherca466732010-12-03 07:45:22 +000059#if defined (__APPLE__) && HAVE_CRASHREPORTERCLIENT_H
Eric Christopher51f290832010-06-28 18:25:51 +000060// If any clients of llvm try to link to libCrashReporterClient.a themselves,
61// only one crash info struct will be used.
Eric Christopher7f103a22010-06-28 18:33:48 +000062extern "C" {
Eric Christopher51f290832010-06-28 18:25:51 +000063CRASH_REPORTER_CLIENT_HIDDEN
64struct crashreporter_annotations_t gCRAnnotations
65 __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
Eric Christopherc4b9b522011-10-05 05:00:26 +000066 = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
Eric Christopher7f103a22010-06-28 18:33:48 +000067}
Eric Christopherca466732010-12-03 07:45:22 +000068#elif defined (__APPLE__) && HAVE_CRASHREPORTER_INFO
Daniel Dunbarc120ffe2010-05-20 23:50:19 +000069static const char *__crashreporter_info__ = 0;
70asm(".desc ___crashreporter_info__, 0x10");
Chris Lattnere41a4342009-03-06 07:19:54 +000071#endif
72
73
74/// CrashHandler - This callback is run if a fatal signal is delivered to the
75/// process, it prints the pretty stack trace.
Eric Christopher87947f72010-08-08 00:00:34 +000076static void CrashHandler(void *) {
Chris Lattnere41a4342009-03-06 07:19:54 +000077#ifndef __APPLE__
78 // On non-apple systems, just emit the crash stack trace to stderr.
79 PrintCurStackTrace(errs());
80#else
81 // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
82 // put it into __crashreporter_info__.
83 SmallString<2048> TmpStr;
84 {
85 raw_svector_ostream Stream(TmpStr);
86 PrintCurStackTrace(Stream);
87 }
88
89 if (!TmpStr.empty()) {
Eric Christopherca466732010-12-03 07:45:22 +000090#ifdef HAVE_CRASHREPORTERCLIENT_H
Eric Christopher87947f72010-08-08 00:00:34 +000091 // Cast to void to avoid warning.
92 (void)CRSetCrashLogMessage(std::string(TmpStr.str()).c_str());
Eric Christopherca466732010-12-03 07:45:22 +000093#elif HAVE_CRASHREPORTER_INFO
94 __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str());
Eric Christophere9c1bb62010-06-22 21:01:04 +000095#endif
Daniel Dunbar8b0b1152009-08-19 20:07:03 +000096 errs() << TmpStr.str();
Chris Lattnere41a4342009-03-06 07:19:54 +000097 }
98
99#endif
100}
101
Chris Lattner68061d52009-03-04 21:40:23 +0000102PrettyStackTraceEntry::PrettyStackTraceEntry() {
Chris Lattner68061d52009-03-04 21:40:23 +0000103 // Link ourselves.
Filip Pizlo67d97092013-09-13 22:59:47 +0000104 NextEntry = PrettyStackTraceHead->get();
105 PrettyStackTraceHead->set(this);
Chris Lattner68061d52009-03-04 21:40:23 +0000106}
107
108PrettyStackTraceEntry::~PrettyStackTraceEntry() {
Filip Pizlo67d97092013-09-13 22:59:47 +0000109 // Do nothing if PrettyStackTraceHead is uninitialized. This can only happen
110 // if a shutdown occurred after we created the PrettyStackTraceEntry. That
111 // does occur in the following idiom:
112 //
113 // PrettyStackTraceProgram X(...);
114 // llvm_shutdown_obj Y;
115 //
116 // Without this check, we may end up removing ourselves from the stack trace
117 // after PrettyStackTraceHead has already been destroyed.
118 if (!PrettyStackTraceHead.isConstructed())
119 return;
120
121 assert(PrettyStackTraceHead->get() == this &&
Chris Lattner68061d52009-03-04 21:40:23 +0000122 "Pretty stack trace entry destruction is out of order");
Filip Pizlo67d97092013-09-13 22:59:47 +0000123 PrettyStackTraceHead->set(getNextEntry());
Chris Lattner68061d52009-03-04 21:40:23 +0000124}
125
126void PrettyStackTraceString::print(raw_ostream &OS) const {
127 OS << Str << "\n";
128}
129
130void PrettyStackTraceProgram::print(raw_ostream &OS) const {
Chris Lattner2674eb42009-03-05 06:51:42 +0000131 OS << "Program arguments: ";
Chris Lattner68061d52009-03-04 21:40:23 +0000132 // Print the argument list.
133 for (unsigned i = 0, e = ArgC; i != e; ++i)
134 OS << ArgV[i] << ' ';
135 OS << '\n';
Chris Lattner2674eb42009-03-05 06:51:42 +0000136}
Filip Pizlo9f50ccd2013-11-03 00:29:47 +0000137
Filip Pizloc10ca902013-11-04 02:22:25 +0000138static bool RegisterCrashPrinter() {
139 sys::AddSignalHandler(CrashHandler, 0);
140 return false;
141}
142
143void llvm::EnablePrettyStackTrace() {
144 // The first time this is called, we register the crash printer.
145 static bool HandlerRegistered = RegisterCrashPrinter();
146 (void)HandlerRegistered;
147}
148
149void LLVMEnablePrettyStackTrace() {
150 EnablePrettyStackTrace();
Filip Pizlo9f50ccd2013-11-03 00:29:47 +0000151}