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

C++EclipseGdbEclipse Cdt

C++ Problem Overview


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

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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!

Solution 3 - C++

In debug view in variables list expand vector:

"vector_name" -> std::_Vector_base<"datatype"> -> _M_impl

then right click on _M_start and select "Display as array...", type its length and then click OK. Now you can expand each item of your vector.

Solution 4 - C++

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.

Solution 5 - C++

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

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

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

  2. 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) 

Solution 6 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDanvilView Question on Stackoverflow
Solution 1 - C++DanChianucciView Answer on Stackoverflow
Solution 2 - C++pedromanoelView Answer on Stackoverflow
Solution 3 - C++0x0000eWanView Answer on Stackoverflow
Solution 4 - C++Adri C.S.View Answer on Stackoverflow
Solution 5 - C++William SymionowView Answer on Stackoverflow
Solution 6 - C++Manuel SelvaView Answer on Stackoverflow