Chris Quenelle is a tools developer at Oracle Corp. He's worked on performance and debugging tools at Sun and Oracle for over 15 years. He reads comic books and science fiction,and has more tivos than he can keep track of.

 

February 2012
SMTWTFS
«Jan  
 1234
567891011
12131415161718
19202122232425
26272829 

Pretty printing C++ types with dbx

Since 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.

http://quenelle.org/unix/wp-content/plugins/sociofluid/images/digg_24.pnghttp://quenelle.org/unix/wp-content/plugins/sociofluid/images/reddit_24.pnghttp://quenelle.org/unix/wp-content/plugins/sociofluid/images/dzone_24.pnghttp://quenelle.org/unix/wp-content/plugins/sociofluid/images/stumbleupon_24.pnghttp://quenelle.org/unix/wp-content/plugins/sociofluid/images/delicious_24.pnghttp://quenelle.org/unix/wp-content/plugins/sociofluid/images/furl_24.pnghttp://quenelle.org/unix/wp-content/plugins/sociofluid/images/technorati_24.pnghttp://quenelle.org/unix/wp-content/plugins/sociofluid/images/facebook_24.pnghttp://quenelle.org/unix/wp-content/plugins/sociofluid/images/twitter_24.png

1 comment to Pretty printing C++ types with dbx

Leave a Reply

  

  

  

You can use these HTML tags

<a href=""title=""><abbr title=""><acronym title=""><b><blockquote cite=""><cite><code><del datetime=""><em><i><q cite=""><strike><strong>