# pahole command for gdb import gdb class Pahole (gdb.Command): """Show the holes in a structure. This command takes a single argument, a type name. It prints the type and displays comments showing where holes are.""" def __init__ (self): super (Pahole, self).__init__ ("pahole", gdb.COMMAND_NONE, gdb.COMPLETE_SYMBOL) @staticmethod def strip (type): while type.code () == gdb.TYPE_CODE_TYPEDEF: type = type.target () return type def pahole (self, type, level, name): if name is None: name = '' tag = type.tag () if tag is None: tag = '' print '%sstruct %s {' % (' ' * (2 * level), tag) bitpos = 0 for field in type.fields (): # Skip static fields. if not hasattr (field, ('bitpos')): continue ftype = self.strip (field.type) if bitpos != field.bitpos: hole = field.bitpos - bitpos print ' /* XXX %d bit hole, try to pack */' % hole bitpos = field.bitpos if field.bitsize > 0: fieldsize = field.bitsize else: # TARGET_CHAR_BIT here... fieldsize = 8 * ftype.sizeof () # TARGET_CHAR_BIT print ' /* %3d %3d */' % (int (bitpos / 8), int (fieldsize / 8)), bitpos = bitpos + fieldsize if ftype.code () == gdb.TYPE_CODE_STRUCT: self.pahole (ftype, level + 1, field.name) else: print ' ' * (2 + 2 * level), print '%s %s' % (str (ftype), field.name) print ' ' * (14 + 2 * level), print '} %s' % name def invoke (self, arg, from_tty): type = gdb.Type (arg) type = self.strip (type) if type.code () != gdb.TYPE_CODE_STRUCT: raise '%s is not a struct type' % arg print ' ' * 14, self.pahole (type, 0, '') Pahole()