1 //===- PrettyStackTrace.cpp - Pretty Crash Handling -----------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines some helpful functions for dealing with the possibility of
10 // Unix signals occurring while your program is running.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/PrettyStackTrace.h"
15 #include "llvm-c/ErrorHandling.h"
16 #include "llvm/Config/config.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/SaveAndRestore.h"
19 #include "llvm/Support/Signals.h"
20 #include "llvm/Support/Watchdog.h"
21 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/ADT/SmallString.h"
34 #ifdef HAVE_CRASHREPORTERCLIENT_H
35 #include <CrashReporterClient.h>
40 static const char *BugReportMsg
=
41 "PLEASE submit a bug report to " BUG_REPORT_URL
42 " and include the crash backtrace.\n";
44 // If backtrace support is not enabled, compile out support for pretty stack
45 // traces. This has the secondary effect of not requiring thread local storage
46 // when backtrace support is disabled.
49 // We need a thread local pointer to manage the stack of our stack trace
50 // objects, but we *really* cannot tolerate destructors running and do not want
51 // to pay any overhead of synchronizing. As a consequence, we use a raw
52 // thread-local variable.
53 static LLVM_THREAD_LOCAL PrettyStackTraceEntry
*PrettyStackTraceHead
= nullptr;
55 // The use of 'volatile' here is to ensure that any particular thread always
56 // reloads the value of the counter. The 'std::atomic' allows us to specify that
57 // this variable is accessed in an unsychronized way (it's not actually
58 // synchronizing). This does technically mean that the value may not appear to
59 // be the same across threads running simultaneously on different CPUs, but in
60 // practice the worst that will happen is that we won't print a stack trace when
63 // This is initialized to 1 because 0 is used as a sentinel for "not enabled on
64 // the current thread". If the user happens to overflow an 'unsigned' with
65 // SIGINFO requests, it's possible that some threads will stop responding to it,
66 // but the program won't crash.
67 static volatile std::atomic
<unsigned> GlobalSigInfoGenerationCounter
= 1;
68 static LLVM_THREAD_LOCAL
unsigned ThreadLocalSigInfoGenerationCounter
= 0;
71 PrettyStackTraceEntry
*ReverseStackTrace(PrettyStackTraceEntry
*Head
) {
72 PrettyStackTraceEntry
*Prev
= nullptr;
74 std::tie(Prev
, Head
, Head
->NextEntry
) =
75 std::make_tuple(Head
, Head
->NextEntry
, Prev
);
80 static void PrintStack(raw_ostream
&OS
) {
81 // Print out the stack in reverse order. To avoid recursion (which is likely
82 // to fail if we crashed due to stack overflow), we do an up-front pass to
83 // reverse the stack, then print it, then reverse it again.
85 SaveAndRestore
<PrettyStackTraceEntry
*> SavedStack
{PrettyStackTraceHead
,
87 PrettyStackTraceEntry
*ReversedStack
= ReverseStackTrace(SavedStack
.get());
88 for (const PrettyStackTraceEntry
*Entry
= ReversedStack
; Entry
;
89 Entry
= Entry
->getNextEntry()) {
94 llvm::ReverseStackTrace(ReversedStack
);
97 /// Print the current stack trace to the specified stream.
99 /// Marked NOINLINE so it can be called from debuggers.
100 LLVM_ATTRIBUTE_NOINLINE
101 static void PrintCurStackTrace(raw_ostream
&OS
) {
102 // Don't print an empty trace.
103 if (!PrettyStackTraceHead
) return;
105 // If there are pretty stack frames registered, walk and emit them.
106 OS
<< "Stack dump:\n";
112 // Integrate with crash reporter libraries.
113 #if defined (__APPLE__) && defined(HAVE_CRASHREPORTERCLIENT_H)
114 // If any clients of llvm try to link to libCrashReporterClient.a themselves,
115 // only one crash info struct will be used.
117 CRASH_REPORTER_CLIENT_HIDDEN
118 struct crashreporter_annotations_t gCRAnnotations
119 __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION
)))
120 #if CRASHREPORTER_ANNOTATIONS_VERSION < 5
121 = { CRASHREPORTER_ANNOTATIONS_VERSION
, 0, 0, 0, 0, 0, 0 };
123 = { CRASHREPORTER_ANNOTATIONS_VERSION
, 0, 0, 0, 0, 0, 0, 0 };
126 #elif defined(__APPLE__) && HAVE_CRASHREPORTER_INFO
127 extern "C" const char *__crashreporter_info__
128 __attribute__((visibility("hidden"))) = 0;
129 asm(".desc ___crashreporter_info__, 0x10");
132 static void setCrashLogMessage(const char *msg
) LLVM_ATTRIBUTE_UNUSED
;
133 static void setCrashLogMessage(const char *msg
) {
134 #ifdef HAVE_CRASHREPORTERCLIENT_H
135 (void)CRSetCrashLogMessage(msg
);
136 #elif HAVE_CRASHREPORTER_INFO
137 __crashreporter_info__
= msg
;
139 // Don't reorder subsequent operations: whatever comes after might crash and
140 // we want the system crash handling to see the message we just set.
141 std::atomic_signal_fence(std::memory_order_seq_cst
);
145 using CrashHandlerString
= SmallString
<2048>;
146 using CrashHandlerStringStorage
=
147 std::aligned_storage
<sizeof(CrashHandlerString
),
148 alignof(CrashHandlerString
)>::type
;
149 static CrashHandlerStringStorage crashHandlerStringStorage
;
152 /// This callback is run if a fatal signal is delivered to the process, it
153 /// prints the pretty stack trace.
154 static void CrashHandler(void *) {
155 errs() << BugReportMsg
;
158 // On non-apple systems, just emit the crash stack trace to stderr.
159 PrintCurStackTrace(errs());
161 // Emit the crash stack trace to a SmallString, put it where the system crash
162 // handling will find it, and also send it to stderr.
164 // The SmallString is fairly large in the hope that we don't allocate (we're
165 // handling a fatal signal, something is already pretty wrong, allocation
166 // might not work). Further, we don't use a magic static in case that's also
167 // borked. We leak any allocation that does occur because the program is about
168 // to die anyways. This is technically racy if we were handling two fatal
169 // signals, however if we're in that situation a race is the least of our
171 auto &crashHandlerString
=
172 *new (&crashHandlerStringStorage
) CrashHandlerString
;
174 // If we crash while trying to print the stack trace, we still want the system
175 // crash handling to have some partial information. That'll work out as long
176 // as the SmallString doesn't allocate. If it does allocate then the system
177 // crash handling will see some garbage because the inline buffer now contains
179 setCrashLogMessage(crashHandlerString
.c_str());
182 raw_svector_ostream
Stream(crashHandlerString
);
183 PrintCurStackTrace(Stream
);
186 if (!crashHandlerString
.empty()) {
187 setCrashLogMessage(crashHandlerString
.c_str());
188 errs() << crashHandlerString
.str();
190 setCrashLogMessage("No crash information.");
194 static void printForSigInfoIfNeeded() {
195 unsigned CurrentSigInfoGeneration
=
196 GlobalSigInfoGenerationCounter
.load(std::memory_order_relaxed
);
197 if (ThreadLocalSigInfoGenerationCounter
== 0 ||
198 ThreadLocalSigInfoGenerationCounter
== CurrentSigInfoGeneration
) {
202 PrintCurStackTrace(errs());
203 ThreadLocalSigInfoGenerationCounter
= CurrentSigInfoGeneration
;
206 #endif // ENABLE_BACKTRACES
208 void llvm::setBugReportMsg(const char *Msg
) {
212 const char *llvm::getBugReportMsg() {
216 PrettyStackTraceEntry::PrettyStackTraceEntry() {
217 #if ENABLE_BACKTRACES
218 // Handle SIGINFO first, because we haven't finished constructing yet.
219 printForSigInfoIfNeeded();
221 NextEntry
= PrettyStackTraceHead
;
222 PrettyStackTraceHead
= this;
226 PrettyStackTraceEntry::~PrettyStackTraceEntry() {
227 #if ENABLE_BACKTRACES
228 assert(PrettyStackTraceHead
== this &&
229 "Pretty stack trace entry destruction is out of order");
230 PrettyStackTraceHead
= NextEntry
;
231 // Handle SIGINFO first, because we already started destructing.
232 printForSigInfoIfNeeded();
236 void PrettyStackTraceString::print(raw_ostream
&OS
) const { OS
<< Str
<< "\n"; }
238 PrettyStackTraceFormat::PrettyStackTraceFormat(const char *Format
, ...) {
240 va_start(AP
, Format
);
241 const int SizeOrError
= vsnprintf(nullptr, 0, Format
, AP
);
243 if (SizeOrError
< 0) {
247 const int Size
= SizeOrError
+ 1; // '\0'
249 va_start(AP
, Format
);
250 vsnprintf(Str
.data(), Size
, Format
, AP
);
254 void PrettyStackTraceFormat::print(raw_ostream
&OS
) const { OS
<< Str
<< "\n"; }
256 void PrettyStackTraceProgram::print(raw_ostream
&OS
) const {
257 OS
<< "Program arguments: ";
258 // Print the argument list.
259 for (int I
= 0; I
< ArgC
; ++I
) {
260 const bool HaveSpace
= ::strchr(ArgV
[I
], ' ');
265 OS
.write_escaped(ArgV
[I
]);
272 #if ENABLE_BACKTRACES
273 static bool RegisterCrashPrinter() {
274 sys::AddSignalHandler(CrashHandler
, nullptr);
279 void llvm::EnablePrettyStackTrace() {
280 #if ENABLE_BACKTRACES
281 // The first time this is called, we register the crash printer.
282 static bool HandlerRegistered
= RegisterCrashPrinter();
283 (void)HandlerRegistered
;
287 void llvm::EnablePrettyStackTraceOnSigInfoForThisThread(bool ShouldEnable
) {
288 #if ENABLE_BACKTRACES
290 ThreadLocalSigInfoGenerationCounter
= 0;
294 // The first time this is called, we register the SIGINFO handler.
295 static bool HandlerRegistered
= []{
296 sys::SetInfoSignalFunction([]{
297 GlobalSigInfoGenerationCounter
.fetch_add(1, std::memory_order_relaxed
);
301 (void)HandlerRegistered
;
303 // Next, enable it for the current thread.
304 ThreadLocalSigInfoGenerationCounter
=
305 GlobalSigInfoGenerationCounter
.load(std::memory_order_relaxed
);
309 const void *llvm::SavePrettyStackState() {
310 #if ENABLE_BACKTRACES
311 return PrettyStackTraceHead
;
317 void llvm::RestorePrettyStackState(const void *Top
) {
318 #if ENABLE_BACKTRACES
319 PrettyStackTraceHead
=
320 static_cast<PrettyStackTraceEntry
*>(const_cast<void *>(Top
));
324 void LLVMEnablePrettyStackTrace() {
325 EnablePrettyStackTrace();