Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SignalHandling supports different printer #238

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions backward.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4044,15 +4044,16 @@ class Printer {
}
}

void print_header(std::ostream &os, size_t thread_id) {
protected:
virtual void print_header(std::ostream &os, size_t thread_id) {
os << "Stack trace (most recent call last)";
if (thread_id) {
os << " in thread " << thread_id;
}
os << ":\n";
}

void print_trace(std::ostream &os, const ResolvedTrace &trace,
virtual void print_trace(std::ostream &os, const ResolvedTrace &trace,
Colorize &colorize) {
os << "#" << std::left << std::setw(2) << trace.idx << std::right;
bool already_indented = true;
Expand Down Expand Up @@ -4090,7 +4091,7 @@ class Printer {
}
}

void print_snippet(std::ostream &os, const char *indent,
virtual void print_snippet(std::ostream &os, const char *indent,
const ResolvedTrace::SourceLoc &source_loc,
Colorize &colorize, Color::type color_code,
int context_size) {
Expand All @@ -4114,7 +4115,7 @@ class Printer {
}
}

void print_source_loc(std::ostream &os, const char *indent,
virtual void print_source_loc(std::ostream &os, const char *indent,
const ResolvedTrace::SourceLoc &source_loc,
void *addr = nullptr) {
os << indent << "Source \"" << source_loc.filename << "\", line "
Expand All @@ -4128,10 +4129,25 @@ class Printer {
};

/*************** SIGNALS HANDLING ***************/
class SignalHandlingBase {
public:
template <typename PT, typename = std::enable_if_t<std::is_convertible<PT*, Printer*>::value>>
static void set_printer() {
get_printer() = std::make_shared<PT>();
}
protected:
typedef std::shared_ptr<Printer> pPrinter;
static pPrinter& get_printer() {
static pPrinter printer;
if (!printer)
printer = std::make_shared<Printer>();
return printer;
}
};

#if defined(BACKWARD_SYSTEM_LINUX) || defined(BACKWARD_SYSTEM_DARWIN)

class SignalHandling {
class SignalHandling : SignalHandlingBase {
public:
static std::vector<int> make_default_signals() {
const int posix_signals[] = {
Expand Down Expand Up @@ -4241,9 +4257,9 @@ class SignalHandling {
st.load_here(32, reinterpret_cast<void *>(uctx), info->si_addr);
}

Printer printer;
printer.address = true;
printer.print(st, stderr);
pPrinter printer = get_printer();
printer->address = true;
printer->print(st, stderr);

#if _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
psiginfo(info, nullptr);
Expand Down Expand Up @@ -4276,7 +4292,7 @@ class SignalHandling {

#ifdef BACKWARD_SYSTEM_WINDOWS

class SignalHandling {
class SignalHandling : SignalHandlingBase {
public:
SignalHandling(const std::vector<int> & = std::vector<int>())
: reporter_thread_([]() {
Expand Down Expand Up @@ -4433,16 +4449,16 @@ class SignalHandling {
// macros.
// StackTrace also requires that the PDBs are already loaded, which is done
// in the constructor of TraceResolver
Printer printer;
pPrinter printer = get_printer();

StackTrace st;
st.set_machine_type(printer.resolver().machine_type());
st.set_machine_type(printer->resolver().machine_type());
st.set_thread_handle(thread_handle());
st.load_here(32 + skip_frames, ctx());
st.skip_n_firsts(skip_frames);

printer.address = true;
printer.print(st, std::cerr);
printer->address = true;
printer->print(st, std::cerr);
}
};

Expand Down