首页 > 解决方案 > gdb 命令列表 - 第一次命中断点时,不调用命令

问题描述

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

测试.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

运行 gdb 的 shell 脚本:testgdbinvoke.sh

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

我在运行 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

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

标签: gdbembedded-linux

解决方案


使用 GDB-10.0 复制。

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

但是run在 testgdbBreakpoint 末尾添加按预期工作:

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

推荐阅读