xref: /freebsd/contrib/llvm-project/lldb/bindings/interface/SBDataExtensions.i (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
106c3fb27SDimitry Andric STRING_EXTENSION_OUTSIDE(SBData)
206c3fb27SDimitry Andric 
306c3fb27SDimitry Andric %extend lldb::SBData {
406c3fb27SDimitry Andric #ifdef SWIGPYTHON
506c3fb27SDimitry Andric     %pythoncode %{
65f757f3fSDimitry Andric         def __len__(self):
75f757f3fSDimitry Andric             return self.GetByteSize()
806c3fb27SDimitry Andric 
906c3fb27SDimitry Andric         class read_data_helper:
1006c3fb27SDimitry Andric             def __init__(self, sbdata, readerfunc, item_size):
1106c3fb27SDimitry Andric                 self.sbdata = sbdata
1206c3fb27SDimitry Andric                 self.readerfunc = readerfunc
1306c3fb27SDimitry Andric                 self.item_size = item_size
1406c3fb27SDimitry Andric             def __getitem__(self,key):
1506c3fb27SDimitry Andric                 if isinstance(key,slice):
1606c3fb27SDimitry Andric                     list = []
1706c3fb27SDimitry Andric                     for x in range(*key.indices(self.__len__())):
1806c3fb27SDimitry Andric                         list.append(self.__getitem__(x))
1906c3fb27SDimitry Andric                     return list
2006c3fb27SDimitry Andric                 if not (isinstance(key, int)):
2106c3fb27SDimitry Andric                     raise TypeError('must be int')
2206c3fb27SDimitry Andric                 key = key * self.item_size # SBData uses byte-based indexes, but we want to use itemsize-based indexes here
2306c3fb27SDimitry Andric                 error = SBError()
2406c3fb27SDimitry Andric                 my_data = self.readerfunc(self.sbdata,error,key)
2506c3fb27SDimitry Andric                 if error.Fail():
2606c3fb27SDimitry Andric                     raise IndexError(error.GetCString())
2706c3fb27SDimitry Andric                 else:
2806c3fb27SDimitry Andric                     return my_data
2906c3fb27SDimitry Andric             def __len__(self):
3006c3fb27SDimitry Andric                 return int(self.sbdata.GetByteSize()/self.item_size)
3106c3fb27SDimitry Andric             def all(self):
3206c3fb27SDimitry Andric                 return self[0:len(self)]
3306c3fb27SDimitry Andric 
3406c3fb27SDimitry Andric         @classmethod
3506c3fb27SDimitry Andric         def CreateDataFromInt (cls, value, size = None, target = None, ptr_size = None, endian = None):
3606c3fb27SDimitry Andric             import sys
3706c3fb27SDimitry Andric             lldbmodule = sys.modules[cls.__module__]
3806c3fb27SDimitry Andric             lldbdict = lldbmodule.__dict__
3906c3fb27SDimitry Andric             if 'target' in lldbdict:
4006c3fb27SDimitry Andric                 lldbtarget = lldbdict['target']
4106c3fb27SDimitry Andric             else:
4206c3fb27SDimitry Andric                 lldbtarget = None
43*0fca6ea1SDimitry Andric             if target is None and lldbtarget is not None and lldbtarget.IsValid():
4406c3fb27SDimitry Andric                 target = lldbtarget
45*0fca6ea1SDimitry Andric             if ptr_size is None:
4606c3fb27SDimitry Andric                 if target and target.IsValid():
4706c3fb27SDimitry Andric                     ptr_size = target.addr_size
4806c3fb27SDimitry Andric                 else:
4906c3fb27SDimitry Andric                     ptr_size = 8
50*0fca6ea1SDimitry Andric             if endian is None:
5106c3fb27SDimitry Andric                 if target and target.IsValid():
5206c3fb27SDimitry Andric                     endian = target.byte_order
5306c3fb27SDimitry Andric                 else:
5406c3fb27SDimitry Andric                     endian = lldbdict['eByteOrderLittle']
55*0fca6ea1SDimitry Andric             if size is None:
5606c3fb27SDimitry Andric                 if value > 2147483647:
5706c3fb27SDimitry Andric                     size = 8
5806c3fb27SDimitry Andric                 elif value < -2147483648:
5906c3fb27SDimitry Andric                     size = 8
6006c3fb27SDimitry Andric                 elif value > 4294967295:
6106c3fb27SDimitry Andric                     size = 8
6206c3fb27SDimitry Andric                 else:
6306c3fb27SDimitry Andric                     size = 4
6406c3fb27SDimitry Andric             if size == 4:
6506c3fb27SDimitry Andric                 if value < 0:
6606c3fb27SDimitry Andric                     return SBData().CreateDataFromSInt32Array(endian, ptr_size, [value])
6706c3fb27SDimitry Andric                 return SBData().CreateDataFromUInt32Array(endian, ptr_size, [value])
6806c3fb27SDimitry Andric             if size == 8:
6906c3fb27SDimitry Andric                 if value < 0:
7006c3fb27SDimitry Andric                     return SBData().CreateDataFromSInt64Array(endian, ptr_size, [value])
7106c3fb27SDimitry Andric                 return SBData().CreateDataFromUInt64Array(endian, ptr_size, [value])
7206c3fb27SDimitry Andric             return None
7306c3fb27SDimitry Andric 
7406c3fb27SDimitry Andric         def _make_helper(self, sbdata, getfunc, itemsize):
7506c3fb27SDimitry Andric             return self.read_data_helper(sbdata, getfunc, itemsize)
7606c3fb27SDimitry Andric 
7706c3fb27SDimitry Andric         def _make_helper_uint8(self):
7806c3fb27SDimitry Andric             return self._make_helper(self, SBData.GetUnsignedInt8, 1)
7906c3fb27SDimitry Andric 
8006c3fb27SDimitry Andric         def _make_helper_uint16(self):
8106c3fb27SDimitry Andric             return self._make_helper(self, SBData.GetUnsignedInt16, 2)
8206c3fb27SDimitry Andric 
8306c3fb27SDimitry Andric         def _make_helper_uint32(self):
8406c3fb27SDimitry Andric             return self._make_helper(self, SBData.GetUnsignedInt32, 4)
8506c3fb27SDimitry Andric 
8606c3fb27SDimitry Andric         def _make_helper_uint64(self):
8706c3fb27SDimitry Andric             return self._make_helper(self, SBData.GetUnsignedInt64, 8)
8806c3fb27SDimitry Andric 
8906c3fb27SDimitry Andric         def _make_helper_sint8(self):
9006c3fb27SDimitry Andric             return self._make_helper(self, SBData.GetSignedInt8, 1)
9106c3fb27SDimitry Andric 
9206c3fb27SDimitry Andric         def _make_helper_sint16(self):
9306c3fb27SDimitry Andric             return self._make_helper(self, SBData.GetSignedInt16, 2)
9406c3fb27SDimitry Andric 
9506c3fb27SDimitry Andric         def _make_helper_sint32(self):
9606c3fb27SDimitry Andric             return self._make_helper(self, SBData.GetSignedInt32, 4)
9706c3fb27SDimitry Andric 
9806c3fb27SDimitry Andric         def _make_helper_sint64(self):
9906c3fb27SDimitry Andric             return self._make_helper(self, SBData.GetSignedInt64, 8)
10006c3fb27SDimitry Andric 
10106c3fb27SDimitry Andric         def _make_helper_float(self):
10206c3fb27SDimitry Andric             return self._make_helper(self, SBData.GetFloat, 4)
10306c3fb27SDimitry Andric 
10406c3fb27SDimitry Andric         def _make_helper_double(self):
10506c3fb27SDimitry Andric             return self._make_helper(self, SBData.GetDouble, 8)
10606c3fb27SDimitry Andric 
10706c3fb27SDimitry Andric         def _read_all_uint8(self):
10806c3fb27SDimitry Andric             return self._make_helper_uint8().all()
10906c3fb27SDimitry Andric 
11006c3fb27SDimitry Andric         def _read_all_uint16(self):
11106c3fb27SDimitry Andric             return self._make_helper_uint16().all()
11206c3fb27SDimitry Andric 
11306c3fb27SDimitry Andric         def _read_all_uint32(self):
11406c3fb27SDimitry Andric             return self._make_helper_uint32().all()
11506c3fb27SDimitry Andric 
11606c3fb27SDimitry Andric         def _read_all_uint64(self):
11706c3fb27SDimitry Andric             return self._make_helper_uint64().all()
11806c3fb27SDimitry Andric 
11906c3fb27SDimitry Andric         def _read_all_sint8(self):
12006c3fb27SDimitry Andric             return self._make_helper_sint8().all()
12106c3fb27SDimitry Andric 
12206c3fb27SDimitry Andric         def _read_all_sint16(self):
12306c3fb27SDimitry Andric             return self._make_helper_sint16().all()
12406c3fb27SDimitry Andric 
12506c3fb27SDimitry Andric         def _read_all_sint32(self):
12606c3fb27SDimitry Andric             return self._make_helper_sint32().all()
12706c3fb27SDimitry Andric 
12806c3fb27SDimitry Andric         def _read_all_sint64(self):
12906c3fb27SDimitry Andric             return self._make_helper_sint64().all()
13006c3fb27SDimitry Andric 
13106c3fb27SDimitry Andric         def _read_all_float(self):
13206c3fb27SDimitry Andric             return self._make_helper_float().all()
13306c3fb27SDimitry Andric 
13406c3fb27SDimitry Andric         def _read_all_double(self):
13506c3fb27SDimitry Andric             return self._make_helper_double().all()
13606c3fb27SDimitry Andric 
13706c3fb27SDimitry Andric         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.''')
13806c3fb27SDimitry Andric         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.''')
13906c3fb27SDimitry Andric         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.''')
14006c3fb27SDimitry Andric         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.''')
14106c3fb27SDimitry Andric         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.''')
14206c3fb27SDimitry Andric         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.''')
14306c3fb27SDimitry Andric         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.''')
14406c3fb27SDimitry Andric         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.''')
14506c3fb27SDimitry Andric         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.''')
14606c3fb27SDimitry Andric         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.''')
14706c3fb27SDimitry Andric         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.''')
14806c3fb27SDimitry Andric         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.''')
14906c3fb27SDimitry Andric         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.''')
15006c3fb27SDimitry Andric         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.''')
15106c3fb27SDimitry Andric         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.''')
15206c3fb27SDimitry Andric         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.''')
15306c3fb27SDimitry Andric         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.''')
15406c3fb27SDimitry Andric         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.''')
15506c3fb27SDimitry Andric         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.''')
15606c3fb27SDimitry Andric         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.''')
15706c3fb27SDimitry Andric         byte_order = property(GetByteOrder, SetByteOrder, doc='''A read/write property getting and setting the endianness of this SBData (data.byte_order = lldb.eByteOrderLittle).''')
15806c3fb27SDimitry Andric         size = property(GetByteSize, None, doc='''A read only property that returns the size the same result as GetByteSize().''')
15906c3fb27SDimitry Andric     %}
16006c3fb27SDimitry Andric #endif
16106c3fb27SDimitry Andric }
162