1 //===-- SBData.cpp ----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include <inttypes.h> // PRIu64
12 #include "lldb/API/SBData.h"
13 #include "lldb/API/SBError.h"
14 #include "lldb/API/SBStream.h"
16 #include "lldb/Core/DataBufferHeap.h"
17 #include "lldb/Core/DataExtractor.h"
18 #include "lldb/Core/Log.h"
19 #include "lldb/Core/Stream.h"
22 using namespace lldb_private;
24 SBData::SBData() : m_opaque_sp(new DataExtractor()) {}
26 SBData::SBData(const lldb::DataExtractorSP &data_sp) : m_opaque_sp(data_sp) {}
28 SBData::SBData(const SBData &rhs) : m_opaque_sp(rhs.m_opaque_sp) {}
30 const SBData &SBData::operator=(const SBData &rhs) {
32 m_opaque_sp = rhs.m_opaque_sp;
38 void SBData::SetOpaque(const lldb::DataExtractorSP &data_sp) {
39 m_opaque_sp = data_sp;
42 lldb_private::DataExtractor *SBData::get() const { return m_opaque_sp.get(); }
44 lldb_private::DataExtractor *SBData::operator->() const {
45 return m_opaque_sp.operator->();
48 lldb::DataExtractorSP &SBData::operator*() { return m_opaque_sp; }
50 const lldb::DataExtractorSP &SBData::operator*() const { return m_opaque_sp; }
52 bool SBData::IsValid() { return m_opaque_sp.get() != NULL; }
54 uint8_t SBData::GetAddressByteSize() {
55 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
57 if (m_opaque_sp.get())
58 value = m_opaque_sp->GetAddressByteSize();
60 log->Printf("SBData::GetAddressByteSize () => "
66 void SBData::SetAddressByteSize(uint8_t addr_byte_size) {
67 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
68 if (m_opaque_sp.get())
69 m_opaque_sp->SetAddressByteSize(addr_byte_size);
71 log->Printf("SBData::SetAddressByteSize (%i)", addr_byte_size);
74 void SBData::Clear() {
75 if (m_opaque_sp.get())
79 size_t SBData::GetByteSize() {
80 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
82 if (m_opaque_sp.get())
83 value = m_opaque_sp->GetByteSize();
85 log->Printf("SBData::GetByteSize () => "
91 lldb::ByteOrder SBData::GetByteOrder() {
92 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
93 lldb::ByteOrder value = eByteOrderInvalid;
94 if (m_opaque_sp.get())
95 value = m_opaque_sp->GetByteOrder();
97 log->Printf("SBData::GetByteOrder () => "
103 void SBData::SetByteOrder(lldb::ByteOrder endian) {
104 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
105 if (m_opaque_sp.get())
106 m_opaque_sp->SetByteOrder(endian);
108 log->Printf("SBData::GetByteOrder (%i)", endian);
111 float SBData::GetFloat(lldb::SBError &error, lldb::offset_t offset) {
112 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
114 if (!m_opaque_sp.get()) {
115 error.SetErrorString("no value to read from");
117 uint32_t old_offset = offset;
118 value = m_opaque_sp->GetFloat(&offset);
119 if (offset == old_offset)
120 error.SetErrorString("unable to read data");
123 log->Printf("SBData::GetFloat (error=%p,offset=%" PRIu64 ") => (%f)",
124 static_cast<void *>(error.get()), offset, value);
128 double SBData::GetDouble(lldb::SBError &error, lldb::offset_t offset) {
129 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
131 if (!m_opaque_sp.get()) {
132 error.SetErrorString("no value to read from");
134 uint32_t old_offset = offset;
135 value = m_opaque_sp->GetDouble(&offset);
136 if (offset == old_offset)
137 error.SetErrorString("unable to read data");
140 log->Printf("SBData::GetDouble (error=%p,offset=%" PRIu64 ") => "
142 static_cast<void *>(error.get()), offset, value);
146 long double SBData::GetLongDouble(lldb::SBError &error, lldb::offset_t offset) {
147 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
148 long double value = 0;
149 if (!m_opaque_sp.get()) {
150 error.SetErrorString("no value to read from");
152 uint32_t old_offset = offset;
153 value = m_opaque_sp->GetLongDouble(&offset);
154 if (offset == old_offset)
155 error.SetErrorString("unable to read data");
158 log->Printf("SBData::GetLongDouble (error=%p,offset=%" PRIu64 ") => "
160 static_cast<void *>(error.get()), offset, value);
164 lldb::addr_t SBData::GetAddress(lldb::SBError &error, lldb::offset_t offset) {
165 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
166 lldb::addr_t value = 0;
167 if (!m_opaque_sp.get()) {
168 error.SetErrorString("no value to read from");
170 uint32_t old_offset = offset;
171 value = m_opaque_sp->GetAddress(&offset);
172 if (offset == old_offset)
173 error.SetErrorString("unable to read data");
176 log->Printf("SBData::GetAddress (error=%p,offset=%" PRIu64 ") => "
178 static_cast<void *>(error.get()), offset,
179 reinterpret_cast<void *>(value));
183 uint8_t SBData::GetUnsignedInt8(lldb::SBError &error, lldb::offset_t offset) {
184 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
186 if (!m_opaque_sp.get()) {
187 error.SetErrorString("no value to read from");
189 uint32_t old_offset = offset;
190 value = m_opaque_sp->GetU8(&offset);
191 if (offset == old_offset)
192 error.SetErrorString("unable to read data");
195 log->Printf("SBData::GetUnsignedInt8 (error=%p,offset=%" PRIu64 ") => "
197 static_cast<void *>(error.get()), offset, value);
201 uint16_t SBData::GetUnsignedInt16(lldb::SBError &error, lldb::offset_t offset) {
202 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
204 if (!m_opaque_sp.get()) {
205 error.SetErrorString("no value to read from");
207 uint32_t old_offset = offset;
208 value = m_opaque_sp->GetU16(&offset);
209 if (offset == old_offset)
210 error.SetErrorString("unable to read data");
213 log->Printf("SBData::GetUnsignedInt16 (error=%p,offset=%" PRIu64 ") => "
215 static_cast<void *>(error.get()), offset, value);
219 uint32_t SBData::GetUnsignedInt32(lldb::SBError &error, lldb::offset_t offset) {
220 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
222 if (!m_opaque_sp.get()) {
223 error.SetErrorString("no value to read from");
225 uint32_t old_offset = offset;
226 value = m_opaque_sp->GetU32(&offset);
227 if (offset == old_offset)
228 error.SetErrorString("unable to read data");
231 log->Printf("SBData::GetUnsignedInt32 (error=%p,offset=%" PRIu64 ") => "
233 static_cast<void *>(error.get()), offset, value);
237 uint64_t SBData::GetUnsignedInt64(lldb::SBError &error, lldb::offset_t offset) {
238 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
240 if (!m_opaque_sp.get()) {
241 error.SetErrorString("no value to read from");
243 uint32_t old_offset = offset;
244 value = m_opaque_sp->GetU64(&offset);
245 if (offset == old_offset)
246 error.SetErrorString("unable to read data");
249 log->Printf("SBData::GetUnsignedInt64 (error=%p,offset=%" PRIu64 ") => "
251 static_cast<void *>(error.get()), offset, value);
255 int8_t SBData::GetSignedInt8(lldb::SBError &error, lldb::offset_t offset) {
256 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
258 if (!m_opaque_sp.get()) {
259 error.SetErrorString("no value to read from");
261 uint32_t old_offset = offset;
262 value = (int8_t)m_opaque_sp->GetMaxS64(&offset, 1);
263 if (offset == old_offset)
264 error.SetErrorString("unable to read data");
267 log->Printf("SBData::GetSignedInt8 (error=%p,offset=%" PRIu64 ") => "
269 static_cast<void *>(error.get()), offset, value);
273 int16_t SBData::GetSignedInt16(lldb::SBError &error, lldb::offset_t offset) {
274 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
276 if (!m_opaque_sp.get()) {
277 error.SetErrorString("no value to read from");
279 uint32_t old_offset = offset;
280 value = (int16_t)m_opaque_sp->GetMaxS64(&offset, 2);
281 if (offset == old_offset)
282 error.SetErrorString("unable to read data");
285 log->Printf("SBData::GetSignedInt16 (error=%p,offset=%" PRIu64 ") => "
287 static_cast<void *>(error.get()), offset, value);
291 int32_t SBData::GetSignedInt32(lldb::SBError &error, lldb::offset_t offset) {
292 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
294 if (!m_opaque_sp.get()) {
295 error.SetErrorString("no value to read from");
297 uint32_t old_offset = offset;
298 value = (int32_t)m_opaque_sp->GetMaxS64(&offset, 4);
299 if (offset == old_offset)
300 error.SetErrorString("unable to read data");
303 log->Printf("SBData::GetSignedInt32 (error=%p,offset=%" PRIu64 ") => "
305 static_cast<void *>(error.get()), offset, value);
309 int64_t SBData::GetSignedInt64(lldb::SBError &error, lldb::offset_t offset) {
310 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
312 if (!m_opaque_sp.get()) {
313 error.SetErrorString("no value to read from");
315 uint32_t old_offset = offset;
316 value = (int64_t)m_opaque_sp->GetMaxS64(&offset, 8);
317 if (offset == old_offset)
318 error.SetErrorString("unable to read data");
321 log->Printf("SBData::GetSignedInt64 (error=%p,offset=%" PRIu64 ") => "
323 static_cast<void *>(error.get()), offset, value);
327 const char *SBData::GetString(lldb::SBError &error, lldb::offset_t offset) {
328 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
329 const char *value = 0;
330 if (!m_opaque_sp.get()) {
331 error.SetErrorString("no value to read from");
333 uint32_t old_offset = offset;
334 value = m_opaque_sp->GetCStr(&offset);
335 if (offset == old_offset || (value == NULL))
336 error.SetErrorString("unable to read data");
339 log->Printf("SBData::GetString (error=%p,offset=%" PRIu64 ") => (%p)",
340 static_cast<void *>(error.get()), offset,
341 static_cast<const void *>(value));
345 bool SBData::GetDescription(lldb::SBStream &description,
346 lldb::addr_t base_addr) {
347 Stream &strm = description.ref();
350 m_opaque_sp->Dump(&strm, 0, lldb::eFormatBytesWithASCII, 1,
351 m_opaque_sp->GetByteSize(), 16, base_addr, 0, 0);
353 strm.PutCString("No value");
358 size_t SBData::ReadRawData(lldb::SBError &error, lldb::offset_t offset,
359 void *buf, size_t size) {
360 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
362 if (!m_opaque_sp.get()) {
363 error.SetErrorString("no value to read from");
365 uint32_t old_offset = offset;
366 ok = m_opaque_sp->GetU8(&offset, buf, size);
367 if ((offset == old_offset) || (ok == NULL))
368 error.SetErrorString("unable to read data");
371 log->Printf("SBData::ReadRawData (error=%p,offset=%" PRIu64
372 ",buf=%p,size=%" PRIu64 ") => "
374 static_cast<void *>(error.get()), offset,
375 static_cast<void *>(buf), static_cast<uint64_t>(size),
376 static_cast<void *>(ok));
377 return ok ? size : 0;
380 void SBData::SetData(lldb::SBError &error, const void *buf, size_t size,
381 lldb::ByteOrder endian, uint8_t addr_size) {
382 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
383 if (!m_opaque_sp.get())
384 m_opaque_sp.reset(new DataExtractor(buf, size, endian, addr_size));
386 m_opaque_sp->SetData(buf, size, endian);
388 log->Printf("SBData::SetData (error=%p,buf=%p,size=%" PRIu64
389 ",endian=%d,addr_size=%c) => "
391 static_cast<void *>(error.get()),
392 static_cast<const void *>(buf), static_cast<uint64_t>(size),
393 endian, addr_size, static_cast<void *>(m_opaque_sp.get()));
396 bool SBData::Append(const SBData &rhs) {
397 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
399 if (m_opaque_sp.get() && rhs.m_opaque_sp.get())
400 value = m_opaque_sp.get()->Append(*rhs.m_opaque_sp);
402 log->Printf("SBData::Append (rhs=%p) => (%s)",
403 static_cast<void *>(rhs.get()), value ? "true" : "false");
407 lldb::SBData SBData::CreateDataFromCString(lldb::ByteOrder endian,
408 uint32_t addr_byte_size,
410 if (!data || !data[0])
413 uint32_t data_len = strlen(data);
415 lldb::DataBufferSP buffer_sp(new DataBufferHeap(data, data_len));
416 lldb::DataExtractorSP data_sp(
417 new DataExtractor(buffer_sp, endian, addr_byte_size));
424 lldb::SBData SBData::CreateDataFromUInt64Array(lldb::ByteOrder endian,
425 uint32_t addr_byte_size,
428 if (!array || array_len == 0)
431 size_t data_len = array_len * sizeof(uint64_t);
433 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
434 lldb::DataExtractorSP data_sp(
435 new DataExtractor(buffer_sp, endian, addr_byte_size));
442 lldb::SBData SBData::CreateDataFromUInt32Array(lldb::ByteOrder endian,
443 uint32_t addr_byte_size,
446 if (!array || array_len == 0)
449 size_t data_len = array_len * sizeof(uint32_t);
451 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
452 lldb::DataExtractorSP data_sp(
453 new DataExtractor(buffer_sp, endian, addr_byte_size));
460 lldb::SBData SBData::CreateDataFromSInt64Array(lldb::ByteOrder endian,
461 uint32_t addr_byte_size,
464 if (!array || array_len == 0)
467 size_t data_len = array_len * sizeof(int64_t);
469 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
470 lldb::DataExtractorSP data_sp(
471 new DataExtractor(buffer_sp, endian, addr_byte_size));
478 lldb::SBData SBData::CreateDataFromSInt32Array(lldb::ByteOrder endian,
479 uint32_t addr_byte_size,
482 if (!array || array_len == 0)
485 size_t data_len = array_len * sizeof(int32_t);
487 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
488 lldb::DataExtractorSP data_sp(
489 new DataExtractor(buffer_sp, endian, addr_byte_size));
496 lldb::SBData SBData::CreateDataFromDoubleArray(lldb::ByteOrder endian,
497 uint32_t addr_byte_size,
500 if (!array || array_len == 0)
503 size_t data_len = array_len * sizeof(double);
505 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
506 lldb::DataExtractorSP data_sp(
507 new DataExtractor(buffer_sp, endian, addr_byte_size));
514 bool SBData::SetDataFromCString(const char *data) {
515 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
519 log->Printf("SBData::SetDataFromCString (data=%p) => false",
520 static_cast<const void *>(data));
524 size_t data_len = strlen(data);
526 lldb::DataBufferSP buffer_sp(new DataBufferHeap(data, data_len));
528 if (!m_opaque_sp.get())
530 new DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()));
532 m_opaque_sp->SetData(buffer_sp);
535 log->Printf("SBData::SetDataFromCString (data=%p) => true",
536 static_cast<const void *>(data));
541 bool SBData::SetDataFromUInt64Array(uint64_t *array, size_t array_len) {
542 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
544 if (!array || array_len == 0) {
547 "SBData::SetDataFromUInt64Array (array=%p, array_len = %" PRIu64
550 static_cast<void *>(array), static_cast<uint64_t>(array_len));
554 size_t data_len = array_len * sizeof(uint64_t);
556 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
558 if (!m_opaque_sp.get())
560 new DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()));
562 m_opaque_sp->SetData(buffer_sp);
565 log->Printf("SBData::SetDataFromUInt64Array (array=%p, array_len = %" PRIu64
568 static_cast<void *>(array), static_cast<uint64_t>(array_len));
573 bool SBData::SetDataFromUInt32Array(uint32_t *array, size_t array_len) {
574 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
576 if (!array || array_len == 0) {
579 "SBData::SetDataFromUInt32Array (array=%p, array_len = %" PRIu64
582 static_cast<void *>(array), static_cast<uint64_t>(array_len));
586 size_t data_len = array_len * sizeof(uint32_t);
588 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
590 if (!m_opaque_sp.get())
592 new DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()));
594 m_opaque_sp->SetData(buffer_sp);
597 log->Printf("SBData::SetDataFromUInt32Array (array=%p, array_len = %" PRIu64
600 static_cast<void *>(array), static_cast<uint64_t>(array_len));
605 bool SBData::SetDataFromSInt64Array(int64_t *array, size_t array_len) {
606 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
608 if (!array || array_len == 0) {
611 "SBData::SetDataFromSInt64Array (array=%p, array_len = %" PRIu64
614 static_cast<void *>(array), static_cast<uint64_t>(array_len));
618 size_t data_len = array_len * sizeof(int64_t);
620 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
622 if (!m_opaque_sp.get())
624 new DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()));
626 m_opaque_sp->SetData(buffer_sp);
629 log->Printf("SBData::SetDataFromSInt64Array (array=%p, array_len = %" PRIu64
632 static_cast<void *>(array), static_cast<uint64_t>(array_len));
637 bool SBData::SetDataFromSInt32Array(int32_t *array, size_t array_len) {
638 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
640 if (!array || array_len == 0) {
643 "SBData::SetDataFromSInt32Array (array=%p, array_len = %" PRIu64
646 static_cast<void *>(array), static_cast<uint64_t>(array_len));
650 size_t data_len = array_len * sizeof(int32_t);
652 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
654 if (!m_opaque_sp.get())
656 new DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()));
658 m_opaque_sp->SetData(buffer_sp);
661 log->Printf("SBData::SetDataFromSInt32Array (array=%p, array_len = %" PRIu64
664 static_cast<void *>(array), static_cast<uint64_t>(array_len));
669 bool SBData::SetDataFromDoubleArray(double *array, size_t array_len) {
670 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
672 if (!array || array_len == 0) {
675 "SBData::SetDataFromDoubleArray (array=%p, array_len = %" PRIu64
678 static_cast<void *>(array), static_cast<uint64_t>(array_len));
682 size_t data_len = array_len * sizeof(double);
684 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
686 if (!m_opaque_sp.get())
688 new DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()));
690 m_opaque_sp->SetData(buffer_sp);
693 log->Printf("SBData::SetDataFromDoubleArray (array=%p, array_len = %" PRIu64
696 static_cast<void *>(array), static_cast<uint64_t>(array_len));