我想将当前下级的可执行文件名称存储在变量中。我在 gdb CLI 中获取可执行文件名称的方法如下:
pipe info inferiors | awk '{ if ($1 == "*") { print $4} }'
但是我无法将此表达式的输出存储在变量中。该表达式使用单引号和双引号,如果与 set
和 eval
组合使用,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/