1*515bc8c1Sserge-sans-paille#!/usr/bin/env python
20a94b661SGreg Clayton
30a94b661SGreg Claytonimport lldb
40a94b661SGreg Clayton
5b9c1b51eSKate Stone
60a94b661SGreg Claytonclass value(object):
70a94b661SGreg Clayton    '''A class that wraps an lldb.SBValue object and returns an object that
80a94b661SGreg Clayton    can be used as an object with attribytes:\n
90a94b661SGreg Clayton    argv = a.value(lldb.frame.FindVariable('argv'))\n
100a94b661SGreg Clayton    argv.name - return the name of the value that this object contains\n
110a94b661SGreg Clayton    argv.type - return the lldb.SBType for this value
120a94b661SGreg Clayton    argv.type_name - return the name of the type
130a94b661SGreg Clayton    argv.size - return the byte size of this value
140a94b661SGreg Clayton    argv.is_in_scope - return true if this value is currently in scope
150a94b661SGreg Clayton    argv.is_pointer - return true if this value is a pointer
160a94b661SGreg Clayton    argv.format - return the current format for this value
170a94b661SGreg Clayton    argv.value - return the value's value as a string
180a94b661SGreg Clayton    argv.summary - return a summary of this value's value
190a94b661SGreg Clayton    argv.description - return the runtime description for this value
200a94b661SGreg Clayton    argv.location - return a string that represents the values location (address, register, etc)
210a94b661SGreg Clayton    argv.target - return the lldb.SBTarget for this value
220a94b661SGreg Clayton    argv.process - return the lldb.SBProcess for this value
230a94b661SGreg Clayton    argv.thread - return the lldb.SBThread for this value
240a94b661SGreg Clayton    argv.frame - return the lldb.SBFrame for this value
250a94b661SGreg Clayton    argv.num_children - return the number of children this value has
260a94b661SGreg Clayton    argv.children - return a list of sbvalue objects that represents all of the children of this value
270a94b661SGreg Clayton    '''
28b9c1b51eSKate Stone
290a94b661SGreg Clayton    def __init__(self, sbvalue):
300a94b661SGreg Clayton        self.sbvalue = sbvalue
310a94b661SGreg Clayton
320a94b661SGreg Clayton    def __nonzero__(self):
330a94b661SGreg Clayton        return self.sbvalue.__nonzero__()
340a94b661SGreg Clayton
350a94b661SGreg Clayton    def __repr__(self):
360a94b661SGreg Clayton        return self.sbvalue.__repr__()
370a94b661SGreg Clayton
380a94b661SGreg Clayton    def __str__(self):
390a94b661SGreg Clayton        return self.sbvalue.__str__()
400a94b661SGreg Clayton
410a94b661SGreg Clayton    def __getitem__(self, key):
42b9c1b51eSKate Stone        if isinstance(key, int):
43b9c1b51eSKate Stone            return value(
44b9c1b51eSKate Stone                self.sbvalue.GetChildAtIndex(
45b9c1b51eSKate Stone                    key, lldb.eNoDynamicValues, True))
460a94b661SGreg Clayton        raise TypeError
470a94b661SGreg Clayton
480a94b661SGreg Clayton    def __getattr__(self, name):
490a94b661SGreg Clayton        if name == 'name':
500a94b661SGreg Clayton            return self.sbvalue.GetName()
510a94b661SGreg Clayton        if name == 'type':
520a94b661SGreg Clayton            return self.sbvalue.GetType()
530a94b661SGreg Clayton        if name == 'type_name':
540a94b661SGreg Clayton            return self.sbvalue.GetTypeName()
550a94b661SGreg Clayton        if name == 'size':
560a94b661SGreg Clayton            return self.sbvalue.GetByteSize()
570a94b661SGreg Clayton        if name == 'is_in_scope':
580a94b661SGreg Clayton            return self.sbvalue.IsInScope()
590a94b661SGreg Clayton        if name == 'is_pointer':
600a94b661SGreg Clayton            return self.sbvalue.TypeIsPointerType()
610a94b661SGreg Clayton        if name == 'format':
620a94b661SGreg Clayton            return self.sbvalue.GetFormat()
630a94b661SGreg Clayton        if name == 'value':
640a94b661SGreg Clayton            return self.sbvalue.GetValue()
650a94b661SGreg Clayton        if name == 'summary':
660a94b661SGreg Clayton            return self.sbvalue.GetSummary()
670a94b661SGreg Clayton        if name == 'description':
680a94b661SGreg Clayton            return self.sbvalue.GetObjectDescription()
690a94b661SGreg Clayton        if name == 'location':
700a94b661SGreg Clayton            return self.sbvalue.GetLocation()
710a94b661SGreg Clayton        if name == 'target':
720a94b661SGreg Clayton            return self.sbvalue.GetTarget()
730a94b661SGreg Clayton        if name == 'process':
740a94b661SGreg Clayton            return self.sbvalue.GetProcess()
750a94b661SGreg Clayton        if name == 'thread':
760a94b661SGreg Clayton            return self.sbvalue.GetThread()
770a94b661SGreg Clayton        if name == 'frame':
780a94b661SGreg Clayton            return self.sbvalue.GetFrame()
790a94b661SGreg Clayton        if name == 'num_children':
800a94b661SGreg Clayton            return self.sbvalue.GetNumChildren()
810a94b661SGreg Clayton        if name == 'children':
820a94b661SGreg Clayton            # Returns an array of sbvalue objects, one for each child of
830a94b661SGreg Clayton            # the value for the lldb.SBValue
840a94b661SGreg Clayton            children = []
850a94b661SGreg Clayton            for i in range(self.sbvalue.GetNumChildren()):
86b9c1b51eSKate Stone                children.append(
87b9c1b51eSKate Stone                    value(
88b9c1b51eSKate Stone                        self.sbvalue.GetChildAtIndex(
89b9c1b51eSKate Stone                            i,
90b9c1b51eSKate Stone                            lldb.eNoDynamicValues,
91b9c1b51eSKate Stone                            True)))
920a94b661SGreg Clayton            return children
930a94b661SGreg Clayton        raise AttributeError
940a94b661SGreg Clayton
95b9c1b51eSKate Stone
960a94b661SGreg Claytonclass variable(object):
970a94b661SGreg Clayton    '''A class that treats a lldb.SBValue and allows it to be used just as
980a94b661SGreg Clayton    a variable would be in code. So if you have a Point structure variable
990a94b661SGreg Clayton    in your code, you would be able to do: "pt.x + pt.y"'''
100b9c1b51eSKate Stone
1010a94b661SGreg Clayton    def __init__(self, sbvalue):
1020a94b661SGreg Clayton        self.sbvalue = sbvalue
1030a94b661SGreg Clayton
1040a94b661SGreg Clayton    def __nonzero__(self):
1050a94b661SGreg Clayton        return self.sbvalue.__nonzero__()
1060a94b661SGreg Clayton
1070a94b661SGreg Clayton    def __repr__(self):
1080a94b661SGreg Clayton        return self.sbvalue.__repr__()
1090a94b661SGreg Clayton
1100a94b661SGreg Clayton    def __str__(self):
1110a94b661SGreg Clayton        return self.sbvalue.__str__()
1120a94b661SGreg Clayton
1130a94b661SGreg Clayton    def __getitem__(self, key):
1140a94b661SGreg Clayton        # Allow array access if this value has children...
115b9c1b51eSKate Stone        if isinstance(key, int):
116b9c1b51eSKate Stone            return variable(
117b9c1b51eSKate Stone                self.sbvalue.GetValueForExpressionPath(
118b9c1b51eSKate Stone                    "[%i]" %
119b9c1b51eSKate Stone                    key))
1200a94b661SGreg Clayton        raise TypeError
1210a94b661SGreg Clayton
1220a94b661SGreg Clayton    def __getattr__(self, name):
1230a94b661SGreg Clayton        child_sbvalue = self.sbvalue.GetChildMemberWithName(name)
1240a94b661SGreg Clayton        if child_sbvalue:
1250a94b661SGreg Clayton            return variable(child_sbvalue)
1260a94b661SGreg Clayton        raise AttributeError
1270a94b661SGreg Clayton
1280a94b661SGreg Clayton    def __add__(self, other):
1290a94b661SGreg Clayton        return int(self) + int(other)
1300a94b661SGreg Clayton
1310a94b661SGreg Clayton    def __sub__(self, other):
1320a94b661SGreg Clayton        return int(self) - int(other)
1330a94b661SGreg Clayton
1340a94b661SGreg Clayton    def __mul__(self, other):
1350a94b661SGreg Clayton        return int(self) * int(other)
1360a94b661SGreg Clayton
1370a94b661SGreg Clayton    def __floordiv__(self, other):
1380a94b661SGreg Clayton        return int(self) // int(other)
1390a94b661SGreg Clayton
1400a94b661SGreg Clayton    def __mod__(self, other):
1410a94b661SGreg Clayton        return int(self) % int(other)
1420a94b661SGreg Clayton
1430a94b661SGreg Clayton    def __divmod__(self, other):
1440a94b661SGreg Clayton        return int(self) % int(other)
1450a94b661SGreg Clayton
1460a94b661SGreg Clayton    def __pow__(self, other):
1470a94b661SGreg Clayton        return int(self) ** int(other)
1480a94b661SGreg Clayton
1490a94b661SGreg Clayton    def __lshift__(self, other):
1500a94b661SGreg Clayton        return int(self) << int(other)
1510a94b661SGreg Clayton
1520a94b661SGreg Clayton    def __rshift__(self, other):
1530a94b661SGreg Clayton        return int(self) >> int(other)
1540a94b661SGreg Clayton
1550a94b661SGreg Clayton    def __and__(self, other):
1560a94b661SGreg Clayton        return int(self) & int(other)
1570a94b661SGreg Clayton
1580a94b661SGreg Clayton    def __xor__(self, other):
1590a94b661SGreg Clayton        return int(self) ^ int(other)
1600a94b661SGreg Clayton
1610a94b661SGreg Clayton    def __or__(self, other):
1620a94b661SGreg Clayton        return int(self) | int(other)
1630a94b661SGreg Clayton
1640a94b661SGreg Clayton    def __div__(self, other):
1650a94b661SGreg Clayton        return int(self) / int(other)
1660a94b661SGreg Clayton
1670a94b661SGreg Clayton    def __truediv__(self, other):
1680a94b661SGreg Clayton        return int(self) / int(other)
1690a94b661SGreg Clayton
1700a94b661SGreg Clayton    def __iadd__(self, other):
1710a94b661SGreg Clayton        result = self.__add__(other)
1720a94b661SGreg Clayton        self.sbvalue.SetValueFromCString(str(result))
1730a94b661SGreg Clayton        return result
1740a94b661SGreg Clayton
1750a94b661SGreg Clayton    def __isub__(self, other):
1760a94b661SGreg Clayton        result = self.__sub__(other)
1770a94b661SGreg Clayton        self.sbvalue.SetValueFromCString(str(result))
1780a94b661SGreg Clayton        return result
1790a94b661SGreg Clayton
1800a94b661SGreg Clayton    def __imul__(self, other):
1810a94b661SGreg Clayton        result = self.__mul__(other)
1820a94b661SGreg Clayton        self.sbvalue.SetValueFromCString(str(result))
1830a94b661SGreg Clayton        return result
1840a94b661SGreg Clayton
1850a94b661SGreg Clayton    def __idiv__(self, other):
1860a94b661SGreg Clayton        result = self.__div__(other)
1870a94b661SGreg Clayton        self.sbvalue.SetValueFromCString(str(result))
1880a94b661SGreg Clayton        return result
1890a94b661SGreg Clayton
1900a94b661SGreg Clayton    def __itruediv__(self, other):
1910a94b661SGreg Clayton        result = self.__truediv__(other)
1920a94b661SGreg Clayton        self.sbvalue.SetValueFromCString(str(result))
1930a94b661SGreg Clayton        return result
1940a94b661SGreg Clayton
1950a94b661SGreg Clayton    def __ifloordiv__(self, other):
1960a94b661SGreg Clayton        result = self.__floordiv__(self, other)
1970a94b661SGreg Clayton        self.sbvalue.SetValueFromCString(str(result))
1980a94b661SGreg Clayton        return result
1990a94b661SGreg Clayton
2000a94b661SGreg Clayton    def __imod__(self, other):
2010a94b661SGreg Clayton        result = self.__and__(self, other)
2020a94b661SGreg Clayton        self.sbvalue.SetValueFromCString(str(result))
2030a94b661SGreg Clayton        return result
2040a94b661SGreg Clayton
2050a94b661SGreg Clayton    def __ipow__(self, other):
2060a94b661SGreg Clayton        result = self.__pow__(self, other)
2070a94b661SGreg Clayton        self.sbvalue.SetValueFromCString(str(result))
2080a94b661SGreg Clayton        return result
2090a94b661SGreg Clayton
2100a94b661SGreg Clayton    def __ipow__(self, other, modulo):
2110a94b661SGreg Clayton        result = self.__pow__(self, other, modulo)
2120a94b661SGreg Clayton        self.sbvalue.SetValueFromCString(str(result))
2130a94b661SGreg Clayton        return result
2140a94b661SGreg Clayton
2150a94b661SGreg Clayton    def __ilshift__(self, other):
2160a94b661SGreg Clayton        result = self.__lshift__(self, other)
2170a94b661SGreg Clayton        self.sbvalue.SetValueFromCString(str(result))
2180a94b661SGreg Clayton        return result
2190a94b661SGreg Clayton
2200a94b661SGreg Clayton    def __irshift__(self, other):
2210a94b661SGreg Clayton        result = self.__rshift__(self, other)
2220a94b661SGreg Clayton        self.sbvalue.SetValueFromCString(str(result))
2230a94b661SGreg Clayton        return result
2240a94b661SGreg Clayton
2250a94b661SGreg Clayton    def __iand__(self, other):
2260a94b661SGreg Clayton        result = self.__and__(self, other)
2270a94b661SGreg Clayton        self.sbvalue.SetValueFromCString(str(result))
2280a94b661SGreg Clayton        return result
2290a94b661SGreg Clayton
2300a94b661SGreg Clayton    def __ixor__(self, other):
2310a94b661SGreg Clayton        result = self.__xor__(self, other)
2320a94b661SGreg Clayton        self.sbvalue.SetValueFromCString(str(result))
2330a94b661SGreg Clayton        return result
2340a94b661SGreg Clayton
2350a94b661SGreg Clayton    def __ior__(self, other):
2360a94b661SGreg Clayton        result = self.__ior__(self, other)
2370a94b661SGreg Clayton        self.sbvalue.SetValueFromCString(str(result))
2380a94b661SGreg Clayton        return result
2390a94b661SGreg Clayton
2400a94b661SGreg Clayton    def __neg__(self):
2410a94b661SGreg Clayton        return -int(self)
2420a94b661SGreg Clayton
2430a94b661SGreg Clayton    def __pos__(self):
2440a94b661SGreg Clayton        return +int(self)
2450a94b661SGreg Clayton
2460a94b661SGreg Clayton    def __abs__(self):
2470a94b661SGreg Clayton        return abs(int(self))
2480a94b661SGreg Clayton
2490a94b661SGreg Clayton    def __invert__(self):
2500a94b661SGreg Clayton        return ~int(self)
2510a94b661SGreg Clayton
2520a94b661SGreg Clayton    def __complex__(self):
2530a94b661SGreg Clayton        return complex(int(self))
2540a94b661SGreg Clayton
2550a94b661SGreg Clayton    def __int__(self):
2560a94b661SGreg Clayton        return self.sbvalue.GetValueAsSigned()
2570a94b661SGreg Clayton
2580a94b661SGreg Clayton    def __long__(self):
2590a94b661SGreg Clayton        return self.sbvalue.GetValueAsSigned()
2600a94b661SGreg Clayton
2610a94b661SGreg Clayton    def __float__(self):
2620a94b661SGreg Clayton        return float(self.sbvalue.GetValueAsSigned())
2630a94b661SGreg Clayton
2640a94b661SGreg Clayton    def __oct__(self):
2650a94b661SGreg Clayton        return '0%o' % self.sbvalue.GetValueAsSigned()
2660a94b661SGreg Clayton
2670a94b661SGreg Clayton    def __hex__(self):
2680a94b661SGreg Clayton        return '0x%x' % self.sbvalue.GetValueAsSigned()
269