13 #ifndef LARCOREALG_COREUTILS_DEBUGUTILS_H 14 #define LARCOREALG_COREUTILS_DEBUGUTILS_H 20 #include "cetlib_except/demangle.h" 64 using range_t = std::pair<size_t, size_t>;
88 return (sep == std::string::npos)
107 {
return emptyRange(r)?
"": s.substr(r.first, r.second - r.first); }
111 { functionName = cet::demangle_symbol(mangledFunctionName); }
150 { options.set(o,
set);
return *
this; }
169 template <
typename Stream>
173 template <
typename Stream>
175 { print(std::forward<Stream>(out), info); }
184 options.
set(opt::demangled);
185 options.
set(opt::library);
186 options.
set(opt::shortLibrary);
187 options.
set(opt::address);
196 template <
typename Stream>
204 unsigned int maxLines = 5;
205 unsigned int skipLines = 1;
206 bool countOthers =
true;
215 { indent = firstIndent = uniformIndent; }
227 template <
typename Stream>
235 template <
typename Stream>
251 template <
typename Stream>
331 template <
typename T,
bool Enable >
347 template <
typename T>
349 {
return cet::demangle_symbol(
typeid(std::decay_t<T>).
name()); }
353 template <
typename Stream>
361 if (
options.has(opt::library)) {
365 else out <<
"unknown";
373 if (
offset > 0) out <<
'+';
387 out <<
" at " << ((
void*) info.
address);
393 template <
typename Stream>
396 print(std::forward<Stream>(out), info);
402 template <
typename Stream>
405 std::vector<void*> buffer
408 unsigned int const nItems
409 = (
unsigned int) backtrace(buffer.data(), buffer.size());
412 char** symbols = backtrace_symbols(buffer.data(), buffer.size());
414 out << options.
firstIndent <<
"<failed to get the call stack>\n" 418 std::vector<CallInfo_t> callStack;
419 for (
size_t i = 0; i < buffer.size(); ++i)
420 callStack.push_back((
const char*) symbols[i]);
423 size_t lastItem = nSkip + options.
maxLines;
424 if (lastItem > nItems) lastItem = nItems;
425 if (lastItem >= buffer.size()) --lastItem;
428 for (
size_t i = nSkip; i < lastItem; ++i) {
430 print(std::forward<Stream>(out), callStack[i]);
434 out << options.
indent <<
" ... and other " << (nItems - lastItem);
435 if (nItems == buffer.size()) out <<
" (or more)";
444 template <
typename Stream>
462 template <
typename T>
466 static_assert(::util::always_false_v<T>,
467 "static_assert_on<T>: check the error message (\"THE_TYPE_IS<>\") for expansion of type `T`." 473 template <
typename T,
bool Enable = true>
480 template <
typename T>
489 #endif // LARCOREALG_COREUTILS_DEBUGUTILS_H
std::string original
String from the backtrace, unparsed.
std::string demangle(T const *=nullptr)
Outputs a demangled name for type T.
std::string firstIndent
Special indentation for the first line.
static std::string extract(std::string const &s, range_t const &r)
Translates a range into a string.
unsigned int maxLines
Total number of lines to print.
std::string functionName
Parsed function name, demangled.
Print the library name the function lives in.
static opt defaultOptions()
Returns a set of default options.
void setDefaultOptions()
Sets this object to use a set of default options.
std::string libraryName
Parsed library name.
void setAll(std::string const &s, range_t addressStr, range_t libraryStr, range_t functionStr, range_t offsetStr)
Fills the information from an original string and parsed ranges.
CallInfo_t(const char *s)
CallInfoPrinter(opt opts)
Constructor: use specified options.
Print the instruction pointer memory address.
QTextStream & hex(QTextStream &s)
bool has(option_t o) const
Returns whether the specified option is set.
void * address
Function address.
void demangleFunction()
Runs the demangler and stores the result.
Backtrace printing options.
bool countOthers
Whether to print number of omitted lines.
CallInfoPrinter()
Default constructor: use default options.
Class handling the output of information in a CallInfo_t object.
bool operator!() const
Returns whether no information was parsed out of the original.
Use demangled function names when possible.
Print a shorter library name (base name only).
std::bitset< NOptions > options
Value of current options.
bool ParseString(std::string const &s)
Returns whether the translation was complete (offset is optional!).
std::pair< size_t, size_t > range_t
opt options
Set of current options.
Structure with information about a single call, parsed.
QTextStream & flush(QTextStream &s)
option_t
List of available options.
static int max(int a, int b)
static bool emptyRange(range_t const &r)
Returns whether the range is empty or invalid.
void setUniformIndent(std::string uniformIndent)
Sets all indentation to the same specified uniformIndent string.
CallInfoPrinter::opt callInfoOptions
Options for each single backtrace call information line.
std::string indent
Indentation string for all lines.
opt & set(option_t o, bool set=true)
Set one option o to the specified set value (true by default).
Stream & operator<<(Stream &&out, CallInfo_t const &info)
Helper operator to insert a call information in a stream with default options.
unsigned int skipLines
Number of lines to skip.
std::string shortLibrary() const
Returns only the library name (with suffix).
std::string mangledFunctionName
Parsed function name, unprocessed.
details::THE_TYPE_IS< T > _
std::string const & function() const
Returns the function name (mangled if nothing better).
void print(Stream &&out, CallInfo_t const &info) const
Print the content of info into the stream out, using the current options.
void printBacktrace(Stream &&out, BacktracePrintOptions options)
Prints the full backtrace into a stream.
void setOptions(opt opts)
Override all the options.
CallInfo_t(std::string const &s)
std::ptrdiff_t offset
Instruction pointer offset.
Set of options for printing.
Print the offset from the beginning of function.