1cfca06d7SDimitry Andric //===-- SBData.cpp --------------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "lldb/API/SBData.h"
10f034231aSEd Maste #include "lldb/API/SBError.h"
11f034231aSEd Maste #include "lldb/API/SBStream.h"
126f8fc217SDimitry Andric #include "lldb/Utility/Instrumentation.h"
13f034231aSEd Maste
1474a628f7SDimitry Andric #include "lldb/Core/DumpDataExtractor.h"
1574a628f7SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
1674a628f7SDimitry Andric #include "lldb/Utility/DataExtractor.h"
1774a628f7SDimitry Andric #include "lldb/Utility/Stream.h"
18f034231aSEd Maste
195f29bb8aSDimitry Andric #include <cinttypes>
205f29bb8aSDimitry Andric #include <memory>
215f29bb8aSDimitry Andric
22f034231aSEd Maste using namespace lldb;
23f034231aSEd Maste using namespace lldb_private;
24f034231aSEd Maste
SBData()255f29bb8aSDimitry Andric SBData::SBData() : m_opaque_sp(new DataExtractor()) {
266f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
275f29bb8aSDimitry Andric }
28f034231aSEd Maste
SBData(const lldb::DataExtractorSP & data_sp)2914f1b3e8SDimitry Andric SBData::SBData(const lldb::DataExtractorSP &data_sp) : m_opaque_sp(data_sp) {}
30f034231aSEd Maste
SBData(const SBData & rhs)315f29bb8aSDimitry Andric SBData::SBData(const SBData &rhs) : m_opaque_sp(rhs.m_opaque_sp) {
326f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
335f29bb8aSDimitry Andric }
34f034231aSEd Maste
operator =(const SBData & rhs)3514f1b3e8SDimitry Andric const SBData &SBData::operator=(const SBData &rhs) {
366f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
375f29bb8aSDimitry Andric
38f034231aSEd Maste if (this != &rhs)
39f034231aSEd Maste m_opaque_sp = rhs.m_opaque_sp;
406f8fc217SDimitry Andric return *this;
41f034231aSEd Maste }
42f034231aSEd Maste
43cfca06d7SDimitry Andric SBData::~SBData() = default;
44f034231aSEd Maste
SetOpaque(const lldb::DataExtractorSP & data_sp)4514f1b3e8SDimitry Andric void SBData::SetOpaque(const lldb::DataExtractorSP &data_sp) {
46f034231aSEd Maste m_opaque_sp = data_sp;
47f034231aSEd Maste }
48f034231aSEd Maste
get() const4914f1b3e8SDimitry Andric lldb_private::DataExtractor *SBData::get() const { return m_opaque_sp.get(); }
50f034231aSEd Maste
operator ->() const5114f1b3e8SDimitry Andric lldb_private::DataExtractor *SBData::operator->() const {
52f034231aSEd Maste return m_opaque_sp.operator->();
53f034231aSEd Maste }
54f034231aSEd Maste
operator *()5514f1b3e8SDimitry Andric lldb::DataExtractorSP &SBData::operator*() { return m_opaque_sp; }
56f034231aSEd Maste
operator *() const5714f1b3e8SDimitry Andric const lldb::DataExtractorSP &SBData::operator*() const { return m_opaque_sp; }
58f034231aSEd Maste
IsValid()595f29bb8aSDimitry Andric bool SBData::IsValid() {
606f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
615f29bb8aSDimitry Andric return this->operator bool();
625f29bb8aSDimitry Andric }
operator bool() const635f29bb8aSDimitry Andric SBData::operator bool() const {
646f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
655f29bb8aSDimitry Andric
665f29bb8aSDimitry Andric return m_opaque_sp.get() != nullptr;
675f29bb8aSDimitry Andric }
68f034231aSEd Maste
GetAddressByteSize()6914f1b3e8SDimitry Andric uint8_t SBData::GetAddressByteSize() {
706f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
715f29bb8aSDimitry Andric
72f034231aSEd Maste uint8_t value = 0;
73f034231aSEd Maste if (m_opaque_sp.get())
74f034231aSEd Maste value = m_opaque_sp->GetAddressByteSize();
75f034231aSEd Maste return value;
76f034231aSEd Maste }
77f034231aSEd Maste
SetAddressByteSize(uint8_t addr_byte_size)7814f1b3e8SDimitry Andric void SBData::SetAddressByteSize(uint8_t addr_byte_size) {
796f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, addr_byte_size);
805f29bb8aSDimitry Andric
81f034231aSEd Maste if (m_opaque_sp.get())
82f034231aSEd Maste m_opaque_sp->SetAddressByteSize(addr_byte_size);
83f034231aSEd Maste }
84f034231aSEd Maste
Clear()8514f1b3e8SDimitry Andric void SBData::Clear() {
866f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
875f29bb8aSDimitry Andric
88f034231aSEd Maste if (m_opaque_sp.get())
89f034231aSEd Maste m_opaque_sp->Clear();
90f034231aSEd Maste }
91f034231aSEd Maste
GetByteSize()9214f1b3e8SDimitry Andric size_t SBData::GetByteSize() {
936f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
945f29bb8aSDimitry Andric
95f034231aSEd Maste size_t value = 0;
96f034231aSEd Maste if (m_opaque_sp.get())
97f034231aSEd Maste value = m_opaque_sp->GetByteSize();
98f034231aSEd Maste return value;
99f034231aSEd Maste }
100f034231aSEd Maste
GetByteOrder()10114f1b3e8SDimitry Andric lldb::ByteOrder SBData::GetByteOrder() {
1026f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1035f29bb8aSDimitry Andric
104f034231aSEd Maste lldb::ByteOrder value = eByteOrderInvalid;
105f034231aSEd Maste if (m_opaque_sp.get())
106f034231aSEd Maste value = m_opaque_sp->GetByteOrder();
107f034231aSEd Maste return value;
108f034231aSEd Maste }
109f034231aSEd Maste
SetByteOrder(lldb::ByteOrder endian)11014f1b3e8SDimitry Andric void SBData::SetByteOrder(lldb::ByteOrder endian) {
1116f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, endian);
1125f29bb8aSDimitry Andric
113f034231aSEd Maste if (m_opaque_sp.get())
114f034231aSEd Maste m_opaque_sp->SetByteOrder(endian);
115f034231aSEd Maste }
116f034231aSEd Maste
GetFloat(lldb::SBError & error,lldb::offset_t offset)11714f1b3e8SDimitry Andric float SBData::GetFloat(lldb::SBError &error, lldb::offset_t offset) {
1186f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, error, offset);
1195f29bb8aSDimitry Andric
120f034231aSEd Maste float value = 0;
12114f1b3e8SDimitry Andric if (!m_opaque_sp.get()) {
122f034231aSEd Maste error.SetErrorString("no value to read from");
12314f1b3e8SDimitry Andric } else {
124f034231aSEd Maste uint32_t old_offset = offset;
125f034231aSEd Maste value = m_opaque_sp->GetFloat(&offset);
126f034231aSEd Maste if (offset == old_offset)
127f034231aSEd Maste error.SetErrorString("unable to read data");
128f034231aSEd Maste }
129f034231aSEd Maste return value;
130f034231aSEd Maste }
131f034231aSEd Maste
GetDouble(lldb::SBError & error,lldb::offset_t offset)13214f1b3e8SDimitry Andric double SBData::GetDouble(lldb::SBError &error, lldb::offset_t offset) {
1336f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, error, offset);
1345f29bb8aSDimitry Andric
135f034231aSEd Maste double value = 0;
13614f1b3e8SDimitry Andric if (!m_opaque_sp.get()) {
137f034231aSEd Maste error.SetErrorString("no value to read from");
13814f1b3e8SDimitry Andric } else {
139f034231aSEd Maste uint32_t old_offset = offset;
140f034231aSEd Maste value = m_opaque_sp->GetDouble(&offset);
141f034231aSEd Maste if (offset == old_offset)
142f034231aSEd Maste error.SetErrorString("unable to read data");
143f034231aSEd Maste }
144f034231aSEd Maste return value;
145f034231aSEd Maste }
146f034231aSEd Maste
GetLongDouble(lldb::SBError & error,lldb::offset_t offset)14714f1b3e8SDimitry Andric long double SBData::GetLongDouble(lldb::SBError &error, lldb::offset_t offset) {
1486f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, error, offset);
1495f29bb8aSDimitry Andric
150f034231aSEd Maste long double value = 0;
15114f1b3e8SDimitry Andric if (!m_opaque_sp.get()) {
152f034231aSEd Maste error.SetErrorString("no value to read from");
15314f1b3e8SDimitry Andric } else {
154f034231aSEd Maste uint32_t old_offset = offset;
155f034231aSEd Maste value = m_opaque_sp->GetLongDouble(&offset);
156f034231aSEd Maste if (offset == old_offset)
157f034231aSEd Maste error.SetErrorString("unable to read data");
158f034231aSEd Maste }
159f034231aSEd Maste return value;
160f034231aSEd Maste }
161f034231aSEd Maste
GetAddress(lldb::SBError & error,lldb::offset_t offset)16214f1b3e8SDimitry Andric lldb::addr_t SBData::GetAddress(lldb::SBError &error, lldb::offset_t offset) {
1636f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, error, offset);
1645f29bb8aSDimitry Andric
165f034231aSEd Maste lldb::addr_t value = 0;
16614f1b3e8SDimitry Andric if (!m_opaque_sp.get()) {
167f034231aSEd Maste error.SetErrorString("no value to read from");
16814f1b3e8SDimitry Andric } else {
169f034231aSEd Maste uint32_t old_offset = offset;
170f034231aSEd Maste value = m_opaque_sp->GetAddress(&offset);
171f034231aSEd Maste if (offset == old_offset)
172f034231aSEd Maste error.SetErrorString("unable to read data");
173f034231aSEd Maste }
174f034231aSEd Maste return value;
175f034231aSEd Maste }
176f034231aSEd Maste
GetUnsignedInt8(lldb::SBError & error,lldb::offset_t offset)17714f1b3e8SDimitry Andric uint8_t SBData::GetUnsignedInt8(lldb::SBError &error, lldb::offset_t offset) {
1786f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, error, offset);
1795f29bb8aSDimitry Andric
180f034231aSEd Maste uint8_t value = 0;
18114f1b3e8SDimitry Andric if (!m_opaque_sp.get()) {
182f034231aSEd Maste error.SetErrorString("no value to read from");
18314f1b3e8SDimitry Andric } else {
184f034231aSEd Maste uint32_t old_offset = offset;
185f034231aSEd Maste value = m_opaque_sp->GetU8(&offset);
186f034231aSEd Maste if (offset == old_offset)
187f034231aSEd Maste error.SetErrorString("unable to read data");
188f034231aSEd Maste }
189f034231aSEd Maste return value;
190f034231aSEd Maste }
191f034231aSEd Maste
GetUnsignedInt16(lldb::SBError & error,lldb::offset_t offset)19214f1b3e8SDimitry Andric uint16_t SBData::GetUnsignedInt16(lldb::SBError &error, lldb::offset_t offset) {
1936f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, error, offset);
1945f29bb8aSDimitry Andric
195f034231aSEd Maste uint16_t value = 0;
19614f1b3e8SDimitry Andric if (!m_opaque_sp.get()) {
197f034231aSEd Maste error.SetErrorString("no value to read from");
19814f1b3e8SDimitry Andric } else {
199f034231aSEd Maste uint32_t old_offset = offset;
200f034231aSEd Maste value = m_opaque_sp->GetU16(&offset);
201f034231aSEd Maste if (offset == old_offset)
202f034231aSEd Maste error.SetErrorString("unable to read data");
203f034231aSEd Maste }
204f034231aSEd Maste return value;
205f034231aSEd Maste }
206f034231aSEd Maste
GetUnsignedInt32(lldb::SBError & error,lldb::offset_t offset)20714f1b3e8SDimitry Andric uint32_t SBData::GetUnsignedInt32(lldb::SBError &error, lldb::offset_t offset) {
2086f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, error, offset);
2095f29bb8aSDimitry Andric
210f034231aSEd Maste uint32_t value = 0;
21114f1b3e8SDimitry Andric if (!m_opaque_sp.get()) {
212f034231aSEd Maste error.SetErrorString("no value to read from");
21314f1b3e8SDimitry Andric } else {
214f034231aSEd Maste uint32_t old_offset = offset;
215f034231aSEd Maste value = m_opaque_sp->GetU32(&offset);
216f034231aSEd Maste if (offset == old_offset)
217f034231aSEd Maste error.SetErrorString("unable to read data");
218f034231aSEd Maste }
219f034231aSEd Maste return value;
220f034231aSEd Maste }
221f034231aSEd Maste
GetUnsignedInt64(lldb::SBError & error,lldb::offset_t offset)22214f1b3e8SDimitry Andric uint64_t SBData::GetUnsignedInt64(lldb::SBError &error, lldb::offset_t offset) {
2236f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, error, offset);
2245f29bb8aSDimitry Andric
225f034231aSEd Maste uint64_t value = 0;
22614f1b3e8SDimitry Andric if (!m_opaque_sp.get()) {
227f034231aSEd Maste error.SetErrorString("no value to read from");
22814f1b3e8SDimitry Andric } else {
229f034231aSEd Maste uint32_t old_offset = offset;
230f034231aSEd Maste value = m_opaque_sp->GetU64(&offset);
231f034231aSEd Maste if (offset == old_offset)
232f034231aSEd Maste error.SetErrorString("unable to read data");
233f034231aSEd Maste }
234f034231aSEd Maste return value;
235f034231aSEd Maste }
236f034231aSEd Maste
GetSignedInt8(lldb::SBError & error,lldb::offset_t offset)23714f1b3e8SDimitry Andric int8_t SBData::GetSignedInt8(lldb::SBError &error, lldb::offset_t offset) {
2386f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, error, offset);
2395f29bb8aSDimitry Andric
240f034231aSEd Maste int8_t value = 0;
24114f1b3e8SDimitry Andric if (!m_opaque_sp.get()) {
242f034231aSEd Maste error.SetErrorString("no value to read from");
24314f1b3e8SDimitry Andric } else {
244f034231aSEd Maste uint32_t old_offset = offset;
245f034231aSEd Maste value = (int8_t)m_opaque_sp->GetMaxS64(&offset, 1);
246f034231aSEd Maste if (offset == old_offset)
247f034231aSEd Maste error.SetErrorString("unable to read data");
248f034231aSEd Maste }
249f034231aSEd Maste return value;
250f034231aSEd Maste }
251f034231aSEd Maste
GetSignedInt16(lldb::SBError & error,lldb::offset_t offset)25214f1b3e8SDimitry Andric int16_t SBData::GetSignedInt16(lldb::SBError &error, lldb::offset_t offset) {
2536f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, error, offset);
2545f29bb8aSDimitry Andric
255f034231aSEd Maste int16_t value = 0;
25614f1b3e8SDimitry Andric if (!m_opaque_sp.get()) {
257f034231aSEd Maste error.SetErrorString("no value to read from");
25814f1b3e8SDimitry Andric } else {
259f034231aSEd Maste uint32_t old_offset = offset;
260f034231aSEd Maste value = (int16_t)m_opaque_sp->GetMaxS64(&offset, 2);
261f034231aSEd Maste if (offset == old_offset)
262f034231aSEd Maste error.SetErrorString("unable to read data");
263f034231aSEd Maste }
264f034231aSEd Maste return value;
265f034231aSEd Maste }
266f034231aSEd Maste
GetSignedInt32(lldb::SBError & error,lldb::offset_t offset)26714f1b3e8SDimitry Andric int32_t SBData::GetSignedInt32(lldb::SBError &error, lldb::offset_t offset) {
2686f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, error, offset);
2695f29bb8aSDimitry Andric
270f034231aSEd Maste int32_t value = 0;
27114f1b3e8SDimitry Andric if (!m_opaque_sp.get()) {
272f034231aSEd Maste error.SetErrorString("no value to read from");
27314f1b3e8SDimitry Andric } else {
274f034231aSEd Maste uint32_t old_offset = offset;
275f034231aSEd Maste value = (int32_t)m_opaque_sp->GetMaxS64(&offset, 4);
276f034231aSEd Maste if (offset == old_offset)
277f034231aSEd Maste error.SetErrorString("unable to read data");
278f034231aSEd Maste }
279f034231aSEd Maste return value;
280f034231aSEd Maste }
281f034231aSEd Maste
GetSignedInt64(lldb::SBError & error,lldb::offset_t offset)28214f1b3e8SDimitry Andric int64_t SBData::GetSignedInt64(lldb::SBError &error, lldb::offset_t offset) {
2836f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, error, offset);
2845f29bb8aSDimitry Andric
285f034231aSEd Maste int64_t value = 0;
28614f1b3e8SDimitry Andric if (!m_opaque_sp.get()) {
287f034231aSEd Maste error.SetErrorString("no value to read from");
28814f1b3e8SDimitry Andric } else {
289f034231aSEd Maste uint32_t old_offset = offset;
290f034231aSEd Maste value = (int64_t)m_opaque_sp->GetMaxS64(&offset, 8);
291f034231aSEd Maste if (offset == old_offset)
292f034231aSEd Maste error.SetErrorString("unable to read data");
293f034231aSEd Maste }
294f034231aSEd Maste return value;
295f034231aSEd Maste }
296f034231aSEd Maste
GetString(lldb::SBError & error,lldb::offset_t offset)29714f1b3e8SDimitry Andric const char *SBData::GetString(lldb::SBError &error, lldb::offset_t offset) {
2986f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, error, offset);
2995f29bb8aSDimitry Andric
3007fa27ce4SDimitry Andric if (!m_opaque_sp) {
301f034231aSEd Maste error.SetErrorString("no value to read from");
3027fa27ce4SDimitry Andric return nullptr;
303f034231aSEd Maste }
3047fa27ce4SDimitry Andric
3057fa27ce4SDimitry Andric lldb::offset_t old_offset = offset;
3067fa27ce4SDimitry Andric const char *value = m_opaque_sp->GetCStr(&offset);
3077fa27ce4SDimitry Andric if (offset == old_offset || value == nullptr) {
3087fa27ce4SDimitry Andric error.SetErrorString("unable to read data");
3097fa27ce4SDimitry Andric return nullptr;
3107fa27ce4SDimitry Andric }
3117fa27ce4SDimitry Andric
3127fa27ce4SDimitry Andric return ConstString(value).GetCString();
313f034231aSEd Maste }
314f034231aSEd Maste
GetDescription(lldb::SBStream & description,lldb::addr_t base_addr)31514f1b3e8SDimitry Andric bool SBData::GetDescription(lldb::SBStream &description,
31614f1b3e8SDimitry Andric lldb::addr_t base_addr) {
3176f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, description, base_addr);
3185f29bb8aSDimitry Andric
319f034231aSEd Maste Stream &strm = description.ref();
320f034231aSEd Maste
32114f1b3e8SDimitry Andric if (m_opaque_sp) {
32274a628f7SDimitry Andric DumpDataExtractor(*m_opaque_sp, &strm, 0, lldb::eFormatBytesWithASCII, 1,
32314f1b3e8SDimitry Andric m_opaque_sp->GetByteSize(), 16, base_addr, 0, 0);
32414f1b3e8SDimitry Andric } else
325f034231aSEd Maste strm.PutCString("No value");
326f034231aSEd Maste
327f034231aSEd Maste return true;
328f034231aSEd Maste }
329f034231aSEd Maste
ReadRawData(lldb::SBError & error,lldb::offset_t offset,void * buf,size_t size)33014f1b3e8SDimitry Andric size_t SBData::ReadRawData(lldb::SBError &error, lldb::offset_t offset,
33114f1b3e8SDimitry Andric void *buf, size_t size) {
3326f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, error, offset, buf, size);
3335f29bb8aSDimitry Andric
3345f29bb8aSDimitry Andric void *ok = nullptr;
33514f1b3e8SDimitry Andric if (!m_opaque_sp.get()) {
336f034231aSEd Maste error.SetErrorString("no value to read from");
33714f1b3e8SDimitry Andric } else {
338f034231aSEd Maste uint32_t old_offset = offset;
339f034231aSEd Maste ok = m_opaque_sp->GetU8(&offset, buf, size);
3405f29bb8aSDimitry Andric if ((offset == old_offset) || (ok == nullptr))
341f034231aSEd Maste error.SetErrorString("unable to read data");
342f034231aSEd Maste }
343f034231aSEd Maste return ok ? size : 0;
344f034231aSEd Maste }
345f034231aSEd Maste
SetData(lldb::SBError & error,const void * buf,size_t size,lldb::ByteOrder endian,uint8_t addr_size)34614f1b3e8SDimitry Andric void SBData::SetData(lldb::SBError &error, const void *buf, size_t size,
34714f1b3e8SDimitry Andric lldb::ByteOrder endian, uint8_t addr_size) {
3486f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, error, buf, size, endian, addr_size);
3495f29bb8aSDimitry Andric
350f034231aSEd Maste if (!m_opaque_sp.get())
3515f29bb8aSDimitry Andric m_opaque_sp = std::make_shared<DataExtractor>(buf, size, endian, addr_size);
352f034231aSEd Maste else
35374a628f7SDimitry Andric {
354f034231aSEd Maste m_opaque_sp->SetData(buf, size, endian);
35574a628f7SDimitry Andric m_opaque_sp->SetAddressByteSize(addr_size);
35674a628f7SDimitry Andric }
357f034231aSEd Maste }
358f034231aSEd Maste
SetDataWithOwnership(lldb::SBError & error,const void * buf,size_t size,lldb::ByteOrder endian,uint8_t addr_size)35977fc4c14SDimitry Andric void SBData::SetDataWithOwnership(lldb::SBError &error, const void *buf,
36077fc4c14SDimitry Andric size_t size, lldb::ByteOrder endian,
36177fc4c14SDimitry Andric uint8_t addr_size) {
3626f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, error, buf, size, endian, addr_size);
36377fc4c14SDimitry Andric
36477fc4c14SDimitry Andric lldb::DataBufferSP buffer_sp = std::make_shared<DataBufferHeap>(buf, size);
36577fc4c14SDimitry Andric
36677fc4c14SDimitry Andric if (!m_opaque_sp.get())
36777fc4c14SDimitry Andric m_opaque_sp = std::make_shared<DataExtractor>(buf, size, endian, addr_size);
36877fc4c14SDimitry Andric else {
36977fc4c14SDimitry Andric m_opaque_sp->SetData(buffer_sp);
37077fc4c14SDimitry Andric m_opaque_sp->SetByteOrder(endian);
37177fc4c14SDimitry Andric m_opaque_sp->SetAddressByteSize(addr_size);
37277fc4c14SDimitry Andric }
37377fc4c14SDimitry Andric }
37477fc4c14SDimitry Andric
Append(const SBData & rhs)37514f1b3e8SDimitry Andric bool SBData::Append(const SBData &rhs) {
3766f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
3775f29bb8aSDimitry Andric
378f034231aSEd Maste bool value = false;
379f034231aSEd Maste if (m_opaque_sp.get() && rhs.m_opaque_sp.get())
380f034231aSEd Maste value = m_opaque_sp.get()->Append(*rhs.m_opaque_sp);
381f034231aSEd Maste return value;
382f034231aSEd Maste }
383f034231aSEd Maste
CreateDataFromCString(lldb::ByteOrder endian,uint32_t addr_byte_size,const char * data)38414f1b3e8SDimitry Andric lldb::SBData SBData::CreateDataFromCString(lldb::ByteOrder endian,
38514f1b3e8SDimitry Andric uint32_t addr_byte_size,
38614f1b3e8SDimitry Andric const char *data) {
3876f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(endian, addr_byte_size, data);
3885f29bb8aSDimitry Andric
389f034231aSEd Maste if (!data || !data[0])
3906f8fc217SDimitry Andric return SBData();
391f034231aSEd Maste
392f034231aSEd Maste uint32_t data_len = strlen(data);
393f034231aSEd Maste
394f034231aSEd Maste lldb::DataBufferSP buffer_sp(new DataBufferHeap(data, data_len));
39514f1b3e8SDimitry Andric lldb::DataExtractorSP data_sp(
39614f1b3e8SDimitry Andric new DataExtractor(buffer_sp, endian, addr_byte_size));
397f034231aSEd Maste
398f034231aSEd Maste SBData ret(data_sp);
399f034231aSEd Maste
4006f8fc217SDimitry Andric return ret;
401f034231aSEd Maste }
402f034231aSEd Maste
CreateDataFromUInt64Array(lldb::ByteOrder endian,uint32_t addr_byte_size,uint64_t * array,size_t array_len)40314f1b3e8SDimitry Andric lldb::SBData SBData::CreateDataFromUInt64Array(lldb::ByteOrder endian,
40414f1b3e8SDimitry Andric uint32_t addr_byte_size,
40514f1b3e8SDimitry Andric uint64_t *array,
40614f1b3e8SDimitry Andric size_t array_len) {
4076f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(endian, addr_byte_size, array, array_len);
4085f29bb8aSDimitry Andric
409f034231aSEd Maste if (!array || array_len == 0)
4106f8fc217SDimitry Andric return SBData();
411f034231aSEd Maste
412f034231aSEd Maste size_t data_len = array_len * sizeof(uint64_t);
413f034231aSEd Maste
414f034231aSEd Maste lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
41514f1b3e8SDimitry Andric lldb::DataExtractorSP data_sp(
41614f1b3e8SDimitry Andric new DataExtractor(buffer_sp, endian, addr_byte_size));
417f034231aSEd Maste
418f034231aSEd Maste SBData ret(data_sp);
419f034231aSEd Maste
4206f8fc217SDimitry Andric return ret;
421f034231aSEd Maste }
422f034231aSEd Maste
CreateDataFromUInt32Array(lldb::ByteOrder endian,uint32_t addr_byte_size,uint32_t * array,size_t array_len)42314f1b3e8SDimitry Andric lldb::SBData SBData::CreateDataFromUInt32Array(lldb::ByteOrder endian,
42414f1b3e8SDimitry Andric uint32_t addr_byte_size,
42514f1b3e8SDimitry Andric uint32_t *array,
42614f1b3e8SDimitry Andric size_t array_len) {
4276f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(endian, addr_byte_size, array, array_len);
4285f29bb8aSDimitry Andric
429f034231aSEd Maste if (!array || array_len == 0)
4306f8fc217SDimitry Andric return SBData();
431f034231aSEd Maste
432f034231aSEd Maste size_t data_len = array_len * sizeof(uint32_t);
433f034231aSEd Maste
434f034231aSEd Maste lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
43514f1b3e8SDimitry Andric lldb::DataExtractorSP data_sp(
43614f1b3e8SDimitry Andric new DataExtractor(buffer_sp, endian, addr_byte_size));
437f034231aSEd Maste
438f034231aSEd Maste SBData ret(data_sp);
439f034231aSEd Maste
4406f8fc217SDimitry Andric return ret;
441f034231aSEd Maste }
442f034231aSEd Maste
CreateDataFromSInt64Array(lldb::ByteOrder endian,uint32_t addr_byte_size,int64_t * array,size_t array_len)44314f1b3e8SDimitry Andric lldb::SBData SBData::CreateDataFromSInt64Array(lldb::ByteOrder endian,
44414f1b3e8SDimitry Andric uint32_t addr_byte_size,
44514f1b3e8SDimitry Andric int64_t *array,
44614f1b3e8SDimitry Andric size_t array_len) {
4476f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(endian, addr_byte_size, array, array_len);
4485f29bb8aSDimitry Andric
449f034231aSEd Maste if (!array || array_len == 0)
4506f8fc217SDimitry Andric return SBData();
451f034231aSEd Maste
452f034231aSEd Maste size_t data_len = array_len * sizeof(int64_t);
453f034231aSEd Maste
454f034231aSEd Maste lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
45514f1b3e8SDimitry Andric lldb::DataExtractorSP data_sp(
45614f1b3e8SDimitry Andric new DataExtractor(buffer_sp, endian, addr_byte_size));
457f034231aSEd Maste
458f034231aSEd Maste SBData ret(data_sp);
459f034231aSEd Maste
4606f8fc217SDimitry Andric return ret;
461f034231aSEd Maste }
462f034231aSEd Maste
CreateDataFromSInt32Array(lldb::ByteOrder endian,uint32_t addr_byte_size,int32_t * array,size_t array_len)46314f1b3e8SDimitry Andric lldb::SBData SBData::CreateDataFromSInt32Array(lldb::ByteOrder endian,
46414f1b3e8SDimitry Andric uint32_t addr_byte_size,
46514f1b3e8SDimitry Andric int32_t *array,
46614f1b3e8SDimitry Andric size_t array_len) {
4676f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(endian, addr_byte_size, array, array_len);
4685f29bb8aSDimitry Andric
469f034231aSEd Maste if (!array || array_len == 0)
4706f8fc217SDimitry Andric return SBData();
471f034231aSEd Maste
472f034231aSEd Maste size_t data_len = array_len * sizeof(int32_t);
473f034231aSEd Maste
474f034231aSEd Maste lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
47514f1b3e8SDimitry Andric lldb::DataExtractorSP data_sp(
47614f1b3e8SDimitry Andric new DataExtractor(buffer_sp, endian, addr_byte_size));
477f034231aSEd Maste
478f034231aSEd Maste SBData ret(data_sp);
479f034231aSEd Maste
4806f8fc217SDimitry Andric return ret;
481f034231aSEd Maste }
482f034231aSEd Maste
CreateDataFromDoubleArray(lldb::ByteOrder endian,uint32_t addr_byte_size,double * array,size_t array_len)48314f1b3e8SDimitry Andric lldb::SBData SBData::CreateDataFromDoubleArray(lldb::ByteOrder endian,
48414f1b3e8SDimitry Andric uint32_t addr_byte_size,
48514f1b3e8SDimitry Andric double *array,
48614f1b3e8SDimitry Andric size_t array_len) {
4876f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(endian, addr_byte_size, array, array_len);
4885f29bb8aSDimitry Andric
489f034231aSEd Maste if (!array || array_len == 0)
4906f8fc217SDimitry Andric return SBData();
491f034231aSEd Maste
492f034231aSEd Maste size_t data_len = array_len * sizeof(double);
493f034231aSEd Maste
494f034231aSEd Maste lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
49514f1b3e8SDimitry Andric lldb::DataExtractorSP data_sp(
49614f1b3e8SDimitry Andric new DataExtractor(buffer_sp, endian, addr_byte_size));
497f034231aSEd Maste
498f034231aSEd Maste SBData ret(data_sp);
499f034231aSEd Maste
5006f8fc217SDimitry Andric return ret;
501f034231aSEd Maste }
502f034231aSEd Maste
SetDataFromCString(const char * data)50314f1b3e8SDimitry Andric bool SBData::SetDataFromCString(const char *data) {
5046f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, data);
505f034231aSEd Maste
50614f1b3e8SDimitry Andric if (!data) {
507f034231aSEd Maste return false;
508f034231aSEd Maste }
509f034231aSEd Maste
510f034231aSEd Maste size_t data_len = strlen(data);
511f034231aSEd Maste
512f034231aSEd Maste lldb::DataBufferSP buffer_sp(new DataBufferHeap(data, data_len));
513f034231aSEd Maste
514f034231aSEd Maste if (!m_opaque_sp.get())
5155f29bb8aSDimitry Andric m_opaque_sp = std::make_shared<DataExtractor>(buffer_sp, GetByteOrder(),
5165f29bb8aSDimitry Andric GetAddressByteSize());
517f034231aSEd Maste else
518f034231aSEd Maste m_opaque_sp->SetData(buffer_sp);
519f034231aSEd Maste
520f034231aSEd Maste
521f034231aSEd Maste return true;
522f034231aSEd Maste }
523f034231aSEd Maste
SetDataFromUInt64Array(uint64_t * array,size_t array_len)52414f1b3e8SDimitry Andric bool SBData::SetDataFromUInt64Array(uint64_t *array, size_t array_len) {
5256f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, array, array_len);
526f034231aSEd Maste
52714f1b3e8SDimitry Andric if (!array || array_len == 0) {
528f034231aSEd Maste return false;
529f034231aSEd Maste }
530f034231aSEd Maste
531f034231aSEd Maste size_t data_len = array_len * sizeof(uint64_t);
532f034231aSEd Maste
533f034231aSEd Maste lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
534f034231aSEd Maste
535f034231aSEd Maste if (!m_opaque_sp.get())
5365f29bb8aSDimitry Andric m_opaque_sp = std::make_shared<DataExtractor>(buffer_sp, GetByteOrder(),
5375f29bb8aSDimitry Andric GetAddressByteSize());
538f034231aSEd Maste else
539f034231aSEd Maste m_opaque_sp->SetData(buffer_sp);
540f034231aSEd Maste
541f034231aSEd Maste
542f034231aSEd Maste return true;
543f034231aSEd Maste }
544f034231aSEd Maste
SetDataFromUInt32Array(uint32_t * array,size_t array_len)54514f1b3e8SDimitry Andric bool SBData::SetDataFromUInt32Array(uint32_t *array, size_t array_len) {
5466f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, array, array_len);
547f034231aSEd Maste
54814f1b3e8SDimitry Andric if (!array || array_len == 0) {
549f034231aSEd Maste return false;
550f034231aSEd Maste }
551f034231aSEd Maste
552f034231aSEd Maste size_t data_len = array_len * sizeof(uint32_t);
553f034231aSEd Maste
554f034231aSEd Maste lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
555f034231aSEd Maste
556f034231aSEd Maste if (!m_opaque_sp.get())
5575f29bb8aSDimitry Andric m_opaque_sp = std::make_shared<DataExtractor>(buffer_sp, GetByteOrder(),
5585f29bb8aSDimitry Andric GetAddressByteSize());
559f034231aSEd Maste else
560f034231aSEd Maste m_opaque_sp->SetData(buffer_sp);
561f034231aSEd Maste
562f034231aSEd Maste return true;
563f034231aSEd Maste }
564f034231aSEd Maste
SetDataFromSInt64Array(int64_t * array,size_t array_len)56514f1b3e8SDimitry Andric bool SBData::SetDataFromSInt64Array(int64_t *array, size_t array_len) {
5666f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, array, array_len);
567f034231aSEd Maste
56814f1b3e8SDimitry Andric if (!array || array_len == 0) {
569f034231aSEd Maste return false;
570f034231aSEd Maste }
571f034231aSEd Maste
572f034231aSEd Maste size_t data_len = array_len * sizeof(int64_t);
573f034231aSEd Maste
574f034231aSEd Maste lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
575f034231aSEd Maste
576f034231aSEd Maste if (!m_opaque_sp.get())
5775f29bb8aSDimitry Andric m_opaque_sp = std::make_shared<DataExtractor>(buffer_sp, GetByteOrder(),
5785f29bb8aSDimitry Andric GetAddressByteSize());
579f034231aSEd Maste else
580f034231aSEd Maste m_opaque_sp->SetData(buffer_sp);
581f034231aSEd Maste
582f034231aSEd Maste return true;
583f034231aSEd Maste }
584f034231aSEd Maste
SetDataFromSInt32Array(int32_t * array,size_t array_len)58514f1b3e8SDimitry Andric bool SBData::SetDataFromSInt32Array(int32_t *array, size_t array_len) {
5866f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, array, array_len);
587f034231aSEd Maste
58814f1b3e8SDimitry Andric if (!array || array_len == 0) {
589f034231aSEd Maste return false;
590f034231aSEd Maste }
591f034231aSEd Maste
592f034231aSEd Maste size_t data_len = array_len * sizeof(int32_t);
593f034231aSEd Maste
594f034231aSEd Maste lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
595f034231aSEd Maste
596f034231aSEd Maste if (!m_opaque_sp.get())
5975f29bb8aSDimitry Andric m_opaque_sp = std::make_shared<DataExtractor>(buffer_sp, GetByteOrder(),
5985f29bb8aSDimitry Andric GetAddressByteSize());
599f034231aSEd Maste else
600f034231aSEd Maste m_opaque_sp->SetData(buffer_sp);
601f034231aSEd Maste
602f034231aSEd Maste return true;
603f034231aSEd Maste }
604f034231aSEd Maste
SetDataFromDoubleArray(double * array,size_t array_len)60514f1b3e8SDimitry Andric bool SBData::SetDataFromDoubleArray(double *array, size_t array_len) {
6066f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, array, array_len);
607f034231aSEd Maste
60814f1b3e8SDimitry Andric if (!array || array_len == 0) {
609f034231aSEd Maste return false;
610f034231aSEd Maste }
611f034231aSEd Maste
612f034231aSEd Maste size_t data_len = array_len * sizeof(double);
613f034231aSEd Maste
614f034231aSEd Maste lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
615f034231aSEd Maste
616f034231aSEd Maste if (!m_opaque_sp.get())
6175f29bb8aSDimitry Andric m_opaque_sp = std::make_shared<DataExtractor>(buffer_sp, GetByteOrder(),
6185f29bb8aSDimitry Andric GetAddressByteSize());
619f034231aSEd Maste else
620f034231aSEd Maste m_opaque_sp->SetData(buffer_sp);
621f034231aSEd Maste
622f034231aSEd Maste return true;
623f034231aSEd Maste }
624