bombela / backward-cpp

A beautiful stack trace pretty printer for C++
MIT License
3.81k stars 479 forks source link

Adding new load_saved function #344

Open BritBlaster opened 1 month ago

BritBlaster commented 1 month ago

Adding new load_saved function to allow loading a previously obtained stacktrace to be printed.

Currently, there does not appear to be any way to use a stacktrace that might have been obtained earlier (e.g. by backtrace). This is particularly relevant if a signal handler has captured a stacktrace, which might want to be printed at a later time.

Example use:

constexpr int max_stackdepth_for_trace = 128;
void *saved_st[max_stackdepth_for_trace];
int SavedStacktraceDepth;

SavedStacktraceDepth = backtrace(saved_st, max_stackdepth_for_trace);

[later]
StackTrace st;
st.load_saved(saved_st, SavedStacktraceDepth);
TraceResolver tr;
tr.load_stacktrace(st);

backward::Printer p;
std::stringstream &ss
p.snippet    = EnableVerboseBackTraces;
p.object     = EnableVerboseBackTraces;
p.address    = true;
p.color_mode = backward::ColorMode::always;

p.print(st, ss);