【问题标题】:How can I use "watch" GDB?如何使用“监视”GDB?
【发布时间】:2023-03-10 00:36:01
【问题描述】:

我试图通过命令“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; 
 }

【问题讨论】:

  • 您是否使用调试信息(-g)进行编译并且您确定您使用的是调试版本?

标签: gdb watch


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    指定您的平台、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
    

    【讨论】:

      【解决方案3】:

      当你想调试一个程序时,你应该总是用 -O0 -g3 构建(我认为你正在使用 gcc,如果你不是你的编译器可能会支持其他标志来拒绝优化并启用调试信息)。

      在我的系统(运行 Gentoo GNU/Linux 的 x86_64)上,当我使用任何大于或等于 -O 的优化时,我无法进入“int a = 12”行,因为编译器随后将应用 dead code elimination。 (取自here,就是-O部分的-fdce标志)

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-30
        • 2013-01-21
        • 1970-01-01
        • 2019-09-14
        相关资源
        最近更新 更多