Pretty printing C++ types with dbx
Tags: dbxSince Lawrence doesn’t work at Sun any more, I’ll swipe a blog entry of his to make sure it stays available.
A tip from Lawrence Crowl:
One of the problems with debugging C++ programs is that they have many user-defined types. The debugger typically does not know anything about those types, and so cannot provide any high-level printing of those types. Instead, the debugger prints the types’ representations.
For example, consider a simple C++ standard string.
#include <string>
#include <iostream>
int main() {
std::string variable( "Hello!" );
std::cout << variable << std::endl;
}
In dbx, the result of printing variable is:
(dbx) print variable
variable = {
__data_ = {
__data_ = 0x41260 "Hello!"
}
npos = 0
__nullref = 0
}
Not nice.
The Sun dbx debugger provides a helpful facility for up-leveling the printout. This facility is called pretty printing.
We can help dbx by defining a pretty printing call-back function. In essence, we write a function that converts from our high-level type into a char*. Dbx will look for pretty printing functions with the name db_pretty_print and a first parameter that is a pointer to the high-level type. In our example, the function is:
char* db_pretty_print( std::string* var_addr, int, char* ) {
return const_cast< char* >( var_addr->data() );
}
(The second and third parameters are not needed in this example.)
Now, with the -p flag on the dbx print command line, dbx calls the pretty-printing function and uses the result for its output.
(dbx) print -p variable
variable = Hello!
You can make pretty printing the default by executing
dbxenv output_pretty_print on
either in the debugger or in your ~/.dbxrc. Once pretty printing is the default, you can print the type’s representation with the +p flag.
(dbx) print +p variable
variable = {
__data_ = {
__data_ = 0x41260 "Hello!"
}
npos = 0
__nullref = 0
}
For more information, type “help prettyprint” within dbx.