#include "llvm/Support/PrettyStackTrace.h"
#include "llvm-c/ErrorHandling.h"
#include "llvm/Config/config.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/SaveAndRestore.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/Watchdog.h"
#include "llvm/Support/raw_ostream.h"
#ifdef __APPLE__
#include "llvm/ADT/SmallString.h"
#endif
#include <atomic>
#include <cassert>
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <tuple>
#ifdef HAVE_CRASHREPORTERCLIENT_H
#include <CrashReporterClient.h>
#endif
using namespace llvm;
static const char *BugReportMsg =
"PLEASE submit a bug report to " BUG_REPORT_URL
" and include the crash backtrace.\n";
#if ENABLE_BACKTRACES
static LLVM_THREAD_LOCAL PrettyStackTraceEntry *PrettyStackTraceHead = nullptr;
static volatile std::atomic<unsigned> GlobalSigInfoGenerationCounter =
ATOMIC_VAR_INIT(1);
static LLVM_THREAD_LOCAL unsigned ThreadLocalSigInfoGenerationCounter = 0;
namespace llvm {
PrettyStackTraceEntry *ReverseStackTrace(PrettyStackTraceEntry *Head) {
PrettyStackTraceEntry *Prev = nullptr;
while (Head)
std::tie(Prev, Head, Head->NextEntry) =
std::make_tuple(Head, Head->NextEntry, Prev);
return Prev;
}
}
static void PrintStack(raw_ostream &OS) {
unsigned ID = 0;
SaveAndRestore<PrettyStackTraceEntry *> SavedStack{PrettyStackTraceHead,
nullptr};
PrettyStackTraceEntry *ReversedStack = ReverseStackTrace(SavedStack.get());
for (const PrettyStackTraceEntry *Entry = ReversedStack; Entry;
Entry = Entry->getNextEntry()) {
OS << ID++ << ".\t";
sys::Watchdog W(5);
Entry->print(OS);
}
llvm::ReverseStackTrace(ReversedStack);
}
LLVM_ATTRIBUTE_NOINLINE
static void PrintCurStackTrace(raw_ostream &OS) {
if (!PrettyStackTraceHead) return;
OS << "Stack dump:\n";
PrintStack(OS);
OS.flush();
}
#if defined (__APPLE__) && defined(HAVE_CRASHREPORTERCLIENT_H)
extern "C" {
CRASH_REPORTER_CLIENT_HIDDEN
struct crashreporter_annotations_t gCRAnnotations
__attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
#if CRASHREPORTER_ANNOTATIONS_VERSION < 5
= { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
#else
= { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0, 0 };
#endif
}
#elif defined(__APPLE__) && HAVE_CRASHREPORTER_INFO
extern "C" const char *__crashreporter_info__
__attribute__((visibility("hidden"))) = 0;
asm(".desc ___crashreporter_info__, 0x10");
#endif
static void setCrashLogMessage(const char *msg) LLVM_ATTRIBUTE_UNUSED;
static void setCrashLogMessage(const char *msg) {
#ifdef HAVE_CRASHREPORTERCLIENT_H
(void)CRSetCrashLogMessage(msg);
#elif HAVE_CRASHREPORTER_INFO
__crashreporter_info__ = msg;
#endif
std::atomic_signal_fence(std::memory_order_seq_cst);
}
#ifdef __APPLE__
using CrashHandlerString = SmallString<2048>;
using CrashHandlerStringStorage =
std::aligned_storage<sizeof(CrashHandlerString),
alignof(CrashHandlerString)>::type;
static CrashHandlerStringStorage crashHandlerStringStorage;
#endif
static void CrashHandler(void *) {
errs() << BugReportMsg ;
#ifndef __APPLE__
PrintCurStackTrace(errs());
#else
auto &crashHandlerString =
*new (&crashHandlerStringStorage) CrashHandlerString;
setCrashLogMessage(crashHandlerString.c_str());
{
raw_svector_ostream Stream(crashHandlerString);
PrintCurStackTrace(Stream);
}
if (!crashHandlerString.empty()) {
setCrashLogMessage(crashHandlerString.c_str());
errs() << crashHandlerString.str();
} else
setCrashLogMessage("No crash information.");
#endif
}
static void printForSigInfoIfNeeded() {
unsigned CurrentSigInfoGeneration =
GlobalSigInfoGenerationCounter.load(std::memory_order_relaxed);
if (ThreadLocalSigInfoGenerationCounter == 0 ||
ThreadLocalSigInfoGenerationCounter == CurrentSigInfoGeneration) {
return;
}
PrintCurStackTrace(errs());
ThreadLocalSigInfoGenerationCounter = CurrentSigInfoGeneration;
}
#endif
void llvm::setBugReportMsg(const char *Msg) {
BugReportMsg = Msg;
}
const char *llvm::getBugReportMsg() {
return BugReportMsg;
}
PrettyStackTraceEntry::PrettyStackTraceEntry() {
#if ENABLE_BACKTRACES
printForSigInfoIfNeeded();
NextEntry = PrettyStackTraceHead;
PrettyStackTraceHead = this;
#endif
}
PrettyStackTraceEntry::~PrettyStackTraceEntry() {
#if ENABLE_BACKTRACES
assert(PrettyStackTraceHead == this &&
"Pretty stack trace entry destruction is out of order");
PrettyStackTraceHead = NextEntry;
printForSigInfoIfNeeded();
#endif
}
void PrettyStackTraceString::print(raw_ostream &OS) const { OS << Str << "\n"; }
PrettyStackTraceFormat::PrettyStackTraceFormat(const char *Format, ...) {
va_list AP;
va_start(AP, Format);
const int SizeOrError = vsnprintf(nullptr, 0, Format, AP);
va_end(AP);
if (SizeOrError < 0) {
return;
}
const int Size = SizeOrError + 1; Str.resize(Size);
va_start(AP, Format);
vsnprintf(Str.data(), Size, Format, AP);
va_end(AP);
}
void PrettyStackTraceFormat::print(raw_ostream &OS) const { OS << Str << "\n"; }
void PrettyStackTraceProgram::print(raw_ostream &OS) const {
OS << "Program arguments: ";
for (int I = 0; I < ArgC; ++I) {
const bool HaveSpace = ::strchr(ArgV[I], ' ');
if (I)
OS << ' ';
if (HaveSpace)
OS << '"';
OS.write_escaped(ArgV[I]);
if (HaveSpace)
OS << '"';
}
OS << '\n';
}
#if ENABLE_BACKTRACES
static bool RegisterCrashPrinter() {
sys::AddSignalHandler(CrashHandler, nullptr);
return false;
}
#endif
void llvm::EnablePrettyStackTrace() {
#if ENABLE_BACKTRACES
static bool HandlerRegistered = RegisterCrashPrinter();
(void)HandlerRegistered;
#endif
}
void llvm::EnablePrettyStackTraceOnSigInfoForThisThread(bool ShouldEnable) {
#if ENABLE_BACKTRACES
if (!ShouldEnable) {
ThreadLocalSigInfoGenerationCounter = 0;
return;
}
static bool HandlerRegistered = []{
sys::SetInfoSignalFunction([]{
GlobalSigInfoGenerationCounter.fetch_add(1, std::memory_order_relaxed);
});
return false;
}();
(void)HandlerRegistered;
ThreadLocalSigInfoGenerationCounter =
GlobalSigInfoGenerationCounter.load(std::memory_order_relaxed);
#endif
}
const void *llvm::SavePrettyStackState() {
#if ENABLE_BACKTRACES
return PrettyStackTraceHead;
#else
return nullptr;
#endif
}
void llvm::RestorePrettyStackState(const void *Top) {
#if ENABLE_BACKTRACES
PrettyStackTraceHead =
static_cast<PrettyStackTraceEntry *>(const_cast<void *>(Top));
#endif
}
void LLVMEnablePrettyStackTrace() {
EnablePrettyStackTrace();
}