我有一个被多次调用的功能,最终会出现段错误。
但是,我不想在此函数上设置断点并在每次调用它后都停止,因为我将在这里呆了多年。
我听说我可以counter
在GDB中为断点设置一个值,并且每次遇到断点时,计数器都会递减,并且仅在counter
= 0时才会触发。
这是准确的吗?如果可以,我该怎么做?请提供用于设置此类断点的gdb代码。
Answers:
阅读GDB手册的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
continue <n>
这是一种方便的方法,它跳过最后一个匹配断点n - 1
时间(因此在第n个匹配点处停止):
main.c
#include <stdio.h>
int main(void) {
int i = 0;
while (1) {
i++; /* Line 6 */
printf("%d\n", i);
}
}
用法:
gdb -n -q main.out
GDB会话:
Reading symbols from main.out...done.
(gdb) start
Temporary breakpoint 1 at 0x6a8: file main.c, line 4.
Starting program: /home/ciro/bak/git/cpp-cheat/gdb/main.out
Temporary breakpoint 1, main () at main.c:4
4 int i = 0;
(gdb) b 6
Breakpoint 2 at 0x5555555546af: file main.c, line 6.
(gdb) c
Continuing.
Breakpoint 2, main () at main.c:6
6 i++; /* Line 6 */
(gdb) c 5
Will ignore next 4 crossings of breakpoint 2. Continuing.
1
2
3
4
5
Breakpoint 2, main () at main.c:6
6 i++; /* Line 6 */
(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).