How to make a GDB breakpoint only break after the point is reached a given number times?

我们两清 提交于 2019-11-28 03:03:33
Kilian Foth

Read section 5.1.6 of the GDB manual. What you have to do is first set a breakpoint, then set an 'ignore count' for that breakpoint number, e.g. ignore 23 1000.

If you don't know how many times to ignore the breakpoint, and don't want to count manually, the following may help:

  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>

This is a convenient method that skips the last hit breakpoint n - 1 times:

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).
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!