C++ – How to enable gdb pretty printing for C++ STL objects in Eclipse CDT

ceclipseeclipse-cdtgdbgdb-python

I'm trying to add pretty printing for STL objects in eclipse cdt. I tried to follow the steps described here:

http://sourceware.org/gdb/wiki/STLSupport

I checked out the python folder, but I can't seem to get this done…

I created a gdbinit and selected for my debug configuration, but whenever I try to start debugging I get the following error:

Error while executing Python code.
!STACK 0
java.lang.Exception: /home/lizardking/workspace/eu.sofia.kpi.cpp.x86.testapp/.gdbinit:6: Error in sourced command file:
Error while executing Python code.
        at org.eclipse.cdt.dsf.mi.service.command.AbstractMIControl$RxThread.processMIOutput(AbstractMIControl.java:824)
        at org.eclipse.cdt.dsf.mi.service.command.AbstractMIControl$RxThread.run(AbstractMIControl.java:662)

If I try to execute the contents of gdbinit in a python shell, I get this error:

Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import sys
sys.path.insert(0, '/home/Documents/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named libstdcxx.v6.printers

It seems that I don't have such module…I don't have a clue about Python so I don't even know what a "module" is in Python….

Can somebody help me with this? It is very important for me to be able to see real debug information, or useful to put it that way. Or someway I can debug even from console and get nice output from gdb, cuz if I go print a string for instance I get useless output….

Regards,
Alex

Best Answer

This is the solution that works for me.

Download ( http://www.gnu.org/software/gdb/download/) and install latest gdb (i.e. with --prefix $HOME). It supports python scripting.

Get python pretty printers by executing

svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python

in a directory of your choice (i.e. $(HOME)/distribs/gdb_printers). You will get 'python' subdirectory in the checkout directory.

Put this in your $(HOME)/.gdbinit file with proper path to pretty printers:

python
import sys 
sys.path.insert(0, '/home/YOUR_NAME_HERE/distribs/gdb_printers/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end

This makes pretty printing usable via command-line interface of gdb ( >(gdb) p my_std_string).

Next explains usage while debugging in Eclipse.

Download ( http://download.eclipse.org/eclipse/downloads/) latest Stream Stable Build or Release of Eclipse (>=3.7 version).

Download ( http://download.eclipse.org/tools/cdt/builds/8.0.0/index.html for Eclipse Indigo or http://www.eclipse.org/cdt/downloads.php for Eclipse Juno) latest Eclipse C/C++ Development Tooling (Eclipse CDT).

Run Eclipse and chose workspace directory where your options will be stored (i.e. $HOME/projects). Click Help->Install New Software... Click Add...->Archive... and choose the CDT build that you've just downloaded. Then you must choose components to install: click CDT Main Features -> C/C++ Development Tools (and possibly other components of your choice). Then proceed with installation and restart Eclipse.

Specify proper location of gdb and .gdbinit in Eclipse and make sure the Pretty Printing option is enabled:

Window -> preferences -> C/C++ -> Debug -> GDB

Now you can see STL containers pretty-printed in Variables view while debugging in Eclipse.

Other commands can be used to make gdb output more decent:

set print pretty on
set print object on
set print static-members on
set print vtbl on
set print demangle on
set demangle-style gnu-v3
set print sevenbit-strings off

UPDATE: Regarding getting it to work for old projects, see point 4) in rustyx answer below.

UPDATE2: ubuntu 12.04 has libstdc++6-4.6-dbg that installs /usr/share/gcc-4.6/python/libstdcxx/ python module for you

Related Topic