3

file main.c:

#include <stdio.h>

int main()
{
    int i;
    for (i=0; i<30 ;i++) {
            printf ("%d\n", i);
    }
    return 0;
}

In gdb, I usually set a breakpoint, then specify a watchpoint as command to be executed on that breakpoint:

(gdb) break main
Breakpoint 1 at 0x4004b0: file main.c, line 6.
(gdb) command
Type commands for when breakpoint 2 is hit, one per line.
End with a line saying just "end".
>watch i
>end

Execution is gonna stop whenever the watched variable is changed, the problem is that there is no way(to my knowledge) to tell gdb to just print the value of the watched variable and continue because it is a nested watchpoint. If it was a standalone watchpoint this could easily be done using command 'continue' (when I'm in the scope if main()):

(gdb) watch i
Hardware watchpoint 2: i
(gdb) command
Type commands for when breakpoint 2 is hit, one per line.
End with a line saying just "end".
>continue
>end

So, is there a way for gdb not to stop on a nested watchpoint, and only print the value change? Or better yes, to specify commands to be executed on nested watch/breakpoints?

I further tried 'set complaints 0' and 'set confirm off' in gdb but to no avail

1 Answers1

2

GDB has no concept of nested watchpoints. All breakpoints and watchpoints are at the top level, regardless of where you've set them.

Here is what you want:

(gdb) break main
Breakpoint 1 at 0x40052c: file t.c, line 6.
(gdb) commands
>watch i
>commands
 >c
 >end
>c
>end

This sets command list on breakpoint 1:

watch i
continue

And separate command list on watchpoint (when it is created):

continue
Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Unfortunately, this doesn't work, isn't this a nested watch/breakpoint? When I type the first 'end', the command input stops, and when I run the binary, I get this error: Can't use the "commands" command among a breakpoint's commands. –  Jul 25 '11 at 17:43
  • The answer was cut/pasted for my GDB session, so this *does* work for at least some versions of GDB, and I am really surprised it doesn't work for you. Are you *sure* you are typing exactly the same commands I did above? – Employed Russian Jul 25 '11 at 18:47
  • Many thanks for confirming that, @Employed Russian - the continue trick works great if you basically `continue` between set breakpoints in your session; but if you `step` manually, it can be a problem because when the given breakpoint is reached, instead of remaining in stopped state - gdb will instead start running (that is, continue, as its told). Any ideas on how to have it remain stopped if single-stepping, and continue if running? Cheers! – sdaau Aug 07 '11 at 01:48