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