Better variable exploring when debugging C++ code with Eclipse/CDT

Danvil picture Danvil · Sep 6, 2010 · Viewed 21.5k times · Source

Using Eclipse and CDT to debug C++ code the variable windows is cumbersome and not very informative for types defined in the standard template library or in boost (e.g. shared_ptr).

Just an example how this may look like for an std::vector:

bar {…}
    std::_Vector_base<TSample<MyTraits>, std::allocator<TSample<MyTraits> > >   
        _M_impl {…} 
            std::allocator<TSample<MyTraits> >  {…} 
            _M_start    0x00007ffff7fb5010  
            _M_finish   0x00007ffff7fd4410  
            _M_end_of_storage   0x00007ffff7fd5010  

Even if this information about the internals of those types may be useful, in almost any cases I would expect a clearer presentation here, i.e. a list of values for the std::vector. Are there any tools, plugins or other modifications around which can do this?

EDIT

The following solutions does not work for linux. I am using ubuntu 14.04, eclipse, g++, gdb.

I cant find a package gdb-python and linux does not use mingw

Answer

DanChianucci picture DanChianucci · Dec 17, 2012

You need a version of GDB capable of using python to pretty print structures. I know at least on windows using mingw that this is not provided in the default install.

Pretty Printers are python modules which tell gdb how to display a given structure. You can write your own, but there are already printers for STL available for download.

To Get Pretty Printers working on Windows (instructions should be similiar for other OS's):

Prerequisites

Installation:

  • Open a command Shell and type:

    mingw-get install gdb-python
    
  • When its finished cd to a local directory and install the printers by typing:

    svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
    
  • Open the .gdbinit (create it in a text editor if need be) and type the following replaceing "C:/directory" with the folder that you checked the printers into.

    Python
    import sys
    sys.path.insert(0, 'C:/directory')
    from libstdcxx.v6.printers import register_libstdcxx_printers
    register_libstdcxx_printers (None)
    end

Eclipse Setup

  • Go To Windows > Preferences > C/C++ > Debug > GDB
  • Where it Says GDB Debugger put the path to the python enabled GDB it will most likely be in the mingw /bin folder with a name like gdb-python27.exe
  • Where it says GDB Command File put the path to the .gdb init file you made earlier.

That's it, debug like normal, the stl structures should be much easier to read.