2009-03-04 21:40:23 +00:00
|
|
|
//===- PrettyStackTrace.cpp - Pretty Crash Handling -----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines some helpful functions for dealing with the possibility of
|
2011-04-15 05:18:47 +00:00
|
|
|
// Unix signals occurring while your program is running.
|
2009-03-04 21:40:23 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/Config/config.h" // Get autoconf configuration settings
|
2013-09-13 22:59:47 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2010-11-29 18:16:10 +00:00
|
|
|
#include "llvm/Support/Signals.h"
|
|
|
|
#include "llvm/Support/ThreadLocal.h"
|
2013-03-26 01:27:52 +00:00
|
|
|
#include "llvm/Support/Watchdog.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2013-11-03 00:29:47 +00:00
|
|
|
#include "llvm-c/Core.h"
|
2010-06-22 21:01:04 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CRASHREPORTERCLIENT_H
|
|
|
|
#include <CrashReporterClient.h>
|
|
|
|
#endif
|
|
|
|
|
2009-03-04 21:40:23 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2013-09-13 22:59:47 +00:00
|
|
|
static ManagedStatic<sys::ThreadLocal<const PrettyStackTraceEntry> > PrettyStackTraceHead;
|
2009-03-04 21:40:23 +00:00
|
|
|
|
2009-03-05 07:03:49 +00:00
|
|
|
static unsigned PrintStack(const PrettyStackTraceEntry *Entry, raw_ostream &OS){
|
|
|
|
unsigned NextID = 0;
|
|
|
|
if (Entry->getNextEntry())
|
|
|
|
NextID = PrintStack(Entry->getNextEntry(), OS);
|
|
|
|
OS << NextID << ".\t";
|
2013-03-26 01:27:52 +00:00
|
|
|
{
|
|
|
|
sys::Watchdog W(5);
|
|
|
|
Entry->print(OS);
|
|
|
|
}
|
2009-03-05 07:03:49 +00:00
|
|
|
|
|
|
|
return NextID+1;
|
|
|
|
}
|
|
|
|
|
2009-03-06 07:19:54 +00:00
|
|
|
/// PrintCurStackTrace - Print the current stack trace to the specified stream.
|
|
|
|
static void PrintCurStackTrace(raw_ostream &OS) {
|
2009-03-05 07:03:49 +00:00
|
|
|
// Don't print an empty trace.
|
2013-09-13 22:59:47 +00:00
|
|
|
if (PrettyStackTraceHead->get() == 0) return;
|
2009-03-05 07:03:49 +00:00
|
|
|
|
2009-03-04 21:40:23 +00:00
|
|
|
// If there are pretty stack frames registered, walk and emit them.
|
|
|
|
OS << "Stack dump:\n";
|
|
|
|
|
2013-09-13 22:59:47 +00:00
|
|
|
PrintStack(PrettyStackTraceHead->get(), OS);
|
2009-03-04 21:40:23 +00:00
|
|
|
OS.flush();
|
|
|
|
}
|
|
|
|
|
2010-06-28 18:25:51 +00:00
|
|
|
// Integrate with crash reporter libraries.
|
2010-12-03 07:45:22 +00:00
|
|
|
#if defined (__APPLE__) && HAVE_CRASHREPORTERCLIENT_H
|
2010-06-28 18:25:51 +00:00
|
|
|
// If any clients of llvm try to link to libCrashReporterClient.a themselves,
|
|
|
|
// only one crash info struct will be used.
|
2010-06-28 18:33:48 +00:00
|
|
|
extern "C" {
|
2010-06-28 18:25:51 +00:00
|
|
|
CRASH_REPORTER_CLIENT_HIDDEN
|
|
|
|
struct crashreporter_annotations_t gCRAnnotations
|
|
|
|
__attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
|
2011-10-05 05:00:26 +00:00
|
|
|
= { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
|
2010-06-28 18:33:48 +00:00
|
|
|
}
|
2010-12-03 07:45:22 +00:00
|
|
|
#elif defined (__APPLE__) && HAVE_CRASHREPORTER_INFO
|
2010-05-20 23:50:19 +00:00
|
|
|
static const char *__crashreporter_info__ = 0;
|
|
|
|
asm(".desc ___crashreporter_info__, 0x10");
|
2009-03-06 07:19:54 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/// CrashHandler - This callback is run if a fatal signal is delivered to the
|
|
|
|
/// process, it prints the pretty stack trace.
|
2010-08-08 00:00:34 +00:00
|
|
|
static void CrashHandler(void *) {
|
2009-03-06 07:19:54 +00:00
|
|
|
#ifndef __APPLE__
|
|
|
|
// On non-apple systems, just emit the crash stack trace to stderr.
|
|
|
|
PrintCurStackTrace(errs());
|
|
|
|
#else
|
|
|
|
// Otherwise, emit to a smallvector of chars, send *that* to stderr, but also
|
|
|
|
// put it into __crashreporter_info__.
|
|
|
|
SmallString<2048> TmpStr;
|
|
|
|
{
|
|
|
|
raw_svector_ostream Stream(TmpStr);
|
|
|
|
PrintCurStackTrace(Stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!TmpStr.empty()) {
|
2010-12-03 07:45:22 +00:00
|
|
|
#ifdef HAVE_CRASHREPORTERCLIENT_H
|
2010-08-08 00:00:34 +00:00
|
|
|
// Cast to void to avoid warning.
|
|
|
|
(void)CRSetCrashLogMessage(std::string(TmpStr.str()).c_str());
|
2010-12-03 07:45:22 +00:00
|
|
|
#elif HAVE_CRASHREPORTER_INFO
|
|
|
|
__crashreporter_info__ = strdup(std::string(TmpStr.str()).c_str());
|
2010-06-22 21:01:04 +00:00
|
|
|
#endif
|
2009-08-19 20:07:03 +00:00
|
|
|
errs() << TmpStr.str();
|
2009-03-06 07:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-03-04 21:40:23 +00:00
|
|
|
PrettyStackTraceEntry::PrettyStackTraceEntry() {
|
|
|
|
// Link ourselves.
|
2013-09-13 22:59:47 +00:00
|
|
|
NextEntry = PrettyStackTraceHead->get();
|
|
|
|
PrettyStackTraceHead->set(this);
|
2009-03-04 21:40:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PrettyStackTraceEntry::~PrettyStackTraceEntry() {
|
2013-09-13 22:59:47 +00:00
|
|
|
// Do nothing if PrettyStackTraceHead is uninitialized. This can only happen
|
|
|
|
// if a shutdown occurred after we created the PrettyStackTraceEntry. That
|
|
|
|
// does occur in the following idiom:
|
|
|
|
//
|
|
|
|
// PrettyStackTraceProgram X(...);
|
|
|
|
// llvm_shutdown_obj Y;
|
|
|
|
//
|
|
|
|
// Without this check, we may end up removing ourselves from the stack trace
|
|
|
|
// after PrettyStackTraceHead has already been destroyed.
|
|
|
|
if (!PrettyStackTraceHead.isConstructed())
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(PrettyStackTraceHead->get() == this &&
|
2009-03-04 21:40:23 +00:00
|
|
|
"Pretty stack trace entry destruction is out of order");
|
2013-09-13 22:59:47 +00:00
|
|
|
PrettyStackTraceHead->set(getNextEntry());
|
2009-03-04 21:40:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrettyStackTraceString::print(raw_ostream &OS) const {
|
|
|
|
OS << Str << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrettyStackTraceProgram::print(raw_ostream &OS) const {
|
2009-03-05 06:51:42 +00:00
|
|
|
OS << "Program arguments: ";
|
2009-03-04 21:40:23 +00:00
|
|
|
// Print the argument list.
|
|
|
|
for (unsigned i = 0, e = ArgC; i != e; ++i)
|
|
|
|
OS << ArgV[i] << ' ';
|
|
|
|
OS << '\n';
|
2009-03-05 06:51:42 +00:00
|
|
|
}
|
2013-11-03 00:29:47 +00:00
|
|
|
|
2013-11-04 02:22:25 +00:00
|
|
|
static bool RegisterCrashPrinter() {
|
|
|
|
sys::AddSignalHandler(CrashHandler, 0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void llvm::EnablePrettyStackTrace() {
|
|
|
|
// The first time this is called, we register the crash printer.
|
|
|
|
static bool HandlerRegistered = RegisterCrashPrinter();
|
|
|
|
(void)HandlerRegistered;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMEnablePrettyStackTrace() {
|
|
|
|
EnablePrettyStackTrace();
|
2013-11-03 00:29:47 +00:00
|
|
|
}
|