xref: /llvm-project/lldb/examples/python/sbvalue.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
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(self.sbvalue.GetChildAtIndex(key, lldb.eNoDynamicValues, True))
44        raise TypeError
45
46    def __getattr__(self, name):
47        if name == "name":
48            return self.sbvalue.GetName()
49        if name == "type":
50            return self.sbvalue.GetType()
51        if name == "type_name":
52            return self.sbvalue.GetTypeName()
53        if name == "size":
54            return self.sbvalue.GetByteSize()
55        if name == "is_in_scope":
56            return self.sbvalue.IsInScope()
57        if name == "is_pointer":
58            return self.sbvalue.TypeIsPointerType()
59        if name == "format":
60            return self.sbvalue.GetFormat()
61        if name == "value":
62            return self.sbvalue.GetValue()
63        if name == "summary":
64            return self.sbvalue.GetSummary()
65        if name == "description":
66            return self.sbvalue.GetObjectDescription()
67        if name == "location":
68            return self.sbvalue.GetLocation()
69        if name == "target":
70            return self.sbvalue.GetTarget()
71        if name == "process":
72            return self.sbvalue.GetProcess()
73        if name == "thread":
74            return self.sbvalue.GetThread()
75        if name == "frame":
76            return self.sbvalue.GetFrame()
77        if name == "num_children":
78            return self.sbvalue.GetNumChildren()
79        if name == "children":
80            # Returns an array of sbvalue objects, one for each child of
81            # the value for the lldb.SBValue
82            children = []
83            for i in range(self.sbvalue.GetNumChildren()):
84                children.append(
85                    value(self.sbvalue.GetChildAtIndex(i, lldb.eNoDynamicValues, True))
86                )
87            return children
88        raise AttributeError
89
90
91class variable(object):
92    '''A class that treats a lldb.SBValue and allows it to be used just as
93    a variable would be in code. So if you have a Point structure variable
94    in your code, you would be able to do: "pt.x + pt.y"'''
95
96    def __init__(self, sbvalue):
97        self.sbvalue = sbvalue
98
99    def __nonzero__(self):
100        return self.sbvalue.__nonzero__()
101
102    def __repr__(self):
103        return self.sbvalue.__repr__()
104
105    def __str__(self):
106        return self.sbvalue.__str__()
107
108    def __getitem__(self, key):
109        # Allow array access if this value has children...
110        if isinstance(key, int):
111            return variable(self.sbvalue.GetValueForExpressionPath("[%i]" % key))
112        raise TypeError
113
114    def __getattr__(self, name):
115        child_sbvalue = self.sbvalue.GetChildMemberWithName(name)
116        if child_sbvalue:
117            return variable(child_sbvalue)
118        raise AttributeError
119
120    def __add__(self, other):
121        return int(self) + int(other)
122
123    def __sub__(self, other):
124        return int(self) - int(other)
125
126    def __mul__(self, other):
127        return int(self) * int(other)
128
129    def __floordiv__(self, other):
130        return int(self) // int(other)
131
132    def __mod__(self, other):
133        return int(self) % int(other)
134
135    def __divmod__(self, other):
136        return int(self) % int(other)
137
138    def __pow__(self, other):
139        return int(self) ** int(other)
140
141    def __lshift__(self, other):
142        return int(self) << int(other)
143
144    def __rshift__(self, other):
145        return int(self) >> int(other)
146
147    def __and__(self, other):
148        return int(self) & int(other)
149
150    def __xor__(self, other):
151        return int(self) ^ int(other)
152
153    def __or__(self, other):
154        return int(self) | int(other)
155
156    def __div__(self, other):
157        return int(self) / int(other)
158
159    def __truediv__(self, other):
160        return int(self) / int(other)
161
162    def __iadd__(self, other):
163        result = self.__add__(other)
164        self.sbvalue.SetValueFromCString(str(result))
165        return result
166
167    def __isub__(self, other):
168        result = self.__sub__(other)
169        self.sbvalue.SetValueFromCString(str(result))
170        return result
171
172    def __imul__(self, other):
173        result = self.__mul__(other)
174        self.sbvalue.SetValueFromCString(str(result))
175        return result
176
177    def __idiv__(self, other):
178        result = self.__div__(other)
179        self.sbvalue.SetValueFromCString(str(result))
180        return result
181
182    def __itruediv__(self, other):
183        result = self.__truediv__(other)
184        self.sbvalue.SetValueFromCString(str(result))
185        return result
186
187    def __ifloordiv__(self, other):
188        result = self.__floordiv__(self, other)
189        self.sbvalue.SetValueFromCString(str(result))
190        return result
191
192    def __imod__(self, other):
193        result = self.__and__(self, other)
194        self.sbvalue.SetValueFromCString(str(result))
195        return result
196
197    def __ipow__(self, other):
198        result = self.__pow__(self, other)
199        self.sbvalue.SetValueFromCString(str(result))
200        return result
201
202    def __ipow__(self, other, modulo):
203        result = self.__pow__(self, other, modulo)
204        self.sbvalue.SetValueFromCString(str(result))
205        return result
206
207    def __ilshift__(self, other):
208        result = self.__lshift__(self, other)
209        self.sbvalue.SetValueFromCString(str(result))
210        return result
211
212    def __irshift__(self, other):
213        result = self.__rshift__(self, other)
214        self.sbvalue.SetValueFromCString(str(result))
215        return result
216
217    def __iand__(self, other):
218        result = self.__and__(self, other)
219        self.sbvalue.SetValueFromCString(str(result))
220        return result
221
222    def __ixor__(self, other):
223        result = self.__xor__(self, other)
224        self.sbvalue.SetValueFromCString(str(result))
225        return result
226
227    def __ior__(self, other):
228        result = self.__ior__(self, other)
229        self.sbvalue.SetValueFromCString(str(result))
230        return result
231
232    def __neg__(self):
233        return -int(self)
234
235    def __pos__(self):
236        return +int(self)
237
238    def __abs__(self):
239        return abs(int(self))
240
241    def __invert__(self):
242        return ~int(self)
243
244    def __complex__(self):
245        return complex(int(self))
246
247    def __int__(self):
248        return self.sbvalue.GetValueAsSigned()
249
250    def __long__(self):
251        return self.sbvalue.GetValueAsSigned()
252
253    def __float__(self):
254        return float(self.sbvalue.GetValueAsSigned())
255
256    def __oct__(self):
257        return "0%o" % self.sbvalue.GetValueAsSigned()
258
259    def __hex__(self):
260        return "0x%x" % self.sbvalue.GetValueAsSigned()
261