【问题标题】:GDB: break if variable equal valueGDB:如果变量相等则中断
【发布时间】:2019-01-11 23:59:52
【问题描述】:

我喜欢让 GDB 在变量等于我设置的某个值时设置断点,我尝试了这个示例:

#include <stdio.h>
main()
{ 
     int i = 0;
     for(i=0;i<7;++i)
        printf("%d\n", i);

     return 0;
}

来自 GDB 的输出:

(gdb) break if ((int)i == 5)
No default breakpoint address now.
(gdb) run
Starting program: /home/SIFE/run 
0
1
2
3
4
5
6

Program exited normally.
(gdb)

如您所见,GDB 没有设置任何断点,GDB 是否可以做到这一点?

【问题讨论】:

    标签: c gdb


    【解决方案1】:

    除了嵌套在断点内的观察点 您还可以在 'filename:line_number' 上设置一个断点并使用条件。 我发现它有时更容易。

    (gdb) break iter.c:6 if i == 5
    Breakpoint 2 at 0x4004dc: file iter.c, line 6.
    (gdb) c
    Continuing.
    0
    1
    2
    3
    4
    
    Breakpoint 2, main () at iter.c:6
    6           printf("%d\n", i);
    

    如果您像我一样厌倦了更改行号,您可以添加标签 然后像这样在标签上设置断点:

    #include <stdio.h>
    main()
    { 
         int i = 0;
         for(i=0;i<7;++i) {
           looping:
            printf("%d\n", i);
         }
         return 0;
    }
    
    (gdb) break main:looping if i == 5
    

    【讨论】:

      【解决方案2】:

      您可以为此使用观察点(数据上的断点而不是代码)。

      您可以先使用watch i
      然后使用condition &lt;breakpoint num&gt; i == 5为其设置条件

      可以通过info watch获取断点号

      【讨论】:

      • (gdb) watch i No symbol "i" in current context.
      • 您必须在代码中存在i 的位置。尝试break mainruncs(确保您通过声明的步骤),然后是答案上的命令。请务必使用-g 标志编译您的程序。 (即带有调试信息)
      • 在开始执行之前,与您的主可执行文件链接的其他编译单元/文件可能尚未加载。一个不错的选择是使用start &lt;args&gt;,就像tb mainrun &lt;args&gt;。这将启动程序,让您更轻松地设置中断/观察点。
      【解决方案3】:

      首先,您需要使用适当的标志编译您的代码,以便在代码中进行调试。

      $ gcc -Wall -g -ggdb -o ex1 ex1.c
      

      然后用你最喜欢的调试器运行你的代码

      $ gdb ./ex1
      

      给我看代码。

      (gdb) list
      1   #include <stdio.h>
      2   int main(void)
      3   { 
      4     int i = 0;
      5     for(i=0;i<7;++i)
      6       printf("%d\n", i);
      7   
      8     return 0;
      9   }
      

      在第 5 行中断并查看 i == 5。

      (gdb) b 5
      Breakpoint 1 at 0x4004fb: file ex1.c, line 5.
      (gdb) rwatch i if i==5
      Hardware read watchpoint 5: i
      

      检查断点

      (gdb) info b
      Num     Type           Disp Enb Address            What
      1       breakpoint     keep y   0x00000000004004fb in main at ex1.c:5
          breakpoint already hit 1 time
      5       read watchpoint keep y                      i
          stop only if i==5
      

      运行程序

      (gdb) c
      Continuing.
      0
      1
      2
      3
      4
      Hardware read watchpoint 5: i
      
      Value = 5
      0x0000000000400523 in main () at ex1.c:5
      5     for(i=0;i<7;++i)
      

      【讨论】:

        【解决方案4】:

        有硬件和软件观察点。它们用于读取和写入变量。需要参考教程:

        http://www.unknownroad.com/rtfm/gdbtut/gdbwatch.html

        要设置观察点,首先需要将代码分解到环境中存在变量 i 的位置,然后设置观察点。

        watch 命令用于设置写入的观察点,rwatch 用于读取,awatch 用于读取/写入。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-21
          • 1970-01-01
          • 1970-01-01
          • 2012-01-29
          • 1970-01-01
          相关资源
          最近更新 更多