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



Answers

Well, gdb don't natively support STL containers. You can't say this is incorrect, since it will expose the inner workings of the STL objects, but most of the time it is not what we want, right?

If you're using gdb 7.0 you can take advantage of the pretty printers. This website http://sourceware.org/gdb/wiki/STLSupport has a pretty simple tutorial on how to set them. I copied below the part that interests you:

  1. Check-out the latest Python libstdc++ printers to a place on your machine. In a local directory, do:

        svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
    
  2. Add the following to your ~/.gdbinit. The path needs to match where the python module above was checked-out. So if checked out to: /home/maude/gdb_printers/, the path would be as written in the example:

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

The path should be the only element that needs to be adjusted in the example above. Once loaded, STL classes that the printers support should printed in a more human-readable format. To print the classes in the old style, use the /r (raw) switch in the print command (i.e., print /r foo). This will print the classes as if the Python pretty-printers were not loaded.

Since you're using eclipse cdt, don't forget to point your debug configuration to your .gdbinit file. When creating a new Debug Configuration, go to the Debugger tab and put the path to the .gdbinit file in the "GDB command file" field.

I hope that helps!

Question

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




I know that JDT (Java environment in eclipse) provides custom "formatters" to be applied when displaying variable values in debug views. A quick look at google for the same in CDT brings this page:

http://wiki.eclipse.org/CDT/Better_Debugging_%28GSoC_project%29

I don't know if this has been yet integrated in the main CDT line, may be you can try to right click on a variable while debugging (in the last CDT) and see if there is a custom formater entry. If not available I recomend you to add a new tracker entry in CDT tracker to ask this enhancement.




If you have gdb support for CDT (see, for example, GDB in Eclipse), you could try this: De-referencing STL containers

Long ago I also stumbled upon your same problem. It was a pain to check the STL containers. Then I found that link and added to my .gdbinit file some of those definitions. Life was easier after that.

NOTE: My gdb version is 7.1 and adding those definitions work fine. I don't know if in newer versions of gdb they are already included.






Links