awk - 如何在 GDB 中使用管道和 awk 设置变量?

标签 awk gdb pipe

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

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

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

(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 命令以重新加载可执行文件(请参阅 using libtool e gdb )。

最佳答案

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

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 实际上对文件名感兴趣,可以通过以下方式找到该文件名:

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

文档:Inferiors , Progspaces

更新2:

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 或名称(这是解析供人类使用的数据的危险)。

当下级没有运行时,info lowers 打印:

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

当下级当前处于事件状态时,它会打印:

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

关于awk - 如何在 GDB 中使用管道和 awk 设置变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62105922/

相关文章:

linux - BASH 在变量中使用 grep 和 awk

c++ - 使用gdb和-D_GLIBCXX_DEBUG

c - sprintf/printf/fprintf - 用于识别格式说明符与多个 src 文件中的参数计数不匹配的工具

c - 是否可以用 tee 捕获标准输入?

linux - 管道标准输出到特定位置,Linux

sed - 从字符串中提取数字

regex - awk 树桩 : regex substitution within a field

linux - 尝试为 ARM 构建 GDB 时出错

c - 将 EOF 写入管道

arrays - 从字符串中删除所有数值