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/DumpDataExtractor.h"
17 #include "lldb/Utility/DataBufferHeap.h"
18 #include "lldb/Utility/DataExtractor.h"
19 #include "lldb/Utility/Log.h"
20 #include "lldb/Utility/Stream.h"
23 using namespace lldb_private;
25 SBData::SBData() : m_opaque_sp(new DataExtractor()) {}
27 SBData::SBData(const lldb::DataExtractorSP &data_sp) : m_opaque_sp(data_sp) {}
29 SBData::SBData(const SBData &rhs) : m_opaque_sp(rhs.m_opaque_sp) {}
31 const SBData &SBData::operator=(const SBData &rhs) {
33 m_opaque_sp = rhs.m_opaque_sp;
39 void SBData::SetOpaque(const lldb::DataExtractorSP &data_sp) {
40 m_opaque_sp = data_sp;
43 lldb_private::DataExtractor *SBData::get() const { return m_opaque_sp.get(); }
45 lldb_private::DataExtractor *SBData::operator->() const {
46 return m_opaque_sp.operator->();
49 lldb::DataExtractorSP &SBData::operator*() { return m_opaque_sp; }
51 const lldb::DataExtractorSP &SBData::operator*() const { return m_opaque_sp; }
53 bool SBData::IsValid() { return m_opaque_sp.get() != NULL; }
55 uint8_t SBData::GetAddressByteSize() {
56 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
58 if (m_opaque_sp.get())
59 value = m_opaque_sp->GetAddressByteSize();
61 log->Printf("SBData::GetAddressByteSize () => "
67 void SBData::SetAddressByteSize(uint8_t addr_byte_size) {
68 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
69 if (m_opaque_sp.get())
70 m_opaque_sp->SetAddressByteSize(addr_byte_size);
72 log->Printf("SBData::SetAddressByteSize (%i)", addr_byte_size);
75 void SBData::Clear() {
76 if (m_opaque_sp.get())
80 size_t SBData::GetByteSize() {
81 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
83 if (m_opaque_sp.get())
84 value = m_opaque_sp->GetByteSize();
86 log->Printf("SBData::GetByteSize () => "
92 lldb::ByteOrder SBData::GetByteOrder() {
93 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
94 lldb::ByteOrder value = eByteOrderInvalid;
95 if (m_opaque_sp.get())
96 value = m_opaque_sp->GetByteOrder();
98 log->Printf("SBData::GetByteOrder () => "
104 void SBData::SetByteOrder(lldb::ByteOrder endian) {
105 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
106 if (m_opaque_sp.get())
107 m_opaque_sp->SetByteOrder(endian);
109 log->Printf("SBData::GetByteOrder (%i)", endian);
112 float SBData::GetFloat(lldb::SBError &error, lldb::offset_t offset) {
113 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
115 if (!m_opaque_sp.get()) {
116 error.SetErrorString("no value to read from");
118 uint32_t old_offset = offset;
119 value = m_opaque_sp->GetFloat(&offset);
120 if (offset == old_offset)
121 error.SetErrorString("unable to read data");
124 log->Printf("SBData::GetFloat (error=%p,offset=%" PRIu64 ") => (%f)",
125 static_cast<void *>(error.get()), offset, value);
129 double SBData::GetDouble(lldb::SBError &error, lldb::offset_t offset) {
130 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
132 if (!m_opaque_sp.get()) {
133 error.SetErrorString("no value to read from");
135 uint32_t old_offset = offset;
136 value = m_opaque_sp->GetDouble(&offset);
137 if (offset == old_offset)
138 error.SetErrorString("unable to read data");
141 log->Printf("SBData::GetDouble (error=%p,offset=%" PRIu64 ") => "
143 static_cast<void *>(error.get()), offset, value);
147 long double SBData::GetLongDouble(lldb::SBError &error, lldb::offset_t offset) {
148 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
149 long double value = 0;
150 if (!m_opaque_sp.get()) {
151 error.SetErrorString("no value to read from");
153 uint32_t old_offset = offset;
154 value = m_opaque_sp->GetLongDouble(&offset);
155 if (offset == old_offset)
156 error.SetErrorString("unable to read data");
159 log->Printf("SBData::GetLongDouble (error=%p,offset=%" PRIu64 ") => "
161 static_cast<void *>(error.get()), offset, value);
165 lldb::addr_t SBData::GetAddress(lldb::SBError &error, lldb::offset_t offset) {
166 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
167 lldb::addr_t value = 0;
168 if (!m_opaque_sp.get()) {
169 error.SetErrorString("no value to read from");
171 uint32_t old_offset = offset;
172 value = m_opaque_sp->GetAddress(&offset);
173 if (offset == old_offset)
174 error.SetErrorString("unable to read data");
177 log->Printf("SBData::GetAddress (error=%p,offset=%" PRIu64 ") => "
179 static_cast<void *>(error.get()), offset,
180 reinterpret_cast<void *>(value));
184 uint8_t SBData::GetUnsignedInt8(lldb::SBError &error, lldb::offset_t offset) {
185 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
187 if (!m_opaque_sp.get()) {
188 error.SetErrorString("no value to read from");
190 uint32_t old_offset = offset;
191 value = m_opaque_sp->GetU8(&offset);
192 if (offset == old_offset)
193 error.SetErrorString("unable to read data");
196 log->Printf("SBData::GetUnsignedInt8 (error=%p,offset=%" PRIu64 ") => "
198 static_cast<void *>(error.get()), offset, value);
202 uint16_t SBData::GetUnsignedInt16(lldb::SBError &error, lldb::offset_t offset) {
203 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
205 if (!m_opaque_sp.get()) {
206 error.SetErrorString("no value to read from");
208 uint32_t old_offset = offset;
209 value = m_opaque_sp->GetU16(&offset);
210 if (offset == old_offset)
211 error.SetErrorString("unable to read data");
214 log->Printf("SBData::GetUnsignedInt16 (error=%p,offset=%" PRIu64 ") => "
216 static_cast<void *>(error.get()), offset, value);
220 uint32_t SBData::GetUnsignedInt32(lldb::SBError &error, lldb::offset_t offset) {
221 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
223 if (!m_opaque_sp.get()) {
224 error.SetErrorString("no value to read from");
226 uint32_t old_offset = offset;
227 value = m_opaque_sp->GetU32(&offset);
228 if (offset == old_offset)
229 error.SetErrorString("unable to read data");
232 log->Printf("SBData::GetUnsignedInt32 (error=%p,offset=%" PRIu64 ") => "
234 static_cast<void *>(error.get()), offset, value);
238 uint64_t SBData::GetUnsignedInt64(lldb::SBError &error, lldb::offset_t offset) {
239 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
241 if (!m_opaque_sp.get()) {
242 error.SetErrorString("no value to read from");
244 uint32_t old_offset = offset;
245 value = m_opaque_sp->GetU64(&offset);
246 if (offset == old_offset)
247 error.SetErrorString("unable to read data");
250 log->Printf("SBData::GetUnsignedInt64 (error=%p,offset=%" PRIu64 ") => "
252 static_cast<void *>(error.get()), offset, value);
256 int8_t SBData::GetSignedInt8(lldb::SBError &error, lldb::offset_t offset) {
257 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
259 if (!m_opaque_sp.get()) {
260 error.SetErrorString("no value to read from");
262 uint32_t old_offset = offset;
263 value = (int8_t)m_opaque_sp->GetMaxS64(&offset, 1);
264 if (offset == old_offset)
265 error.SetErrorString("unable to read data");
268 log->Printf("SBData::GetSignedInt8 (error=%p,offset=%" PRIu64 ") => "
270 static_cast<void *>(error.get()), offset, value);
274 int16_t SBData::GetSignedInt16(lldb::SBError &error, lldb::offset_t offset) {
275 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
277 if (!m_opaque_sp.get()) {
278 error.SetErrorString("no value to read from");
280 uint32_t old_offset = offset;
281 value = (int16_t)m_opaque_sp->GetMaxS64(&offset, 2);
282 if (offset == old_offset)
283 error.SetErrorString("unable to read data");
286 log->Printf("SBData::GetSignedInt16 (error=%p,offset=%" PRIu64 ") => "
288 static_cast<void *>(error.get()), offset, value);
292 int32_t SBData::GetSignedInt32(lldb::SBError &error, lldb::offset_t offset) {
293 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
295 if (!m_opaque_sp.get()) {
296 error.SetErrorString("no value to read from");
298 uint32_t old_offset = offset;
299 value = (int32_t)m_opaque_sp->GetMaxS64(&offset, 4);
300 if (offset == old_offset)
301 error.SetErrorString("unable to read data");
304 log->Printf("SBData::GetSignedInt32 (error=%p,offset=%" PRIu64 ") => "
306 static_cast<void *>(error.get()), offset, value);
310 int64_t SBData::GetSignedInt64(lldb::SBError &error, lldb::offset_t offset) {
311 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
313 if (!m_opaque_sp.get()) {
314 error.SetErrorString("no value to read from");
316 uint32_t old_offset = offset;
317 value = (int64_t)m_opaque_sp->GetMaxS64(&offset, 8);
318 if (offset == old_offset)
319 error.SetErrorString("unable to read data");
322 log->Printf("SBData::GetSignedInt64 (error=%p,offset=%" PRIu64 ") => "
324 static_cast<void *>(error.get()), offset, value);
328 const char *SBData::GetString(lldb::SBError &error, lldb::offset_t offset) {
329 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
330 const char *value = 0;
331 if (!m_opaque_sp.get()) {
332 error.SetErrorString("no value to read from");
334 uint32_t old_offset = offset;
335 value = m_opaque_sp->GetCStr(&offset);
336 if (offset == old_offset || (value == NULL))
337 error.SetErrorString("unable to read data");
340 log->Printf("SBData::GetString (error=%p,offset=%" PRIu64 ") => (%p)",
341 static_cast<void *>(error.get()), offset,
342 static_cast<const void *>(value));
346 bool SBData::GetDescription(lldb::SBStream &description,
347 lldb::addr_t base_addr) {
348 Stream &strm = description.ref();
351 DumpDataExtractor(*m_opaque_sp, &strm, 0, lldb::eFormatBytesWithASCII, 1,
352 m_opaque_sp->GetByteSize(), 16, base_addr, 0, 0);
354 strm.PutCString("No value");
359 size_t SBData::ReadRawData(lldb::SBError &error, lldb::offset_t offset,
360 void *buf, size_t size) {
361 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
363 if (!m_opaque_sp.get()) {
364 error.SetErrorString("no value to read from");
366 uint32_t old_offset = offset;
367 ok = m_opaque_sp->GetU8(&offset, buf, size);
368 if ((offset == old_offset) || (ok == NULL))
369 error.SetErrorString("unable to read data");
372 log->Printf("SBData::ReadRawData (error=%p,offset=%" PRIu64
373 ",buf=%p,size=%" PRIu64 ") => "
375 static_cast<void *>(error.get()), offset,
376 static_cast<void *>(buf), static_cast<uint64_t>(size),
377 static_cast<void *>(ok));
378 return ok ? size : 0;
381 void SBData::SetData(lldb::SBError &error, const void *buf, size_t size,
382 lldb::ByteOrder endian, uint8_t addr_size) {
383 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
384 if (!m_opaque_sp.get())
385 m_opaque_sp.reset(new DataExtractor(buf, size, endian, addr_size));
388 m_opaque_sp->SetData(buf, size, endian);
389 m_opaque_sp->SetAddressByteSize(addr_size);
393 log->Printf("SBData::SetData (error=%p,buf=%p,size=%" PRIu64
394 ",endian=%d,addr_size=%c) => "
396 static_cast<void *>(error.get()),
397 static_cast<const void *>(buf), static_cast<uint64_t>(size),
398 endian, addr_size, static_cast<void *>(m_opaque_sp.get()));
401 bool SBData::Append(const SBData &rhs) {
402 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
404 if (m_opaque_sp.get() && rhs.m_opaque_sp.get())
405 value = m_opaque_sp.get()->Append(*rhs.m_opaque_sp);
407 log->Printf("SBData::Append (rhs=%p) => (%s)",
408 static_cast<void *>(rhs.get()), value ? "true" : "false");
412 lldb::SBData SBData::CreateDataFromCString(lldb::ByteOrder endian,
413 uint32_t addr_byte_size,
415 if (!data || !data[0])
418 uint32_t data_len = strlen(data);
420 lldb::DataBufferSP buffer_sp(new DataBufferHeap(data, data_len));
421 lldb::DataExtractorSP data_sp(
422 new DataExtractor(buffer_sp, endian, addr_byte_size));
429 lldb::SBData SBData::CreateDataFromUInt64Array(lldb::ByteOrder endian,
430 uint32_t addr_byte_size,
433 if (!array || array_len == 0)
436 size_t data_len = array_len * sizeof(uint64_t);
438 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
439 lldb::DataExtractorSP data_sp(
440 new DataExtractor(buffer_sp, endian, addr_byte_size));
447 lldb::SBData SBData::CreateDataFromUInt32Array(lldb::ByteOrder endian,
448 uint32_t addr_byte_size,
451 if (!array || array_len == 0)
454 size_t data_len = array_len * sizeof(uint32_t);
456 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
457 lldb::DataExtractorSP data_sp(
458 new DataExtractor(buffer_sp, endian, addr_byte_size));
465 lldb::SBData SBData::CreateDataFromSInt64Array(lldb::ByteOrder endian,
466 uint32_t addr_byte_size,
469 if (!array || array_len == 0)
472 size_t data_len = array_len * sizeof(int64_t);
474 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
475 lldb::DataExtractorSP data_sp(
476 new DataExtractor(buffer_sp, endian, addr_byte_size));
483 lldb::SBData SBData::CreateDataFromSInt32Array(lldb::ByteOrder endian,
484 uint32_t addr_byte_size,
487 if (!array || array_len == 0)
490 size_t data_len = array_len * sizeof(int32_t);
492 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
493 lldb::DataExtractorSP data_sp(
494 new DataExtractor(buffer_sp, endian, addr_byte_size));
501 lldb::SBData SBData::CreateDataFromDoubleArray(lldb::ByteOrder endian,
502 uint32_t addr_byte_size,
505 if (!array || array_len == 0)
508 size_t data_len = array_len * sizeof(double);
510 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
511 lldb::DataExtractorSP data_sp(
512 new DataExtractor(buffer_sp, endian, addr_byte_size));
519 bool SBData::SetDataFromCString(const char *data) {
520 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
524 log->Printf("SBData::SetDataFromCString (data=%p) => false",
525 static_cast<const void *>(data));
529 size_t data_len = strlen(data);
531 lldb::DataBufferSP buffer_sp(new DataBufferHeap(data, data_len));
533 if (!m_opaque_sp.get())
535 new DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()));
537 m_opaque_sp->SetData(buffer_sp);
540 log->Printf("SBData::SetDataFromCString (data=%p) => true",
541 static_cast<const void *>(data));
546 bool SBData::SetDataFromUInt64Array(uint64_t *array, size_t array_len) {
547 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
549 if (!array || array_len == 0) {
552 "SBData::SetDataFromUInt64Array (array=%p, array_len = %" PRIu64
555 static_cast<void *>(array), static_cast<uint64_t>(array_len));
559 size_t data_len = array_len * sizeof(uint64_t);
561 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
563 if (!m_opaque_sp.get())
565 new DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()));
567 m_opaque_sp->SetData(buffer_sp);
570 log->Printf("SBData::SetDataFromUInt64Array (array=%p, array_len = %" PRIu64
573 static_cast<void *>(array), static_cast<uint64_t>(array_len));
578 bool SBData::SetDataFromUInt32Array(uint32_t *array, size_t array_len) {
579 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
581 if (!array || array_len == 0) {
584 "SBData::SetDataFromUInt32Array (array=%p, array_len = %" PRIu64
587 static_cast<void *>(array), static_cast<uint64_t>(array_len));
591 size_t data_len = array_len * sizeof(uint32_t);
593 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
595 if (!m_opaque_sp.get())
597 new DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()));
599 m_opaque_sp->SetData(buffer_sp);
602 log->Printf("SBData::SetDataFromUInt32Array (array=%p, array_len = %" PRIu64
605 static_cast<void *>(array), static_cast<uint64_t>(array_len));
610 bool SBData::SetDataFromSInt64Array(int64_t *array, size_t array_len) {
611 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
613 if (!array || array_len == 0) {
616 "SBData::SetDataFromSInt64Array (array=%p, array_len = %" PRIu64
619 static_cast<void *>(array), static_cast<uint64_t>(array_len));
623 size_t data_len = array_len * sizeof(int64_t);
625 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
627 if (!m_opaque_sp.get())
629 new DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()));
631 m_opaque_sp->SetData(buffer_sp);
634 log->Printf("SBData::SetDataFromSInt64Array (array=%p, array_len = %" PRIu64
637 static_cast<void *>(array), static_cast<uint64_t>(array_len));
642 bool SBData::SetDataFromSInt32Array(int32_t *array, size_t array_len) {
643 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
645 if (!array || array_len == 0) {
648 "SBData::SetDataFromSInt32Array (array=%p, array_len = %" PRIu64
651 static_cast<void *>(array), static_cast<uint64_t>(array_len));
655 size_t data_len = array_len * sizeof(int32_t);
657 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
659 if (!m_opaque_sp.get())
661 new DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()));
663 m_opaque_sp->SetData(buffer_sp);
666 log->Printf("SBData::SetDataFromSInt32Array (array=%p, array_len = %" PRIu64
669 static_cast<void *>(array), static_cast<uint64_t>(array_len));
674 bool SBData::SetDataFromDoubleArray(double *array, size_t array_len) {
675 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
677 if (!array || array_len == 0) {
680 "SBData::SetDataFromDoubleArray (array=%p, array_len = %" PRIu64
683 static_cast<void *>(array), static_cast<uint64_t>(array_len));
687 size_t data_len = array_len * sizeof(double);
689 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
691 if (!m_opaque_sp.get())
693 new DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()));
695 m_opaque_sp->SetData(buffer_sp);
698 log->Printf("SBData::SetDataFromDoubleArray (array=%p, array_len = %" PRIu64
701 static_cast<void *>(array), static_cast<uint64_t>(array_len));