繁体   English   中英

从perl调用并控制GDB

[英]Invoke and control GDB from perl

我正在尝试在 perl 中编写一些脚本来控制 GDB 调试过程。 在下面的代码中,我能够运行 GDB 并传递一些 MI2 命令,但我无法从 GDB 捕获 output:

open(G, "| gdb -n -q --interpreter=mi2") || die("error opening gdb");
syswrite(G, "-file-exec-and-symbols ./a.out\n");
$x = sysread(G, $resp, 4096);
printf("x: %u\n", $x);
printf("resp: '%s'\n", $resp)

结果是:

x: 0
resp: ''
=thread-group-added,id="i1"
(gdb) 
^done
(gdb) 

这里的问题是 GDB 将 output '=thread-group-added,id="i1"' 直接打印到 tty,因此我无法在我的 perl 脚本中捕获它以进行进一步解释。 我想从 GDB 捕获 output 到某个变量并解释它。 正如在 output sysread() 过程中所见,没有读取任何内容(“x: 0”)。 我怎样才能做到这一点?

编辑:经过一段时间的思考,我实现了一些从 GDB 捕获 output 的解决方案。它需要命名为 pipe(下面是“/tmp/pipe”):

open (G, "| gdb -n -q --interpreter=mi2 >& /tmp/pipe") || die("error opening gdb");

现在我可以从 perl 脚本中读取 GDB 中的 output 并解析它。

打开gdb进程只是为了提供(替换)它的STDIN ,而不是读取它的 output G是 pipe 的写端。不能同时使用open ,请参阅 pipe-open in open

相反,使用同时提供两者的库,例如IPC::Run

use warnings;
use strict;
use feature 'say';

use IPC::Run qw(run); 

run [ qw(gdb -n -q --interpreter=mi2) ], \my $stdin, \my $so, \my $se; 

say "out: $so" if $so;    
say "err: $se" if $se;

这会打印出以下行out: =thread-group-added,id="i1" and (gdb)

分配给$stdin以写入程序的STDIN ,并在最后从$so和/或$se读取其标准输出/错误(有一些方法可以在它发出时得到它)。 该模块可以做更多的事情,请参阅文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM