1#!/usr/bin/env python
2
3import lldb
4
5
6class value(object):
7    '''A class that wraps an lldb.SBValue object and returns an object that
8    can be used as an object with attribytes:\n
9    argv = a.value(lldb.frame.FindVariable('argv'))\n
10    argv.name - return the name of the value that this object contains\n
11    argv.type - return the lldb.SBType for this value
12    argv.type_name - return the name of the type
13    argv.size - return the byte size of this value
14    argv.is_in_scope - return true if this value is currently in scope
15    argv.is_pointer - return true if this value is a pointer
16    argv.format - return the current format for this value
17    argv.value - return the value's value as a string
18    argv.summary - return a summary of this value's value
19    argv.description - return the runtime description for this value
20    argv.location - return a string that represents the values location (address, register, etc)
21    argv.target - return the lldb.SBTarget for this value
22    argv.process - return the lldb.SBProcess for this value
23    argv.thread - return the lldb.SBThread for this value
24    argv.frame - return the lldb.SBFrame for this value
25    argv.num_children - return the number of children this value has
26    argv.children - return a list of sbvalue objects that represents all of the children of this value
27    '''
28
29    def __init__(self, sbvalue):
30        self.sbvalue = sbvalue
31
32    def __nonzero__(self):
33        return self.sbvalue.__nonzero__()
34
35    def __repr__(self):
36        return self.sbvalue.__repr__()
37
38    def __str__(self):
39        return self.sbvalue.__str__()
40
41    def __getitem__(self, key):
42        if isinstance(key, int):
43            return value(
44                self.sbvalue.GetChildAtIndex(
45                    key, lldb.eNoDynamicValues, True))
46        raise TypeError
47
48    def __getattr__(self, name):
49        if name == 'name':
50            return self.sbvalue.GetName()
51        if name == 'type':
52            return self.sbvalue.GetType()
53        if name == 'type_name':
54            return self.sbvalue.GetTypeName()
55        if name == 'size':
56            return self.sbvalue.GetByteSize()
57        if name == 'is_in_scope':
58            return self.sbvalue.IsInScope()
59        if name == 'is_pointer':
60            return self.sbvalue.TypeIsPointerType()
61        if name == 'format':
62            return self.sbvalue.GetFormat()
63        if name == 'value':
64            return self.sbvalue.GetValue()
65        if name == 'summary':
66            return self.sbvalue.GetSummary()
67        if name == 'description':
68            return self.sbvalue.GetObjectDescription()
69        if name == 'location':
70            return self.sbvalue.GetLocation()
71        if name == 'target':
72            return self.sbvalue.GetTarget()
73        if name == 'process':
74            return self.sbvalue.GetProcess()
75        if name == 'thread':
76            return self.sbvalue.GetThread()
77        if name == 'frame':
78            return self.sbvalue.GetFrame()
79        if name == 'num_children':
80            return self.sbvalue.GetNumChildren()
81        if name == 'children':
82            # Returns an array of sbvalue objects, one for each child of
83            # the value for the lldb.SBValue
84            children = []
85            for i in range(self.sbvalue.GetNumChildren()):
86                children.append(
87                    value(
88                        self.sbvalue.GetChildAtIndex(
89                            i,
90                            lldb.eNoDynamicValues,
91                            True)))
92            return children
93        raise AttributeError
94
95
96class variable(object):
97    '''A class that treats a lldb.SBValue and allows it to be used just as
98    a variable would be in code. So if you have a Point structure variable
99    in your code, you would be able to do: "pt.x + pt.y"'''
100
101    def __init__(self, sbvalue):
102        self.sbvalue = sbvalue
103
104    def __nonzero__(self):
105        return self.sbvalue.__nonzero__()
106
107    def __repr__(self):
108        return self.sbvalue.__repr__()
109
110    def __str__(self):
111        return self.sbvalue.__str__()
112
113    def __getitem__(self, key):
114        # Allow array access if this value has children...
115        if isinstance(key, int):
116            return variable(
117                self.sbvalue.GetValueForExpressionPath(
118                    "[%i]" %
119                    key))
120        raise TypeError
121
122    def __getattr__(self, name):
123        child_sbvalue = self.sbvalue.GetChildMemberWithName(name)
124        if child_sbvalue:
125            return variable(child_sbvalue)
126        raise AttributeError
127
128    def __add__(self, other):
129        return int(self) + int(other)
130
131    def __sub__(self, other):
132        return int(self) - int(other)
133
134    def __mul__(self, other):
135        return int(self) * int(other)
136
137    def __floordiv__(self, other):
138        return int(self) // int(other)
139
140    def __mod__(self, other):
141        return int(self) % int(other)
142
143    def __divmod__(self, other):
144        return int(self) % int(other)
145
146    def __pow__(self, other):
147        return int(self) ** int(other)
148
149    def __lshift__(self, other):
150        return int(self) << int(other)
151
152    def __rshift__(self, other):
153        return int(self) >> int(other)
154
155    def __and__(self, other):
156        return int(self) & int(other)
157
158    def __xor__(self, other):
159        return int(self) ^ int(other)
160
161    def __or__(self, other):
162        return int(self) | int(other)
163
164    def __div__(self, other):
165        return int(self) / int(other)
166
167    def __truediv__(self, other):
168        return int(self) / int(other)
169
170    def __iadd__(self, other):
171        result = self.__add__(other)
172        self.sbvalue.SetValueFromCString(str(result))
173        return result
174
175    def __isub__(self, other):
176        result = self.__sub__(other)
177        self.sbvalue.SetValueFromCString(str(result))
178        return result
179
180    def __imul__(self, other):
181        result = self.__mul__(other)
182        self.sbvalue.SetValueFromCString(str(result))
183        return result
184
185    def __idiv__(self, other):
186        result = self.__div__(other)
187        self.sbvalue.SetValueFromCString(str(result))
188        return result
189
190    def __itruediv__(self, other):
191        result = self.__truediv__(other)
192        self.sbvalue.SetValueFromCString(str(result))
193        return result
194
195    def __ifloordiv__(self, other):
196        result = self.__floordiv__(self, other)
197        self.sbvalue.SetValueFromCString(str(result))
198        return result
199
200    def __imod__(self, other):
201        result = self.__and__(self, other)
202        self.sbvalue.SetValueFromCString(str(result))
203        return result
204
205    def __ipow__(self, other):
206        result = self.__pow__(self, other)
207        self.sbvalue.SetValueFromCString(str(result))
208        return result
209
210    def __ipow__(self, other, modulo):
211        result = self.__pow__(self, other, modulo)
212        self.sbvalue.SetValueFromCString(str(result))
213        return result
214
215    def __ilshift__(self, other):
216        result = self.__lshift__(self, other)
217        self.sbvalue.SetValueFromCString(str(result))
218        return result
219
220    def __irshift__(self, other):
221        result = self.__rshift__(self, other)
222        self.sbvalue.SetValueFromCString(str(result))
223        return result
224
225    def __iand__(self, other):
226        result = self.__and__(self, other)
227        self.sbvalue.SetValueFromCString(str(result))
228        return result
229
230    def __ixor__(self, other):
231        result = self.__xor__(self, other)
232        self.sbvalue.SetValueFromCString(str(result))
233        return result
234
235    def __ior__(self, other):
236        result = self.__ior__(self, other)
237        self.sbvalue.SetValueFromCString(str(result))
238        return result
239
240    def __neg__(self):
241        return -int(self)
242
243    def __pos__(self):
244        return +int(self)
245
246    def __abs__(self):
247        return abs(int(self))
248
249    def __invert__(self):
250        return ~int(self)
251
252    def __complex__(self):
253        return complex(int(self))
254
255    def __int__(self):
256        return self.sbvalue.GetValueAsSigned()
257
258    def __long__(self):
259        return self.sbvalue.GetValueAsSigned()
260
261    def __float__(self):
262        return float(self.sbvalue.GetValueAsSigned())
263
264    def __oct__(self):
265        return '0%o' % self.sbvalue.GetValueAsSigned()
266
267    def __hex__(self):
268        return '0x%x' % self.sbvalue.GetValueAsSigned()
269