While trying to answer another question, I tried to set-up a Python script to automatize the extraction of an assembly execution trace. But, I am really not satisfied of this script and I would like to know how to improve it.
First, here is the script:
import gdb gdb.execute('break main') gdb.execute('run') while (True): gdb.write (gdb.execute('x /i $pc', to_string=True).rstrip('\n'), gdb.STDOUT) gdb.execute('stepi', to_string=False) gdb.flush ()
Then, just execute:
$> gdb -x ./script.py ./main 1> log.txt
The problems that I would like to solve are as follow:
while(True)is definitely not satisfactory. I would like to stop or suspend the loop when a breakpoint or an exit is reached.
Also, the way we export the list of instructions outside of
gdbis not really satisfactory. Saving it to a file would be much better than having to redirect
stdoutto a file.
Finally, be able to interact with the software, feeding it through
stdinwould also be something we want.
So, if you know how to improve this script in any manner, I would be interested.