如何在GDB中仅打印变量的值?

4

我有一个GDB命令脚本:

$ cat gdb_commands.txt
set pagination off
set logging file output.txt
set logging on
file stuff
b *0x80000014
run
echo ***DIFF THIS***\n
echo eax:
print $eax
echo ebx:
print $ebx
echo ecx:
print $ecx
echo edx:
print $edx
echo ***DIFF THIS END***\n
quit

如果我在GDB中运行它,会得到以下结果:

$ gdb -q -x gdb_commands.txt
Breakpoint 1 at 0x80000014

Breakpoint 1, 0x80000014 in _start ()
***DIFF THIS***
eax:$1 = 1
ebx:$2 = 2
ecx:$3 = 3
edx:$4 = 4
***DIFF THIS END***
A debugging session is active.

    Inferior 1 [process 8947] will be killed.

Quit anyway? (y or n) [answered Y; input not from terminal]

那里有一个丑陋的美元符号。我可以使用 sed 去掉它,但是我想让 GDB 来做这件事。这种情况是否可行?

(我这样使用 GDB 的原因是因为我们正在编写一个模拟器,并希望测试其行为是否正确。)

2个回答

10

那个丑陋的美元符号...我希望使用gdb来做到那一点

您可以使用printf命令精确控制GDB的输出:

(gdb) print/x $rax
$1 = 0x7ffff7ffe2a0

(gdb) printf "0x%lx\n", $rax
0x7ffff7ffe2a0

3
有一个命令可以完全做到这一点:
(gdb) help output
Like "print" but don't put in value history and don't print newline.
This is useful in user-defined commands.

output命令会输出变量,但不会包含$1 = 以及换行符。


这正是我想要得到的。谢谢。 - Tony Su

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接