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