24#include "llvm/ADT/ArrayRef.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/ADT/StringExtras.h"
27#include "llvm/Support/LEB128.h"
28#include "llvm/Support/MD5.h"
29#include "llvm/Support/MathExtras.h"
46 memcpy(&value, ptr + offset, 2);
50static inline uint32_t
ReadInt32(
const unsigned char *ptr,
53 memcpy(&value, ptr + offset, 4);
57static inline uint64_t
ReadInt64(
const unsigned char *ptr,
60 memcpy(&value, ptr + offset, 8);
66 memcpy(&value, ptr, 2);
73 memcpy(&value, ptr + offset, 2);
74 return llvm::byteswap<uint16_t>(value);
80 memcpy(&value, ptr + offset, 4);
81 return llvm::byteswap<uint32_t>(value);
87 memcpy(&value, ptr + offset, 8);
88 return llvm::byteswap<uint64_t>(value);
93 memcpy(&value, ptr, 2);
94 return llvm::byteswap<uint16_t>(value);
99 memcpy(&value, ptr, 4);
100 return llvm::byteswap<uint32_t>(value);
105 memcpy(&value, ptr, 8);
106 return llvm::byteswap<uint64_t>(value);
109static inline uint64_t
ReadMaxInt64(
const uint8_t *data,
size_t byte_size,
113 for (
size_t i = 0; i < byte_size; ++i)
114 res = (res << 8) | data[i];
117 for (
size_t i = 0; i < byte_size; ++i)
118 res = (res << 8) | data[byte_size - 1 - i];
124 : m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)),
131 uint32_t target_byte_size )
132 : m_start(const_cast<uint8_t *>(static_cast<const uint8_t *>(data))),
133 m_end(const_cast<uint8_t *>(static_cast<const uint8_t *>(data)) + length),
134 m_byte_order(endian), m_addr_size(addr_size), m_data_sp(),
135 m_target_byte_size(target_byte_size) {
136 assert(addr_size >= 1 && addr_size <= 8);
145 uint32_t target_byte_size )
146 : m_byte_order(endian), m_addr_size(addr_size), m_data_sp(),
147 m_target_byte_size(target_byte_size) {
148 assert(addr_size >= 1 && addr_size <= 8);
158 offset_t length, uint32_t target_byte_size )
159 : m_byte_order(data.m_byte_order), m_addr_size(data.m_addr_size),
160 m_data_sp(), m_target_byte_size(target_byte_size) {
164 if (length > bytes_available)
165 length = bytes_available;
171 : m_start(rhs.m_start), m_end(rhs.m_end), m_byte_order(rhs.m_byte_order),
172 m_addr_size(rhs.m_addr_size), m_data_sp(rhs.m_data_sp),
173 m_target_byte_size(rhs.m_target_byte_size) {
206 if (data !=
nullptr) {
207 const uint8_t *data_bytes = data->
GetBytes();
208 if (data_bytes !=
nullptr) {
228 if (bytes ==
nullptr || length == 0) {
232 m_start =
const_cast<uint8_t *
>(
static_cast<const uint8_t *
>(bytes));
262 if (data_length > data.
GetByteSize() - data_offset)
286 if (data_length > 0) {
289 const size_t data_size = data_sp->GetByteSize();
290 if (data_offset < data_size) {
291 m_start = data_sp->GetBytes() + data_offset;
292 const size_t bytes_left = data_size - data_offset;
294 if (data_length <= bytes_left)
318 const uint8_t *data =
static_cast<const uint8_t *
>(
GetData(offset_ptr, 1));
331 uint32_t count)
const {
332 const uint8_t *data =
333 static_cast<const uint8_t *
>(
GetData(offset_ptr, count));
336 memcpy(dst, data, count);
350 const uint8_t *data =
351 static_cast<const uint8_t *
>(
GetData(offset_ptr,
sizeof(val)));
367 *offset_ptr +=
sizeof(val);
377 *offset_ptr +=
sizeof(val);
387 *offset_ptr +=
sizeof(val);
398 uint32_t count)
const {
399 const size_t src_size =
sizeof(uint16_t) * count;
400 const uint16_t *src =
401 static_cast<const uint16_t *
>(
GetData(offset_ptr, src_size));
404 uint16_t *dst_pos =
static_cast<uint16_t *
>(void_dst);
405 uint16_t *dst_end = dst_pos + count;
406 const uint16_t *src_pos = src;
407 while (dst_pos < dst_end) {
413 memcpy(void_dst, src, src_size);
428 const uint8_t *data =
429 static_cast<const uint8_t *
>(
GetData(offset_ptr,
sizeof(val)));
434 memcpy(&val, data, 4);
447 uint32_t count)
const {
448 const size_t src_size =
sizeof(uint32_t) * count;
449 const uint32_t *src =
450 static_cast<const uint32_t *
>(
GetData(offset_ptr, src_size));
453 uint32_t *dst_pos =
static_cast<uint32_t *
>(void_dst);
454 uint32_t *dst_end = dst_pos + count;
455 const uint32_t *src_pos = src;
456 while (dst_pos < dst_end) {
462 memcpy(void_dst, src, src_size);
477 const uint8_t *data =
478 static_cast<const uint8_t *
>(
GetData(offset_ptr,
sizeof(val)));
483 memcpy(&val, data, 8);
495 uint32_t count)
const {
496 const size_t src_size =
sizeof(uint64_t) * count;
497 const uint64_t *src =
498 static_cast<const uint64_t *
>(
GetData(offset_ptr, src_size));
501 uint64_t *dst_pos =
static_cast<uint64_t *
>(void_dst);
502 uint64_t *dst_end = dst_pos + count;
503 const uint64_t *src_pos = src;
504 while (dst_pos < dst_end) {
510 memcpy(void_dst, src, src_size);
520 size_t byte_size)
const {
521 lldbassert(byte_size > 0 && byte_size <= 4 &&
"GetMaxU32 invalid byte_size!");
526 size_t byte_size)
const {
527 lldbassert(byte_size > 0 && byte_size <= 8 &&
"GetMaxU64 invalid byte_size!");
530 return GetU8(offset_ptr);
532 return GetU16(offset_ptr);
534 return GetU32(offset_ptr);
536 return GetU64(offset_ptr);
539 const uint8_t *data =
540 static_cast<const uint8_t *
>(
GetData(offset_ptr, byte_size));
550 size_t byte_size)
const {
562 *offset_ptr += byte_size;
570 uint64_t u64 =
GetMaxU64(offset_ptr, byte_size);
571 return llvm::SignExtend64(u64, 8 * byte_size);
575 uint32_t bitfield_bit_size,
576 uint32_t bitfield_bit_offset)
const {
577 assert(bitfield_bit_size <= 64);
578 uint64_t uval64 =
GetMaxU64(offset_ptr, size);
580 if (bitfield_bit_size == 0)
583 int32_t lsbcount = bitfield_bit_offset;
585 lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
590 uint64_t bitfield_mask =
591 (bitfield_bit_size == 64
592 ? std::numeric_limits<uint64_t>::max()
593 : ((
static_cast<uint64_t
>(1) << bitfield_bit_size) - 1));
594 if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64)
597 uval64 &= bitfield_mask;
603 uint32_t bitfield_bit_size,
604 uint32_t bitfield_bit_offset)
const {
605 assert(size >= 1 &&
"GetMaxS64Bitfield size must be >= 1");
606 assert(size <= 8 &&
"GetMaxS64Bitfield size must be <= 8");
607 int64_t sval64 =
GetMaxS64(offset_ptr, size);
608 if (bitfield_bit_size == 0)
610 int32_t lsbcount = bitfield_bit_offset;
612 lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
615 uint64_t bitfield_mask = llvm::maskTrailingOnes<uint64_t>(bitfield_bit_size);
616 sval64 &= bitfield_mask;
618 if (sval64 & ((
static_cast<uint64_t
>(1)) << (bitfield_bit_size - 1)))
619 sval64 |= ~bitfield_mask;
624 return Get<float>(offset_ptr, 0.0f);
628 return Get<double>(offset_ptr, 0.0);
632 long double val = 0.0;
633#if defined(__i386__) || defined(__amd64__) || defined(__x86_64__) || \
634 defined(_M_IX86) || defined(_M_IA64) || defined(_M_X64)
661 ByteOrder dst_byte_order,
void *dst)
const {
662 const uint8_t *src =
PeekData(offset, length);
666 assert(length == 1 || length == 2 || length == 4 || length == 8 ||
667 length == 10 || length == 16 || length == 32);
669 for (uint32_t i = 0; i < length; ++i)
670 (
static_cast<uint8_t *
>(dst))[i] = src[length - i - 1];
672 ::memcpy(dst, src, length);
681 const uint8_t *src =
PeekData(offset, length);
683 ::memcpy(dst, src, length);
692 void *dst_void_ptr,
offset_t dst_len,
701 assert(dst_void_ptr !=
nullptr);
706 assert(dst_byte_order ==
m_byte_order || dst_len == 1 || dst_len == 2 ||
707 dst_len == 4 || dst_len == 8 || dst_len == 10 || dst_len == 16 ||
716 uint8_t *dst =
static_cast<uint8_t *
>(dst_void_ptr);
717 const uint8_t *src =
PeekData(src_offset, src_len);
719 if (dst_len >= src_len) {
723 const size_t num_zeroes = dst_len - src_len;
727 ::memset(dst, 0, num_zeroes);
730 ::memcpy(dst + num_zeroes, src, src_len);
732 for (uint32_t i = 0; i < src_len; ++i)
733 dst[i + num_zeroes] = src[src_len - 1 - i];
738 for (uint32_t i = 0; i < src_len; ++i)
739 dst[i] = src[src_len - 1 - i];
741 ::memcpy(dst, src, src_len);
745 ::memset(dst + src_len, 0, num_zeroes);
755 ::memcpy(dst, src + (src_len - dst_len), dst_len);
758 for (uint32_t i = 0; i < dst_len; ++i)
759 dst[i] = src[dst_len - 1 - i];
765 for (uint32_t i = 0; i < dst_len; ++i)
766 dst[i] = src[src_len - 1 - i];
769 ::memcpy(dst, src, dst_len);
786 const char *start =
reinterpret_cast<const char *
>(
PeekData(*offset_ptr, 1));
791 const char *end =
reinterpret_cast<const char *
>(
m_end);
794 const char *terminator_or_end = std::find(start, end,
'\0');
798 if (terminator_or_end == end)
803 *offset_ptr += (terminator_or_end - start + 1UL);
816 const char *cstr =
reinterpret_cast<const char *
>(
PeekData(*offset_ptr, len));
817 if (cstr !=
nullptr) {
818 if (memchr(cstr,
'\0', len) ==
nullptr) {
834 return reinterpret_cast<const char *
>(
PeekData(offset, 1));
844 const uint8_t *src =
PeekData(*offset_ptr, 1);
848 unsigned byte_count = 0;
849 uint64_t result = llvm::decodeULEB128(src, &byte_count,
m_end);
850 *offset_ptr += byte_count;
861 const uint8_t *src =
PeekData(*offset_ptr, 1);
865 unsigned byte_count = 0;
866 int64_t result = llvm::decodeSLEB128(src, &byte_count,
m_end);
867 *offset_ptr += byte_count;
878 uint32_t bytes_consumed = 0;
879 const uint8_t *src =
PeekData(*offset_ptr, 1);
883 const uint8_t *end =
m_end;
886 const uint8_t *src_pos = src;
887 while ((src_pos < end) && (*src_pos++ & 0x80))
889 *offset_ptr += src_pos - src;
891 return bytes_consumed;
904 offset_t length, uint64_t base_addr,
905 uint32_t num_per_line,
914 for (offset = start_offset, end_offset = offset + length, count = 0;
915 ValidOffset(offset) && offset < end_offset; ++count) {
916 if ((count % num_per_line) == 0) {
924 sstr.
Printf(
"0x%8.8" PRIx64
":",
925 static_cast<uint64_t
>(base_addr + (offset - start_offset)));
933 char ch =
GetU8(&offset);
934 sstr.
Printf(
" %c", llvm::isPrint(ch) ? ch :
' ');
968 const uint8_t *base_ptr =
m_start;
983 return (rhs.
Copy(*
this) > 0);
990 if (!buffer_sp || buffer_heap_ptr ==
nullptr)
993 uint8_t *bytes_ptr = buffer_heap_ptr->
GetBytes();
1015 if (!buffer_sp || buffer_heap_ptr ==
nullptr)
1018 uint8_t *bytes_ptr = buffer_heap_ptr->
GetBytes();
1031 uint64_t max_data) {
1039 const llvm::ArrayRef<uint8_t> data(
GetDataStart(), max_data);
1042 llvm::MD5::MD5Result result;
1046 dest.append(result.begin(), result.end());
A subclass of DataBuffer that stores a data buffer on the heap.
A pure virtual protocol class for abstracted read only data buffers.
const uint8_t * GetBytes() const
Get a const pointer to the data.
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.
uint8_t * GetBytes()
Get a pointer to the data.
#define LLDB_INVALID_ADDRESS
lldb::ByteOrder InlHostByteOrder()
A class that represents a running process on the host machine.
ByteOrder
Byte ordering definitions.
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP