I have written a pretty-printer using the GDB Python interface for a (C) struct that has a tendency to change between software releases. Since the format is fluctuating I have tried to make the printer dynamic enough to adapt and always try to print something useful instead of throwing a Python exception.
Right now there are two major formats that I need to support but there is likely to be a few more in the future. I would like to write some unit tests for the printer to avoid having to manually load coredumps from different releases to test it.
I thought that maybe I could serialize the gdb.Value from within a debugging session and load them in my unit tests but I wasn’t able to do it (pickle didn’t work with gdb.Value). The core-dumps are really large so storing them with the pretty-printer and script GDB for the tests is not an option.
How can I unit-test my pretty printer without keeping huge core-dumps around?
Keep small core dumps 🙂
Let’s assume you have
struct Foothat you have a pretty printer for, and that keeps changing between releases.Compile the following program:
Compile and run this program, store it and the resulting core as
foo-v2andfoo-v2.core(assuming you are now on version 2 of thestruct Foo). The core should be quite small.Now checkout
foo.hthat corresponds to version 1 ofstruct Foofrom your revision control system (you do use a revision control system, right?). Rebuild and re-run the program, store it asfoo-v1andfoo-v1.core.Now, with every release, you just have to rebuild/rerun above program, and compare output of GDB pretty printer with expected result. If it still works ok, you are done. If not,
struct Foomust have changed, and you need to update your pretty printer.Save the new binary and core as
foo-v3andfoo-v3.core, update the pretty printer, then test it against allfoo-vNandfoo-vN.corepairs (for the test itself, use GDB unittesting framework that comes with GDB sources).