16#include "llvm/Config/config.h"
34#if __has_include(<CrashReporterClient.h>)
35#include <CrashReporterClient.h>
36#define LLVM_HAVE_CRASHREPORTERCLIENT_H
42 "PLEASE submit a bug report to " BUG_REPORT_URL
43 " and include the crash backtrace and instructions to reproduce the bug.\n";
68static volatile std::atomic<unsigned> GlobalSigInfoGenerationCounter = 1;
75 std::tie(Prev, Head, Head->NextEntry) =
76 std::make_tuple(Head, Head->NextEntry, Prev);
95 llvm::ReverseStackTrace(ReversedStack);
104 if (!PrettyStackTraceHead)
return;
107 OS <<
"Stack dump:\n";
114#if defined(__APPLE__) && defined(LLVM_HAVE_CRASHREPORTERCLIENT_H)
118#ifdef CRASHREPORTER_ANNOTATIONS_INITIALIZER
120CRASHREPORTER_ANNOTATIONS_INITIALIZER()
123CRASH_REPORTER_CLIENT_HIDDEN
124struct crashreporter_annotations_t gCRAnnotations
125 __attribute__((section(
"__DATA," CRASHREPORTER_ANNOTATIONS_SECTION))) = {
126 CRASHREPORTER_ANNOTATIONS_VERSION,
133#if CRASHREPORTER_ANNOTATIONS_VERSION > 4
139#elif defined(__APPLE__) && HAVE_CRASHREPORTER_INFO
140extern "C" const char *__crashreporter_info__
141 __attribute__((visibility(
"hidden"))) = 0;
142asm(
".desc ___crashreporter_info__, 0x10");
145[[maybe_unused]]
static void setCrashLogMessage(
const char *msg);
146static void setCrashLogMessage(
const char *msg) {
147#ifdef LLVM_HAVE_CRASHREPORTERCLIENT_H
148 (void)CRSetCrashLogMessage(msg);
149#elif HAVE_CRASHREPORTER_INFO
150 __crashreporter_info__ = msg;
154 std::atomic_signal_fence(std::memory_order_seq_cst);
159using CrashHandlerStringStorage = std::byte[
sizeof(CrashHandlerString)];
160alignas(CrashHandlerString)
static CrashHandlerStringStorage
161 crashHandlerStringStorage;
166static void CrashHandler(
void *) {
171 PrintCurStackTrace(
errs());
183 auto &crashHandlerString =
184 *
new (&crashHandlerStringStorage) CrashHandlerString;
191 setCrashLogMessage(crashHandlerString.c_str());
195 PrintCurStackTrace(Stream);
198 if (!crashHandlerString.empty()) {
199 setCrashLogMessage(crashHandlerString.c_str());
200 errs() << crashHandlerString.str();
202 setCrashLogMessage(
"No crash information.");
207static void printForSigInfoIfNeeded() {
208 unsigned CurrentSigInfoGeneration =
209 GlobalSigInfoGenerationCounter.load(std::memory_order_relaxed);
210 if (ThreadLocalSigInfoGenerationCounter == 0 ||
211 ThreadLocalSigInfoGenerationCounter == CurrentSigInfoGeneration) {
215 PrintCurStackTrace(
errs());
216 ThreadLocalSigInfoGenerationCounter = CurrentSigInfoGeneration;
232 printForSigInfoIfNeeded();
234 NextEntry = PrettyStackTraceHead;
235 PrettyStackTraceHead =
this;
241 assert(PrettyStackTraceHead ==
this &&
242 "Pretty stack trace entry destruction is out of order");
243 PrettyStackTraceHead = NextEntry;
245 printForSigInfoIfNeeded();
254 const int SizeOrError = vsnprintf(
nullptr, 0,
Format, AP);
256 if (SizeOrError < 0) {
260 const int Size = SizeOrError + 1;
270 OS <<
"Program arguments: ";
272 for (
int I = 0;
I < ArgC; ++
I) {
273 const bool HaveSpace = ::strchr(ArgV[
I],
' ');
286static bool RegisterCrashPrinter() {
295 static bool HandlerRegistered = RegisterCrashPrinter();
296 (void)HandlerRegistered;
303 ThreadLocalSigInfoGenerationCounter = 0;
308 static bool HandlerRegistered = []{
310 GlobalSigInfoGenerationCounter.fetch_add(1, std::memory_order_relaxed);
314 (void)HandlerRegistered;
317 ThreadLocalSigInfoGenerationCounter =
318 GlobalSigInfoGenerationCounter.load(std::memory_order_relaxed);
324 return PrettyStackTraceHead;
332 PrettyStackTraceHead =
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_THREAD_LOCAL
\macro LLVM_THREAD_LOCAL A thread-local storage specifier which can be used with globals,...
#define LLVM_ATTRIBUTE_NOINLINE
LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so, mark a method "not for inl...
static const char * BugReportMsg
This file provides utility classes that use RAII to save and restore values.
This file defines the SmallString class.
PrettyStackTraceEntry - This class is used to represent a frame of the "pretty" stack trace that is d...
virtual ~PrettyStackTraceEntry()
const PrettyStackTraceEntry * getNextEntry() const
getNextEntry - Return the next entry in the list of frames.
void print(raw_ostream &OS) const override
print - Emit information about this stack frame to OS.
void print(raw_ostream &OS) const override
print - Emit information about this stack frame to OS.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
This class implements an extremely fast bulk output stream that can only output to a stream.
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '\t', ' ', '"', and anything that doesn't satisfy llvm::isPrint into an esca...
A raw_ostream that writes to an SmallVector or SmallString.
This class provides an abstraction for a timeout around an operation that must complete in a given am...
LLVM_C_ABI void LLVMEnablePrettyStackTrace(void)
Enable LLVM's built-in stack trace code.
LLVM_ABI void AddSignalHandler(SignalHandlerCallback FnPtr, void *Cookie, bool NeedsPOSIXUtilitySignalHandling=false)
Add a function to be called when an abort/kill signal is delivered to the process.
LLVM_ABI void SetInfoSignalFunction(void(*Handler)())
Registers a function to be called when an "info" signal is delivered to the process.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void setBugReportMsg(const char *Msg)
Replaces the generic bug report message that is output upon a crash.
LLVM_ABI const void * SavePrettyStackState()
Returns the topmost element of the "pretty" stack state.
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_ABI void RestorePrettyStackState(const void *State)
Restores the topmost element of the "pretty" stack state to State, which should come from a previous ...
LLVM_ABI void EnablePrettyStackTraceOnSigInfoForThisThread(bool ShouldEnable=true)
Enables (or disables) dumping a "pretty" stack trace when the user sends SIGINFO or SIGUSR1 to the cu...
LLVM_ABI void EnablePrettyStackTrace()
Enables dumping a "pretty" stack trace when the program crashes.
LLVM_ABI const char * getBugReportMsg()
Get the bug report message that will be output upon a crash.
A utility class that uses RAII to save and restore the value of a variable.