Path: blob/main/contrib/llvm-project/lldb/source/API/SBData.cpp
39587 views
//===-- SBData.cpp --------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/API/SBData.h"9#include "lldb/API/SBError.h"10#include "lldb/API/SBStream.h"11#include "lldb/Utility/Instrumentation.h"1213#include "lldb/Core/DumpDataExtractor.h"14#include "lldb/Utility/DataBufferHeap.h"15#include "lldb/Utility/DataExtractor.h"16#include "lldb/Utility/Stream.h"1718#include <cinttypes>19#include <memory>2021using namespace lldb;22using namespace lldb_private;2324SBData::SBData() : m_opaque_sp(new DataExtractor()) {25LLDB_INSTRUMENT_VA(this);26}2728SBData::SBData(const lldb::DataExtractorSP &data_sp) : m_opaque_sp(data_sp) {}2930SBData::SBData(const SBData &rhs) : m_opaque_sp(rhs.m_opaque_sp) {31LLDB_INSTRUMENT_VA(this, rhs);32}3334const SBData &SBData::operator=(const SBData &rhs) {35LLDB_INSTRUMENT_VA(this, rhs);3637if (this != &rhs)38m_opaque_sp = rhs.m_opaque_sp;39return *this;40}4142SBData::~SBData() = default;4344void SBData::SetOpaque(const lldb::DataExtractorSP &data_sp) {45m_opaque_sp = data_sp;46}4748lldb_private::DataExtractor *SBData::get() const { return m_opaque_sp.get(); }4950lldb_private::DataExtractor *SBData::operator->() const {51return m_opaque_sp.operator->();52}5354lldb::DataExtractorSP &SBData::operator*() { return m_opaque_sp; }5556const lldb::DataExtractorSP &SBData::operator*() const { return m_opaque_sp; }5758bool SBData::IsValid() {59LLDB_INSTRUMENT_VA(this);60return this->operator bool();61}62SBData::operator bool() const {63LLDB_INSTRUMENT_VA(this);6465return m_opaque_sp.get() != nullptr;66}6768uint8_t SBData::GetAddressByteSize() {69LLDB_INSTRUMENT_VA(this);7071uint8_t value = 0;72if (m_opaque_sp.get())73value = m_opaque_sp->GetAddressByteSize();74return value;75}7677void SBData::SetAddressByteSize(uint8_t addr_byte_size) {78LLDB_INSTRUMENT_VA(this, addr_byte_size);7980if (m_opaque_sp.get())81m_opaque_sp->SetAddressByteSize(addr_byte_size);82}8384void SBData::Clear() {85LLDB_INSTRUMENT_VA(this);8687if (m_opaque_sp.get())88m_opaque_sp->Clear();89}9091size_t SBData::GetByteSize() {92LLDB_INSTRUMENT_VA(this);9394size_t value = 0;95if (m_opaque_sp.get())96value = m_opaque_sp->GetByteSize();97return value;98}99100lldb::ByteOrder SBData::GetByteOrder() {101LLDB_INSTRUMENT_VA(this);102103lldb::ByteOrder value = eByteOrderInvalid;104if (m_opaque_sp.get())105value = m_opaque_sp->GetByteOrder();106return value;107}108109void SBData::SetByteOrder(lldb::ByteOrder endian) {110LLDB_INSTRUMENT_VA(this, endian);111112if (m_opaque_sp.get())113m_opaque_sp->SetByteOrder(endian);114}115116float SBData::GetFloat(lldb::SBError &error, lldb::offset_t offset) {117LLDB_INSTRUMENT_VA(this, error, offset);118119float value = 0;120if (!m_opaque_sp.get()) {121error.SetErrorString("no value to read from");122} else {123uint32_t old_offset = offset;124value = m_opaque_sp->GetFloat(&offset);125if (offset == old_offset)126error.SetErrorString("unable to read data");127}128return value;129}130131double SBData::GetDouble(lldb::SBError &error, lldb::offset_t offset) {132LLDB_INSTRUMENT_VA(this, error, offset);133134double value = 0;135if (!m_opaque_sp.get()) {136error.SetErrorString("no value to read from");137} else {138uint32_t old_offset = offset;139value = m_opaque_sp->GetDouble(&offset);140if (offset == old_offset)141error.SetErrorString("unable to read data");142}143return value;144}145146long double SBData::GetLongDouble(lldb::SBError &error, lldb::offset_t offset) {147LLDB_INSTRUMENT_VA(this, error, offset);148149long double value = 0;150if (!m_opaque_sp.get()) {151error.SetErrorString("no value to read from");152} else {153uint32_t old_offset = offset;154value = m_opaque_sp->GetLongDouble(&offset);155if (offset == old_offset)156error.SetErrorString("unable to read data");157}158return value;159}160161lldb::addr_t SBData::GetAddress(lldb::SBError &error, lldb::offset_t offset) {162LLDB_INSTRUMENT_VA(this, error, offset);163164lldb::addr_t value = 0;165if (!m_opaque_sp.get()) {166error.SetErrorString("no value to read from");167} else {168uint32_t old_offset = offset;169value = m_opaque_sp->GetAddress(&offset);170if (offset == old_offset)171error.SetErrorString("unable to read data");172}173return value;174}175176uint8_t SBData::GetUnsignedInt8(lldb::SBError &error, lldb::offset_t offset) {177LLDB_INSTRUMENT_VA(this, error, offset);178179uint8_t value = 0;180if (!m_opaque_sp.get()) {181error.SetErrorString("no value to read from");182} else {183uint32_t old_offset = offset;184value = m_opaque_sp->GetU8(&offset);185if (offset == old_offset)186error.SetErrorString("unable to read data");187}188return value;189}190191uint16_t SBData::GetUnsignedInt16(lldb::SBError &error, lldb::offset_t offset) {192LLDB_INSTRUMENT_VA(this, error, offset);193194uint16_t value = 0;195if (!m_opaque_sp.get()) {196error.SetErrorString("no value to read from");197} else {198uint32_t old_offset = offset;199value = m_opaque_sp->GetU16(&offset);200if (offset == old_offset)201error.SetErrorString("unable to read data");202}203return value;204}205206uint32_t SBData::GetUnsignedInt32(lldb::SBError &error, lldb::offset_t offset) {207LLDB_INSTRUMENT_VA(this, error, offset);208209uint32_t value = 0;210if (!m_opaque_sp.get()) {211error.SetErrorString("no value to read from");212} else {213uint32_t old_offset = offset;214value = m_opaque_sp->GetU32(&offset);215if (offset == old_offset)216error.SetErrorString("unable to read data");217}218return value;219}220221uint64_t SBData::GetUnsignedInt64(lldb::SBError &error, lldb::offset_t offset) {222LLDB_INSTRUMENT_VA(this, error, offset);223224uint64_t value = 0;225if (!m_opaque_sp.get()) {226error.SetErrorString("no value to read from");227} else {228uint32_t old_offset = offset;229value = m_opaque_sp->GetU64(&offset);230if (offset == old_offset)231error.SetErrorString("unable to read data");232}233return value;234}235236int8_t SBData::GetSignedInt8(lldb::SBError &error, lldb::offset_t offset) {237LLDB_INSTRUMENT_VA(this, error, offset);238239int8_t value = 0;240if (!m_opaque_sp.get()) {241error.SetErrorString("no value to read from");242} else {243uint32_t old_offset = offset;244value = (int8_t)m_opaque_sp->GetMaxS64(&offset, 1);245if (offset == old_offset)246error.SetErrorString("unable to read data");247}248return value;249}250251int16_t SBData::GetSignedInt16(lldb::SBError &error, lldb::offset_t offset) {252LLDB_INSTRUMENT_VA(this, error, offset);253254int16_t value = 0;255if (!m_opaque_sp.get()) {256error.SetErrorString("no value to read from");257} else {258uint32_t old_offset = offset;259value = (int16_t)m_opaque_sp->GetMaxS64(&offset, 2);260if (offset == old_offset)261error.SetErrorString("unable to read data");262}263return value;264}265266int32_t SBData::GetSignedInt32(lldb::SBError &error, lldb::offset_t offset) {267LLDB_INSTRUMENT_VA(this, error, offset);268269int32_t value = 0;270if (!m_opaque_sp.get()) {271error.SetErrorString("no value to read from");272} else {273uint32_t old_offset = offset;274value = (int32_t)m_opaque_sp->GetMaxS64(&offset, 4);275if (offset == old_offset)276error.SetErrorString("unable to read data");277}278return value;279}280281int64_t SBData::GetSignedInt64(lldb::SBError &error, lldb::offset_t offset) {282LLDB_INSTRUMENT_VA(this, error, offset);283284int64_t value = 0;285if (!m_opaque_sp.get()) {286error.SetErrorString("no value to read from");287} else {288uint32_t old_offset = offset;289value = (int64_t)m_opaque_sp->GetMaxS64(&offset, 8);290if (offset == old_offset)291error.SetErrorString("unable to read data");292}293return value;294}295296const char *SBData::GetString(lldb::SBError &error, lldb::offset_t offset) {297LLDB_INSTRUMENT_VA(this, error, offset);298299if (!m_opaque_sp) {300error.SetErrorString("no value to read from");301return nullptr;302}303304lldb::offset_t old_offset = offset;305const char *value = m_opaque_sp->GetCStr(&offset);306if (offset == old_offset || value == nullptr) {307error.SetErrorString("unable to read data");308return nullptr;309}310311return ConstString(value).GetCString();312}313314bool SBData::GetDescription(lldb::SBStream &description,315lldb::addr_t base_addr) {316LLDB_INSTRUMENT_VA(this, description, base_addr);317318Stream &strm = description.ref();319320if (m_opaque_sp) {321DumpDataExtractor(*m_opaque_sp, &strm, 0, lldb::eFormatBytesWithASCII, 1,322m_opaque_sp->GetByteSize(), 16, base_addr, 0, 0);323} else324strm.PutCString("No value");325326return true;327}328329size_t SBData::ReadRawData(lldb::SBError &error, lldb::offset_t offset,330void *buf, size_t size) {331LLDB_INSTRUMENT_VA(this, error, offset, buf, size);332333void *ok = nullptr;334if (!m_opaque_sp.get()) {335error.SetErrorString("no value to read from");336} else {337uint32_t old_offset = offset;338ok = m_opaque_sp->GetU8(&offset, buf, size);339if ((offset == old_offset) || (ok == nullptr))340error.SetErrorString("unable to read data");341}342return ok ? size : 0;343}344345void SBData::SetData(lldb::SBError &error, const void *buf, size_t size,346lldb::ByteOrder endian, uint8_t addr_size) {347LLDB_INSTRUMENT_VA(this, error, buf, size, endian, addr_size);348349if (!m_opaque_sp.get())350m_opaque_sp = std::make_shared<DataExtractor>(buf, size, endian, addr_size);351else352{353m_opaque_sp->SetData(buf, size, endian);354m_opaque_sp->SetAddressByteSize(addr_size);355}356}357358void SBData::SetDataWithOwnership(lldb::SBError &error, const void *buf,359size_t size, lldb::ByteOrder endian,360uint8_t addr_size) {361LLDB_INSTRUMENT_VA(this, error, buf, size, endian, addr_size);362363lldb::DataBufferSP buffer_sp = std::make_shared<DataBufferHeap>(buf, size);364365if (!m_opaque_sp.get())366m_opaque_sp = std::make_shared<DataExtractor>(buf, size, endian, addr_size);367else {368m_opaque_sp->SetData(buffer_sp);369m_opaque_sp->SetByteOrder(endian);370m_opaque_sp->SetAddressByteSize(addr_size);371}372}373374bool SBData::Append(const SBData &rhs) {375LLDB_INSTRUMENT_VA(this, rhs);376377bool value = false;378if (m_opaque_sp.get() && rhs.m_opaque_sp.get())379value = m_opaque_sp.get()->Append(*rhs.m_opaque_sp);380return value;381}382383lldb::SBData SBData::CreateDataFromCString(lldb::ByteOrder endian,384uint32_t addr_byte_size,385const char *data) {386LLDB_INSTRUMENT_VA(endian, addr_byte_size, data);387388if (!data || !data[0])389return SBData();390391uint32_t data_len = strlen(data);392393lldb::DataBufferSP buffer_sp(new DataBufferHeap(data, data_len));394lldb::DataExtractorSP data_sp(395new DataExtractor(buffer_sp, endian, addr_byte_size));396397SBData ret(data_sp);398399return ret;400}401402lldb::SBData SBData::CreateDataFromUInt64Array(lldb::ByteOrder endian,403uint32_t addr_byte_size,404uint64_t *array,405size_t array_len) {406LLDB_INSTRUMENT_VA(endian, addr_byte_size, array, array_len);407408if (!array || array_len == 0)409return SBData();410411size_t data_len = array_len * sizeof(uint64_t);412413lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));414lldb::DataExtractorSP data_sp(415new DataExtractor(buffer_sp, endian, addr_byte_size));416417SBData ret(data_sp);418419return ret;420}421422lldb::SBData SBData::CreateDataFromUInt32Array(lldb::ByteOrder endian,423uint32_t addr_byte_size,424uint32_t *array,425size_t array_len) {426LLDB_INSTRUMENT_VA(endian, addr_byte_size, array, array_len);427428if (!array || array_len == 0)429return SBData();430431size_t data_len = array_len * sizeof(uint32_t);432433lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));434lldb::DataExtractorSP data_sp(435new DataExtractor(buffer_sp, endian, addr_byte_size));436437SBData ret(data_sp);438439return ret;440}441442lldb::SBData SBData::CreateDataFromSInt64Array(lldb::ByteOrder endian,443uint32_t addr_byte_size,444int64_t *array,445size_t array_len) {446LLDB_INSTRUMENT_VA(endian, addr_byte_size, array, array_len);447448if (!array || array_len == 0)449return SBData();450451size_t data_len = array_len * sizeof(int64_t);452453lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));454lldb::DataExtractorSP data_sp(455new DataExtractor(buffer_sp, endian, addr_byte_size));456457SBData ret(data_sp);458459return ret;460}461462lldb::SBData SBData::CreateDataFromSInt32Array(lldb::ByteOrder endian,463uint32_t addr_byte_size,464int32_t *array,465size_t array_len) {466LLDB_INSTRUMENT_VA(endian, addr_byte_size, array, array_len);467468if (!array || array_len == 0)469return SBData();470471size_t data_len = array_len * sizeof(int32_t);472473lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));474lldb::DataExtractorSP data_sp(475new DataExtractor(buffer_sp, endian, addr_byte_size));476477SBData ret(data_sp);478479return ret;480}481482lldb::SBData SBData::CreateDataFromDoubleArray(lldb::ByteOrder endian,483uint32_t addr_byte_size,484double *array,485size_t array_len) {486LLDB_INSTRUMENT_VA(endian, addr_byte_size, array, array_len);487488if (!array || array_len == 0)489return SBData();490491size_t data_len = array_len * sizeof(double);492493lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));494lldb::DataExtractorSP data_sp(495new DataExtractor(buffer_sp, endian, addr_byte_size));496497SBData ret(data_sp);498499return ret;500}501502bool SBData::SetDataFromCString(const char *data) {503LLDB_INSTRUMENT_VA(this, data);504505if (!data) {506return false;507}508509size_t data_len = strlen(data);510511lldb::DataBufferSP buffer_sp(new DataBufferHeap(data, data_len));512513if (!m_opaque_sp.get())514m_opaque_sp = std::make_shared<DataExtractor>(buffer_sp, GetByteOrder(),515GetAddressByteSize());516else517m_opaque_sp->SetData(buffer_sp);518519520return true;521}522523bool SBData::SetDataFromUInt64Array(uint64_t *array, size_t array_len) {524LLDB_INSTRUMENT_VA(this, array, array_len);525526if (!array || array_len == 0) {527return false;528}529530size_t data_len = array_len * sizeof(uint64_t);531532lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));533534if (!m_opaque_sp.get())535m_opaque_sp = std::make_shared<DataExtractor>(buffer_sp, GetByteOrder(),536GetAddressByteSize());537else538m_opaque_sp->SetData(buffer_sp);539540541return true;542}543544bool SBData::SetDataFromUInt32Array(uint32_t *array, size_t array_len) {545LLDB_INSTRUMENT_VA(this, array, array_len);546547if (!array || array_len == 0) {548return false;549}550551size_t data_len = array_len * sizeof(uint32_t);552553lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));554555if (!m_opaque_sp.get())556m_opaque_sp = std::make_shared<DataExtractor>(buffer_sp, GetByteOrder(),557GetAddressByteSize());558else559m_opaque_sp->SetData(buffer_sp);560561return true;562}563564bool SBData::SetDataFromSInt64Array(int64_t *array, size_t array_len) {565LLDB_INSTRUMENT_VA(this, array, array_len);566567if (!array || array_len == 0) {568return false;569}570571size_t data_len = array_len * sizeof(int64_t);572573lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));574575if (!m_opaque_sp.get())576m_opaque_sp = std::make_shared<DataExtractor>(buffer_sp, GetByteOrder(),577GetAddressByteSize());578else579m_opaque_sp->SetData(buffer_sp);580581return true;582}583584bool SBData::SetDataFromSInt32Array(int32_t *array, size_t array_len) {585LLDB_INSTRUMENT_VA(this, array, array_len);586587if (!array || array_len == 0) {588return false;589}590591size_t data_len = array_len * sizeof(int32_t);592593lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));594595if (!m_opaque_sp.get())596m_opaque_sp = std::make_shared<DataExtractor>(buffer_sp, GetByteOrder(),597GetAddressByteSize());598else599m_opaque_sp->SetData(buffer_sp);600601return true;602}603604bool SBData::SetDataFromDoubleArray(double *array, size_t array_len) {605LLDB_INSTRUMENT_VA(this, array, array_len);606607if (!array || array_len == 0) {608return false;609}610611size_t data_len = array_len * sizeof(double);612613lldb::DataBufferSP buffer_sp(new DataBufferHeap(array, data_len));614615if (!m_opaque_sp.get())616m_opaque_sp = std::make_shared<DataExtractor>(buffer_sp, GetByteOrder(),617GetAddressByteSize());618else619m_opaque_sp->SetData(buffer_sp);620621return true;622}623624625