自动化GDB调试会话的最佳方法是什么?


Answers:


64

gdb.gdbinit运行后执行文件。因此,您可以将命令添加到此文件,然后查看是否适合您。这是.gdbinit为了打印所有f()呼叫的回溯的示例:

set pagination off
set logging file gdb.txt
set logging on
file a.out
b f
commands
bt
continue
end
info breakpoints
r
set logging off
quit

2
我可以设置一个环境变量,使GDB在启动时运行其他文件吗?
匿名2012年

27
@Anonymous否,但是有一个命令行选项:--command = FILE,-x从FILE执行GDB命令。
马特2012年

86

我只是在经历类似的事情,并提出了一个基本示例-知道我很快就会忘记它,所以我认为最好:)将其发布,因此我将其发布在这里,因为它看起来与问题有关。

基本上,在此示例中,我想在代码的特定位置获取一些变量值;并让它们输出直到程序崩溃。所以这里首先是一个小程序,它保证会在几个步骤中崩溃test.c

#include <stdio.h>
#include <stdlib.h>

int icount = 1; // default value

main(int argc, char *argv[])
{
  int i;

  if (argc == 2) {
    icount = atoi(argv[1]);
  }

  i = icount;
  while (i > -1) {
    int b = 5 / i;
    printf(" 5 / %d = %d \n", i, b );
    i = i - 1;
  }

  printf("Finished\n");
  return 0;
}

程序接受命令行参数的唯一原因是能够选择崩溃前的步骤数-并以批处理方式显示gdb忽略--args。我用这个编译:

gcc -g test.c -o test.exe

然后,我准备以下脚本-这里的主要技巧是为command每个分配一个breakpoint,最终将continue(请参见自动gdb:在每次调用put函数时显示backtrace)。我称这个脚本为test.gdb

# http://sourceware.org/gdb/wiki/FAQ: to disable the
# "---Type <return> to continue, or q <return> to quit---"
# in batch mode:
set width 0
set height 0
set verbose off

# at entry point - cmd1
b main
commands 1
  print argc
  continue
end

# printf line - cmd2
b test.c:17
commands 2
  p i
  p b
  continue
end

# int b = line - cmd3
b test.c:16
commands 3
  p i
  p b
  continue
end

# show arguments for program
show args
printf "Note, however: in batch mode, arguments will be ignored!\n"

# note: even if arguments are shown;
# must specify cmdline arg for "run"
# when running in batch mode! (then they are ignored)
# below, we specify command line argument "2":
run 2     # run

#start # alternative to run: runs to main, and stops
#continue

请注意,如果要在批处理模式下使用它,则必须最后使用runstart或类似的方法“启动”脚本。

使用此脚本后,我可以gdb以批处理方式调用-这将在终端中生成以下输出:

$ gdb --batch --command=test.gdb --args ./test.exe 5
Breakpoint 1 at 0x804844d: file test.c, line 10.
Breakpoint 2 at 0x8048485: file test.c, line 17.
Breakpoint 3 at 0x8048473: file test.c, line 16.
Argument list to give program being debugged when it is started is "5".
Note, however: in batch mode, arguments will be ignored!

Breakpoint 1, main (argc=2, argv=0xbffff424) at test.c:10
10    if (argc == 2) {
$1 = 2

Breakpoint 3, main (argc=2, argv=0xbffff424) at test.c:16
16      int b = 5 / i;
$2 = 2
$3 = 134513899

Breakpoint 2, main (argc=2, argv=0xbffff424) at test.c:17
17      printf(" 5 / %d = %d \n", i, b );
$4 = 2
$5 = 2
 5 / 2 = 2 

Breakpoint 3, main (argc=2, argv=0xbffff424) at test.c:16
16      int b = 5 / i;
$6 = 1
$7 = 2

Breakpoint 2, main (argc=2, argv=0xbffff424) at test.c:17
17      printf(" 5 / %d = %d \n", i, b );
$8 = 1
$9 = 5
 5 / 1 = 5 

Breakpoint 3, main (argc=2, argv=0xbffff424) at test.c:16
16      int b = 5 / i;
$10 = 0
$11 = 5

Program received signal SIGFPE, Arithmetic exception.
0x0804847d in main (argc=2, argv=0xbffff424) at test.c:16
16      int b = 5 / i;

请注意,虽然我们指定命令行参数5,循环仍然纺纱只有两次(因为是规范rungdb脚本); 如果run没有任何参数,它将仅旋转一次(程序的默认值),以确认将--args ./test.exe 5其忽略。

但是,由于现在这是在单个调用中输出的,并且不需要任何用户交互,因此可以使用bash重定向将命令行输出轻松捕获到文本文件中,例如:

gdb --batch --command=test.gdb --args ./test.exe 5 > out.txt

还有一个在C语言中使用python自动化gdb的示例-GDB自动步进-自动打印行,同时自由运行?

希望这会
有所帮助,干杯!


3
感谢您的分享,这很有用
Gearoid Murphy

1
对我来说太越野车了 detach在内部command使gdb崩溃,continue在内部coammand导致怪异的Selected thread is running.警报。
phil294

10

如果-x带有文件的太多了,只需使用多个即可-ex

这是一个跟踪正在运行的程序的示例,该程序在崩溃时显示(并保存)回溯

sudo gdb -p "$(pidof my-app)" -batch \
  -ex "set logging on" \
  -ex continue \
  -ex "bt full" \
  -ex quit
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.