13 #ifndef LARCOREALG_COREUTILS_DEBUGUTILS_H 14 #define LARCOREALG_COREUTILS_DEBUGUTILS_H 20 #include "cetlib_except/demangle.h" 56 inline std::string
demangle(T
const* =
nullptr);
62 using range_t = std::pair<size_t, size_t>;
77 std::string
const&
function()
const 104 return emptyRange(r) ?
"" : s.substr(r.first, r.second - r.first);
111 void setAll(std::string
const& s,
168 template <
typename Stream>
169 void print(Stream&& out,
CallInfo_t const& info)
const;
172 template <
typename Stream>
175 print(std::forward<Stream>(out), info);
185 options.
set(opt::demangled);
186 options.
set(opt::library);
187 options.
set(opt::shortLibrary);
188 options.
set(opt::address);
196 template <
typename Stream>
203 unsigned int maxLines = 5;
204 unsigned int skipLines = 1;
205 bool countOthers =
true;
224 template <
typename Stream>
232 template <
typename Stream>
250 template <
typename Stream>
252 unsigned int maxLines,
329 template <
typename T,
bool Enable >
342 template <
typename T>
345 return cet::demangle_symbol(
typeid(std::decay_t<T>).name());
349 template <
typename Stream>
358 if (options.has(opt::library)) {
368 if (
offset && options.has(opt::offset)) {
376 out << std::showbase << std::hex <<
offset <<
"]";
379 if (!info.
libraryName.empty() && options.has(opt::library)) {
383 if (info.
address && options.has(opt::address)) out <<
" at " << ((
void*)info.
address);
388 template <
typename Stream>
392 print(std::forward<Stream>(out), info);
397 template <
typename Stream>
400 unsigned int nSkip = std::max(options.
skipLines, 0U);
401 std::vector<void*> buffer(nSkip + std::max(options.
maxLines, 200U),
nullptr);
403 unsigned int const nItems = (
unsigned int)backtrace(buffer.data(), buffer.size());
406 char** symbols = backtrace_symbols(buffer.data(), buffer.size());
408 out << options.
firstIndent <<
"<failed to get the call stack>\n" << std::flush;
411 std::vector<CallInfo_t> callStack;
412 for (
size_t i = 0; i < buffer.size(); ++i)
413 callStack.push_back((
const char*)symbols[i]);
416 size_t lastItem = nSkip + options.
maxLines;
417 if (lastItem > nItems) lastItem = nItems;
418 if (lastItem >= buffer.size()) --lastItem;
421 for (
size_t i = nSkip; i < lastItem; ++i) {
423 print(std::forward<Stream>(out), callStack[i]);
427 out << options.
indent <<
" ... and other " << (nItems - lastItem);
428 if (nItems == buffer.size()) out <<
" (or more)";
436 template <
typename Stream>
438 unsigned int maxLines,
453 template <
typename T>
457 static_assert(::util::always_false_v<T>,
458 "static_assert_on<T>: check the error message (\"THE_TYPE_IS<>\") for " 459 "expansion of type `T`.");
464 template <
typename T,
bool Enable = true>
471 template <
typename T>
478 #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.
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.
std::string indent(std::size_t const i)
Structure with information about a single call, parsed.
option_t
List of available options.
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.
void operator()(Stream &&out, CallInfo_t const &info) const
Print the content of info into the stream out, using the current options.
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.