如何评估用于 gdb monitor 命令的表达式?

How to evaluate an expression to be used for a gdb monitor command?

在脚本化的 gdb 会话中,我想使用 monitor <cmd>,其中 cmd 应该包含一个符号的地址。例如:

monitor foobar &myVariable

应该变成:

monitor foobar 0x00004711

因为远端无法计算表达式。无论我尝试什么,都会发送字符串“&myVariable”而不是地址。

我试过方便的变量和其他东西,但这是我找到的唯一解决方法:

# write the command into a file and execute this file
# didn't find a direct way to include the address into the monitor command
set logging overwrite on
set logging on command.tmp
printf "monitor foobar 0x%08x\n", &myVariable
set logging off
source command.tmp

有什么想法可以更优雅地解决这个问题吗?

执行此操作的最简单方法是使用 gdb eval 命令,该命令正是为此目的而引入的。它的工作原理有点像 printf:

(gdb) eval "monitor foobar %p", &myVariable

(我没有实际尝试过,所以小心。)

如果你的 gdb 没有 eval,那么它是旧的。我会建议升级。

如果您无法升级,那么您也可以使用 Python.

编写脚本

如果您的 gdb 没有 Python,那么它要么很旧(升级!),要么在没有 Python 支持的情况下编译(重新编译!)。

如果您无法获得这些功能中的任何一个,那么恐怕只剩下 "write out a script and source it" 方法了。