If I set a watchpoint for a variable local to the current scope, it will be auto deleted when going out of the scope. Is there any way to set it once and keep it auto alive whenever entering the same scope?
Is there anyway to set conditional watchpoint, like
watch var1 if var1==0
? In my case, the condition does't work. gdb stops whenevervar1
's value is changed, instead of untillvar1 == 0
is true. My gdb is GNU gdb 6.8-debian.

- 347,512
- 102
- 1,199
- 985

- 1
- 141
- 372
- 590
4 Answers
I agree with Dave that a conditional breakpoint is the way to go.
However, to do what you asked, you can use GDB's commands
command to set a list of GDB commands to execute whenever a breakpoint is hit. I find this incredibly useful.
I suggest writing your GDB commands into a file so that they are easy to edit and easy to reload with the source
command. Or you can specify command files to load on the GDB command line or use .gdbinit to make them load automatically.
An example of a good use of commands
:
Suppose that I have a function format
that is called by a lot of other functions. I want to break on it, but only after function do_step_3
has been called.
break do_step_3
commands
break format
continue
end
You could use this for your problem with something like:
break func
commands
watch var
continue
end

- 53,022
- 10
- 79
- 131
-
This is really cool. I find it very useful but I think it has one problem, that the breakpoint at `format` will stay until explicitly deleted. This means I have to manually delete the breakpoint every time `format` is hit. Is there a way to make it automatically? – Salahuddin Sep 22 '20 at 06:29
-
1@Salahuddin Try `tbreak`. I think it's temporary break and will delete or disable itself after hitting. – Zan Lynx Sep 22 '20 at 16:36
You can set conditions on watchpoints in the same way that you do with breakpoints. This is in the documentation but admittedly it hardly calls attention to itself.
So watch my_var if my_var > 3
works just fine, as does the condition
command.
To recreate the watchpoint if the variable it is watching goes out of scope, have gdb do this automatically using a breakpoint at the start of the function as Zan has described.

- 6,492
- 2
- 30
- 34
You can set a watchpoint that does not go out of scope by setting it to the memory address.
(gdb) p &var1
$1 = (int *) 0x41523c0
(gdb) watch *(int *)0x41523c0
Hardware watchpoint 1: *(int *)0x41523c0
This also works for other data types and pointers.

- 81
- 1
- 2
-
8This can be useful, but won't work here. Local variables won't maintain the same address between function calls - but other stack variables are likely to use that memory location... – Nick Oct 05 '11 at 16:54
I'm not sure which language us are using, so the exact answer will vary, but could you change the variable to either be static, global, or dynamically allocated (and don't free it when the function returns?). This way it's raw address won't change, and gdb will be able breakpoint on it.
Instead of watching the value whe it equals a specific value; you should set a conditional break point on the line where you want to check the value of
var1
. This should effectively have the same effect
e.g.
(gdb) break main.c:123 if (var1 == 0)

- 9,540
- 3
- 39
- 58
-
I am using C++. I use watchpoint over breakpoint because there could be many places in the code changing the value of the variable. So it is not realistic to add breakpoint to each place. I would like to see if it is possible to set conditional watchpoint. – Tim Aug 31 '09 at 00:52