13 #ifndef COREUTILS_DEBUGUTILS_H 14 #define COREUTILS_DEBUGUTILS_H 1 17 #include "cetlib_except/demangle.h" 57 {
return cet::demangle_symbol(
typeid(std::decay_t<T>).
name()); }
63 using range_t = std::pair<size_t, size_t>;
87 return (sep == std::string::npos)
106 {
return emptyRange(r)?
"": s.substr(r.first, r.second - r.first); }
110 { functionName = cet::demangle_symbol(mangledFunctionName); }
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 <<
'+';
201 if (info.
address && options.
has(opt::address))
202 out <<
" at " << ((
void*) info.
address);
207 template <
typename Stream>
209 { print(std::forward<Stream>(out), info); }
218 options.
set(opt::demangled);
219 options.
set(opt::library);
220 options.
set(opt::shortLibrary);
221 options.
set(opt::address);
228 template <
typename Stream>
232 print(std::forward<Stream>(out), info);
248 template <
typename Stream>
255 constexpr
unsigned int nSkip = 1;
256 std::vector<void*> buffer(nSkip +
std::max(maxLines, 200U),
nullptr);
259 = (
unsigned int) backtrace(buffer.data(), buffer.size());
262 char** symbols = backtrace_symbols(buffer.data(), buffer.size());
266 std::vector<CallInfo_t> callStack;
267 for (
size_t i = 0; i < buffer.size(); ++i)
268 callStack.push_back((
const char*) symbols[i]);
271 size_t lastItem = nSkip + maxLines;
272 if (lastItem > nItems) lastItem = nItems;
273 if (lastItem >= buffer.size()) --lastItem;
277 for (
size_t i = nSkip; i < lastItem; ++i) {
279 print(std::forward<Stream>(out), callStack[i]);
282 if (lastItem < nItems) {
283 out <<
indent <<
" ... and other " << (nItems - lastItem);
284 if (nItems == buffer.size()) out <<
" (or more)";
295 #endif // COREUTILS_DEBUGUTILS_H
std::string const & function() const
Returns the function name (mangled if nothing better)
void * address
function address
bool ParseString(std::string const &s)
Returns whether the translation was complete (offset is optional!)
std::string shortLibrary() const
Returns only the library name (with suffix)
std::string libraryName
parsed library name
std::string demangle(T const *=nullptr)
Outputs a demangled name for type T.
option_t
List of available options.
void demangleFunction()
Runs the demangler and stores the result.
use demangled function names when possible
std::pair< size_t, size_t > range_t
std::ptrdiff_t offset
instruction pointer offset
Set of options for printing.
QTextStream & hex(QTextStream &s)
std::string original
string from the backtrace, unparsed
static opt defaultOptions()
Returns a set of default options.
bool operator!() const
Returns whether no information was parsed out of the original.
static bool emptyRange(range_t const &r)
Returns whether the range is empty or invalid.
CallInfo_t(std::string const &s)
print the offset from the beginning of function
Structure with information about a single call, parsed.
std::string functionName
parsed function name, demangled
std::string mangledFunctionName
parsed function name, unprocessed
opt & set(option_t o, bool set=true)
Set one option o to the specified set value (true by default)
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.
QTextStream & flush(QTextStream &s)
opt options
set of current options
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, unsigned int maxLines=5, std::string indent=" ", CallInfoPrinter::opt const *options=nullptr)
Prints the full backtrace into a stream.
static int max(int a, int b)
print a shorter library name (base name only)
CallInfoPrinter()
Default constructor: use default options.
General GArSoft Utilities.
void setOptions(opt opts)
Override all the options.
CallInfoPrinter(opt opts)
Constructor: use specified options.
Class handling the output of information in a CallInfo_t object.
Stream & operator<<(Stream &&out, CallInfo_t const &info)
Helper operator to insert a call information in a stream with default options.
print the instruction pointer memory address
print the library name the function lives in
CallInfo_t(const char *s)
std::bitset< NOptions > options
value of current options
void setDefaultOptions()
Sets this object to use a set of default options.
QTextStream & endl(QTextStream &s)
bool has(option_t o) const
Returns whether the specified option is set.
static std::string extract(std::string const &s, range_t const &r)
Translates a range into a string.