compare-sections 命令不会返回可在if 语句中使用的值,但以下命令可能会满足您的要求。
首先,定义一个名为 $cmdeval 的 convenience function,它将执行 gdb 命令并将其输出作为字符串返回:
import gdb
class CmdEval(gdb.Function):
"""$cmdeval(str) - evaluate argument string as a gdb command
and return the result as a string. Trailing newlines are removed.
"""
def __init__(self):
super(CmdEval, self).__init__("cmdeval")
def invoke(self, gdbcmd):
return gdb.execute(gdbcmd.string(), from_tty=False, to_string=True).rstrip('\n')
CmdEval()
你可以把它放在一个名为cmdeval.py的文件中,然后输入(gdb) source cmdeval.py把它加载到gdb中。
接下来,由于compare-sections 为任何已更改的部分输出"MIS-MATCHED",您可以使用$_regex 便利函数查找该字符串,该函数包含在更新版本的gdb 中:
(gdb) if $_regex($cmdeval("compare-sections -r"),".*MIS-MATCHED.*")
>echo need to load again\n
>end