blob: 4593306943b532761bbcf5881a67b3bfd6aedb6f [file] [log] [blame]
Enrico Granataeb4a4792012-02-23 23:10:27 +00001# a wrapper for the Objective-C runtime for use by LLDB
2import lldb
3import cache
4
5class Utilities:
6 @staticmethod
Enrico Granata7bc0ec32012-02-29 03:28:49 +00007 def read_ascii(process, pointer,max_len=128):
Enrico Granataeb4a4792012-02-23 23:10:27 +00008 error = lldb.SBError()
Enrico Granata7bc0ec32012-02-29 03:28:49 +00009 content = None
10 try:
11 content = process.ReadCStringFromMemory(pointer,max_len,error)
12 except:
13 pass
14 if content == None or len(content) == 0 or error.fail == True:
15 return None
16 return content
Enrico Granataeb4a4792012-02-23 23:10:27 +000017
18 @staticmethod
Enrico Granata7bc0ec32012-02-29 03:28:49 +000019 def is_valid_pointer(pointer, pointer_size, allow_tagged=False, allow_NULL=False):
Enrico Granataeb4a4792012-02-23 23:10:27 +000020 if pointer == None:
21 return False
22 if pointer == 0:
Enrico Granata7bc0ec32012-02-29 03:28:49 +000023 return allow_NULL
24 if allow_tagged and (pointer % 2) == 1:
25 return True
Enrico Granataeb4a4792012-02-23 23:10:27 +000026 return ((pointer % pointer_size) == 0)
27
28 # Objective-C runtime has a rule that pointers in a class_t will only have bits 0 thru 46 set
29 # so if any pointer has bits 47 thru 63 high we know that this is not a valid isa
30 @staticmethod
31 def is_allowed_pointer(pointer):
32 if pointer == None:
33 return False
Enrico Granata7bc0ec32012-02-29 03:28:49 +000034 return ((pointer & 0xFFFF800000000000) == 0)
Enrico Granataeb4a4792012-02-23 23:10:27 +000035
36 @staticmethod
37 def read_child_of(valobj,offset,type):
38 child = valobj.CreateChildAtOffset("childUNK",offset,type)
39 if child == None or child.IsValid() == False:
40 return None;
41 return child.GetValueAsUnsigned()
42
43 @staticmethod
44 def is_valid_identifier(name):
45 if name is None:
46 return None
47 if len(name) == 0:
48 return None
Enrico Granata7bc0ec32012-02-29 03:28:49 +000049 # technically, the ObjC runtime does not enforce any rules about what name a class can have
50 # in practice, the commonly used byte values for a class name are the letters, digits and some
51 # symbols: $, %, -, _, .
52 # WARNING: this means that you cannot use this runtime implementation if you need to deal
53 # with class names that use anything but what is allowed here
54 ok_values = dict.fromkeys("$%_.-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890")
55 return all(c in ok_values for c in name)
56
57 @staticmethod
58 def check_is_osx_lion(target):
59 # assume the only thing that has a Foundation.framework is a Mac
60 # assume anything < Lion does not even exist
61 mod = target.module['Foundation']
62 if mod == None or mod.IsValid() == False:
63 return None
64 ver = mod.GetVersion()
65 if ver == None or ver == []:
66 return None
67 return (ver[0] < 900)
Enrico Granataeb4a4792012-02-23 23:10:27 +000068
69class RoT_Data:
70 def __init__(self,rot_pointer,params):
71 if (Utilities.is_valid_pointer(rot_pointer.GetValueAsUnsigned(),params.pointer_size, allow_tagged=False)):
72 self.sys_params = params
73 self.valobj = rot_pointer
Enrico Granata7bc0ec32012-02-29 03:28:49 +000074 #self.flags = Utilities.read_child_of(self.valobj,0,self.sys_params.uint32_t)
Enrico Granataeb4a4792012-02-23 23:10:27 +000075 self.instanceStart = Utilities.read_child_of(self.valobj,4,self.sys_params.uint32_t)
76 self.instanceSize = Utilities.read_child_of(self.valobj,8,self.sys_params.uint32_t)
Enrico Granata7bc0ec32012-02-29 03:28:49 +000077 offset = 24 if self.sys_params.is_64_bit else 16
78 #self.ivarLayoutPtr = Utilities.read_child_of(self.valobj,offset,self.sys_params.addr_ptr_type)
Enrico Granataeb4a4792012-02-23 23:10:27 +000079 self.namePointer = Utilities.read_child_of(self.valobj,offset,self.sys_params.addr_ptr_type)
80 self.check_valid()
81 else:
82 self.valid = False
83 if self.valid:
84 self.name = Utilities.read_ascii(self.valobj.GetTarget().GetProcess(),self.namePointer)
85 if not(Utilities.is_valid_identifier(self.name)):
86 self.valid = False
87
88 # perform sanity checks on the contents of this class_rw_t
89 def check_valid(self):
90 self.valid = True
91 # misaligned pointers seem to be possible for this field
92 #if not(Utilities.is_valid_pointer(self.namePointer,self.sys_params.pointer_size,allow_tagged=False)):
93 # self.valid = False
94 # pass
95
96 def __str__(self):
Enrico Granata7bc0ec32012-02-29 03:28:49 +000097 return \
Enrico Granataeb4a4792012-02-23 23:10:27 +000098 "instanceStart = " + hex(self.instanceStart) + "\n" + \
99 "instanceSize = " + hex(self.instanceSize) + "\n" + \
Enrico Granataeb4a4792012-02-23 23:10:27 +0000100 "namePointer = " + hex(self.namePointer) + " --> " + self.name
101
102 def is_valid(self):
103 return self.valid
104
105
106class RwT_Data:
107 def __init__(self,rwt_pointer,params):
108 if (Utilities.is_valid_pointer(rwt_pointer.GetValueAsUnsigned(),params.pointer_size, allow_tagged=False)):
109 self.sys_params = params
110 self.valobj = rwt_pointer
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000111 #self.flags = Utilities.read_child_of(self.valobj,0,self.sys_params.uint32_t)
112 #self.version = Utilities.read_child_of(self.valobj,4,self.sys_params.uint32_t)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000113 self.roPointer = Utilities.read_child_of(self.valobj,8,self.sys_params.addr_ptr_type)
114 self.check_valid()
115 else:
116 self.valid = False
117 if self.valid:
118 self.rot = self.valobj.CreateValueFromAddress("rot",self.roPointer,self.sys_params.addr_ptr_type).AddressOf()
119 self.data = RoT_Data(self.rot,self.sys_params)
120
121 # perform sanity checks on the contents of this class_rw_t
122 def check_valid(self):
123 self.valid = True
124 if not(Utilities.is_valid_pointer(self.roPointer,self.sys_params.pointer_size,allow_tagged=False)):
125 self.valid = False
126
127 def __str__(self):
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000128 return \
Enrico Granataeb4a4792012-02-23 23:10:27 +0000129 "roPointer = " + hex(self.roPointer)
130
131 def is_valid(self):
132 if self.valid:
133 return self.data.is_valid()
134 return False
135
136class Class_Data_V2:
137 def __init__(self,isa_pointer,params):
138 if (isa_pointer != None) and (Utilities.is_valid_pointer(isa_pointer.GetValueAsUnsigned(),params.pointer_size, allow_tagged=False)):
139 self.sys_params = params
140 self.valobj = isa_pointer
141 self.isaPointer = Utilities.read_child_of(self.valobj,0,self.sys_params.addr_ptr_type)
142 self.superclassIsaPointer = Utilities.read_child_of(self.valobj,1*self.sys_params.pointer_size,self.sys_params.addr_ptr_type)
143 self.cachePointer = Utilities.read_child_of(self.valobj,2*self.sys_params.pointer_size,self.sys_params.addr_ptr_type)
144 self.vtablePointer = Utilities.read_child_of(self.valobj,3*self.sys_params.pointer_size,self.sys_params.addr_ptr_type)
145 self.dataPointer = Utilities.read_child_of(self.valobj,4*self.sys_params.pointer_size,self.sys_params.addr_ptr_type)
146 self.check_valid()
147 else:
148 self.valid = False
149 if self.valid:
150 self.rwt = self.valobj.CreateValueFromAddress("rwt",self.dataPointer,self.sys_params.addr_ptr_type).AddressOf()
151 self.data = RwT_Data(self.rwt,self.sys_params)
152
153 # perform sanity checks on the contents of this class_t
154 def check_valid(self):
155 self.valid = True
156 if not(Utilities.is_valid_pointer(self.isaPointer,self.sys_params.pointer_size,allow_tagged=False)):
157 self.valid = False
158 return
159 if not(Utilities.is_valid_pointer(self.superclassIsaPointer,self.sys_params.pointer_size,allow_tagged=False)):
160 # NULL is a valid value for superclass (it means we have reached NSObject)
161 if self.superclassIsaPointer != 0:
162 self.valid = False
163 return
164 if not(Utilities.is_valid_pointer(self.cachePointer,self.sys_params.pointer_size,allow_tagged=False)):
165 self.valid = False
166 return
167 if not(Utilities.is_valid_pointer(self.vtablePointer,self.sys_params.pointer_size,allow_tagged=False)):
168 self.valid = False
169 return
170 if not(Utilities.is_valid_pointer(self.dataPointer,self.sys_params.pointer_size,allow_tagged=False)):
171 self.valid = False
172 return
173 if not(Utilities.is_allowed_pointer(self.isaPointer)):
174 self.valid = False
175 return
176 if not(Utilities.is_allowed_pointer(self.superclassIsaPointer)):
177 # NULL is a valid value for superclass (it means we have reached NSObject)
178 if self.superclassIsaPointer != 0:
179 self.valid = False
180 return
181 if not(Utilities.is_allowed_pointer(self.cachePointer)):
182 self.valid = False
183 return
184 if not(Utilities.is_allowed_pointer(self.vtablePointer)):
185 self.valid = False
186 return
187 if not(Utilities.is_allowed_pointer(self.dataPointer)):
188 self.valid = False
189 return
190
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000191 # in general, KVO is implemented by transparently subclassing
192 # however, there could be exceptions where a class does something else
193 # internally to implement the feature - this method will have no clue that a class
194 # has been KVO'ed unless the standard implementation technique is used
Enrico Granataeb4a4792012-02-23 23:10:27 +0000195 def is_kvo(self):
196 if self.is_valid():
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000197 if self.class_name().startswith("NSKVONotifying_"):
Enrico Granataeb4a4792012-02-23 23:10:27 +0000198 return True
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000199 return False
Enrico Granataeb4a4792012-02-23 23:10:27 +0000200
201 def get_superclass(self):
202 if self.is_valid():
203 parent_isa_pointer = self.valobj.CreateChildAtOffset("parent_isa",
204 self.sys_params.pointer_size,
205 self.sys_params.addr_ptr_type)
206 return Class_Data_V2(parent_isa_pointer,self.sys_params)
207 else:
208 return None
209
210 def class_name(self):
211 if self.is_valid():
212 return self.data.data.name
213 else:
214 return None
215
216 def is_valid(self):
217 if self.valid:
218 return self.data.is_valid()
219 return False
220
221 def __str__(self):
222 return 'isaPointer = ' + hex(self.isaPointer) + "\n" + \
223 "superclassIsaPointer = " + hex(self.superclassIsaPointer) + "\n" + \
224 "cachePointer = " + hex(self.cachePointer) + "\n" + \
225 "vtablePointer = " + hex(self.vtablePointer) + "\n" + \
226 "data = " + hex(self.dataPointer)
227
228 def is_tagged(self):
229 return False
230
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000231 def instance_size(self,align=False):
232 if self.is_valid() == False:
233 return None
234 if align:
235 unalign = self.instance_size(False)
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000236 if self.sys_params.is_64_bit:
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000237 return ((unalign + 7) & ~7) % 0x100000000
238 else:
239 return ((unalign + 3) & ~3) % 0x100000000
240 else:
241 return self.rwt.rot.instanceSize
242
Enrico Granataeb4a4792012-02-23 23:10:27 +0000243# 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 +0000244class Class_Data_V1:
245 def __init__(self,isa_pointer,params):
246 if (isa_pointer != None) and (Utilities.is_valid_pointer(isa_pointer.GetValueAsUnsigned(),params.pointer_size, allow_tagged=False)):
Enrico Granata0448d9f2012-02-29 20:46:00 +0000247 self.valid = True
Enrico Granataeb4a4792012-02-23 23:10:27 +0000248 self.sys_params = params
249 self.valobj = isa_pointer
250 self.isaPointer = Utilities.read_child_of(self.valobj,0,self.sys_params.addr_ptr_type)
251 self.superclassIsaPointer = Utilities.read_child_of(self.valobj,1*self.sys_params.pointer_size,self.sys_params.addr_ptr_type)
252 self.namePointer = Utilities.read_child_of(self.valobj,2*self.sys_params.pointer_size,self.sys_params.addr_ptr_type)
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000253 self.version = Utilities.read_child_of(self.valobj,3*self.sys_params.pointer_size,self.sys_params.addr_ptr_type)
254 self.info = Utilities.read_child_of(self.valobj,4*self.sys_params.pointer_size,self.sys_params.addr_ptr_type)
255 self.instanceSize = Utilities.read_child_of(self.valobj,5*self.sys_params.pointer_size,self.sys_params.addr_ptr_type)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000256 else:
257 self.valid = False
258 if self.valid:
259 self.name = Utilities.read_ascii(self.valobj.GetTarget().GetProcess(),self.namePointer)
260 if not(Utilities.is_valid_identifier(self.name)):
261 self.valid = False
262
263 # perform sanity checks on the contents of this class_t
264 def check_valid(self):
265 self.valid = True
266 if not(Utilities.is_valid_pointer(self.isaPointer,self.sys_params.pointer_size,allow_tagged=False)):
267 self.valid = False
268 return
269 if not(Utilities.is_valid_pointer(self.superclassIsaPointer,self.sys_params.pointer_size,allow_tagged=False)):
270 # NULL is a valid value for superclass (it means we have reached NSObject)
271 if self.superclassIsaPointer != 0:
272 self.valid = False
273 return
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000274 if not(Utilities.is_valid_pointer(self.namePointer,self.sys_params.pointer_size,allow_tagged=False,allow_NULL=True)):
275 self.valid = False
276 return
Enrico Granataeb4a4792012-02-23 23:10:27 +0000277
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000278 # in general, KVO is implemented by transparently subclassing
279 # however, there could be exceptions where a class does something else
280 # internally to implement the feature - this method will have no clue that a class
281 # has been KVO'ed unless the standard implementation technique is used
Enrico Granataeb4a4792012-02-23 23:10:27 +0000282 def is_kvo(self):
283 if self.is_valid():
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000284 if self.class_name().startswith("NSKVONotifying_"):
Enrico Granataeb4a4792012-02-23 23:10:27 +0000285 return True
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000286 return False
Enrico Granataeb4a4792012-02-23 23:10:27 +0000287
288 def get_superclass(self):
289 if self.is_valid():
290 parent_isa_pointer = self.valobj.CreateChildAtOffset("parent_isa",
291 self.sys_params.pointer_size,
292 self.sys_params.addr_ptr_type)
293 return Class_Data_V1(parent_isa_pointer,self.sys_params)
294 else:
295 return None
296
297 def class_name(self):
298 if self.is_valid():
299 return self.name
300 else:
301 return None
302
303 def is_valid(self):
304 return self.valid
305
306 def __str__(self):
307 return 'isaPointer = ' + hex(self.isaPointer) + "\n" + \
308 "superclassIsaPointer = " + hex(self.superclassIsaPointer) + "\n" + \
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000309 "namePointer = " + hex(self.namePointer) + " --> " + self.name + \
310 "version = " + hex(self.version) + "\n" + \
311 "info = " + hex(self.info) + "\n" + \
312 "instanceSize = " + hex(self.instanceSize) + "\n"
Enrico Granataeb4a4792012-02-23 23:10:27 +0000313
314 def is_tagged(self):
315 return False
316
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000317 def instance_size(self,align=False):
318 if self.is_valid() == False:
319 return None
320 if align:
321 unalign = self.instance_size(False)
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000322 if self.sys_params.is_64_bit:
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000323 return ((unalign + 7) & ~7) % 0x100000000
324 else:
325 return ((unalign + 3) & ~3) % 0x100000000
326 else:
327 return self.instanceSize
328
Enrico Granata0448d9f2012-02-29 20:46:00 +0000329# these are the only tagged pointers values for current versions
Enrico Granata0c489f52012-03-01 04:24:26 +0000330# of OSX - they might change in future OS releases, and no-one is
Enrico Granata0448d9f2012-02-29 20:46:00 +0000331# advised to rely on these values, or any of the bitmasking formulas
332# in TaggedClass_Data. doing otherwise is at your own risk
Enrico Granata0c489f52012-03-01 04:24:26 +0000333TaggedClass_Values_Lion = {1 : 'NSNumber', \
334 5: 'NSManagedObject', \
335 6: 'NSDate', \
336 7: 'NSDateTS' };
337TaggedClass_Values_NMOS = {0: 'NSAtom', \
338 3 : 'NSNumber', \
339 4: 'NSDateTS', \
340 5: 'NSManagedObject', \
341 6: 'NSDate' };
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000342
Enrico Granataeb4a4792012-02-23 23:10:27 +0000343class TaggedClass_Data:
344 def __init__(self,pointer,params):
Enrico Granata0c489f52012-03-01 04:24:26 +0000345 global TaggedClass_Values_Lion,TaggedClass_Values_NMOS
Enrico Granataeb4a4792012-02-23 23:10:27 +0000346 self.valid = True
347 self.name = None
348 self.sys_params = params
349 self.valobj = pointer
350 self.val = (pointer & ~0x0000000000000000FF) >> 8
351 self.class_bits = (pointer & 0xE) >> 1
352 self.i_bits = (pointer & 0xF0) >> 4
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000353
Enrico Granata0c489f52012-03-01 04:24:26 +0000354 if self.sys_params.is_lion:
355 if self.class_bits in TaggedClass_Values_Lion:
356 self.name = TaggedClass_Values_Lion[self.class_bits]
357 else:
358 self.valid = False
Enrico Granataeb4a4792012-02-23 23:10:27 +0000359 else:
Enrico Granata0c489f52012-03-01 04:24:26 +0000360 if self.class_bits in TaggedClass_Values_NMOS:
361 self.name = TaggedClass_Values_NMOS[self.class_bits]
362 else:
363 self.valid = False
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000364
Enrico Granataeb4a4792012-02-23 23:10:27 +0000365
366 def is_valid(self):
367 return self.valid
368
369 def class_name(self):
370 if self.is_valid():
371 return self.name
372 else:
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000373 return False
Enrico Granataeb4a4792012-02-23 23:10:27 +0000374
375 def value(self):
376 return self.val if self.is_valid() else None
377
378 def info_bits(self):
379 return self.i_bits if self.is_valid() else None
380
381 def is_kvo(self):
382 return False
383
384 # we would need to go around looking for the superclass or ask the runtime
385 # for now, we seem not to require support for this operation so we will merrily
386 # pretend to be at a root point in the hierarchy
387 def get_superclass(self):
388 return None
389
390 # anything that is handled here is tagged
391 def is_tagged(self):
392 return True
393
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000394 # it seems reasonable to say that a tagged pointer is the size of a pointer
395 def instance_size(self,align=False):
396 if self.is_valid() == False:
397 return None
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000398 return 8 if self.sys_params.is_64_bit else 4
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000399
400
Enrico Granataeb4a4792012-02-23 23:10:27 +0000401class InvalidClass_Data:
402 def __init__(self):
403 pass
404 def is_valid(self):
405 return False
406
407runtime_version = cache.Cache()
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000408os_version = cache.Cache()
Enrico Granataeb4a4792012-02-23 23:10:27 +0000409
410class SystemParameters:
411 def __init__(self,valobj):
412 self.adjust_for_architecture(valobj)
413
414 def adjust_for_architecture(self,valobj):
415 self.process = valobj.GetTarget().GetProcess()
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000416 self.is_64_bit = (self.process.GetAddressByteSize() == 8)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000417 self.is_little = (self.process.GetByteOrder() == lldb.eByteOrderLittle)
418 self.pointer_size = self.process.GetAddressByteSize()
419 self.addr_type = valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong)
420 self.addr_ptr_type = self.addr_type.GetPointerType()
421 self.uint32_t = valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedInt)
422 global runtime_version
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000423 global os_version
Enrico Granataeb4a4792012-02-23 23:10:27 +0000424 pid = self.process.GetProcessID()
Enrico Granata21b22362012-02-24 19:46:04 +0000425 if runtime_version.look_for_key(pid):
Enrico Granataeb4a4792012-02-23 23:10:27 +0000426 self.runtime_version = runtime_version.get_value(pid)
427 else:
428 self.runtime_version = ObjCRuntime.runtime_version(self.process)
429 runtime_version.add_item(pid,self.runtime_version)
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000430 if os_version.look_for_key(pid):
431 self.is_lion = os_version.get_value(pid)
432 else:
433 self.is_lion = Utilities.check_is_osx_lion(valobj.GetTarget())
434 os_version.add_item(pid,self.is_lion)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000435
436isa_cache = cache.Cache()
437
438class ObjCRuntime:
439
440 # the ObjC runtime has no explicit "version" field that we can use
441 # instead, we discriminate v1 from v2 by looking for the presence
442 # of a well-known section only present in v1
443 @staticmethod
444 def runtime_version(process):
445 if process.IsValid() == False:
446 return None
447 target = process.GetTarget()
448 num_modules = target.GetNumModules()
449 module_objc = None
450 for idx in range(num_modules):
451 module = target.GetModuleAtIndex(idx)
452 if module.GetFileSpec().GetFilename() == 'libobjc.A.dylib':
453 module_objc = module
454 break
455 if module_objc == None or module_objc.IsValid() == False:
456 return None
457 num_sections = module.GetNumSections()
458 section_objc = None
459 for idx in range(num_sections):
460 section = module.GetSectionAtIndex(idx)
461 if section.GetName() == '__OBJC':
462 section_objc = section
463 break
464 if section_objc != None and section_objc.IsValid():
465 return 1
466 return 2
467
468 def __init__(self,valobj):
469 self.valobj = valobj
470 self.adjust_for_architecture()
471 self.sys_params = SystemParameters(self.valobj)
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000472 self.unsigned_value = self.valobj.GetValueAsUnsigned()
473 self.isa_value = None
Enrico Granataeb4a4792012-02-23 23:10:27 +0000474
475 def adjust_for_architecture(self):
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000476 self.is_64_bit = (self.valobj.GetTarget().GetProcess().GetAddressByteSize() == 8)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000477 self.is_little = (self.valobj.GetTarget().GetProcess().GetByteOrder() == lldb.eByteOrderLittle)
478 self.pointer_size = self.valobj.GetTarget().GetProcess().GetAddressByteSize()
479 self.addr_type = self.valobj.GetType().GetBasicType(lldb.eBasicTypeUnsignedLong)
480 self.addr_ptr_type = self.addr_type.GetPointerType()
481
482# an ObjC pointer can either be tagged or must be aligned
483 def is_tagged(self):
484 if self.valobj is None:
485 return False
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000486 return (Utilities.is_valid_pointer(self.unsigned_value,self.pointer_size, allow_tagged=True) and \
487 not(Utilities.is_valid_pointer(self.unsigned_value,self.pointer_size, allow_tagged=False)))
Enrico Granataeb4a4792012-02-23 23:10:27 +0000488
489 def is_valid(self):
490 if self.valobj is None:
491 return False
492 if self.valobj.IsInScope() == False:
493 return False
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000494 return Utilities.is_valid_pointer(self.unsigned_value,self.pointer_size, allow_tagged=True)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000495
496 def read_isa(self):
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000497 if self.isa_value != None:
498 return self.isa_value
Enrico Granataeb4a4792012-02-23 23:10:27 +0000499 isa_pointer = self.valobj.CreateChildAtOffset("cfisa",
500 0,
501 self.addr_ptr_type)
502 if isa_pointer == None or isa_pointer.IsValid() == False:
503 return None;
504 if isa_pointer.GetValueAsUnsigned(1) == 1:
505 return None;
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000506 self.isa_value = isa_pointer
Enrico Granataeb4a4792012-02-23 23:10:27 +0000507 return isa_pointer
508
509 def read_class_data(self):
510 global isa_cache
511 if self.is_tagged():
512 # tagged pointers only exist in ObjC v2
513 if self.sys_params.runtime_version == 2:
514 # not every odd-valued pointer is actually tagged. most are just plain wrong
515 # we could try and predetect this before even creating a TaggedClass_Data object
516 # but unless performance requires it, this seems a cleaner way to tackle the task
Enrico Granata7bc0ec32012-02-29 03:28:49 +0000517 tentative_tagged = TaggedClass_Data(self.unsigned_value,self.sys_params)
Enrico Granataeb4a4792012-02-23 23:10:27 +0000518 return tentative_tagged if tentative_tagged.is_valid() else InvalidClass_Data()
519 else:
520 return InvalidClass_Data()
521 if self.is_valid() == False:
522 return InvalidClass_Data()
523 isa = self.read_isa()
524 if isa == None:
525 return InvalidClass_Data()
526 isa_value = isa.GetValueAsUnsigned(1)
527 if isa_value == 1:
528 return InvalidClass_Data()
529 data = isa_cache.get_value(isa_value,default=None)
530 if data != None:
531 return data
532 if self.sys_params.runtime_version == 2:
533 data = Class_Data_V2(isa,self.sys_params)
534 else:
535 data = Class_Data_V1(isa,self.sys_params)
536 if data == None:
537 return InvalidClass_Data()
538 if data.is_valid():
539 isa_cache.add_item(isa_value,data,ok_to_replace=True)
540 return data
Enrico Granatab8cbe9c2012-02-23 23:26:48 +0000541