gdb 命令列表 - 第一次命中断点时,不调用命令

gdb Commands List - the first time a breakpoint is hit, commands aren't invoked

当我 运行 一个带有 gdb 的程序并且有一个命令列表时,这些命令不会在第一次命中断点时被调用。我设置了一个小测试程序来演示。

test.c:

#include <stdio.h>
void incrementI(){
     static int i = 0;
     i++;
     printf("i: %d\n", i);
     sleep(10);
}
int main() {
     int i = 0;
     while(1){
         incrementI();
     }
     return 0;
}

命令列表:testgdbBreakpoint

set breakpoint pending on
b incrementI
commands
backtrace
continue
end

shell 脚本 运行s gdb: testgdbinvoke.sh

#!/bin/sh
gdb -x testGDBBreakpoint -ex=r --args testGDB

我在 运行ning testgdbinvoke.sh 时看到的输出是:

GNU gdb (GDB) 7.12.1
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from testGDB...done.
Breakpoint 1 at 0x10478
Starting program: /data/config/testGDB

Breakpoint 1, 0x00010478 in incrementI ()
(gdb) c
Continuing.
i: 1

Breakpoint 1, 0x00010478 in incrementI ()
#0  0x00010478 in incrementI ()
#1  0x000104d8 in main ()
i: 2

Breakpoint 1, 0x00010478 in incrementI ()
#0  0x00010478 in incrementI ()
#1  0x000104d8 in main ()
i: 3

第一次遇到断点时,我没有看到回溯,而是必须手动继续。 是否有特定的选项我必须在命令列表中设置才能让它们 运行 每次遇到断点?

使用 GDB-10.0 转载。

使用多个 -x-ex 参数之间似乎存在某种意外的交互。这不起作用:
gdb -x testgdbBreakpoint -ex run ./a.out
gdb -ex 'source testgdbBreakpoint' -ex run ./a.out

但是在 testgdbBreakpoint 的末尾添加 run 可以正常工作:

gdb -q -x 'testgdbBreakpoint' ./a.out

Breakpoint 1 at 0x401136: file test.c, line 4.

Breakpoint 1, incrementI () at test.c:4
4            i++;
#0  incrementI () at test.c:4
#1  0x0000000000401189 in main () at test.c:11
i: 1

Breakpoint 1, incrementI () at test.c:4
4            i++;
#0  incrementI () at test.c:4
#1  0x0000000000401189 in main () at test.c:11
i: 2
^C