为什么观察点不起作用?

发布于 12-23 04:02 字数 1420 浏览 5 评论 0原文

我正在研究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) 

在我的测试代码中,变量“x”被更改,但gdb并没有停止。 为什么观察点在这里不起作用?多谢。

I am studying the watchpoint of GDB. I write a simple test code as following:

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) 

In my test codes, the variable "x" is changed, but gdb doesn't stop then.
Why the watchpoint doesn't effect here? Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Spring初心2024-12-30 04:02:27

这:

断点 1,main(argc=<优化输出>、argv=<优化输出>)位于 watch.c:5

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

即使如此,GDB 中仍然存在一个故障(buglet)。这是我所看到的:

(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

This:

Breakpoint 1, main (argc=<optimized out>, argv=<optimized out>) at watch.c:5

suggests that you used -O2 or some such flag when building the test. Try building with -O0 (which will explicitly disable optimization).

Even then, there is a glitch (a buglet) in GDB. Here is what I see:

(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

This can't be right: the value of x changes from 30 to 10, not from 0 to 10.

If I set the breakpoint on the very first instruction of main, then it works as expected:

(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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文