xref: /freebsd/contrib/llvm-project/lldb/bindings/python/python-extensions.swig (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
1%extend lldb::SBBreakpoint {
2    %pythoncode %{
3        def __eq__(self, rhs):
4            if not isinstance(rhs, type(self)):
5                return False
6
7            return getattr(_lldb,self.__class__.__name__+"___eq__")(self, rhs)
8
9        def __ne__(self, rhs):
10            if not isinstance(rhs, type(self)):
11                return True
12
13            return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
14    %}
15}
16
17%extend lldb::SBBroadcaster {
18    %pythoncode %{
19        def __eq__(self, rhs):
20            if not isinstance(rhs, type(self)):
21                return False
22
23            return getattr(_lldb,self.__class__.__name__+"___eq__")(self, rhs)
24
25        def __ne__(self, rhs):
26            if not isinstance(rhs, type(self)):
27                return True
28
29            return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
30    %}
31}
32
33%extend lldb::SBCommandReturnObject {
34        /* the write() and flush() calls are not part of the SB API proper, and are solely for Python usage
35        they are meant to make an SBCommandReturnObject into a file-like object so that instructions of the sort
36        print >>sb_command_return_object, "something"
37        will work correctly */
38
39        void lldb::SBCommandReturnObject::write (const char* str)
40        {
41            if (str)
42                $self->Printf("%s",str);
43        }
44        void lldb::SBCommandReturnObject::flush ()
45        {}
46}
47
48%extend lldb::SBCompileUnit {
49    %pythoncode %{
50        def __eq__(self, rhs):
51            if not isinstance(rhs, type(self)):
52                return False
53
54            return getattr(_lldb,self.__class__.__name__+"___eq__")(self, rhs)
55
56        def __ne__(self, rhs):
57            if not isinstance(rhs, type(self)):
58                return True
59
60            return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
61    %}
62}
63
64%extend lldb::SBDeclaration {
65    %pythoncode %{
66        def __eq__(self, rhs):
67            if not isinstance(rhs, type(self)):
68                return False
69
70            return getattr(_lldb,self.__class__.__name__+"___eq__")(self, rhs)
71
72        def __ne__(self, rhs):
73            if not isinstance(rhs, type(self)):
74                return True
75
76            return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
77    %}
78}
79
80%extend lldb::SBFunction {
81    %pythoncode %{
82        def __eq__(self, rhs):
83            if not isinstance(rhs, type(self)):
84                return False
85
86            return getattr(_lldb,self.__class__.__name__+"___eq__")(self, rhs)
87
88        def __ne__(self, rhs):
89            if not isinstance(rhs, type(self)):
90                return True
91
92            return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
93    %}
94}
95
96%extend lldb::SBLineEntry {
97    %pythoncode %{
98        def __eq__(self, rhs):
99            if not isinstance(rhs, type(self)):
100                return False
101
102            return getattr(_lldb,self.__class__.__name__+"___eq__")(self, rhs)
103
104        def __ne__(self, rhs):
105            if not isinstance(rhs, type(self)):
106                return True
107
108            return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
109    %}
110}
111
112%extend lldb::SBModule {
113    %pythoncode %{
114        def __eq__(self, rhs):
115            if not isinstance(rhs, type(self)):
116                return False
117
118            return getattr(_lldb,self.__class__.__name__+"___eq__")(self, rhs)
119
120        def __ne__(self, rhs):
121            if not isinstance(rhs, type(self)):
122                return True
123
124            return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
125    %}
126}
127
128%extend lldb::SBSection {
129    %pythoncode %{
130        def __eq__(self, rhs):
131            if not isinstance(rhs, type(self)):
132                return False
133
134            return getattr(_lldb,self.__class__.__name__+"___eq__")(self, rhs)
135
136        def __ne__(self, rhs):
137            if not isinstance(rhs, type(self)):
138                return True
139
140            return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
141    %}
142}
143%extend lldb::SBStream {
144        /* the write() and flush() calls are not part of the SB API proper, and are solely for Python usage
145        they are meant to make an SBStream into a file-like object so that instructions of the sort
146        print >>sb_stream, "something"
147        will work correctly */
148
149        void lldb::SBStream::write (const char* str)
150        {
151            if (str)
152                $self->Printf("%s",str);
153        }
154        void lldb::SBStream::flush ()
155        {}
156}
157%extend lldb::SBSymbol {
158    %pythoncode %{
159        def __eq__(self, rhs):
160            if not isinstance(rhs, type(self)):
161                return False
162
163            return getattr(_lldb,self.__class__.__name__+"___eq__")(self, rhs)
164
165        def __ne__(self, rhs):
166            if not isinstance(rhs, type(self)):
167                return True
168
169            return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
170    %}
171}
172
173%extend lldb::SBTarget {
174    %pythoncode %{
175        def __eq__(self, rhs):
176            if not isinstance(rhs, type(self)):
177                return False
178
179            return getattr(_lldb,self.__class__.__name__+"___eq__")(self, rhs)
180
181        def __ne__(self, rhs):
182            if not isinstance(rhs, type(self)):
183                return True
184
185            return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
186    %}
187}
188
189%extend lldb::SBTypeFilter {
190    %pythoncode %{
191        def __eq__(self, rhs):
192            if not isinstance(rhs, type(self)):
193                return False
194
195            return getattr(_lldb,self.__class__.__name__+"___eq__")(self, rhs)
196
197        def __ne__(self, rhs):
198            if not isinstance(rhs, type(self)):
199                return True
200
201            return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
202    %}
203}
204
205%extend lldb::SBTypeNameSpecifier {
206    %pythoncode %{
207        def __eq__(self, rhs):
208            if not isinstance(rhs, type(self)):
209                return False
210
211            return getattr(_lldb,self.__class__.__name__+"___eq__")(self, rhs)
212
213        def __ne__(self, rhs):
214            if not isinstance(rhs, type(self)):
215                return True
216
217            return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
218    %}
219}
220
221%extend lldb::SBTypeSummary {
222    %pythoncode %{
223        def __eq__(self, rhs):
224            if not isinstance(rhs, type(self)):
225                return False
226
227            return getattr(_lldb,self.__class__.__name__+"___eq__")(self, rhs)
228
229        def __ne__(self, rhs):
230            if not isinstance(rhs, type(self)):
231                return True
232
233            return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
234    %}
235}
236
237%extend lldb::SBTypeSynthetic {
238    %pythoncode %{
239        def __eq__(self, rhs):
240            if not isinstance(rhs, type(self)):
241                return False
242
243            return getattr(_lldb,self.__class__.__name__+"___eq__")(self, rhs)
244
245        def __ne__(self, rhs):
246            if not isinstance(rhs, type(self)):
247                return True
248
249            return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
250    %}
251}
252
253%extend lldb::SBThread {
254    %pythoncode %{
255        def __eq__(self, rhs):
256            if not isinstance(rhs, type(self)):
257                return False
258
259            return getattr(_lldb,self.__class__.__name__+"___eq__")(self, rhs)
260
261        def __ne__(self, rhs):
262            if not isinstance(rhs, type(self)):
263                return True
264
265            return getattr(_lldb,self.__class__.__name__+"___ne__")(self, rhs)
266    %}
267}
268
269%pythoncode %{
270
271def command(command_name=None, doc=None):
272    import lldb
273    """A decorator function that registers an LLDB command line
274        command that is bound to the function it is attached to."""
275    def callable(function):
276        """Registers an lldb command for the decorated function."""
277        command = "command script add -f %s.%s %s" % (function.__module__, function.__name__, command_name or function.__name__)
278        lldb.debugger.HandleCommand(command)
279        if doc:
280            function.__doc__ = doc
281        return function
282
283    return callable
284
285class declaration(object):
286    '''A class that represents a source declaration location with file, line and column.'''
287    def __init__(self, file, line, col):
288        self.file = file
289        self.line = line
290        self.col = col
291
292class value_iter(object):
293    '''Allows iterating over the children of an :py:class:`SBValue`.'''
294    def __iter__(self):
295        return self
296
297    def __next__(self):
298        if self.index >= self.length:
299            raise StopIteration()
300        child_sbvalue = self.sbvalue.GetChildAtIndex(self.index)
301        self.index += 1
302        return value(child_sbvalue)
303
304    def next(self):
305        return self.__next__()
306
307    def __init__(self,value):
308        self.index = 0
309        self.sbvalue = value
310        if type(self.sbvalue) is value:
311            self.sbvalue = self.sbvalue.sbvalue
312        self.length = self.sbvalue.GetNumChildren()
313
314class value(object):
315    '''Wraps :py:class:`SBValue` objects so the resulting object can be used as a variable would be in code.
316
317    So if you have a Point structure variable in your code in the current frame named "pt",
318    you can initialize an instance of this class with it: ::
319
320        pt = lldb.value(lldb.frame.FindVariable("pt"))
321        print pt
322        print pt.x
323        print pt.y
324
325        pt = lldb.value(lldb.frame.FindVariable("rectangle_array"))
326        print rectangle_array[12]
327        print rectangle_array[5].origin.x'''
328    def __init__(self, sbvalue):
329        self.sbvalue = sbvalue
330
331    def __nonzero__(self):
332        return self.sbvalue.__nonzero__()
333
334    def __bool__(self):
335        return self.sbvalue.__bool__()
336
337    def __str__(self):
338        return self.sbvalue.__str__()
339
340    def __getitem__(self, key):
341        # Allow array access if this value has children...
342        if type(key) is value:
343            key = int(key)
344        if type(key) is int:
345            child_sbvalue = (self.sbvalue.GetValueForExpressionPath("[%i]" % key))
346            if child_sbvalue and child_sbvalue.IsValid():
347                return value(child_sbvalue)
348            raise IndexError("Index '%d' is out of range" % key)
349        raise TypeError("No array item of type %s" % str(type(key)))
350
351    def __iter__(self):
352        return value_iter(self.sbvalue)
353
354    def __getattr__(self, name):
355        child_sbvalue = self.sbvalue.GetChildMemberWithName (name)
356        if child_sbvalue and child_sbvalue.IsValid():
357            return value(child_sbvalue)
358        raise AttributeError("Attribute '%s' is not defined" % name)
359
360    def __add__(self, other):
361        return int(self) + int(other)
362
363    def __sub__(self, other):
364        return int(self) - int(other)
365
366    def __mul__(self, other):
367        return int(self) * int(other)
368
369    def __floordiv__(self, other):
370        return int(self) // int(other)
371
372    def __mod__(self, other):
373        return int(self) % int(other)
374
375    def __divmod__(self, other):
376        return int(self) % int(other)
377
378    def __pow__(self, other):
379        return int(self) ** int(other)
380
381    def __lshift__(self, other):
382        return int(self) << int(other)
383
384    def __rshift__(self, other):
385        return int(self) >> int(other)
386
387    def __and__(self, other):
388        return int(self) & int(other)
389
390    def __xor__(self, other):
391        return int(self) ^ int(other)
392
393    def __or__(self, other):
394        return int(self) | int(other)
395
396    def __div__(self, other):
397        return int(self) / int(other)
398
399    def __truediv__(self, other):
400        return int(self) / int(other)
401
402    def __iadd__(self, other):
403        result = self.__add__(other)
404        self.sbvalue.SetValueFromCString (str(result))
405        return result
406
407    def __isub__(self, other):
408        result = self.__sub__(other)
409        self.sbvalue.SetValueFromCString (str(result))
410        return result
411
412    def __imul__(self, other):
413        result = self.__mul__(other)
414        self.sbvalue.SetValueFromCString (str(result))
415        return result
416
417    def __idiv__(self, other):
418        result = self.__div__(other)
419        self.sbvalue.SetValueFromCString (str(result))
420        return result
421
422    def __itruediv__(self, other):
423        result = self.__truediv__(other)
424        self.sbvalue.SetValueFromCString (str(result))
425        return result
426
427    def __ifloordiv__(self, other):
428        result =  self.__floordiv__(self, other)
429        self.sbvalue.SetValueFromCString (str(result))
430        return result
431
432    def __imod__(self, other):
433        result =  self.__and__(self, other)
434        self.sbvalue.SetValueFromCString (str(result))
435        return result
436
437    def __ipow__(self, other):
438        result = self.__pow__(self, other)
439        self.sbvalue.SetValueFromCString (str(result))
440        return result
441
442    def __ipow__(self, other, modulo):
443        result = self.__pow__(self, other, modulo)
444        self.sbvalue.SetValueFromCString (str(result))
445        return result
446
447    def __ilshift__(self, other):
448        result = self.__lshift__(other)
449        self.sbvalue.SetValueFromCString (str(result))
450        return result
451
452    def __irshift__(self, other):
453        result =  self.__rshift__(other)
454        self.sbvalue.SetValueFromCString (str(result))
455        return result
456
457    def __iand__(self, other):
458        result =  self.__and__(self, other)
459        self.sbvalue.SetValueFromCString (str(result))
460        return result
461
462    def __ixor__(self, other):
463        result =  self.__xor__(self, other)
464        self.sbvalue.SetValueFromCString (str(result))
465        return result
466
467    def __ior__(self, other):
468        result =  self.__ior__(self, other)
469        self.sbvalue.SetValueFromCString (str(result))
470        return result
471
472    def __neg__(self):
473        return -int(self)
474
475    def __pos__(self):
476        return +int(self)
477
478    def __abs__(self):
479        return abs(int(self))
480
481    def __invert__(self):
482        return ~int(self)
483
484    def __complex__(self):
485        return complex (int(self))
486
487    def __int__(self):
488        is_num,is_sign = is_numeric_type(self.sbvalue.GetType().GetCanonicalType().GetBasicType())
489        if is_num and not is_sign: return self.sbvalue.GetValueAsUnsigned()
490        return self.sbvalue.GetValueAsSigned()
491
492    def __long__(self):
493        return self.__int__()
494
495    def __float__(self):
496        return float (self.sbvalue.GetValueAsSigned())
497
498    def __oct__(self):
499        return '0%o' % self.sbvalue.GetValueAsUnsigned()
500
501    def __hex__(self):
502        return '0x%x' % self.sbvalue.GetValueAsUnsigned()
503
504    def __len__(self):
505        return self.sbvalue.GetNumChildren()
506
507    def __eq__(self, other):
508        if type(other) is int:
509                return int(self) == other
510        elif type(other) is str:
511                return str(self) == other
512        elif type(other) is value:
513                self_err = SBError()
514                other_err = SBError()
515                self_val = self.sbvalue.GetValueAsUnsigned(self_err)
516                if self_err.fail:
517                        raise ValueError("unable to extract value of self")
518                other_val = other.sbvalue.GetValueAsUnsigned(other_err)
519                if other_err.fail:
520                        raise ValueError("unable to extract value of other")
521                return self_val == other_val
522        raise TypeError("Unknown type %s, No equality operation defined." % str(type(other)))
523
524    def __ne__(self, other):
525        return not self.__eq__(other)
526%}
527
528%pythoncode %{
529
530class SBSyntheticValueProvider(object):
531    def __init__(self,valobj):
532        pass
533
534    def num_children(self):
535        return 0
536
537    def get_child_index(self,name):
538        return None
539
540    def get_child_at_index(self,idx):
541        return None
542
543    def update(self):
544        pass
545
546    def has_children(self):
547        return False
548
549
550%}
551
552%pythoncode %{
553
554# given an lldb.SBBasicType it returns a tuple
555# (is_numeric, is_signed)
556# the value of is_signed is undefined if is_numeric == false
557def is_numeric_type(basic_type):
558    if basic_type == eBasicTypeInvalid: return (False,False)
559    if basic_type == eBasicTypeVoid: return (False,False)
560    if basic_type == eBasicTypeChar: return (True,False)
561    if basic_type == eBasicTypeSignedChar: return (True,True)
562    if basic_type == eBasicTypeUnsignedChar: return (True,False)
563    if basic_type == eBasicTypeWChar: return (True,False)
564    if basic_type == eBasicTypeSignedWChar: return (True,True)
565    if basic_type == eBasicTypeUnsignedWChar: return (True,False)
566    if basic_type == eBasicTypeChar16: return (True,False)
567    if basic_type == eBasicTypeChar32: return (True,False)
568    if basic_type == eBasicTypeChar8: return (True,False)
569    if basic_type == eBasicTypeShort: return (True,True)
570    if basic_type == eBasicTypeUnsignedShort: return (True,False)
571    if basic_type == eBasicTypeInt: return (True,True)
572    if basic_type == eBasicTypeUnsignedInt: return (True,False)
573    if basic_type == eBasicTypeLong: return (True,True)
574    if basic_type == eBasicTypeUnsignedLong: return (True,False)
575    if basic_type == eBasicTypeLongLong: return (True,True)
576    if basic_type == eBasicTypeUnsignedLongLong: return (True,False)
577    if basic_type == eBasicTypeInt128: return (True,True)
578    if basic_type == eBasicTypeUnsignedInt128: return (True,False)
579    if basic_type == eBasicTypeBool: return (False,False)
580    if basic_type == eBasicTypeHalf: return (True,True)
581    if basic_type == eBasicTypeFloat: return (True,True)
582    if basic_type == eBasicTypeDouble: return (True,True)
583    if basic_type == eBasicTypeLongDouble: return (True,True)
584    if basic_type == eBasicTypeFloatComplex: return (True,True)
585    if basic_type == eBasicTypeDoubleComplex: return (True,True)
586    if basic_type == eBasicTypeLongDoubleComplex: return (True,True)
587    if basic_type == eBasicTypeObjCID: return (False,False)
588    if basic_type == eBasicTypeObjCClass: return (False,False)
589    if basic_type == eBasicTypeObjCSel: return (False,False)
590    if basic_type == eBasicTypeNullPtr: return (False,False)
591    #if basic_type == eBasicTypeOther:
592    return (False,False)
593
594%}
595