blob: beeec6dd115a16800e41cc9dde57d0c0df3a09d5 [file] [log] [blame]
Enrico Granatacfdafa32012-03-05 19:56:33 +00001"""
2Objective-C runtime wrapper for use by LLDB Python formatters
3
4part of The LLVM Compiler Infrastructure
5This file is distributed under the University of Illinois Open Source
6License. See LICENSE.TXT for details.
7"""
Enrico Granataeb4a4792012-02-23 23:10:27 +00008import lldb
9import cache
Enrico Granatacfdafa32012-03-05 19:56:33 +000010import attrib_fromdict
11import functools
Enrico Granataeb4a4792012-02-23 23:10:27 +000012
13class Utilities:
14 @staticmethod
Enrico Granata7bc0ec32012-02-29 03:28:49 +000015 def read_ascii(process, pointer,max_len=128):
Enrico Granataeb4a4792012-02-23 23:10:27 +000016 error = lldb.SBError()
Enrico Granata7bc0ec32012-02-29 03:28:49 +000017 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 Granataeb4a4792012-02-23 23:10:27 +000025
26 @staticmethod
Enrico Granata7bc0ec32012-02-29 03:28:49 +000027 def is_valid_pointer(pointer, pointer_size, allow_tagged=False, allow_NULL=False):
Enrico Granataeb4a4792012-02-23 23:10:27 +000028 if pointer == None:
29 return False
30 if pointer == 0:
Enrico Granata7bc0ec32012-02-29 03:28:49 +000031 return allow_NULL
32 if allow_tagged and (pointer % 2) == 1:
33 return True
Enrico Granataeb4a4792012-02-23 23:10:27 +000034 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 Granata7bc0ec32012-02-29 03:28:49 +000042 return ((pointer & 0xFFFF800000000000) == 0)
Enrico Granataeb4a4792012-02-23 23:10:27 +000043
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 Granata7bc0ec32012-02-29 03:28:49 +000057 # 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 Granataeb4a4792012-02-23 23:10:27 +000076
Enrico Granata3f1052b2012-03-13 21:52:00 +000077 # a utility method that factors out code common to almost all the formatters
78 # takes in an SBValue and a metrics object
79 # returns a class_data and a wrapper (or None, if the runtime alone can't decide on a wrapper)
80 @staticmethod
81 def prepare_class_detection(valobj,statistics):
82 class_data = ObjCRuntime(valobj)
83 if class_data.is_valid() == False:
84 statistics.metric_hit('invalid_pointer',valobj)
85 wrapper = InvalidPointer_Description(valobj.GetValueAsUnsigned(0) == 0)
86 return class_data,wrapper
87 class_data = class_data.read_class_data()
88 if class_data.is_valid() == False:
89 statistics.metric_hit('invalid_isa',valobj)
90 wrapper = InvalidISA_Description()
91 return class_data,wrapper
92 if class_data.is_kvo():
93 class_data = class_data.get_superclass()
94 if class_data.is_valid() == False:
95 statistics.metric_hit('invalid_isa',valobj)
96 wrapper = InvalidISA_Description()
97 return class_data,wrapper
98 return class_data,None
99
100
Enrico Granataeb4a4792012-02-23 23:10:27 +0000101class RoT_Data:
102 def __init__(self,rot_pointer,params):
103 if (Utilities.is_valid_pointer(rot_pointer.GetValueAsUnsigned(),params.pointer_size, allow_tagged=False)):
104 self.sys_params = params
105 self.valobj = rot_pointer
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000106 #self.flags = Utilities.read_child_of(self.valobj,0,self.sys_params.uint32_t)
Enrico Granatacfdafa32012-03-05 19:56:33 +0000107 #self.instanceStart = Utilities.read_child_of(self.valobj,4,self.sys_params.uint32_t)
108 self.instanceSize = None # lazy fetching
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000109 offset = 24 if self.sys_params.is_64_bit else 16
110 #self.ivarLayoutPtr = Utilities.read_child_of(self.valobj,offset,self.sys_params.addr_ptr_type)
Enrico Granatacfdafa32012-03-05 19:56:33 +0000111 self.namePointer = Utilities.read_child_of(self.valobj,offset,self.sys_params.types_cache.addr_ptr_type)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000112 self.check_valid()
113 else:
114 self.valid = False
115 if self.valid:
116 self.name = Utilities.read_ascii(self.valobj.GetTarget().GetProcess(),self.namePointer)
117 if not(Utilities.is_valid_identifier(self.name)):
118 self.valid = False
119
Enrico Granatacfdafa32012-03-05 19:56:33 +0000120 # perform sanity checks on the contents of this class_ro_t
Enrico Granataeb4a4792012-02-23 23:10:27 +0000121 def check_valid(self):
122 self.valid = True
123 # misaligned pointers seem to be possible for this field
124 #if not(Utilities.is_valid_pointer(self.namePointer,self.sys_params.pointer_size,allow_tagged=False)):
125 # self.valid = False
126 # pass
127
128 def __str__(self):
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000129 return \
Enrico Granatacfdafa32012-03-05 19:56:33 +0000130 "instanceSize = " + hex(self.instance_size()) + "\n" + \
Enrico Granataeb4a4792012-02-23 23:10:27 +0000131 "namePointer = " + hex(self.namePointer) + " --> " + self.name
132
133 def is_valid(self):
134 return self.valid
135
Enrico Granatacfdafa32012-03-05 19:56:33 +0000136 def instance_size(self,align=False):
137 if self.is_valid() == False:
138 return None
139 if self.instanceSize == None:
140 self.instanceSize = Utilities.read_child_of(self.valobj,8,self.sys_params.types_cache.uint32_t)
141 if align:
142 unalign = self.instance_size(False)
143 if self.sys_params.is_64_bit:
144 return ((unalign + 7) & ~7) % 0x100000000
145 else:
146 return ((unalign + 3) & ~3) % 0x100000000
147 else:
148 return self.instanceSize
Enrico Granataeb4a4792012-02-23 23:10:27 +0000149
150class RwT_Data:
151 def __init__(self,rwt_pointer,params):
152 if (Utilities.is_valid_pointer(rwt_pointer.GetValueAsUnsigned(),params.pointer_size, allow_tagged=False)):
153 self.sys_params = params
154 self.valobj = rwt_pointer
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000155 #self.flags = Utilities.read_child_of(self.valobj,0,self.sys_params.uint32_t)
156 #self.version = Utilities.read_child_of(self.valobj,4,self.sys_params.uint32_t)
Enrico Granatacfdafa32012-03-05 19:56:33 +0000157 self.roPointer = Utilities.read_child_of(self.valobj,8,self.sys_params.types_cache.addr_ptr_type)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000158 self.check_valid()
159 else:
160 self.valid = False
161 if self.valid:
Enrico Granatacfdafa32012-03-05 19:56:33 +0000162 self.rot = self.valobj.CreateValueFromAddress("rot",self.roPointer,self.sys_params.types_cache.addr_ptr_type).AddressOf()
Enrico Granataeb4a4792012-02-23 23:10:27 +0000163 self.data = RoT_Data(self.rot,self.sys_params)
164
165 # perform sanity checks on the contents of this class_rw_t
166 def check_valid(self):
167 self.valid = True
168 if not(Utilities.is_valid_pointer(self.roPointer,self.sys_params.pointer_size,allow_tagged=False)):
169 self.valid = False
170
171 def __str__(self):
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000172 return \
Enrico Granataeb4a4792012-02-23 23:10:27 +0000173 "roPointer = " + hex(self.roPointer)
174
175 def is_valid(self):
176 if self.valid:
177 return self.data.is_valid()
178 return False
179
180class Class_Data_V2:
181 def __init__(self,isa_pointer,params):
182 if (isa_pointer != None) and (Utilities.is_valid_pointer(isa_pointer.GetValueAsUnsigned(),params.pointer_size, allow_tagged=False)):
183 self.sys_params = params
184 self.valobj = isa_pointer
Enrico Granataeb4a4792012-02-23 23:10:27 +0000185 self.check_valid()
186 else:
187 self.valid = False
188 if self.valid:
Enrico Granatacfdafa32012-03-05 19:56:33 +0000189 self.rwt = self.valobj.CreateValueFromAddress("rwt",self.dataPointer,self.sys_params.types_cache.addr_ptr_type).AddressOf()
Enrico Granataeb4a4792012-02-23 23:10:27 +0000190 self.data = RwT_Data(self.rwt,self.sys_params)
191
192 # perform sanity checks on the contents of this class_t
Enrico Granatacfdafa32012-03-05 19:56:33 +0000193 # this call tries to minimize the amount of data fetched- as soon as we have "proven"
194 # that we have an invalid object, we stop reading
Enrico Granataeb4a4792012-02-23 23:10:27 +0000195 def check_valid(self):
196 self.valid = True
Enrico Granatacfdafa32012-03-05 19:56:33 +0000197
198 self.isaPointer = Utilities.read_child_of(self.valobj,0,self.sys_params.types_cache.addr_ptr_type)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000199 if not(Utilities.is_valid_pointer(self.isaPointer,self.sys_params.pointer_size,allow_tagged=False)):
200 self.valid = False
201 return
Enrico Granataeb4a4792012-02-23 23:10:27 +0000202 if not(Utilities.is_allowed_pointer(self.isaPointer)):
203 self.valid = False
204 return
Enrico Granatacfdafa32012-03-05 19:56:33 +0000205
206 self.cachePointer = Utilities.read_child_of(self.valobj,2*self.sys_params.pointer_size,self.sys_params.types_cache.addr_ptr_type)
207 if not(Utilities.is_valid_pointer(self.cachePointer,self.sys_params.pointer_size,allow_tagged=False)):
208 self.valid = False
209 return
Enrico Granataeb4a4792012-02-23 23:10:27 +0000210 if not(Utilities.is_allowed_pointer(self.cachePointer)):
211 self.valid = False
212 return
Enrico Granatacfdafa32012-03-05 19:56:33 +0000213
214 self.vtablePointer = Utilities.read_child_of(self.valobj,3*self.sys_params.pointer_size,self.sys_params.types_cache.addr_ptr_type)
215 if not(Utilities.is_valid_pointer(self.vtablePointer,self.sys_params.pointer_size,allow_tagged=False)):
216 self.valid = False
217 return
Enrico Granataeb4a4792012-02-23 23:10:27 +0000218 if not(Utilities.is_allowed_pointer(self.vtablePointer)):
219 self.valid = False
220 return
Enrico Granatacfdafa32012-03-05 19:56:33 +0000221
222 self.dataPointer = Utilities.read_child_of(self.valobj,4*self.sys_params.pointer_size,self.sys_params.types_cache.addr_ptr_type)
223 if not(Utilities.is_valid_pointer(self.dataPointer,self.sys_params.pointer_size,allow_tagged=False)):
224 self.valid = False
225 return
Enrico Granataeb4a4792012-02-23 23:10:27 +0000226 if not(Utilities.is_allowed_pointer(self.dataPointer)):
227 self.valid = False
228 return
229
Enrico Granatacfdafa32012-03-05 19:56:33 +0000230 self.superclassIsaPointer = Utilities.read_child_of(self.valobj,1*self.sys_params.pointer_size,self.sys_params.types_cache.addr_ptr_type)
231 if not(Utilities.is_valid_pointer(self.superclassIsaPointer,self.sys_params.pointer_size,allow_tagged=False, allow_NULL=True)):
232 self.valid = False
233 return
234 if not(Utilities.is_allowed_pointer(self.superclassIsaPointer)):
235 self.valid = False
236 return
237
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000238 # in general, KVO is implemented by transparently subclassing
239 # however, there could be exceptions where a class does something else
240 # internally to implement the feature - this method will have no clue that a class
241 # has been KVO'ed unless the standard implementation technique is used
Enrico Granataeb4a4792012-02-23 23:10:27 +0000242 def is_kvo(self):
243 if self.is_valid():
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000244 if self.class_name().startswith("NSKVONotifying_"):
Enrico Granataeb4a4792012-02-23 23:10:27 +0000245 return True
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000246 return False
Enrico Granataeb4a4792012-02-23 23:10:27 +0000247
Enrico Granatacfdafa32012-03-05 19:56:33 +0000248 # some CF classes have a valid ObjC isa in their CFRuntimeBase
249 # but instead of being class-specific this isa points to a match-'em-all class
250 # which is __NSCFType (the versions without __ also exists and we are matching to it
251 # just to be on the safe side)
252 def is_cftype(self):
253 if self.is_valid():
Enrico Granata3f1052b2012-03-13 21:52:00 +0000254 return self.class_name() == '__NSCFType' or self.class_name() == 'NSCFType'
Enrico Granatacfdafa32012-03-05 19:56:33 +0000255
Enrico Granataeb4a4792012-02-23 23:10:27 +0000256 def get_superclass(self):
257 if self.is_valid():
258 parent_isa_pointer = self.valobj.CreateChildAtOffset("parent_isa",
259 self.sys_params.pointer_size,
260 self.sys_params.addr_ptr_type)
261 return Class_Data_V2(parent_isa_pointer,self.sys_params)
262 else:
263 return None
264
265 def class_name(self):
266 if self.is_valid():
267 return self.data.data.name
268 else:
269 return None
270
271 def is_valid(self):
272 if self.valid:
273 return self.data.is_valid()
274 return False
275
276 def __str__(self):
277 return 'isaPointer = ' + hex(self.isaPointer) + "\n" + \
278 "superclassIsaPointer = " + hex(self.superclassIsaPointer) + "\n" + \
279 "cachePointer = " + hex(self.cachePointer) + "\n" + \
280 "vtablePointer = " + hex(self.vtablePointer) + "\n" + \
281 "data = " + hex(self.dataPointer)
282
283 def is_tagged(self):
284 return False
285
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000286 def instance_size(self,align=False):
287 if self.is_valid() == False:
288 return None
Enrico Granatacfdafa32012-03-05 19:56:33 +0000289 return self.rwt.rot.instance_size(align)
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000290
Enrico Granataeb4a4792012-02-23 23:10:27 +0000291# runtime v1 is much less intricate than v2 and stores relevant information directly in the class_t object
Enrico Granataeb4a4792012-02-23 23:10:27 +0000292class Class_Data_V1:
293 def __init__(self,isa_pointer,params):
294 if (isa_pointer != None) and (Utilities.is_valid_pointer(isa_pointer.GetValueAsUnsigned(),params.pointer_size, allow_tagged=False)):
Enrico Granata0448d9f2012-02-29 20:46:00 +0000295 self.valid = True
Enrico Granataeb4a4792012-02-23 23:10:27 +0000296 self.sys_params = params
297 self.valobj = isa_pointer
Enrico Granatacfdafa32012-03-05 19:56:33 +0000298 self.check_valid()
Enrico Granataeb4a4792012-02-23 23:10:27 +0000299 else:
300 self.valid = False
301 if self.valid:
302 self.name = Utilities.read_ascii(self.valobj.GetTarget().GetProcess(),self.namePointer)
303 if not(Utilities.is_valid_identifier(self.name)):
304 self.valid = False
305
306 # perform sanity checks on the contents of this class_t
307 def check_valid(self):
308 self.valid = True
Enrico Granatacfdafa32012-03-05 19:56:33 +0000309
310 self.isaPointer = Utilities.read_child_of(self.valobj,0,self.sys_params.types_cache.addr_ptr_type)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000311 if not(Utilities.is_valid_pointer(self.isaPointer,self.sys_params.pointer_size,allow_tagged=False)):
312 self.valid = False
313 return
Enrico Granatacfdafa32012-03-05 19:56:33 +0000314
315 self.superclassIsaPointer = Utilities.read_child_of(self.valobj,1*self.sys_params.pointer_size,self.sys_params.types_cache.addr_ptr_type)
316 if not(Utilities.is_valid_pointer(self.superclassIsaPointer,self.sys_params.pointer_size,allow_tagged=False,allow_NULL=True)):
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000317 self.valid = False
318 return
Enrico Granataeb4a4792012-02-23 23:10:27 +0000319
Enrico Granatacfdafa32012-03-05 19:56:33 +0000320 self.namePointer = Utilities.read_child_of(self.valobj,2*self.sys_params.pointer_size,self.sys_params.types_cache.addr_ptr_type)
321 #if not(Utilities.is_valid_pointer(self.namePointer,self.sys_params.pointer_size,allow_tagged=False,allow_NULL=False)):
322 # self.valid = False
323 # return
324
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000325 # in general, KVO is implemented by transparently subclassing
326 # however, there could be exceptions where a class does something else
327 # internally to implement the feature - this method will have no clue that a class
328 # has been KVO'ed unless the standard implementation technique is used
Enrico Granataeb4a4792012-02-23 23:10:27 +0000329 def is_kvo(self):
330 if self.is_valid():
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000331 if self.class_name().startswith("NSKVONotifying_"):
Enrico Granataeb4a4792012-02-23 23:10:27 +0000332 return True
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000333 return False
Enrico Granataeb4a4792012-02-23 23:10:27 +0000334
Enrico Granatacfdafa32012-03-05 19:56:33 +0000335 # some CF classes have a valid ObjC isa in their CFRuntimeBase
336 # but instead of being class-specific this isa points to a match-'em-all class
337 # which is __NSCFType (the versions without __ also exists and we are matching to it
338 # just to be on the safe side)
339 def is_cftype(self):
340 if self.is_valid():
Enrico Granata3f1052b2012-03-13 21:52:00 +0000341 return self.class_name() == '__NSCFType' or self.class_name() == 'NSCFType'
Enrico Granatacfdafa32012-03-05 19:56:33 +0000342
Enrico Granataeb4a4792012-02-23 23:10:27 +0000343 def get_superclass(self):
344 if self.is_valid():
345 parent_isa_pointer = self.valobj.CreateChildAtOffset("parent_isa",
346 self.sys_params.pointer_size,
347 self.sys_params.addr_ptr_type)
348 return Class_Data_V1(parent_isa_pointer,self.sys_params)
349 else:
350 return None
351
352 def class_name(self):
353 if self.is_valid():
354 return self.name
355 else:
356 return None
357
358 def is_valid(self):
359 return self.valid
360
361 def __str__(self):
362 return 'isaPointer = ' + hex(self.isaPointer) + "\n" + \
363 "superclassIsaPointer = " + hex(self.superclassIsaPointer) + "\n" + \
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000364 "namePointer = " + hex(self.namePointer) + " --> " + self.name + \
Enrico Granatacfdafa32012-03-05 19:56:33 +0000365 "instanceSize = " + hex(self.instanceSize()) + "\n"
Enrico Granataeb4a4792012-02-23 23:10:27 +0000366
367 def is_tagged(self):
368 return False
369
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000370 def instance_size(self,align=False):
371 if self.is_valid() == False:
372 return None
Enrico Granatacfdafa32012-03-05 19:56:33 +0000373 if self.instanceSize == None:
374 self.instanceSize = Utilities.read_child_of(self.valobj,5*self.sys_params.pointer_size,self.sys_params.types_cache.addr_ptr_type)
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000375 if align:
376 unalign = self.instance_size(False)
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000377 if self.sys_params.is_64_bit:
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000378 return ((unalign + 7) & ~7) % 0x100000000
379 else:
380 return ((unalign + 3) & ~3) % 0x100000000
381 else:
382 return self.instanceSize
383
Enrico Granata0448d9f2012-02-29 20:46:00 +0000384# these are the only tagged pointers values for current versions
Enrico Granata0c489f52012-03-01 04:24:26 +0000385# of OSX - they might change in future OS releases, and no-one is
Enrico Granata0448d9f2012-02-29 20:46:00 +0000386# advised to rely on these values, or any of the bitmasking formulas
387# in TaggedClass_Data. doing otherwise is at your own risk
Enrico Granata0c489f52012-03-01 04:24:26 +0000388TaggedClass_Values_Lion = {1 : 'NSNumber', \
389 5: 'NSManagedObject', \
390 6: 'NSDate', \
391 7: 'NSDateTS' };
392TaggedClass_Values_NMOS = {0: 'NSAtom', \
393 3 : 'NSNumber', \
394 4: 'NSDateTS', \
395 5: 'NSManagedObject', \
396 6: 'NSDate' };
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000397
Enrico Granataeb4a4792012-02-23 23:10:27 +0000398class TaggedClass_Data:
399 def __init__(self,pointer,params):
Enrico Granata0c489f52012-03-01 04:24:26 +0000400 global TaggedClass_Values_Lion,TaggedClass_Values_NMOS
Enrico Granataeb4a4792012-02-23 23:10:27 +0000401 self.valid = True
402 self.name = None
403 self.sys_params = params
404 self.valobj = pointer
405 self.val = (pointer & ~0x0000000000000000FF) >> 8
406 self.class_bits = (pointer & 0xE) >> 1
407 self.i_bits = (pointer & 0xF0) >> 4
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000408
Enrico Granata0c489f52012-03-01 04:24:26 +0000409 if self.sys_params.is_lion:
410 if self.class_bits in TaggedClass_Values_Lion:
411 self.name = TaggedClass_Values_Lion[self.class_bits]
412 else:
413 self.valid = False
Enrico Granataeb4a4792012-02-23 23:10:27 +0000414 else:
Enrico Granata0c489f52012-03-01 04:24:26 +0000415 if self.class_bits in TaggedClass_Values_NMOS:
416 self.name = TaggedClass_Values_NMOS[self.class_bits]
417 else:
418 self.valid = False
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000419
Enrico Granataeb4a4792012-02-23 23:10:27 +0000420
421 def is_valid(self):
422 return self.valid
423
424 def class_name(self):
425 if self.is_valid():
426 return self.name
427 else:
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000428 return False
Enrico Granataeb4a4792012-02-23 23:10:27 +0000429
430 def value(self):
431 return self.val if self.is_valid() else None
432
433 def info_bits(self):
434 return self.i_bits if self.is_valid() else None
435
436 def is_kvo(self):
437 return False
438
Enrico Granatacfdafa32012-03-05 19:56:33 +0000439 def is_cftype(self):
440 return False
441
Enrico Granataeb4a4792012-02-23 23:10:27 +0000442 # we would need to go around looking for the superclass or ask the runtime
443 # for now, we seem not to require support for this operation so we will merrily
444 # pretend to be at a root point in the hierarchy
445 def get_superclass(self):
446 return None
447
448 # anything that is handled here is tagged
449 def is_tagged(self):
450 return True
451
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000452 # it seems reasonable to say that a tagged pointer is the size of a pointer
453 def instance_size(self,align=False):
454 if self.is_valid() == False:
455 return None
Enrico Granatacfdafa32012-03-05 19:56:33 +0000456 return self.sys_params.pointer_size
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000457
458
Enrico Granataeb4a4792012-02-23 23:10:27 +0000459class InvalidClass_Data:
460 def __init__(self):
461 pass
462 def is_valid(self):
463 return False
464
Enrico Granatacfdafa32012-03-05 19:56:33 +0000465@functools.total_ordering
466class Version:
467 def __init__(self, major, minor, release, build_string):
468 self._major = major
469 self._minor = minor
470 self._release = release
471 self._build_string = build_string
472
473 def get_major(self):
474 return self._major
475 def get_minor(self):
476 return self._minor
477 def get_release(self):
478 return self._release
479 def get_build_string(self):
480 return self._build_string
481
482 major = property(get_major,None)
483 minor = property(get_minor,None)
484 release = property(get_release,None)
485 build_string = property(get_build_string,None)
486
487 def __lt__(self,other):
488 if (self.major < other.major):
489 return True
490 if (self.minor < other.minor):
491 return True
492 if (self.release < other.release):
493 return True
494 # build strings are not compared since they are heavily platform-dependent and might not always
495 # be available
496 return False
497
498 def __eq__(self,other):
499 return (self.major == other.major) and \
500 (self.minor == other.minor) and \
501 (self.release == other.release) and \
502 (self.build_string == other.build_string)
503
Enrico Granataeb4a4792012-02-23 23:10:27 +0000504runtime_version = cache.Cache()
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000505os_version = cache.Cache()
Enrico Granatacfdafa32012-03-05 19:56:33 +0000506types_caches = cache.Cache()
507isa_caches = cache.Cache()
Enrico Granataeb4a4792012-02-23 23:10:27 +0000508
509class SystemParameters:
510 def __init__(self,valobj):
511 self.adjust_for_architecture(valobj)
Enrico Granatacfdafa32012-03-05 19:56:33 +0000512 self.adjust_for_process(valobj)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000513
Enrico Granatacfdafa32012-03-05 19:56:33 +0000514 def adjust_for_process(self, valobj):
Enrico Granataeb4a4792012-02-23 23:10:27 +0000515 global runtime_version
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000516 global os_version
Enrico Granatacfdafa32012-03-05 19:56:33 +0000517 global types_caches
518 global isa_caches
519
520 process = valobj.GetTarget().GetProcess()
521 self.pid = process.GetProcessID()
522
523 if runtime_version.look_for_key(self.pid):
524 self.runtime_version = runtime_version.get_value(self.pid)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000525 else:
Enrico Granatacfdafa32012-03-05 19:56:33 +0000526 self.runtime_version = ObjCRuntime.runtime_version(process)
527 runtime_version.add_item(self.pid,self.runtime_version)
528
529 if os_version.look_for_key(self.pid):
530 self.is_lion = os_version.get_value(self.pid)
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000531 else:
532 self.is_lion = Utilities.check_is_osx_lion(valobj.GetTarget())
Enrico Granatacfdafa32012-03-05 19:56:33 +0000533 os_version.add_item(self.pid,self.is_lion)
534
535 if types_caches.look_for_key(self.pid):
536 self.types_cache = types_caches.get_value(self.pid)
537 else:
538 self.types_cache = attrib_fromdict.AttributesDictionary(allow_reset=False)
539 self.types_cache.addr_type = valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong)
540 self.types_cache.addr_ptr_type = self.types_cache.addr_type.GetPointerType()
541 self.types_cache.uint32_t = valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt)
542 types_caches.add_item(self.pid,self.types_cache)
543
544 if isa_caches.look_for_key(self.pid):
545 self.isa_cache = isa_caches.get_value(self.pid)
546 else:
547 self.isa_cache = cache.Cache()
548 isa_caches.add_item(self.pid,self.isa_cache)
549
550 def adjust_for_architecture(self,valobj):
551 process = valobj.GetTarget().GetProcess()
552 self.pointer_size = process.GetAddressByteSize()
553 self.is_64_bit = (self.pointer_size == 8)
554 self.is_little = (process.GetByteOrder() == lldb.eByteOrderLittle)
Enrico Granata385ad4e2012-03-03 00:45:57 +0000555 self.cfruntime_size = 16 if self.is_64_bit else 8
Enrico Granataeb4a4792012-02-23 23:10:27 +0000556
Enrico Granatacfdafa32012-03-05 19:56:33 +0000557 # a simple helper function that makes it more explicit that one is calculating
558 # an offset that is made up of X pointers and Y bytes of additional data
559 # taking into account pointer size - if you know there is going to be some padding
560 # you can pass that in and it will be taken into account (since padding may be different between
561 # 32 and 64 bit versions, you can pass padding value for both, the right one will be used)
562 def calculate_offset(self, num_pointers = 0, bytes_count = 0, padding32 = 0, padding64 = 0):
563 value = bytes_count + num_pointers*self.pointer_size
564 return value + padding64 if self.is_64_bit else value + padding32
Enrico Granataeb4a4792012-02-23 23:10:27 +0000565
566class ObjCRuntime:
567
568 # the ObjC runtime has no explicit "version" field that we can use
569 # instead, we discriminate v1 from v2 by looking for the presence
570 # of a well-known section only present in v1
571 @staticmethod
572 def runtime_version(process):
573 if process.IsValid() == False:
574 return None
575 target = process.GetTarget()
576 num_modules = target.GetNumModules()
577 module_objc = None
578 for idx in range(num_modules):
579 module = target.GetModuleAtIndex(idx)
580 if module.GetFileSpec().GetFilename() == 'libobjc.A.dylib':
581 module_objc = module
582 break
583 if module_objc == None or module_objc.IsValid() == False:
584 return None
585 num_sections = module.GetNumSections()
586 section_objc = None
587 for idx in range(num_sections):
588 section = module.GetSectionAtIndex(idx)
589 if section.GetName() == '__OBJC':
590 section_objc = section
591 break
592 if section_objc != None and section_objc.IsValid():
593 return 1
594 return 2
595
596 def __init__(self,valobj):
597 self.valobj = valobj
598 self.adjust_for_architecture()
599 self.sys_params = SystemParameters(self.valobj)
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000600 self.unsigned_value = self.valobj.GetValueAsUnsigned()
601 self.isa_value = None
Enrico Granataeb4a4792012-02-23 23:10:27 +0000602
603 def adjust_for_architecture(self):
Enrico Granatacfdafa32012-03-05 19:56:33 +0000604 pass
Enrico Granataeb4a4792012-02-23 23:10:27 +0000605
606# an ObjC pointer can either be tagged or must be aligned
607 def is_tagged(self):
608 if self.valobj is None:
609 return False
Enrico Granatacfdafa32012-03-05 19:56:33 +0000610 return (Utilities.is_valid_pointer(self.unsigned_value,self.sys_params.pointer_size, allow_tagged=True) and \
611 not(Utilities.is_valid_pointer(self.unsigned_value,self.sys_params.pointer_size, allow_tagged=False)))
Enrico Granataeb4a4792012-02-23 23:10:27 +0000612
613 def is_valid(self):
614 if self.valobj is None:
615 return False
616 if self.valobj.IsInScope() == False:
617 return False
Enrico Granatacfdafa32012-03-05 19:56:33 +0000618 return Utilities.is_valid_pointer(self.unsigned_value,self.sys_params.pointer_size, allow_tagged=True)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000619
Enrico Granata3f1052b2012-03-13 21:52:00 +0000620 def is_nil(self):
621 return self.unsigned_value == 0
622
Enrico Granataeb4a4792012-02-23 23:10:27 +0000623 def read_isa(self):
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000624 if self.isa_value != None:
625 return self.isa_value
Enrico Granataeb4a4792012-02-23 23:10:27 +0000626 isa_pointer = self.valobj.CreateChildAtOffset("cfisa",
627 0,
Enrico Granatacfdafa32012-03-05 19:56:33 +0000628 self.sys_params.types_cache.addr_ptr_type)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000629 if isa_pointer == None or isa_pointer.IsValid() == False:
630 return None;
631 if isa_pointer.GetValueAsUnsigned(1) == 1:
632 return None;
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000633 self.isa_value = isa_pointer
Enrico Granataeb4a4792012-02-23 23:10:27 +0000634 return isa_pointer
635
636 def read_class_data(self):
637 global isa_cache
638 if self.is_tagged():
639 # tagged pointers only exist in ObjC v2
640 if self.sys_params.runtime_version == 2:
641 # not every odd-valued pointer is actually tagged. most are just plain wrong
642 # we could try and predetect this before even creating a TaggedClass_Data object
643 # but unless performance requires it, this seems a cleaner way to tackle the task
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000644 tentative_tagged = TaggedClass_Data(self.unsigned_value,self.sys_params)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000645 return tentative_tagged if tentative_tagged.is_valid() else InvalidClass_Data()
646 else:
647 return InvalidClass_Data()
648 if self.is_valid() == False:
649 return InvalidClass_Data()
650 isa = self.read_isa()
651 if isa == None:
652 return InvalidClass_Data()
653 isa_value = isa.GetValueAsUnsigned(1)
654 if isa_value == 1:
655 return InvalidClass_Data()
Enrico Granatacfdafa32012-03-05 19:56:33 +0000656 data = self.sys_params.isa_cache.get_value(isa_value,default=None)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000657 if data != None:
658 return data
659 if self.sys_params.runtime_version == 2:
660 data = Class_Data_V2(isa,self.sys_params)
661 else:
662 data = Class_Data_V1(isa,self.sys_params)
663 if data == None:
664 return InvalidClass_Data()
665 if data.is_valid():
Enrico Granatacfdafa32012-03-05 19:56:33 +0000666 self.sys_params.isa_cache.add_item(isa_value,data,ok_to_replace=True)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000667 return data
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000668
Enrico Granata3f1052b2012-03-13 21:52:00 +0000669# these classes below can be used by the data formatters to provide a consistent message that describes a given runtime-generated situation
670class SpecialSituation_Description:
671 def message(self):
672 return ''
673
674class InvalidPointer_Description(SpecialSituation_Description):
675
676 def __init__(self,nil):
677 self.is_nil = nil
678
679 def message(self):
680 if self.is_nil:
681 return '@"<nil>"'
682 else:
683 return '<invalid pointer>'
684
685class InvalidISA_Description(SpecialSituation_Description):
686
687 def __init__(self):
688 pass
689
690 def message(self):
691 return '<not an Objective-C object>'
692