C语言 GDB:如果变量等于值则中断

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14390256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:04:40  来源:igfitidea点击:

GDB: break if variable equal value

cgdb

提问by SIFE

I like to make GDB set a break point when a variable equal some value I set, I tried this example:

当一个变量等于我设置的某个值时,我喜欢让 GDB 设置一个断点,我试过这个例子:

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

     return 0;
}

Output from GDB:

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)

Like you see, GDB didn't make any break point, is this possible with GDB?

如您所见,GDB 没有设置任何断点,这可以用 GDB 实现吗?

回答by matt

in addition to a watchpoint nested inside a breakpoint you can also set a single breakpoint on the 'filename:line_number' and use a condition. I find it sometimes easier.

除了嵌套在断点内的观察点之外,您还可以在“文件名:行号”上设置单个断点并使用条件。我发现它有时更容易。

(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);

If like me you get tired of line numbers changing, you can add a label then set the breakpoint on the label like so:

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

#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

回答by imreal

You can use a watchpoint for this (A breakpoint on data instead of code).

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

You can start by using watch i.
Then set a condition for it using condition <breakpoint num> i == 5

您可以从使用开始watch i
然后使用设置它的条件condition <breakpoint num> i == 5

You can get the breakpoint number by using info watch

您可以通过使用获取断点号 info watch

回答by Alexandre Mulatinho

First, you need to compile your code with appropriate flags, enabling debug into code.

首先,您需要使用适当的标志编译您的代码,从而启用调试到代码中。

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

then just run you code with your favourite debugger

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

$ gdb ./ex1

show me the code.

给我看代码。

(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   }

break on lines 5 and looks if i == 5.

在第 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

checking breakpoints

检查断点

(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

running the program

运行程序

(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)

回答by alinsoar

There are hardware and software watchpoints. They are for reading and for writing a variable. You need to consult a tutorial:

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

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

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

To set a watchpoint, first you need to break the code into a place where the varianle i is present in the environment, and set the watchpoint.

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

watchcommand is used to set a watchpoit for writing, while rwatchfor reading, and awatchfor reading/writing.

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