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 10 namespace lldb { 11 12 %feature("docstring", 13 "Represents a data buffer." 14 ) SBData; 15 class SBData 16 { 17 public: 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 bool 100 Append (const SBData& rhs); 101 102 static lldb::SBData 103 CreateDataFromCString (lldb::ByteOrder endian, uint32_t addr_byte_size, const char* data); 104 105 // in the following CreateData*() and SetData*() prototypes, the two parameters array and array_len 106 // should not be renamed or rearranged, because doing so will break the SWIG typemap 107 static lldb::SBData 108 CreateDataFromUInt64Array (lldb::ByteOrder endian, uint32_t addr_byte_size, uint64_t* array, size_t array_len); 109 110 static lldb::SBData 111 CreateDataFromUInt32Array (lldb::ByteOrder endian, uint32_t addr_byte_size, uint32_t* array, size_t array_len); 112 113 static lldb::SBData 114 CreateDataFromSInt64Array (lldb::ByteOrder endian, uint32_t addr_byte_size, int64_t* array, size_t array_len); 115 116 static lldb::SBData 117 CreateDataFromSInt32Array (lldb::ByteOrder endian, uint32_t addr_byte_size, int32_t* array, size_t array_len); 118 119 static lldb::SBData 120 CreateDataFromDoubleArray (lldb::ByteOrder endian, uint32_t addr_byte_size, double* array, size_t array_len); 121 122 bool 123 SetDataFromCString (const char* data); 124 125 bool 126 SetDataFromUInt64Array (uint64_t* array, size_t array_len); 127 128 bool 129 SetDataFromUInt32Array (uint32_t* array, size_t array_len); 130 131 bool 132 SetDataFromSInt64Array (int64_t* array, size_t array_len); 133 134 bool 135 SetDataFromSInt32Array (int32_t* array, size_t array_len); 136 137 bool 138 SetDataFromDoubleArray (double* array, size_t array_len); 139 140 STRING_EXTENSION(SBData) 141 142 #ifdef SWIGPYTHON 143 %pythoncode %{ 144 145 class read_data_helper: 146 def __init__(self, sbdata, readerfunc, item_size): 147 self.sbdata = sbdata 148 self.readerfunc = readerfunc 149 self.item_size = item_size 150 def __getitem__(self,key): 151 if isinstance(key,slice): 152 list = [] 153 for x in range(*key.indices(self.__len__())): 154 list.append(self.__getitem__(x)) 155 return list 156 if not (isinstance(key,six.integer_types)): 157 raise TypeError('must be int') 158 key = key * self.item_size # SBData uses byte-based indexes, but we want to use itemsize-based indexes here 159 error = SBError() 160 my_data = self.readerfunc(self.sbdata,error,key) 161 if error.Fail(): 162 raise IndexError(error.GetCString()) 163 else: 164 return my_data 165 def __len__(self): 166 return int(self.sbdata.GetByteSize()/self.item_size) 167 def all(self): 168 return self[0:len(self)] 169 170 @classmethod 171 def CreateDataFromInt (cls, value, size = None, target = None, ptr_size = None, endian = None): 172 import sys 173 lldbmodule = sys.modules[cls.__module__] 174 lldbdict = lldbmodule.__dict__ 175 if 'target' in lldbdict: 176 lldbtarget = lldbdict['target'] 177 else: 178 lldbtarget = None 179 if target == None and lldbtarget != None and lldbtarget.IsValid(): 180 target = lldbtarget 181 if ptr_size == None: 182 if target and target.IsValid(): 183 ptr_size = target.addr_size 184 else: 185 ptr_size = 8 186 if endian == None: 187 if target and target.IsValid(): 188 endian = target.byte_order 189 else: 190 endian = lldbdict['eByteOrderLittle'] 191 if size == None: 192 if value > 2147483647: 193 size = 8 194 elif value < -2147483648: 195 size = 8 196 elif value > 4294967295: 197 size = 8 198 else: 199 size = 4 200 if size == 4: 201 if value < 0: 202 return SBData().CreateDataFromSInt32Array(endian, ptr_size, [value]) 203 return SBData().CreateDataFromUInt32Array(endian, ptr_size, [value]) 204 if size == 8: 205 if value < 0: 206 return SBData().CreateDataFromSInt64Array(endian, ptr_size, [value]) 207 return SBData().CreateDataFromUInt64Array(endian, ptr_size, [value]) 208 return None 209 210 def _make_helper(self, sbdata, getfunc, itemsize): 211 return self.read_data_helper(sbdata, getfunc, itemsize) 212 213 def _make_helper_uint8(self): 214 return self._make_helper(self, SBData.GetUnsignedInt8, 1) 215 216 def _make_helper_uint16(self): 217 return self._make_helper(self, SBData.GetUnsignedInt16, 2) 218 219 def _make_helper_uint32(self): 220 return self._make_helper(self, SBData.GetUnsignedInt32, 4) 221 222 def _make_helper_uint64(self): 223 return self._make_helper(self, SBData.GetUnsignedInt64, 8) 224 225 def _make_helper_sint8(self): 226 return self._make_helper(self, SBData.GetSignedInt8, 1) 227 228 def _make_helper_sint16(self): 229 return self._make_helper(self, SBData.GetSignedInt16, 2) 230 231 def _make_helper_sint32(self): 232 return self._make_helper(self, SBData.GetSignedInt32, 4) 233 234 def _make_helper_sint64(self): 235 return self._make_helper(self, SBData.GetSignedInt64, 8) 236 237 def _make_helper_float(self): 238 return self._make_helper(self, SBData.GetFloat, 4) 239 240 def _make_helper_double(self): 241 return self._make_helper(self, SBData.GetDouble, 8) 242 243 def _read_all_uint8(self): 244 return self._make_helper_uint8().all() 245 246 def _read_all_uint16(self): 247 return self._make_helper_uint16().all() 248 249 def _read_all_uint32(self): 250 return self._make_helper_uint32().all() 251 252 def _read_all_uint64(self): 253 return self._make_helper_uint64().all() 254 255 def _read_all_sint8(self): 256 return self._make_helper_sint8().all() 257 258 def _read_all_sint16(self): 259 return self._make_helper_sint16().all() 260 261 def _read_all_sint32(self): 262 return self._make_helper_sint32().all() 263 264 def _read_all_sint64(self): 265 return self._make_helper_sint64().all() 266 267 def _read_all_float(self): 268 return self._make_helper_float().all() 269 270 def _read_all_double(self): 271 return self._make_helper_double().all() 272 273 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.''') 274 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.''') 275 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.''') 276 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.''') 277 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.''') 278 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.''') 279 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.''') 280 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.''') 281 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.''') 282 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.''') 283 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.''') 284 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.''') 285 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.''') 286 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.''') 287 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.''') 288 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.''') 289 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.''') 290 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.''') 291 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.''') 292 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.''') 293 byte_order = property(GetByteOrder, SetByteOrder, doc='''A read/write property getting and setting the endianness of this SBData (data.byte_order = lldb.eByteOrderLittle).''') 294 size = property(GetByteSize, None, doc='''A read only property that returns the size the same result as GetByteSize().''') 295 %} 296 #endif 297 298 }; 299 300 } // namespace lldb 301