我有一个被多次调用的函数,最终是段错误。
但是,我不想在这个函数上设置一个断点,并且每次调用它都会停止,因为我会在这里待多年。
我听说我可以在GDB中为断点设置counter
,每次遇到断点,计数器都会递减,只有在counter
= 0时才会被触发。
这是否准确,如果是这样,我该怎么办?请提供gdb代码来设置这样的断点。
答案 0 :(得分:142)
阅读GDB手册的section 5.1.6。你要做的是首先设置一个断点,然后为那个断点号设置一个'忽略计数',例如ignore 23 1000
。
如果您不知道忽略断点的次数,并且不想手动计数,则以下内容可能有所帮助:
ignore 23 1000000 # set ignore count very high.
run # the program will SIGSEGV before reaching the ignore count.
# Once it stops with SIGSEGV:
info break 23 # tells you how many times the breakpoint has been hit,
# which is exactly the count you want
答案 1 :(得分:8)
<强> continue <n>
强>
这是一种方便的方法,可以跳过最后一次点击断点n - 1
次:
gdb -n -q tmp.out
Reading symbols from tmp.out...done.
(gdb) l
1 #include <stdio.h>
2
3 int main(void) {
4 int i = 0;
5 while (1) {
6 i++;
7 printf("%d\n", i);
8 }
9 }
(gdb) start
Temporary breakpoint 1 at 0x6a8: file tmp.c, line 4.
Starting program: /home/ciro/bak/git/cpp-cheat/gdb/tmp.out
Temporary breakpoint 1, main () at tmp.c:4
4 int i = 0;
(gdb) b 6
Breakpoint 2 at 0x5555555546af: file tmp.c, line 6.
(gdb) c
Continuing.
Breakpoint 2, main () at tmp.c:6
6 i++;
(gdb) c 5
Will ignore next 4 crossings of breakpoint 2. Continuing.
1
2
3
4
5
Breakpoint 2, main () at tmp.c:6
6 i++;
(gdb) p i
$1 = 5
(gdb)
(gdb) help c
Continue program being debugged, after signal or breakpoint.
Usage: continue [N]
If proceeding from breakpoint, a number N may be used as an argument,
which means to set the ignore count of that breakpoint to N - 1 (so that
the breakpoint won't break until the Nth time it is reached).