xref: /openbsd-src/gnu/llvm/lldb/examples/python/sbvalue.py (revision be691f3bb6417f04a68938fadbcaee2d5795e764)
1*be691f3bSpatrick#!/usr/bin/env python
2061da546Spatrick
3061da546Spatrickimport lldb
4061da546Spatrick
5061da546Spatrick
6061da546Spatrickclass value(object):
7061da546Spatrick    '''A class that wraps an lldb.SBValue object and returns an object that
8061da546Spatrick    can be used as an object with attribytes:\n
9061da546Spatrick    argv = a.value(lldb.frame.FindVariable('argv'))\n
10061da546Spatrick    argv.name - return the name of the value that this object contains\n
11061da546Spatrick    argv.type - return the lldb.SBType for this value
12061da546Spatrick    argv.type_name - return the name of the type
13061da546Spatrick    argv.size - return the byte size of this value
14061da546Spatrick    argv.is_in_scope - return true if this value is currently in scope
15061da546Spatrick    argv.is_pointer - return true if this value is a pointer
16061da546Spatrick    argv.format - return the current format for this value
17061da546Spatrick    argv.value - return the value's value as a string
18061da546Spatrick    argv.summary - return a summary of this value's value
19061da546Spatrick    argv.description - return the runtime description for this value
20061da546Spatrick    argv.location - return a string that represents the values location (address, register, etc)
21061da546Spatrick    argv.target - return the lldb.SBTarget for this value
22061da546Spatrick    argv.process - return the lldb.SBProcess for this value
23061da546Spatrick    argv.thread - return the lldb.SBThread for this value
24061da546Spatrick    argv.frame - return the lldb.SBFrame for this value
25061da546Spatrick    argv.num_children - return the number of children this value has
26061da546Spatrick    argv.children - return a list of sbvalue objects that represents all of the children of this value
27061da546Spatrick    '''
28061da546Spatrick
29061da546Spatrick    def __init__(self, sbvalue):
30061da546Spatrick        self.sbvalue = sbvalue
31061da546Spatrick
32061da546Spatrick    def __nonzero__(self):
33061da546Spatrick        return self.sbvalue.__nonzero__()
34061da546Spatrick
35061da546Spatrick    def __repr__(self):
36061da546Spatrick        return self.sbvalue.__repr__()
37061da546Spatrick
38061da546Spatrick    def __str__(self):
39061da546Spatrick        return self.sbvalue.__str__()
40061da546Spatrick
41061da546Spatrick    def __getitem__(self, key):
42061da546Spatrick        if isinstance(key, int):
43061da546Spatrick            return value(
44061da546Spatrick                self.sbvalue.GetChildAtIndex(
45061da546Spatrick                    key, lldb.eNoDynamicValues, True))
46061da546Spatrick        raise TypeError
47061da546Spatrick
48061da546Spatrick    def __getattr__(self, name):
49061da546Spatrick        if name == 'name':
50061da546Spatrick            return self.sbvalue.GetName()
51061da546Spatrick        if name == 'type':
52061da546Spatrick            return self.sbvalue.GetType()
53061da546Spatrick        if name == 'type_name':
54061da546Spatrick            return self.sbvalue.GetTypeName()
55061da546Spatrick        if name == 'size':
56061da546Spatrick            return self.sbvalue.GetByteSize()
57061da546Spatrick        if name == 'is_in_scope':
58061da546Spatrick            return self.sbvalue.IsInScope()
59061da546Spatrick        if name == 'is_pointer':
60061da546Spatrick            return self.sbvalue.TypeIsPointerType()
61061da546Spatrick        if name == 'format':
62061da546Spatrick            return self.sbvalue.GetFormat()
63061da546Spatrick        if name == 'value':
64061da546Spatrick            return self.sbvalue.GetValue()
65061da546Spatrick        if name == 'summary':
66061da546Spatrick            return self.sbvalue.GetSummary()
67061da546Spatrick        if name == 'description':
68061da546Spatrick            return self.sbvalue.GetObjectDescription()
69061da546Spatrick        if name == 'location':
70061da546Spatrick            return self.sbvalue.GetLocation()
71061da546Spatrick        if name == 'target':
72061da546Spatrick            return self.sbvalue.GetTarget()
73061da546Spatrick        if name == 'process':
74061da546Spatrick            return self.sbvalue.GetProcess()
75061da546Spatrick        if name == 'thread':
76061da546Spatrick            return self.sbvalue.GetThread()
77061da546Spatrick        if name == 'frame':
78061da546Spatrick            return self.sbvalue.GetFrame()
79061da546Spatrick        if name == 'num_children':
80061da546Spatrick            return self.sbvalue.GetNumChildren()
81061da546Spatrick        if name == 'children':
82061da546Spatrick            # Returns an array of sbvalue objects, one for each child of
83061da546Spatrick            # the value for the lldb.SBValue
84061da546Spatrick            children = []
85061da546Spatrick            for i in range(self.sbvalue.GetNumChildren()):
86061da546Spatrick                children.append(
87061da546Spatrick                    value(
88061da546Spatrick                        self.sbvalue.GetChildAtIndex(
89061da546Spatrick                            i,
90061da546Spatrick                            lldb.eNoDynamicValues,
91061da546Spatrick                            True)))
92061da546Spatrick            return children
93061da546Spatrick        raise AttributeError
94061da546Spatrick
95061da546Spatrick
96061da546Spatrickclass variable(object):
97061da546Spatrick    '''A class that treats a lldb.SBValue and allows it to be used just as
98061da546Spatrick    a variable would be in code. So if you have a Point structure variable
99061da546Spatrick    in your code, you would be able to do: "pt.x + pt.y"'''
100061da546Spatrick
101061da546Spatrick    def __init__(self, sbvalue):
102061da546Spatrick        self.sbvalue = sbvalue
103061da546Spatrick
104061da546Spatrick    def __nonzero__(self):
105061da546Spatrick        return self.sbvalue.__nonzero__()
106061da546Spatrick
107061da546Spatrick    def __repr__(self):
108061da546Spatrick        return self.sbvalue.__repr__()
109061da546Spatrick
110061da546Spatrick    def __str__(self):
111061da546Spatrick        return self.sbvalue.__str__()
112061da546Spatrick
113061da546Spatrick    def __getitem__(self, key):
114061da546Spatrick        # Allow array access if this value has children...
115061da546Spatrick        if isinstance(key, int):
116061da546Spatrick            return variable(
117061da546Spatrick                self.sbvalue.GetValueForExpressionPath(
118061da546Spatrick                    "[%i]" %
119061da546Spatrick                    key))
120061da546Spatrick        raise TypeError
121061da546Spatrick
122061da546Spatrick    def __getattr__(self, name):
123061da546Spatrick        child_sbvalue = self.sbvalue.GetChildMemberWithName(name)
124061da546Spatrick        if child_sbvalue:
125061da546Spatrick            return variable(child_sbvalue)
126061da546Spatrick        raise AttributeError
127061da546Spatrick
128061da546Spatrick    def __add__(self, other):
129061da546Spatrick        return int(self) + int(other)
130061da546Spatrick
131061da546Spatrick    def __sub__(self, other):
132061da546Spatrick        return int(self) - int(other)
133061da546Spatrick
134061da546Spatrick    def __mul__(self, other):
135061da546Spatrick        return int(self) * int(other)
136061da546Spatrick
137061da546Spatrick    def __floordiv__(self, other):
138061da546Spatrick        return int(self) // int(other)
139061da546Spatrick
140061da546Spatrick    def __mod__(self, other):
141061da546Spatrick        return int(self) % int(other)
142061da546Spatrick
143061da546Spatrick    def __divmod__(self, other):
144061da546Spatrick        return int(self) % int(other)
145061da546Spatrick
146061da546Spatrick    def __pow__(self, other):
147061da546Spatrick        return int(self) ** int(other)
148061da546Spatrick
149061da546Spatrick    def __lshift__(self, other):
150061da546Spatrick        return int(self) << int(other)
151061da546Spatrick
152061da546Spatrick    def __rshift__(self, other):
153061da546Spatrick        return int(self) >> int(other)
154061da546Spatrick
155061da546Spatrick    def __and__(self, other):
156061da546Spatrick        return int(self) & int(other)
157061da546Spatrick
158061da546Spatrick    def __xor__(self, other):
159061da546Spatrick        return int(self) ^ int(other)
160061da546Spatrick
161061da546Spatrick    def __or__(self, other):
162061da546Spatrick        return int(self) | int(other)
163061da546Spatrick
164061da546Spatrick    def __div__(self, other):
165061da546Spatrick        return int(self) / int(other)
166061da546Spatrick
167061da546Spatrick    def __truediv__(self, other):
168061da546Spatrick        return int(self) / int(other)
169061da546Spatrick
170061da546Spatrick    def __iadd__(self, other):
171061da546Spatrick        result = self.__add__(other)
172061da546Spatrick        self.sbvalue.SetValueFromCString(str(result))
173061da546Spatrick        return result
174061da546Spatrick
175061da546Spatrick    def __isub__(self, other):
176061da546Spatrick        result = self.__sub__(other)
177061da546Spatrick        self.sbvalue.SetValueFromCString(str(result))
178061da546Spatrick        return result
179061da546Spatrick
180061da546Spatrick    def __imul__(self, other):
181061da546Spatrick        result = self.__mul__(other)
182061da546Spatrick        self.sbvalue.SetValueFromCString(str(result))
183061da546Spatrick        return result
184061da546Spatrick
185061da546Spatrick    def __idiv__(self, other):
186061da546Spatrick        result = self.__div__(other)
187061da546Spatrick        self.sbvalue.SetValueFromCString(str(result))
188061da546Spatrick        return result
189061da546Spatrick
190061da546Spatrick    def __itruediv__(self, other):
191061da546Spatrick        result = self.__truediv__(other)
192061da546Spatrick        self.sbvalue.SetValueFromCString(str(result))
193061da546Spatrick        return result
194061da546Spatrick
195061da546Spatrick    def __ifloordiv__(self, other):
196061da546Spatrick        result = self.__floordiv__(self, other)
197061da546Spatrick        self.sbvalue.SetValueFromCString(str(result))
198061da546Spatrick        return result
199061da546Spatrick
200061da546Spatrick    def __imod__(self, other):
201061da546Spatrick        result = self.__and__(self, other)
202061da546Spatrick        self.sbvalue.SetValueFromCString(str(result))
203061da546Spatrick        return result
204061da546Spatrick
205061da546Spatrick    def __ipow__(self, other):
206061da546Spatrick        result = self.__pow__(self, other)
207061da546Spatrick        self.sbvalue.SetValueFromCString(str(result))
208061da546Spatrick        return result
209061da546Spatrick
210061da546Spatrick    def __ipow__(self, other, modulo):
211061da546Spatrick        result = self.__pow__(self, other, modulo)
212061da546Spatrick        self.sbvalue.SetValueFromCString(str(result))
213061da546Spatrick        return result
214061da546Spatrick
215061da546Spatrick    def __ilshift__(self, other):
216061da546Spatrick        result = self.__lshift__(self, other)
217061da546Spatrick        self.sbvalue.SetValueFromCString(str(result))
218061da546Spatrick        return result
219061da546Spatrick
220061da546Spatrick    def __irshift__(self, other):
221061da546Spatrick        result = self.__rshift__(self, other)
222061da546Spatrick        self.sbvalue.SetValueFromCString(str(result))
223061da546Spatrick        return result
224061da546Spatrick
225061da546Spatrick    def __iand__(self, other):
226061da546Spatrick        result = self.__and__(self, other)
227061da546Spatrick        self.sbvalue.SetValueFromCString(str(result))
228061da546Spatrick        return result
229061da546Spatrick
230061da546Spatrick    def __ixor__(self, other):
231061da546Spatrick        result = self.__xor__(self, other)
232061da546Spatrick        self.sbvalue.SetValueFromCString(str(result))
233061da546Spatrick        return result
234061da546Spatrick
235061da546Spatrick    def __ior__(self, other):
236061da546Spatrick        result = self.__ior__(self, other)
237061da546Spatrick        self.sbvalue.SetValueFromCString(str(result))
238061da546Spatrick        return result
239061da546Spatrick
240061da546Spatrick    def __neg__(self):
241061da546Spatrick        return -int(self)
242061da546Spatrick
243061da546Spatrick    def __pos__(self):
244061da546Spatrick        return +int(self)
245061da546Spatrick
246061da546Spatrick    def __abs__(self):
247061da546Spatrick        return abs(int(self))
248061da546Spatrick
249061da546Spatrick    def __invert__(self):
250061da546Spatrick        return ~int(self)
251061da546Spatrick
252061da546Spatrick    def __complex__(self):
253061da546Spatrick        return complex(int(self))
254061da546Spatrick
255061da546Spatrick    def __int__(self):
256061da546Spatrick        return self.sbvalue.GetValueAsSigned()
257061da546Spatrick
258061da546Spatrick    def __long__(self):
259061da546Spatrick        return self.sbvalue.GetValueAsSigned()
260061da546Spatrick
261061da546Spatrick    def __float__(self):
262061da546Spatrick        return float(self.sbvalue.GetValueAsSigned())
263061da546Spatrick
264061da546Spatrick    def __oct__(self):
265061da546Spatrick        return '0%o' % self.sbvalue.GetValueAsSigned()
266061da546Spatrick
267061da546Spatrick    def __hex__(self):
268061da546Spatrick        return '0x%x' % self.sbvalue.GetValueAsSigned()
269