xref: /llvm-project/llvm/lib/Support/PrettyStackTrace.cpp (revision 2946cd701067404b99c39fb29dc9c74bd7193eb3)
1 //===- PrettyStackTrace.cpp - Pretty Crash Handling -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines some helpful functions for dealing with the possibility of
10 // Unix signals occurring while your program is running.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Support/PrettyStackTrace.h"
15 #include "llvm-c/ErrorHandling.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/Config/config.h" // Get autoconf configuration settings
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/Signals.h"
20 #include "llvm/Support/Watchdog.h"
21 #include "llvm/Support/raw_ostream.h"
22 
23 #include <cstdarg>
24 #include <cstdio>
25 #include <tuple>
26 
27 #ifdef HAVE_CRASHREPORTERCLIENT_H
28 #include <CrashReporterClient.h>
29 #endif
30 
31 using namespace llvm;
32 
33 // If backtrace support is not enabled, compile out support for pretty stack
34 // traces.  This has the secondary effect of not requiring thread local storage
35 // when backtrace support is disabled.
36 #if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
37 
38 // We need a thread local pointer to manage the stack of our stack trace
39 // objects, but we *really* cannot tolerate destructors running and do not want
40 // to pay any overhead of synchronizing. As a consequence, we use a raw
41 // thread-local variable.
42 static LLVM_THREAD_LOCAL PrettyStackTraceEntry *PrettyStackTraceHead = nullptr;
43 
44 namespace llvm {
45 PrettyStackTraceEntry *ReverseStackTrace(PrettyStackTraceEntry *Head) {
46   PrettyStackTraceEntry *Prev = nullptr;
47   while (Head)
48     std::tie(Prev, Head, Head->NextEntry) =
49         std::make_tuple(Head, Head->NextEntry, Prev);
50   return Prev;
51 }
52 }
53 
54 static void PrintStack(raw_ostream &OS) {
55   // Print out the stack in reverse order. To avoid recursion (which is likely
56   // to fail if we crashed due to stack overflow), we do an up-front pass to
57   // reverse the stack, then print it, then reverse it again.
58   unsigned ID = 0;
59   PrettyStackTraceEntry *ReversedStack =
60       llvm::ReverseStackTrace(PrettyStackTraceHead);
61   for (const PrettyStackTraceEntry *Entry = ReversedStack; Entry;
62        Entry = Entry->getNextEntry()) {
63     OS << ID++ << ".\t";
64     sys::Watchdog W(5);
65     Entry->print(OS);
66   }
67   llvm::ReverseStackTrace(ReversedStack);
68 }
69 
70 /// PrintCurStackTrace - Print the current stack trace to the specified stream.
71 static void PrintCurStackTrace(raw_ostream &OS) {
72   // Don't print an empty trace.
73   if (!PrettyStackTraceHead) return;
74 
75   // If there are pretty stack frames registered, walk and emit them.
76   OS << "Stack dump:\n";
77 
78   PrintStack(OS);
79   OS.flush();
80 }
81 
82 // Integrate with crash reporter libraries.
83 #if defined (__APPLE__) && defined(HAVE_CRASHREPORTERCLIENT_H)
84 //  If any clients of llvm try to link to libCrashReporterClient.a themselves,
85 //  only one crash info struct will be used.
86 extern "C" {
87 CRASH_REPORTER_CLIENT_HIDDEN
88 struct crashreporter_annotations_t gCRAnnotations
89         __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
90 #if CRASHREPORTER_ANNOTATIONS_VERSION < 5
91         = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
92 #else
93         = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0, 0 };
94 #endif
95 }
96 #elif defined(__APPLE__) && HAVE_CRASHREPORTER_INFO
97 extern "C" const char *__crashreporter_info__
98     __attribute__((visibility("hidden"))) = 0;
99 asm(".desc ___crashreporter_info__, 0x10");
100 #endif
101 
102 /// CrashHandler - This callback is run if a fatal signal is delivered to the
103 /// process, it prints the pretty stack trace.
104 static void CrashHandler(void *) {
105 #ifndef __APPLE__
106   // On non-apple systems, just emit the crash stack trace to stderr.
107   PrintCurStackTrace(errs());
108 #else
109   // Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
110   // put it into __crashreporter_info__.
111   SmallString<2048> TmpStr;
112   {
113     raw_svector_ostream Stream(TmpStr);
114     PrintCurStackTrace(Stream);
115   }
116 
117   if (!TmpStr.empty()) {
118 #ifdef HAVE_CRASHREPORTERCLIENT_H
119     // Cast to void to avoid warning.
120     (void)CRSetCrashLogMessage(TmpStr.c_str());
121 #elif HAVE_CRASHREPORTER_INFO
122     __crashreporter_info__ = strdup(TmpStr.c_str());
123 #endif
124     errs() << TmpStr.str();
125   }
126 
127 #endif
128 }
129 
130 // defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
131 #endif
132 
133 PrettyStackTraceEntry::PrettyStackTraceEntry() {
134 #if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
135   // Link ourselves.
136   NextEntry = PrettyStackTraceHead;
137   PrettyStackTraceHead = this;
138 #endif
139 }
140 
141 PrettyStackTraceEntry::~PrettyStackTraceEntry() {
142 #if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
143   assert(PrettyStackTraceHead == this &&
144          "Pretty stack trace entry destruction is out of order");
145   PrettyStackTraceHead = NextEntry;
146 #endif
147 }
148 
149 void PrettyStackTraceString::print(raw_ostream &OS) const { OS << Str << "\n"; }
150 
151 PrettyStackTraceFormat::PrettyStackTraceFormat(const char *Format, ...) {
152   va_list AP;
153   va_start(AP, Format);
154   const int SizeOrError = vsnprintf(nullptr, 0, Format, AP);
155   va_end(AP);
156   if (SizeOrError < 0) {
157     return;
158   }
159 
160   const int Size = SizeOrError + 1; // '\0'
161   Str.resize(Size);
162   va_start(AP, Format);
163   vsnprintf(Str.data(), Size, Format, AP);
164   va_end(AP);
165 }
166 
167 void PrettyStackTraceFormat::print(raw_ostream &OS) const { OS << Str << "\n"; }
168 
169 void PrettyStackTraceProgram::print(raw_ostream &OS) const {
170   OS << "Program arguments: ";
171   // Print the argument list.
172   for (unsigned i = 0, e = ArgC; i != e; ++i)
173     OS << ArgV[i] << ' ';
174   OS << '\n';
175 }
176 
177 #if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
178 static bool RegisterCrashPrinter() {
179   sys::AddSignalHandler(CrashHandler, nullptr);
180   return false;
181 }
182 #endif
183 
184 void llvm::EnablePrettyStackTrace() {
185 #if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
186   // The first time this is called, we register the crash printer.
187   static bool HandlerRegistered = RegisterCrashPrinter();
188   (void)HandlerRegistered;
189 #endif
190 }
191 
192 const void *llvm::SavePrettyStackState() {
193 #if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
194   return PrettyStackTraceHead;
195 #else
196   return nullptr;
197 #endif
198 }
199 
200 void llvm::RestorePrettyStackState(const void *Top) {
201 #if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES
202   PrettyStackTraceHead =
203       static_cast<PrettyStackTraceEntry *>(const_cast<void *>(Top));
204 #endif
205 }
206 
207 void LLVMEnablePrettyStackTrace() {
208   EnablePrettyStackTrace();
209 }
210