如何在达到给定次数的点之后使GDB断点断开?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在达到给定次数的点之后使GDB断点断开?相关的知识,希望对你有一定的参考价值。

我有一个被称为大量次数的函数,最终是段错误。

但是,我不想在这个函数上设置一个断点,并且每次调用它都会停止,因为我会在这里待多年。

我听说我可以在GDB中为断点设置一个counter,并且每次遇到断点时,计数器都会递减,并且只有在counter = 0时才会被触发。

这是否准确,如果是这样,我该怎么做?请提供gdb代码来设置这样的断点。

答案

阅读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
另一答案

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
", 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).

以上是关于如何在达到给定次数的点之后使GDB断点断开?的主要内容,如果未能解决你的问题,请参考以下文章

GDB:断点没有被命中

如何使 GDB 与外部程序一起工作

GDB 如何知道它必须在指定的断点处中断?

GDB调试命令

GDB

GDB 断点不会命中模板函数