[albert@localhost#17:52:47#/home/albert/test]$gdb watchtest
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-83.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/albert/test/watchtest...done.
(gdb) b watchtest.cpp : 6
Breakpoint 1 at 0x40085c: file watchtest.cpp, line 6.
(gdb) watch n
No symbol "n" in current context.
(gdb) r
Starting program: /home/albert/test/watchtest
Breakpoint 1, main () at watchtest.cpp:6
6 int k = 1;
Missing separate debuginfos, use: debuginfo-install
glibc-2.12-1.209.el6_9.2.x86_64 libstdc++-4.4.7-23.el6.x86_64
(gdb) watch n
Hardware watchpoint 2: n
(gdb) c
Continuing.
Hardware watchpoint 2: n
Old value = 0
New value = 1
main () at watchtest.cpp:10
10 k = 2;
(gdb) c
Continuing.
1,2
Hardware watchpoint 2: n
Old value = 1
New value = 3
main () at watchtest.cpp:14
14 k = 4;
(gdb) c
Continuing.
3,4
Watchpoint 2 deleted because the program has left the block in
which its expression is valid.
0x00007ffff72c6d1d in __libc_start_main () from /lib64/libc.so.6
(gdb) q
A debugging session is active.
Inferior 1 [process 18567] will be killed.
Quit anyway? (y or n) y
[albert@localhost#17:55:04#/home/albert/test]$
总结
设置数据断点需要在程序启动之后,在运行r命令之前设置断点给出信息:No symbol "n" in current context.
当程序运行到监控变量的作用域之外以后,断点自动被删除,这一点观察执行q命令之前的文字可以看出
添加数据变化断点(硬件断点)格式:watch 变量名