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