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"
23 using namespace lldb_private;
26 m_opaque_sp(new DataExtractor())
30 SBData::SBData (const lldb::DataExtractorSP& data_sp) :
35 SBData::SBData(const SBData &rhs) :
36 m_opaque_sp (rhs.m_opaque_sp)
41 SBData::operator = (const SBData &rhs)
44 m_opaque_sp = rhs.m_opaque_sp;
53 SBData::SetOpaque (const lldb::DataExtractorSP &data_sp)
55 m_opaque_sp = data_sp;
58 lldb_private::DataExtractor *
61 return m_opaque_sp.get();
64 lldb_private::DataExtractor *
65 SBData::operator->() const
67 return m_opaque_sp.operator->();
70 lldb::DataExtractorSP &
76 const lldb::DataExtractorSP &
77 SBData::operator*() const
85 return m_opaque_sp.get() != NULL;
89 SBData::GetAddressByteSize ()
91 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
93 if (m_opaque_sp.get())
94 value = m_opaque_sp->GetAddressByteSize();
96 log->Printf ("SBData::GetAddressByteSize () => "
102 SBData::SetAddressByteSize (uint8_t addr_byte_size)
104 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
105 if (m_opaque_sp.get())
106 m_opaque_sp->SetAddressByteSize(addr_byte_size);
108 log->Printf ("SBData::SetAddressByteSize (%i)", addr_byte_size);
114 if (m_opaque_sp.get())
115 m_opaque_sp->Clear();
119 SBData::GetByteSize ()
121 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
123 if (m_opaque_sp.get())
124 value = m_opaque_sp->GetByteSize();
126 log->Printf ("SBData::GetByteSize () => "
127 "( %" PRIu64 " )", (uint64_t)value);
132 SBData::GetByteOrder ()
134 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
135 lldb::ByteOrder value = eByteOrderInvalid;
136 if (m_opaque_sp.get())
137 value = m_opaque_sp->GetByteOrder();
139 log->Printf ("SBData::GetByteOrder () => "
145 SBData::SetByteOrder (lldb::ByteOrder endian)
147 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
148 if (m_opaque_sp.get())
149 m_opaque_sp->SetByteOrder(endian);
151 log->Printf ("SBData::GetByteOrder (%i)", endian);
156 SBData::GetFloat (lldb::SBError& error, lldb::offset_t offset)
158 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
160 if (!m_opaque_sp.get())
162 error.SetErrorString("no value to read from");
166 uint32_t old_offset = offset;
167 value = m_opaque_sp->GetFloat(&offset);
168 if (offset == old_offset)
169 error.SetErrorString("unable to read data");
172 log->Printf ("SBData::GetFloat (error=%p,offset=%" PRIu64 ") => (%f)",
173 static_cast<void*>(error.get()), offset, value);
178 SBData::GetDouble (lldb::SBError& error, lldb::offset_t offset)
180 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
182 if (!m_opaque_sp.get())
184 error.SetErrorString("no value to read from");
188 uint32_t old_offset = offset;
189 value = m_opaque_sp->GetDouble(&offset);
190 if (offset == old_offset)
191 error.SetErrorString("unable to read data");
194 log->Printf ("SBData::GetDouble (error=%p,offset=%" PRIu64 ") => "
195 "(%f)", static_cast<void*>(error.get()), offset, value);
200 SBData::GetLongDouble (lldb::SBError& error, lldb::offset_t offset)
202 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
203 long double value = 0;
204 if (!m_opaque_sp.get())
206 error.SetErrorString("no value to read from");
210 uint32_t old_offset = offset;
211 value = m_opaque_sp->GetLongDouble(&offset);
212 if (offset == old_offset)
213 error.SetErrorString("unable to read data");
216 log->Printf ("SBData::GetLongDouble (error=%p,offset=%" PRIu64 ") => "
217 "(%Lf)", static_cast<void*>(error.get()), offset, value);
222 SBData::GetAddress (lldb::SBError& error, lldb::offset_t offset)
224 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
225 lldb::addr_t value = 0;
226 if (!m_opaque_sp.get())
228 error.SetErrorString("no value to read from");
232 uint32_t old_offset = offset;
233 value = m_opaque_sp->GetAddress(&offset);
234 if (offset == old_offset)
235 error.SetErrorString("unable to read data");
238 log->Printf ("SBData::GetAddress (error=%p,offset=%" PRIu64 ") => "
239 "(%p)", static_cast<void*>(error.get()), offset,
240 reinterpret_cast<void*>(value));
245 SBData::GetUnsignedInt8 (lldb::SBError& error, lldb::offset_t offset)
247 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
249 if (!m_opaque_sp.get())
251 error.SetErrorString("no value to read from");
255 uint32_t old_offset = offset;
256 value = m_opaque_sp->GetU8(&offset);
257 if (offset == old_offset)
258 error.SetErrorString("unable to read data");
261 log->Printf ("SBData::GetUnsignedInt8 (error=%p,offset=%" PRIu64 ") => "
262 "(%c)", static_cast<void*>(error.get()), offset, value);
267 SBData::GetUnsignedInt16 (lldb::SBError& error, lldb::offset_t offset)
269 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
271 if (!m_opaque_sp.get())
273 error.SetErrorString("no value to read from");
277 uint32_t old_offset = offset;
278 value = m_opaque_sp->GetU16(&offset);
279 if (offset == old_offset)
280 error.SetErrorString("unable to read data");
283 log->Printf ("SBData::GetUnsignedInt16 (error=%p,offset=%" PRIu64 ") => "
284 "(%hd)", static_cast<void*>(error.get()), offset, value);
289 SBData::GetUnsignedInt32 (lldb::SBError& error, lldb::offset_t offset)
291 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
293 if (!m_opaque_sp.get())
295 error.SetErrorString("no value to read from");
299 uint32_t old_offset = offset;
300 value = m_opaque_sp->GetU32(&offset);
301 if (offset == old_offset)
302 error.SetErrorString("unable to read data");
305 log->Printf ("SBData::GetUnsignedInt32 (error=%p,offset=%" PRIu64 ") => "
306 "(%d)", static_cast<void*>(error.get()), offset, value);
311 SBData::GetUnsignedInt64 (lldb::SBError& error, lldb::offset_t offset)
313 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
315 if (!m_opaque_sp.get())
317 error.SetErrorString("no value to read from");
321 uint32_t old_offset = offset;
322 value = m_opaque_sp->GetU64(&offset);
323 if (offset == old_offset)
324 error.SetErrorString("unable to read data");
327 log->Printf ("SBData::GetUnsignedInt64 (error=%p,offset=%" PRIu64 ") => "
328 "(%" PRId64 ")", static_cast<void*>(error.get()), offset,
334 SBData::GetSignedInt8 (lldb::SBError& error, lldb::offset_t offset)
336 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
338 if (!m_opaque_sp.get())
340 error.SetErrorString("no value to read from");
344 uint32_t old_offset = offset;
345 value = (int8_t)m_opaque_sp->GetMaxS64(&offset, 1);
346 if (offset == old_offset)
347 error.SetErrorString("unable to read data");
350 log->Printf ("SBData::GetSignedInt8 (error=%p,offset=%" PRIu64 ") => "
351 "(%c)", static_cast<void*>(error.get()), offset, value);
356 SBData::GetSignedInt16 (lldb::SBError& error, lldb::offset_t offset)
358 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
360 if (!m_opaque_sp.get())
362 error.SetErrorString("no value to read from");
366 uint32_t old_offset = offset;
367 value = (int16_t)m_opaque_sp->GetMaxS64(&offset, 2);
368 if (offset == old_offset)
369 error.SetErrorString("unable to read data");
372 log->Printf ("SBData::GetSignedInt16 (error=%p,offset=%" PRIu64 ") => "
373 "(%hd)", static_cast<void*>(error.get()), offset, value);
378 SBData::GetSignedInt32 (lldb::SBError& error, lldb::offset_t offset)
380 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
382 if (!m_opaque_sp.get())
384 error.SetErrorString("no value to read from");
388 uint32_t old_offset = offset;
389 value = (int32_t)m_opaque_sp->GetMaxS64(&offset, 4);
390 if (offset == old_offset)
391 error.SetErrorString("unable to read data");
394 log->Printf ("SBData::GetSignedInt32 (error=%p,offset=%" PRIu64 ") => "
395 "(%d)", static_cast<void*>(error.get()), offset, value);
400 SBData::GetSignedInt64 (lldb::SBError& error, lldb::offset_t offset)
402 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
404 if (!m_opaque_sp.get())
406 error.SetErrorString("no value to read from");
410 uint32_t old_offset = offset;
411 value = (int64_t)m_opaque_sp->GetMaxS64(&offset, 8);
412 if (offset == old_offset)
413 error.SetErrorString("unable to read data");
416 log->Printf ("SBData::GetSignedInt64 (error=%p,offset=%" PRIu64 ") => "
417 "(%" PRId64 ")", static_cast<void*>(error.get()), offset,
423 SBData::GetString (lldb::SBError& error, lldb::offset_t offset)
425 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
426 const char* value = 0;
427 if (!m_opaque_sp.get())
429 error.SetErrorString("no value to read from");
433 uint32_t old_offset = offset;
434 value = m_opaque_sp->GetCStr(&offset);
435 if (offset == old_offset || (value == NULL))
436 error.SetErrorString("unable to read data");
439 log->Printf ("SBData::GetString (error=%p,offset=%" PRIu64 ") => (%p)",
440 static_cast<void*>(error.get()), offset,
441 static_cast<const void*>(value));
446 SBData::GetDescription (lldb::SBStream &description, lldb::addr_t base_addr)
448 Stream &strm = description.ref();
452 m_opaque_sp->Dump (&strm,
454 lldb::eFormatBytesWithASCII,
456 m_opaque_sp->GetByteSize(),
463 strm.PutCString ("No value");
469 SBData::ReadRawData (lldb::SBError& error,
470 lldb::offset_t offset,
474 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
476 if (!m_opaque_sp.get())
478 error.SetErrorString("no value to read from");
482 uint32_t old_offset = offset;
483 ok = m_opaque_sp->GetU8(&offset, buf, size);
484 if ((offset == old_offset) || (ok == NULL))
485 error.SetErrorString("unable to read data");
488 log->Printf("SBData::ReadRawData (error=%p,offset=%" PRIu64 ",buf=%p,size=%" PRIu64 ") => "
489 "(%p)", static_cast<void*>(error.get()), offset,
490 static_cast<void*>(buf), static_cast<uint64_t>(size),
491 static_cast<void*>(ok));
492 return ok ? size : 0;
496 SBData::SetData (lldb::SBError& error,
499 lldb::ByteOrder endian,
502 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
503 if (!m_opaque_sp.get())
504 m_opaque_sp.reset(new DataExtractor(buf, size, endian, addr_size));
506 m_opaque_sp->SetData(buf, size, endian);
508 log->Printf("SBData::SetData (error=%p,buf=%p,size=%" PRIu64 ",endian=%d,addr_size=%c) => "
509 "(%p)", static_cast<void*>(error.get()),
510 static_cast<const void*>(buf), static_cast<uint64_t>(size),
511 endian, addr_size, static_cast<void*>(m_opaque_sp.get()));
515 SBData::Append (const SBData& rhs)
517 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
519 if (m_opaque_sp.get() && rhs.m_opaque_sp.get())
520 value = m_opaque_sp.get()->Append(*rhs.m_opaque_sp);
522 log->Printf ("SBData::Append (rhs=%p) => (%s)",
523 static_cast<void*>(rhs.get()), value ? "true" : "false");
528 SBData::CreateDataFromCString (lldb::ByteOrder endian, uint32_t addr_byte_size, const char* data)
530 if (!data || !data[0])
533 uint32_t data_len = strlen(data);
535 lldb::DataBufferSP buffer_sp(new DataBufferHeap(data, data_len));
536 lldb::DataExtractorSP data_sp(new DataExtractor(buffer_sp, endian, addr_byte_size));
544 SBData::CreateDataFromUInt64Array (lldb::ByteOrder endian, uint32_t addr_byte_size, uint64_t* array, size_t array_len)
546 if (!array || array_len == 0)
549 size_t data_len = array_len * sizeof(uint64_t);
551 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
552 lldb::DataExtractorSP data_sp(new DataExtractor(buffer_sp, endian, addr_byte_size));
560 SBData::CreateDataFromUInt32Array (lldb::ByteOrder endian, uint32_t addr_byte_size, uint32_t* array, size_t array_len)
562 if (!array || array_len == 0)
565 size_t data_len = array_len * sizeof(uint32_t);
567 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
568 lldb::DataExtractorSP data_sp(new DataExtractor(buffer_sp, endian, addr_byte_size));
576 SBData::CreateDataFromSInt64Array (lldb::ByteOrder endian, uint32_t addr_byte_size, int64_t* array, size_t array_len)
578 if (!array || array_len == 0)
581 size_t data_len = array_len * sizeof(int64_t);
583 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
584 lldb::DataExtractorSP data_sp(new DataExtractor(buffer_sp, endian, addr_byte_size));
592 SBData::CreateDataFromSInt32Array (lldb::ByteOrder endian, uint32_t addr_byte_size, int32_t* array, size_t array_len)
594 if (!array || array_len == 0)
597 size_t data_len = array_len * sizeof(int32_t);
599 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
600 lldb::DataExtractorSP data_sp(new DataExtractor(buffer_sp, endian, addr_byte_size));
608 SBData::CreateDataFromDoubleArray (lldb::ByteOrder endian, uint32_t addr_byte_size, double* array, size_t array_len)
610 if (!array || array_len == 0)
613 size_t data_len = array_len * sizeof(double);
615 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
616 lldb::DataExtractorSP data_sp(new DataExtractor(buffer_sp, endian, addr_byte_size));
624 SBData::SetDataFromCString (const char* data)
626 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
631 log->Printf ("SBData::SetDataFromCString (data=%p) => false",
632 static_cast<const void*>(data));
636 size_t data_len = strlen(data);
638 lldb::DataBufferSP buffer_sp(new DataBufferHeap(data, data_len));
640 if (!m_opaque_sp.get())
641 m_opaque_sp.reset(new DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()));
643 m_opaque_sp->SetData(buffer_sp);
646 log->Printf ("SBData::SetDataFromCString (data=%p) => true",
647 static_cast<const void*>(data));
653 SBData::SetDataFromUInt64Array (uint64_t* array, size_t array_len)
655 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
657 if (!array || array_len == 0)
660 log->Printf("SBData::SetDataFromUInt64Array (array=%p, array_len = %" PRIu64 ") => "
661 "false", static_cast<void*>(array),
662 static_cast<uint64_t>(array_len));
666 size_t data_len = array_len * sizeof(uint64_t);
668 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
670 if (!m_opaque_sp.get())
671 m_opaque_sp.reset(new DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()));
673 m_opaque_sp->SetData(buffer_sp);
676 log->Printf("SBData::SetDataFromUInt64Array (array=%p, array_len = %" PRIu64 ") => "
677 "true", static_cast<void*>(array),
678 static_cast<uint64_t>(array_len));
684 SBData::SetDataFromUInt32Array (uint32_t* array, size_t array_len)
686 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
688 if (!array || array_len == 0)
691 log->Printf("SBData::SetDataFromUInt32Array (array=%p, array_len = %" PRIu64 ") => "
692 "false", static_cast<void*>(array),
693 static_cast<uint64_t>(array_len));
697 size_t data_len = array_len * sizeof(uint32_t);
699 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
701 if (!m_opaque_sp.get())
702 m_opaque_sp.reset(new DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()));
704 m_opaque_sp->SetData(buffer_sp);
707 log->Printf("SBData::SetDataFromUInt32Array (array=%p, array_len = %" PRIu64 ") => "
708 "true", static_cast<void*>(array),
709 static_cast<uint64_t>(array_len));
715 SBData::SetDataFromSInt64Array (int64_t* array, size_t array_len)
717 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
719 if (!array || array_len == 0)
722 log->Printf("SBData::SetDataFromSInt64Array (array=%p, array_len = %" PRIu64 ") => "
723 "false", static_cast<void*>(array),
724 static_cast<uint64_t>(array_len));
728 size_t data_len = array_len * sizeof(int64_t);
730 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
732 if (!m_opaque_sp.get())
733 m_opaque_sp.reset(new DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()));
735 m_opaque_sp->SetData(buffer_sp);
738 log->Printf("SBData::SetDataFromSInt64Array (array=%p, array_len = %" PRIu64 ") => "
739 "true", static_cast<void*>(array),
740 static_cast<uint64_t>(array_len));
746 SBData::SetDataFromSInt32Array (int32_t* array, size_t array_len)
748 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
750 if (!array || array_len == 0)
753 log->Printf("SBData::SetDataFromSInt32Array (array=%p, array_len = %" PRIu64 ") => "
754 "false", static_cast<void*>(array),
755 static_cast<uint64_t>(array_len));
759 size_t data_len = array_len * sizeof(int32_t);
761 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
763 if (!m_opaque_sp.get())
764 m_opaque_sp.reset(new DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()));
766 m_opaque_sp->SetData(buffer_sp);
769 log->Printf("SBData::SetDataFromSInt32Array (array=%p, array_len = %" PRIu64 ") => "
770 "true", static_cast<void*>(array),
771 static_cast<uint64_t>(array_len));
777 SBData::SetDataFromDoubleArray (double* array, size_t array_len)
779 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
781 if (!array || array_len == 0)
784 log->Printf("SBData::SetDataFromDoubleArray (array=%p, array_len = %" PRIu64 ") => "
785 "false", static_cast<void*>(array),
786 static_cast<uint64_t>(array_len));
790 size_t data_len = array_len * sizeof(double);
792 lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));
794 if (!m_opaque_sp.get())
795 m_opaque_sp.reset(new DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize()));
797 m_opaque_sp->SetData(buffer_sp);
800 log->Printf("SBData::SetDataFromDoubleArray (array=%p, array_len = %" PRIu64 ") => "
801 "true", static_cast<void*>(array),
802 static_cast<uint64_t>(array_len));