Libcwd reads the symbol table of the application and of each of the linked object files upon initialization. It then allows you to translate program counter addresses to function names, source file names and line numbers. You can also print demangled names of any symbol or type, making the debug output better human readable.
Example 1: printing the location that a function was called from:#ifdef CWDEBUG
libcwd::location_ct location((char*)__builtin_return_address(0)
std::string demangled_function_name;
Dout(
dc::notice,
"This function was called from " << demangled_function_name <<
'(' << location <<
')');
#endif
#define Dout(cntrl, data)
Macro for writing debug output.
Definition: debug.h:154
channel_ct notice
Definition: debug.cc:460
void demangle_symbol(char const *input, std::string &output)
Demangle mangled symbol name input and write the result to string output.
Definition: demangle3.cc:825
int const builtin_return_address_offset
Offset to __builtin_return_address() needed to get the correct line number from location_ct.
Definition: sys.h:39
Example 2: Printing the demangled name of the current (template) function:
Dout(
dc::notice,
"We are in Foo<" << type_info_of<TYPE>().demangled_name() <<
">::f()");
Note that calling
libcwd::demangle_symbol costs cpu every time you call it, but using
libcwd::type_info_of<> does not cost any cpu: the demangling is done once, during the initialization of libcwd;
libcwd::type_info_of<> merely returns a static pointer.