如何在GDB中使用pipe和awk设置变量? [英] How to set variable using pipe and awk in GDB?

查看:125
本文介绍了如何在GDB中使用pipe和awk设置变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将当前劣等文件的可执行文件名称存储在一个变量中.我在gdb CLI中获取可执行文件名称的方法如下:

I would like to store the executable name of the current inferior in a variable. I way to obtain the executable name in the gdb CLI is the following:

 pipe info inferiors | awk '{ if ($1 == "*") { print $4} }'

但是我无法将该表达式的输出存储在变量中.表达式使用单引号和双引号,如果gdb与seteval组合使用,则会使gdb抱怨.

But I am unable to store the output of this expression in a variable. The expression uses single and double quotes, which makes gdb complain if it is combined with set and eval.

(gdb) set $exec="pipe info inferiors | awk '{ if ($1 == "*") { print $4} }'"
Argument to arithmetic operation not a number or boolean.
(gdb) eval "!echo %s", "pipe info inferiors | awk '{ if ($1 == "*") { print $4} }'"
Argument to arithmetic operation not a number or boolean.

然后,如何将可执行文件名称存储在变量中?我的目标是在使用Autotools时将可执行文件的名称传递给file命令以重新加载可执行文件(请参阅使用libtool e gdb ).

Then, how can I store the executable name in a variable? My goal is to pass the executable name to the file command to reload the executable when Autotools are used (see using libtool e gdb).

推荐答案

您最好的选择是使用嵌入式Python.示例:

Your best bet is to use embedded Python. Example:

gdb -q ./a.out
Reading symbols from ./a.out...

(gdb) start
Temporary breakpoint 1 at 0x1129: file foo.c, line 3.
Starting program: /tmp/a.out 

Temporary breakpoint 1, main () at foo.c:3
3         int v1 = 1, v2 = 2, v3 = 42;
(gdb) info inferiors
  Num  Description       Connection           Executable        
* 1    process 217200    1 (native)           /tmp/a.out        

(gdb) python gdb.execute('set $pid = ' + str(gdb.selected_inferior().pid))
(gdb) p $pid
$1 = 217200

更新:

最初的问题要求输入字段4,即pid,但是看来Gorka实际上对文件名感兴趣,可以通过以下方式找到该文件名:

Original question asked for field 4, which is the pid, but it appears that Gorka was actually interested in the file name, which can be found this way:

(gdb) python gdb.execute('set $f = "' + str(gdb.selected_inferior().progspace.filename) + '"')
(gdb) p $f
$2 = "/tmp/a.out"

文档:下等 Progspaces

更新2:

现在,我再次获得带有字段4的文件名...,但是正如您所说的,我正在获取pid.我不知道为什么会这样.

Now I get the file name again with the field 4... but I was obtaining the pid as you said. I cannot figure why this is happening.

您将获得pid或名称,具体取决于下级是否正在运行(这是解析供人类使用的数据的危险).

You get pid or name depending on whether the inferior is running or not (that's the danger of parsing data that is intended for humans).

当下级未运行时,info inferiors打印:

When the inferior is not running, info inferiors prints:

(gdb) info inferiors
  Num  Description       Connection           Executable
* 1    <null>                                 /bin/date

下级当前处于活动状态时,它会打印:

When the inferior is currently active, it prints:

(gdb) info inferiors
  Num  Description       Connection           Executable
* 1    process 118431    2 (native)           /bin/date

这篇关于如何在GDB中使用pipe和awk设置变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆