1

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

2 Answers 2

2

backtrace isn't a linux command, it's a gdb command.

If you want to send commands to a popen'd gdb session, you have to push them through stdin, with something like...

import subprocess

gdb = subprocess.Popen(['gdb'], stdin=subprocess.PIPE)
gdb.stdin.write('backtrace\n')
Sign up to request clarification or add additional context in comments.

Comments

2

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:

  1. 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
  2. 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
$

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.