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