Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a watchpoint for a local variable in Eclipse CDT?

int global = 0;

int main() {
    int local = 0;
    local = 1;
    global = 1;
}

I can create a watchpoint for global with:

  • highlight the variable declaration
  • Run -> Toggle watchpoint

and then the program breaks whenever global is modified.

However, if I try this with local an error popup appears:

This operation is unavailable on the current selection. Please select or place the cursor on a field.

GDB allows that with the watch command, using hardware watchpoints.

I know that this can give weird results if the stack frame exits, but that is fine.

Eclipse 4.7.0, CDT 9.3, Ubuntu 16.04.

Related: Setting Memory Breakpoints in Eclipse


1 Answers

It seems the Toggle Watchpoint command are note yet implemented for local variables: see Eclipse bug 110016:

[...]. But the reason why the "Add Watchpoint" feature is left out is because we currently can't catch the moment when the expression comes back to scope. So if you set a watchpoint on a local variable and it goes out of scope, the watchpoint seats in the Breakpoint view and will never be set on any session again.

As workaround you can manually set watchpoints for local variables via GDB in the Console view: see Eclipse CDT + GDB: Setting Watchpoints (Juno)

like image 172
howlger Avatar answered Oct 11 '22 02:10

howlger