1//===-- SWIG Interface for SBData -------------------------------*- C++ -*-===// 2// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6// 7//===----------------------------------------------------------------------===// 8 9 10namespace lldb { 11 12%feature("docstring", 13"Represents a data buffer." 14) SBData; 15class SBData 16{ 17public: 18 19 SBData (); 20 21 SBData (const SBData &rhs); 22 23 ~SBData (); 24 25 uint8_t 26 GetAddressByteSize (); 27 28 void 29 SetAddressByteSize (uint8_t addr_byte_size); 30 31 void 32 Clear (); 33 34 bool 35 IsValid(); 36 37 explicit operator bool() const; 38 39 size_t 40 GetByteSize (); 41 42 lldb::ByteOrder 43 GetByteOrder(); 44 45 void 46 SetByteOrder (lldb::ByteOrder endian); 47 48 float 49 GetFloat (lldb::SBError& error, lldb::offset_t offset); 50 51 double 52 GetDouble (lldb::SBError& error, lldb::offset_t offset); 53 54 long double 55 GetLongDouble (lldb::SBError& error, lldb::offset_t offset); 56 57 lldb::addr_t 58 GetAddress (lldb::SBError& error, lldb::offset_t offset); 59 60 uint8_t 61 GetUnsignedInt8 (lldb::SBError& error, lldb::offset_t offset); 62 63 uint16_t 64 GetUnsignedInt16 (lldb::SBError& error, lldb::offset_t offset); 65 66 uint32_t 67 GetUnsignedInt32 (lldb::SBError& error, lldb::offset_t offset); 68 69 uint64_t 70 GetUnsignedInt64 (lldb::SBError& error, lldb::offset_t offset); 71 72 int8_t 73 GetSignedInt8 (lldb::SBError& error, lldb::offset_t offset); 74 75 int16_t 76 GetSignedInt16 (lldb::SBError& error, lldb::offset_t offset); 77 78 int32_t 79 GetSignedInt32 (lldb::SBError& error, lldb::offset_t offset); 80 81 int64_t 82 GetSignedInt64 (lldb::SBError& error, lldb::offset_t offset); 83 84 const char* 85 GetString (lldb::SBError& error, lldb::offset_t offset); 86 87 bool 88 GetDescription (lldb::SBStream &description, lldb::addr_t base_addr); 89 90 size_t 91 ReadRawData (lldb::SBError& error, 92 lldb::offset_t offset, 93 void *buf, 94 size_t size); 95 96 void 97 SetData (lldb::SBError& error, const void *buf, size_t size, lldb::ByteOrder endian, uint8_t addr_size); 98 99 void 100 SetDataWithOwnership (lldb::SBError& error, const void *buf, size_t size, 101 lldb::ByteOrder endian, uint8_t addr_size); 102 103 bool 104 Append (const SBData& rhs); 105 106 static lldb::SBData 107 CreateDataFromCString (lldb::ByteOrder endian, uint32_t addr_byte_size, const char* data); 108 109 // in the following CreateData*() and SetData*() prototypes, the two parameters array and array_len 110 // should not be renamed or rearranged, because doing so will break the SWIG typemap 111 static lldb::SBData 112 CreateDataFromUInt64Array (lldb::ByteOrder endian, uint32_t addr_byte_size, uint64_t* array, size_t array_len); 113 114 static lldb::SBData 115 CreateDataFromUInt32Array (lldb::ByteOrder endian, uint32_t addr_byte_size, uint32_t* array, size_t array_len); 116 117 static lldb::SBData 118 CreateDataFromSInt64Array (lldb::ByteOrder endian, uint32_t addr_byte_size, int64_t* array, size_t array_len); 119 120 static lldb::SBData 121 CreateDataFromSInt32Array (lldb::ByteOrder endian, uint32_t addr_byte_size, int32_t* array, size_t array_len); 122 123 static lldb::SBData 124 CreateDataFromDoubleArray (lldb::ByteOrder endian, uint32_t addr_byte_size, double* array, size_t array_len); 125 126 bool 127 SetDataFromCString (const char* data); 128 129 bool 130 SetDataFromUInt64Array (uint64_t* array, size_t array_len); 131 132 bool 133 SetDataFromUInt32Array (uint32_t* array, size_t array_len); 134 135 bool 136 SetDataFromSInt64Array (int64_t* array, size_t array_len); 137 138 bool 139 SetDataFromSInt32Array (int32_t* array, size_t array_len); 140 141 bool 142 SetDataFromDoubleArray (double* array, size_t array_len); 143 144 STRING_EXTENSION(SBData) 145 146#ifdef SWIGPYTHON 147 %pythoncode %{ 148 149 class read_data_helper: 150 def __init__(self, sbdata, readerfunc, item_size): 151 self.sbdata = sbdata 152 self.readerfunc = readerfunc 153 self.item_size = item_size 154 def __getitem__(self,key): 155 if isinstance(key,slice): 156 list = [] 157 for x in range(*key.indices(self.__len__())): 158 list.append(self.__getitem__(x)) 159 return list 160 if not (isinstance(key, int)): 161 raise TypeError('must be int') 162 key = key * self.item_size # SBData uses byte-based indexes, but we want to use itemsize-based indexes here 163 error = SBError() 164 my_data = self.readerfunc(self.sbdata,error,key) 165 if error.Fail(): 166 raise IndexError(error.GetCString()) 167 else: 168 return my_data 169 def __len__(self): 170 return int(self.sbdata.GetByteSize()/self.item_size) 171 def all(self): 172 return self[0:len(self)] 173 174 @classmethod 175 def CreateDataFromInt (cls, value, size = None, target = None, ptr_size = None, endian = None): 176 import sys 177 lldbmodule = sys.modules[cls.__module__] 178 lldbdict = lldbmodule.__dict__ 179 if 'target' in lldbdict: 180 lldbtarget = lldbdict['target'] 181 else: 182 lldbtarget = None 183 if target == None and lldbtarget != None and lldbtarget.IsValid(): 184 target = lldbtarget 185 if ptr_size == None: 186 if target and target.IsValid(): 187 ptr_size = target.addr_size 188 else: 189 ptr_size = 8 190 if endian == None: 191 if target and target.IsValid(): 192 endian = target.byte_order 193 else: 194 endian = lldbdict['eByteOrderLittle'] 195 if size == None: 196 if value > 2147483647: 197 size = 8 198 elif value < -2147483648: 199 size = 8 200 elif value > 4294967295: 201 size = 8 202 else: 203 size = 4 204 if size == 4: 205 if value < 0: 206 return SBData().CreateDataFromSInt32Array(endian, ptr_size, [value]) 207 return SBData().CreateDataFromUInt32Array(endian, ptr_size, [value]) 208 if size == 8: 209 if value < 0: 210 return SBData().CreateDataFromSInt64Array(endian, ptr_size, [value]) 211 return SBData().CreateDataFromUInt64Array(endian, ptr_size, [value]) 212 return None 213 214 def _make_helper(self, sbdata, getfunc, itemsize): 215 return self.read_data_helper(sbdata, getfunc, itemsize) 216 217 def _make_helper_uint8(self): 218 return self._make_helper(self, SBData.GetUnsignedInt8, 1) 219 220 def _make_helper_uint16(self): 221 return self._make_helper(self, SBData.GetUnsignedInt16, 2) 222 223 def _make_helper_uint32(self): 224 return self._make_helper(self, SBData.GetUnsignedInt32, 4) 225 226 def _make_helper_uint64(self): 227 return self._make_helper(self, SBData.GetUnsignedInt64, 8) 228 229 def _make_helper_sint8(self): 230 return self._make_helper(self, SBData.GetSignedInt8, 1) 231 232 def _make_helper_sint16(self): 233 return self._make_helper(self, SBData.GetSignedInt16, 2) 234 235 def _make_helper_sint32(self): 236 return self._make_helper(self, SBData.GetSignedInt32, 4) 237 238 def _make_helper_sint64(self): 239 return self._make_helper(self, SBData.GetSignedInt64, 8) 240 241 def _make_helper_float(self): 242 return self._make_helper(self, SBData.GetFloat, 4) 243 244 def _make_helper_double(self): 245 return self._make_helper(self, SBData.GetDouble, 8) 246 247 def _read_all_uint8(self): 248 return self._make_helper_uint8().all() 249 250 def _read_all_uint16(self): 251 return self._make_helper_uint16().all() 252 253 def _read_all_uint32(self): 254 return self._make_helper_uint32().all() 255 256 def _read_all_uint64(self): 257 return self._make_helper_uint64().all() 258 259 def _read_all_sint8(self): 260 return self._make_helper_sint8().all() 261 262 def _read_all_sint16(self): 263 return self._make_helper_sint16().all() 264 265 def _read_all_sint32(self): 266 return self._make_helper_sint32().all() 267 268 def _read_all_sint64(self): 269 return self._make_helper_sint64().all() 270 271 def _read_all_float(self): 272 return self._make_helper_float().all() 273 274 def _read_all_double(self): 275 return self._make_helper_double().all() 276 277 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.''') 278 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.''') 279 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.''') 280 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.''') 281 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.''') 282 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.''') 283 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.''') 284 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.''') 285 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.''') 286 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.''') 287 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.''') 288 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.''') 289 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.''') 290 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.''') 291 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.''') 292 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.''') 293 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.''') 294 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.''') 295 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.''') 296 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.''') 297 byte_order = property(GetByteOrder, SetByteOrder, doc='''A read/write property getting and setting the endianness of this SBData (data.byte_order = lldb.eByteOrderLittle).''') 298 size = property(GetByteSize, None, doc='''A read only property that returns the size the same result as GetByteSize().''') 299 %} 300#endif 301 302}; 303 304} // namespace lldb 305