]> git.saurik.com Git - apple/xnu.git/blob - tools/lldbmacros/core/cvalue.py
e58c7752fd4947cb686857d0e41d03ca402d6773
[apple/xnu.git] / tools / lldbmacros / core / cvalue.py
1 """
2 Defines a class value which encapsulates the basic lldb Scripting Bridge APIs. This provides an easy
3 wrapper to extract information from C based constructs.
4 |------- core.value------------|
5 | |--lldb Scripting Bridge--| |
6 | | |--lldb core--| | |
7 | |-------------------------| |
8 |------------------------------|
9 Use the member function GetSBValue() to access the base Scripting Bridge value.
10 """
11 import lldb
12 import re
13 from lazytarget import *
14
15 _cstring_rex = re.compile("((?:\s*|const\s+)\s*char(?:\s+\*|\s+[A-Za-z_0-9]*\s*\[|)\s*)",re.MULTILINE|re.DOTALL)
16
17 class value(object):
18 '''A class designed to wrap lldb.SBValue() objects so the resulting object
19 can be used as a variable would be in code. So if you have a Point structure
20 variable in your code in the current frame named "pt", you can initialize an instance
21 of this class with it:
22
23 pt = lldb.value(lldb.frame.FindVariable("pt"))
24 print pt
25 print pt.x
26 print pt.y
27
28 pt = lldb.value(lldb.frame.FindVariable("rectangle_array"))
29 print rectangle_array[12]
30 print rectangle_array[5].origin.x'''
31 def __init__(self, sbvalue):
32 #_sbval19k84obscure747 is specifically chosen to be obscure.
33 #This avoids conflicts when attributes could mean any field value in code
34 self._sbval19k84obscure747 = sbvalue
35 self._sbval19k84obscure747_type = sbvalue.GetType()
36 self._sbval19k84obscure747_is_ptr = sbvalue.GetType().IsPointerType()
37 self.sbvalue = sbvalue
38
39 def __nonzero__(self):
40 return ( self._sbval19k84obscure747.__nonzero__() and self._GetValueAsUnsigned() != 0 )
41
42 def __repr__(self):
43 return self._sbval19k84obscure747.__str__()
44
45 def __cmp__(self, other):
46 if type(other) is int or type(other) is long:
47 me = int(self)
48 if type(me) is long:
49 other = long(other)
50 return me.__cmp__(other)
51 if type(other) is value:
52 return int(self).__cmp__(int(other))
53 raise TypeError("Cannot compare value with type {}".format(type(other)))
54
55 def __str__(self):
56 global _cstring_rex
57 type_name = self._sbval19k84obscure747_type.GetName()
58 if len(_cstring_rex.findall(type_name)) > 0 :
59 return self._GetValueAsString()
60 summary = self._sbval19k84obscure747.GetSummary()
61 if summary:
62 return summary.strip('"')
63 return self._sbval19k84obscure747.__str__()
64
65 def __getitem__(self, key):
66 # Allow array access if this value has children...
67 if type(key) is slice:
68 _start = int(key.start)
69 _end = int(key.stop)
70 _step = 1
71 if key.step != None:
72 _step = int(key.step)
73 retval = []
74 while _start < _end:
75 retval.append(self[_start])
76 _start += _step
77 return retval
78 if type(key) in (int, long):
79 return value(self._sbval19k84obscure747.GetValueForExpressionPath("[%i]" % key))
80 if type(key) is value:
81 return value(self._sbval19k84obscure747.GetValueForExpressionPath("[%i]" % int(key)))
82 raise TypeError("Cannot fetch Array item for this type")
83
84 def __getattr__(self, name):
85 child_sbvalue = self._sbval19k84obscure747.GetChildMemberWithName (name)
86 if child_sbvalue:
87 return value(child_sbvalue)
88 raise AttributeError("No field by name: "+name )
89
90 def __add__(self, other):
91 return int(self) + int(other)
92
93 def __radd__(self, other):
94 return int(self) + int(other)
95
96 def __sub__(self, other):
97 return int(self) - int(other)
98
99 def __rsub__(self, other):
100 return int(other) - int(self)
101
102 def __mul__(self, other):
103 return int(self) * int(other)
104
105 def __rmul__(self, other):
106 return int(self) * int(other)
107
108 def __floordiv__(self, other):
109 return int(self) // int(other)
110
111 def __mod__(self, other):
112 return int(self) % int(other)
113
114 def __rmod__(self, other):
115 return int(other) % int(self)
116
117 def __divmod__(self, other):
118 return int(self) % int(other)
119
120 def __rdivmod__(self, other):
121 return int(other) % int(self)
122
123 def __pow__(self, other):
124 return int(self) ** int(other)
125
126 def __lshift__(self, other):
127 return int(self) << int(other)
128
129 def __rshift__(self, other):
130 return int(self) >> int(other)
131
132 def __and__(self, other):
133 return int(self) & int(other)
134
135 def __rand(self, other):
136 return int(self) & int(other)
137
138 def __xor__(self, other):
139 return int(self) ^ int(other)
140
141 def __or__(self, other):
142 return int(self) | int(other)
143
144 def __div__(self, other):
145 return int(self) / int(other)
146
147 def __rdiv__(self, other):
148 return int(other)/int(self)
149
150 def __truediv__(self, other):
151 return int(self) / int(other)
152
153 def __iadd__(self, other):
154 result = self.__add__(other)
155 self._sbval19k84obscure747.SetValueFromCString (str(result))
156 return result
157
158 def __isub__(self, other):
159 result = self.__sub__(other)
160 self._sbval19k84obscure747.SetValueFromCString (str(result))
161 return result
162
163 def __imul__(self, other):
164 result = self.__mul__(other)
165 self._sbval19k84obscure747.SetValueFromCString (str(result))
166 return result
167
168 def __idiv__(self, other):
169 result = self.__div__(other)
170 self._sbval19k84obscure747.SetValueFromCString (str(result))
171 return result
172
173 def __itruediv__(self, other):
174 result = self.__truediv__(other)
175 self._sbval19k84obscure747.SetValueFromCString (str(result))
176 return result
177
178 def __ifloordiv__(self, other):
179 result = self.__floordiv__(self, other)
180 self._sbval19k84obscure747.SetValueFromCString (str(result))
181 return result
182
183 def __imod__(self, other):
184 result = self.__and__(self, other)
185 self._sbval19k84obscure747.SetValueFromCString (str(result))
186 return result
187
188 def __ipow__(self, other):
189 result = self.__pow__(self, other)
190 self._sbval19k84obscure747.SetValueFromCString (str(result))
191 return result
192
193 def __ipow__(self, other, modulo):
194 result = self.__pow__(self, other, modulo)
195 self._sbval19k84obscure747.SetValueFromCString (str(result))
196 return result
197
198 def __ilshift__(self, other):
199 result = self.__lshift__(other)
200 self._sbval19k84obscure747.SetValueFromCString (str(result))
201 return result
202
203 def __irshift__(self, other):
204 result = self.__rshift__(other)
205 self._sbval19k84obscure747.SetValueFromCString (str(result))
206 return result
207
208 def __iand__(self, other):
209 result = self.__and__(self, other)
210 self._sbval19k84obscure747.SetValueFromCString (str(result))
211 return result
212
213 def __ixor__(self, other):
214 result = self.__xor__(self, other)
215 self._sbval19k84obscure747.SetValueFromCString (str(result))
216 return result
217
218 def __ior__(self, other):
219 result = self.__ior__(self, other)
220 self._sbval19k84obscure747.SetValueFromCString (str(result))
221 return result
222
223 def __neg__(self):
224 return -int(self)
225
226 def __pos__(self):
227 return +int(self)
228
229 def __abs__(self):
230 return abs(int(self))
231
232 def __invert__(self):
233 return ~int(self)
234
235 def __complex__(self):
236 return complex (int(self))
237
238 def __int__(self):
239 if self._sbval19k84obscure747_is_ptr:
240 return self._GetValueAsUnsigned()
241 tname= self._sbval19k84obscure747_type.GetName()
242 if tname.find('uint') >= 0 or tname.find('unsigned') >= 0:
243 return self._GetValueAsUnsigned()
244 retval = self._sbval19k84obscure747.GetValueAsSigned()
245 # <rdar://problem/12481949> lldb python: GetValueAsSigned does not return the correct value
246 if (retval & 0x80000000):
247 retval = retval - 0x100000000
248 return retval
249
250 def __long__(self):
251 return self._sbval19k84obscure747.GetValueAsSigned()
252
253 def __float__(self):
254 return float (self._sbval19k84obscure747.GetValueAsSigned())
255
256 def __oct__(self):
257 return '0%o' % self._GetValueAsUnsigned()
258
259 def __hex__(self):
260 return '0x%x' % self._GetValueAsUnsigned()
261
262 def __eq__(self, other):
263 self_err = lldb.SBError()
264 other_err = lldb.SBError()
265 self_val = self._sbval19k84obscure747.GetValueAsUnsigned(self_err)
266 if self_err.fail:
267 raise ValueError("unable to extract value of self")
268 if type(other) is value:
269 other_val = other._sbval19k84obscure747.GetValueAsUnsigned(other_err)
270 if other_err.fail:
271 raise ValueError("unable to extract value of other")
272 return self_val == other_val
273 if type(other) is int:
274 return int(self) == other
275 raise TypeError("Equality operation is not defined for this type.")
276
277 def __neq__(self, other):
278 return not self.__eq__(other)
279
280 def GetSBValue(self):
281 return self._sbval19k84obscure747
282
283 def __getstate__(self):
284 err = lldb.SBError()
285 if self._sbval19k84obscure747_is_ptr:
286 addr = self._sbval19k84obscure747.GetValueAsUnsigned()
287 size = self._sbval19k84obscure747_type.GetPointeeType().GetByteSize()
288 else:
289 addr = self._sbval19k84obscure747.AddressOf().GetValueAsUnsigned()
290 size = self._sbval19k84obscure747_type.GetByteSize()
291
292 content = LazyTarget.GetProcess().ReadMemory(addr, size, err)
293 if err.fail:
294 content = ''
295 return content
296
297 def _GetValueAsSigned(self):
298 if self._sbval19k84obscure747_is_ptr:
299 print "ERROR: You cannot get 'int' from pointer type %s, please use unsigned(obj) for such purposes." % str(self._sbval19k84obscure747_type)
300 raise ValueError("Cannot get signed int for pointer data.")
301 serr = lldb.SBError()
302 retval = self._sbval19k84obscure747.GetValueAsSigned(serr)
303 if serr.success:
304 return retval
305 raise ValueError("Failed to read signed data. "+ str(self._sbval19k84obscure747) +"(type =" + str(self._sbval19k84obscure747_type) + ") Error description: " + serr.GetCString())
306
307 def _GetValueAsCast(self, dest_type):
308 if type(dest_type) is not lldb.SBType:
309 raise ValueError("Invalid type for dest_type: {}".format(type(dest_type)))
310 addr = self._GetValueAsUnsigned()
311 sbval = self._sbval19k84obscure747.target.CreateValueFromExpression("newname", "(void *)"+str(addr))
312 val = value(sbval.Cast(dest_type))
313 return val
314
315 def _GetValueAsUnsigned(self):
316 serr = lldb.SBError()
317 if self._sbval19k84obscure747_is_ptr:
318 retval = self._sbval19k84obscure747.GetValueAsAddress()
319 else:
320 retval = self._sbval19k84obscure747.GetValueAsUnsigned(serr)
321 if serr.success:
322 return retval
323 raise ValueError("Failed to read unsigned data. "+ str(self._sbval19k84obscure747) +"(type =" + str(self._sbval19k84obscure747_type) + ") Error description: " + serr.GetCString())
324
325 def _GetValueAsString(self, offset = 0, maxlen = 1024):
326 serr = lldb.SBError()
327 sbdata = None
328 if self._sbval19k84obscure747_is_ptr:
329 sbdata = self._sbval19k84obscure747.GetPointeeData(offset, maxlen)
330 else:
331 sbdata = self._sbval19k84obscure747.GetData()
332
333 retval = ''
334 bytesize = sbdata.GetByteSize()
335 if bytesize == 0 :
336 #raise ValueError('Unable to read value as string')
337 return ''
338 for i in range(0, bytesize) :
339 serr.Clear()
340 ch = chr(sbdata.GetUnsignedInt8(serr, i))
341 if serr.fail :
342 raise ValueError("Unable to read string data: " + serr.GetCString())
343 if ch == '\0':
344 break
345 retval += ch
346 return retval
347
348 def __format__(self, format_spec):
349 ret_format = "{0:"+format_spec+"}"
350 # typechar is last char. see http://www.python.org/dev/peps/pep-3101/
351 type_spec=format_spec.strip().lower()[-1]
352 if type_spec == 'x':
353 return ret_format.format(self._GetValueAsUnsigned())
354 if type_spec == 'd':
355 return ret_format.format(int(self))
356 if type_spec == 's':
357 return ret_format.format(str(self))
358 if type_spec == 'o':
359 return ret_format.format(int(oct(self)))
360 if type_spec == 'c':
361 return ret_format.format(int(self))
362
363 return "unknown format " + format_spec + str(self)
364
365
366 def unsigned(val):
367 """ Helper function to get unsigned value from core.value
368 params: val - value (see value class above) representation of an integer type
369 returns: int which is unsigned.
370 raises : ValueError if the type cannot be represented as unsigned int.
371 """
372 if type(val) is value:
373 return val._GetValueAsUnsigned()
374 return int(val)
375
376 def sizeof(t):
377 """ Find the byte size of a type.
378 params: t - str : ex 'time_spec' returns equivalent of sizeof(time_spec) in C
379 t - value: ex a value object. returns size of the object
380 returns: int - byte size length
381 """
382 if type(t) is value :
383 return t.GetSBValue().GetByteSize()
384 if type(t) is str:
385 return gettype(t).GetByteSize()
386 raise ValueError("Cannot get sizeof. Invalid argument")
387
388
389 def dereference(val):
390 """ Get a dereferenced obj for a pointer type obj
391 params: val - value object representing a pointer type C construct in lldb
392 returns: value - value
393 ex. val = dereference(ptr_obj) #python
394 is same as
395 obj_ptr = (int *)0x1234 #C
396 val = *obj_ptr #C
397 """
398 if type(val) is value and val._sbval19k84obscure747_is_ptr:
399 return value(val.GetSBValue().Dereference())
400 raise TypeError('Cannot dereference this type.')
401
402 def addressof(val):
403 """ Get address of a core.value object.
404 params: val - value object representing a C construct in lldb
405 returns: value - value object referring to 'type(val) *' type
406 ex. addr = addressof(hello_obj) #python
407 is same as
408 uintptr_t addr = (uintptr_t)&hello_obj #C
409 """
410 if type(val) is value:
411 return value(val.GetSBValue().AddressOf())
412 raise TypeError("Cannot do addressof for non-value type objects")
413
414 def cast(obj, target_type):
415 """ Type cast an object to another C type.
416 params:
417 obj - core.value object representing some C construct in lldb
418 target_type - str : ex 'char *'
419 - lldb.SBType :
420 """
421 dest_type = target_type
422 if type(target_type) is str:
423 dest_type = gettype(target_type)
424 elif type(target_type) is value:
425 dest_type = target_type.GetSBValue().GetType()
426
427 if type(obj) is value:
428 return obj._GetValueAsCast(dest_type)
429 elif type(obj) is int:
430 print "ERROR: You cannot cast an 'int' to %s, please use kern.GetValueFromAddress() for such purposes." % str(target_type)
431 raise TypeError("object of type %s cannot be casted to %s" % (str(type(obj)), str(target_type)))
432
433 def containerof(obj, target_type, field_name):
434 """ Type cast an object to another C type from a pointer to a field.
435 params:
436 obj - core.value object representing some C construct in lldb
437 target_type - str : ex 'struct thread'
438 - lldb.SBType :
439 field_name - the field name within the target_type obj is a pointer to
440 """
441 addr = int(obj) - getfieldoffset(target_type, field_name)
442 obj = value(obj.GetSBValue().CreateValueFromExpression(None,'(void *)'+str(addr)))
443 return cast(obj, target_type + " *")
444
445
446 _value_types_cache={}
447
448 def gettype(target_type):
449 """ Returns lldb.SBType of the given target_type
450 params:
451 target_type - str, ex. 'char', 'uint32_t' etc
452 returns:
453 lldb.SBType - SBType corresponding to the given target_type
454 raises:
455 NameError - Incase the type is not identified
456 """
457 global _value_types_cache
458 target_type = str(target_type).strip()
459 if target_type in _value_types_cache:
460 return _value_types_cache[target_type]
461
462 target_type = target_type.strip()
463
464 requested_type_is_struct = False
465 m = re.match(r'\s*struct\s*(.*)$', target_type)
466 if m:
467 requested_type_is_struct = True
468 target_type = m.group(1)
469
470 tmp_type = None
471 requested_type_is_pointer = False
472 if target_type.endswith('*') :
473 requested_type_is_pointer = True
474
475 # tmp_type = LazyTarget.GetTarget().FindFirstType(target_type.rstrip('*').strip())
476 search_type = target_type.rstrip('*').strip()
477 type_arr = [t for t in LazyTarget.GetTarget().FindTypes(search_type)]
478
479 if requested_type_is_struct:
480 type_arr = [t for t in type_arr if t.type == lldb.eTypeClassStruct]
481
482 # After the sort, the struct type with more fields will be at index [0].
483 # This hueristic helps selecting struct type with more fields compared to ones with "opaque" members
484 type_arr.sort(reverse=True, key=lambda x: x.GetNumberOfFields())
485 if len(type_arr) > 0:
486 tmp_type = type_arr[0]
487 else:
488 raise NameError('Unable to find type '+target_type)
489
490 if not tmp_type.IsValid():
491 raise NameError('Unable to Cast to type '+target_type)
492
493 if requested_type_is_pointer:
494 tmp_type = tmp_type.GetPointerType()
495 _value_types_cache[target_type] = tmp_type
496
497 return _value_types_cache[target_type]
498
499
500 def getfieldoffset(struct_type, field_name):
501 """ Returns the byte offset of a field inside a given struct
502 Understands anonymous unions and field names in sub-structs
503 params:
504 struct_type - str or lldb.SBType, ex. 'struct ipc_port *' or port.gettype()
505 field_name - str, name of the field inside the struct ex. 'ip_messages'
506 returns:
507 int - byte offset of the field_name inside the struct_type
508 raises:
509 TypeError - - In case the struct_type has no field with the name field_name
510 """
511
512 if type(struct_type) == str:
513 struct_type = gettype(struct_type)
514
515 if '.' in field_name :
516 # Handle recursive fields in sub-structs
517 components = field_name.split('.', 1)
518 for field in struct_type.get_fields_array():
519 if str(field.GetName()) == components[0]:
520 return getfieldoffset(struct_type, components[0]) + getfieldoffset(field.GetType(), components[1])
521 raise TypeError('Field name "%s" not found in type "%s"' % (components[0], str(struct_type)))
522
523 offset = 0
524 for field in struct_type.get_fields_array():
525 if str(field.GetName()) == field_name:
526 return field.GetOffsetInBytes()
527
528 # Hack for anonymous unions - the compiler does this, so cvalue should too
529 if field.GetName() is None and field.GetType().GetTypeClass() == lldb.eTypeClassUnion :
530 for union_field in field.GetType().get_fields_array():
531 if str(union_field.GetName()) == field_name:
532 return union_field.GetOffsetInBytes() + field.GetOffsetInBytes()
533 raise TypeError('Field name "%s" not found in type "%s"' % (field_name, str(struct_type)))
534
535 def islong(x):
536 """ Returns True if a string represents a long integer, False otherwise
537 """
538 try:
539 long(x,16)
540 except ValueError:
541 try:
542 long(x)
543 except ValueError:
544 return False
545 return True
546
547 def readmemory(val):
548 """ Returns a string of hex data that is referenced by the value.
549 params: val - a value object.
550 return: str - string of hex bytes.
551 raises: TypeError if val is not a valid type
552 """
553 if not type(val) is value:
554 raise TypeError('%s is not of type value' % str(type(val)))
555 return val.__getstate__()