#include <assert.h>
#include <stddef.h>
#include <bitset>
#include <string>
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/MathExtras.h"
#include "lldb/Core/DataExtractor.h"
#include "lldb/Core/DataBuffer.h"
#include "lldb/Core/Log.h"
#include "lldb/Core/Stream.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Core/UUID.h"
#include "lldb/Core/dwarf.h"
#include "lldb/Host/Endian.h"
using namespace lldb;
using namespace lldb_private;
static inline uint16_t
ReadInt16(const unsigned char* ptr, unsigned offset)
{
return *(uint16_t *)(ptr + offset);
}
static inline uint32_t
ReadInt32 (const unsigned char* ptr, unsigned offset)
{
return *(uint32_t *)(ptr + offset);
}
static inline uint64_t
ReadInt64(const unsigned char* ptr, unsigned offset)
{
return *(uint64_t *)(ptr + offset);
}
static inline uint16_t
ReadSwapInt16(const unsigned char* ptr, unsigned offset)
{
return llvm::ByteSwap_16(*(uint16_t *)(ptr + offset));
}
static inline uint32_t
ReadSwapInt32 (const unsigned char* ptr, unsigned offset)
{
return llvm::ByteSwap_32(*(uint32_t *)(ptr + offset));
}
static inline uint64_t
ReadSwapInt64(const unsigned char* ptr, unsigned offset)
{
return llvm::ByteSwap_64(*(uint64_t *)(ptr + offset));
}
#define NON_PRINTABLE_CHAR '.'
DataExtractor::DataExtractor () :
m_start (NULL),
m_end (NULL),
m_byte_order(lldb::endian::InlHostByteOrder()),
m_addr_size (4),
m_data_sp ()
{
}
DataExtractor::DataExtractor (const void* data, uint32_t length, ByteOrder endian, uint8_t addr_size) :
m_start ((uint8_t*)data),
m_end ((uint8_t*)data + length),
m_byte_order(endian),
m_addr_size (addr_size),
m_data_sp ()
{
}
DataExtractor::DataExtractor (const DataBufferSP& data_sp, ByteOrder endian, uint8_t addr_size) :
m_start (NULL),
m_end (NULL),
m_byte_order(endian),
m_addr_size (addr_size),
m_data_sp ()
{
SetData (data_sp);
}
DataExtractor::DataExtractor (const DataExtractor& data, uint32_t offset, uint32_t length) :
m_start(NULL),
m_end(NULL),
m_byte_order(data.m_byte_order),
m_addr_size(data.m_addr_size),
m_data_sp()
{
if (data.ValidOffset(offset))
{
uint32_t bytes_available = data.GetByteSize() - offset;
if (length > bytes_available)
length = bytes_available;
SetData(data, offset, length);
}
}
DataExtractor::DataExtractor (const DataExtractor& rhs) :
m_start (rhs.m_start),
m_end (rhs.m_end),
m_byte_order (rhs.m_byte_order),
m_addr_size (rhs.m_addr_size),
m_data_sp (rhs.m_data_sp)
{
}
const DataExtractor&
DataExtractor::operator= (const DataExtractor& rhs)
{
if (this != &rhs)
{
m_start = rhs.m_start;
m_end = rhs.m_end;
m_byte_order = rhs.m_byte_order;
m_addr_size = rhs.m_addr_size;
m_data_sp = rhs.m_data_sp;
}
return *this;
}
DataExtractor::~DataExtractor ()
{
}
void
DataExtractor::Clear ()
{
m_start = NULL;
m_end = NULL;
m_byte_order = lldb::endian::InlHostByteOrder();
m_addr_size = 4;
m_data_sp.reset();
}
size_t
DataExtractor::GetByteSize () const
{
return m_end - m_start;
}
size_t
DataExtractor::GetSharedDataOffset () const
{
if (m_start != NULL)
{
const DataBuffer * data = m_data_sp.get();
if (data != NULL)
{
const uint8_t * data_bytes = data->GetBytes();
if (data_bytes != NULL)
{
assert(m_start >= data_bytes);
return m_start - data_bytes;
}
}
}
return 0;
}
bool
DataExtractor::ValidOffset (uint32_t offset) const
{
return offset < GetByteSize();
}
bool
DataExtractor::ValidOffsetForDataOfSize (uint32_t offset, uint32_t length) const
{
size_t size = GetByteSize();
if (offset >= size)
return false;
if (length == 0)
return true;
if (length > ~offset)
return false;
return ((offset + length) <= size);
}
const uint8_t *
DataExtractor::GetDataStart () const
{
return m_start;
}
const uint8_t *
DataExtractor::GetDataEnd () const
{
return m_end;
}
ByteOrder
DataExtractor::GetByteOrder () const
{
return m_byte_order;
}
void
DataExtractor::SetByteOrder (ByteOrder endian)
{
m_byte_order = endian;
}
uint8_t
DataExtractor::GetAddressByteSize () const
{
return m_addr_size;
}
void
DataExtractor::SetAddressByteSize (uint8_t addr_size)
{
m_addr_size = addr_size;
}
uint32_t
DataExtractor::SetData (const void *bytes, uint32_t length, ByteOrder endian)
{
m_byte_order = endian;
m_data_sp.reset();
if (bytes == NULL || length == 0)
{
m_start = NULL;
m_end = NULL;
}
else
{
m_start = (uint8_t *)bytes;
m_end = m_start + length;
}
return GetByteSize();
}
uint32_t
DataExtractor::SetData (const DataExtractor& data, uint32_t data_offset, uint32_t data_length)
{
m_addr_size = data.m_addr_size;
if (data.m_data_sp.get())
{
m_byte_order = data.m_byte_order;
return SetData(data.m_data_sp, data.GetSharedDataOffset() + data_offset, data_length);
}
if (data.ValidOffset(data_offset))
{
if (data_length > data.GetByteSize() - data_offset)
data_length = data.GetByteSize() - data_offset;
return SetData (data.GetDataStart() + data_offset, data_length, data.GetByteOrder());
}
return 0;
}
uint32_t
DataExtractor::SetData (const DataBufferSP& data_sp, uint32_t data_offset, uint32_t data_length)
{
m_start = m_end = NULL;
if (data_length > 0)
{
m_data_sp = data_sp;
if (data_sp.get())
{
const size_t data_size = data_sp->GetByteSize();
if (data_offset < data_size)
{
m_start = data_sp->GetBytes() + data_offset;
const size_t bytes_left = data_size - data_offset;
if (data_length <= bytes_left)
m_end = m_start + data_length; else
m_end = m_start + bytes_left; }
}
}
uint32_t new_size = GetByteSize();
if (new_size == 0)
m_data_sp.reset();
return new_size;
}
uint8_t
DataExtractor::GetU8 (uint32_t *offset_ptr) const
{
uint8_t val = 0;
if ( m_start < m_end )
{
val = m_start[*offset_ptr];
*offset_ptr += sizeof(val);
}
return val;
}
void *
DataExtractor::GetU8 (uint32_t *offset_ptr, void *dst, uint32_t count) const
{
register uint32_t offset = *offset_ptr;
if ((count > 0) && ValidOffsetForDataOfSize(offset, count) )
{
memcpy (dst, m_start + offset, count);
*offset_ptr += count;
return dst;
}
return NULL;
}
uint16_t
DataExtractor::GetU16 (uint32_t *offset_ptr) const
{
uint16_t val = 0;
register uint32_t offset = *offset_ptr;
if ( ValidOffsetForDataOfSize(offset, sizeof(val)) )
{
if (m_byte_order != lldb::endian::InlHostByteOrder())
val = ReadSwapInt16(m_start, offset);
else
val = ReadInt16 (m_start, offset);
*offset_ptr += sizeof(val);
}
return val;
}
uint16_t
DataExtractor::GetU16_unchecked (uint32_t *offset_ptr) const
{
uint16_t val = (m_byte_order == lldb::endian::InlHostByteOrder()) ?
ReadInt16 (m_start, *offset_ptr) :
ReadSwapInt16(m_start, *offset_ptr);
*offset_ptr += sizeof(val);
return val;
}
uint32_t
DataExtractor::GetU32_unchecked (uint32_t *offset_ptr) const
{
uint32_t val = (m_byte_order == lldb::endian::InlHostByteOrder()) ?
ReadInt32 (m_start, *offset_ptr) :
ReadSwapInt32 (m_start, *offset_ptr);
*offset_ptr += sizeof(val);
return val;
}
uint64_t
DataExtractor::GetU64_unchecked (uint32_t *offset_ptr) const
{
uint64_t val = (m_byte_order == lldb::endian::InlHostByteOrder()) ?
ReadInt64 (m_start, *offset_ptr) :
ReadSwapInt64 (m_start, *offset_ptr);
*offset_ptr += sizeof(val);
return val;
}
void *
DataExtractor::GetU16 (uint32_t *offset_ptr, void *void_dst, uint32_t count) const
{
uint16_t *dst = (uint16_t *)void_dst;
const size_t value_size = sizeof(*dst);
register uint32_t offset = *offset_ptr;
if ((count > 0) && ValidOffsetForDataOfSize(offset, value_size * count) )
{
uint16_t *value_ptr;
uint16_t *end = dst + count;
if (m_byte_order != lldb::endian::InlHostByteOrder())
{
for (value_ptr = dst; value_ptr < end; ++value_ptr, offset += value_size)
*value_ptr = ReadSwapInt16 (m_start, offset);
}
else
{
for (value_ptr = dst; value_ptr < end; ++value_ptr, offset += value_size)
*value_ptr = ReadInt16 (m_start, offset);
}
*offset_ptr = offset;
return dst;
}
return NULL;
}
uint32_t
DataExtractor::GetU32 (uint32_t *offset_ptr) const
{
uint32_t val = 0;
register uint32_t offset = *offset_ptr;
if ( ValidOffsetForDataOfSize(offset, sizeof(val)) )
{
if (m_byte_order != lldb::endian::InlHostByteOrder())
val = ReadSwapInt32 (m_start, offset);
else
val = ReadInt32 (m_start, offset);
*offset_ptr += sizeof(val);
}
return val;
}
void *
DataExtractor::GetU32 (uint32_t *offset_ptr, void *void_dst, uint32_t count) const
{
uint32_t *dst = (uint32_t *)void_dst;
const size_t value_size = sizeof(*dst);
register uint32_t offset = *offset_ptr;
if ((count > 0) && ValidOffsetForDataOfSize(offset, value_size * count))
{
uint32_t *value_ptr;
uint32_t *end = dst + count;
if (m_byte_order != lldb::endian::InlHostByteOrder())
{
for (value_ptr = dst; value_ptr < end; ++value_ptr, offset += value_size)
*value_ptr = ReadSwapInt32 (m_start, offset);
}
else
{
for (value_ptr = dst; value_ptr < end; ++value_ptr, offset += value_size)
*value_ptr = ReadInt32 (m_start, offset);
}
*offset_ptr = offset;
return dst;
}
return NULL;
}
uint64_t
DataExtractor::GetU64 (uint32_t *offset_ptr) const
{
uint64_t val = 0;
register uint32_t offset = *offset_ptr;
if ( ValidOffsetForDataOfSize(offset, sizeof(val)) )
{
if (m_byte_order != lldb::endian::InlHostByteOrder())
val = ReadSwapInt64 (m_start, offset);
else
val = ReadInt64 (m_start, offset);
*offset_ptr += sizeof(val);
}
return val;
}
void *
DataExtractor::GetU64 (uint32_t *offset_ptr, void *void_dst, uint32_t count) const
{
uint64_t *dst = (uint64_t *)void_dst;
const size_t value_size = sizeof(uint64_t);
register uint32_t offset = *offset_ptr;
if ((count > 0) && ValidOffsetForDataOfSize(offset, value_size * count))
{
uint64_t *value_ptr;
uint64_t *end = dst + count;
if (m_byte_order != lldb::endian::InlHostByteOrder())
{
for (value_ptr = dst; value_ptr < end; ++value_ptr, offset += value_size)
*value_ptr = ReadSwapInt64 (m_start, offset);
}
else
{
for (value_ptr = dst; value_ptr < end; ++value_ptr, offset += value_size)
*value_ptr = ReadInt64 (m_start, offset);
}
*offset_ptr = offset;
return dst;
}
return NULL;
}
uint32_t
DataExtractor::GetMaxU32 (uint32_t *offset_ptr, uint32_t byte_size) const
{
switch (byte_size)
{
case 1: return GetU8 (offset_ptr); break;
case 2: return GetU16(offset_ptr); break;
case 4: return GetU32(offset_ptr); break;
default:
assert(!"GetMaxU32 unhandled case!");
break;
}
return 0;
}
uint64_t
DataExtractor::GetMaxU64 (uint32_t *offset_ptr, uint32_t size) const
{
switch (size)
{
case 1: return GetU8 (offset_ptr); break;
case 2: return GetU16(offset_ptr); break;
case 4: return GetU32(offset_ptr); break;
case 8: return GetU64(offset_ptr); break;
default:
assert(!"GetMax64 unhandled case!");
break;
}
return 0;
}
int64_t
DataExtractor::GetMaxS64 (uint32_t *offset_ptr, uint32_t size) const
{
switch (size)
{
case 1: return (int8_t)GetU8 (offset_ptr); break;
case 2: return (int16_t)GetU16(offset_ptr); break;
case 4: return (int32_t)GetU32(offset_ptr); break;
case 8: return (int64_t)GetU64(offset_ptr); break;
default:
assert(!"GetMax64 unhandled case!");
break;
}
return 0;
}
uint64_t
DataExtractor::GetMaxU64Bitfield (uint32_t *offset_ptr, uint32_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
{
uint64_t uval64 = GetMaxU64 (offset_ptr, size);
if (bitfield_bit_size > 0)
{
if (bitfield_bit_offset > 0)
uval64 >>= bitfield_bit_offset;
uint64_t bitfield_mask = ((1ul << bitfield_bit_size) - 1);
if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64)
return uval64;
uval64 &= bitfield_mask;
}
return uval64;
}
int64_t
DataExtractor::GetMaxS64Bitfield (uint32_t *offset_ptr, uint32_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
{
int64_t sval64 = GetMaxS64 (offset_ptr, size);
if (bitfield_bit_size > 0)
{
if (bitfield_bit_offset > 0)
sval64 >>= bitfield_bit_offset;
uint64_t bitfield_mask = ((1 << bitfield_bit_size) - 1);
sval64 &= bitfield_mask;
if (sval64 & (1 << (bitfield_bit_size - 1)))
sval64 |= ~bitfield_mask;
}
return sval64;
}
float
DataExtractor::GetFloat (uint32_t *offset_ptr) const
{
typedef float float_type;
float_type val = 0.0;
const uint8_t *src_data = PeekData (*offset_ptr, sizeof(float_type));
if (src_data)
{
if (m_byte_order != lldb::endian::InlHostByteOrder())
{
uint8_t *dst_data = (uint8_t *)&val;
for (int i=0; i<sizeof(float_type); ++i)
dst_data[sizeof(float_type) - 1 - i] = src_data[i];
}
else
{
::memcpy (&val, src_data, sizeof (float_type));
}
*offset_ptr += sizeof(val);
}
return val;
}
double
DataExtractor::GetDouble (uint32_t *offset_ptr) const
{
typedef double float_type;
float_type val = 0.0;
const uint8_t *src_data = PeekData (*offset_ptr, sizeof(float_type));
if (src_data)
{
if (m_byte_order != lldb::endian::InlHostByteOrder())
{
uint8_t *dst_data = (uint8_t *)&val;
for (int i=0; i<sizeof(float_type); ++i)
dst_data[sizeof(float_type) - 1 - i] = src_data[i];
}
else
{
::memcpy (&val, src_data, sizeof (float_type));
}
*offset_ptr += sizeof(val);
}
return val;
}
long double
DataExtractor::GetLongDouble (uint32_t *offset_ptr) const
{
typedef long double float_type;
float_type val = 0.0;
const uint8_t *src_data = PeekData (*offset_ptr, sizeof(float_type));
if (src_data)
{
if (m_byte_order != lldb::endian::InlHostByteOrder())
{
uint8_t *dst_data = (uint8_t *)&val;
for (int i=0; i<sizeof(float_type); ++i)
dst_data[sizeof(float_type) - 1 - i] = src_data[i];
}
else
{
::memcpy (&val, src_data, sizeof (float_type));
}
*offset_ptr += sizeof(val);
}
return val;
}
uint64_t
DataExtractor::GetAddress (uint32_t *offset_ptr) const
{
return GetMaxU64 (offset_ptr, m_addr_size);
}
uint64_t
DataExtractor::GetPointer (uint32_t *offset_ptr) const
{
return GetMaxU64 (offset_ptr, m_addr_size);
}
uint64_t
DataExtractor::GetGNUEHPointer (uint32_t *offset_ptr, uint32_t eh_ptr_enc, lldb::addr_t pc_rel_addr, lldb::addr_t text_addr, lldb::addr_t data_addr){
if (eh_ptr_enc == DW_EH_PE_omit)
return ULLONG_MAX;
uint64_t baseAddress = 0;
uint64_t addressValue = 0;
const uint32_t addr_size = GetAddressByteSize();
bool signExtendValue = false;
switch (eh_ptr_enc & 0x70)
{
case DW_EH_PE_pcrel:
signExtendValue = true;
baseAddress = *offset_ptr;
if (pc_rel_addr != LLDB_INVALID_ADDRESS)
baseAddress += pc_rel_addr;
break;
case DW_EH_PE_textrel:
signExtendValue = true;
if (text_addr != LLDB_INVALID_ADDRESS)
baseAddress = text_addr;
break;
case DW_EH_PE_datarel:
signExtendValue = true;
if (data_addr != LLDB_INVALID_ADDRESS)
baseAddress = data_addr;
break;
case DW_EH_PE_funcrel:
signExtendValue = true;
break;
case DW_EH_PE_aligned:
{
assert(addr_size != 0);
if (addr_size)
{
uint32_t alignOffset = *offset_ptr % addr_size;
if (alignOffset)
offset_ptr += addr_size - alignOffset;
}
}
break;
default:
break;
}
switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING)
{
case DW_EH_PE_absptr :
{
addressValue = GetAddress (offset_ptr);
}
break;
case DW_EH_PE_uleb128 : addressValue = GetULEB128(offset_ptr); break;
case DW_EH_PE_udata2 : addressValue = GetU16(offset_ptr); break;
case DW_EH_PE_udata4 : addressValue = GetU32(offset_ptr); break;
case DW_EH_PE_udata8 : addressValue = GetU64(offset_ptr); break;
case DW_EH_PE_sleb128 : addressValue = GetSLEB128(offset_ptr); break;
case DW_EH_PE_sdata2 : addressValue = (int16_t)GetU16(offset_ptr); break;
case DW_EH_PE_sdata4 : addressValue = (int32_t)GetU32(offset_ptr); break;
case DW_EH_PE_sdata8 : addressValue = (int64_t)GetU64(offset_ptr); break;
default:
assert(eh_ptr_enc);
break;
}
if (signExtendValue && addr_size < sizeof(baseAddress))
{
uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull);
if (sign_bit & addressValue)
{
uint64_t mask = ~sign_bit + 1;
addressValue |= mask;
}
}
return baseAddress + addressValue;
}
size_t
DataExtractor::ExtractBytes (uint32_t offset, uint32_t length, ByteOrder dst_byte_order, void *dst) const
{
const uint8_t *src = PeekData (offset, length);
if (src)
{
if (dst_byte_order != GetByteOrder())
{
for (uint32_t i=0; i<length; ++i)
((uint8_t*)dst)[i] = src[length - i - 1];
}
else
::memcpy (dst, src, length);
return length;
}
return 0;
}
const uint8_t*
DataExtractor::PeekData (uint32_t offset, uint32_t length) const
{
if ( length > 0 && ValidOffsetForDataOfSize(offset, length) )
return m_start + offset;
return NULL;
}
const void*
DataExtractor::GetData (uint32_t *offset_ptr, uint32_t length) const
{
const uint8_t* bytes = NULL;
register uint32_t offset = *offset_ptr;
if ( length > 0 && ValidOffsetForDataOfSize(offset, length) )
{
bytes = m_start + offset;
*offset_ptr = offset + length;
}
return bytes;
}
uint32_t
DataExtractor::CopyByteOrderedData (uint32_t src_offset,
uint32_t src_len,
void *dst_void_ptr,
uint32_t dst_len,
ByteOrder dst_byte_order) const
{
assert (ValidOffsetForDataOfSize(src_offset, src_len));
assert (src_len > 0);
assert (m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle);
assert (dst_void_ptr != NULL);
assert (dst_len > 0);
assert (dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle);
if (!(dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle) ||
!(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle))
return 0;
uint32_t i;
uint8_t* dst = (uint8_t*)dst_void_ptr;
const uint8_t* src = (const uint8_t *)PeekData (src_offset, src_len);
if (src)
{
if (dst_len >= src_len)
{
const uint32_t num_zeroes = dst_len - src_len;
if (dst_byte_order == eByteOrderBig)
{
if (num_zeroes > 0)
::memset (dst, 0, num_zeroes);
if (m_byte_order == eByteOrderBig)
{
::memcpy (dst + num_zeroes, src, src_len);
}
else
{
for (i=0; i<src_len; ++i)
dst[i+num_zeroes] = src[src_len - 1 - i];
}
}
else
{
if (m_byte_order == eByteOrderBig)
{
for (i=0; i<src_len; ++i)
dst[i] = src[src_len - 1 - i];
}
else
{
::memcpy (dst, src, src_len);
}
if (num_zeroes > 0)
::memset (dst + src_len, 0, num_zeroes);
}
return src_len;
}
else
{
if (dst_byte_order == eByteOrderBig)
{
if (m_byte_order == eByteOrderBig)
{
::memcpy (dst, src + (src_len - dst_len), dst_len);
}
else
{
for (i=0; i<dst_len; ++i)
dst[i] = src[dst_len - 1 - i];
}
}
else
{
if (m_byte_order == eByteOrderBig)
{
for (i=0; i<dst_len; ++i)
dst[i] = src[src_len - 1 - i];
}
else
{
::memcpy (dst, src, dst_len);
}
}
return dst_len;
}
}
return 0;
}
const char*
DataExtractor::GetCStr (uint32_t *offset_ptr) const
{
const char *s = NULL;
if ( m_start < m_end )
{
s = (char*)m_start + *offset_ptr;
size_t length = strlen(s) + 1;
if (!ValidOffsetForDataOfSize(*offset_ptr, length))
return NULL;
*offset_ptr += length;
}
return s;
}
const char *
DataExtractor::PeekCStr (uint32_t offset) const
{
if (ValidOffset (offset))
return (const char*)m_start + offset;
return NULL;
}
uint64_t
DataExtractor::GetULEB128 (uint32_t *offset_ptr) const
{
uint64_t result = 0;
if ( m_start < m_end )
{
int shift = 0;
const uint8_t *src = m_start + *offset_ptr;
uint8_t byte;
int bytecount = 0;
while (src < m_end)
{
bytecount++;
byte = *src++;
result |= (byte & 0x7f) << shift;
shift += 7;
if ((byte & 0x80) == 0)
break;
}
*offset_ptr += bytecount;
}
return result;
}
int64_t
DataExtractor::GetSLEB128 (uint32_t *offset_ptr) const
{
int64_t result = 0;
if ( m_start < m_end )
{
int shift = 0;
int size = sizeof (uint32_t) * 8;
const uint8_t *src = m_start + *offset_ptr;
uint8_t byte = 0;
int bytecount = 0;
while (src < m_end)
{
bytecount++;
byte = *src++;
result |= (byte & 0x7f) << shift;
shift += 7;
if ((byte & 0x80) == 0)
break;
}
if (shift < size && (byte & 0x40))
result |= - (1 << shift);
*offset_ptr += bytecount;
}
return result;
}
uint32_t
DataExtractor::Skip_LEB128 (uint32_t *offset_ptr) const
{
uint32_t bytes_consumed = 0;
if ( m_start < m_end )
{
const uint8_t *start = m_start + *offset_ptr;
const uint8_t *src = start;
while ((src < m_end) && (*src++ & 0x80))
++bytes_consumed;
*offset_ptr += src - start;
}
return bytes_consumed;
}
static uint32_t
DumpAPInt (Stream *s, const DataExtractor &data, uint32_t offset, uint32_t byte_size, bool is_signed, unsigned radix)
{
llvm::SmallVector<uint64_t, 2> uint64_array;
uint32_t bytes_left = byte_size;
uint64_t u64;
const lldb::ByteOrder byte_order = data.GetByteOrder();
if (byte_order == lldb::eByteOrderLittle)
{
while (bytes_left > 0)
{
if (bytes_left >= 8)
{
u64 = data.GetU64(&offset);
bytes_left -= 8;
}
else
{
u64 = data.GetMaxU64(&offset, bytes_left);
bytes_left = 0;
}
uint64_array.push_back(u64);
}
}
else if (byte_order == lldb::eByteOrderBig)
{
uint32_t be_offset = offset + byte_size;
uint32_t temp_offset;
while (bytes_left > 0)
{
if (bytes_left >= 8)
{
be_offset -= 8;
temp_offset = be_offset;
u64 = data.GetU64(&temp_offset);
bytes_left -= 8;
}
else
{
be_offset -= bytes_left;
temp_offset = be_offset;
u64 = data.GetMaxU64(&temp_offset, bytes_left);
bytes_left = 0;
}
uint64_array.push_back(u64);
}
}
else
return offset;
llvm::APInt apint (byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
std::string apint_str(apint.toString(radix, is_signed));
switch (radix)
{
case 2:
s->Write ("0b", 2);
break;
case 8:
s->Write ("0", 1);
break;
case 10:
break;
}
s->Write(apint_str.c_str(), apint_str.size());
return offset;
}
uint32_t
DataExtractor::Dump
(
Stream *s,
uint32_t start_offset,
lldb::Format item_format,
uint32_t item_byte_size,
uint32_t item_count,
uint32_t num_per_line,
uint64_t base_addr,
uint32_t item_bit_size, uint32_t item_bit_offset ) const
{
if (s == NULL)
return start_offset;
uint32_t offset;
uint32_t count;
uint32_t line_start_offset;
if (item_format == eFormatPointer)
{
if (item_byte_size != 4 && item_byte_size != 8)
item_byte_size = s->GetAddressByteSize();
}
if (item_format == eFormatOSType && item_byte_size > 8)
item_format = eFormatHex;
for (offset = start_offset, line_start_offset = start_offset, count = 0; ValidOffset(offset) && count < item_count; ++count)
{
if ((count % num_per_line) == 0)
{
if (count > 0)
{
if (item_format == eFormatBytesWithASCII && offset > line_start_offset)
{
s->Printf("%*s", (num_per_line - (offset - line_start_offset)) * 3 + 2, "");
Dump(s, line_start_offset, eFormatCharPrintable, 1, offset - line_start_offset, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
}
s->EOL();
}
if (base_addr != LLDB_INVALID_ADDRESS)
s->Printf ("0x%8.8llx: ", (uint64_t)(base_addr + (offset - start_offset)));
line_start_offset = offset;
}
else
if (item_format != eFormatChar &&
item_format != eFormatCharPrintable &&
item_format != eFormatCharArray &&
count > 0)
{
s->PutChar(' ');
}
uint32_t i;
switch (item_format)
{
case eFormatBoolean:
s->Printf ("%s", GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset) ? "true" : "false");
break;
case eFormatBinary:
if (item_byte_size <= 8)
{
uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
std::string binary_value(64, '0');
std::bitset<64> bits(uval64);
for (i = 0; i < 64; ++i)
if (bits[i])
binary_value[64 - 1 - i] = '1';
if (item_bit_size > 0)
s->Printf("0b%s", binary_value.c_str() + 64 - item_bit_size);
else if (item_byte_size > 0 && item_byte_size <= 8)
s->Printf("0b%s", binary_value.c_str() + 64 - item_byte_size * 8);
}
else
{
const bool is_signed = false;
const unsigned radix = 2;
offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
}
break;
case eFormatBytes:
case eFormatBytesWithASCII:
for (i=0; i<item_byte_size; ++i)
{
s->Printf ("%2.2x", GetU8(&offset));
}
if (item_byte_size > 1)
s->PutChar(' ');
break;
case eFormatChar:
case eFormatCharPrintable:
case eFormatCharArray:
{
if (item_count == 1 && item_format == eFormatChar)
s->PutChar('\'');
uint32_t ch = GetMaxU64(&offset, item_byte_size);
if (isprint(ch))
s->Printf ("%c", ch);
else if (item_format != eFormatCharPrintable)
{
switch (ch)
{
case '\e': s->Printf ("\\e", (uint8_t)ch); break;
case '\a': s->Printf ("\\a", ch); break;
case '\b': s->Printf ("\\b", ch); break;
case '\f': s->Printf ("\\f", ch); break;
case '\n': s->Printf ("\\n", ch); break;
case '\r': s->Printf ("\\r", ch); break;
case '\t': s->Printf ("\\t", ch); break;
case '\v': s->Printf ("\\v", ch); break;
case '\0': s->Printf ("\\0", ch); break;
default:
if (item_byte_size == 1)
s->Printf ("\\x%2.2x", ch);
else
s->Printf ("\\u%x", ch);
break;
}
}
else
{
s->PutChar(NON_PRINTABLE_CHAR);
}
if (item_count == 1 && item_format == eFormatChar)
s->PutChar('\'');
}
break;
case eFormatDecimal:
if (item_byte_size <= 8)
s->Printf ("%lld", GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
else
{
const bool is_signed = true;
const unsigned radix = 10;
offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
}
break;
case eFormatUnsigned:
if (item_byte_size <= 8)
s->Printf ("%llu", GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
else
{
const bool is_signed = false;
const unsigned radix = 10;
offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
}
break;
case eFormatOctal:
if (item_byte_size <= 8)
s->Printf ("0%llo", GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
else
{
const bool is_signed = false;
const unsigned radix = 8;
offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
}
break;
case eFormatOSType:
{
uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
s->PutChar('\'');
for (i=0; i<item_byte_size; ++i)
{
uint8_t ch;
if (i > 0)
ch = (uint8_t)(uval64 >> ((item_byte_size - i - 1) * 8));
else
ch = (uint8_t)uval64;
if (isprint(ch))
s->Printf ("%c", ch);
else
{
switch (ch)
{
case '\e': s->Printf ("\\e", (uint8_t)ch); break;
case '\a': s->Printf ("\\a", ch); break;
case '\b': s->Printf ("\\b", ch); break;
case '\f': s->Printf ("\\f", ch); break;
case '\n': s->Printf ("\\n", ch); break;
case '\r': s->Printf ("\\r", ch); break;
case '\t': s->Printf ("\\t", ch); break;
case '\v': s->Printf ("\\v", ch); break;
case '\0': s->Printf ("\\0", ch); break;
default: s->Printf ("\\x%2.2x", ch); break;
}
}
}
s->PutChar('\'');
}
break;
case eFormatEnum:
s->Printf ("%lld", GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
break;
case eFormatCString:
{
const char *cstr = GetCStr(&offset);
if (cstr)
s->Printf("\"%s\"", cstr);
else
{
s->Printf("NULL", cstr);
offset = UINT32_MAX;
}
}
break;
case eFormatPointer:
s->Address(GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset), sizeof (addr_t));
break;
case eFormatComplexInteger:
{
uint32_t complex_int_byte_size = item_byte_size / 2;
if (complex_int_byte_size <= 8)
{
s->Printf("%llu", GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
s->Printf(" + %llui", GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
}
}
break;
case eFormatComplex:
if (sizeof(float) * 2 == item_byte_size)
{
float f32_1 = GetFloat (&offset);
float f32_2 = GetFloat (&offset);
s->Printf ("%g + %gi", f32_1, f32_2);
break;
}
else if (sizeof(double) * 2 == item_byte_size)
{
double d64_1 = GetDouble (&offset);
double d64_2 = GetDouble (&offset);
s->Printf ("%lg + %lgi", d64_1, d64_2);
break;
}
else if (sizeof(long double) * 2 == item_byte_size)
{
long double ld64_1 = GetLongDouble (&offset);
long double ld64_2 = GetLongDouble (&offset);
s->Printf ("%Lg + %Lgi", ld64_1, ld64_2);
break;
}
else
{
s->Printf ("unsupported complex float byte size %u", item_byte_size);
return start_offset;
}
break;
default:
case eFormatDefault:
case eFormatHex:
if (item_byte_size <= 8)
{
s->Printf("0x%*.*llx", 2 * item_byte_size, 2 * item_byte_size, GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
}
else
{
assert (item_bit_size == 0 && item_bit_offset == 0);
s->PutCString("0x");
const uint8_t *bytes = (const uint8_t* )GetData(&offset, item_byte_size);
if (bytes)
{
uint32_t idx;
if (m_byte_order == eByteOrderBig)
{
for (idx = 0; idx < item_byte_size; ++idx)
s->Printf("%2.2x", bytes[idx]);
}
else
{
for (idx = 0; idx < item_byte_size; ++idx)
s->Printf("%2.2x", bytes[item_byte_size - 1 - idx]);
}
}
}
break;
case eFormatFloat:
if (sizeof(float) == item_byte_size)
{
s->Printf ("%g", GetFloat (&offset));
}
else if (sizeof(double) == item_byte_size)
{
s->Printf ("%lg", GetDouble(&offset));
}
else if (sizeof(long double) == item_byte_size)
{
s->Printf ("%Lg", GetLongDouble(&offset));
}
break;
case eFormatUnicode16:
s->Printf("0x%4.4x", GetU16 (&offset));
break;
case eFormatUnicode32:
s->Printf("0x%8.8x", GetU32 (&offset));
break;
case eFormatVectorOfChar: s->PutChar('{'); offset = Dump (s, start_offset, eFormatCharArray, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfSInt8:
s->PutChar('{');
offset = Dump (s, start_offset, eFormatDecimal, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfUInt8:
s->PutChar('{');
offset = Dump (s, start_offset, eFormatHex, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfSInt16:
s->PutChar('{');
offset = Dump (s, start_offset, eFormatDecimal, sizeof(uint16_t), item_byte_size / sizeof(uint16_t), item_byte_size / sizeof(uint16_t), LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfUInt16:
s->PutChar('{');
offset = Dump (s, start_offset, eFormatHex, sizeof(uint16_t), item_byte_size / sizeof(uint16_t), item_byte_size / sizeof(uint16_t), LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfSInt32:
s->PutChar('{');
offset = Dump (s, start_offset, eFormatDecimal, sizeof(uint32_t), item_byte_size / sizeof(uint32_t), item_byte_size / sizeof(uint32_t), LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfUInt32:
s->PutChar('{');
offset = Dump (s, start_offset, eFormatHex, sizeof(uint32_t), item_byte_size / sizeof(uint32_t), item_byte_size / sizeof(uint32_t), LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfSInt64:
s->PutChar('{');
offset = Dump (s, start_offset, eFormatDecimal, sizeof(uint64_t), item_byte_size / sizeof(uint64_t), item_byte_size / sizeof(uint64_t), LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfUInt64:
s->PutChar('{');
offset = Dump (s, start_offset, eFormatHex, sizeof(uint32_t), item_byte_size / sizeof(uint32_t), item_byte_size / sizeof(uint32_t), LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfFloat32:
s->PutChar('{');
offset = Dump (s, start_offset, eFormatFloat, 4, item_byte_size / 4, item_byte_size / 4, LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfFloat64:
s->PutChar('{');
offset = Dump (s, start_offset, eFormatFloat, 8, item_byte_size / 8, item_byte_size / 8, LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfUInt128:
s->PutChar('{');
offset = Dump (s, start_offset, eFormatHex, 16, item_byte_size / 16, item_byte_size / 16, LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
}
}
if (item_format == eFormatBytesWithASCII && offset > line_start_offset)
{
s->Printf("%*s", (num_per_line - (offset - line_start_offset)) * 3 + 2, "");
Dump(s, line_start_offset, eFormatCharPrintable, 1, offset - line_start_offset, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
}
return offset; }
uint32_t
DataExtractor::PutToLog
(
Log *log,
uint32_t start_offset,
uint32_t length,
uint64_t base_addr,
uint32_t num_per_line,
DataExtractor::Type type,
const char *format
) const
{
if (log == NULL)
return start_offset;
uint32_t offset;
uint32_t end_offset;
uint32_t count;
StreamString sstr;
for (offset = start_offset, end_offset = offset + length, count = 0; ValidOffset(offset) && offset < end_offset; ++count)
{
if ((count % num_per_line) == 0)
{
if (sstr.GetSize() > 0)
{
log->Printf("%s", sstr.GetData());
sstr.Clear();
}
if (base_addr != LLDB_INVALID_ADDRESS)
sstr.Printf("0x%8.8llx:", (uint64_t)(base_addr + (offset - start_offset)));
}
switch (type)
{
default:
case TypeUInt8: sstr.Printf (format ? format : " %2.2x", GetU8(&offset)); break;
case TypeChar:
{
char ch = GetU8(&offset);
sstr.Printf (format ? format : " %c", isprint(ch) ? ch : ' ');
}
break;
case TypeUInt16: sstr.Printf (format ? format : " %4.4x", GetU16(&offset)); break;
case TypeUInt32: sstr.Printf (format ? format : " %8.8x", GetU32(&offset)); break;
case TypeUInt64: sstr.Printf (format ? format : " %16.16llx", GetU64(&offset)); break;
case TypePointer: sstr.Printf (format ? format : " 0x%llx", GetAddress(&offset)); break;
case TypeULEB128: sstr.Printf (format ? format : " 0x%llx", GetULEB128(&offset)); break;
case TypeSLEB128: sstr.Printf (format ? format : " %lld", GetSLEB128(&offset)); break;
}
}
if (sstr.GetSize() > 0)
log->Printf("%s", sstr.GetData());
return offset; }
void
DataExtractor::DumpUUID (Stream *s, uint32_t offset) const
{
if (s)
{
const uint8_t *uuid_data = PeekData(offset, 16);
if ( uuid_data )
{
lldb_private::UUID uuid(uuid_data, 16);
uuid.Dump(s);
}
else
{
s->Printf("<not enough data for UUID at offset 0x%8.8x>", offset);
}
}
}
void
DataExtractor::DumpHexBytes (Stream *s,
const void *src,
size_t src_len,
uint32_t bytes_per_line,
addr_t base_addr)
{
DataExtractor data (src, src_len, eByteOrderLittle, 4);
data.Dump (s,
0, eFormatBytes, 1, src_len, bytes_per_line, base_addr, 0, 0); }