views:

3533

answers:

5

When using CDT I would like to have std::string show up in the 'variable' debug window with the string it contains. For instance if it is currently holding the word "history" I would like to see history in the debugger window labeled "variables".

I think that there is a general way to have it drill down into objects but I can't put my finger on it. Does anyone out there know how to do this?

This would also be useful for me to use when outputting just a single field from a complex object.

Thanks, Bill

A: 

Note that you don't want a general way of drilling down into objects; you want to pretty-print STL containers.

I think CDT delegates this to the debugger backend; i.e. it is up to your debugger (gdb, perhaps?) to inform CDT about the value of a variable and how it can be expanded.

Edit: I don't think that the GDB backend shipped with CDT has any support for "intelligent" display of STL containers. If you're really interested in this, I'd suggest that you contact the CDT development team at [email protected].

JesperE
A: 

I'm using the debugger that comes with CDT out of the box (as it were). Any idea what I might need to do to make this pretty-printing work?

billcoke
A: 

The only way I have found is to use the GDB command line:

  1. In the Debug window, click on gdb. It's just below the stack trace.
  2. In the Console window, use the GDB up command to get to the stack frame you want
  3. Again in the Console window, use the print command to display the std::string variable:

    p mystring

But that's a lot of trouble.

Something I have noticed is that when I hover over the variable, the fields of the std::string actually are displayed, but it is truncated, so I do not see the most interesting part: the string value. Perhaps there is a way to increase the maximum length of the value displayed.

+5  A: 

Hi all!

Displaying stl containers with eclipse/gdb was also a major pain for me for a long time.

But now I've just discovered that the latest version of gdb with python enabled can help with that.

It follows what I've done (using Ubuntu Linux 8.10):

  • Install gdb version >= 6.8.50 (for instance from debian experimental)
  • Create a file named .gdbinit in your project root with the following content:

    python import gdb.libstdcxx.v6.printers

Now the stl containers will be pretty printed.

If you want to check if you already have a python enabled gbd (or if your new installation have worked):

  • Start gdb from the console
  • On the gdb prompt execute the following

    (gdb) python print 'Python enabled GDB is working!'

  • If the above command produces what we are expecting then it is working.

For more details check this blog.

rpseng
+1 This is good stuff. I had a heck of a time getting it to work with Gentoo. Build GDB with Python support was a breeze (set the USE flag) but the gdb.libstdcxx module was not included. I finally found it here: http://sourceware.org/gdb/wiki/STLSupport
Mark
Had a bit of trouble getting this working. The code checked out from SVN had "import gdb" commented out, causing an error. Once I uncommented that line, it worked from the command line. To get it working within Eclipse, I had to edit the debug configuration to select my customized .gdbinit file. Now that it's all set up, I can see a sensible printed version of most STL data structures. Very pleased to finally have this available!
bnsmith
Oh, and one more little note. Instead of checking out with svn://, I used http://.
bnsmith
A: 

billcoke your best shot is to use some gdb macros which you can type in at the gdb console that can be opened in the Console/Debugger window in eclipse. you can find them here: http://www.yolinux.com/TUTORIALS/src/dbinit_stl_views-1.01.txt

it wont be automatic on hover, but you have to type which is tiring.... but even in the year 2009 the eclipse CDT has not succeeded in bringing us some nice debugging features, so this is your best shot.

TO RPSENG: thanks for the tip rpseng! it works nicely! unfortunately it seems my eclipse (Ganymede) gets so confused that it will not discover any more the type of pointers to instances of my own-made classes and their inheritance, so stepping though member variables of those pointers is not possible anymore. Are you working with CDT 6.0 and is it working better there?