如何使用“手表” 广东发展银行?

发布于 2024-07-19 07:09:59 字数 431 浏览 2 评论 0原文

我试图通过命令“watch a”来观察“int a”的变化。 然而,当它变成12时,程序并没有停止。为什么呢?

 /* FILE: test.c */
 #include <stdio.h>
 #include <stdlib.h>

 int main(int argc, char** argv){
  printf("Hello world\n");

  int a = 12;
  a = 10;                                                                                                                                                                      
  return 0; 
 }

I tried to watch the change of the "int a" by the command "watch a". However, the program does not stop, when it changes to 12. Why?

 /* FILE: test.c */
 #include <stdio.h>
 #include <stdlib.h>

 int main(int argc, char** argv){
  printf("Hello world\n");

  int a = 12;
  a = 10;                                                                                                                                                                      
  return 0; 
 }

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

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

发布评论

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

评论(3

鹿港巷口少年归 2024-07-26 07:10:00

编译器甚至可能不会生成将 12 分配给“a”的代码,最好反汇编生成的代码进行确认。 您可能需要稍微复杂一点的测试来尝试这一点。

The compiler is likely not even generating code to assign 12 to "a", it would be a good idea to disassemble the generated code to confirm. You probably need a slightly more complex test to try this out.

三五鸿雁 2024-07-26 07:09:59

它可能有助于指定您的平台、GDB 版本以及您使用的 GDB 命令的确切顺序。

这是我所看到的(GDB 似乎工作得很好):

$ gcc -g test.c

$ gdb a.out
GNU gdb (GDB) 6.8.50.20090430-cvs
Copyright (C) 2009 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-unknown-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
(gdb) list
1       #include <stdio.h>
2       #include <stdlib.h>
3
4       int main(int argc, char** argv){
5         printf("Hello world\n");
6
7         int a = 12;
8         a = 10;                                                                                                                                                                      
9         return 0; 
10      }
11
(gdb) b 5
Breakpoint 1 at 0x4004a7: file test.c, line 5.
(gdb) r

Breakpoint 1, main (argc=1, argv=0x7fffffffdb28) at test.c:5
5         printf("Hello world\n");
(gdb) watch a
Hardware watchpoint 2: a
(gdb) c
Hello world
Hardware watchpoint 2: a

Old value = 0
New value = 12
main (argc=1, argv=0x7fffffffdb28) at test.c:8
8         a = 10;                                                                                                                                                                      
(gdb) c
Hardware watchpoint 2: a

Old value = 12
New value = 10
main (argc=1, argv=0x7fffffffdb28) at test.c:9
9         return 0; 
(gdb) c

Watchpoint 2 deleted because the program has left the block in
which its expression is valid.
0x00007ffff7ab3033 in exit () from /lib/libc.so.6
(gdb) c

Program exited normally.
(gdb) q

It may help to specify your platform, version of GDB, and exact sequence of GDB commands you used.

Here is what I see (GDB appears to work just fine):

$ gcc -g test.c

$ gdb a.out
GNU gdb (GDB) 6.8.50.20090430-cvs
Copyright (C) 2009 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-unknown-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
(gdb) list
1       #include <stdio.h>
2       #include <stdlib.h>
3
4       int main(int argc, char** argv){
5         printf("Hello world\n");
6
7         int a = 12;
8         a = 10;                                                                                                                                                                      
9         return 0; 
10      }
11
(gdb) b 5
Breakpoint 1 at 0x4004a7: file test.c, line 5.
(gdb) r

Breakpoint 1, main (argc=1, argv=0x7fffffffdb28) at test.c:5
5         printf("Hello world\n");
(gdb) watch a
Hardware watchpoint 2: a
(gdb) c
Hello world
Hardware watchpoint 2: a

Old value = 0
New value = 12
main (argc=1, argv=0x7fffffffdb28) at test.c:8
8         a = 10;                                                                                                                                                                      
(gdb) c
Hardware watchpoint 2: a

Old value = 12
New value = 10
main (argc=1, argv=0x7fffffffdb28) at test.c:9
9         return 0; 
(gdb) c

Watchpoint 2 deleted because the program has left the block in
which its expression is valid.
0x00007ffff7ab3033 in exit () from /lib/libc.so.6
(gdb) c

Program exited normally.
(gdb) q
空宴 2024-07-26 07:09:59

当您想要调试程序时,您应该始终使用 -O0 -g3 进行构建(我认为您正在使用 gcc,如果您不是,您的编译器可能会支持其他标志来关闭优化并启用调试信息)。

在我的系统(运行 Gentoo GNU/Linux 的 x86_64)上,当我使用任何大于或等于 -O 的优化时,我无法到达“int a = 12”行,因为编译器将应用 死代码消除。 (取自此处,它是 - 中的 -fdce 标志O 部分)

调试时请始终牢记这一点! 通过使用 objdump -D 反汇编代码或告诉编译器显示生成的程序集来验证您的代码(在带有 -S 标志的 gcc 上)

When you want to debug a program you should always build with -O0 -g3 (i take that you are using gcc, if you are not your compiler will probably support other flags to turn down optimization and enable debug information).

On my system (x86_64 running Gentoo GNU/Linux) i am unable to get at the 'int a = 12' line when i use any optimization greater or equal to -O as the compiler will then apply dead code elimination. (Taken from here, it is the -fdce flag in the -O section)

Always keep this in mind when you are debugging! Verify your code by either disassembling it with objdump -D or tell your compiler to show you the generated assembly (on gcc with the -S flag)

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