Skip to content

Commit

Permalink
Prepare PrettyStackTrace for LLDB adoption
Browse files Browse the repository at this point in the history
This patch fixes the linkage for __crashtracer_info__, making it have the proper mangling (extern "C") and linkage (private extern).
It also adds a new PrettyStackTrace type, allowing LLDB to adopt this instead of Host::SetCrashDescriptionWithFormat().

Without this patch, CrashTracer on macOS won't pick up pretty stack traces from any LLVM client. 
An LLDB commit adopting this API will follow shortly.

Differential Revision: https://reviews.llvm.org/D27683

llvm-svn: 289689
  • Loading branch information
scallanan committed Dec 14, 2016
1 parent 373e36a commit 032dbf9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
11 changes: 11 additions & 0 deletions llvm/include/llvm/Support/PrettyStackTrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#ifndef LLVM_SUPPORT_PRETTYSTACKTRACE_H
#define LLVM_SUPPORT_PRETTYSTACKTRACE_H

#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Compiler.h"

namespace llvm {
Expand Down Expand Up @@ -55,6 +56,16 @@ namespace llvm {
void print(raw_ostream &OS) const override;
};

/// PrettyStackTraceFormat - This object prints a string (which may use
/// printf-style formatting but should not contain newlines) to the stream
/// as the stack trace when a crash occurs.
class PrettyStackTraceFormat : public PrettyStackTraceEntry {
llvm::SmallVector<char, 32> Str;
public:
PrettyStackTraceFormat(const char *Format, ...);
void print(raw_ostream &OS) const override;
};

/// PrettyStackTraceProgram - This object prints a specified program arguments
/// to the stream as the stack trace when a crash occurs.
class PrettyStackTraceProgram : public PrettyStackTraceEntry {
Expand Down
26 changes: 21 additions & 5 deletions llvm/lib/Support/PrettyStackTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ struct crashreporter_annotations_t gCRAnnotations
__attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION)))
= { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 };
}
#elif defined (__APPLE__) && HAVE_CRASHREPORTER_INFO
static const char *__crashreporter_info__ = 0;
#elif defined(__APPLE__) && HAVE_CRASHREPORTER_INFO
extern "C" const char *__crashreporter_info__
__attribute__((visibility("hidden"))) = 0;
asm(".desc ___crashreporter_info__, 0x10");
#endif


/// CrashHandler - This callback is run if a fatal signal is delivered to the
/// process, it prints the pretty stack trace.
static void CrashHandler(void *) {
Expand Down Expand Up @@ -141,10 +141,26 @@ PrettyStackTraceEntry::~PrettyStackTraceEntry() {
#endif
}

void PrettyStackTraceString::print(raw_ostream &OS) const {
OS << Str << "\n";
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; // '\0'
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: ";
// Print the argument list.
Expand Down

0 comments on commit 032dbf9

Please sign in to comment.