问题
I would like to do only flash the code on a remote gdb target if it has changed since last time gdb was run. I envisage something along the lines of the following in gdb script;
target extended-remote /dev/<device>
<Attach to Target>
file <Target Program>
if ![compare-sections -r]
load
start
...however, I cannot see how to make a conditional on a command output.
Can anyone help? I think I probably missed something, but I've no idea what....
回答1:
The compare-sections
command doesn't return a value that can be used in an if
statement, but the following may do what you want.
First, define a convenience function named $cmdeval
which will execute a gdb command and return its output as a string:
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()
You can put this in a file named cmdeval.py
and type (gdb) source cmdeval.py
to load it into gdb.
Next, since compare-sections
outputs "MIS-MATCHED"
for any section that has been changed, you can look for that string using the $_regex
convenience function, which is included in more recent versions of gdb:
(gdb) if $_regex($cmdeval("compare-sections -r"),".*MIS-MATCHED.*")
>echo need to load again\n
>end
来源:https://stackoverflow.com/questions/43219115/gdb-script-flow-control-for-remote-target