Gdb 为什么watchpoint不';t效应?

Gdb 为什么watchpoint不';t效应?,gdb,watchpoint,Gdb,Watchpoint,我正在研究GDB的观察点。我编写了一个简单的测试代码,如下所示: int main(int argc, char **argv) { int x = 30; int y = 10; x = y; return 0; } I build it via gcc -g -o wt watch.c. And then I started gdb and did following experiment: lihacker@lihacker-laptop:~/

我正在研究GDB的观察点。我编写了一个简单的测试代码,如下所示:

int main(int argc, char **argv)
{
    int x = 30;
    int y = 10;

    x = y;

    return 0;
}

I build it via gcc -g -o wt watch.c. And then I started gdb and did following experiment:
    lihacker@lihacker-laptop:~/mySrc$ gdb ./wt
    GNU gdb (GDB) 7.3
    Copyright (C) 2011 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 "i686-pc-linux-gnu".
    For bug reporting instructions, please see:
    <http://www.gnu.org/software/gdb/bugs/>...
    Reading symbols from /home/lihacker/mySrc/wt...done.
    (gdb) b main
    Breakpoint 1 at 0x80483a5: file watch.c, line 5.
    (gdb) run
    Starting program: /home/lihacker/mySrc/wt 

    Breakpoint 1, main (argc=<optimized out>, argv=<optimized out>) at watch.c:5
    5     int x = 30;
    (gdb) watch x
    Hardware watchpoint 2: x
    (gdb) c
    Continuing.

    Watchpoint 2 deleted because the program has left the block in
    which its expression is valid.
    0xb7e83775 in __libc_start_main () from /lib/tls/i686/cmov/libc.so.6
    (gdb) 
int main(int argc,char**argv)
{
int x=30;
int y=10;
x=y;
返回0;
}
我通过gcc-g-o wt watch.c构建它。然后我启动了gdb并做了以下实验:
lihacker@lihacker-笔记本电脑:~/mySrc$gdb./wt
GNU gdb(gdb)7.3
版权所有(C)2011免费软件基金会。
许可证GPLv3+:GNU GPL版本3或更高版本
这是自由软件:您可以自由更改和重新发布它。
在法律允许的范围内,不存在任何担保。键入“显示复制”
和“显示保修”了解详细信息。
此GDB配置为“i686 pc linux gnu”。
有关错误报告说明,请参阅:
...
从/home/lihacker/mySrc/wt读取符号…完成。
(gdb)b干管
断点1位于0x80483a5:文件watch.c,第5行。
(gdb)运行
启动程序:/home/lihacker/mySrc/wt
断点1,main(argc=,argv=)位于watch.c:5
5 int x=30;
(gdb)手表x
硬件观察点2:x
(gdb)c
持续的。
监视点2已删除,因为程序已将块留在
它的表达式是有效的。
0xb7e83775位于/lib/tls/i686/cmov/libc.so.6中的
(gdb)
在我的测试代码中,变量“x”已更改,但gdb不会停止。 为什么观察点在这里不起作用?非常感谢。

这:

watch.c:5的主断点1(argc=,argv=)

建议您在构建测试时使用
-O2
或类似的标志。尝试使用
-O0
构建(这将显式禁用优化)

即便如此,GDB还是有一个小故障(小号)。以下是我看到的:

(gdb) b main
Breakpoint 3 at 0x80483ba: file t.c, line 3.
(gdb) r

Breakpoint 3, main (argc=1, argv=0xffffca94) at t.c:3
3       int x = 30;
(gdb) watch x
Hardware watchpoint 4: x
(gdb) c
Hardware watchpoint 4: x

Old value = 0
New value = 10
main (argc=1, argv=0xffffca94) at t.c:8
8       return 0;
(gdb) c

Watchpoint 4 deleted because the program has left the block in
which its expression is valid.
0xf7e7cbd6 in __libc_start_main () from /lib32/libc.so.6
这不可能是正确的:x的值从30变为10,而不是从0变为10

如果我在main的第一条指令上设置断点,那么它将按预期工作:

(gdb) b *main
Breakpoint 1 at 0x80483b4: file t.c, line 2.
(gdb) r

Breakpoint 1, main (argc=1, argv=0xffffca94) at t.c:2
2   {
(gdb) watch x
Hardware watchpoint 2: x
(gdb) c
Hardware watchpoint 2: x

Old value = 0
New value = 30
main (argc=1, argv=0xffffca94) at t.c:4
4       int y = 10;
(gdb) c
Hardware watchpoint 2: x

Old value = 30
New value = 10
main (argc=1, argv=0xffffca94) at t.c:8
8       return 0;
(gdb) c

Watchpoint 2 deleted because the program has left the block in
which its expression is valid.
0xf7e7cbd6 in __libc_start_main () from /lib32/libc.so.6
这:

watch.c:5的主断点1(argc=,argv=)

建议您在构建测试时使用
-O2
或类似的标志。尝试使用
-O0
构建(这将显式禁用优化)

即便如此,GDB还是有一个小故障(小号)。以下是我看到的:

(gdb) b main
Breakpoint 3 at 0x80483ba: file t.c, line 3.
(gdb) r

Breakpoint 3, main (argc=1, argv=0xffffca94) at t.c:3
3       int x = 30;
(gdb) watch x
Hardware watchpoint 4: x
(gdb) c
Hardware watchpoint 4: x

Old value = 0
New value = 10
main (argc=1, argv=0xffffca94) at t.c:8
8       return 0;
(gdb) c

Watchpoint 4 deleted because the program has left the block in
which its expression is valid.
0xf7e7cbd6 in __libc_start_main () from /lib32/libc.so.6
这不可能是正确的:x的值从30变为10,而不是从0变为10

如果我在main的第一条指令上设置断点,那么它将按预期工作:

(gdb) b *main
Breakpoint 1 at 0x80483b4: file t.c, line 2.
(gdb) r

Breakpoint 1, main (argc=1, argv=0xffffca94) at t.c:2
2   {
(gdb) watch x
Hardware watchpoint 2: x
(gdb) c
Hardware watchpoint 2: x

Old value = 0
New value = 30
main (argc=1, argv=0xffffca94) at t.c:4
4       int y = 10;
(gdb) c
Hardware watchpoint 2: x

Old value = 30
New value = 10
main (argc=1, argv=0xffffca94) at t.c:8
8       return 0;
(gdb) c

Watchpoint 2 deleted because the program has left the block in
which its expression is valid.
0xf7e7cbd6 in __libc_start_main () from /lib32/libc.so.6

谢谢你的回复。实际上,在通过gcc构建时,我没有设置任何优化标志。结果是一样的,尽管我像你说的那样加了-O0。我的测试平台是运行在虚拟机上的Ubuntu 9.0.4,我的gdb版本是最新的7.3。你的测试环境是什么?谢谢你的回复。实际上,在通过gcc构建时,我没有设置任何优化标志。结果是一样的,尽管我像你说的那样加了-O0。我的测试平台是运行在虚拟机上的Ubuntu 9.0.4,我的gdb版本是最新的7.3。你的测试环境是什么?