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