Better Variable Exploring When Debugging C++ Code with Eclipse/Cdt

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

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

  • Make sure you have you have Python 2.7 installed and in the system path.

    http://www.python.org/download/

  • Make sure MinGW-get is installed

    http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/

  • Make sure you have an SVN client are installed

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.

Eclipse/CDT/MinGW/GDB Watching objects while debugging

Yes. During debugging, in the Debug Perspective, on the Variables tab, you can click on any variable in scope and look at it's fields. You can then right click on it and add it as a watch variable and it will show up on the Expressions tab where you can see all of it's fields as well.

Debugging C++ code with Eclipse, the view Variables is empty and no tooltip showing variable value

I solved upgrading the version of Eclipse. Apparently on Ubuntu's repos there is an old version. I downloaded the last version and installed it, like shown here:

http://ubuntuhandbook.org/index.php/2016/01/how-to-install-the-latest-eclipse-in-ubuntu-16-04-15-10/

now variables appear.

Eclipse CDT - how to quickly evaluate an expression which is not in the code?

Off the top of my head I would suggest either the Expression view or the Console view, depending on what your flow is, etc.

Expressions

The Expressions view allows you to add any arbitrary expressions to be evaluated by the selected frame every time the debugger hits a breakpoint or completes a single step.

The Expressions view is not open by default. You can open it from the Window -> Show View -> Expressions.

You can then press the Add new expression button and enter an expression to evaluate. (You can enter pretty much any expression:i+j, strlen(s), or anything else you like)

Alternatively, you can:

  1. from the context menu, select *Add watch expression...":

Sample Image


  1. enter the expression in the pop-up:

Sample Image


  1. the Expressions view will open and/or come to the top.

Sample Image

Console

The console view allows you to enter GDB commands in standard GDB syntax.

To use the Console:

  1. Select the gdb item in the Debug View

Sample Image


  1. The Console view will now display the interface to GDB:

Sample Image


  1. Type commands to GDB, e.g. print i + j or p i - j (p is short for print):

Sample Image

Other Hints when using the Console

  • You can "pin" the console so that it stays the selected console top with this button on the toolbar: Sample Image

  • You can switch console with this drop-down:

Sample Image

  • You can open a second console, e.g. one for GDB interaction and one for output of your program with this button:

Sample Image

Problem debugging with Eclipse CDT - Execution and GUI not aligned

It seems pretty likely that the source that Eclipse is showing, and the source that was actually used to compile the executable are different versions of the same file.

You can ask GDB (Eclipse has a console window for GDB) what source was actually used to build the executable, and where GDB found the source it is showing (to Eclipse) with info source GDB command. I bet the Compilation directory + Current source file != Located in.



Related Topics



Leave a reply



Submit