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