Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 1 | # a wrapper for the Objective-C runtime for use by LLDB |
| 2 | import lldb |
| 3 | import cache |
| 4 | |
| 5 | class Utilities: |
| 6 | @staticmethod |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 7 | def read_ascii(process, pointer,max_len=128): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 8 | error = lldb.SBError() |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 9 | content = None |
| 10 | try: |
| 11 | content = process.ReadCStringFromMemory(pointer,max_len,error) |
| 12 | except: |
| 13 | pass |
| 14 | if content == None or len(content) == 0 or error.fail == True: |
| 15 | return None |
| 16 | return content |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 17 | |
| 18 | @staticmethod |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 19 | def is_valid_pointer(pointer, pointer_size, allow_tagged=False, allow_NULL=False): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 20 | if pointer == None: |
| 21 | return False |
| 22 | if pointer == 0: |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 23 | return allow_NULL |
| 24 | if allow_tagged and (pointer % 2) == 1: |
| 25 | return True |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 26 | return ((pointer % pointer_size) == 0) |
| 27 | |
| 28 | # Objective-C runtime has a rule that pointers in a class_t will only have bits 0 thru 46 set |
| 29 | # so if any pointer has bits 47 thru 63 high we know that this is not a valid isa |
| 30 | @staticmethod |
| 31 | def is_allowed_pointer(pointer): |
| 32 | if pointer == None: |
| 33 | return False |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 34 | return ((pointer & 0xFFFF800000000000) == 0) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 35 | |
| 36 | @staticmethod |
| 37 | def read_child_of(valobj,offset,type): |
| 38 | child = valobj.CreateChildAtOffset("childUNK",offset,type) |
| 39 | if child == None or child.IsValid() == False: |
| 40 | return None; |
| 41 | return child.GetValueAsUnsigned() |
| 42 | |
| 43 | @staticmethod |
| 44 | def is_valid_identifier(name): |
| 45 | if name is None: |
| 46 | return None |
| 47 | if len(name) == 0: |
| 48 | return None |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 49 | # technically, the ObjC runtime does not enforce any rules about what name a class can have |
| 50 | # in practice, the commonly used byte values for a class name are the letters, digits and some |
| 51 | # symbols: $, %, -, _, . |
| 52 | # WARNING: this means that you cannot use this runtime implementation if you need to deal |
| 53 | # with class names that use anything but what is allowed here |
| 54 | ok_values = dict.fromkeys("$%_.-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890") |
| 55 | return all(c in ok_values for c in name) |
| 56 | |
| 57 | @staticmethod |
| 58 | def check_is_osx_lion(target): |
| 59 | # assume the only thing that has a Foundation.framework is a Mac |
| 60 | # assume anything < Lion does not even exist |
| 61 | mod = target.module['Foundation'] |
| 62 | if mod == None or mod.IsValid() == False: |
| 63 | return None |
| 64 | ver = mod.GetVersion() |
| 65 | if ver == None or ver == []: |
| 66 | return None |
| 67 | return (ver[0] < 900) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 68 | |
| 69 | class RoT_Data: |
| 70 | def __init__(self,rot_pointer,params): |
| 71 | if (Utilities.is_valid_pointer(rot_pointer.GetValueAsUnsigned(),params.pointer_size, allow_tagged=False)): |
| 72 | self.sys_params = params |
| 73 | self.valobj = rot_pointer |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 74 | #self.flags = Utilities.read_child_of(self.valobj,0,self.sys_params.uint32_t) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 75 | self.instanceStart = Utilities.read_child_of(self.valobj,4,self.sys_params.uint32_t) |
| 76 | self.instanceSize = Utilities.read_child_of(self.valobj,8,self.sys_params.uint32_t) |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 77 | offset = 24 if self.sys_params.is_64_bit else 16 |
| 78 | #self.ivarLayoutPtr = Utilities.read_child_of(self.valobj,offset,self.sys_params.addr_ptr_type) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 79 | self.namePointer = Utilities.read_child_of(self.valobj,offset,self.sys_params.addr_ptr_type) |
| 80 | self.check_valid() |
| 81 | else: |
| 82 | self.valid = False |
| 83 | if self.valid: |
| 84 | self.name = Utilities.read_ascii(self.valobj.GetTarget().GetProcess(),self.namePointer) |
| 85 | if not(Utilities.is_valid_identifier(self.name)): |
| 86 | self.valid = False |
| 87 | |
| 88 | # perform sanity checks on the contents of this class_rw_t |
| 89 | def check_valid(self): |
| 90 | self.valid = True |
| 91 | # misaligned pointers seem to be possible for this field |
| 92 | #if not(Utilities.is_valid_pointer(self.namePointer,self.sys_params.pointer_size,allow_tagged=False)): |
| 93 | # self.valid = False |
| 94 | # pass |
| 95 | |
| 96 | def __str__(self): |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 97 | return \ |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 98 | "instanceStart = " + hex(self.instanceStart) + "\n" + \ |
| 99 | "instanceSize = " + hex(self.instanceSize) + "\n" + \ |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 100 | "namePointer = " + hex(self.namePointer) + " --> " + self.name |
| 101 | |
| 102 | def is_valid(self): |
| 103 | return self.valid |
| 104 | |
| 105 | |
| 106 | class RwT_Data: |
| 107 | def __init__(self,rwt_pointer,params): |
| 108 | if (Utilities.is_valid_pointer(rwt_pointer.GetValueAsUnsigned(),params.pointer_size, allow_tagged=False)): |
| 109 | self.sys_params = params |
| 110 | self.valobj = rwt_pointer |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 111 | #self.flags = Utilities.read_child_of(self.valobj,0,self.sys_params.uint32_t) |
| 112 | #self.version = Utilities.read_child_of(self.valobj,4,self.sys_params.uint32_t) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 113 | self.roPointer = Utilities.read_child_of(self.valobj,8,self.sys_params.addr_ptr_type) |
| 114 | self.check_valid() |
| 115 | else: |
| 116 | self.valid = False |
| 117 | if self.valid: |
| 118 | self.rot = self.valobj.CreateValueFromAddress("rot",self.roPointer,self.sys_params.addr_ptr_type).AddressOf() |
| 119 | self.data = RoT_Data(self.rot,self.sys_params) |
| 120 | |
| 121 | # perform sanity checks on the contents of this class_rw_t |
| 122 | def check_valid(self): |
| 123 | self.valid = True |
| 124 | if not(Utilities.is_valid_pointer(self.roPointer,self.sys_params.pointer_size,allow_tagged=False)): |
| 125 | self.valid = False |
| 126 | |
| 127 | def __str__(self): |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 128 | return \ |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 129 | "roPointer = " + hex(self.roPointer) |
| 130 | |
| 131 | def is_valid(self): |
| 132 | if self.valid: |
| 133 | return self.data.is_valid() |
| 134 | return False |
| 135 | |
| 136 | class Class_Data_V2: |
| 137 | def __init__(self,isa_pointer,params): |
| 138 | if (isa_pointer != None) and (Utilities.is_valid_pointer(isa_pointer.GetValueAsUnsigned(),params.pointer_size, allow_tagged=False)): |
| 139 | self.sys_params = params |
| 140 | self.valobj = isa_pointer |
| 141 | self.isaPointer = Utilities.read_child_of(self.valobj,0,self.sys_params.addr_ptr_type) |
| 142 | self.superclassIsaPointer = Utilities.read_child_of(self.valobj,1*self.sys_params.pointer_size,self.sys_params.addr_ptr_type) |
| 143 | self.cachePointer = Utilities.read_child_of(self.valobj,2*self.sys_params.pointer_size,self.sys_params.addr_ptr_type) |
| 144 | self.vtablePointer = Utilities.read_child_of(self.valobj,3*self.sys_params.pointer_size,self.sys_params.addr_ptr_type) |
| 145 | self.dataPointer = Utilities.read_child_of(self.valobj,4*self.sys_params.pointer_size,self.sys_params.addr_ptr_type) |
| 146 | self.check_valid() |
| 147 | else: |
| 148 | self.valid = False |
| 149 | if self.valid: |
| 150 | self.rwt = self.valobj.CreateValueFromAddress("rwt",self.dataPointer,self.sys_params.addr_ptr_type).AddressOf() |
| 151 | self.data = RwT_Data(self.rwt,self.sys_params) |
| 152 | |
| 153 | # perform sanity checks on the contents of this class_t |
| 154 | def check_valid(self): |
| 155 | self.valid = True |
| 156 | if not(Utilities.is_valid_pointer(self.isaPointer,self.sys_params.pointer_size,allow_tagged=False)): |
| 157 | self.valid = False |
| 158 | return |
| 159 | if not(Utilities.is_valid_pointer(self.superclassIsaPointer,self.sys_params.pointer_size,allow_tagged=False)): |
| 160 | # NULL is a valid value for superclass (it means we have reached NSObject) |
| 161 | if self.superclassIsaPointer != 0: |
| 162 | self.valid = False |
| 163 | return |
| 164 | if not(Utilities.is_valid_pointer(self.cachePointer,self.sys_params.pointer_size,allow_tagged=False)): |
| 165 | self.valid = False |
| 166 | return |
| 167 | if not(Utilities.is_valid_pointer(self.vtablePointer,self.sys_params.pointer_size,allow_tagged=False)): |
| 168 | self.valid = False |
| 169 | return |
| 170 | if not(Utilities.is_valid_pointer(self.dataPointer,self.sys_params.pointer_size,allow_tagged=False)): |
| 171 | self.valid = False |
| 172 | return |
| 173 | if not(Utilities.is_allowed_pointer(self.isaPointer)): |
| 174 | self.valid = False |
| 175 | return |
| 176 | if not(Utilities.is_allowed_pointer(self.superclassIsaPointer)): |
| 177 | # NULL is a valid value for superclass (it means we have reached NSObject) |
| 178 | if self.superclassIsaPointer != 0: |
| 179 | self.valid = False |
| 180 | return |
| 181 | if not(Utilities.is_allowed_pointer(self.cachePointer)): |
| 182 | self.valid = False |
| 183 | return |
| 184 | if not(Utilities.is_allowed_pointer(self.vtablePointer)): |
| 185 | self.valid = False |
| 186 | return |
| 187 | if not(Utilities.is_allowed_pointer(self.dataPointer)): |
| 188 | self.valid = False |
| 189 | return |
| 190 | |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 191 | # in general, KVO is implemented by transparently subclassing |
| 192 | # however, there could be exceptions where a class does something else |
| 193 | # internally to implement the feature - this method will have no clue that a class |
| 194 | # has been KVO'ed unless the standard implementation technique is used |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 195 | def is_kvo(self): |
| 196 | if self.is_valid(): |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 197 | if self.class_name().startswith("NSKVONotifying_"): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 198 | return True |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 199 | return False |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 200 | |
| 201 | def get_superclass(self): |
| 202 | if self.is_valid(): |
| 203 | parent_isa_pointer = self.valobj.CreateChildAtOffset("parent_isa", |
| 204 | self.sys_params.pointer_size, |
| 205 | self.sys_params.addr_ptr_type) |
| 206 | return Class_Data_V2(parent_isa_pointer,self.sys_params) |
| 207 | else: |
| 208 | return None |
| 209 | |
| 210 | def class_name(self): |
| 211 | if self.is_valid(): |
| 212 | return self.data.data.name |
| 213 | else: |
| 214 | return None |
| 215 | |
| 216 | def is_valid(self): |
| 217 | if self.valid: |
| 218 | return self.data.is_valid() |
| 219 | return False |
| 220 | |
| 221 | def __str__(self): |
| 222 | return 'isaPointer = ' + hex(self.isaPointer) + "\n" + \ |
| 223 | "superclassIsaPointer = " + hex(self.superclassIsaPointer) + "\n" + \ |
| 224 | "cachePointer = " + hex(self.cachePointer) + "\n" + \ |
| 225 | "vtablePointer = " + hex(self.vtablePointer) + "\n" + \ |
| 226 | "data = " + hex(self.dataPointer) |
| 227 | |
| 228 | def is_tagged(self): |
| 229 | return False |
| 230 | |
Enrico Granata | b8cbe9c | 2012-02-23 23:26:48 +0000 | [diff] [blame] | 231 | def instance_size(self,align=False): |
| 232 | if self.is_valid() == False: |
| 233 | return None |
| 234 | if align: |
| 235 | unalign = self.instance_size(False) |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 236 | if self.sys_params.is_64_bit: |
Enrico Granata | b8cbe9c | 2012-02-23 23:26:48 +0000 | [diff] [blame] | 237 | return ((unalign + 7) & ~7) % 0x100000000 |
| 238 | else: |
| 239 | return ((unalign + 3) & ~3) % 0x100000000 |
| 240 | else: |
| 241 | return self.rwt.rot.instanceSize |
| 242 | |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 243 | # runtime v1 is much less intricate than v2 and stores relevant information directly in the class_t object |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 244 | class Class_Data_V1: |
| 245 | def __init__(self,isa_pointer,params): |
| 246 | if (isa_pointer != None) and (Utilities.is_valid_pointer(isa_pointer.GetValueAsUnsigned(),params.pointer_size, allow_tagged=False)): |
Enrico Granata | 0448d9f | 2012-02-29 20:46:00 +0000 | [diff] [blame] | 247 | self.valid = True |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 248 | self.sys_params = params |
| 249 | self.valobj = isa_pointer |
| 250 | self.isaPointer = Utilities.read_child_of(self.valobj,0,self.sys_params.addr_ptr_type) |
| 251 | self.superclassIsaPointer = Utilities.read_child_of(self.valobj,1*self.sys_params.pointer_size,self.sys_params.addr_ptr_type) |
| 252 | self.namePointer = Utilities.read_child_of(self.valobj,2*self.sys_params.pointer_size,self.sys_params.addr_ptr_type) |
Enrico Granata | b8cbe9c | 2012-02-23 23:26:48 +0000 | [diff] [blame] | 253 | self.version = Utilities.read_child_of(self.valobj,3*self.sys_params.pointer_size,self.sys_params.addr_ptr_type) |
| 254 | self.info = Utilities.read_child_of(self.valobj,4*self.sys_params.pointer_size,self.sys_params.addr_ptr_type) |
| 255 | self.instanceSize = Utilities.read_child_of(self.valobj,5*self.sys_params.pointer_size,self.sys_params.addr_ptr_type) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 256 | else: |
| 257 | self.valid = False |
| 258 | if self.valid: |
| 259 | self.name = Utilities.read_ascii(self.valobj.GetTarget().GetProcess(),self.namePointer) |
| 260 | if not(Utilities.is_valid_identifier(self.name)): |
| 261 | self.valid = False |
| 262 | |
| 263 | # perform sanity checks on the contents of this class_t |
| 264 | def check_valid(self): |
| 265 | self.valid = True |
| 266 | if not(Utilities.is_valid_pointer(self.isaPointer,self.sys_params.pointer_size,allow_tagged=False)): |
| 267 | self.valid = False |
| 268 | return |
| 269 | if not(Utilities.is_valid_pointer(self.superclassIsaPointer,self.sys_params.pointer_size,allow_tagged=False)): |
| 270 | # NULL is a valid value for superclass (it means we have reached NSObject) |
| 271 | if self.superclassIsaPointer != 0: |
| 272 | self.valid = False |
| 273 | return |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 274 | if not(Utilities.is_valid_pointer(self.namePointer,self.sys_params.pointer_size,allow_tagged=False,allow_NULL=True)): |
| 275 | self.valid = False |
| 276 | return |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 277 | |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 278 | # in general, KVO is implemented by transparently subclassing |
| 279 | # however, there could be exceptions where a class does something else |
| 280 | # internally to implement the feature - this method will have no clue that a class |
| 281 | # has been KVO'ed unless the standard implementation technique is used |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 282 | def is_kvo(self): |
| 283 | if self.is_valid(): |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 284 | if self.class_name().startswith("NSKVONotifying_"): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 285 | return True |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 286 | return False |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 287 | |
| 288 | def get_superclass(self): |
| 289 | if self.is_valid(): |
| 290 | parent_isa_pointer = self.valobj.CreateChildAtOffset("parent_isa", |
| 291 | self.sys_params.pointer_size, |
| 292 | self.sys_params.addr_ptr_type) |
| 293 | return Class_Data_V1(parent_isa_pointer,self.sys_params) |
| 294 | else: |
| 295 | return None |
| 296 | |
| 297 | def class_name(self): |
| 298 | if self.is_valid(): |
| 299 | return self.name |
| 300 | else: |
| 301 | return None |
| 302 | |
| 303 | def is_valid(self): |
| 304 | return self.valid |
| 305 | |
| 306 | def __str__(self): |
| 307 | return 'isaPointer = ' + hex(self.isaPointer) + "\n" + \ |
| 308 | "superclassIsaPointer = " + hex(self.superclassIsaPointer) + "\n" + \ |
Enrico Granata | b8cbe9c | 2012-02-23 23:26:48 +0000 | [diff] [blame] | 309 | "namePointer = " + hex(self.namePointer) + " --> " + self.name + \ |
| 310 | "version = " + hex(self.version) + "\n" + \ |
| 311 | "info = " + hex(self.info) + "\n" + \ |
| 312 | "instanceSize = " + hex(self.instanceSize) + "\n" |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 313 | |
| 314 | def is_tagged(self): |
| 315 | return False |
| 316 | |
Enrico Granata | b8cbe9c | 2012-02-23 23:26:48 +0000 | [diff] [blame] | 317 | def instance_size(self,align=False): |
| 318 | if self.is_valid() == False: |
| 319 | return None |
| 320 | if align: |
| 321 | unalign = self.instance_size(False) |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 322 | if self.sys_params.is_64_bit: |
Enrico Granata | b8cbe9c | 2012-02-23 23:26:48 +0000 | [diff] [blame] | 323 | return ((unalign + 7) & ~7) % 0x100000000 |
| 324 | else: |
| 325 | return ((unalign + 3) & ~3) % 0x100000000 |
| 326 | else: |
| 327 | return self.instanceSize |
| 328 | |
Enrico Granata | 0448d9f | 2012-02-29 20:46:00 +0000 | [diff] [blame] | 329 | # these are the only tagged pointers values for current versions |
| 330 | # of OSX - this might change in future OS releases, and no-one is |
| 331 | # advised to rely on these values, or any of the bitmasking formulas |
| 332 | # in TaggedClass_Data. doing otherwise is at your own risk |
| 333 | TaggedClass_Values = {3 : 'NSNumber', \ |
| 334 | 5: 'NSManagedObject', \ |
| 335 | 6: 'NSDate', \ |
| 336 | 7: 'NSDateTS' }; |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 337 | |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 338 | class TaggedClass_Data: |
| 339 | def __init__(self,pointer,params): |
Enrico Granata | 0448d9f | 2012-02-29 20:46:00 +0000 | [diff] [blame] | 340 | global TaggedClass_Values |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 341 | self.valid = True |
| 342 | self.name = None |
| 343 | self.sys_params = params |
| 344 | self.valobj = pointer |
| 345 | self.val = (pointer & ~0x0000000000000000FF) >> 8 |
| 346 | self.class_bits = (pointer & 0xE) >> 1 |
| 347 | self.i_bits = (pointer & 0xF0) >> 4 |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 348 | |
Enrico Granata | 0448d9f | 2012-02-29 20:46:00 +0000 | [diff] [blame] | 349 | if self.class_bits in TaggedClass_Values: |
| 350 | self.name = TaggedClass_Values[self.class_bits] |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 351 | else: |
Enrico Granata | 0448d9f | 2012-02-29 20:46:00 +0000 | [diff] [blame] | 352 | self.valid = False |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 353 | |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 354 | |
| 355 | def is_valid(self): |
| 356 | return self.valid |
| 357 | |
| 358 | def class_name(self): |
| 359 | if self.is_valid(): |
| 360 | return self.name |
| 361 | else: |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 362 | return False |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 363 | |
| 364 | def value(self): |
| 365 | return self.val if self.is_valid() else None |
| 366 | |
| 367 | def info_bits(self): |
| 368 | return self.i_bits if self.is_valid() else None |
| 369 | |
| 370 | def is_kvo(self): |
| 371 | return False |
| 372 | |
| 373 | # we would need to go around looking for the superclass or ask the runtime |
| 374 | # for now, we seem not to require support for this operation so we will merrily |
| 375 | # pretend to be at a root point in the hierarchy |
| 376 | def get_superclass(self): |
| 377 | return None |
| 378 | |
| 379 | # anything that is handled here is tagged |
| 380 | def is_tagged(self): |
| 381 | return True |
| 382 | |
Enrico Granata | b8cbe9c | 2012-02-23 23:26:48 +0000 | [diff] [blame] | 383 | # it seems reasonable to say that a tagged pointer is the size of a pointer |
| 384 | def instance_size(self,align=False): |
| 385 | if self.is_valid() == False: |
| 386 | return None |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 387 | return 8 if self.sys_params.is_64_bit else 4 |
Enrico Granata | b8cbe9c | 2012-02-23 23:26:48 +0000 | [diff] [blame] | 388 | |
| 389 | |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 390 | class InvalidClass_Data: |
| 391 | def __init__(self): |
| 392 | pass |
| 393 | def is_valid(self): |
| 394 | return False |
| 395 | |
| 396 | runtime_version = cache.Cache() |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 397 | os_version = cache.Cache() |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 398 | |
| 399 | class SystemParameters: |
| 400 | def __init__(self,valobj): |
| 401 | self.adjust_for_architecture(valobj) |
| 402 | |
| 403 | def adjust_for_architecture(self,valobj): |
| 404 | self.process = valobj.GetTarget().GetProcess() |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 405 | self.is_64_bit = (self.process.GetAddressByteSize() == 8) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 406 | self.is_little = (self.process.GetByteOrder() == lldb.eByteOrderLittle) |
| 407 | self.pointer_size = self.process.GetAddressByteSize() |
| 408 | self.addr_type = valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) |
| 409 | self.addr_ptr_type = self.addr_type.GetPointerType() |
| 410 | self.uint32_t = valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt) |
| 411 | global runtime_version |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 412 | global os_version |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 413 | pid = self.process.GetProcessID() |
Enrico Granata | 21b2236 | 2012-02-24 19:46:04 +0000 | [diff] [blame] | 414 | if runtime_version.look_for_key(pid): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 415 | self.runtime_version = runtime_version.get_value(pid) |
| 416 | else: |
| 417 | self.runtime_version = ObjCRuntime.runtime_version(self.process) |
| 418 | runtime_version.add_item(pid,self.runtime_version) |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 419 | if os_version.look_for_key(pid): |
| 420 | self.is_lion = os_version.get_value(pid) |
| 421 | else: |
| 422 | self.is_lion = Utilities.check_is_osx_lion(valobj.GetTarget()) |
| 423 | os_version.add_item(pid,self.is_lion) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 424 | |
| 425 | isa_cache = cache.Cache() |
| 426 | |
| 427 | class ObjCRuntime: |
| 428 | |
| 429 | # the ObjC runtime has no explicit "version" field that we can use |
| 430 | # instead, we discriminate v1 from v2 by looking for the presence |
| 431 | # of a well-known section only present in v1 |
| 432 | @staticmethod |
| 433 | def runtime_version(process): |
| 434 | if process.IsValid() == False: |
| 435 | return None |
| 436 | target = process.GetTarget() |
| 437 | num_modules = target.GetNumModules() |
| 438 | module_objc = None |
| 439 | for idx in range(num_modules): |
| 440 | module = target.GetModuleAtIndex(idx) |
| 441 | if module.GetFileSpec().GetFilename() == 'libobjc.A.dylib': |
| 442 | module_objc = module |
| 443 | break |
| 444 | if module_objc == None or module_objc.IsValid() == False: |
| 445 | return None |
| 446 | num_sections = module.GetNumSections() |
| 447 | section_objc = None |
| 448 | for idx in range(num_sections): |
| 449 | section = module.GetSectionAtIndex(idx) |
| 450 | if section.GetName() == '__OBJC': |
| 451 | section_objc = section |
| 452 | break |
| 453 | if section_objc != None and section_objc.IsValid(): |
| 454 | return 1 |
| 455 | return 2 |
| 456 | |
| 457 | def __init__(self,valobj): |
| 458 | self.valobj = valobj |
| 459 | self.adjust_for_architecture() |
| 460 | self.sys_params = SystemParameters(self.valobj) |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 461 | self.unsigned_value = self.valobj.GetValueAsUnsigned() |
| 462 | self.isa_value = None |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 463 | |
| 464 | def adjust_for_architecture(self): |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 465 | self.is_64_bit = (self.valobj.GetTarget().GetProcess().GetAddressByteSize() == 8) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 466 | self.is_little = (self.valobj.GetTarget().GetProcess().GetByteOrder() == lldb.eByteOrderLittle) |
| 467 | self.pointer_size = self.valobj.GetTarget().GetProcess().GetAddressByteSize() |
| 468 | self.addr_type = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) |
| 469 | self.addr_ptr_type = self.addr_type.GetPointerType() |
| 470 | |
| 471 | # an ObjC pointer can either be tagged or must be aligned |
| 472 | def is_tagged(self): |
| 473 | if self.valobj is None: |
| 474 | return False |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 475 | return (Utilities.is_valid_pointer(self.unsigned_value,self.pointer_size, allow_tagged=True) and \ |
| 476 | not(Utilities.is_valid_pointer(self.unsigned_value,self.pointer_size, allow_tagged=False))) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 477 | |
| 478 | def is_valid(self): |
| 479 | if self.valobj is None: |
| 480 | return False |
| 481 | if self.valobj.IsInScope() == False: |
| 482 | return False |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 483 | return Utilities.is_valid_pointer(self.unsigned_value,self.pointer_size, allow_tagged=True) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 484 | |
| 485 | def read_isa(self): |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 486 | if self.isa_value != None: |
| 487 | return self.isa_value |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 488 | isa_pointer = self.valobj.CreateChildAtOffset("cfisa", |
| 489 | 0, |
| 490 | self.addr_ptr_type) |
| 491 | if isa_pointer == None or isa_pointer.IsValid() == False: |
| 492 | return None; |
| 493 | if isa_pointer.GetValueAsUnsigned(1) == 1: |
| 494 | return None; |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 495 | self.isa_value = isa_pointer |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 496 | return isa_pointer |
| 497 | |
| 498 | def read_class_data(self): |
| 499 | global isa_cache |
| 500 | if self.is_tagged(): |
| 501 | # tagged pointers only exist in ObjC v2 |
| 502 | if self.sys_params.runtime_version == 2: |
| 503 | # not every odd-valued pointer is actually tagged. most are just plain wrong |
| 504 | # we could try and predetect this before even creating a TaggedClass_Data object |
| 505 | # but unless performance requires it, this seems a cleaner way to tackle the task |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 506 | tentative_tagged = TaggedClass_Data(self.unsigned_value,self.sys_params) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 507 | return tentative_tagged if tentative_tagged.is_valid() else InvalidClass_Data() |
| 508 | else: |
| 509 | return InvalidClass_Data() |
| 510 | if self.is_valid() == False: |
| 511 | return InvalidClass_Data() |
| 512 | isa = self.read_isa() |
| 513 | if isa == None: |
| 514 | return InvalidClass_Data() |
| 515 | isa_value = isa.GetValueAsUnsigned(1) |
| 516 | if isa_value == 1: |
| 517 | return InvalidClass_Data() |
| 518 | data = isa_cache.get_value(isa_value,default=None) |
| 519 | if data != None: |
| 520 | return data |
| 521 | if self.sys_params.runtime_version == 2: |
| 522 | data = Class_Data_V2(isa,self.sys_params) |
| 523 | else: |
| 524 | data = Class_Data_V1(isa,self.sys_params) |
| 525 | if data == None: |
| 526 | return InvalidClass_Data() |
| 527 | if data.is_valid(): |
| 528 | isa_cache.add_item(isa_value,data,ok_to_replace=True) |
| 529 | return data |
Enrico Granata | b8cbe9c | 2012-02-23 23:26:48 +0000 | [diff] [blame] | 530 | |