27

I am running a Python GUI application. I want to invoke and control GDB from it, like load an executable file, set breakpoints etc. I see that GDB has a command line interface which can be used by sending strings to the GDB process, but I want to do it the Python way. Is there a gdb.py? I see that "archer" branch has something like "import gdb", but it doesn't work in Ubuntu's default installation of Python. Where do I get this module or is there any other method to control GDB from Python?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Seeker
  • 987
  • 1
  • 8
  • 20

2 Answers2

21

Yes, you can control GDB from Python. The Python documentation is at http://sourceware.org/gdb/current/onlinedocs/gdb/Python.html#Python.

If you want an example of some scripting, take a look at http://tromey.com/blog/?p=548

timbo
  • 13,244
  • 8
  • 51
  • 71
  • thx. i've mentioned this archer branch in my question already. it seems to be a work-in-progress. i'm going to give it a shot. but what i actually want is a python module that hooks up with existing GDB, not the archer one. – Seeker Aug 17 '10 at 06:01
9

pygdbmi is what you want.

With it I was able to reset an embedded target from Python using the following:

from pygdbmi.gdbcontroller import GdbController
if __name__ == '__main__':
    gdbmi = GdbController()
    response = gdbmi.write('b main')
    response = gdbmi.write('target remote localhost:2331')
    response = gdbmi.write('mon reset 0')
    response = gdbmi.write('c')

gdbgui provides a much cooler example.

Steven Eckhoff
  • 992
  • 9
  • 18