Stop when the value of the expression changes.
watch expressionexpression: a variable, an address cast to an data
type, a C/C++ expression.Sample: stop when p changes.
(gdb) start
(gdb) p &a
$1 = (int *) 0xffffffffec94
(gdb) p &b
$2 = (int *) 0xffffffffec90
(gdb) watch p
Hardware watchpoint 2: p
(gdb) continue
Hardware watchpoint 2: p
Old value = (int *) 0x0
New value = (int *) 0xffffffffec94
main () at demo.c:7
(gdb) continue
Hardware watchpoint 2: p
Old value = (int *) 0xffffffffec94
New value = (int *) 0xffffffffec90
main () at demo.c:8Sample: stop when *p changes.
int main()
{
int *p = 0; //watch *p
int a = 1;
int b = 2;
p = &a; //hit: *p = 1
*p = 10;//hit: *p = 10
a = 100;//hit: *p = a = 100
p = &b; //hit: *p = b = 2
return 0;
}(gdb) start
(gdb) watch *p
Hardware watchpoint 2: *p(gdb) continue
Hardware watchpoint 2: *p
Old value = <unreadable>
New value = 1
main () at demo.c:7
(gdb) continue
Hardware watchpoint 2: *p
Old value = 1
New value = 10
main () at demo.c:8
(gdb) continue
Hardware watchpoint 2: *p
Old value = 10
New value = 100
main () at demo.c:9
(gdb) continue
Hardware watchpoint 2: *p
Old value = 100
New value = 2
main () at demo.c:10 Stop when the memory referred to by the expression changes.
watch -location expression-location, -l: watch the memory referred to by the
expression. The memory address is fixed at the moment the watchpoint is
set. Stop when contents at that address change.Sample: watch the memory p points to.
(gdb) watch -l *p
(gdb) continue
Hardware watchpoint 2: -location *p
Old value = 1
New value = 10
main () at demo.c:9
(gdb) continue
Hardware watchpoint 2: -location *p
Old value = 10
New value = 100
main () at demo.c:10Sample: Watch an absolute memory.
| Command | Explain |
|---|---|
watch p |
Consider p as expression. Watch value of the expression. |
watch *p |
Consider *p as expression. Watch value of the expression. |
watch -l p |
Find memory address of p. Watch content at the address. |
watch -l *p |
Find address p points to. Watch content at the address. |
watch *((int*)addr) |
Watch absolute memory. |
(gdb) print &n
$1 = (int *) 0xffffffffec9c
(gdb) print *((int *) 0xffffffffec9c)
$2 = 3
# watch a 4-byte region at the address
(gdb) watch *((int *) 0xffffffffec9c)
(gdb) continue
Hardware watchpoint 2: *((int *) 0xffffffffec9c)
Old value = 3
New value = 4
main () at demo.c:5Stop when the expression is read.
rwatch [-l|-location] expressionStop when the expression is either read or written.
awatch [-l|-location] expression