]>
git.saurik.com Git - apple/xnu.git/blob - tools/lldbmacros/core/cvalue.py
07bc25aec5114bea27939cd1a348c35e5eca32af
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.
13 from lazytarget
import *
15 _cstring_rex
= re
.compile("((?:\s*|const\s+)\s*char(?:\s+\*|\s+[A-Za-z_0-9]*\s*\[|)\s*)",re
.MULTILINE|re
.DOTALL
)
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:
23 pt = lldb.value(lldb.frame.FindVariable("pt"))
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
._sbval
19k
84obscure
747 = sbvalue
35 self
._sbval
19k
84obscure
747_type
= sbvalue
.GetType()
36 self
._sbval
19k
84obscure
747_is
_ptr
= sbvalue
.GetType().IsPointerType()
37 self
.sbvalue
= sbvalue
39 def __nonzero__(self
):
40 return ( self
._sbval
19k
84obscure
747.__nonzero
__() and self
._GetValueAsUnsigned
() != 0 )
43 return self
._sbval
19k
84obscure
747.__str
__()
45 def __cmp__(self
, other
):
46 if type(other
) is int:
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 this type")
57 type_name
= self
._sbval
19k
84obscure
747_type
.GetName()
58 if len(_cstring_rex
.findall(type_name
)) > 0 :
59 return self
._GetValueAsString
()
60 summary
= self
._sbval
19k
84obscure
747.GetSummary()
62 return summary
.strip('"')
63 return self
._sbval
19k
84obscure
747.__str
__()
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
)
75 retval
.append(self
[_start
])
78 if type(key
) in (int, long):
79 return value(self
._sbval
19k
84obscure
747.GetValueForExpressionPath("[%i]" % key
))
80 if type(key
) is value
:
81 return value(self
._sbval
19k
84obscure
747.GetValueForExpressionPath("[%i]" % int(key
)))
82 raise TypeError("Cannot fetch Array item for this type")
84 def __getattr__(self
, name
):
85 child_sbvalue
= self
._sbval
19k
84obscure
747.GetChildMemberWithName (name
)
87 return value(child_sbvalue
)
88 raise AttributeError("No field by name: "+name
)
90 def __add__(self
, other
):
91 return int(self
) + int(other
)
93 def __radd__(self
, other
):
94 return int(self
) + int(other
)
96 def __sub__(self
, other
):
97 return int(self
) - int(other
)
99 def __rsub__(self
, other
):
100 return int(other
) - int(self
)
102 def __mul__(self
, other
):
103 return int(self
) * int(other
)
105 def __rmul__(self
, other
):
106 return int(self
) * int(other
)
108 def __floordiv__(self
, other
):
109 return int(self
) // int(other
)
111 def __mod__(self
, other
):
112 return int(self
) % int(other
)
114 def __rmod__(self
, other
):
115 return int(other
) % int(self
)
117 def __divmod__(self
, other
):
118 return int(self
) % int(other
)
120 def __rdivmod__(self
, other
):
121 return int(other
) % int(self
)
123 def __pow__(self
, other
):
124 return int(self
) ** int(other
)
126 def __lshift__(self
, other
):
127 return int(self
) << int(other
)
129 def __rshift__(self
, other
):
130 return int(self
) >> int(other
)
132 def __and__(self
, other
):
133 return int(self
) & int(other
)
135 def __rand(self
, other
):
136 return int(self
) & int(other
)
138 def __xor__(self
, other
):
139 return int(self
) ^
int(other
)
141 def __or__(self
, other
):
142 return int(self
) |
int(other
)
144 def __div__(self
, other
):
145 return int(self
) / int(other
)
147 def __rdiv__(self
, other
):
148 return int(other
)/int(self
)
150 def __truediv__(self
, other
):
151 return int(self
) / int(other
)
153 def __iadd__(self
, other
):
154 result
= self
.__add
__(other
)
155 self
._sbval
19k
84obscure
747.SetValueFromCString (str(result
))
158 def __isub__(self
, other
):
159 result
= self
.__sub
__(other
)
160 self
._sbval
19k
84obscure
747.SetValueFromCString (str(result
))
163 def __imul__(self
, other
):
164 result
= self
.__mul
__(other
)
165 self
._sbval
19k
84obscure
747.SetValueFromCString (str(result
))
168 def __idiv__(self
, other
):
169 result
= self
.__div
__(other
)
170 self
._sbval
19k
84obscure
747.SetValueFromCString (str(result
))
173 def __itruediv__(self
, other
):
174 result
= self
.__truediv
__(other
)
175 self
._sbval
19k
84obscure
747.SetValueFromCString (str(result
))
178 def __ifloordiv__(self
, other
):
179 result
= self
.__floordiv
__(self
, other
)
180 self
._sbval
19k
84obscure
747.SetValueFromCString (str(result
))
183 def __imod__(self
, other
):
184 result
= self
.__and
__(self
, other
)
185 self
._sbval
19k
84obscure
747.SetValueFromCString (str(result
))
188 def __ipow__(self
, other
):
189 result
= self
.__pow
__(self
, other
)
190 self
._sbval
19k
84obscure
747.SetValueFromCString (str(result
))
193 def __ipow__(self
, other
, modulo
):
194 result
= self
.__pow
__(self
, other
, modulo
)
195 self
._sbval
19k
84obscure
747.SetValueFromCString (str(result
))
198 def __ilshift__(self
, other
):
199 result
= self
.__lshift
__(other
)
200 self
._sbval
19k
84obscure
747.SetValueFromCString (str(result
))
203 def __irshift__(self
, other
):
204 result
= self
.__rshift
__(other
)
205 self
._sbval
19k
84obscure
747.SetValueFromCString (str(result
))
208 def __iand__(self
, other
):
209 result
= self
.__and
__(self
, other
)
210 self
._sbval
19k
84obscure
747.SetValueFromCString (str(result
))
213 def __ixor__(self
, other
):
214 result
= self
.__xor
__(self
, other
)
215 self
._sbval
19k
84obscure
747.SetValueFromCString (str(result
))
218 def __ior__(self
, other
):
219 result
= self
.__ior
__(self
, other
)
220 self
._sbval
19k
84obscure
747.SetValueFromCString (str(result
))
230 return abs(int(self
))
232 def __invert__(self
):
235 def __complex__(self
):
236 return complex (int(self
))
239 if self
._sbval
19k
84obscure
747_is
_ptr
:
240 return self
._GetValueAsUnsigned
()
241 tname
= self
._sbval
19k
84obscure
747_type
.GetName()
242 if tname
.find('uint') >= 0 or tname
.find('unsigned') >= 0:
243 return self
._GetValueAsUnsigned
()
244 retval
= self
._sbval
19k
84obscure
747.GetValueAsSigned()
245 # <rdar://problem/12481949> lldb python: GetValueAsSigned does not return the correct value
246 if (retval
& 0x80000000):
247 retval
= retval
- 0x100000000
251 return self
._sbval
19k
84obscure
747.GetValueAsSigned()
254 return float (self
._sbval
19k
84obscure
747.GetValueAsSigned())
257 return '0%o' % self
._GetValueAsUnsigned
()
260 return '0x%x' % self
._GetValueAsUnsigned
()
262 def __eq__(self
, other
):
263 self_err
= lldb
.SBError()
264 other_err
= lldb
.SBError()
265 self_val
= self
._sbval
19k
84obscure
747.GetValueAsUnsigned(self_err
)
267 raise ValueError("unable to extract value of self")
268 if type(other
) is value
:
269 other_val
= other
._sbval
19k
84obscure
747.GetValueAsUnsigned(other_err
)
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.")
277 def __neq__(self
, other
):
278 return not self
.__eq
__(other
)
280 def GetSBValue(self
):
281 return self
._sbval
19k
84obscure
747
283 def _GetValueAsSigned(self
):
284 serr
= lldb
.SBError()
285 retval
= self
._sbval
19k
84obscure
747.GetValueAsSigned(serr
)
288 raise ValueError("Failed to read signed data. "+ str(self
._sbval
19k
84obscure
747) +"(type =" + str(self
._sbval
19k
84obscure
747_type
) + ") Error description: " + serr
.GetCString())
290 def _GetValueAsUnsigned(self
):
291 serr
= lldb
.SBError()
292 retval
= self
._sbval
19k
84obscure
747.GetValueAsUnsigned(serr
)
295 raise ValueError("Failed to read unsigned data. "+ str(self
._sbval
19k
84obscure
747) +"(type =" + str(self
._sbval
19k
84obscure
747_type
) + ") Error description: " + serr
.GetCString())
297 def _GetValueAsString(self
, offset
= 0, maxlen
= 1024):
298 serr
= lldb
.SBError()
300 if self
._sbval
19k
84obscure
747.TypeIsPointerType():
301 sbdata
= self
._sbval
19k
84obscure
747.GetPointeeData(offset
, maxlen
)
303 sbdata
= self
._sbval
19k
84obscure
747.GetData()
306 bytesize
= sbdata
.GetByteSize()
308 #raise ValueError('Unable to read value as string')
310 for i
in range(0, bytesize
) :
312 ch
= chr(sbdata
.GetUnsignedInt8(serr
, i
))
314 raise ValueError("Unable to read string data: " + serr
.GetCString())
320 def __format__(self
, format_spec
):
321 ret_format
= "{0:"+format_spec+"}"
322 # typechar is last char. see http://www.python.org/dev/peps/pep-3101/
323 type_spec
=format_spec
.strip().lower()[-1]
325 return ret_format
.format(self
._GetValueAsUnsigned
())
327 return ret_format
.format(int(self
))
329 return ret_format
.format(str(self
))
331 return ret_format
.format(int(oct(self
)))
333 return ret_format
.format(int(self
))
335 return "unknown format " + format_spec
+ str(self
)
339 """ Helper function to get unsigned value from core.value
340 params: val - value (see value class above) representation of an integer type
341 returns: int which is unsigned.
342 raises : ValueError if the type cannot be represented as unsigned int.
344 if type(val
) is value
:
345 return val
._GetValueAsUnsigned
()
349 """ Find the byte size of a type.
350 params: t - str : ex 'time_spec' returns equivalent of sizeof(time_spec) in C
351 t - value: ex a value object. returns size of the object
352 returns: int - byte size length
354 if type(t
) is value
:
355 return t
.GetSBValue().GetByteSize()
357 return gettype(t
).GetByteSize()
358 raise ValueError("Cannot get sizeof. Invalid argument")
361 def dereference(val
):
362 """ Get a dereferenced obj for a pointer type obj
363 params: val - value object representing a pointer type C construct in lldb
364 returns: value - value
365 ex. val = dereference(ptr_obj) #python
367 obj_ptr = (int *)0x1234 #C
370 if type(val
) is value
and val
.GetSBValue().TypeIsPointerType():
371 return value(val
.GetSBValue().Dereference())
372 raise TypeError('Cannot dereference this type.')
375 """ Get address of a core.value object.
376 params: val - value object representing a C construct in lldb
377 returns: value - value object referring to 'type(val) *' type
378 ex. addr = addressof(hello_obj) #python
380 uintptr_t addr = (uintptr_t)&hello_obj #C
382 if type(val
) is value
:
383 return value(val
.GetSBValue().AddressOf())
384 raise TypeError("Cannot do addressof for non-value type objects")
386 def cast(obj
, target_type
):
387 """ Type cast an object to another C type.
389 obj - core.value object representing some C construct in lldb
390 target_type - str : ex 'char *'
393 dest_type
= target_type
394 if type(target_type
) is str:
395 dest_type
= gettype(target_type
)
396 elif type(target_type
) is value
:
397 dest_type
= target_type
.GetSBValue().GetType()
399 if type(obj
) is value
:
400 return value(obj
.GetSBValue().Cast(dest_type
))
401 elif type(obj
) is int:
402 print "ERROR: You cannot cast an 'int' to %s, please use kern.GetValueFromAddress() for such purposes." % str(target_type
)
403 raise TypeError("object of type %s cannot be casted to %s" % (str(type(obj
)), str(target_type
)))
405 _value_types_cache
={}
407 def gettype(target_type
):
408 """ Returns lldb.SBType of the given target_type
410 target_type - str, ex. 'char', 'uint32_t' etc
412 lldb.SBType - SBType corresponding to the given target_type
414 NameError - Incase the type is not identified
416 global _value_types_cache
417 # LLDB Somehow does not support finding types for 'struct pmap' while 'pmap' works fine
418 # <rdar://problem/12473003>
419 target_type
= target_type
.replace('struct', '')
420 target_type
= str(target_type
).strip()
421 if target_type
not in _value_types_cache
:
423 if target_type
.endswith('*') :
424 tmp_type
= LazyTarget
.GetTarget().FindFirstType(target_type
.rstrip('*').strip())
425 if not tmp_type
.IsValid():
426 raise NameError('Unable to Cast to type '+target_type
)
427 tmp_type
= tmp_type
.GetPointerType()
429 tmp_type
= LazyTarget
.GetTarget().FindFirstType(target_type
)
430 if not tmp_type
.IsValid():
431 raise NameError('Unable to Cast to type '+target_type
)
432 _value_types_cache
[target_type
] = tmp_type
433 return _value_types_cache
[target_type
]
435 def getfieldoffset(struct_type
, field_name
):
436 """ Returns the byte offset of a field inside a given struct
438 struct_type - str or lldb.SBType, ex. 'struct ipc_port *' or port.gettype()
439 field_name - str, name of the field inside the struct ex. 'ip_messages'
441 int - byte offset of the field_name inside the struct_type
443 TypeError - - In case the struct_type has no field with the name field_name
445 if type(struct_type
) == str:
446 struct_type
= gettype(struct_type
)
448 for field
in struct_type
.get_fields_array():
449 if str(field
.GetName()) == field_name
:
450 return field
.GetOffsetInBytes()
451 raise TypeError('Field name "%s" not found in type "%s"' % (field_name
, str(struct_type
)))
454 """ Returns True if a string represents a long integer, False otherwise