diff options
Diffstat (limited to 'lldb/source/Core')
-rw-r--r-- | lldb/source/Core/CMakeLists.txt | 4 | ||||
-rw-r--r-- | lldb/source/Core/DataBufferHeap.cpp | 94 | ||||
-rw-r--r-- | lldb/source/Core/DataBufferLLVM.cpp | 58 | ||||
-rw-r--r-- | lldb/source/Core/DataEncoder.cpp | 286 | ||||
-rw-r--r-- | lldb/source/Core/DataExtractor.cpp | 1239 | ||||
-rw-r--r-- | lldb/source/Core/Disassembler.cpp | 4 | ||||
-rw-r--r-- | lldb/source/Core/DumpDataExtractor.cpp | 2 | ||||
-rw-r--r-- | lldb/source/Core/EmulateInstruction.cpp | 2 | ||||
-rw-r--r-- | lldb/source/Core/Event.cpp | 2 | ||||
-rw-r--r-- | lldb/source/Core/Module.cpp | 4 | ||||
-rw-r--r-- | lldb/source/Core/Opcode.cpp | 4 | ||||
-rw-r--r-- | lldb/source/Core/RegisterValue.cpp | 2 | ||||
-rw-r--r-- | lldb/source/Core/Scalar.cpp | 2 | ||||
-rw-r--r-- | lldb/source/Core/SourceManager.cpp | 2 | ||||
-rw-r--r-- | lldb/source/Core/StructuredData.cpp | 2 | ||||
-rw-r--r-- | lldb/source/Core/Value.cpp | 4 | ||||
-rw-r--r-- | lldb/source/Core/ValueObject.cpp | 2 | ||||
-rw-r--r-- | lldb/source/Core/ValueObjectConstResult.cpp | 2 | ||||
-rw-r--r-- | lldb/source/Core/ValueObjectConstResultImpl.cpp | 2 |
19 files changed, 18 insertions, 1699 deletions
diff --git a/lldb/source/Core/CMakeLists.txt b/lldb/source/Core/CMakeLists.txt index 2316a235ee4..223ae619414 100644 --- a/lldb/source/Core/CMakeLists.txt +++ b/lldb/source/Core/CMakeLists.txt @@ -9,10 +9,6 @@ add_lldb_library(lldbCore Broadcaster.cpp Communication.cpp Connection.cpp - DataBufferHeap.cpp - DataBufferLLVM.cpp - DataEncoder.cpp - DataExtractor.cpp Debugger.cpp Disassembler.cpp DumpDataExtractor.cpp diff --git a/lldb/source/Core/DataBufferHeap.cpp b/lldb/source/Core/DataBufferHeap.cpp deleted file mode 100644 index cdd37bfdf0f..00000000000 --- a/lldb/source/Core/DataBufferHeap.cpp +++ /dev/null @@ -1,94 +0,0 @@ -//===-- DataBufferHeap.cpp --------------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "lldb/Core/DataBufferHeap.h" - -// C Includes -// C++ Includes -// Other libraries and framework includes -// Project includes - -using namespace lldb_private; - -//---------------------------------------------------------------------- -// Default constructor -//---------------------------------------------------------------------- -DataBufferHeap::DataBufferHeap() : m_data() {} - -//---------------------------------------------------------------------- -// Initialize this class with "n" characters and fill the buffer -// with "ch". -//---------------------------------------------------------------------- -DataBufferHeap::DataBufferHeap(lldb::offset_t n, uint8_t ch) : m_data() { - if (n < m_data.max_size()) - m_data.assign(n, ch); -} - -//---------------------------------------------------------------------- -// Initialize this class with a copy of the "n" bytes from the "bytes" -// buffer. -//---------------------------------------------------------------------- -DataBufferHeap::DataBufferHeap(const void *src, lldb::offset_t src_len) - : m_data() { - CopyData(src, src_len); -} - -//---------------------------------------------------------------------- -// Virtual destructor since this class inherits from a pure virtual -// base class. -//---------------------------------------------------------------------- -DataBufferHeap::~DataBufferHeap() = default; - -//---------------------------------------------------------------------- -// Return a pointer to the bytes owned by this object, or nullptr if -// the object contains no bytes. -//---------------------------------------------------------------------- -uint8_t *DataBufferHeap::GetBytes() { - return (m_data.empty() ? nullptr : m_data.data()); -} - -//---------------------------------------------------------------------- -// Return a const pointer to the bytes owned by this object, or nullptr -// if the object contains no bytes. -//---------------------------------------------------------------------- -const uint8_t *DataBufferHeap::GetBytes() const { - return (m_data.empty() ? nullptr : m_data.data()); -} - -//---------------------------------------------------------------------- -// Return the number of bytes this object currently contains. -//---------------------------------------------------------------------- -uint64_t DataBufferHeap::GetByteSize() const { return m_data.size(); } - -//---------------------------------------------------------------------- -// Sets the number of bytes that this object should be able to -// contain. This can be used prior to copying data into the buffer. -//---------------------------------------------------------------------- -uint64_t DataBufferHeap::SetByteSize(uint64_t new_size) { - m_data.resize(new_size); - return m_data.size(); -} - -void DataBufferHeap::CopyData(const void *src, uint64_t src_len) { - const uint8_t *src_u8 = (const uint8_t *)src; - if (src && src_len > 0) - m_data.assign(src_u8, src_u8 + src_len); - else - m_data.clear(); -} - -void DataBufferHeap::AppendData(const void *src, uint64_t src_len) { - m_data.insert(m_data.end(), (const uint8_t *)src, - (const uint8_t *)src + src_len); -} - -void DataBufferHeap::Clear() { - buffer_t empty; - m_data.swap(empty); -} diff --git a/lldb/source/Core/DataBufferLLVM.cpp b/lldb/source/Core/DataBufferLLVM.cpp deleted file mode 100644 index e42765891c8..00000000000 --- a/lldb/source/Core/DataBufferLLVM.cpp +++ /dev/null @@ -1,58 +0,0 @@ -//===--- DataBufferLLVM.cpp -------------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "lldb/Core/DataBufferLLVM.h" - -#include "lldb/Host/FileSpec.h" -#include "llvm/Support/FileSystem.h" -#include "llvm/Support/MemoryBuffer.h" - -using namespace lldb_private; - -DataBufferLLVM::DataBufferLLVM(std::unique_ptr<llvm::MemoryBuffer> MemBuffer) - : Buffer(std::move(MemBuffer)) { - assert(Buffer != nullptr && - "Cannot construct a DataBufferLLVM with a null buffer"); -} - -DataBufferLLVM::~DataBufferLLVM() {} - -std::shared_ptr<DataBufferLLVM> -DataBufferLLVM::CreateFromPath(llvm::StringRef Path, uint64_t Size, - uint64_t Offset) { - // If the file resides non-locally, pass the volatile flag so that we don't - // mmap it. - bool Volatile = !llvm::sys::fs::is_local(Path); - - auto Buffer = llvm::MemoryBuffer::getFileSlice(Path, Size, Offset, Volatile); - if (!Buffer) - return nullptr; - return std::shared_ptr<DataBufferLLVM>( - new DataBufferLLVM(std::move(*Buffer))); -} - -std::shared_ptr<DataBufferLLVM> -DataBufferLLVM::CreateFromFileSpec(const FileSpec &F, uint64_t Size, - uint64_t Offset) { - return CreateFromPath(F.GetPath(), Size, Offset); -} - -uint8_t *DataBufferLLVM::GetBytes() { - return const_cast<uint8_t *>(GetBuffer()); -} - -const uint8_t *DataBufferLLVM::GetBytes() const { return GetBuffer(); } - -lldb::offset_t DataBufferLLVM::GetByteSize() const { - return Buffer->getBufferSize(); -} - -const uint8_t *DataBufferLLVM::GetBuffer() const { - return reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()); -} diff --git a/lldb/source/Core/DataEncoder.cpp b/lldb/source/Core/DataEncoder.cpp deleted file mode 100644 index 8eed6b0034c..00000000000 --- a/lldb/source/Core/DataEncoder.cpp +++ /dev/null @@ -1,286 +0,0 @@ -//===-- DataEncoder.cpp -----------------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "lldb/Core/DataEncoder.h" - -// C Includes -// C++ Includes -#include <cassert> -#include <cstddef> - -// Other libraries and framework includes -#include "llvm/Support/MathExtras.h" - -// Project includes -#include "lldb/Core/DataBuffer.h" -#include "lldb/Utility/Endian.h" - -using namespace lldb; -using namespace lldb_private; - -static inline void WriteInt16(unsigned char *ptr, unsigned offset, - uint16_t value) { - *(uint16_t *)(ptr + offset) = value; -} - -static inline void WriteInt32(unsigned char *ptr, unsigned offset, - uint32_t value) { - *(uint32_t *)(ptr + offset) = value; -} - -static inline void WriteInt64(unsigned char *ptr, unsigned offset, - uint64_t value) { - *(uint64_t *)(ptr + offset) = value; -} - -static inline void WriteSwappedInt16(unsigned char *ptr, unsigned offset, - uint16_t value) { - *(uint16_t *)(ptr + offset) = llvm::ByteSwap_16(value); -} - -static inline void WriteSwappedInt32(unsigned char *ptr, unsigned offset, - uint32_t value) { - *(uint32_t *)(ptr + offset) = llvm::ByteSwap_32(value); -} - -static inline void WriteSwappedInt64(unsigned char *ptr, unsigned offset, - uint64_t value) { - *(uint64_t *)(ptr + offset) = llvm::ByteSwap_64(value); -} - -//---------------------------------------------------------------------- -// Default constructor. -//---------------------------------------------------------------------- -DataEncoder::DataEncoder() - : m_start(nullptr), m_end(nullptr), - m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)), - m_data_sp() {} - -//---------------------------------------------------------------------- -// This constructor allows us to use data that is owned by someone else. -// The data must stay around as long as this object is valid. -//---------------------------------------------------------------------- -DataEncoder::DataEncoder(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() {} - -//---------------------------------------------------------------------- -// Make a shared pointer reference to the shared data in "data_sp" and -// set the endian swapping setting to "swap", and the address size to -// "addr_size". The shared data reference will ensure the data lives -// as long as any DataEncoder objects exist that have a reference to -// this data. -//---------------------------------------------------------------------- -DataEncoder::DataEncoder(const DataBufferSP &data_sp, ByteOrder endian, - uint8_t addr_size) - : m_start(nullptr), m_end(nullptr), m_byte_order(endian), - m_addr_size(addr_size), m_data_sp() { - SetData(data_sp); -} - -DataEncoder::~DataEncoder() = default; - -//------------------------------------------------------------------ -// Clears the object contents back to a default invalid state, and -// release any references to shared data that this object may -// contain. -//------------------------------------------------------------------ -void DataEncoder::Clear() { - m_start = nullptr; - m_end = nullptr; - m_byte_order = endian::InlHostByteOrder(); - m_addr_size = sizeof(void *); - m_data_sp.reset(); -} - -//------------------------------------------------------------------ -// If this object contains shared data, this function returns the -// offset into that shared data. Else zero is returned. -//------------------------------------------------------------------ -size_t DataEncoder::GetSharedDataOffset() const { - if (m_start != nullptr) { - const DataBuffer *data = m_data_sp.get(); - if (data != nullptr) { - const uint8_t *data_bytes = data->GetBytes(); - if (data_bytes != nullptr) { - assert(m_start >= data_bytes); - return m_start - data_bytes; - } - } - } - return 0; -} - -//---------------------------------------------------------------------- -// Set the data with which this object will extract from to data -// starting at BYTES and set the length of the data to LENGTH bytes -// long. The data is externally owned must be around at least as -// long as this object points to the data. No copy of the data is -// made, this object just refers to this data and can extract from -// it. If this object refers to any shared data upon entry, the -// reference to that data will be released. Is SWAP is set to true, -// any data extracted will be endian swapped. -//---------------------------------------------------------------------- -uint32_t DataEncoder::SetData(void *bytes, uint32_t length, ByteOrder endian) { - m_byte_order = endian; - m_data_sp.reset(); - if (bytes == nullptr || length == 0) { - m_start = nullptr; - m_end = nullptr; - } else { - m_start = (uint8_t *)bytes; - m_end = m_start + length; - } - return GetByteSize(); -} - -//---------------------------------------------------------------------- -// Assign the data for this object to be a subrange of the shared -// data in "data_sp" starting "data_offset" bytes into "data_sp" -// and ending "data_length" bytes later. If "data_offset" is not -// a valid offset into "data_sp", then this object will contain no -// bytes. If "data_offset" is within "data_sp" yet "data_length" is -// too large, the length will be capped at the number of bytes -// remaining in "data_sp". A ref counted pointer to the data in -// "data_sp" will be made in this object IF the number of bytes this -// object refers to in greater than zero (if at least one byte was -// available starting at "data_offset") to ensure the data stays -// around as long as it is needed. The address size and endian swap -// settings will remain unchanged from their current settings. -//---------------------------------------------------------------------- -uint32_t DataEncoder::SetData(const DataBufferSP &data_sp, uint32_t data_offset, - uint32_t data_length) { - m_start = m_end = nullptr; - - if (data_length > 0) { - m_data_sp = data_sp; - if (data_sp) { - 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; - // Cap the length of we asked for too many - if (data_length <= bytes_left) - m_end = m_start + data_length; // We got all the bytes we wanted - else - m_end = m_start + bytes_left; // Not all the bytes requested were - // available in the shared data - } - } - } - - uint32_t new_size = GetByteSize(); - - // Don't hold a shared pointer to the data buffer if we don't share - // any valid bytes in the shared buffer. - if (new_size == 0) - m_data_sp.reset(); - - return new_size; -} - -//---------------------------------------------------------------------- -// Extract a single unsigned char from the binary data and update -// the offset pointed to by "offset_ptr". -// -// RETURNS the byte that was extracted, or zero on failure. -//---------------------------------------------------------------------- -uint32_t DataEncoder::PutU8(uint32_t offset, uint8_t value) { - if (ValidOffset(offset)) { - m_start[offset] = value; - return offset + 1; - } - return UINT32_MAX; -} - -uint32_t DataEncoder::PutU16(uint32_t offset, uint16_t value) { - if (ValidOffsetForDataOfSize(offset, sizeof(value))) { - if (m_byte_order != endian::InlHostByteOrder()) - WriteSwappedInt16(m_start, offset, value); - else - WriteInt16(m_start, offset, value); - - return offset + sizeof(value); - } - return UINT32_MAX; -} - -uint32_t DataEncoder::PutU32(uint32_t offset, uint32_t value) { - if (ValidOffsetForDataOfSize(offset, sizeof(value))) { - if (m_byte_order != endian::InlHostByteOrder()) - WriteSwappedInt32(m_start, offset, value); - else - WriteInt32(m_start, offset, value); - - return offset + sizeof(value); - } - return UINT32_MAX; -} - -uint32_t DataEncoder::PutU64(uint32_t offset, uint64_t value) { - if (ValidOffsetForDataOfSize(offset, sizeof(value))) { - if (m_byte_order != endian::InlHostByteOrder()) - WriteSwappedInt64(m_start, offset, value); - else - WriteInt64(m_start, offset, value); - - return offset + sizeof(value); - } - return UINT32_MAX; -} - -//---------------------------------------------------------------------- -// Extract a single integer value from the data and update the offset -// pointed to by "offset_ptr". The size of the extracted integer -// is specified by the "byte_size" argument. "byte_size" should have -// a value >= 1 and <= 8 since the return value is only 64 bits -// wide. Any "byte_size" values less than 1 or greater than 8 will -// result in nothing being extracted, and zero being returned. -// -// RETURNS the integer value that was extracted, or zero on failure. -//---------------------------------------------------------------------- -uint32_t DataEncoder::PutMaxU64(uint32_t offset, uint32_t byte_size, - uint64_t value) { - switch (byte_size) { - case 1: - return PutU8(offset, value); - case 2: - return PutU16(offset, value); - case 4: - return PutU32(offset, value); - case 8: - return PutU64(offset, value); - default: - llvm_unreachable("GetMax64 unhandled case!"); - } - return UINT32_MAX; -} - -uint32_t DataEncoder::PutData(uint32_t offset, const void *src, - uint32_t src_len) { - if (src == nullptr || src_len == 0) - return offset; - - if (ValidOffsetForDataOfSize(offset, src_len)) { - memcpy(m_start + offset, src, src_len); - return offset + src_len; - } - return UINT32_MAX; -} - -uint32_t DataEncoder::PutAddress(uint32_t offset, lldb::addr_t addr) { - return PutMaxU64(offset, GetAddressByteSize(), addr); -} - -uint32_t DataEncoder::PutCString(uint32_t offset, const char *cstr) { - if (cstr != nullptr) - return PutData(offset, cstr, strlen(cstr) + 1); - return UINT32_MAX; -} diff --git a/lldb/source/Core/DataExtractor.cpp b/lldb/source/Core/DataExtractor.cpp deleted file mode 100644 index f99144fc18e..00000000000 --- a/lldb/source/Core/DataExtractor.cpp +++ /dev/null @@ -1,1239 +0,0 @@ -//===-- DataExtractor.cpp ---------------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -// C Includes -// C++ Includes -#include <cassert> -#include <cmath> -#include <cstddef> -#include <string> - -// Other libraries and framework includes -#include "llvm/ADT/ArrayRef.h" -#include "llvm/ADT/SmallVector.h" -#include "llvm/Support/MD5.h" -#include "llvm/Support/MathExtras.h" - -// Project includes -#include "lldb/Core/DataBuffer.h" -#include "lldb/Core/DataBufferHeap.h" -#include "lldb/Core/DataExtractor.h" -#include "lldb/Utility/UUID.h" -#include "lldb/Utility/Endian.h" -#include "lldb/Utility/Log.h" -#include "lldb/Utility/Stream.h" -#include "lldb/Utility/StreamString.h" - -using namespace lldb; -using namespace lldb_private; - -static inline uint16_t ReadInt16(const unsigned char *ptr, offset_t offset) { - uint16_t value; - memcpy(&value, ptr + offset, 2); - return value; -} - -static inline uint32_t ReadInt32(const unsigned char *ptr, - offset_t offset = 0) { - uint32_t value; - memcpy(&value, ptr + offset, 4); - return value; -} - -static inline uint64_t ReadInt64(const unsigned char *ptr, - offset_t offset = 0) { - uint64_t value; - memcpy(&value, ptr + offset, 8); - return value; -} - -static inline uint16_t ReadInt16(const void *ptr) { - uint16_t value; - memcpy(&value, ptr, 2); - return value; -} - -static inline uint16_t ReadSwapInt16(const unsigned char *ptr, - offset_t offset) { - uint16_t value; - memcpy(&value, ptr + offset, 2); - return llvm::ByteSwap_16(value); -} - -static inline uint32_t ReadSwapInt32(const unsigned char *ptr, - offset_t offset) { - uint32_t value; - memcpy(&value, ptr + offset, 4); - return llvm::ByteSwap_32(value); -} - -static inline uint64_t ReadSwapInt64(const unsigned char *ptr, - offset_t offset) { - uint64_t value; - memcpy(&value, ptr + offset, 8); - return llvm::ByteSwap_64(value); -} - -static inline uint16_t ReadSwapInt16(const void *ptr) { - uint16_t value; - memcpy(&value, ptr, 2); - return llvm::ByteSwap_16(value); -} - -static inline uint32_t ReadSwapInt32(const void *ptr) { - uint32_t value; - memcpy(&value, ptr, 4); - return llvm::ByteSwap_32(value); -} - -static inline uint64_t ReadSwapInt64(const void *ptr) { - uint64_t value; - memcpy(&value, ptr, 8); - return llvm::ByteSwap_64(value); -} - -DataExtractor::DataExtractor() - : m_start(nullptr), m_end(nullptr), - m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)), - m_data_sp(), m_target_byte_size(1) {} - -//---------------------------------------------------------------------- -// This constructor allows us to use data that is owned by someone else. -// The data must stay around as long as this object is valid. -//---------------------------------------------------------------------- -DataExtractor::DataExtractor(const void *data, offset_t length, - ByteOrder endian, uint32_t addr_size, - uint32_t target_byte_size /*=1*/) - : m_start(const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(data))), - m_end(const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(data)) + - length), - m_byte_order(endian), m_addr_size(addr_size), m_data_sp(), - m_target_byte_size(target_byte_size) { -#ifdef LLDB_CONFIGURATION_DEBUG - assert(addr_size == 4 || addr_size == 8); -#endif -} - -//---------------------------------------------------------------------- -// Make a shared pointer reference to the shared data in "data_sp" and -// set the endian swapping setting to "swap", and the address size to -// "addr_size". The shared data reference will ensure the data lives -// as long as any DataExtractor objects exist that have a reference to -// this data. -//---------------------------------------------------------------------- -DataExtractor::DataExtractor(const DataBufferSP &data_sp, ByteOrder endian, - uint32_t addr_size, - uint32_t target_byte_size /*=1*/) - : m_start(nullptr), m_end(nullptr), m_byte_order(endian), - m_addr_size(addr_size), m_data_sp(), - m_target_byte_size(target_byte_size) { -#ifdef LLDB_CONFIGURATION_DEBUG - assert(addr_size == 4 || addr_size == 8); -#endif - SetData(data_sp); -} - -//---------------------------------------------------------------------- -// Initialize this object with a subset of the data bytes in "data". -// If "data" contains shared data, then a reference to this shared -// data will added and the shared data will stay around as long -// as any object contains a reference to that data. The endian -// swap and address size settings are copied from "data". -//---------------------------------------------------------------------- -DataExtractor::DataExtractor(const DataExtractor &data, offset_t offset, - offset_t length, uint32_t target_byte_size /*=1*/) - : m_start(nullptr), m_end(nullptr), m_byte_order(data.m_byte_order), - m_addr_size(data.m_addr_size), m_data_sp(), - m_target_byte_size(target_byte_size) { -#ifdef LLDB_CONFIGURATION_DEBUG - assert(m_addr_size == 4 || m_addr_size == 8); -#endif - if (data.ValidOffset(offset)) { - offset_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), - m_target_byte_size(rhs.m_target_byte_size) { -#ifdef LLDB_CONFIGURATION_DEBUG - assert(m_addr_size == 4 || m_addr_size == 8); -#endif -} - -//---------------------------------------------------------------------- -// Assignment operator -//---------------------------------------------------------------------- -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() = default; - -//------------------------------------------------------------------ -// Clears the object contents back to a default invalid state, and -// release any references to shared data that this object may -// contain. -//------------------------------------------------------------------ -void DataExtractor::Clear() { - m_start = nullptr; - m_end = nullptr; - m_byte_order = endian::InlHostByteOrder(); - m_addr_size = sizeof(void *); - m_data_sp.reset(); -} - -//------------------------------------------------------------------ -// If this object contains shared data, this function returns the -// offset into that shared data. Else zero is returned. -//------------------------------------------------------------------ -size_t DataExtractor::GetSharedDataOffset() const { - if (m_start != nullptr) { - const DataBuffer *data = m_data_sp.get(); - if (data != nullptr) { - const uint8_t *data_bytes = data->GetBytes(); - if (data_bytes != nullptr) { - assert(m_start >= data_bytes); - return m_start - data_bytes; - } - } - } - return 0; -} - -//---------------------------------------------------------------------- -// Set the data with which this object will extract from to data -// starting at BYTES and set the length of the data to LENGTH bytes -// long. The data is externally owned must be around at least as -// long as this object points to the data. No copy of the data is -// made, this object just refers to this data and can extract from -// it. If this object refers to any shared data upon entry, the -// reference to that data will be released. Is SWAP is set to true, -// any data extracted will be endian swapped. -//---------------------------------------------------------------------- -lldb::offset_t DataExtractor::SetData(const void *bytes, offset_t length, - ByteOrder endian) { - m_byte_order = endian; - m_data_sp.reset(); - if (bytes == nullptr || length == 0) { - m_start = nullptr; - m_end = nullptr; - } else { - m_start = const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(bytes)); - m_end = m_start + length; - } - return GetByteSize(); -} - -//---------------------------------------------------------------------- -// Assign the data for this object to be a subrange in "data" -// starting "data_offset" bytes into "data" and ending "data_length" -// bytes later. If "data_offset" is not a valid offset into "data", -// then this object will contain no bytes. If "data_offset" is -// within "data" yet "data_length" is too large, the length will be -// capped at the number of bytes remaining in "data". If "data" -// contains a shared pointer to other data, then a ref counted -// pointer to that data will be made in this object. If "data" -// doesn't contain a shared pointer to data, then the bytes referred -// to in "data" will need to exist at least as long as this object -// refers to those bytes. The address size and endian swap settings -// are copied from the current values in "data". -//---------------------------------------------------------------------- -lldb::offset_t DataExtractor::SetData(const DataExtractor &data, - offset_t data_offset, - offset_t data_length) { - m_addr_size = data.m_addr_size; -#ifdef LLDB_CONFIGURATION_DEBUG - assert(m_addr_size == 4 || m_addr_size == 8); -#endif - // If "data" contains shared pointer to data, then we can use that - if (data.m_data_sp) { - m_byte_order = data.m_byte_order; - return SetData(data.m_data_sp, data.GetSharedDataOffset() + data_offset, - data_length); - } - - // We have a DataExtractor object that just has a pointer to bytes - 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; -} - -//---------------------------------------------------------------------- -// Assign the data for this object to be a subrange of the shared -// data in "data_sp" starting "data_offset" bytes into "data_sp" -// and ending "data_length" bytes later. If "data_offset" is not -// a valid offset into "data_sp", then this object will contain no -// bytes. If "data_offset" is within "data_sp" yet "data_length" is -// too large, the length will be capped at the number of bytes -// remaining in "data_sp". A ref counted pointer to the data in -// "data_sp" will be made in this object IF the number of bytes this -// object refers to in greater than zero (if at least one byte was -// available starting at "data_offset") to ensure the data stays -// around as long as it is needed. The address size and endian swap -// settings will remain unchanged from their current settings. -//---------------------------------------------------------------------- -lldb::offset_t DataExtractor::SetData(const DataBufferSP &data_sp, - offset_t data_offset, - offset_t data_length) { - m_start = m_end = nullptr; - - if (data_length > 0) { - m_data_sp = data_sp; - if (data_sp) { - 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; - // Cap the length of we asked for too many - if (data_length <= bytes_left) - m_end = m_start + data_length; // We got all the bytes we wanted - else - m_end = m_start + bytes_left; // Not all the bytes requested were - // available in the shared data - } - } - } - - size_t new_size = GetByteSize(); - - // Don't hold a shared pointer to the data buffer if we don't share - // any valid bytes in the shared buffer. - if (new_size == 0) - m_data_sp.reset(); - - return new_size; -} - -//---------------------------------------------------------------------- -// Extract a single unsigned char from the binary data and update -// the offset pointed to by "offset_ptr". -// -// RETURNS the byte that was extracted, or zero on failure. -//---------------------------------------------------------------------- -uint8_t DataExtractor::GetU8(offset_t *offset_ptr) const { - const uint8_t *data = (const uint8_t *)GetData(offset_ptr, 1); - if (data) - return *data; - return 0; -} - -//---------------------------------------------------------------------- -// Extract "count" unsigned chars from the binary data and update the -// offset pointed to by "offset_ptr". The extracted data is copied into -// "dst". -// -// RETURNS the non-nullptr buffer pointer upon successful extraction of -// all the requested bytes, or nullptr when the data is not available in -// the buffer due to being out of bounds, or insufficient data. -//---------------------------------------------------------------------- -void *DataExtractor::GetU8(offset_t *offset_ptr, void *dst, - uint32_t count) const { - const uint8_t *data = (const uint8_t *)GetData(offset_ptr, count); - if (data) { - // Copy the data into the buffer - memcpy(dst, data, count); - // Return a non-nullptr pointer to the converted data as an indicator of - // success - return dst; - } - return nullptr; -} - -//---------------------------------------------------------------------- -// Extract a single uint16_t from the data and update the offset -// pointed to by "offset_ptr". -// -// RETURNS the uint16_t that was extracted, or zero on failure. -//---------------------------------------------------------------------- -uint16_t DataExtractor::GetU16(offset_t *offset_ptr) const { - uint16_t val = 0; - const uint8_t *data = (const uint8_t *)GetData(offset_ptr, sizeof(val)); - if (data) { - if (m_byte_order != endian::InlHostByteOrder()) - val = ReadSwapInt16(data); - else - val = ReadInt16(data); - } - return val; -} - -uint16_t DataExtractor::GetU16_unchecked(offset_t *offset_ptr) const { - uint16_t val; - if (m_byte_order == endian::InlHostByteOrder()) - val = ReadInt16(m_start, *offset_ptr); - else - val = ReadSwapInt16(m_start, *offset_ptr); - *offset_ptr += sizeof(val); - return val; -} - -uint32_t DataExtractor::GetU32_unchecked(offset_t *offset_ptr) const { - uint32_t val; - if (m_byte_order == endian::InlHostByteOrder()) - val = ReadInt32(m_start, *offset_ptr); - else - val = ReadSwapInt32(m_start, *offset_ptr); - *offset_ptr += sizeof(val); - return val; -} - -uint64_t DataExtractor::GetU64_unchecked(offset_t *offset_ptr) const { - uint64_t val; - if (m_byte_order == endian::InlHostByteOrder()) - val = ReadInt64(m_start, *offset_ptr); - else - val = ReadSwapInt64(m_start, *offset_ptr); - *offset_ptr += sizeof(val); - return val; -} - -//---------------------------------------------------------------------- -// Extract "count" uint16_t values from the binary data and update -// the offset pointed to by "offset_ptr". The extracted data is -// copied into "dst". -// -// RETURNS the non-nullptr buffer pointer upon successful extraction of -// all the requested bytes, or nullptr when the data is not available -// in the buffer due to being out of bounds, or insufficient data. -//---------------------------------------------------------------------- -void *DataExtractor::GetU16(offset_t *offset_ptr, void *void_dst, - uint32_t count) const { - const size_t src_size = sizeof(uint16_t) * count; - const uint16_t *src = (const uint16_t *)GetData(offset_ptr, src_size); - if (src) { - if (m_byte_order != endian::InlHostByteOrder()) { - uint16_t *dst_pos = (uint16_t *)void_dst; - uint16_t *dst_end = dst_pos + count; - const uint16_t *src_pos = src; - while (dst_pos < dst_end) { - *dst_pos = ReadSwapInt16(src_pos); - ++dst_pos; - ++src_pos; - } - } else { - memcpy(void_dst, src, src_size); - } - // Return a non-nullptr pointer to the converted data as an indicator of - // success - return void_dst; - } - return nullptr; -} - -//---------------------------------------------------------------------- -// Extract a single uint32_t from the data and update the offset -// pointed to by "offset_ptr". -// -// RETURNS the uint32_t that was extracted, or zero on failure. -//---------------------------------------------------------------------- -uint32_t DataExtractor::GetU32(offset_t *offset_ptr) const { - uint32_t val = 0; - const uint8_t *data = (const uint8_t *)GetData(offset_ptr, sizeof(val)); - if (data) { - if (m_byte_order != endian::InlHostByteOrder()) { - val = ReadSwapInt32(data); - } else { - memcpy(&val, data, 4); - } - } - return val; -} - -//---------------------------------------------------------------------- -// Extract "count" uint32_t values from the binary data and update -// the offset pointed to by "offset_ptr". The extracted data is -// copied into "dst". -// -// RETURNS the non-nullptr buffer pointer upon successful extraction of -// all the requested bytes, or nullptr when the data is not available -// in the buffer due to being out of bounds, or insufficient data. -//---------------------------------------------------------------------- -void *DataExtractor::GetU32(offset_t *offset_ptr, void *void_dst, - uint32_t count) const { - const size_t src_size = sizeof(uint32_t) * count; - const uint32_t *src = (const uint32_t *)GetData(offset_ptr, src_size); - if (src) { - if (m_byte_order != endian::InlHostByteOrder()) { - uint32_t *dst_pos = (uint32_t *)void_dst; - uint32_t *dst_end = dst_pos + count; - const uint32_t *src_pos = src; - while (dst_pos < dst_end) { - *dst_pos = ReadSwapInt32(src_pos); - ++dst_pos; - ++src_pos; - } - } else { - memcpy(void_dst, src, src_size); - } - // Return a non-nullptr pointer to the converted data as an indicator of - // success - return void_dst; - } - return nullptr; -} - -//---------------------------------------------------------------------- -// Extract a single uint64_t from the data and update the offset -// pointed to by "offset_ptr". -// -// RETURNS the uint64_t that was extracted, or zero on failure. -//---------------------------------------------------------------------- -uint64_t DataExtractor::GetU64(offset_t *offset_ptr) const { - uint64_t val = 0; - const uint8_t *data = (const uint8_t *)GetData(offset_ptr, sizeof(val)); - if (data) { - if (m_byte_order != endian::InlHostByteOrder()) { - val = ReadSwapInt64(data); - } else { - memcpy(&val, data, 8); - } - } - return val; -} - -//---------------------------------------------------------------------- -// GetU64 -// -// Get multiple consecutive 64 bit values. Return true if the entire -// read succeeds and increment the offset pointed to by offset_ptr, else -// return false and leave the offset pointed to by offset_ptr unchanged. -//---------------------------------------------------------------------- -void *DataExtractor::GetU64(offset_t *offset_ptr, void *void_dst, - uint32_t count) const { - const size_t src_size = sizeof(uint64_t) * count; - const uint64_t *src = (const uint64_t *)GetData(offset_ptr, src_size); - if (src) { - if (m_byte_order != endian::InlHostByteOrder()) { - uint64_t *dst_pos = (uint64_t *)void_dst; - uint64_t *dst_end = dst_pos + count; - const uint64_t *src_pos = src; - while (dst_pos < dst_end) { - *dst_pos = ReadSwapInt64(src_pos); - ++dst_pos; - ++src_pos; - } - } else { - memcpy(void_dst, src, src_size); - } - // Return a non-nullptr pointer to the converted data as an indicator of - // success - return void_dst; - } - return nullptr; -} - -//---------------------------------------------------------------------- -// Extract a single integer value from the data and update the offset -// pointed to by "offset_ptr". The size of the extracted integer -// is specified by the "byte_size" argument. "byte_size" should have -// a value between 1 and 4 since the return value is only 32 bits -// wide. Any "byte_size" values less than 1 or greater than 4 will -// result in nothing being extracted, and zero being returned. -// -// RETURNS the integer value that was extracted, or zero on failure. -//---------------------------------------------------------------------- -uint32_t DataExtractor::GetMaxU32(offset_t *offset_ptr, - size_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(false && "GetMaxU32 unhandled case!"); - break; - } - return 0; -} - -//---------------------------------------------------------------------- -// Extract a single integer value from the data and update the offset -// pointed to by "offset_ptr". The size of the extracted integer -// is specified by the "byte_size" argument. "byte_size" should have -// a value >= 1 and <= 8 since the return value is only 64 bits -// wide. Any "byte_size" values less than 1 or greater than 8 will -// result in nothing being extracted, and zero being returned. -// -// RETURNS the integer value that was extracted, or zero on failure. -//---------------------------------------------------------------------- -uint64_t DataExtractor::GetMaxU64(offset_t *offset_ptr, size_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(false && "GetMax64 unhandled case!"); - break; - } - return 0; -} - -uint64_t DataExtractor::GetMaxU64_unchecked(offset_t *offset_ptr, - size_t size) const { - switch (size) { - case 1: - return GetU8_unchecked(offset_ptr); - break; - case 2: - return GetU16_unchecked(offset_ptr); - break; - case 4: - return GetU32_unchecked(offset_ptr); - break; - case 8: - return GetU64_unchecked(offset_ptr); - break; - default: - assert(false && "GetMax64 unhandled case!"); - break; - } - return 0; -} - -int64_t DataExtractor::GetMaxS64(offset_t *offset_ptr, size_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(false && "GetMax64 unhandled case!"); - break; - } - return 0; -} - -uint64_t DataExtractor::GetMaxU64Bitfield(offset_t *offset_ptr, size_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) { - int32_t lsbcount = bitfield_bit_offset; - if (m_byte_order == eByteOrderBig) - lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size; - if (lsbcount > 0) - uval64 >>= lsbcount; - 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(offset_t *offset_ptr, size_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) { - int32_t lsbcount = bitfield_bit_offset; - if (m_byte_order == eByteOrderBig) - lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size; - if (lsbcount > 0) - sval64 >>= lsbcount; - uint64_t bitfield_mask = (((uint64_t)1) << bitfield_bit_size) - 1; - sval64 &= bitfield_mask; - // sign extend if needed - if (sval64 & (((uint64_t)1) << (bitfield_bit_size - 1))) - sval64 |= ~bitfield_mask; - } - return sval64; -} - -float DataExtractor::GetFloat(offset_t *offset_ptr) const { - typedef float float_type; - float_type val = 0.0; - const size_t src_size = sizeof(float_type); - const float_type *src = (const float_type *)GetData(offset_ptr, src_size); - if (src) { - if (m_byte_order != endian::InlHostByteOrder()) { - const uint8_t *src_data = (const uint8_t *)src; - uint8_t *dst_data = (uint8_t *)&val; - for (size_t i = 0; i < sizeof(float_type); ++i) - dst_data[sizeof(float_type) - 1 - i] = src_data[i]; - } else { - val = *src; - } - } - return val; -} - -double DataExtractor::GetDouble(offset_t *offset_ptr) const { - typedef double float_type; - float_type val = 0.0; - const size_t src_size = sizeof(float_type); - const float_type *src = (const float_type *)GetData(offset_ptr, src_size); - if (src) { - if (m_byte_order != endian::InlHostByteOrder()) { - const uint8_t *src_data = (const uint8_t *)src; - uint8_t *dst_data = (uint8_t *)&val; - for (size_t i = 0; i < sizeof(float_type); ++i) - dst_data[sizeof(float_type) - 1 - i] = src_data[i]; - } else { - val = *src; - } - } - return val; -} - -long double DataExtractor::GetLongDouble(offset_t *offset_ptr) const { - long double val = 0.0; -#if defined(__i386__) || defined(__amd64__) || defined(__x86_64__) || \ - defined(_M_IX86) || defined(_M_IA64) || defined(_M_X64) - *offset_ptr += CopyByteOrderedData(*offset_ptr, 10, &val, sizeof(val), - endian::InlHostByteOrder()); -#else - *offset_ptr += CopyByteOrderedData(*offset_ptr, sizeof(val), &val, - sizeof(val), endian::InlHostByteOrder()); -#endif - return val; -} - -//------------------------------------------------------------------ -// Extract a single address from the data and update the offset -// pointed to by "offset_ptr". The size of the extracted address -// comes from the "this->m_addr_size" member variable and should be -// set correctly prior to extracting any address values. -// -// RETURNS the address that was extracted, or zero on failure. -//------------------------------------------------------------------ -uint64_t DataExtractor::GetAddress(offset_t *offset_ptr) const { -#ifdef LLDB_CONFIGURATION_DEBUG - assert(m_addr_size == 4 || m_addr_size == 8); -#endif - return GetMaxU64(offset_ptr, m_addr_size); -} - -uint64_t DataExtractor::GetAddress_unchecked(offset_t *offset_ptr) const { -#ifdef LLDB_CONFIGURATION_DEBUG - assert(m_addr_size == 4 || m_addr_size == 8); -#endif - return GetMaxU64_unchecked(offset_ptr, m_addr_size); -} - -//------------------------------------------------------------------ -// Extract a single pointer from the data and update the offset -// pointed to by "offset_ptr". The size of the extracted pointer -// comes from the "this->m_addr_size" member variable and should be -// set correctly prior to extracting any pointer values. -// -// RETURNS the pointer that was extracted, or zero on failure. -//------------------------------------------------------------------ -uint64_t DataExtractor::GetPointer(offset_t *offset_ptr) const { -#ifdef LLDB_CONFIGURATION_DEBUG - assert(m_addr_size == 4 || m_addr_size == 8); -#endif - return GetMaxU64(offset_ptr, m_addr_size); -} - - -size_t DataExtractor::ExtractBytes(offset_t offset, offset_t length, - ByteOrder dst_byte_order, void *dst) const { - const uint8_t *src = PeekData(offset, length); - if (src) { - if (dst_byte_order != GetByteOrder()) { - // Validate that only a word- or register-sized dst is byte swapped - assert(length == 1 || length == 2 || length == 4 || length == 8 || - length == 10 || length == 16 || length == 32); - - 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; -} - -// Extract data as it exists in target memory -lldb::offset_t DataExtractor::CopyData(offset_t offset, offset_t length, - void *dst) const { - const uint8_t *src = PeekData(offset, length); - if (src) { - ::memcpy(dst, src, length); - return length; - } - return 0; -} - -// Extract data and swap if needed when doing the copy -lldb::offset_t -DataExtractor::CopyByteOrderedData(offset_t src_offset, offset_t src_len, - void *dst_void_ptr, offset_t dst_len, - ByteOrder dst_byte_order) const { - // Validate the source info - if (!ValidOffsetForDataOfSize(src_offset, src_len)) - assert(ValidOffsetForDataOfSize(src_offset, src_len)); - assert(src_len > 0); - assert(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle); - - // Validate the destination info - assert(dst_void_ptr != nullptr); - assert(dst_len > 0); - assert(dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle); - - // Validate that only a word- or register-sized dst is byte swapped - assert(dst_byte_order == m_byte_order || dst_len == 1 || dst_len == 2 || - dst_len == 4 || dst_len == 8 || dst_len == 10 || dst_len == 16 || - dst_len == 32); - - // Must have valid byte orders set in this object and for destination - if (!(dst_byte_order == eByteOrderBig || - dst_byte_order == eByteOrderLittle) || - !(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle)) - return 0; - - 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) { - // We are copying the entire value from src into dst. - // Calculate how many, if any, zeroes we need for the most - // significant bytes if "dst_len" is greater than "src_len"... - const size_t num_zeroes = dst_len - src_len; - if (dst_byte_order == eByteOrderBig) { - // Big endian, so we lead with zeroes... - if (num_zeroes > 0) - ::memset(dst, 0, num_zeroes); - // Then either copy or swap the rest - if (m_byte_order == eByteOrderBig) { - ::memcpy(dst + num_zeroes, src, src_len); - } else { - for (uint32_t i = 0; i < src_len; ++i) - dst[i + num_zeroes] = src[src_len - 1 - i]; - } - } else { - // Little endian destination, so we lead the value bytes - if (m_byte_order == eByteOrderBig) { - for (uint32_t i = 0; i < src_len; ++i) - dst[i] = src[src_len - 1 - i]; - } else { - ::memcpy(dst, src, src_len); - } - // And zero the rest... - if (num_zeroes > 0) - ::memset(dst + src_len, 0, num_zeroes); - } - return src_len; - } else { - // We are only copying some of the value from src into dst.. - - if (dst_byte_order == eByteOrderBig) { - // Big endian dst - if (m_byte_order == eByteOrderBig) { - // Big endian dst, with big endian src - ::memcpy(dst, src + (src_len - dst_len), dst_len); - } else { - // Big endian dst, with little endian src - for (uint32_t i = 0; i < dst_len; ++i) - dst[i] = src[dst_len - 1 - i]; - } - } else { - // Little endian dst - if (m_byte_order == eByteOrderBig) { - // Little endian dst, with big endian src - for (uint32_t i = 0; i < dst_len; ++i) - dst[i] = src[src_len - 1 - i]; - } else { - // Little endian dst, with big endian src - ::memcpy(dst, src, dst_len); - } - } - return dst_len; - } - } - return 0; -} - -//---------------------------------------------------------------------- -// Extracts a variable length NULL terminated C string from -// the data at the offset pointed to by "offset_ptr". The -// "offset_ptr" will be updated with the offset of the byte that -// follows the NULL terminator byte. -// -// If the offset pointed to by "offset_ptr" is out of bounds, or if -// "length" is non-zero and there aren't enough available -// bytes, nullptr will be returned and "offset_ptr" will not be -// updated. -//---------------------------------------------------------------------- -const char *DataExtractor::GetCStr(offset_t *offset_ptr) const { - const char *cstr = (const char *)PeekData(*offset_ptr, 1); - if (cstr) { - const char *cstr_end = cstr; - const char *end = (const char *)m_end; - while (cstr_end < end && *cstr_end) - ++cstr_end; - - // Now we are either at the end of the data or we point to the - // NULL C string terminator with cstr_end... - if (*cstr_end == '\0') { - // Advance the offset with one extra byte for the NULL terminator - *offset_ptr += (cstr_end - cstr + 1); - return cstr; - } - - // We reached the end of the data without finding a NULL C string - // terminator. Fall through and return nullptr otherwise anyone that - // would have used the result as a C string can wander into - // unknown memory... - } - return nullptr; -} - -//---------------------------------------------------------------------- -// Extracts a NULL terminated C string from the fixed length field of -// length "len" at the offset pointed to by "offset_ptr". -// The "offset_ptr" will be updated with the offset of the byte that -// follows the fixed length field. -// -// If the offset pointed to by "offset_ptr" is out of bounds, or if -// the offset plus the length of the field is out of bounds, or if the -// field does not contain a NULL terminator byte, nullptr will be returned -// and "offset_ptr" will not be updated. -//---------------------------------------------------------------------- -const char *DataExtractor::GetCStr(offset_t *offset_ptr, offset_t len) const { - const char *cstr = (const char *)PeekData(*offset_ptr, len); - if (cstr != nullptr) { - if (memchr(cstr, '\0', len) == nullptr) { - return nullptr; - } - *offset_ptr += len; - return cstr; - } - return nullptr; -} - -//------------------------------------------------------------------ -// Peeks at a string in the contained data. No verification is done -// to make sure the entire string lies within the bounds of this -// object's data, only "offset" is verified to be a valid offset. -// -// Returns a valid C string pointer if "offset" is a valid offset in -// this object's data, else nullptr is returned. -//------------------------------------------------------------------ -const char *DataExtractor::PeekCStr(offset_t offset) const { - return (const char *)PeekData(offset, 1); -} - -//---------------------------------------------------------------------- -// Extracts an unsigned LEB128 number from this object's data -// starting at the offset pointed to by "offset_ptr". The offset -// pointed to by "offset_ptr" will be updated with the offset of the -// byte following the last extracted byte. -// -// Returned the extracted integer value. -//---------------------------------------------------------------------- -uint64_t DataExtractor::GetULEB128(offset_t *offset_ptr) const { - const uint8_t *src = (const uint8_t *)PeekData(*offset_ptr, 1); - if (src == nullptr) - return 0; - - const uint8_t *end = m_end; - - if (src < end) { - uint64_t result = *src++; - if (result >= 0x80) { - result &= 0x7f; - int shift = 7; - while (src < end) { - uint8_t byte = *src++; - result |= (uint64_t)(byte & 0x7f) << shift; - if ((byte & 0x80) == 0) - break; - shift += 7; - } - } - *offset_ptr = src - m_start; - return result; - } - - return 0; -} - -//---------------------------------------------------------------------- -// Extracts an signed LEB128 number from this object's data -// starting at the offset pointed to by "offset_ptr". The offset -// pointed to by "offset_ptr" will be updated with the offset of the -// byte following the last extracted byte. -// -// Returned the extracted integer value. -//---------------------------------------------------------------------- -int64_t DataExtractor::GetSLEB128(offset_t *offset_ptr) const { - const uint8_t *src = (const uint8_t *)PeekData(*offset_ptr, 1); - if (src == nullptr) - return 0; - - const uint8_t *end = m_end; - - if (src < end) { - int64_t result = 0; - int shift = 0; - int size = sizeof(int64_t) * 8; - - uint8_t byte = 0; - int bytecount = 0; - - while (src < end) { - bytecount++; - byte = *src++; - result |= (int64_t)(byte & 0x7f) << shift; - shift += 7; - if ((byte & 0x80) == 0) - break; - } - - // Sign bit of byte is 2nd high order bit (0x40) - if (shift < size && (byte & 0x40)) - result |= -(1 << shift); - - *offset_ptr += bytecount; - return result; - } - return 0; -} - -//---------------------------------------------------------------------- -// Skips a ULEB128 number (signed or unsigned) from this object's -// data starting at the offset pointed to by "offset_ptr". The -// offset pointed to by "offset_ptr" will be updated with the offset -// of the byte following the last extracted byte. -// -// Returns the number of bytes consumed during the extraction. -//---------------------------------------------------------------------- -uint32_t DataExtractor::Skip_LEB128(offset_t *offset_ptr) const { - uint32_t bytes_consumed = 0; - const uint8_t *src = (const uint8_t *)PeekData(*offset_ptr, 1); - if (src == nullptr) - return 0; - - const uint8_t *end = m_end; - - if (src < end) { - const uint8_t *src_pos = src; - while ((src_pos < end) && (*src_pos++ & 0x80)) - ++bytes_consumed; - *offset_ptr += src_pos - src; - } - return bytes_consumed; -} - -//---------------------------------------------------------------------- -// Dumps bytes from this object's data to the stream "s" starting -// "start_offset" bytes into this data, and ending with the byte -// before "end_offset". "base_addr" will be added to the offset -// into the dumped data when showing the offset into the data in the -// output information. "num_per_line" objects of type "type" will -// be dumped with the option to override the format for each object -// with "type_format". "type_format" is a printf style formatting -// string. If "type_format" is nullptr, then an appropriate format -// string will be used for the supplied "type". If the stream "s" -// is nullptr, then the output will be send to Log(). -//---------------------------------------------------------------------- -lldb::offset_t DataExtractor::PutToLog(Log *log, offset_t start_offset, - offset_t length, uint64_t base_addr, - uint32_t num_per_line, - DataExtractor::Type type, - const char *format) const { - if (log == nullptr) - return start_offset; - - offset_t offset; - offset_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) { - // Print out any previous string - if (sstr.GetSize() > 0) { - log->PutString(sstr.GetString()); - sstr.Clear(); - } - // Reset string offset and fill the current line string with address: - if (base_addr != LLDB_INVALID_ADDRESS) - sstr.Printf("0x%8.8" PRIx64 ":", - (uint64_t)(base_addr + (offset - start_offset))); - } - - switch (type) { - 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.16" PRIx64, GetU64(&offset)); - break; - case TypePointer: - sstr.Printf(format ? format : " 0x%" PRIx64, GetAddress(&offset)); - break; - case TypeULEB128: - sstr.Printf(format ? format : " 0x%" PRIx64, GetULEB128(&offset)); - break; - case TypeSLEB128: - sstr.Printf(format ? format : " %" PRId64, GetSLEB128(&offset)); - break; - } - } - - if (!sstr.Empty()) - log->PutString(sstr.GetString()); - - return offset; // Return the offset at which we ended up -} - -//---------------------------------------------------------------------- -// DumpUUID -// -// Dump out a UUID starting at 'offset' bytes into the buffer -//---------------------------------------------------------------------- -void DataExtractor::DumpUUID(Stream *s, offset_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.8" PRIx64 ">", - offset); - } - } -} - -size_t DataExtractor::Copy(DataExtractor &dest_data) const { - if (m_data_sp) { - // we can pass along the SP to the data - dest_data.SetData(m_data_sp); - } else { - const uint8_t *base_ptr = m_start; - size_t data_size = GetByteSize(); - dest_data.SetData(DataBufferSP(new DataBufferHeap(base_ptr, data_size))); - } - return GetByteSize(); -} - -bool DataExtractor::Append(DataExtractor &rhs) { - if (rhs.GetByteOrder() != GetByteOrder()) - return false; - - if (rhs.GetByteSize() == 0) - return true; - - if (GetByteSize() == 0) - return (rhs.Copy(*this) > 0); - - size_t bytes = GetByteSize() + rhs.GetByteSize(); - - DataBufferHeap *buffer_heap_ptr = nullptr; - DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0)); - - if (!buffer_sp || buffer_heap_ptr == nullptr) - return false; - - uint8_t *bytes_ptr = buffer_heap_ptr->GetBytes(); - - memcpy(bytes_ptr, GetDataStart(), GetByteSize()); - memcpy(bytes_ptr + GetByteSize(), rhs.GetDataStart(), rhs.GetByteSize()); - - SetData(buffer_sp); - - return true; -} - -bool DataExtractor::Append(void *buf, offset_t length) { - if (buf == nullptr) - return false; - - if (length == 0) - return true; - - size_t bytes = GetByteSize() + length; - - DataBufferHeap *buffer_heap_ptr = nullptr; - DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0)); - - if (!buffer_sp || buffer_heap_ptr == nullptr) - return false; - - uint8_t *bytes_ptr = buffer_heap_ptr->GetBytes(); - - if (GetByteSize() > 0) - memcpy(bytes_ptr, GetDataStart(), GetByteSize()); - - memcpy(bytes_ptr + GetByteSize(), buf, length); - - SetData(buffer_sp); - - return true; -} - -void DataExtractor::Checksum(llvm::SmallVectorImpl<uint8_t> &dest, - uint64_t max_data) { - if (max_data == 0) - max_data = GetByteSize(); - else - max_data = std::min(max_data, GetByteSize()); - - llvm::MD5 md5; - - const llvm::ArrayRef<uint8_t> data(GetDataStart(), max_data); - md5.update(data); - - llvm::MD5::MD5Result result; - md5.final(result); - - dest.resize(16); - std::copy(result, result + 16, dest.begin()); -} diff --git a/lldb/source/Core/Disassembler.cpp b/lldb/source/Core/Disassembler.cpp index c520fd4d30b..ca19e2883d0 100644 --- a/lldb/source/Core/Disassembler.cpp +++ b/lldb/source/Core/Disassembler.cpp @@ -16,8 +16,6 @@ // Other libraries and framework includes // Project includes -#include "lldb/Core/DataBufferHeap.h" -#include "lldb/Core/DataExtractor.h" #include "lldb/Core/Debugger.h" #include "lldb/Core/EmulateInstruction.h" #include "lldb/Core/Module.h" @@ -37,6 +35,8 @@ #include "lldb/Target/SectionLoadList.h" #include "lldb/Target/StackFrame.h" #include "lldb/Target/Target.h" +#include "lldb/Utility/DataBufferHeap.h" +#include "lldb/Utility/DataExtractor.h" #include "lldb/Utility/Error.h" #include "lldb/Utility/RegularExpression.h" #include "lldb/lldb-private.h" diff --git a/lldb/source/Core/DumpDataExtractor.cpp b/lldb/source/Core/DumpDataExtractor.cpp index c4fad436af8..e2e0af9b07a 100644 --- a/lldb/source/Core/DumpDataExtractor.cpp +++ b/lldb/source/Core/DumpDataExtractor.cpp @@ -9,13 +9,13 @@ #include "lldb/Core/DumpDataExtractor.h" -#include "lldb/Core/DataExtractor.h" #include "lldb/Core/Disassembler.h" #include "lldb/Symbol/ClangASTContext.h" #include "lldb/Target/ExecutionContext.h" #include "lldb/Target/ExecutionContextScope.h" #include "lldb/Target/SectionLoadList.h" #include "lldb/Target/Target.h" +#include "lldb/Utility/DataExtractor.h" #include "lldb/Utility/Stream.h" #include <bitset> diff --git a/lldb/source/Core/EmulateInstruction.cpp b/lldb/source/Core/EmulateInstruction.cpp index 7f2dfb3d24d..8662fd6aa1b 100644 --- a/lldb/source/Core/EmulateInstruction.cpp +++ b/lldb/source/Core/EmulateInstruction.cpp @@ -16,7 +16,6 @@ // Other libraries and framework includes // Project includes #include "lldb/Core/Address.h" -#include "lldb/Core/DataExtractor.h" #include "lldb/Core/PluginManager.h" #include "lldb/Core/RegisterValue.h" #include "lldb/Core/StreamFile.h" @@ -25,6 +24,7 @@ #include "lldb/Target/RegisterContext.h" #include "lldb/Target/Target.h" #include "lldb/Target/Thread.h" +#include "lldb/Utility/DataExtractor.h" #include "lldb/Utility/Endian.h" #include "lldb/Utility/Error.h" #include "lldb/Utility/StreamString.h" diff --git a/lldb/source/Core/Event.cpp b/lldb/source/Core/Event.cpp index 31b409426d2..e8b7c096452 100644 --- a/lldb/source/Core/Event.cpp +++ b/lldb/source/Core/Event.cpp @@ -14,11 +14,11 @@ // Other libraries and framework includes // Project includes #include "lldb/Core/Broadcaster.h" -#include "lldb/Core/DataExtractor.h" #include "lldb/Core/DumpDataExtractor.h" #include "lldb/Core/Event.h" #include "lldb/Core/State.h" #include "lldb/Target/Process.h" +#include "lldb/Utility/DataExtractor.h" #include "lldb/Utility/Endian.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/Stream.h" diff --git a/lldb/source/Core/Module.cpp b/lldb/source/Core/Module.cpp index 12c2560c91d..c8afb61e37b 100644 --- a/lldb/source/Core/Module.cpp +++ b/lldb/source/Core/Module.cpp @@ -19,8 +19,6 @@ #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" #include "Plugins/Language/ObjC/ObjCLanguage.h" #include "lldb/Core/AddressResolverFileLine.h" -#include "lldb/Core/DataBuffer.h" -#include "lldb/Core/DataBufferHeap.h" #include "lldb/Core/ModuleList.h" #include "lldb/Core/ModuleSpec.h" #include "lldb/Core/PluginManager.h" @@ -42,6 +40,8 @@ #include "lldb/Target/Process.h" #include "lldb/Target/SectionLoadList.h" #include "lldb/Target/Target.h" +#include "lldb/Utility/DataBuffer.h" +#include "lldb/Utility/DataBufferHeap.h" #include "lldb/Utility/Error.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/RegularExpression.h" diff --git a/lldb/source/Core/Opcode.cpp b/lldb/source/Core/Opcode.cpp index bbb02e7003d..a98d5e6a47e 100644 --- a/lldb/source/Core/Opcode.cpp +++ b/lldb/source/Core/Opcode.cpp @@ -16,8 +16,8 @@ // Project includes #include "lldb/Core/ArchSpec.h" -#include "lldb/Core/DataBufferHeap.h" -#include "lldb/Core/DataExtractor.h" +#include "lldb/Utility/DataBufferHeap.h" +#include "lldb/Utility/DataExtractor.h" #include "lldb/Utility/Endian.h" #include "lldb/Utility/Stream.h" diff --git a/lldb/source/Core/RegisterValue.cpp b/lldb/source/Core/RegisterValue.cpp index 603043bdd0f..9db22a66dd9 100644 --- a/lldb/source/Core/RegisterValue.cpp +++ b/lldb/source/Core/RegisterValue.cpp @@ -18,11 +18,11 @@ #include "llvm/ADT/StringRef.h" // Project includes -#include "lldb/Core/DataExtractor.h" #include "lldb/Core/DumpDataExtractor.h" #include "lldb/Core/Scalar.h" #include "lldb/Host/StringConvert.h" #include "lldb/Interpreter/Args.h" +#include "lldb/Utility/DataExtractor.h" #include "lldb/Utility/Error.h" #include "lldb/Utility/Stream.h" #include "lldb/Utility/StreamString.h" diff --git a/lldb/source/Core/Scalar.cpp b/lldb/source/Core/Scalar.cpp index dddcfd6df6d..3c0a770cd54 100644 --- a/lldb/source/Core/Scalar.cpp +++ b/lldb/source/Core/Scalar.cpp @@ -19,9 +19,9 @@ #include "llvm/ADT/SmallString.h" // Project includes -#include "lldb/Core/DataExtractor.h" #include "lldb/Host/StringConvert.h" #include "lldb/Interpreter/Args.h" +#include "lldb/Utility/DataExtractor.h" #include "lldb/Utility/Endian.h" #include "lldb/Utility/Error.h" #include "lldb/Utility/Stream.h" diff --git a/lldb/source/Core/SourceManager.cpp b/lldb/source/Core/SourceManager.cpp index 9293d67a115..d4101ab7f43 100644 --- a/lldb/source/Core/SourceManager.cpp +++ b/lldb/source/Core/SourceManager.cpp @@ -13,7 +13,6 @@ // C++ Includes // Other libraries and framework includes // Project includes -#include "lldb/Core/DataBuffer.h" #include "lldb/Core/Debugger.h" #include "lldb/Core/Module.h" #include "lldb/Host/FileSystem.h" @@ -22,6 +21,7 @@ #include "lldb/Symbol/SymbolContext.h" #include "lldb/Target/Target.h" #include "lldb/Utility/AnsiTerminal.h" +#include "lldb/Utility/DataBuffer.h" #include "lldb/Utility/RegularExpression.h" #include "lldb/Utility/Stream.h" diff --git a/lldb/source/Core/StructuredData.cpp b/lldb/source/Core/StructuredData.cpp index d3cbf74eeca..545e4cb411d 100644 --- a/lldb/source/Core/StructuredData.cpp +++ b/lldb/source/Core/StructuredData.cpp @@ -13,10 +13,10 @@ #include <inttypes.h> #include <stdlib.h> -#include "lldb/Core/DataBuffer.h" #include "lldb/Host/File.h" #include "lldb/Host/FileSpec.h" #include "lldb/Host/StringConvert.h" +#include "lldb/Utility/DataBuffer.h" #include "lldb/Utility/Error.h" #include "lldb/Utility/JSON.h" #include "lldb/Utility/StreamString.h" diff --git a/lldb/source/Core/Value.cpp b/lldb/source/Core/Value.cpp index 70350f47712..8874fc8fe0e 100644 --- a/lldb/source/Core/Value.cpp +++ b/lldb/source/Core/Value.cpp @@ -13,8 +13,6 @@ // C++ Includes // Other libraries and framework includes // Project includes -#include "lldb/Core/DataBufferHeap.h" -#include "lldb/Core/DataExtractor.h" #include "lldb/Core/Module.h" #include "lldb/Core/State.h" #include "lldb/Symbol/ClangASTContext.h" @@ -27,6 +25,8 @@ #include "lldb/Target/Process.h" #include "lldb/Target/SectionLoadList.h" #include "lldb/Target/Target.h" +#include "lldb/Utility/DataBufferHeap.h" +#include "lldb/Utility/DataExtractor.h" #include "lldb/Utility/Stream.h" using namespace lldb; diff --git a/lldb/source/Core/ValueObject.cpp b/lldb/source/Core/ValueObject.cpp index 759855bc758..3947551e2b2 100644 --- a/lldb/source/Core/ValueObject.cpp +++ b/lldb/source/Core/ValueObject.cpp @@ -17,7 +17,6 @@ #include "llvm/Support/raw_ostream.h" // Project includes -#include "lldb/Core/DataBufferHeap.h" #include "lldb/Core/Debugger.h" #include "lldb/Core/Module.h" #include "lldb/Core/ValueObjectCast.h" @@ -27,6 +26,7 @@ #include "lldb/Core/ValueObjectList.h" #include "lldb/Core/ValueObjectMemory.h" #include "lldb/Core/ValueObjectSyntheticFilter.h" +#include "lldb/Utility/DataBufferHeap.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/StreamString.h" diff --git a/lldb/source/Core/ValueObjectConstResult.cpp b/lldb/source/Core/ValueObjectConstResult.cpp index f78574ef7ee..cac7b2657b7 100644 --- a/lldb/source/Core/ValueObjectConstResult.cpp +++ b/lldb/source/Core/ValueObjectConstResult.cpp @@ -9,12 +9,12 @@ #include "lldb/Core/ValueObjectConstResult.h" -#include "lldb/Core/DataExtractor.h" #include "lldb/Core/Module.h" #include "lldb/Core/ValueObjectChild.h" #include "lldb/Core/ValueObjectConstResultChild.h" #include "lldb/Core/ValueObjectDynamicValue.h" #include "lldb/Core/ValueObjectList.h" +#include "lldb/Utility/DataExtractor.h" #include "lldb/Symbol/CompilerType.h" #include "lldb/Symbol/ObjectFile.h" diff --git a/lldb/source/Core/ValueObjectConstResultImpl.cpp b/lldb/source/Core/ValueObjectConstResultImpl.cpp index 0e4f73f13b5..02f2e0975aa 100644 --- a/lldb/source/Core/ValueObjectConstResultImpl.cpp +++ b/lldb/source/Core/ValueObjectConstResultImpl.cpp @@ -10,7 +10,6 @@ #include "lldb/Core/ValueObjectConstResultImpl.h" -#include "lldb/Core/DataExtractor.h" #include "lldb/Core/Module.h" #include "lldb/Core/ValueObjectChild.h" #include "lldb/Core/ValueObjectConstResult.h" @@ -18,6 +17,7 @@ #include "lldb/Core/ValueObjectConstResultChild.h" #include "lldb/Core/ValueObjectList.h" #include "lldb/Core/ValueObjectMemory.h" +#include "lldb/Utility/DataExtractor.h" #include "lldb/Symbol/CompilerType.h" #include "lldb/Symbol/ObjectFile.h" |