13 #ifndef LARCORE_COREUTILS_DEBUGUTILS_H 14 #define LARCORE_COREUTILS_DEBUGUTILS_H 1 17 #include "cetlib_except/demangle.h" 56 inline std::string
demangle(T
const* =
nullptr)
57 {
return cet::demangle_symbol(
typeid(std::decay_t<T>).name()); }
63 using range_t = std::pair<size_t, size_t>;
80 std::string
const&
function()
const 87 return (sep == std::string::npos)
106 {
return emptyRange(r)?
"": s.substr(r.first, r.second - r.first); }
110 { functionName = cet::demangle_symbol(mangledFunctionName); }
114 std::string
const& s,
148 { options.set(o,
set);
return *
this; }
167 template <
typename Stream>
175 if (options.
has(opt::library)) {
179 else out <<
"unknown";
185 if (
offset && options.
has(opt::offset)) {
187 if (
offset > 0) out <<
'+';
192 out << std::showbase << std::hex <<
offset <<
"]";
200 if (info.
address && options.
has(opt::address))
201 out <<
" at " << ((
void*) info.
address);
206 template <
typename Stream>
208 { print(std::forward<Stream>(out), info); }
217 options.
set(opt::demangled);
218 options.
set(opt::library);
219 options.
set(opt::shortLibrary);
220 options.
set(opt::address);
227 template <
typename Stream>
231 print(std::forward<Stream>(out), info);
238 unsigned int maxLines = 5;
239 unsigned int skipLines = 1;
240 bool countOthers =
true;
249 { indent = firstIndent = uniformIndent; }
261 template <
typename Stream>
264 std::vector<void*> buffer
267 unsigned int const nItems
268 = (
unsigned int) backtrace(buffer.data(), buffer.size());
271 char** symbols = backtrace_symbols(buffer.data(), buffer.size());
273 out << options.
firstIndent <<
"<failed to get the call stack>\n" 277 std::vector<CallInfo_t> callStack;
278 for (
size_t i = 0; i < buffer.size(); ++i)
279 callStack.push_back((
const char*) symbols[i]);
282 size_t lastItem = nSkip + options.
maxLines;
283 if (lastItem > nItems) lastItem = nItems;
284 if (lastItem >= buffer.size()) --lastItem;
287 for (
size_t i = nSkip; i < lastItem; ++i) {
289 print(std::forward<Stream>(out), callStack[i]);
293 out << options.
indent <<
" ... and other " << (nItems - lastItem);
294 if (nItems == buffer.size()) out <<
" (or more)";
306 template <
typename Stream>
322 template <
typename Stream>
325 unsigned int maxLines, std::string
indent =
" ",
341 #endif // LARCORE_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.
Stream & operator<<(Stream &&out, CallInfo_t const &info)
Helper operator to insert a call information in a stream with default options.
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.
opt & set(option_t o, bool set=true)
Set one option o to the specified set value (true by default).
unsigned int skipLines
Number of lines to skip.
LArSoft-specific namespace.
std::string shortLibrary() const
Returns only the library name (with suffix).
std::string mangledFunctionName
Parsed function name, unprocessed.
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.