blob: ddad00a2230f5131d9ee7769c2cb66ba7caa9d37 [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
77class 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 Granata7bc0ec32012-02-29 03:28:49 +000082 #self.flags = Utilities.read_child_of(self.valobj,0,self.sys_params.uint32_t)
Enrico Granatacfdafa32012-03-05 19:56:33 +000083 #self.instanceStart = Utilities.read_child_of(self.valobj,4,self.sys_params.uint32_t)
84 self.instanceSize = None # lazy fetching
Enrico Granata7bc0ec32012-02-29 03:28:49 +000085 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 Granatacfdafa32012-03-05 19:56:33 +000087 self.namePointer = Utilities.read_child_of(self.valobj,offset,self.sys_params.types_cache.addr_ptr_type)
Enrico Granataeb4a4792012-02-23 23:10:27 +000088 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 Granatacfdafa32012-03-05 19:56:33 +000096 # perform sanity checks on the contents of this class_ro_t
Enrico Granataeb4a4792012-02-23 23:10:27 +000097 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 Granata7bc0ec32012-02-29 03:28:49 +0000105 return \
Enrico Granatacfdafa32012-03-05 19:56:33 +0000106 "instanceSize = " + hex(self.instance_size()) + "\n" + \
Enrico Granataeb4a4792012-02-23 23:10:27 +0000107 "namePointer = " + hex(self.namePointer) + " --> " + self.name
108
109 def is_valid(self):
110 return self.valid
111
Enrico Granatacfdafa32012-03-05 19:56:33 +0000112 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 Granataeb4a4792012-02-23 23:10:27 +0000125
126class 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 Granata7bc0ec32012-02-29 03:28:49 +0000131 #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 Granatacfdafa32012-03-05 19:56:33 +0000133 self.roPointer = Utilities.read_child_of(self.valobj,8,self.sys_params.types_cache.addr_ptr_type)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000134 self.check_valid()
135 else:
136 self.valid = False
137 if self.valid:
Enrico Granatacfdafa32012-03-05 19:56:33 +0000138 self.rot = self.valobj.CreateValueFromAddress("rot",self.roPointer,self.sys_params.types_cache.addr_ptr_type).AddressOf()
Enrico Granataeb4a4792012-02-23 23:10:27 +0000139 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 Granata7bc0ec32012-02-29 03:28:49 +0000148 return \
Enrico Granataeb4a4792012-02-23 23:10:27 +0000149 "roPointer = " + hex(self.roPointer)
150
151 def is_valid(self):
152 if self.valid:
153 return self.data.is_valid()
154 return False
155
156class 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 Granataeb4a4792012-02-23 23:10:27 +0000161 self.check_valid()
162 else:
163 self.valid = False
164 if self.valid:
Enrico Granatacfdafa32012-03-05 19:56:33 +0000165 self.rwt = self.valobj.CreateValueFromAddress("rwt",self.dataPointer,self.sys_params.types_cache.addr_ptr_type).AddressOf()
Enrico Granataeb4a4792012-02-23 23:10:27 +0000166 self.data = RwT_Data(self.rwt,self.sys_params)
167
168 # perform sanity checks on the contents of this class_t
Enrico Granatacfdafa32012-03-05 19:56:33 +0000169 # 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 Granataeb4a4792012-02-23 23:10:27 +0000171 def check_valid(self):
172 self.valid = True
Enrico Granatacfdafa32012-03-05 19:56:33 +0000173
174 self.isaPointer = Utilities.read_child_of(self.valobj,0,self.sys_params.types_cache.addr_ptr_type)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000175 if not(Utilities.is_valid_pointer(self.isaPointer,self.sys_params.pointer_size,allow_tagged=False)):
176 self.valid = False
177 return
Enrico Granataeb4a4792012-02-23 23:10:27 +0000178 if not(Utilities.is_allowed_pointer(self.isaPointer)):
179 self.valid = False
180 return
Enrico Granatacfdafa32012-03-05 19:56:33 +0000181
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 Granataeb4a4792012-02-23 23:10:27 +0000186 if not(Utilities.is_allowed_pointer(self.cachePointer)):
187 self.valid = False
188 return
Enrico Granatacfdafa32012-03-05 19:56:33 +0000189
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 Granataeb4a4792012-02-23 23:10:27 +0000194 if not(Utilities.is_allowed_pointer(self.vtablePointer)):
195 self.valid = False
196 return
Enrico Granatacfdafa32012-03-05 19:56:33 +0000197
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 Granataeb4a4792012-02-23 23:10:27 +0000202 if not(Utilities.is_allowed_pointer(self.dataPointer)):
203 self.valid = False
204 return
205
Enrico Granatacfdafa32012-03-05 19:56:33 +0000206 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 Granata7bc0ec32012-02-29 03:28:49 +0000214 # 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 Granataeb4a4792012-02-23 23:10:27 +0000218 def is_kvo(self):
219 if self.is_valid():
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000220 if self.class_name().startswith("NSKVONotifying_"):
Enrico Granataeb4a4792012-02-23 23:10:27 +0000221 return True
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000222 return False
Enrico Granataeb4a4792012-02-23 23:10:27 +0000223
Enrico Granatacfdafa32012-03-05 19:56:33 +0000224 # 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 Granataeb4a4792012-02-23 23:10:27 +0000232 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 Granatab8cbe9c2012-02-23 23:26:48 +0000262 def instance_size(self,align=False):
263 if self.is_valid() == False:
264 return None
Enrico Granatacfdafa32012-03-05 19:56:33 +0000265 return self.rwt.rot.instance_size(align)
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000266
Enrico Granataeb4a4792012-02-23 23:10:27 +0000267# 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 +0000268class 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 Granata0448d9f2012-02-29 20:46:00 +0000271 self.valid = True
Enrico Granataeb4a4792012-02-23 23:10:27 +0000272 self.sys_params = params
273 self.valobj = isa_pointer
Enrico Granatacfdafa32012-03-05 19:56:33 +0000274 self.check_valid()
Enrico Granataeb4a4792012-02-23 23:10:27 +0000275 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 Granatacfdafa32012-03-05 19:56:33 +0000285
286 self.isaPointer = Utilities.read_child_of(self.valobj,0,self.sys_params.types_cache.addr_ptr_type)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000287 if not(Utilities.is_valid_pointer(self.isaPointer,self.sys_params.pointer_size,allow_tagged=False)):
288 self.valid = False
289 return
Enrico Granatacfdafa32012-03-05 19:56:33 +0000290
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 Granata7bc0ec32012-02-29 03:28:49 +0000293 self.valid = False
294 return
Enrico Granataeb4a4792012-02-23 23:10:27 +0000295
Enrico Granatacfdafa32012-03-05 19:56:33 +0000296 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 Granata7bc0ec32012-02-29 03:28:49 +0000301 # 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 Granataeb4a4792012-02-23 23:10:27 +0000305 def is_kvo(self):
306 if self.is_valid():
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000307 if self.class_name().startswith("NSKVONotifying_"):
Enrico Granataeb4a4792012-02-23 23:10:27 +0000308 return True
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000309 return False
Enrico Granataeb4a4792012-02-23 23:10:27 +0000310
Enrico Granatacfdafa32012-03-05 19:56:33 +0000311 # 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 Granataeb4a4792012-02-23 23:10:27 +0000319 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 Granatab8cbe9c2012-02-23 23:26:48 +0000340 "namePointer = " + hex(self.namePointer) + " --> " + self.name + \
Enrico Granatacfdafa32012-03-05 19:56:33 +0000341 "instanceSize = " + hex(self.instanceSize()) + "\n"
Enrico Granataeb4a4792012-02-23 23:10:27 +0000342
343 def is_tagged(self):
344 return False
345
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000346 def instance_size(self,align=False):
347 if self.is_valid() == False:
348 return None
Enrico Granatacfdafa32012-03-05 19:56:33 +0000349 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 Granatab8cbe9c2012-02-23 23:26:48 +0000351 if align:
352 unalign = self.instance_size(False)
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000353 if self.sys_params.is_64_bit:
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000354 return ((unalign + 7) & ~7) % 0x100000000
355 else:
356 return ((unalign + 3) & ~3) % 0x100000000
357 else:
358 return self.instanceSize
359
Enrico Granata0448d9f2012-02-29 20:46:00 +0000360# these are the only tagged pointers values for current versions
Enrico Granata0c489f52012-03-01 04:24:26 +0000361# of OSX - they might change in future OS releases, and no-one is
Enrico Granata0448d9f2012-02-29 20:46:00 +0000362# advised to rely on these values, or any of the bitmasking formulas
363# in TaggedClass_Data. doing otherwise is at your own risk
Enrico Granata0c489f52012-03-01 04:24:26 +0000364TaggedClass_Values_Lion = {1 : 'NSNumber', \
365 5: 'NSManagedObject', \
366 6: 'NSDate', \
367 7: 'NSDateTS' };
368TaggedClass_Values_NMOS = {0: 'NSAtom', \
369 3 : 'NSNumber', \
370 4: 'NSDateTS', \
371 5: 'NSManagedObject', \
372 6: 'NSDate' };
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000373
Enrico Granataeb4a4792012-02-23 23:10:27 +0000374class TaggedClass_Data:
375 def __init__(self,pointer,params):
Enrico Granata0c489f52012-03-01 04:24:26 +0000376 global TaggedClass_Values_Lion,TaggedClass_Values_NMOS
Enrico Granataeb4a4792012-02-23 23:10:27 +0000377 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 Granata7bc0ec32012-02-29 03:28:49 +0000384
Enrico Granata0c489f52012-03-01 04:24:26 +0000385 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 Granataeb4a4792012-02-23 23:10:27 +0000390 else:
Enrico Granata0c489f52012-03-01 04:24:26 +0000391 if self.class_bits in TaggedClass_Values_NMOS:
392 self.name = TaggedClass_Values_NMOS[self.class_bits]
393 else:
394 self.valid = False
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000395
Enrico Granataeb4a4792012-02-23 23:10:27 +0000396
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 Granata7bc0ec32012-02-29 03:28:49 +0000404 return False
Enrico Granataeb4a4792012-02-23 23:10:27 +0000405
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 Granatacfdafa32012-03-05 19:56:33 +0000415 def is_cftype(self):
416 return False
417
Enrico Granataeb4a4792012-02-23 23:10:27 +0000418 # 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 Granatab8cbe9c2012-02-23 23:26:48 +0000428 # 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 Granatacfdafa32012-03-05 19:56:33 +0000432 return self.sys_params.pointer_size
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000433
434
Enrico Granataeb4a4792012-02-23 23:10:27 +0000435class InvalidClass_Data:
436 def __init__(self):
437 pass
438 def is_valid(self):
439 return False
440
Enrico Granatacfdafa32012-03-05 19:56:33 +0000441@functools.total_ordering
442class 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 Granataeb4a4792012-02-23 23:10:27 +0000480runtime_version = cache.Cache()
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000481os_version = cache.Cache()
Enrico Granatacfdafa32012-03-05 19:56:33 +0000482types_caches = cache.Cache()
483isa_caches = cache.Cache()
Enrico Granataeb4a4792012-02-23 23:10:27 +0000484
485class SystemParameters:
486 def __init__(self,valobj):
487 self.adjust_for_architecture(valobj)
Enrico Granatacfdafa32012-03-05 19:56:33 +0000488 self.adjust_for_process(valobj)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000489
Enrico Granatacfdafa32012-03-05 19:56:33 +0000490 def adjust_for_process(self, valobj):
Enrico Granataeb4a4792012-02-23 23:10:27 +0000491 global runtime_version
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000492 global os_version
Enrico Granatacfdafa32012-03-05 19:56:33 +0000493 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 Granataeb4a4792012-02-23 23:10:27 +0000501 else:
Enrico Granatacfdafa32012-03-05 19:56:33 +0000502 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 Granata7bc0ec32012-02-29 03:28:49 +0000507 else:
508 self.is_lion = Utilities.check_is_osx_lion(valobj.GetTarget())
Enrico Granatacfdafa32012-03-05 19:56:33 +0000509 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 Granata385ad4e2012-03-03 00:45:57 +0000531 self.cfruntime_size = 16 if self.is_64_bit else 8
Enrico Granataeb4a4792012-02-23 23:10:27 +0000532
Enrico Granatacfdafa32012-03-05 19:56:33 +0000533 # 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 Granataeb4a4792012-02-23 23:10:27 +0000541
542class 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 Granata7bc0ec32012-02-29 03:28:49 +0000576 self.unsigned_value = self.valobj.GetValueAsUnsigned()
577 self.isa_value = None
Enrico Granataeb4a4792012-02-23 23:10:27 +0000578
579 def adjust_for_architecture(self):
Enrico Granatacfdafa32012-03-05 19:56:33 +0000580 pass
Enrico Granataeb4a4792012-02-23 23:10:27 +0000581
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 Granatacfdafa32012-03-05 19:56:33 +0000586 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 Granataeb4a4792012-02-23 23:10:27 +0000588
589 def is_valid(self):
590 if self.valobj is None:
591 return False
592 if self.valobj.IsInScope() == False:
593 return False
Enrico Granatacfdafa32012-03-05 19:56:33 +0000594 return Utilities.is_valid_pointer(self.unsigned_value,self.sys_params.pointer_size, allow_tagged=True)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000595
596 def read_isa(self):
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000597 if self.isa_value != None:
598 return self.isa_value
Enrico Granataeb4a4792012-02-23 23:10:27 +0000599 isa_pointer = self.valobj.CreateChildAtOffset("cfisa",
600 0,
Enrico Granatacfdafa32012-03-05 19:56:33 +0000601 self.sys_params.types_cache.addr_ptr_type)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000602 if isa_pointer == None or isa_pointer.IsValid() == False:
603 return None;
604 if isa_pointer.GetValueAsUnsigned(1) == 1:
605 return None;
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000606 self.isa_value = isa_pointer
Enrico Granataeb4a4792012-02-23 23:10:27 +0000607 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 Granata7bc0ec32012-02-29 03:28:49 +0000617 tentative_tagged = TaggedClass_Data(self.unsigned_value,self.sys_params)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000618 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 Granatacfdafa32012-03-05 19:56:33 +0000629 data = self.sys_params.isa_cache.get_value(isa_value,default=None)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000630 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 Granatacfdafa32012-03-05 19:56:33 +0000639 self.sys_params.isa_cache.add_item(isa_value,data,ok_to_replace=True)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000640 return data
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000641