xref: /freebsd/contrib/llvm-project/lldb/bindings/interface/SBDataExtensions.i (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 STRING_EXTENSION_OUTSIDE(SBData)
2 
3 %extend lldb::SBData {
4 #ifdef SWIGPYTHON
5     %pythoncode %{
6 
7         class read_data_helper:
8             def __init__(self, sbdata, readerfunc, item_size):
9                 self.sbdata = sbdata
10                 self.readerfunc = readerfunc
11                 self.item_size = item_size
12             def __getitem__(self,key):
13                 if isinstance(key,slice):
14                     list = []
15                     for x in range(*key.indices(self.__len__())):
16                         list.append(self.__getitem__(x))
17                     return list
18                 if not (isinstance(key, int)):
19                     raise TypeError('must be int')
20                 key = key * self.item_size # SBData uses byte-based indexes, but we want to use itemsize-based indexes here
21                 error = SBError()
22                 my_data = self.readerfunc(self.sbdata,error,key)
23                 if error.Fail():
24                     raise IndexError(error.GetCString())
25                 else:
26                     return my_data
27             def __len__(self):
28                 return int(self.sbdata.GetByteSize()/self.item_size)
29             def all(self):
30                 return self[0:len(self)]
31 
32         @classmethod
33         def CreateDataFromInt (cls, value, size = None, target = None, ptr_size = None, endian = None):
34             import sys
35             lldbmodule = sys.modules[cls.__module__]
36             lldbdict = lldbmodule.__dict__
37             if 'target' in lldbdict:
38                 lldbtarget = lldbdict['target']
39             else:
40                 lldbtarget = None
41             if target == None and lldbtarget != None and lldbtarget.IsValid():
42                 target = lldbtarget
43             if ptr_size == None:
44                 if target and target.IsValid():
45                     ptr_size = target.addr_size
46                 else:
47                     ptr_size = 8
48             if endian == None:
49                 if target and target.IsValid():
50                     endian = target.byte_order
51                 else:
52                     endian = lldbdict['eByteOrderLittle']
53             if size == None:
54                 if value > 2147483647:
55                     size = 8
56                 elif value < -2147483648:
57                     size = 8
58                 elif value > 4294967295:
59                     size = 8
60                 else:
61                     size = 4
62             if size == 4:
63                 if value < 0:
64                     return SBData().CreateDataFromSInt32Array(endian, ptr_size, [value])
65                 return SBData().CreateDataFromUInt32Array(endian, ptr_size, [value])
66             if size == 8:
67                 if value < 0:
68                     return SBData().CreateDataFromSInt64Array(endian, ptr_size, [value])
69                 return SBData().CreateDataFromUInt64Array(endian, ptr_size, [value])
70             return None
71 
72         def _make_helper(self, sbdata, getfunc, itemsize):
73             return self.read_data_helper(sbdata, getfunc, itemsize)
74 
75         def _make_helper_uint8(self):
76             return self._make_helper(self, SBData.GetUnsignedInt8, 1)
77 
78         def _make_helper_uint16(self):
79             return self._make_helper(self, SBData.GetUnsignedInt16, 2)
80 
81         def _make_helper_uint32(self):
82             return self._make_helper(self, SBData.GetUnsignedInt32, 4)
83 
84         def _make_helper_uint64(self):
85             return self._make_helper(self, SBData.GetUnsignedInt64, 8)
86 
87         def _make_helper_sint8(self):
88             return self._make_helper(self, SBData.GetSignedInt8, 1)
89 
90         def _make_helper_sint16(self):
91             return self._make_helper(self, SBData.GetSignedInt16, 2)
92 
93         def _make_helper_sint32(self):
94             return self._make_helper(self, SBData.GetSignedInt32, 4)
95 
96         def _make_helper_sint64(self):
97             return self._make_helper(self, SBData.GetSignedInt64, 8)
98 
99         def _make_helper_float(self):
100             return self._make_helper(self, SBData.GetFloat, 4)
101 
102         def _make_helper_double(self):
103             return self._make_helper(self, SBData.GetDouble, 8)
104 
105         def _read_all_uint8(self):
106             return self._make_helper_uint8().all()
107 
108         def _read_all_uint16(self):
109             return self._make_helper_uint16().all()
110 
111         def _read_all_uint32(self):
112             return self._make_helper_uint32().all()
113 
114         def _read_all_uint64(self):
115             return self._make_helper_uint64().all()
116 
117         def _read_all_sint8(self):
118             return self._make_helper_sint8().all()
119 
120         def _read_all_sint16(self):
121             return self._make_helper_sint16().all()
122 
123         def _read_all_sint32(self):
124             return self._make_helper_sint32().all()
125 
126         def _read_all_sint64(self):
127             return self._make_helper_sint64().all()
128 
129         def _read_all_float(self):
130             return self._make_helper_float().all()
131 
132         def _read_all_double(self):
133             return self._make_helper_double().all()
134 
135         uint8 = property(_make_helper_uint8, None, doc='''A read only property that returns an array-like object out of which you can read uint8 values.''')
136         uint16 = property(_make_helper_uint16, None, doc='''A read only property that returns an array-like object out of which you can read uint16 values.''')
137         uint32 = property(_make_helper_uint32, None, doc='''A read only property that returns an array-like object out of which you can read uint32 values.''')
138         uint64 = property(_make_helper_uint64, None, doc='''A read only property that returns an array-like object out of which you can read uint64 values.''')
139         sint8 = property(_make_helper_sint8, None, doc='''A read only property that returns an array-like object out of which you can read sint8 values.''')
140         sint16 = property(_make_helper_sint16, None, doc='''A read only property that returns an array-like object out of which you can read sint16 values.''')
141         sint32 = property(_make_helper_sint32, None, doc='''A read only property that returns an array-like object out of which you can read sint32 values.''')
142         sint64 = property(_make_helper_sint64, None, doc='''A read only property that returns an array-like object out of which you can read sint64 values.''')
143         float = property(_make_helper_float, None, doc='''A read only property that returns an array-like object out of which you can read float values.''')
144         double = property(_make_helper_double, None, doc='''A read only property that returns an array-like object out of which you can read double values.''')
145         uint8s = property(_read_all_uint8, None, doc='''A read only property that returns an array with all the contents of this SBData represented as uint8 values.''')
146         uint16s = property(_read_all_uint16, None, doc='''A read only property that returns an array with all the contents of this SBData represented as uint16 values.''')
147         uint32s = property(_read_all_uint32, None, doc='''A read only property that returns an array with all the contents of this SBData represented as uint32 values.''')
148         uint64s = property(_read_all_uint64, None, doc='''A read only property that returns an array with all the contents of this SBData represented as uint64 values.''')
149         sint8s = property(_read_all_sint8, None, doc='''A read only property that returns an array with all the contents of this SBData represented as sint8 values.''')
150         sint16s = property(_read_all_sint16, None, doc='''A read only property that returns an array with all the contents of this SBData represented as sint16 values.''')
151         sint32s = property(_read_all_sint32, None, doc='''A read only property that returns an array with all the contents of this SBData represented as sint32 values.''')
152         sint64s = property(_read_all_sint64, None, doc='''A read only property that returns an array with all the contents of this SBData represented as sint64 values.''')
153         floats = property(_read_all_float, None, doc='''A read only property that returns an array with all the contents of this SBData represented as float values.''')
154         doubles = property(_read_all_double, None, doc='''A read only property that returns an array with all the contents of this SBData represented as double values.''')
155         byte_order = property(GetByteOrder, SetByteOrder, doc='''A read/write property getting and setting the endianness of this SBData (data.byte_order = lldb.eByteOrderLittle).''')
156         size = property(GetByteSize, None, doc='''A read only property that returns the size the same result as GetByteSize().''')
157     %}
158 #endif
159 }
160