LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PrettyStackTrace.cpp
Go to the documentation of this file.
1 //===- 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
11 // Unix signals occurring while your program is running.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/Config/config.h" // Get autoconf configuration settings
19 #include "llvm/Support/Signals.h"
21 #include "llvm/Support/Watchdog.h"
23 #include "llvm-c/Core.h"
24 
25 #ifdef HAVE_CRASHREPORTERCLIENT_H
26 #include <CrashReporterClient.h>
27 #endif
28 
29 using namespace llvm;
30 
32 
33 static 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";
38  {
39  sys::Watchdog W(5);
40  Entry->print(OS);
41  }
42 
43  return NextID+1;
44 }
45 
46 /// PrintCurStackTrace - Print the current stack trace to the specified stream.
47 static void PrintCurStackTrace(raw_ostream &OS) {
48  // Don't print an empty trace.
49  if (PrettyStackTraceHead->get() == 0) return;
50 
51  // If there are pretty stack frames registered, walk and emit them.
52  OS << "Stack dump:\n";
53 
54  PrintStack(PrettyStackTraceHead->get(), OS);
55  OS.flush();
56 }
57 
58 // Integrate with crash reporter libraries.
59 #if defined (__APPLE__) && HAVE_CRASHREPORTERCLIENT_H
60 // If any clients of llvm try to link to libCrashReporterClient.a themselves,
61 // only one crash info struct will be used.
62 extern "C" {
63 CRASH_REPORTER_CLIENT_HIDDEN
64 struct crashreporter_annotations_t gCRAnnotations
65  __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
66  = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
67 }
68 #elif defined (__APPLE__) && HAVE_CRASHREPORTER_INFO
69 static const char *__crashreporter_info__ = 0;
70 asm(".desc ___crashreporter_info__, 0x10");
71 #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.
76 static void CrashHandler(void *) {
77 #ifndef __APPLE__
78  // On non-apple systems, just emit the crash stack trace to stderr.
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()) {
90 #ifdef HAVE_CRASHREPORTERCLIENT_H
91  // Cast to void to avoid warning.
92  (void)CRSetCrashLogMessage(std::string(TmpStr.str()).c_str());
93 #elif HAVE_CRASHREPORTER_INFO
94  __crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str());
95 #endif
96  errs() << TmpStr.str();
97  }
98 
99 #endif
100 }
101 
103  // Link ourselves.
104  NextEntry = PrettyStackTraceHead->get();
105  PrettyStackTraceHead->set(this);
106 }
107 
109  // 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 &&
122  "Pretty stack trace entry destruction is out of order");
124 }
125 
127  OS << Str << "\n";
128 }
129 
131  OS << "Program arguments: ";
132  // Print the argument list.
133  for (unsigned i = 0, e = ArgC; i != e; ++i)
134  OS << ArgV[i] << ' ';
135  OS << '\n';
136 }
137 
138 static bool RegisterCrashPrinter() {
140  return false;
141 }
142 
144  // The first time this is called, we register the crash printer.
145  static bool HandlerRegistered = RegisterCrashPrinter();
146  (void)HandlerRegistered;
147 }
148 
151 }
raw_ostream & errs()
void EnablePrettyStackTrace()
const PrettyStackTraceEntry * getNextEntry() const
getNextEntry - Return the next entry in the list of frames.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:56
void LLVMEnablePrettyStackTrace()
static void PrintCurStackTrace(raw_ostream &OS)
PrintCurStackTrace - Print the current stack trace to the specified stream.
char *strdup(const char *s1);
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
Definition: Windows.h:154
static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS)
virtual void print(raw_ostream &OS) const LLVM_OVERRIDE
print - Emit information about this stack frame to OS.
virtual void print(raw_ostream &OS) const LLVM_OVERRIDE
print - Emit information about this stack frame to OS.
static bool RegisterCrashPrinter()
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:270
static ManagedStatic< sys::ThreadLocal< const PrettyStackTraceEntry > > PrettyStackTraceHead
virtual void print(raw_ostream &OS) const =0
print - Emit information about this stack frame to OS.
static void CrashHandler(void *)
void *__dso_handle __attribute__((__visibility__("hidden")))
void AddSignalHandler(void(*FnPtr)(void *), void *Cookie)