24#include "llvm/ADT/ArrayRef.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/Support/LEB128.h"
27#include "llvm/Support/MD5.h"
28#include "llvm/Support/MathExtras.h"
45 memcpy(&value, ptr + offset, 2);
52 memcpy(&value, ptr + offset, 4);
56static inline uint64_t
ReadInt64(
const unsigned char *ptr,
59 memcpy(&value, ptr + offset, 8);
65 memcpy(&value, ptr, 2);
72 memcpy(&value, ptr + offset, 2);
73 return llvm::byteswap<uint16_t>(value);
79 memcpy(&value, ptr + offset, 4);
80 return llvm::byteswap<uint32_t>(value);
86 memcpy(&value, ptr + offset, 8);
87 return llvm::byteswap<uint64_t>(value);
92 memcpy(&value, ptr, 2);
93 return llvm::byteswap<uint16_t>(value);
98 memcpy(&value, ptr, 4);
99 return llvm::byteswap<uint32_t>(value);
104 memcpy(&value, ptr, 8);
105 return llvm::byteswap<uint64_t>(value);
108static inline uint64_t
ReadMaxInt64(
const uint8_t *data,
size_t byte_size,
112 for (
size_t i = 0; i < byte_size; ++i)
113 res = (res << 8) | data[i];
116 for (
size_t i = 0; i < byte_size; ++i)
117 res = (res << 8) | data[byte_size - 1 - i];
123 : m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)),
131 : m_start(const_cast<uint8_t *>(static_cast<const uint8_t *>(data))),
132 m_end(const_cast<uint8_t *>(static_cast<const uint8_t *>(data)) + length),
133 m_byte_order(endian), m_addr_size(addr_size), m_data_sp(),
134 m_target_byte_size(target_byte_size) {
135 assert(addr_size >= 1 && addr_size <= 8);
145 : m_byte_order(endian), m_addr_size(addr_size), m_data_sp(),
146 m_target_byte_size(target_byte_size) {
147 assert(addr_size >= 1 && addr_size <= 8);
158 : m_byte_order(data.m_byte_order), m_addr_size(data.m_addr_size),
159 m_data_sp(), m_target_byte_size(target_byte_size) {
163 if (length > bytes_available)
164 length = bytes_available;
170 : m_start(rhs.m_start), m_end(rhs.m_end), m_byte_order(rhs.m_byte_order),
171 m_addr_size(rhs.m_addr_size), m_data_sp(rhs.m_data_sp),
172 m_target_byte_size(rhs.m_target_byte_size) {
205 if (data !=
nullptr) {
206 const uint8_t *data_bytes = data->GetBytes();
207 if (data_bytes !=
nullptr) {
227 if (bytes ==
nullptr || length == 0) {
231 m_start =
const_cast<uint8_t *
>(
static_cast<const uint8_t *
>(bytes));
261 if (data_length > data.
GetByteSize() - data_offset)
285 if (data_length > 0) {
288 const size_t data_size = data_sp->GetByteSize();
289 if (data_offset < data_size) {
290 m_start = data_sp->GetBytes() + data_offset;
291 const size_t bytes_left = data_size - data_offset;
293 if (data_length <= bytes_left)
317 const uint8_t *data =
static_cast<const uint8_t *
>(
GetData(offset_ptr, 1));
331 const uint8_t *data =
332 static_cast<const uint8_t *
>(
GetData(offset_ptr, count));
335 memcpy(dst, data, count);
349 const uint8_t *data =
350 static_cast<const uint8_t *
>(
GetData(offset_ptr,
sizeof(val)));
366 *offset_ptr +=
sizeof(val);
376 *offset_ptr +=
sizeof(val);
386 *offset_ptr +=
sizeof(val);
398 const size_t src_size =
sizeof(
uint16_t) * count;
404 uint16_t *dst_end = dst_pos + count;
406 while (dst_pos < dst_end) {
412 memcpy(void_dst, src, src_size);
427 const uint8_t *data =
428 static_cast<const uint8_t *
>(
GetData(offset_ptr,
sizeof(val)));
433 memcpy(&val, data, 4);
447 const size_t src_size =
sizeof(
uint32_t) * count;
453 uint32_t *dst_end = dst_pos + count;
455 while (dst_pos < dst_end) {
461 memcpy(void_dst, src, src_size);
476 const uint8_t *data =
477 static_cast<const uint8_t *
>(
GetData(offset_ptr,
sizeof(val)));
482 memcpy(&val, data, 8);
495 const size_t src_size =
sizeof(uint64_t) * count;
496 const uint64_t *src =
497 static_cast<const uint64_t *
>(
GetData(offset_ptr, src_size));
500 uint64_t *dst_pos =
static_cast<uint64_t *
>(void_dst);
501 uint64_t *dst_end = dst_pos + count;
502 const uint64_t *src_pos = src;
503 while (dst_pos < dst_end) {
509 memcpy(void_dst, src, src_size);
519 size_t byte_size)
const {
520 lldbassert(byte_size > 0 && byte_size <= 4 &&
"GetMaxU32 invalid byte_size!");
525 size_t byte_size)
const {
526 lldbassert(byte_size > 0 && byte_size <= 8 &&
"GetMaxU64 invalid byte_size!");
529 return GetU8(offset_ptr);
531 return GetU16(offset_ptr);
533 return GetU32(offset_ptr);
535 return GetU64(offset_ptr);
538 const uint8_t *data =
539 static_cast<const uint8_t *
>(
GetData(offset_ptr, byte_size));
549 size_t byte_size)
const {
561 *offset_ptr += byte_size;
569 uint64_t u64 =
GetMaxU64(offset_ptr, byte_size);
570 return llvm::SignExtend64(u64, 8 * byte_size);
575 uint32_t bitfield_bit_offset)
const {
576 assert(bitfield_bit_size <= 64);
577 uint64_t uval64 =
GetMaxU64(offset_ptr, size);
579 if (bitfield_bit_size == 0)
582 int32_t lsbcount = bitfield_bit_offset;
584 lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
589 uint64_t bitfield_mask =
590 (bitfield_bit_size == 64
591 ? std::numeric_limits<uint64_t>::max()
592 : ((
static_cast<uint64_t
>(1) << bitfield_bit_size) - 1));
593 if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64)
596 uval64 &= bitfield_mask;
603 uint32_t bitfield_bit_offset)
const {
604 assert(size >= 1 &&
"GetMaxS64Bitfield size must be >= 1");
605 assert(size <= 8 &&
"GetMaxS64Bitfield size must be <= 8");
606 int64_t sval64 =
GetMaxS64(offset_ptr, size);
607 if (bitfield_bit_size == 0)
609 int32_t lsbcount = bitfield_bit_offset;
611 lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
614 uint64_t bitfield_mask = llvm::maskTrailingOnes<uint64_t>(bitfield_bit_size);
615 sval64 &= bitfield_mask;
617 if (sval64 & ((
static_cast<uint64_t
>(1)) << (bitfield_bit_size - 1)))
618 sval64 |= ~bitfield_mask;
623 return Get<float>(offset_ptr, 0.0f);
627 return Get<double>(offset_ptr, 0.0);
631 long double val = 0.0;
632#if defined(__i386__) || defined(__amd64__) || defined(__x86_64__) || \
633 defined(_M_IX86) || defined(_M_IA64) || defined(_M_X64)
660 ByteOrder dst_byte_order,
void *dst)
const {
661 const uint8_t *src =
PeekData(offset, length);
665 assert(length == 1 || length == 2 || length == 4 || length == 8 ||
666 length == 10 || length == 16 || length == 32);
668 for (
uint32_t i = 0; i < length; ++i)
669 (
static_cast<uint8_t *
>(dst))[i] = src[length - i - 1];
671 ::memcpy(dst, src, length);
680 const uint8_t *src =
PeekData(offset, length);
682 ::memcpy(dst, src, length);
691 void *dst_void_ptr,
offset_t dst_len,
700 assert(dst_void_ptr !=
nullptr);
705 assert(dst_byte_order ==
m_byte_order || dst_len == 1 || dst_len == 2 ||
706 dst_len == 4 || dst_len == 8 || dst_len == 10 || dst_len == 16 ||
715 uint8_t *dst =
static_cast<uint8_t *
>(dst_void_ptr);
716 const uint8_t *src =
PeekData(src_offset, src_len);
718 if (dst_len >= src_len) {
722 const size_t num_zeroes = dst_len - src_len;
726 ::memset(dst, 0, num_zeroes);
729 ::memcpy(dst + num_zeroes, src, src_len);
731 for (
uint32_t i = 0; i < src_len; ++i)
732 dst[i + num_zeroes] = src[src_len - 1 - i];
737 for (
uint32_t i = 0; i < src_len; ++i)
738 dst[i] = src[src_len - 1 - i];
740 ::memcpy(dst, src, src_len);
744 ::memset(dst + src_len, 0, num_zeroes);
754 ::memcpy(dst, src + (src_len - dst_len), dst_len);
757 for (
uint32_t i = 0; i < dst_len; ++i)
758 dst[i] = src[dst_len - 1 - i];
764 for (
uint32_t i = 0; i < dst_len; ++i)
765 dst[i] = src[src_len - 1 - i];
768 ::memcpy(dst, src, dst_len);
785 const char *start =
reinterpret_cast<const char *
>(
PeekData(*offset_ptr, 1));
790 const char *end =
reinterpret_cast<const char *
>(
m_end);
793 const char *terminator_or_end = std::find(start, end,
'\0');
797 if (terminator_or_end == end)
802 *offset_ptr += (terminator_or_end - start + 1UL);
815 const char *cstr =
reinterpret_cast<const char *
>(
PeekData(*offset_ptr, len));
816 if (cstr !=
nullptr) {
817 if (memchr(cstr,
'\0', len) ==
nullptr) {
833 return reinterpret_cast<const char *
>(
PeekData(offset, 1));
843 const uint8_t *src =
PeekData(*offset_ptr, 1);
847 unsigned byte_count = 0;
848 uint64_t result = llvm::decodeULEB128(src, &byte_count,
m_end);
849 *offset_ptr += byte_count;
860 const uint8_t *src =
PeekData(*offset_ptr, 1);
864 unsigned byte_count = 0;
865 int64_t result = llvm::decodeSLEB128(src, &byte_count,
m_end);
866 *offset_ptr += byte_count;
878 const uint8_t *src =
PeekData(*offset_ptr, 1);
882 const uint8_t *end =
m_end;
885 const uint8_t *src_pos = src;
886 while ((src_pos < end) && (*src_pos++ & 0x80))
888 *offset_ptr += src_pos - src;
890 return bytes_consumed;
903 offset_t length, uint64_t base_addr,
913 for (offset = start_offset, end_offset = offset + length, count = 0;
914 ValidOffset(offset) && offset < end_offset; ++count) {
915 if ((count % num_per_line) == 0) {
923 sstr.
Printf(
"0x%8.8" PRIx64
":",
924 static_cast<uint64_t
>(base_addr + (offset - start_offset)));
932 char ch =
GetU8(&offset);
933 sstr.
Printf(
" %c", llvm::isPrint(ch) ? ch :
' ');
967 const uint8_t *base_ptr =
m_start;
982 return (rhs.
Copy(*
this) > 0);
987 DataBufferSP buffer_sp(buffer_heap_ptr =
new DataBufferHeap(bytes, 0));
989 if (!buffer_sp || buffer_heap_ptr ==
nullptr)
992 uint8_t *bytes_ptr = buffer_heap_ptr->GetBytes();
1012 DataBufferSP buffer_sp(buffer_heap_ptr =
new DataBufferHeap(bytes, 0));
1014 if (!buffer_sp || buffer_heap_ptr ==
nullptr)
1017 uint8_t *bytes_ptr = buffer_heap_ptr->GetBytes();
1030 uint64_t max_data) {
1038 const llvm::ArrayRef<uint8_t> data(
GetDataStart(), max_data);
1041 llvm::MD5::MD5Result result;
1045 dest.append(result.begin(), result.end());
A subclass of DataBuffer that stores a data buffer on the heap.
void PutString(llvm::StringRef str)
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
#define LLDB_INVALID_ADDRESS
lldb::ByteOrder InlHostByteOrder()
A class that represents a running process on the host machine.
ByteOrder
Byte ordering definitions.