How can I reload a pretty printer?
Jan Vrany
jan@vrany.io
Tue Jul 25 16:32:49 GMT 2023
On Tue, 2023-07-25 at 18:13 +0200, meator via Gdb wrote:
> Hi. When writing a pretty printer, I want to repeatedly test it. This is
> pretty complicated because GDB has no builtin mechanisms to do such
> thing (that I know of) and breakpoint() pretty much can not be used
> because GDB's Python doesn't have access to the capable pty so
> interactive use of PDB is impossible. Even the classic "printf style"
> debugging doesn't work because Python's print() doesn't work
> (gdb.write() has to be used instead).
>
> I need to test the pretty printer often because the official Python
> interface documentation is quite lacking and the interface itself is
> imperfect^1. Accessing the docstrings of the Python interface is
> difficult because the gdb module nor other modules can be imported in
> standalone Python (I've tried to add it to $PYTHONPATH but with no
> success; How are you supposed to access the docstrings?).
>
> Therefore my last solution is to load the pretty printer, try it in gdb,
> modify the pretty printer and repeat. But a pretty printer can be
> sourced only once because the second source call will fail due to the
> fact that the pretty printer is already registered. I have to restart
> GDB every time I modify the pretty printer which is extremely
> impractical. Is there a way to live reload the pretty printer?
It is, though it requires a bit of custom Python hackery. You may find
the code I'm using - to my satisfaction - here:
https://swing.fit.cvut.cz/hg/jv-vdb/file/tip/python/vdb/cli.py#l19
Each time I modify the code in editor, I either run "pr" command
from GDB CLI or "pr()" from Python's REPL and it reload everything,
live. It has some limitations comping from limits of Python itself
and of my knowledge of Python, but for me works well enough.
You may also be interested in "autodebug" hook:
https://swing.fit.cvut.cz/hg/jv-vdb/file/tip/python/vdb/__init__.py#l117
HTH, Jan
>
> 1: There is no sane way to call member functions through gdb.Value which
> is kind of important in C++: https://stackoverflow.com/q/22774067/13840624
More information about the Gdb
mailing list