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