I'm using IPython to explore the GDB API for the same purpose. I can start up an IPython kernel inside GDB, and then connect to it from another IPython console in another terminal. From there it's possible to interactively use the Python API in the running GDB process.
I have defined a helper command for GDB in my .gdbinit
file:
# gdb command to start an embedded ipython kernel.
# connect to it with "ipython3 console --existing ..."
# use gdb.parse_and_eval() to evaluate variables etc.
define ipython_embed
python
import sys
print(sys.version)
# helper functions
import gdb
def gdb_run(cmd):
print(gdb.execute(cmd, to_string=True))
def gdb_eval(expression):
return gdb.parse_and_eval(expression)
def gdb_vis(value):
return gdb.default_visualizer(value)
import IPython
IPython.embed_kernel()
end
# gdb command prompt is basically unusable after the ipython kernel stops, so just exit gdb
quit
end
Running it in GDB will print something like:
To connect another client to this kernel, use:
--existing kernel-2701.json
In another terminal, run ipython console
with these command line options to connect to GDB. You can then easily use the GDB API.
To debug your visualizer, get a printable expression with gdb.parse_and_eval()
(or gdb_eval()
), get the associated printer class with gdb.default_visualizer()
(or gdb_vis()
) and then call/debug your methods.
Note that you might have to install some additional packages for the necessary IPython support, and that some details might vary based on the Python and GDB versions.
set python print-stack full
. That will get you full stack traces from exceptions. I never tried a python debugger for my pretty-printers; I mostly ended up doing print debugging. – Culch