1 //===-- DataExtractor.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 "lldb/Utility/DataExtractor.h"
12 #include "lldb/lldb-defines.h"
13 #include "lldb/lldb-enumerations.h"
14 #include "lldb/lldb-forward.h"
15 #include "lldb/lldb-types.h"
17 #include "lldb/Utility/DataBuffer.h"
18 #include "lldb/Utility/DataBufferHeap.h"
19 #include "lldb/Utility/Endian.h"
20 #include "lldb/Utility/LLDBAssert.h"
21 #include "lldb/Utility/Log.h"
22 #include "lldb/Utility/Stream.h"
23 #include "lldb/Utility/StreamString.h"
24 #include "lldb/Utility/UUID.h"
26 #include "llvm/ADT/ArrayRef.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/Support/MD5.h"
29 #include "llvm/Support/MathExtras.h"
42 using namespace lldb_private;
44 static inline uint16_t ReadInt16(const unsigned char *ptr, offset_t offset) {
46 memcpy(&value, ptr + offset, 2);
50 static inline uint32_t ReadInt32(const unsigned char *ptr,
51 offset_t offset = 0) {
53 memcpy(&value, ptr + offset, 4);
57 static inline uint64_t ReadInt64(const unsigned char *ptr,
58 offset_t offset = 0) {
60 memcpy(&value, ptr + offset, 8);
64 static inline uint16_t ReadInt16(const void *ptr) {
66 memcpy(&value, ptr, 2);
70 static inline uint16_t ReadSwapInt16(const unsigned char *ptr,
73 memcpy(&value, ptr + offset, 2);
74 return llvm::ByteSwap_16(value);
77 static inline uint32_t ReadSwapInt32(const unsigned char *ptr,
80 memcpy(&value, ptr + offset, 4);
81 return llvm::ByteSwap_32(value);
84 static inline uint64_t ReadSwapInt64(const unsigned char *ptr,
87 memcpy(&value, ptr + offset, 8);
88 return llvm::ByteSwap_64(value);
91 static inline uint16_t ReadSwapInt16(const void *ptr) {
93 memcpy(&value, ptr, 2);
94 return llvm::ByteSwap_16(value);
97 static inline uint32_t ReadSwapInt32(const void *ptr) {
99 memcpy(&value, ptr, 4);
100 return llvm::ByteSwap_32(value);
103 static inline uint64_t ReadSwapInt64(const void *ptr) {
105 memcpy(&value, ptr, 8);
106 return llvm::ByteSwap_64(value);
109 static inline uint64_t ReadMaxInt64(const uint8_t *data, size_t byte_size,
110 ByteOrder byte_order) {
112 if (byte_order == eByteOrderBig)
113 for (size_t i = 0; i < byte_size; ++i)
114 res = (res << 8) | data[i];
116 assert(byte_order == eByteOrderLittle);
117 for (size_t i = 0; i < byte_size; ++i)
118 res = (res << 8) | data[byte_size - 1 - i];
123 DataExtractor::DataExtractor()
124 : m_start(nullptr), m_end(nullptr),
125 m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)),
126 m_data_sp(), m_target_byte_size(1) {}
128 //----------------------------------------------------------------------
129 // This constructor allows us to use data that is owned by someone else. The
130 // data must stay around as long as this object is valid.
131 //----------------------------------------------------------------------
132 DataExtractor::DataExtractor(const void *data, offset_t length,
133 ByteOrder endian, uint32_t addr_size,
134 uint32_t target_byte_size /*=1*/)
135 : m_start(const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(data))),
136 m_end(const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(data)) +
138 m_byte_order(endian), m_addr_size(addr_size), m_data_sp(),
139 m_target_byte_size(target_byte_size) {
140 #ifdef LLDB_CONFIGURATION_DEBUG
141 assert(addr_size == 4 || addr_size == 8);
145 //----------------------------------------------------------------------
146 // Make a shared pointer reference to the shared data in "data_sp" and set the
147 // endian swapping setting to "swap", and the address size to "addr_size". The
148 // shared data reference will ensure the data lives as long as any
149 // DataExtractor objects exist that have a reference to this data.
150 //----------------------------------------------------------------------
151 DataExtractor::DataExtractor(const DataBufferSP &data_sp, ByteOrder endian,
153 uint32_t target_byte_size /*=1*/)
154 : m_start(nullptr), m_end(nullptr), m_byte_order(endian),
155 m_addr_size(addr_size), m_data_sp(),
156 m_target_byte_size(target_byte_size) {
157 #ifdef LLDB_CONFIGURATION_DEBUG
158 assert(addr_size == 4 || addr_size == 8);
163 //----------------------------------------------------------------------
164 // Initialize this object with a subset of the data bytes in "data". If "data"
165 // contains shared data, then a reference to this shared data will added and
166 // the shared data will stay around as long as any object contains a reference
167 // to that data. The endian swap and address size settings are copied from
169 //----------------------------------------------------------------------
170 DataExtractor::DataExtractor(const DataExtractor &data, offset_t offset,
171 offset_t length, uint32_t target_byte_size /*=1*/)
172 : m_start(nullptr), m_end(nullptr), m_byte_order(data.m_byte_order),
173 m_addr_size(data.m_addr_size), m_data_sp(),
174 m_target_byte_size(target_byte_size) {
175 #ifdef LLDB_CONFIGURATION_DEBUG
176 assert(m_addr_size == 4 || m_addr_size == 8);
178 if (data.ValidOffset(offset)) {
179 offset_t bytes_available = data.GetByteSize() - offset;
180 if (length > bytes_available)
181 length = bytes_available;
182 SetData(data, offset, length);
186 DataExtractor::DataExtractor(const DataExtractor &rhs)
187 : m_start(rhs.m_start), m_end(rhs.m_end), m_byte_order(rhs.m_byte_order),
188 m_addr_size(rhs.m_addr_size), m_data_sp(rhs.m_data_sp),
189 m_target_byte_size(rhs.m_target_byte_size) {
190 #ifdef LLDB_CONFIGURATION_DEBUG
191 assert(m_addr_size == 4 || m_addr_size == 8);
195 //----------------------------------------------------------------------
196 // Assignment operator
197 //----------------------------------------------------------------------
198 const DataExtractor &DataExtractor::operator=(const DataExtractor &rhs) {
200 m_start = rhs.m_start;
202 m_byte_order = rhs.m_byte_order;
203 m_addr_size = rhs.m_addr_size;
204 m_data_sp = rhs.m_data_sp;
209 DataExtractor::~DataExtractor() = default;
211 //------------------------------------------------------------------
212 // Clears the object contents back to a default invalid state, and release any
213 // references to shared data that this object may contain.
214 //------------------------------------------------------------------
215 void DataExtractor::Clear() {
218 m_byte_order = endian::InlHostByteOrder();
219 m_addr_size = sizeof(void *);
223 //------------------------------------------------------------------
224 // If this object contains shared data, this function returns the offset into
225 // that shared data. Else zero is returned.
226 //------------------------------------------------------------------
227 size_t DataExtractor::GetSharedDataOffset() const {
228 if (m_start != nullptr) {
229 const DataBuffer *data = m_data_sp.get();
230 if (data != nullptr) {
231 const uint8_t *data_bytes = data->GetBytes();
232 if (data_bytes != nullptr) {
233 assert(m_start >= data_bytes);
234 return m_start - data_bytes;
241 //----------------------------------------------------------------------
242 // Set the data with which this object will extract from to data starting at
243 // BYTES and set the length of the data to LENGTH bytes long. The data is
244 // externally owned must be around at least as long as this object points to
245 // the data. No copy of the data is made, this object just refers to this data
246 // and can extract from it. If this object refers to any shared data upon
247 // entry, the reference to that data will be released. Is SWAP is set to true,
248 // any data extracted will be endian swapped.
249 //----------------------------------------------------------------------
250 lldb::offset_t DataExtractor::SetData(const void *bytes, offset_t length,
252 m_byte_order = endian;
254 if (bytes == nullptr || length == 0) {
258 m_start = const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(bytes));
259 m_end = m_start + length;
261 return GetByteSize();
264 //----------------------------------------------------------------------
265 // Assign the data for this object to be a subrange in "data" starting
266 // "data_offset" bytes into "data" and ending "data_length" bytes later. If
267 // "data_offset" is not a valid offset into "data", then this object will
268 // contain no bytes. If "data_offset" is within "data" yet "data_length" is too
269 // large, the length will be capped at the number of bytes remaining in "data".
270 // If "data" contains a shared pointer to other data, then a ref counted
271 // pointer to that data will be made in this object. If "data" doesn't contain
272 // a shared pointer to data, then the bytes referred to in "data" will need to
273 // exist at least as long as this object refers to those bytes. The address
274 // size and endian swap settings are copied from the current values in "data".
275 //----------------------------------------------------------------------
276 lldb::offset_t DataExtractor::SetData(const DataExtractor &data,
277 offset_t data_offset,
278 offset_t data_length) {
279 m_addr_size = data.m_addr_size;
280 #ifdef LLDB_CONFIGURATION_DEBUG
281 assert(m_addr_size == 4 || m_addr_size == 8);
283 // If "data" contains shared pointer to data, then we can use that
284 if (data.m_data_sp) {
285 m_byte_order = data.m_byte_order;
286 return SetData(data.m_data_sp, data.GetSharedDataOffset() + data_offset,
290 // We have a DataExtractor object that just has a pointer to bytes
291 if (data.ValidOffset(data_offset)) {
292 if (data_length > data.GetByteSize() - data_offset)
293 data_length = data.GetByteSize() - data_offset;
294 return SetData(data.GetDataStart() + data_offset, data_length,
295 data.GetByteOrder());
300 //----------------------------------------------------------------------
301 // Assign the data for this object to be a subrange of the shared data in
302 // "data_sp" starting "data_offset" bytes into "data_sp" and ending
303 // "data_length" bytes later. If "data_offset" is not a valid offset into
304 // "data_sp", then this object will contain no bytes. If "data_offset" is
305 // within "data_sp" yet "data_length" is too large, the length will be capped
306 // at the number of bytes remaining in "data_sp". A ref counted pointer to the
307 // data in "data_sp" will be made in this object IF the number of bytes this
308 // object refers to in greater than zero (if at least one byte was available
309 // starting at "data_offset") to ensure the data stays around as long as it is
310 // needed. The address size and endian swap settings will remain unchanged from
311 // their current settings.
312 //----------------------------------------------------------------------
313 lldb::offset_t DataExtractor::SetData(const DataBufferSP &data_sp,
314 offset_t data_offset,
315 offset_t data_length) {
316 m_start = m_end = nullptr;
318 if (data_length > 0) {
321 const size_t data_size = data_sp->GetByteSize();
322 if (data_offset < data_size) {
323 m_start = data_sp->GetBytes() + data_offset;
324 const size_t bytes_left = data_size - data_offset;
325 // Cap the length of we asked for too many
326 if (data_length <= bytes_left)
327 m_end = m_start + data_length; // We got all the bytes we wanted
329 m_end = m_start + bytes_left; // Not all the bytes requested were
330 // available in the shared data
335 size_t new_size = GetByteSize();
337 // Don't hold a shared pointer to the data buffer if we don't share any valid
338 // bytes in the shared buffer.
345 //----------------------------------------------------------------------
346 // Extract a single unsigned char from the binary data and update the offset
347 // pointed to by "offset_ptr".
349 // RETURNS the byte that was extracted, or zero on failure.
350 //----------------------------------------------------------------------
351 uint8_t DataExtractor::GetU8(offset_t *offset_ptr) const {
352 const uint8_t *data = (const uint8_t *)GetData(offset_ptr, 1);
358 //----------------------------------------------------------------------
359 // Extract "count" unsigned chars from the binary data and update the offset
360 // pointed to by "offset_ptr". The extracted data is copied into "dst".
362 // RETURNS the non-nullptr buffer pointer upon successful extraction of
363 // all the requested bytes, or nullptr when the data is not available in the
364 // buffer due to being out of bounds, or insufficient data.
365 //----------------------------------------------------------------------
366 void *DataExtractor::GetU8(offset_t *offset_ptr, void *dst,
367 uint32_t count) const {
368 const uint8_t *data = (const uint8_t *)GetData(offset_ptr, count);
370 // Copy the data into the buffer
371 memcpy(dst, data, count);
372 // Return a non-nullptr pointer to the converted data as an indicator of
379 //----------------------------------------------------------------------
380 // Extract a single uint16_t from the data and update the offset pointed to by
383 // RETURNS the uint16_t that was extracted, or zero on failure.
384 //----------------------------------------------------------------------
385 uint16_t DataExtractor::GetU16(offset_t *offset_ptr) const {
387 const uint8_t *data = (const uint8_t *)GetData(offset_ptr, sizeof(val));
389 if (m_byte_order != endian::InlHostByteOrder())
390 val = ReadSwapInt16(data);
392 val = ReadInt16(data);
397 uint16_t DataExtractor::GetU16_unchecked(offset_t *offset_ptr) const {
399 if (m_byte_order == endian::InlHostByteOrder())
400 val = ReadInt16(m_start, *offset_ptr);
402 val = ReadSwapInt16(m_start, *offset_ptr);
403 *offset_ptr += sizeof(val);
407 uint32_t DataExtractor::GetU32_unchecked(offset_t *offset_ptr) const {
409 if (m_byte_order == endian::InlHostByteOrder())
410 val = ReadInt32(m_start, *offset_ptr);
412 val = ReadSwapInt32(m_start, *offset_ptr);
413 *offset_ptr += sizeof(val);
417 uint64_t DataExtractor::GetU64_unchecked(offset_t *offset_ptr) const {
419 if (m_byte_order == endian::InlHostByteOrder())
420 val = ReadInt64(m_start, *offset_ptr);
422 val = ReadSwapInt64(m_start, *offset_ptr);
423 *offset_ptr += sizeof(val);
427 //----------------------------------------------------------------------
428 // Extract "count" uint16_t values from the binary data and update the offset
429 // pointed to by "offset_ptr". The extracted data is copied into "dst".
431 // RETURNS the non-nullptr buffer pointer upon successful extraction of
432 // all the requested bytes, or nullptr when the data is not available in the
433 // buffer due to being out of bounds, or insufficient data.
434 //----------------------------------------------------------------------
435 void *DataExtractor::GetU16(offset_t *offset_ptr, void *void_dst,
436 uint32_t count) const {
437 const size_t src_size = sizeof(uint16_t) * count;
438 const uint16_t *src = (const uint16_t *)GetData(offset_ptr, src_size);
440 if (m_byte_order != endian::InlHostByteOrder()) {
441 uint16_t *dst_pos = (uint16_t *)void_dst;
442 uint16_t *dst_end = dst_pos + count;
443 const uint16_t *src_pos = src;
444 while (dst_pos < dst_end) {
445 *dst_pos = ReadSwapInt16(src_pos);
450 memcpy(void_dst, src, src_size);
452 // Return a non-nullptr pointer to the converted data as an indicator of
459 //----------------------------------------------------------------------
460 // Extract a single uint32_t from the data and update the offset pointed to by
463 // RETURNS the uint32_t that was extracted, or zero on failure.
464 //----------------------------------------------------------------------
465 uint32_t DataExtractor::GetU32(offset_t *offset_ptr) const {
467 const uint8_t *data = (const uint8_t *)GetData(offset_ptr, sizeof(val));
469 if (m_byte_order != endian::InlHostByteOrder()) {
470 val = ReadSwapInt32(data);
472 memcpy(&val, data, 4);
478 //----------------------------------------------------------------------
479 // Extract "count" uint32_t values from the binary data and update the offset
480 // pointed to by "offset_ptr". The extracted data is copied into "dst".
482 // RETURNS the non-nullptr buffer pointer upon successful extraction of
483 // all the requested bytes, or nullptr when the data is not available in the
484 // buffer due to being out of bounds, or insufficient data.
485 //----------------------------------------------------------------------
486 void *DataExtractor::GetU32(offset_t *offset_ptr, void *void_dst,
487 uint32_t count) const {
488 const size_t src_size = sizeof(uint32_t) * count;
489 const uint32_t *src = (const uint32_t *)GetData(offset_ptr, src_size);
491 if (m_byte_order != endian::InlHostByteOrder()) {
492 uint32_t *dst_pos = (uint32_t *)void_dst;
493 uint32_t *dst_end = dst_pos + count;
494 const uint32_t *src_pos = src;
495 while (dst_pos < dst_end) {
496 *dst_pos = ReadSwapInt32(src_pos);
501 memcpy(void_dst, src, src_size);
503 // Return a non-nullptr pointer to the converted data as an indicator of
510 //----------------------------------------------------------------------
511 // Extract a single uint64_t from the data and update the offset pointed to by
514 // RETURNS the uint64_t that was extracted, or zero on failure.
515 //----------------------------------------------------------------------
516 uint64_t DataExtractor::GetU64(offset_t *offset_ptr) const {
518 const uint8_t *data = (const uint8_t *)GetData(offset_ptr, sizeof(val));
520 if (m_byte_order != endian::InlHostByteOrder()) {
521 val = ReadSwapInt64(data);
523 memcpy(&val, data, 8);
529 //----------------------------------------------------------------------
532 // Get multiple consecutive 64 bit values. Return true if the entire read
533 // succeeds and increment the offset pointed to by offset_ptr, else return
534 // false and leave the offset pointed to by offset_ptr unchanged.
535 //----------------------------------------------------------------------
536 void *DataExtractor::GetU64(offset_t *offset_ptr, void *void_dst,
537 uint32_t count) const {
538 const size_t src_size = sizeof(uint64_t) * count;
539 const uint64_t *src = (const uint64_t *)GetData(offset_ptr, src_size);
541 if (m_byte_order != endian::InlHostByteOrder()) {
542 uint64_t *dst_pos = (uint64_t *)void_dst;
543 uint64_t *dst_end = dst_pos + count;
544 const uint64_t *src_pos = src;
545 while (dst_pos < dst_end) {
546 *dst_pos = ReadSwapInt64(src_pos);
551 memcpy(void_dst, src, src_size);
553 // Return a non-nullptr pointer to the converted data as an indicator of
560 uint32_t DataExtractor::GetMaxU32(offset_t *offset_ptr,
561 size_t byte_size) const {
562 lldbassert(byte_size > 0 && byte_size <= 4 && "GetMaxU32 invalid byte_size!");
563 return GetMaxU64(offset_ptr, byte_size);
566 uint64_t DataExtractor::GetMaxU64(offset_t *offset_ptr,
567 size_t byte_size) const {
568 lldbassert(byte_size > 0 && byte_size <= 8 && "GetMaxU64 invalid byte_size!");
571 return GetU8(offset_ptr);
573 return GetU16(offset_ptr);
575 return GetU32(offset_ptr);
577 return GetU64(offset_ptr);
580 const uint8_t *data =
581 static_cast<const uint8_t *>(GetData(offset_ptr, byte_size));
584 return ReadMaxInt64(data, byte_size, m_byte_order);
590 uint64_t DataExtractor::GetMaxU64_unchecked(offset_t *offset_ptr,
591 size_t byte_size) const {
594 return GetU8_unchecked(offset_ptr);
596 return GetU16_unchecked(offset_ptr);
598 return GetU32_unchecked(offset_ptr);
600 return GetU64_unchecked(offset_ptr);
602 uint64_t res = ReadMaxInt64(&m_start[*offset_ptr], byte_size, m_byte_order);
603 *offset_ptr += byte_size;
610 int64_t DataExtractor::GetMaxS64(offset_t *offset_ptr, size_t byte_size) const {
611 uint64_t u64 = GetMaxU64(offset_ptr, byte_size);
612 return llvm::SignExtend64(u64, 8 * byte_size);
615 uint64_t DataExtractor::GetMaxU64Bitfield(offset_t *offset_ptr, size_t size,
616 uint32_t bitfield_bit_size,
617 uint32_t bitfield_bit_offset) const {
618 uint64_t uval64 = GetMaxU64(offset_ptr, size);
619 if (bitfield_bit_size > 0) {
620 int32_t lsbcount = bitfield_bit_offset;
621 if (m_byte_order == eByteOrderBig)
622 lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
625 uint64_t bitfield_mask = ((1ul << bitfield_bit_size) - 1);
626 if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64)
628 uval64 &= bitfield_mask;
633 int64_t DataExtractor::GetMaxS64Bitfield(offset_t *offset_ptr, size_t size,
634 uint32_t bitfield_bit_size,
635 uint32_t bitfield_bit_offset) const {
636 int64_t sval64 = GetMaxS64(offset_ptr, size);
637 if (bitfield_bit_size > 0) {
638 int32_t lsbcount = bitfield_bit_offset;
639 if (m_byte_order == eByteOrderBig)
640 lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
643 uint64_t bitfield_mask = (((uint64_t)1) << bitfield_bit_size) - 1;
644 sval64 &= bitfield_mask;
645 // sign extend if needed
646 if (sval64 & (((uint64_t)1) << (bitfield_bit_size - 1)))
647 sval64 |= ~bitfield_mask;
652 float DataExtractor::GetFloat(offset_t *offset_ptr) const {
653 typedef float float_type;
654 float_type val = 0.0;
655 const size_t src_size = sizeof(float_type);
656 const float_type *src = (const float_type *)GetData(offset_ptr, src_size);
658 if (m_byte_order != endian::InlHostByteOrder()) {
659 const uint8_t *src_data = (const uint8_t *)src;
660 uint8_t *dst_data = (uint8_t *)&val;
661 for (size_t i = 0; i < sizeof(float_type); ++i)
662 dst_data[sizeof(float_type) - 1 - i] = src_data[i];
670 double DataExtractor::GetDouble(offset_t *offset_ptr) const {
671 typedef double float_type;
672 float_type val = 0.0;
673 const size_t src_size = sizeof(float_type);
674 const float_type *src = (const float_type *)GetData(offset_ptr, src_size);
676 if (m_byte_order != endian::InlHostByteOrder()) {
677 const uint8_t *src_data = (const uint8_t *)src;
678 uint8_t *dst_data = (uint8_t *)&val;
679 for (size_t i = 0; i < sizeof(float_type); ++i)
680 dst_data[sizeof(float_type) - 1 - i] = src_data[i];
688 long double DataExtractor::GetLongDouble(offset_t *offset_ptr) const {
689 long double val = 0.0;
690 #if defined(__i386__) || defined(__amd64__) || defined(__x86_64__) || \
691 defined(_M_IX86) || defined(_M_IA64) || defined(_M_X64)
692 *offset_ptr += CopyByteOrderedData(*offset_ptr, 10, &val, sizeof(val),
693 endian::InlHostByteOrder());
695 *offset_ptr += CopyByteOrderedData(*offset_ptr, sizeof(val), &val,
696 sizeof(val), endian::InlHostByteOrder());
701 //------------------------------------------------------------------
702 // Extract a single address from the data and update the offset pointed to by
703 // "offset_ptr". The size of the extracted address comes from the
704 // "this->m_addr_size" member variable and should be set correctly prior to
705 // extracting any address values.
707 // RETURNS the address that was extracted, or zero on failure.
708 //------------------------------------------------------------------
709 uint64_t DataExtractor::GetAddress(offset_t *offset_ptr) const {
710 #ifdef LLDB_CONFIGURATION_DEBUG
711 assert(m_addr_size == 4 || m_addr_size == 8);
713 return GetMaxU64(offset_ptr, m_addr_size);
716 uint64_t DataExtractor::GetAddress_unchecked(offset_t *offset_ptr) const {
717 #ifdef LLDB_CONFIGURATION_DEBUG
718 assert(m_addr_size == 4 || m_addr_size == 8);
720 return GetMaxU64_unchecked(offset_ptr, m_addr_size);
723 //------------------------------------------------------------------
724 // Extract a single pointer from the data and update the offset pointed to by
725 // "offset_ptr". The size of the extracted pointer comes from the
726 // "this->m_addr_size" member variable and should be set correctly prior to
727 // extracting any pointer values.
729 // RETURNS the pointer that was extracted, or zero on failure.
730 //------------------------------------------------------------------
731 uint64_t DataExtractor::GetPointer(offset_t *offset_ptr) const {
732 #ifdef LLDB_CONFIGURATION_DEBUG
733 assert(m_addr_size == 4 || m_addr_size == 8);
735 return GetMaxU64(offset_ptr, m_addr_size);
738 size_t DataExtractor::ExtractBytes(offset_t offset, offset_t length,
739 ByteOrder dst_byte_order, void *dst) const {
740 const uint8_t *src = PeekData(offset, length);
742 if (dst_byte_order != GetByteOrder()) {
743 // Validate that only a word- or register-sized dst is byte swapped
744 assert(length == 1 || length == 2 || length == 4 || length == 8 ||
745 length == 10 || length == 16 || length == 32);
747 for (uint32_t i = 0; i < length; ++i)
748 ((uint8_t *)dst)[i] = src[length - i - 1];
750 ::memcpy(dst, src, length);
756 // Extract data as it exists in target memory
757 lldb::offset_t DataExtractor::CopyData(offset_t offset, offset_t length,
759 const uint8_t *src = PeekData(offset, length);
761 ::memcpy(dst, src, length);
767 // Extract data and swap if needed when doing the copy
769 DataExtractor::CopyByteOrderedData(offset_t src_offset, offset_t src_len,
770 void *dst_void_ptr, offset_t dst_len,
771 ByteOrder dst_byte_order) const {
772 // Validate the source info
773 if (!ValidOffsetForDataOfSize(src_offset, src_len))
774 assert(ValidOffsetForDataOfSize(src_offset, src_len));
776 assert(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle);
778 // Validate the destination info
779 assert(dst_void_ptr != nullptr);
781 assert(dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle);
783 // Validate that only a word- or register-sized dst is byte swapped
784 assert(dst_byte_order == m_byte_order || dst_len == 1 || dst_len == 2 ||
785 dst_len == 4 || dst_len == 8 || dst_len == 10 || dst_len == 16 ||
788 // Must have valid byte orders set in this object and for destination
789 if (!(dst_byte_order == eByteOrderBig ||
790 dst_byte_order == eByteOrderLittle) ||
791 !(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle))
794 uint8_t *dst = (uint8_t *)dst_void_ptr;
795 const uint8_t *src = (const uint8_t *)PeekData(src_offset, src_len);
797 if (dst_len >= src_len) {
798 // We are copying the entire value from src into dst. Calculate how many,
799 // if any, zeroes we need for the most significant bytes if "dst_len" is
800 // greater than "src_len"...
801 const size_t num_zeroes = dst_len - src_len;
802 if (dst_byte_order == eByteOrderBig) {
803 // Big endian, so we lead with zeroes...
805 ::memset(dst, 0, num_zeroes);
806 // Then either copy or swap the rest
807 if (m_byte_order == eByteOrderBig) {
808 ::memcpy(dst + num_zeroes, src, src_len);
810 for (uint32_t i = 0; i < src_len; ++i)
811 dst[i + num_zeroes] = src[src_len - 1 - i];
814 // Little endian destination, so we lead the value bytes
815 if (m_byte_order == eByteOrderBig) {
816 for (uint32_t i = 0; i < src_len; ++i)
817 dst[i] = src[src_len - 1 - i];
819 ::memcpy(dst, src, src_len);
821 // And zero the rest...
823 ::memset(dst + src_len, 0, num_zeroes);
827 // We are only copying some of the value from src into dst..
829 if (dst_byte_order == eByteOrderBig) {
831 if (m_byte_order == eByteOrderBig) {
832 // Big endian dst, with big endian src
833 ::memcpy(dst, src + (src_len - dst_len), dst_len);
835 // Big endian dst, with little endian src
836 for (uint32_t i = 0; i < dst_len; ++i)
837 dst[i] = src[dst_len - 1 - i];
841 if (m_byte_order == eByteOrderBig) {
842 // Little endian dst, with big endian src
843 for (uint32_t i = 0; i < dst_len; ++i)
844 dst[i] = src[src_len - 1 - i];
846 // Little endian dst, with big endian src
847 ::memcpy(dst, src, dst_len);
856 //----------------------------------------------------------------------
857 // Extracts a variable length NULL terminated C string from the data at the
858 // offset pointed to by "offset_ptr". The "offset_ptr" will be updated with
859 // the offset of the byte that follows the NULL terminator byte.
861 // If the offset pointed to by "offset_ptr" is out of bounds, or if "length" is
862 // non-zero and there aren't enough available bytes, nullptr will be returned
863 // and "offset_ptr" will not be updated.
864 //----------------------------------------------------------------------
865 const char *DataExtractor::GetCStr(offset_t *offset_ptr) const {
866 const char *cstr = (const char *)PeekData(*offset_ptr, 1);
868 const char *cstr_end = cstr;
869 const char *end = (const char *)m_end;
870 while (cstr_end < end && *cstr_end)
873 // Now we are either at the end of the data or we point to the
874 // NULL C string terminator with cstr_end...
875 if (*cstr_end == '\0') {
876 // Advance the offset with one extra byte for the NULL terminator
877 *offset_ptr += (cstr_end - cstr + 1);
881 // We reached the end of the data without finding a NULL C string
882 // terminator. Fall through and return nullptr otherwise anyone that would
883 // have used the result as a C string can wander into unknown memory...
888 //----------------------------------------------------------------------
889 // Extracts a NULL terminated C string from the fixed length field of length
890 // "len" at the offset pointed to by "offset_ptr". The "offset_ptr" will be
891 // updated with the offset of the byte that follows the fixed length field.
893 // If the offset pointed to by "offset_ptr" is out of bounds, or if the offset
894 // plus the length of the field is out of bounds, or if the field does not
895 // contain a NULL terminator byte, nullptr will be returned and "offset_ptr"
896 // will not be updated.
897 //----------------------------------------------------------------------
898 const char *DataExtractor::GetCStr(offset_t *offset_ptr, offset_t len) const {
899 const char *cstr = (const char *)PeekData(*offset_ptr, len);
900 if (cstr != nullptr) {
901 if (memchr(cstr, '\0', len) == nullptr) {
910 //------------------------------------------------------------------
911 // Peeks at a string in the contained data. No verification is done to make
912 // sure the entire string lies within the bounds of this object's data, only
913 // "offset" is verified to be a valid offset.
915 // Returns a valid C string pointer if "offset" is a valid offset in this
916 // object's data, else nullptr is returned.
917 //------------------------------------------------------------------
918 const char *DataExtractor::PeekCStr(offset_t offset) const {
919 return (const char *)PeekData(offset, 1);
922 //----------------------------------------------------------------------
923 // Extracts an unsigned LEB128 number from this object's data starting at the
924 // offset pointed to by "offset_ptr". The offset pointed to by "offset_ptr"
925 // will be updated with the offset of the byte following the last extracted
928 // Returned the extracted integer value.
929 //----------------------------------------------------------------------
930 uint64_t DataExtractor::GetULEB128(offset_t *offset_ptr) const {
931 const uint8_t *src = (const uint8_t *)PeekData(*offset_ptr, 1);
935 const uint8_t *end = m_end;
938 uint64_t result = *src++;
939 if (result >= 0x80) {
943 uint8_t byte = *src++;
944 result |= (uint64_t)(byte & 0x7f) << shift;
945 if ((byte & 0x80) == 0)
950 *offset_ptr = src - m_start;
957 //----------------------------------------------------------------------
958 // Extracts an signed LEB128 number from this object's data starting at the
959 // offset pointed to by "offset_ptr". The offset pointed to by "offset_ptr"
960 // will be updated with the offset of the byte following the last extracted
963 // Returned the extracted integer value.
964 //----------------------------------------------------------------------
965 int64_t DataExtractor::GetSLEB128(offset_t *offset_ptr) const {
966 const uint8_t *src = (const uint8_t *)PeekData(*offset_ptr, 1);
970 const uint8_t *end = m_end;
975 int size = sizeof(int64_t) * 8;
983 result |= (int64_t)(byte & 0x7f) << shift;
985 if ((byte & 0x80) == 0)
989 // Sign bit of byte is 2nd high order bit (0x40)
990 if (shift < size && (byte & 0x40))
991 result |= -(1 << shift);
993 *offset_ptr += bytecount;
999 //----------------------------------------------------------------------
1000 // Skips a ULEB128 number (signed or unsigned) from this object's data starting
1001 // at the offset pointed to by "offset_ptr". The offset pointed to by
1002 // "offset_ptr" will be updated with the offset of the byte following the last
1005 // Returns the number of bytes consumed during the extraction.
1006 //----------------------------------------------------------------------
1007 uint32_t DataExtractor::Skip_LEB128(offset_t *offset_ptr) const {
1008 uint32_t bytes_consumed = 0;
1009 const uint8_t *src = (const uint8_t *)PeekData(*offset_ptr, 1);
1013 const uint8_t *end = m_end;
1016 const uint8_t *src_pos = src;
1017 while ((src_pos < end) && (*src_pos++ & 0x80))
1019 *offset_ptr += src_pos - src;
1021 return bytes_consumed;
1024 //----------------------------------------------------------------------
1025 // Dumps bytes from this object's data to the stream "s" starting
1026 // "start_offset" bytes into this data, and ending with the byte before
1027 // "end_offset". "base_addr" will be added to the offset into the dumped data
1028 // when showing the offset into the data in the output information.
1029 // "num_per_line" objects of type "type" will be dumped with the option to
1030 // override the format for each object with "type_format". "type_format" is a
1031 // printf style formatting string. If "type_format" is nullptr, then an
1032 // appropriate format string will be used for the supplied "type". If the
1033 // stream "s" is nullptr, then the output will be send to Log().
1034 //----------------------------------------------------------------------
1035 lldb::offset_t DataExtractor::PutToLog(Log *log, offset_t start_offset,
1036 offset_t length, uint64_t base_addr,
1037 uint32_t num_per_line,
1038 DataExtractor::Type type,
1039 const char *format) const {
1041 return start_offset;
1044 offset_t end_offset;
1047 for (offset = start_offset, end_offset = offset + length, count = 0;
1048 ValidOffset(offset) && offset < end_offset; ++count) {
1049 if ((count % num_per_line) == 0) {
1050 // Print out any previous string
1051 if (sstr.GetSize() > 0) {
1052 log->PutString(sstr.GetString());
1055 // Reset string offset and fill the current line string with address:
1056 if (base_addr != LLDB_INVALID_ADDRESS)
1057 sstr.Printf("0x%8.8" PRIx64 ":",
1058 (uint64_t)(base_addr + (offset - start_offset)));
1063 sstr.Printf(format ? format : " %2.2x", GetU8(&offset));
1066 char ch = GetU8(&offset);
1067 sstr.Printf(format ? format : " %c", isprint(ch) ? ch : ' ');
1070 sstr.Printf(format ? format : " %4.4x", GetU16(&offset));
1073 sstr.Printf(format ? format : " %8.8x", GetU32(&offset));
1076 sstr.Printf(format ? format : " %16.16" PRIx64, GetU64(&offset));
1079 sstr.Printf(format ? format : " 0x%" PRIx64, GetAddress(&offset));
1082 sstr.Printf(format ? format : " 0x%" PRIx64, GetULEB128(&offset));
1085 sstr.Printf(format ? format : " %" PRId64, GetSLEB128(&offset));
1091 log->PutString(sstr.GetString());
1093 return offset; // Return the offset at which we ended up
1096 size_t DataExtractor::Copy(DataExtractor &dest_data) const {
1098 // we can pass along the SP to the data
1099 dest_data.SetData(m_data_sp);
1101 const uint8_t *base_ptr = m_start;
1102 size_t data_size = GetByteSize();
1103 dest_data.SetData(DataBufferSP(new DataBufferHeap(base_ptr, data_size)));
1105 return GetByteSize();
1108 bool DataExtractor::Append(DataExtractor &rhs) {
1109 if (rhs.GetByteOrder() != GetByteOrder())
1112 if (rhs.GetByteSize() == 0)
1115 if (GetByteSize() == 0)
1116 return (rhs.Copy(*this) > 0);
1118 size_t bytes = GetByteSize() + rhs.GetByteSize();
1120 DataBufferHeap *buffer_heap_ptr = nullptr;
1121 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
1123 if (!buffer_sp || buffer_heap_ptr == nullptr)
1126 uint8_t *bytes_ptr = buffer_heap_ptr->GetBytes();
1128 memcpy(bytes_ptr, GetDataStart(), GetByteSize());
1129 memcpy(bytes_ptr + GetByteSize(), rhs.GetDataStart(), rhs.GetByteSize());
1136 bool DataExtractor::Append(void *buf, offset_t length) {
1143 size_t bytes = GetByteSize() + length;
1145 DataBufferHeap *buffer_heap_ptr = nullptr;
1146 DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
1148 if (!buffer_sp || buffer_heap_ptr == nullptr)
1151 uint8_t *bytes_ptr = buffer_heap_ptr->GetBytes();
1153 if (GetByteSize() > 0)
1154 memcpy(bytes_ptr, GetDataStart(), GetByteSize());
1156 memcpy(bytes_ptr + GetByteSize(), buf, length);
1163 void DataExtractor::Checksum(llvm::SmallVectorImpl<uint8_t> &dest,
1164 uint64_t max_data) {
1166 max_data = GetByteSize();
1168 max_data = std::min(max_data, GetByteSize());
1172 const llvm::ArrayRef<uint8_t> data(GetDataStart(), max_data);
1175 llvm::MD5::MD5Result result;
1179 dest.append(result.Bytes.begin(), result.Bytes.end());