Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 1 | """ |
| 2 | Objective-C runtime wrapper for use by LLDB Python formatters |
| 3 | |
| 4 | part of The LLVM Compiler Infrastructure |
| 5 | This file is distributed under the University of Illinois Open Source |
| 6 | License. See LICENSE.TXT for details. |
| 7 | """ |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 8 | import lldb |
| 9 | import cache |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 10 | import attrib_fromdict |
| 11 | import functools |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 12 | |
| 13 | class Utilities: |
| 14 | @staticmethod |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 15 | def read_ascii(process, pointer,max_len=128): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 16 | error = lldb.SBError() |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 17 | content = None |
| 18 | try: |
| 19 | content = process.ReadCStringFromMemory(pointer,max_len,error) |
| 20 | except: |
| 21 | pass |
| 22 | if content == None or len(content) == 0 or error.fail == True: |
| 23 | return None |
| 24 | return content |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 25 | |
| 26 | @staticmethod |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 27 | 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] | 28 | if pointer == None: |
| 29 | return False |
| 30 | if pointer == 0: |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 31 | return allow_NULL |
| 32 | if allow_tagged and (pointer % 2) == 1: |
| 33 | return True |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 34 | return ((pointer % pointer_size) == 0) |
| 35 | |
| 36 | # Objective-C runtime has a rule that pointers in a class_t will only have bits 0 thru 46 set |
| 37 | # so if any pointer has bits 47 thru 63 high we know that this is not a valid isa |
| 38 | @staticmethod |
| 39 | def is_allowed_pointer(pointer): |
| 40 | if pointer == None: |
| 41 | return False |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 42 | return ((pointer & 0xFFFF800000000000) == 0) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 43 | |
| 44 | @staticmethod |
| 45 | def read_child_of(valobj,offset,type): |
| 46 | child = valobj.CreateChildAtOffset("childUNK",offset,type) |
| 47 | if child == None or child.IsValid() == False: |
| 48 | return None; |
| 49 | return child.GetValueAsUnsigned() |
| 50 | |
| 51 | @staticmethod |
| 52 | def is_valid_identifier(name): |
| 53 | if name is None: |
| 54 | return None |
| 55 | if len(name) == 0: |
| 56 | return None |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 57 | # technically, the ObjC runtime does not enforce any rules about what name a class can have |
| 58 | # in practice, the commonly used byte values for a class name are the letters, digits and some |
| 59 | # symbols: $, %, -, _, . |
| 60 | # WARNING: this means that you cannot use this runtime implementation if you need to deal |
| 61 | # with class names that use anything but what is allowed here |
| 62 | ok_values = dict.fromkeys("$%_.-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890") |
| 63 | return all(c in ok_values for c in name) |
| 64 | |
| 65 | @staticmethod |
| 66 | def check_is_osx_lion(target): |
| 67 | # assume the only thing that has a Foundation.framework is a Mac |
| 68 | # assume anything < Lion does not even exist |
| 69 | mod = target.module['Foundation'] |
| 70 | if mod == None or mod.IsValid() == False: |
| 71 | return None |
| 72 | ver = mod.GetVersion() |
| 73 | if ver == None or ver == []: |
| 74 | return None |
| 75 | return (ver[0] < 900) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 76 | |
| 77 | class RoT_Data: |
| 78 | def __init__(self,rot_pointer,params): |
| 79 | if (Utilities.is_valid_pointer(rot_pointer.GetValueAsUnsigned(),params.pointer_size, allow_tagged=False)): |
| 80 | self.sys_params = params |
| 81 | self.valobj = rot_pointer |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 82 | #self.flags = Utilities.read_child_of(self.valobj,0,self.sys_params.uint32_t) |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 83 | #self.instanceStart = Utilities.read_child_of(self.valobj,4,self.sys_params.uint32_t) |
| 84 | self.instanceSize = None # lazy fetching |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 85 | offset = 24 if self.sys_params.is_64_bit else 16 |
| 86 | #self.ivarLayoutPtr = Utilities.read_child_of(self.valobj,offset,self.sys_params.addr_ptr_type) |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 87 | self.namePointer = Utilities.read_child_of(self.valobj,offset,self.sys_params.types_cache.addr_ptr_type) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 88 | self.check_valid() |
| 89 | else: |
| 90 | self.valid = False |
| 91 | if self.valid: |
| 92 | self.name = Utilities.read_ascii(self.valobj.GetTarget().GetProcess(),self.namePointer) |
| 93 | if not(Utilities.is_valid_identifier(self.name)): |
| 94 | self.valid = False |
| 95 | |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 96 | # perform sanity checks on the contents of this class_ro_t |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 97 | def check_valid(self): |
| 98 | self.valid = True |
| 99 | # misaligned pointers seem to be possible for this field |
| 100 | #if not(Utilities.is_valid_pointer(self.namePointer,self.sys_params.pointer_size,allow_tagged=False)): |
| 101 | # self.valid = False |
| 102 | # pass |
| 103 | |
| 104 | def __str__(self): |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 105 | return \ |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 106 | "instanceSize = " + hex(self.instance_size()) + "\n" + \ |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 107 | "namePointer = " + hex(self.namePointer) + " --> " + self.name |
| 108 | |
| 109 | def is_valid(self): |
| 110 | return self.valid |
| 111 | |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 112 | def instance_size(self,align=False): |
| 113 | if self.is_valid() == False: |
| 114 | return None |
| 115 | if self.instanceSize == None: |
| 116 | self.instanceSize = Utilities.read_child_of(self.valobj,8,self.sys_params.types_cache.uint32_t) |
| 117 | if align: |
| 118 | unalign = self.instance_size(False) |
| 119 | if self.sys_params.is_64_bit: |
| 120 | return ((unalign + 7) & ~7) % 0x100000000 |
| 121 | else: |
| 122 | return ((unalign + 3) & ~3) % 0x100000000 |
| 123 | else: |
| 124 | return self.instanceSize |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 125 | |
| 126 | class RwT_Data: |
| 127 | def __init__(self,rwt_pointer,params): |
| 128 | if (Utilities.is_valid_pointer(rwt_pointer.GetValueAsUnsigned(),params.pointer_size, allow_tagged=False)): |
| 129 | self.sys_params = params |
| 130 | self.valobj = rwt_pointer |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 131 | #self.flags = Utilities.read_child_of(self.valobj,0,self.sys_params.uint32_t) |
| 132 | #self.version = Utilities.read_child_of(self.valobj,4,self.sys_params.uint32_t) |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 133 | self.roPointer = Utilities.read_child_of(self.valobj,8,self.sys_params.types_cache.addr_ptr_type) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 134 | self.check_valid() |
| 135 | else: |
| 136 | self.valid = False |
| 137 | if self.valid: |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 138 | self.rot = self.valobj.CreateValueFromAddress("rot",self.roPointer,self.sys_params.types_cache.addr_ptr_type).AddressOf() |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 139 | self.data = RoT_Data(self.rot,self.sys_params) |
| 140 | |
| 141 | # perform sanity checks on the contents of this class_rw_t |
| 142 | def check_valid(self): |
| 143 | self.valid = True |
| 144 | if not(Utilities.is_valid_pointer(self.roPointer,self.sys_params.pointer_size,allow_tagged=False)): |
| 145 | self.valid = False |
| 146 | |
| 147 | def __str__(self): |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 148 | return \ |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 149 | "roPointer = " + hex(self.roPointer) |
| 150 | |
| 151 | def is_valid(self): |
| 152 | if self.valid: |
| 153 | return self.data.is_valid() |
| 154 | return False |
| 155 | |
| 156 | class Class_Data_V2: |
| 157 | def __init__(self,isa_pointer,params): |
| 158 | if (isa_pointer != None) and (Utilities.is_valid_pointer(isa_pointer.GetValueAsUnsigned(),params.pointer_size, allow_tagged=False)): |
| 159 | self.sys_params = params |
| 160 | self.valobj = isa_pointer |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 161 | self.check_valid() |
| 162 | else: |
| 163 | self.valid = False |
| 164 | if self.valid: |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 165 | self.rwt = self.valobj.CreateValueFromAddress("rwt",self.dataPointer,self.sys_params.types_cache.addr_ptr_type).AddressOf() |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 166 | self.data = RwT_Data(self.rwt,self.sys_params) |
| 167 | |
| 168 | # perform sanity checks on the contents of this class_t |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 169 | # this call tries to minimize the amount of data fetched- as soon as we have "proven" |
| 170 | # that we have an invalid object, we stop reading |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 171 | def check_valid(self): |
| 172 | self.valid = True |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 173 | |
| 174 | self.isaPointer = Utilities.read_child_of(self.valobj,0,self.sys_params.types_cache.addr_ptr_type) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 175 | if not(Utilities.is_valid_pointer(self.isaPointer,self.sys_params.pointer_size,allow_tagged=False)): |
| 176 | self.valid = False |
| 177 | return |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 178 | if not(Utilities.is_allowed_pointer(self.isaPointer)): |
| 179 | self.valid = False |
| 180 | return |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 181 | |
| 182 | self.cachePointer = Utilities.read_child_of(self.valobj,2*self.sys_params.pointer_size,self.sys_params.types_cache.addr_ptr_type) |
| 183 | if not(Utilities.is_valid_pointer(self.cachePointer,self.sys_params.pointer_size,allow_tagged=False)): |
| 184 | self.valid = False |
| 185 | return |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 186 | if not(Utilities.is_allowed_pointer(self.cachePointer)): |
| 187 | self.valid = False |
| 188 | return |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 189 | |
| 190 | self.vtablePointer = Utilities.read_child_of(self.valobj,3*self.sys_params.pointer_size,self.sys_params.types_cache.addr_ptr_type) |
| 191 | if not(Utilities.is_valid_pointer(self.vtablePointer,self.sys_params.pointer_size,allow_tagged=False)): |
| 192 | self.valid = False |
| 193 | return |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 194 | if not(Utilities.is_allowed_pointer(self.vtablePointer)): |
| 195 | self.valid = False |
| 196 | return |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 197 | |
| 198 | self.dataPointer = Utilities.read_child_of(self.valobj,4*self.sys_params.pointer_size,self.sys_params.types_cache.addr_ptr_type) |
| 199 | if not(Utilities.is_valid_pointer(self.dataPointer,self.sys_params.pointer_size,allow_tagged=False)): |
| 200 | self.valid = False |
| 201 | return |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 202 | if not(Utilities.is_allowed_pointer(self.dataPointer)): |
| 203 | self.valid = False |
| 204 | return |
| 205 | |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 206 | self.superclassIsaPointer = Utilities.read_child_of(self.valobj,1*self.sys_params.pointer_size,self.sys_params.types_cache.addr_ptr_type) |
| 207 | if not(Utilities.is_valid_pointer(self.superclassIsaPointer,self.sys_params.pointer_size,allow_tagged=False, allow_NULL=True)): |
| 208 | self.valid = False |
| 209 | return |
| 210 | if not(Utilities.is_allowed_pointer(self.superclassIsaPointer)): |
| 211 | self.valid = False |
| 212 | return |
| 213 | |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 214 | # in general, KVO is implemented by transparently subclassing |
| 215 | # however, there could be exceptions where a class does something else |
| 216 | # internally to implement the feature - this method will have no clue that a class |
| 217 | # has been KVO'ed unless the standard implementation technique is used |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 218 | def is_kvo(self): |
| 219 | if self.is_valid(): |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 220 | if self.class_name().startswith("NSKVONotifying_"): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 221 | return True |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 222 | return False |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 223 | |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 224 | # some CF classes have a valid ObjC isa in their CFRuntimeBase |
| 225 | # but instead of being class-specific this isa points to a match-'em-all class |
| 226 | # which is __NSCFType (the versions without __ also exists and we are matching to it |
| 227 | # just to be on the safe side) |
| 228 | def is_cftype(self): |
| 229 | if self.is_valid(): |
| 230 | return self.name == '__NSCFType' or self.name == 'NSCFType' |
| 231 | |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 232 | def get_superclass(self): |
| 233 | if self.is_valid(): |
| 234 | parent_isa_pointer = self.valobj.CreateChildAtOffset("parent_isa", |
| 235 | self.sys_params.pointer_size, |
| 236 | self.sys_params.addr_ptr_type) |
| 237 | return Class_Data_V2(parent_isa_pointer,self.sys_params) |
| 238 | else: |
| 239 | return None |
| 240 | |
| 241 | def class_name(self): |
| 242 | if self.is_valid(): |
| 243 | return self.data.data.name |
| 244 | else: |
| 245 | return None |
| 246 | |
| 247 | def is_valid(self): |
| 248 | if self.valid: |
| 249 | return self.data.is_valid() |
| 250 | return False |
| 251 | |
| 252 | def __str__(self): |
| 253 | return 'isaPointer = ' + hex(self.isaPointer) + "\n" + \ |
| 254 | "superclassIsaPointer = " + hex(self.superclassIsaPointer) + "\n" + \ |
| 255 | "cachePointer = " + hex(self.cachePointer) + "\n" + \ |
| 256 | "vtablePointer = " + hex(self.vtablePointer) + "\n" + \ |
| 257 | "data = " + hex(self.dataPointer) |
| 258 | |
| 259 | def is_tagged(self): |
| 260 | return False |
| 261 | |
Enrico Granata | b8cbe9c | 2012-02-23 23:26:48 +0000 | [diff] [blame] | 262 | def instance_size(self,align=False): |
| 263 | if self.is_valid() == False: |
| 264 | return None |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 265 | return self.rwt.rot.instance_size(align) |
Enrico Granata | b8cbe9c | 2012-02-23 23:26:48 +0000 | [diff] [blame] | 266 | |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 267 | # 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] | 268 | class Class_Data_V1: |
| 269 | def __init__(self,isa_pointer,params): |
| 270 | 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] | 271 | self.valid = True |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 272 | self.sys_params = params |
| 273 | self.valobj = isa_pointer |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 274 | self.check_valid() |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 275 | else: |
| 276 | self.valid = False |
| 277 | if self.valid: |
| 278 | self.name = Utilities.read_ascii(self.valobj.GetTarget().GetProcess(),self.namePointer) |
| 279 | if not(Utilities.is_valid_identifier(self.name)): |
| 280 | self.valid = False |
| 281 | |
| 282 | # perform sanity checks on the contents of this class_t |
| 283 | def check_valid(self): |
| 284 | self.valid = True |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 285 | |
| 286 | self.isaPointer = Utilities.read_child_of(self.valobj,0,self.sys_params.types_cache.addr_ptr_type) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 287 | if not(Utilities.is_valid_pointer(self.isaPointer,self.sys_params.pointer_size,allow_tagged=False)): |
| 288 | self.valid = False |
| 289 | return |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 290 | |
| 291 | self.superclassIsaPointer = Utilities.read_child_of(self.valobj,1*self.sys_params.pointer_size,self.sys_params.types_cache.addr_ptr_type) |
| 292 | if not(Utilities.is_valid_pointer(self.superclassIsaPointer,self.sys_params.pointer_size,allow_tagged=False,allow_NULL=True)): |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 293 | self.valid = False |
| 294 | return |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 295 | |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 296 | self.namePointer = Utilities.read_child_of(self.valobj,2*self.sys_params.pointer_size,self.sys_params.types_cache.addr_ptr_type) |
| 297 | #if not(Utilities.is_valid_pointer(self.namePointer,self.sys_params.pointer_size,allow_tagged=False,allow_NULL=False)): |
| 298 | # self.valid = False |
| 299 | # return |
| 300 | |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 301 | # in general, KVO is implemented by transparently subclassing |
| 302 | # however, there could be exceptions where a class does something else |
| 303 | # internally to implement the feature - this method will have no clue that a class |
| 304 | # has been KVO'ed unless the standard implementation technique is used |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 305 | def is_kvo(self): |
| 306 | if self.is_valid(): |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 307 | if self.class_name().startswith("NSKVONotifying_"): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 308 | return True |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 309 | return False |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 310 | |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 311 | # some CF classes have a valid ObjC isa in their CFRuntimeBase |
| 312 | # but instead of being class-specific this isa points to a match-'em-all class |
| 313 | # which is __NSCFType (the versions without __ also exists and we are matching to it |
| 314 | # just to be on the safe side) |
| 315 | def is_cftype(self): |
| 316 | if self.is_valid(): |
| 317 | return self.name == '__NSCFType' or self.name == 'NSCFType' |
| 318 | |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 319 | def get_superclass(self): |
| 320 | if self.is_valid(): |
| 321 | parent_isa_pointer = self.valobj.CreateChildAtOffset("parent_isa", |
| 322 | self.sys_params.pointer_size, |
| 323 | self.sys_params.addr_ptr_type) |
| 324 | return Class_Data_V1(parent_isa_pointer,self.sys_params) |
| 325 | else: |
| 326 | return None |
| 327 | |
| 328 | def class_name(self): |
| 329 | if self.is_valid(): |
| 330 | return self.name |
| 331 | else: |
| 332 | return None |
| 333 | |
| 334 | def is_valid(self): |
| 335 | return self.valid |
| 336 | |
| 337 | def __str__(self): |
| 338 | return 'isaPointer = ' + hex(self.isaPointer) + "\n" + \ |
| 339 | "superclassIsaPointer = " + hex(self.superclassIsaPointer) + "\n" + \ |
Enrico Granata | b8cbe9c | 2012-02-23 23:26:48 +0000 | [diff] [blame] | 340 | "namePointer = " + hex(self.namePointer) + " --> " + self.name + \ |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 341 | "instanceSize = " + hex(self.instanceSize()) + "\n" |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 342 | |
| 343 | def is_tagged(self): |
| 344 | return False |
| 345 | |
Enrico Granata | b8cbe9c | 2012-02-23 23:26:48 +0000 | [diff] [blame] | 346 | def instance_size(self,align=False): |
| 347 | if self.is_valid() == False: |
| 348 | return None |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 349 | if self.instanceSize == None: |
| 350 | self.instanceSize = Utilities.read_child_of(self.valobj,5*self.sys_params.pointer_size,self.sys_params.types_cache.addr_ptr_type) |
Enrico Granata | b8cbe9c | 2012-02-23 23:26:48 +0000 | [diff] [blame] | 351 | if align: |
| 352 | unalign = self.instance_size(False) |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 353 | if self.sys_params.is_64_bit: |
Enrico Granata | b8cbe9c | 2012-02-23 23:26:48 +0000 | [diff] [blame] | 354 | return ((unalign + 7) & ~7) % 0x100000000 |
| 355 | else: |
| 356 | return ((unalign + 3) & ~3) % 0x100000000 |
| 357 | else: |
| 358 | return self.instanceSize |
| 359 | |
Enrico Granata | 0448d9f | 2012-02-29 20:46:00 +0000 | [diff] [blame] | 360 | # these are the only tagged pointers values for current versions |
Enrico Granata | 0c489f5 | 2012-03-01 04:24:26 +0000 | [diff] [blame] | 361 | # of OSX - they might change in future OS releases, and no-one is |
Enrico Granata | 0448d9f | 2012-02-29 20:46:00 +0000 | [diff] [blame] | 362 | # advised to rely on these values, or any of the bitmasking formulas |
| 363 | # in TaggedClass_Data. doing otherwise is at your own risk |
Enrico Granata | 0c489f5 | 2012-03-01 04:24:26 +0000 | [diff] [blame] | 364 | TaggedClass_Values_Lion = {1 : 'NSNumber', \ |
| 365 | 5: 'NSManagedObject', \ |
| 366 | 6: 'NSDate', \ |
| 367 | 7: 'NSDateTS' }; |
| 368 | TaggedClass_Values_NMOS = {0: 'NSAtom', \ |
| 369 | 3 : 'NSNumber', \ |
| 370 | 4: 'NSDateTS', \ |
| 371 | 5: 'NSManagedObject', \ |
| 372 | 6: 'NSDate' }; |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 373 | |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 374 | class TaggedClass_Data: |
| 375 | def __init__(self,pointer,params): |
Enrico Granata | 0c489f5 | 2012-03-01 04:24:26 +0000 | [diff] [blame] | 376 | global TaggedClass_Values_Lion,TaggedClass_Values_NMOS |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 377 | self.valid = True |
| 378 | self.name = None |
| 379 | self.sys_params = params |
| 380 | self.valobj = pointer |
| 381 | self.val = (pointer & ~0x0000000000000000FF) >> 8 |
| 382 | self.class_bits = (pointer & 0xE) >> 1 |
| 383 | self.i_bits = (pointer & 0xF0) >> 4 |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 384 | |
Enrico Granata | 0c489f5 | 2012-03-01 04:24:26 +0000 | [diff] [blame] | 385 | if self.sys_params.is_lion: |
| 386 | if self.class_bits in TaggedClass_Values_Lion: |
| 387 | self.name = TaggedClass_Values_Lion[self.class_bits] |
| 388 | else: |
| 389 | self.valid = False |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 390 | else: |
Enrico Granata | 0c489f5 | 2012-03-01 04:24:26 +0000 | [diff] [blame] | 391 | if self.class_bits in TaggedClass_Values_NMOS: |
| 392 | self.name = TaggedClass_Values_NMOS[self.class_bits] |
| 393 | else: |
| 394 | self.valid = False |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 395 | |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 396 | |
| 397 | def is_valid(self): |
| 398 | return self.valid |
| 399 | |
| 400 | def class_name(self): |
| 401 | if self.is_valid(): |
| 402 | return self.name |
| 403 | else: |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 404 | return False |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 405 | |
| 406 | def value(self): |
| 407 | return self.val if self.is_valid() else None |
| 408 | |
| 409 | def info_bits(self): |
| 410 | return self.i_bits if self.is_valid() else None |
| 411 | |
| 412 | def is_kvo(self): |
| 413 | return False |
| 414 | |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 415 | def is_cftype(self): |
| 416 | return False |
| 417 | |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 418 | # we would need to go around looking for the superclass or ask the runtime |
| 419 | # for now, we seem not to require support for this operation so we will merrily |
| 420 | # pretend to be at a root point in the hierarchy |
| 421 | def get_superclass(self): |
| 422 | return None |
| 423 | |
| 424 | # anything that is handled here is tagged |
| 425 | def is_tagged(self): |
| 426 | return True |
| 427 | |
Enrico Granata | b8cbe9c | 2012-02-23 23:26:48 +0000 | [diff] [blame] | 428 | # it seems reasonable to say that a tagged pointer is the size of a pointer |
| 429 | def instance_size(self,align=False): |
| 430 | if self.is_valid() == False: |
| 431 | return None |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 432 | return self.sys_params.pointer_size |
Enrico Granata | b8cbe9c | 2012-02-23 23:26:48 +0000 | [diff] [blame] | 433 | |
| 434 | |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 435 | class InvalidClass_Data: |
| 436 | def __init__(self): |
| 437 | pass |
| 438 | def is_valid(self): |
| 439 | return False |
| 440 | |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 441 | @functools.total_ordering |
| 442 | class Version: |
| 443 | def __init__(self, major, minor, release, build_string): |
| 444 | self._major = major |
| 445 | self._minor = minor |
| 446 | self._release = release |
| 447 | self._build_string = build_string |
| 448 | |
| 449 | def get_major(self): |
| 450 | return self._major |
| 451 | def get_minor(self): |
| 452 | return self._minor |
| 453 | def get_release(self): |
| 454 | return self._release |
| 455 | def get_build_string(self): |
| 456 | return self._build_string |
| 457 | |
| 458 | major = property(get_major,None) |
| 459 | minor = property(get_minor,None) |
| 460 | release = property(get_release,None) |
| 461 | build_string = property(get_build_string,None) |
| 462 | |
| 463 | def __lt__(self,other): |
| 464 | if (self.major < other.major): |
| 465 | return True |
| 466 | if (self.minor < other.minor): |
| 467 | return True |
| 468 | if (self.release < other.release): |
| 469 | return True |
| 470 | # build strings are not compared since they are heavily platform-dependent and might not always |
| 471 | # be available |
| 472 | return False |
| 473 | |
| 474 | def __eq__(self,other): |
| 475 | return (self.major == other.major) and \ |
| 476 | (self.minor == other.minor) and \ |
| 477 | (self.release == other.release) and \ |
| 478 | (self.build_string == other.build_string) |
| 479 | |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 480 | runtime_version = cache.Cache() |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 481 | os_version = cache.Cache() |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 482 | types_caches = cache.Cache() |
| 483 | isa_caches = cache.Cache() |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 484 | |
| 485 | class SystemParameters: |
| 486 | def __init__(self,valobj): |
| 487 | self.adjust_for_architecture(valobj) |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 488 | self.adjust_for_process(valobj) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 489 | |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 490 | def adjust_for_process(self, valobj): |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 491 | global runtime_version |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 492 | global os_version |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 493 | global types_caches |
| 494 | global isa_caches |
| 495 | |
| 496 | process = valobj.GetTarget().GetProcess() |
| 497 | self.pid = process.GetProcessID() |
| 498 | |
| 499 | if runtime_version.look_for_key(self.pid): |
| 500 | self.runtime_version = runtime_version.get_value(self.pid) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 501 | else: |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 502 | self.runtime_version = ObjCRuntime.runtime_version(process) |
| 503 | runtime_version.add_item(self.pid,self.runtime_version) |
| 504 | |
| 505 | if os_version.look_for_key(self.pid): |
| 506 | self.is_lion = os_version.get_value(self.pid) |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 507 | else: |
| 508 | self.is_lion = Utilities.check_is_osx_lion(valobj.GetTarget()) |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 509 | os_version.add_item(self.pid,self.is_lion) |
| 510 | |
| 511 | if types_caches.look_for_key(self.pid): |
| 512 | self.types_cache = types_caches.get_value(self.pid) |
| 513 | else: |
| 514 | self.types_cache = attrib_fromdict.AttributesDictionary(allow_reset=False) |
| 515 | self.types_cache.addr_type = valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong) |
| 516 | self.types_cache.addr_ptr_type = self.types_cache.addr_type.GetPointerType() |
| 517 | self.types_cache.uint32_t = valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt) |
| 518 | types_caches.add_item(self.pid,self.types_cache) |
| 519 | |
| 520 | if isa_caches.look_for_key(self.pid): |
| 521 | self.isa_cache = isa_caches.get_value(self.pid) |
| 522 | else: |
| 523 | self.isa_cache = cache.Cache() |
| 524 | isa_caches.add_item(self.pid,self.isa_cache) |
| 525 | |
| 526 | def adjust_for_architecture(self,valobj): |
| 527 | process = valobj.GetTarget().GetProcess() |
| 528 | self.pointer_size = process.GetAddressByteSize() |
| 529 | self.is_64_bit = (self.pointer_size == 8) |
| 530 | self.is_little = (process.GetByteOrder() == lldb.eByteOrderLittle) |
Enrico Granata | 385ad4e | 2012-03-03 00:45:57 +0000 | [diff] [blame] | 531 | self.cfruntime_size = 16 if self.is_64_bit else 8 |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 532 | |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 533 | # a simple helper function that makes it more explicit that one is calculating |
| 534 | # an offset that is made up of X pointers and Y bytes of additional data |
| 535 | # taking into account pointer size - if you know there is going to be some padding |
| 536 | # you can pass that in and it will be taken into account (since padding may be different between |
| 537 | # 32 and 64 bit versions, you can pass padding value for both, the right one will be used) |
| 538 | def calculate_offset(self, num_pointers = 0, bytes_count = 0, padding32 = 0, padding64 = 0): |
| 539 | value = bytes_count + num_pointers*self.pointer_size |
| 540 | return value + padding64 if self.is_64_bit else value + padding32 |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 541 | |
| 542 | class ObjCRuntime: |
| 543 | |
| 544 | # the ObjC runtime has no explicit "version" field that we can use |
| 545 | # instead, we discriminate v1 from v2 by looking for the presence |
| 546 | # of a well-known section only present in v1 |
| 547 | @staticmethod |
| 548 | def runtime_version(process): |
| 549 | if process.IsValid() == False: |
| 550 | return None |
| 551 | target = process.GetTarget() |
| 552 | num_modules = target.GetNumModules() |
| 553 | module_objc = None |
| 554 | for idx in range(num_modules): |
| 555 | module = target.GetModuleAtIndex(idx) |
| 556 | if module.GetFileSpec().GetFilename() == 'libobjc.A.dylib': |
| 557 | module_objc = module |
| 558 | break |
| 559 | if module_objc == None or module_objc.IsValid() == False: |
| 560 | return None |
| 561 | num_sections = module.GetNumSections() |
| 562 | section_objc = None |
| 563 | for idx in range(num_sections): |
| 564 | section = module.GetSectionAtIndex(idx) |
| 565 | if section.GetName() == '__OBJC': |
| 566 | section_objc = section |
| 567 | break |
| 568 | if section_objc != None and section_objc.IsValid(): |
| 569 | return 1 |
| 570 | return 2 |
| 571 | |
| 572 | def __init__(self,valobj): |
| 573 | self.valobj = valobj |
| 574 | self.adjust_for_architecture() |
| 575 | self.sys_params = SystemParameters(self.valobj) |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 576 | self.unsigned_value = self.valobj.GetValueAsUnsigned() |
| 577 | self.isa_value = None |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 578 | |
| 579 | def adjust_for_architecture(self): |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 580 | pass |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 581 | |
| 582 | # an ObjC pointer can either be tagged or must be aligned |
| 583 | def is_tagged(self): |
| 584 | if self.valobj is None: |
| 585 | return False |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 586 | return (Utilities.is_valid_pointer(self.unsigned_value,self.sys_params.pointer_size, allow_tagged=True) and \ |
| 587 | not(Utilities.is_valid_pointer(self.unsigned_value,self.sys_params.pointer_size, allow_tagged=False))) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 588 | |
| 589 | def is_valid(self): |
| 590 | if self.valobj is None: |
| 591 | return False |
| 592 | if self.valobj.IsInScope() == False: |
| 593 | return False |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 594 | return Utilities.is_valid_pointer(self.unsigned_value,self.sys_params.pointer_size, allow_tagged=True) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 595 | |
| 596 | def read_isa(self): |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 597 | if self.isa_value != None: |
| 598 | return self.isa_value |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 599 | isa_pointer = self.valobj.CreateChildAtOffset("cfisa", |
| 600 | 0, |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 601 | self.sys_params.types_cache.addr_ptr_type) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 602 | if isa_pointer == None or isa_pointer.IsValid() == False: |
| 603 | return None; |
| 604 | if isa_pointer.GetValueAsUnsigned(1) == 1: |
| 605 | return None; |
Enrico Granata | 7bc0ec3 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 606 | self.isa_value = isa_pointer |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 607 | return isa_pointer |
| 608 | |
| 609 | def read_class_data(self): |
| 610 | global isa_cache |
| 611 | if self.is_tagged(): |
| 612 | # tagged pointers only exist in ObjC v2 |
| 613 | if self.sys_params.runtime_version == 2: |
| 614 | # not every odd-valued pointer is actually tagged. most are just plain wrong |
| 615 | # we could try and predetect this before even creating a TaggedClass_Data object |
| 616 | # 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] | 617 | tentative_tagged = TaggedClass_Data(self.unsigned_value,self.sys_params) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 618 | return tentative_tagged if tentative_tagged.is_valid() else InvalidClass_Data() |
| 619 | else: |
| 620 | return InvalidClass_Data() |
| 621 | if self.is_valid() == False: |
| 622 | return InvalidClass_Data() |
| 623 | isa = self.read_isa() |
| 624 | if isa == None: |
| 625 | return InvalidClass_Data() |
| 626 | isa_value = isa.GetValueAsUnsigned(1) |
| 627 | if isa_value == 1: |
| 628 | return InvalidClass_Data() |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 629 | data = self.sys_params.isa_cache.get_value(isa_value,default=None) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 630 | if data != None: |
| 631 | return data |
| 632 | if self.sys_params.runtime_version == 2: |
| 633 | data = Class_Data_V2(isa,self.sys_params) |
| 634 | else: |
| 635 | data = Class_Data_V1(isa,self.sys_params) |
| 636 | if data == None: |
| 637 | return InvalidClass_Data() |
| 638 | if data.is_valid(): |
Enrico Granata | cfdafa3 | 2012-03-05 19:56:33 +0000 | [diff] [blame] | 639 | self.sys_params.isa_cache.add_item(isa_value,data,ok_to_replace=True) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 640 | return data |
Enrico Granata | b8cbe9c | 2012-02-23 23:26:48 +0000 | [diff] [blame] | 641 | |