我喜欢让 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 中可能吗?
最佳答案
除了嵌套在断点内的观察点 您还可以在“文件名:行号”上设置一个断点并使用条件。 我发现它有时更容易。
(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
关于c - GDB:如果变量值相等则中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14390256/