xref: /xnu-10063.141.1/tools/lldbmacros/structanalyze.py (revision d8b80295118ef25ac3a784134bcf95cd8e88109f)
1*d8b80295SApple OSS Distributionsimport lldb
2*d8b80295SApple OSS Distributions
3*d8b80295SApple OSS Distributionsfrom xnu import *
4*d8b80295SApple OSS Distributions
5*d8b80295SApple OSS Distributions_UnionStructClass = [ lldb.eTypeClassStruct, lldb.eTypeClassClass, lldb.eTypeClassUnion ]
6*d8b80295SApple OSS Distributions
7*d8b80295SApple OSS Distributionsdef _get_offset_formatter(ctx, fmt_hex, fmt_dec):
8*d8b80295SApple OSS Distributions    """ Returns a formatter of struct member offsets and sizes.
9*d8b80295SApple OSS Distributions
10*d8b80295SApple OSS Distributions    params:
11*d8b80295SApple OSS Distributions        ctx - configuration context
12*d8b80295SApple OSS Distributions        fmt_hex - hexadecimal format
13*d8b80295SApple OSS Distributions        fmt_dec - decimal format
14*d8b80295SApple OSS Distributions    returns:
15*d8b80295SApple OSS Distributions        offset formatter
16*d8b80295SApple OSS Distributions    """
17*d8b80295SApple OSS Distributions    O = ctx[0]
18*d8b80295SApple OSS Distributions    use_hex = ctx[1]
19*d8b80295SApple OSS Distributions    if use_hex:
20*d8b80295SApple OSS Distributions        fmt = fmt_hex
21*d8b80295SApple OSS Distributions    else:
22*d8b80295SApple OSS Distributions        fmt = fmt_dec
23*d8b80295SApple OSS Distributions    return lambda o, s: O.format(fmt, o, s)
24*d8b80295SApple OSS Distributions
25*d8b80295SApple OSS Distributionsdef _get_num_formatter(ctx, fmt_hex, fmt_dec):
26*d8b80295SApple OSS Distributions    """ Returns a number formatter.
27*d8b80295SApple OSS Distributions
28*d8b80295SApple OSS Distributions    params:
29*d8b80295SApple OSS Distributions        ctx - configuration context
30*d8b80295SApple OSS Distributions        fmt_hex - hexadecimal format
31*d8b80295SApple OSS Distributions        fmt_dec - decimal format
32*d8b80295SApple OSS Distributions    returns:
33*d8b80295SApple OSS Distributions        number formatter
34*d8b80295SApple OSS Distributions    """
35*d8b80295SApple OSS Distributions    O = ctx[0]
36*d8b80295SApple OSS Distributions    use_hex = ctx[1]
37*d8b80295SApple OSS Distributions    if use_hex:
38*d8b80295SApple OSS Distributions        fmt = fmt_hex
39*d8b80295SApple OSS Distributions    else:
40*d8b80295SApple OSS Distributions        fmt = fmt_dec
41*d8b80295SApple OSS Distributions    return lambda n: O.format(fmt, n)
42*d8b80295SApple OSS Distributions
43*d8b80295SApple OSS Distributionsdef _showStructPacking(ctx, symbol, begin_offset=0, symsize=0, typedef=None, outerSize=0, memberName=None):
44*d8b80295SApple OSS Distributions    """ Recursively parse the field members of structure.
45*d8b80295SApple OSS Distributions
46*d8b80295SApple OSS Distributions        params :
47*d8b80295SApple OSS Distributions            ctx - context containing configuration settings and the output formatter (standard.py) symbol (lldb.SBType) reference to symbol in binary
48*d8b80295SApple OSS Distributions        returns:
49*d8b80295SApple OSS Distributions            string containing lines of output.
50*d8b80295SApple OSS Distributions    """
51*d8b80295SApple OSS Distributions
52*d8b80295SApple OSS Distributions    O = ctx[0]
53*d8b80295SApple OSS Distributions    format_offset = _get_offset_formatter(ctx, "{:#06x},[{:#6x}]", "{:04d},[{:4d}]")
54*d8b80295SApple OSS Distributions    format_num = _get_num_formatter(ctx, "{:#04x}", "{:2d}")
55*d8b80295SApple OSS Distributions
56*d8b80295SApple OSS Distributions    ctype = "unknown type"
57*d8b80295SApple OSS Distributions    is_union = False
58*d8b80295SApple OSS Distributions    is_class = False
59*d8b80295SApple OSS Distributions    union_size = None
60*d8b80295SApple OSS Distributions    sym_size = symbol.GetByteSize()
61*d8b80295SApple OSS Distributions
62*d8b80295SApple OSS Distributions    if symbol.GetTypeClass() == lldb.eTypeClassUnion:
63*d8b80295SApple OSS Distributions        ctype = "union"
64*d8b80295SApple OSS Distributions        is_union = True
65*d8b80295SApple OSS Distributions        union_size = sym_size
66*d8b80295SApple OSS Distributions    if symbol.GetTypeClass() == lldb.eTypeClassStruct:
67*d8b80295SApple OSS Distributions        ctype = "struct"
68*d8b80295SApple OSS Distributions    if symbol.GetTypeClass() == lldb.eTypeClassClass:
69*d8b80295SApple OSS Distributions        ctype = "class"
70*d8b80295SApple OSS Distributions        is_class = True
71*d8b80295SApple OSS Distributions
72*d8b80295SApple OSS Distributions    if not outerSize or outerSize == sym_size:
73*d8b80295SApple OSS Distributions        outstr = format_offset(begin_offset, sym_size)
74*d8b80295SApple OSS Distributions    elif outerSize < sym_size: # happens with c++ inheritance
75*d8b80295SApple OSS Distributions        outstr = format_offset(begin_offset, outerSize)
76*d8b80295SApple OSS Distributions    else:
77*d8b80295SApple OSS Distributions        outstr = O.format("{:s}{VT.DarkRed}{{{:s}}}{VT.Default}",
78*d8b80295SApple OSS Distributions                format_offset(begin_offset, sym_size),
79*d8b80295SApple OSS Distributions                format_num(outerSize - sym_size))
80*d8b80295SApple OSS Distributions
81*d8b80295SApple OSS Distributions    if typedef:
82*d8b80295SApple OSS Distributions        outstr += O.format(" {0}", typedef)
83*d8b80295SApple OSS Distributions    if symbol.IsAnonymousType():
84*d8b80295SApple OSS Distributions        outstr += O.format(" ({VT.DarkMagenta}anonymous {0}{VT.Default})", ctype)
85*d8b80295SApple OSS Distributions    else:
86*d8b80295SApple OSS Distributions        outstr += O.format(" ({VT.DarkMagenta}{0} {1}{VT.Default})", ctype, symbol.GetName())
87*d8b80295SApple OSS Distributions    if memberName:
88*d8b80295SApple OSS Distributions        outstr += O.format(" {0} {{", memberName)
89*d8b80295SApple OSS Distributions    else:
90*d8b80295SApple OSS Distributions        outstr += ") {"
91*d8b80295SApple OSS Distributions
92*d8b80295SApple OSS Distributions    print(outstr)
93*d8b80295SApple OSS Distributions
94*d8b80295SApple OSS Distributions    with O.indent():
95*d8b80295SApple OSS Distributions        _previous_size = 0
96*d8b80295SApple OSS Distributions        _packed_bit_offset = 0
97*d8b80295SApple OSS Distributions        _nfields = symbol.GetNumberOfFields()
98*d8b80295SApple OSS Distributions
99*d8b80295SApple OSS Distributions        if is_class:
100*d8b80295SApple OSS Distributions            _next_offset_in_bits = 0
101*d8b80295SApple OSS Distributions            _nclasses = symbol.GetNumberOfDirectBaseClasses()
102*d8b80295SApple OSS Distributions
103*d8b80295SApple OSS Distributions            for i in range(_nclasses):
104*d8b80295SApple OSS Distributions                member = symbol.GetDirectBaseClassAtIndex(i)
105*d8b80295SApple OSS Distributions                if i < _nclasses - 1:
106*d8b80295SApple OSS Distributions                    m_size_bits = symbol.GetDirectBaseClassAtIndex(i + 1).GetOffsetInBits()
107*d8b80295SApple OSS Distributions                elif _nfields:
108*d8b80295SApple OSS Distributions                    m_size_bits = symbol.GetFieldAtIndex(0).GetOffsetInBits()
109*d8b80295SApple OSS Distributions                else:
110*d8b80295SApple OSS Distributions                    m_size_bits = symbol.GetByteSize() * 8
111*d8b80295SApple OSS Distributions
112*d8b80295SApple OSS Distributions                m_offset = member.GetOffsetInBytes() + begin_offset
113*d8b80295SApple OSS Distributions                m_type = member.GetType()
114*d8b80295SApple OSS Distributions                m_name = member.GetName()
115*d8b80295SApple OSS Distributions                m_size = m_size_bits // 8
116*d8b80295SApple OSS Distributions
117*d8b80295SApple OSS Distributions                _previous_size = m_size
118*d8b80295SApple OSS Distributions                _packed_bit_offset = member.GetOffsetInBits() + m_size_bits
119*d8b80295SApple OSS Distributions
120*d8b80295SApple OSS Distributions                _showStructPacking(ctx, m_type, m_offset, str(m_type), outerSize=m_size, memberName=m_name)
121*d8b80295SApple OSS Distributions
122*d8b80295SApple OSS Distributions        for i in range(_nfields):
123*d8b80295SApple OSS Distributions            member = symbol.GetFieldAtIndex(i)
124*d8b80295SApple OSS Distributions            m_offset = member.GetOffsetInBytes() + begin_offset
125*d8b80295SApple OSS Distributions            m_offset_bits = member.GetOffsetInBits()
126*d8b80295SApple OSS Distributions
127*d8b80295SApple OSS Distributions            m_type = member.GetType()
128*d8b80295SApple OSS Distributions            m_name = member.GetName()
129*d8b80295SApple OSS Distributions            m_size = m_type.GetByteSize()
130*d8b80295SApple OSS Distributions
131*d8b80295SApple OSS Distributions            if member.IsBitfield():
132*d8b80295SApple OSS Distributions                m_is_bitfield = True
133*d8b80295SApple OSS Distributions                m_size_bits = member.GetBitfieldSizeInBits()
134*d8b80295SApple OSS Distributions            else:
135*d8b80295SApple OSS Distributions                m_is_bitfield = False
136*d8b80295SApple OSS Distributions                m_size_bits = m_size * 8
137*d8b80295SApple OSS Distributions
138*d8b80295SApple OSS Distributions            if not is_union and _packed_bit_offset < m_offset_bits:
139*d8b80295SApple OSS Distributions                m_previous_offset = begin_offset + (_packed_bit_offset // 8)
140*d8b80295SApple OSS Distributions                m_hole_bits = m_offset_bits - _packed_bit_offset
141*d8b80295SApple OSS Distributions                if _packed_bit_offset % 8 == 0:
142*d8b80295SApple OSS Distributions                    print(O.format("{:s} ({VT.DarkRed}*** padding ***{VT.Default})",
143*d8b80295SApple OSS Distributions                           format_offset(m_previous_offset, (m_hole_bits // 8))))
144*d8b80295SApple OSS Distributions                else:
145*d8b80295SApple OSS Distributions                    print(O.format("{:s} ({VT.Brown}*** padding : {:s} ***{VT.Default})",
146*d8b80295SApple OSS Distributions                            format_offset(m_previous_offset, _previous_size),
147*d8b80295SApple OSS Distributions                            format_num(m_hole_bits)))
148*d8b80295SApple OSS Distributions
149*d8b80295SApple OSS Distributions            _previous_size = m_size
150*d8b80295SApple OSS Distributions            _packed_bit_offset = m_offset_bits + m_size_bits
151*d8b80295SApple OSS Distributions
152*d8b80295SApple OSS Distributions            _type_class = m_type.GetTypeClass()
153*d8b80295SApple OSS Distributions            _canonical_type = m_type.GetCanonicalType()
154*d8b80295SApple OSS Distributions            _canonical_type_class = m_type.GetCanonicalType().GetTypeClass()
155*d8b80295SApple OSS Distributions
156*d8b80295SApple OSS Distributions            if _type_class == lldb.eTypeClassTypedef and _canonical_type_class in _UnionStructClass:
157*d8b80295SApple OSS Distributions                _showStructPacking(ctx, _canonical_type, m_offset, str(m_type), outerSize=union_size, memberName=m_name)
158*d8b80295SApple OSS Distributions            elif _type_class in _UnionStructClass:
159*d8b80295SApple OSS Distributions                _showStructPacking(ctx, m_type, m_offset, outerSize=union_size, memberName=m_name)
160*d8b80295SApple OSS Distributions            else:
161*d8b80295SApple OSS Distributions                outstr = format_offset(m_offset, m_size)
162*d8b80295SApple OSS Distributions                if is_union and union_size != (m_size_bits // 8):
163*d8b80295SApple OSS Distributions                    outstr += O.format("{VT.DarkRed}{{{:s}}}{VT.Default}",
164*d8b80295SApple OSS Distributions                            format_num(union_size - (m_size_bits // 8)))
165*d8b80295SApple OSS Distributions                if m_is_bitfield:
166*d8b80295SApple OSS Distributions                    outstr += O.format(" ({VT.DarkGreen}{:s} : {:s}{VT.Default}) {:s}",
167*d8b80295SApple OSS Distributions                            m_type.GetName(),
168*d8b80295SApple OSS Distributions                            format_num(m_size_bits),
169*d8b80295SApple OSS Distributions                            m_name)
170*d8b80295SApple OSS Distributions                else:
171*d8b80295SApple OSS Distributions                    outstr += O.format(" ({VT.DarkGreen}{:s}{VT.Default}) {:s}",
172*d8b80295SApple OSS Distributions                            m_type.GetName(), m_name)
173*d8b80295SApple OSS Distributions                print(outstr)
174*d8b80295SApple OSS Distributions
175*d8b80295SApple OSS Distributions        referenceSize = sym_size
176*d8b80295SApple OSS Distributions        if outerSize:
177*d8b80295SApple OSS Distributions            referenceSize = min(outerSize, sym_size)
178*d8b80295SApple OSS Distributions
179*d8b80295SApple OSS Distributions        if not is_union and _packed_bit_offset < referenceSize * 8:
180*d8b80295SApple OSS Distributions            m_previous_offset = begin_offset + (_packed_bit_offset // 8)
181*d8b80295SApple OSS Distributions            m_hole_bits = referenceSize * 8 - _packed_bit_offset
182*d8b80295SApple OSS Distributions            if _packed_bit_offset % 8 == 0:
183*d8b80295SApple OSS Distributions                print(O.format("{:s} ({VT.DarkRed}*** padding ***{VT.Default})",
184*d8b80295SApple OSS Distributions                        format_offset(m_previous_offset, m_hole_bits // 8)))
185*d8b80295SApple OSS Distributions            else:
186*d8b80295SApple OSS Distributions                print(O.format("{:s} ({VT.Brown}padding : {:s}{VT.Default})\n",
187*d8b80295SApple OSS Distributions                        format_offset(m_previous_offset, _previous_size),
188*d8b80295SApple OSS Distributions                        format_num(m_hole_bits)))
189*d8b80295SApple OSS Distributions
190*d8b80295SApple OSS Distributions    print("}")
191*d8b80295SApple OSS Distributions
192*d8b80295SApple OSS Distributions@lldb_command('showstructpacking', "X" , fancy=True)
193*d8b80295SApple OSS Distributionsdef showStructInfo(cmd_args=None, cmd_options={}, O=None):
194*d8b80295SApple OSS Distributions    """ Show how a structure is packed in the binary.
195*d8b80295SApple OSS Distributions
196*d8b80295SApple OSS Distributions        Usage: showstructpacking [-X] <type name>
197*d8b80295SApple OSS Distributions            -X : prints struct members offsets and sizes in a hexadecimal format (decimal is default)
198*d8b80295SApple OSS Distributions
199*d8b80295SApple OSS Distributions        The format is:
200*d8b80295SApple OSS Distributions            <offset>, [<size_of_member>] (<type>) <name>
201*d8b80295SApple OSS Distributions
202*d8b80295SApple OSS Distributions        Example:
203*d8b80295SApple OSS Distributions            (lldb) showstructpacking pollfd
204*d8b80295SApple OSS Distributions                0,[   8] struct pollfd {
205*d8b80295SApple OSS Distributions                    0,[   4] (int) fd
206*d8b80295SApple OSS Distributions                    4,[   2] (short) events
207*d8b80295SApple OSS Distributions                    6,[   2] (short) revents
208*d8b80295SApple OSS Distributions                }
209*d8b80295SApple OSS Distributions    """
210*d8b80295SApple OSS Distributions    if not cmd_args:
211*d8b80295SApple OSS Distributions        raise ArgumentError("Please provide a type name.")
212*d8b80295SApple OSS Distributions
213*d8b80295SApple OSS Distributions    ty_name = cmd_args[0]
214*d8b80295SApple OSS Distributions    try:
215*d8b80295SApple OSS Distributions        sym = gettype(ty_name)
216*d8b80295SApple OSS Distributions    except NameError:
217*d8b80295SApple OSS Distributions        return O.error("Cannot find type named {0}", ty_name)
218*d8b80295SApple OSS Distributions
219*d8b80295SApple OSS Distributions    if sym.GetTypeClass() == lldb.eTypeClassTypedef:
220*d8b80295SApple OSS Distributions        sym = sym.GetCanonicalType()
221*d8b80295SApple OSS Distributions
222*d8b80295SApple OSS Distributions    if sym.GetTypeClass() not in _UnionStructClass:
223*d8b80295SApple OSS Distributions        return O.error("{0} is not a structure/union/class type", ty_name)
224*d8b80295SApple OSS Distributions
225*d8b80295SApple OSS Distributions    ctx = (O, "-X" in cmd_options)
226*d8b80295SApple OSS Distributions
227*d8b80295SApple OSS Distributions    _showStructPacking(ctx, sym, 0)
228*d8b80295SApple OSS Distributions
229*d8b80295SApple OSS Distributions# EndMacro: showstructinto
230