I want to execute linux commands (eg. bt, break, frame etc) inside gdb prompt using python scripting.
for example: i am using subprocess.call(["gdb"], shell=True)
this line takes me to (gdb) prompt by executing gdb command but after it when i try
subprocess.call(["backtrace"], shell=True)
it shows /bin/sh:backtrace: command not found
Add a comment
|
2 Answers
The commands you type into a (gdb) prompt like backtrace, break, frame etc are gdb commands. Only gdb knows how to interpret them and they won't work with subprocess.call() as the latter can only run Linux executable files.
There are two ways to accomplish something close to what you want:
- Start GDB under the control of Python and use the GDB/MI protocol to talk to it. This is how pyclewn works. E.g.
p = subprocess.Popen(['gdb', '-i=mi'], stdin=fd_in, stdout=fd_out). See also https://bitbucket.org/minami/python-gdb-mi/src/tip/gdbmi/session.py?at=default - Use GDB's builtin Python scripting. (API Reference) E.g.
Save this to t.py
import gdb
gdb.execute('set confirm off')
gdb.execute('file /bin/true')
gdb.execute('start')
gdb.execute('backtrace')
gdb.execute('quit')
Then run:
$ gdb -q -x t.py
Temporary breakpoint 1 at 0x4014c0: file true.c, line 59.
Temporary breakpoint 1, main (argc=1, argv=0x7fffffffde28) at true.c:59
59 if (argc == 2)
#0 main (argc=1, argv=0x7fffffffde28) at true.c:59
$