Eclipse CDT + GDB: Setting Watchpoints (Juno)

I was trying to watch a variable in Eclipse today and just could not find how to set it up. No matter what I did the Toggle Watchpoint option remained greyed out.

A watchpoint is more fun than a regular breakpoint, because the debugger will stop whenever the variable is changed, even if your program is busy doing something else (like overrunning the end of an array).

So after a bit of research it turns out that you can set watchpoints, but the variable type dictates how.

Global variables

If your variable is global, you need to double-click the variable to highlight it (anywhere in your source) and then select Run > Toggle Watchpoint. Until your variable is selected, this option will always be greyed out.

You can also right click on it in the Outline View and then select Toggle Watchpoint from the context menu.

In both cases a watchpoint properties box pops up so you can edit the details. Just click OK and the debugger will stop every time the variable is changed.

If the variable is not listed in the Outline View then sorry – it’s not global and you can’t use the Eclipse GUI to set it – but you can do this:

Local variables

When you’re debugging with gdb via Eclipse, there is a sneaky gdb console view that you can use to talk directly to the gdb session.

Select the console tab at the bottom and on the right-hand side click the Display Selected Console button. This will reveal a drop down menu. Choose [C/C++ Application] gdb.

And now, in the console itself, just type your watchpoint as if you were on the gdb command line (in the screenshot below I want to be notified if a local variable called count goes above the value of 45):

 

gdb console watchpoint

Ta da! Eclipse will now stop whenever your local variable changes.


5 Comments

  1. Burns Fisher
    Posted 13 March 2013 at 14:36 | Permalink

    Thank you!!!! For some reason watchpoints remain grayed out for me (I’m in a cross-development environment) but I always wanted a way to type gdb command directly, but never happened to find that console. That should solve the problem for me.

  2. Faye
    Posted 16 March 2013 at 20:45 | Permalink

    The GDB console is a great find – I was very happy to stumble across it!

  3. zura
    Posted 10 June 2013 at 08:26 | Permalink

    You should try KDevelop.

    I don’t see why someone should use Eclipse, if it is not absolutely necessary.

  4. tim
    Posted 6 December 2013 at 17:07 | Permalink

    You rock! This is so helpful.

  5. Posted 20 May 2014 at 21:40 | Permalink

    Thanks to this post I could finally use that confusing CDT feature and… I’ve discovered your blog, which is great! Thx!