Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke and control GDB from Python

Tags:

python

gdb

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?

like image 523
Seeker Avatar asked Aug 14 '10 10:08

Seeker


People also ask

Can you use GDB with Python?

Pretty much anything that can be done at the GDB command line can be done with a breakpoint from Python. You can attach commands to a breakpoint, make it conditional, ignore a certain number of hits, make the breakpoint specific to a thread or process, and all of the things you can do from the command line.

How do I import GDB into Python?

Explanation. GDB embeds the Python interpreter so it can use Python as an extension language. You can't just import gdb from /usr/bin/python like it's an ordinary Python library because GDB isn't structured as a library. What you can do is source MY-SCRIPT.py from within gdb (equivalent to running gdb -x MY-SCRIPT.py ) ...


2 Answers

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

like image 134
timbo Avatar answered Sep 21 '22 19:09

timbo


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.

like image 35
Steven Eckhoff Avatar answered Sep 18 '22 19:09

Steven Eckhoff