8

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

6 Answers 6

22

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 gdb (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.

Sign up to request clarification or add additional context in comments.

9 Comments

+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: sourceware.org/gdb/wiki/STLSupport
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!
Oh, and one more little note. Instead of checking out with svn://, I used http://.
@bnsmith how do you do this : "I had to edit the debug configuration to select my customized .gdbinit file". I see Window/Preference/Debug/GDB but not what you are mentioning
This should be the selected answer. Such a great help!
|
2

I would like to expand on the Windows 7 response because some key steps are left out:

This is for MinGW users with Eclipse CDT

0) If you don't have python GDB, open a shell/command and use MinGW-get.exe to 'install' Python-enabled GDB e.g.

   MinGw-get.exe install gdb-python

1a) Get Python 2.7.x from http://python.org/download/ and install

1b) Make sure PYTHONPATH and PYTHONHOME are set in your environment:

 PYTHONPATH should be C:\Python27\Lib   (or similar)
 PYTHONHOME should be C:\Python27

1c) Add PYTHONHOME to your PATH

 %PYTHONHOME%;...

2a) Open a text enter, enter the following statements. Notice the 3rd line is pointing to where the python scripts are located. See notes below about this!

python
import sys
sys.path.insert(0, 'C:/MinGW/share/gcc-4.6.1/python')         
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end

2b) Save as '.gdbinit' NOTE: Windows explorer will not let you name a file that starts with with a period from explorer. Most text edits (including Notepad) will let you. GDB init files are like 'scripts' of GDB commands that GBD will execute upon loading.

2c) The '.gdbinit' file needs to be in the working directory of GDB (most likely this is your projects root directory but your IDE can tell you.

3) Open your Eclipse (or other IDE) Preferences dialog. Go to the C++ Debugger sub-menu.

4) Configure Eclipse to use C:\MinGW\bin\gdb-python27.exe as the debugger and your .gdbinit as the config file.

5a) Re-create all your debug launch configurations (delete the old one and create a new one from scratch).

--OR--

5b) Edit each debug configuration and point it to the new gdb-python.exe AND point it to the.

If you run into issues:

--Don't forget to change the location to the python directory in the above python code! This directory is created by MinGW, so don't go looking to download the pretty printers, MinGW did it for you in step zero. Just goto your MinGW install director, the share folder, the GCC folder (has version number) and you will find python folder. This location is what should be in python script loaded by GDB.

--Also, the .gdbinit is a PITA, make sure its named correctly and in the working folder of GDB which isn't necessarily where gdb-python.exe is located! Look at your GDB output when loading GDB to see if a) 'python-enabled' appears during load and that the statements in the .gdbinit are appearing.

--Finally, I had alot of issues with the system variables. If python gives you 'ImportError' then most likely you have not set PYTHONPATH or PYTHONHOME.

--The directory with 'gdb-python27' (e.g. C:\MinGW\bin') should also be on your path and if it is, it makes setting up eclipse a bit nicer because you don't need to put in absolute paths. But still, sometimes the .gbdinit needs an absoulte path. if it works you'll see output from gbd (console->gdb traces) like this on startup of debugger:

835,059 4^done
835,059 (gdb) 
835,059 5-enable-pretty-printing
835,069 5^done
....
835,129 12^done
835,129 (gdb) 
835,129 13source C:\MinGW\bin\.gdbinit
835,139 &"source C:\\MinGW\\bin\\.gdbinit\n"
835,142 13^done
835,142 (gdb) 

Comments

1

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

Update/Edit: Please see other responses below for instructions on how to enable pretty printers in CDT.

1 Comment

displaying a string is really basic stuff and lazily answered in CDT/GDB, nothing to do with a general way of drilling down into objects
1

Windows 7

Get the pretty printers: svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python

Set up eclipse with: C/C++ Debug Gdb:

C:\MinGW\bin\gdb-python27.exe
C:\MinGW\bin\.gdbinit

1 Comment

Then, how would I make CDT debugger to display the length of a string + leading characters?
0

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.

Comments

0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.