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

时间:2012-05-25 04:34:14

标签: scripting automation gdb expect

GDB是否有内置脚本机制,我应该编写一个期望脚本,还是有更好的解决方案?

我每次都会发送相同的命令序列,我会将每个命令的输出保存到文件中(最有可能使用GDB的内置日志记录机制,除非有人有更好的想法)。

3 个答案:

答案 0 :(得分:74)

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

基本上,在这个例子中,我想在代码的特定位置获取一些变量值;并让它们输出,直到程序崩溃。所以这里首先是一个小程序guaranteed to crash,分几步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(另请参阅Automate gdb: show backtrace at every call to function puts)。这个脚本我称之为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,但循环仍然只旋转两次(就像run脚本中gdb的规范一样);如果run没有任何参数,它只会旋转一次(程序的默认值),确认忽略--args ./test.exe 5

但是,由于现在只在一次调用中输出,并且没有任何用户交互,因此可以使用bash重定向轻松地在文本文件中捕获命令行输出,例如:

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

还有一个使用python在c - GDB auto stepping - automatic printout of lines, while free running?

中自动化gdb的示例

希望这有帮助,
干杯!

答案 1 :(得分:54)

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 :(得分:7)

如果带文件的-x对你来说太多了,只需使用多个-ex即可。这是一个跟踪正在运行的程序的示例,该程序显示(并保存)崩溃时的回溯

sudo gdb -p $(pidof my-app) -batch \
  -ex "set logging on" \
  -ex continue \
  -ex "bt full" \
  -ex quit