Search code examples
eclipse-cdt

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


I have been away from Eclipse CDT for a while and have become spoiled by JetBrains' excellent IDEs (and the company won't let me have Clion). Admittedly these are mostly for interpreted, rather than compiled languages, so evaluation is simpler.

When I am breakpointed in Eclipse CDT, (how) can I quickly evaluate expressions involving variables known to the debugger, but in ways in which they are not used in the code?

For instance, if I have two integers, i and j, (how) can I quickly evaluate i + j?

Is it possible, given char *s, to evaluate stlen(s)? Or the result of a local function call?


Solution

  • 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...":

    enter image description here

    1. enter the expression in the pop-up:

    enter image description here

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

    enter image description here

    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

    enter image description here

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

    enter image description here

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

    enter image description here

    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: enter image description here

    • You can switch console with this drop-down:

    enter image description here

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

    enter image description here