ATLAS Offline Software
Loading...
Searching...
No Matches
python.pahole.Pahole Class Reference
Inheritance diagram for python.pahole.Pahole:
Collaboration diagram for python.pahole.Pahole:

Public Member Functions

 __init__ (self)
 pahole (self, atype, level, name, walk=False, nested=False)
 invoke (self, arg, from_tty)

Detailed Description

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.

Definition at line 27 of file pahole.py.

Constructor & Destructor Documentation

◆ __init__()

python.pahole.Pahole.__init__ ( self)

Definition at line 32 of file pahole.py.

32 def __init__ (self):
33 super (Pahole, self).__init__ ("pahole", gdb.COMMAND_DATA,
34 gdb.COMPLETE_SYMBOL)
35

Member Function Documentation

◆ invoke()

python.pahole.Pahole.invoke ( self,
arg,
from_tty )

Definition at line 96 of file pahole.py.

96 def invoke (self, arg, from_tty):
97 argv = gdb.string_to_argv(arg)
98 if len(argv) > 2:
99 raise gdb.GdbError('pahole takes a type name and an optional "walk" argument.')
100 stype = gdb.lookup_type (argv[0])
101 ptype = stype.strip_typedefs()
102 if ptype.code != gdb.TYPE_CODE_STRUCT and ptype.code != gdb.TYPE_CODE_UNION:
103 raise gdb.GdbError('%s is not a struct/union type: %s' % (arg, ptype.code))
104
105 # Should the entire object be walked recursively?
106 walk = len(argv) > 1 and argv[1] == "walk"
107
108 self.pahole (ptype, 0, argv[0], walk=walk)
109

◆ pahole()

python.pahole.Pahole.pahole ( self,
atype,
level,
name,
walk = False,
nested = False )

Definition at line 36 of file pahole.py.

36 def pahole (self, atype, level, name, walk=False, nested=False):
37 if name is None:
38 name = ''
39 tag = atype.tag
40 if tag is None:
41 tag = ''
42 kind = 'struct' if atype.code == gdb.TYPE_CODE_STRUCT else 'union'
43 if not nested:
44 print ('/* %4d 0x%04x */ ' % (atype.sizeof, atype.sizeof), end="")
45 print ('%s%s %s {' % ( ' ' * (2 * level), kind, tag))
46 endpos = 0
47 for field in atype.fields():
48 # Skip static fields
49 if not hasattr (field, ('bitpos')):
50 continue
51 # find the type
52 ftype = field.type.strip_typedefs()
53
54 # Detect hole
55 if endpos < field.bitpos:
56 hole = field.bitpos - endpos
57 print ('/* XXX %4d */ !!' % (hole // 8), end="")
58 print (' ' * (4 + 2 * level - 2), end="")
59 print ('char [%d] __%d_bit_padding__' % (math.ceil(hole / 8), hole))
60
61 # Are we a bitfield?
62 if field.bitsize > 0:
63 fieldsize = field.bitsize
64 else:
65 if (ftype.code == gdb.TYPE_CODE_STRUCT or ftype.code == gdb.TYPE_CODE_UNION) and len(ftype.fields()) == 0:
66 fieldsize = 0 # empty struct
67 else:
68 fieldsize = 8 * ftype.sizeof # will get packing wrong for structs
69
70 print ('/* %3d 0x%03x %4d */ ' % (field.bitpos // 8, field.bitpos // 8, fieldsize // 8), end="")
71 endpos = field.bitpos + fieldsize
72
73 # Walk nested structure or print variable size (this is not a hole)
74 if walk and (ftype.code == gdb.TYPE_CODE_STRUCT or ftype.code == gdb.TYPE_CODE_UNION):
75 print (' ', end="")
76 self.pahole (ftype, level + 1, field.name, walk=walk, nested=True)
77 else:
78 print (' ' * (4 + 2 * level), end="")
79 print ('%s %s' % (str (ftype), field.name),end="")
80 # Append bitfield size if non-standard
81 if fieldsize != ftype.sizeof * 8:
82 print (':%d' % fieldsize)
83 else:
84 print ('')
85
86 # Check for padding at the end
87 if endpos // 8 < atype.sizeof:
88 hole = 8 * atype.sizeof - endpos
89 print ('/* XXX %4d */ !!' % hole, end="")
90 print (' ' * (4 + 2 * level - 2), end="")
91 print ('char [%d] __%d_bit_padding__' % (math.ceil(hole / 8), hole))
92
93 print (' ' * (14 + 2 * level), end="")
94 print (' } %s' % name)
95

The documentation for this class was generated from the following file: