xref: /llvm-project/lldb/bindings/interface/SBDataExtensions.i (revision 586114510c5fa71d1377c7f53e68a3b12c472aa2)
1662548c8SAlex Langford STRING_EXTENSION_OUTSIDE(SBData)
2662548c8SAlex Langford 
3662548c8SAlex Langford %extend lldb::SBData {
4662548c8SAlex Langford #ifdef SWIGPYTHON
5662548c8SAlex Langford     %pythoncode %{
66813ef37SMed Ismail Bennani         def __len__(self):
76813ef37SMed Ismail Bennani             return self.GetByteSize()
8662548c8SAlex Langford 
9662548c8SAlex Langford         class read_data_helper:
10662548c8SAlex Langford             def __init__(self, sbdata, readerfunc, item_size):
11662548c8SAlex Langford                 self.sbdata = sbdata
12662548c8SAlex Langford                 self.readerfunc = readerfunc
13662548c8SAlex Langford                 self.item_size = item_size
14662548c8SAlex Langford             def __getitem__(self,key):
15662548c8SAlex Langford                 if isinstance(key,slice):
16662548c8SAlex Langford                     list = []
17662548c8SAlex Langford                     for x in range(*key.indices(self.__len__())):
18662548c8SAlex Langford                         list.append(self.__getitem__(x))
19662548c8SAlex Langford                     return list
20662548c8SAlex Langford                 if not (isinstance(key, int)):
21662548c8SAlex Langford                     raise TypeError('must be int')
22662548c8SAlex Langford                 key = key * self.item_size # SBData uses byte-based indexes, but we want to use itemsize-based indexes here
23662548c8SAlex Langford                 error = SBError()
24662548c8SAlex Langford                 my_data = self.readerfunc(self.sbdata,error,key)
25662548c8SAlex Langford                 if error.Fail():
26662548c8SAlex Langford                     raise IndexError(error.GetCString())
27662548c8SAlex Langford                 else:
28662548c8SAlex Langford                     return my_data
29662548c8SAlex Langford             def __len__(self):
30662548c8SAlex Langford                 return int(self.sbdata.GetByteSize()/self.item_size)
31662548c8SAlex Langford             def all(self):
32662548c8SAlex Langford                 return self[0:len(self)]
33662548c8SAlex Langford 
34662548c8SAlex Langford         @classmethod
35662548c8SAlex Langford         def CreateDataFromInt (cls, value, size = None, target = None, ptr_size = None, endian = None):
36662548c8SAlex Langford             import sys
37662548c8SAlex Langford             lldbmodule = sys.modules[cls.__module__]
38662548c8SAlex Langford             lldbdict = lldbmodule.__dict__
39662548c8SAlex Langford             if 'target' in lldbdict:
40662548c8SAlex Langford                 lldbtarget = lldbdict['target']
41662548c8SAlex Langford             else:
42662548c8SAlex Langford                 lldbtarget = None
43*58611451SEisuke Kawashima             if target is None and lldbtarget is not None and lldbtarget.IsValid():
44662548c8SAlex Langford                 target = lldbtarget
45*58611451SEisuke Kawashima             if ptr_size is None:
46662548c8SAlex Langford                 if target and target.IsValid():
47662548c8SAlex Langford                     ptr_size = target.addr_size
48662548c8SAlex Langford                 else:
49662548c8SAlex Langford                     ptr_size = 8
50*58611451SEisuke Kawashima             if endian is None:
51662548c8SAlex Langford                 if target and target.IsValid():
52662548c8SAlex Langford                     endian = target.byte_order
53662548c8SAlex Langford                 else:
54662548c8SAlex Langford                     endian = lldbdict['eByteOrderLittle']
55*58611451SEisuke Kawashima             if size is None:
56662548c8SAlex Langford                 if value > 2147483647:
57662548c8SAlex Langford                     size = 8
58662548c8SAlex Langford                 elif value < -2147483648:
59662548c8SAlex Langford                     size = 8
60662548c8SAlex Langford                 elif value > 4294967295:
61662548c8SAlex Langford                     size = 8
62662548c8SAlex Langford                 else:
63662548c8SAlex Langford                     size = 4
64662548c8SAlex Langford             if size == 4:
65662548c8SAlex Langford                 if value < 0:
66662548c8SAlex Langford                     return SBData().CreateDataFromSInt32Array(endian, ptr_size, [value])
67662548c8SAlex Langford                 return SBData().CreateDataFromUInt32Array(endian, ptr_size, [value])
68662548c8SAlex Langford             if size == 8:
69662548c8SAlex Langford                 if value < 0:
70662548c8SAlex Langford                     return SBData().CreateDataFromSInt64Array(endian, ptr_size, [value])
71662548c8SAlex Langford                 return SBData().CreateDataFromUInt64Array(endian, ptr_size, [value])
72662548c8SAlex Langford             return None
73662548c8SAlex Langford 
74662548c8SAlex Langford         def _make_helper(self, sbdata, getfunc, itemsize):
75662548c8SAlex Langford             return self.read_data_helper(sbdata, getfunc, itemsize)
76662548c8SAlex Langford 
77662548c8SAlex Langford         def _make_helper_uint8(self):
78662548c8SAlex Langford             return self._make_helper(self, SBData.GetUnsignedInt8, 1)
79662548c8SAlex Langford 
80662548c8SAlex Langford         def _make_helper_uint16(self):
81662548c8SAlex Langford             return self._make_helper(self, SBData.GetUnsignedInt16, 2)
82662548c8SAlex Langford 
83662548c8SAlex Langford         def _make_helper_uint32(self):
84662548c8SAlex Langford             return self._make_helper(self, SBData.GetUnsignedInt32, 4)
85662548c8SAlex Langford 
86662548c8SAlex Langford         def _make_helper_uint64(self):
87662548c8SAlex Langford             return self._make_helper(self, SBData.GetUnsignedInt64, 8)
88662548c8SAlex Langford 
89662548c8SAlex Langford         def _make_helper_sint8(self):
90662548c8SAlex Langford             return self._make_helper(self, SBData.GetSignedInt8, 1)
91662548c8SAlex Langford 
92662548c8SAlex Langford         def _make_helper_sint16(self):
93662548c8SAlex Langford             return self._make_helper(self, SBData.GetSignedInt16, 2)
94662548c8SAlex Langford 
95662548c8SAlex Langford         def _make_helper_sint32(self):
96662548c8SAlex Langford             return self._make_helper(self, SBData.GetSignedInt32, 4)
97662548c8SAlex Langford 
98662548c8SAlex Langford         def _make_helper_sint64(self):
99662548c8SAlex Langford             return self._make_helper(self, SBData.GetSignedInt64, 8)
100662548c8SAlex Langford 
101662548c8SAlex Langford         def _make_helper_float(self):
102662548c8SAlex Langford             return self._make_helper(self, SBData.GetFloat, 4)
103662548c8SAlex Langford 
104662548c8SAlex Langford         def _make_helper_double(self):
105662548c8SAlex Langford             return self._make_helper(self, SBData.GetDouble, 8)
106662548c8SAlex Langford 
107662548c8SAlex Langford         def _read_all_uint8(self):
108662548c8SAlex Langford             return self._make_helper_uint8().all()
109662548c8SAlex Langford 
110662548c8SAlex Langford         def _read_all_uint16(self):
111662548c8SAlex Langford             return self._make_helper_uint16().all()
112662548c8SAlex Langford 
113662548c8SAlex Langford         def _read_all_uint32(self):
114662548c8SAlex Langford             return self._make_helper_uint32().all()
115662548c8SAlex Langford 
116662548c8SAlex Langford         def _read_all_uint64(self):
117662548c8SAlex Langford             return self._make_helper_uint64().all()
118662548c8SAlex Langford 
119662548c8SAlex Langford         def _read_all_sint8(self):
120662548c8SAlex Langford             return self._make_helper_sint8().all()
121662548c8SAlex Langford 
122662548c8SAlex Langford         def _read_all_sint16(self):
123662548c8SAlex Langford             return self._make_helper_sint16().all()
124662548c8SAlex Langford 
125662548c8SAlex Langford         def _read_all_sint32(self):
126662548c8SAlex Langford             return self._make_helper_sint32().all()
127662548c8SAlex Langford 
128662548c8SAlex Langford         def _read_all_sint64(self):
129662548c8SAlex Langford             return self._make_helper_sint64().all()
130662548c8SAlex Langford 
131662548c8SAlex Langford         def _read_all_float(self):
132662548c8SAlex Langford             return self._make_helper_float().all()
133662548c8SAlex Langford 
134662548c8SAlex Langford         def _read_all_double(self):
135662548c8SAlex Langford             return self._make_helper_double().all()
136662548c8SAlex Langford 
137662548c8SAlex Langford         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.''')
138662548c8SAlex Langford         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.''')
139662548c8SAlex Langford         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.''')
140662548c8SAlex Langford         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.''')
141662548c8SAlex Langford         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.''')
142662548c8SAlex Langford         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.''')
143662548c8SAlex Langford         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.''')
144662548c8SAlex Langford         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.''')
145662548c8SAlex Langford         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.''')
146662548c8SAlex Langford         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.''')
147662548c8SAlex Langford         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.''')
148662548c8SAlex Langford         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.''')
149662548c8SAlex Langford         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.''')
150662548c8SAlex Langford         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.''')
151662548c8SAlex Langford         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.''')
152662548c8SAlex Langford         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.''')
153662548c8SAlex Langford         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.''')
154662548c8SAlex Langford         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.''')
155662548c8SAlex Langford         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.''')
156662548c8SAlex Langford         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.''')
157662548c8SAlex Langford         byte_order = property(GetByteOrder, SetByteOrder, doc='''A read/write property getting and setting the endianness of this SBData (data.byte_order = lldb.eByteOrderLittle).''')
158662548c8SAlex Langford         size = property(GetByteSize, None, doc='''A read only property that returns the size the same result as GetByteSize().''')
159662548c8SAlex Langford     %}
160662548c8SAlex Langford #endif
161662548c8SAlex Langford }
162