Monday, April 25, 2016

Debugging gdb pretty printers

Leave a Comment

I've started experimenting with building gdb pretty printers for some of my C++ data structures, but the documentation is pretty thin.

As a result, I need to guess about how to do things, and frequently my pretty printers just crash with a non-useful python exception with no indication of where the actual problem is.

Is there any good way of debugging a pretty printer? I've had success in other python programs by inserting an explicit call to pydb in the code:

import pydb pydb.debugger() 

but that doesn't seem to work when running python in gdb -- it just runs past the debugger call and doesn't stop or say or do anything.

1 Answers

Answers 1

You can run pdb (one of the python debuggers) within gdb. Here is an excerpt of a gdb session with a simple example:

(gdb) print (ObjectSignature *) 0x7f71e4018000 $1 = (ObjectSignature *) 0x7f71e4018000 (gdb) python import pdb (gdb) python pdb.run('gdb.execute("print $1[0]")') > <string>(1)<module>() (Pdb) b svtprinters.printers.ObjectSignaturePrinter.to_string Breakpoint 1 at /svtfs/svtprinters/printers.py:195 (Pdb) c $2 = > /svtfs/svtprinters/printers.py(196)to_string() -> sizetypestr = 'invalid' (Pdb) n > /svtfs/svtprinters/printers.py(197)to_string() -> sizetypeidx = int(self.val['mSizeType']) (Pdb) self.val['mSizeType'] <gdb.Value object at 0x7effc90ff430> (Pdb) int(self.val['mSizeType']) 3 (Pdb) n > /svtfs/svtprinters/printers.py(199)to_string() -> if sizetypeidx < len(self.sizetypes): (Pdb) self.sizetypes ['unknown', 'meta_1K', 'data_4K', 'data_8K', 'data_16K', 'data_32K', 'data_64K'] (Pdb) n > /svtfs/svtprinters/printers.py(200)to_string() -> sizetypestr = self.sizetypes[sizetypeidx] (Pdb)  > /svtfs/svtprinters/printers.py(202)to_string() -> return (20*"%02x"+" %s") % tuple([self.val['mValue'][i] for i in range(20)]+[sizetypestr]) (Pdb) sizetypestr 'data_8K' (Pdb) c 98d6687a2ea63a134901f0df140b13112e64bfb7 data_8K (gdb)  

In this example ObjectSignaturePrinter is a class which is associated via gdb.pretty_printers with the ObjectSignature type in $1. The output of the second print command is split; $2 = is printed before the pretty printer breakpoint is reached, and the rest of the output appears after the pdb continue command.

It's likely that variations on this approach will work with other python debuggers.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment