1*5ffd83dbSDimitry Andric //===-- SBStructuredData.cpp ----------------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "lldb/API/SBStructuredData.h" 100b57cec5SDimitry Andric #include "SBReproducerPrivate.h" 110b57cec5SDimitry Andric 120b57cec5SDimitry Andric #include "lldb/API/SBStream.h" 130b57cec5SDimitry Andric #include "lldb/API/SBStringList.h" 140b57cec5SDimitry Andric #include "lldb/Core/StructuredDataImpl.h" 150b57cec5SDimitry Andric #include "lldb/Target/StructuredDataPlugin.h" 160b57cec5SDimitry Andric #include "lldb/Utility/Event.h" 170b57cec5SDimitry Andric #include "lldb/Utility/Status.h" 180b57cec5SDimitry Andric #include "lldb/Utility/Stream.h" 190b57cec5SDimitry Andric #include "lldb/Utility/StructuredData.h" 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric using namespace lldb; 220b57cec5SDimitry Andric using namespace lldb_private; 230b57cec5SDimitry Andric 240b57cec5SDimitry Andric #pragma mark-- 250b57cec5SDimitry Andric #pragma mark SBStructuredData 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric SBStructuredData::SBStructuredData() : m_impl_up(new StructuredDataImpl()) { 280b57cec5SDimitry Andric LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBStructuredData); 290b57cec5SDimitry Andric } 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs) 320b57cec5SDimitry Andric : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up.get())) { 330b57cec5SDimitry Andric LLDB_RECORD_CONSTRUCTOR(SBStructuredData, (const lldb::SBStructuredData &), 340b57cec5SDimitry Andric rhs); 350b57cec5SDimitry Andric } 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp) 380b57cec5SDimitry Andric : m_impl_up(new StructuredDataImpl(event_sp)) { 390b57cec5SDimitry Andric LLDB_RECORD_CONSTRUCTOR(SBStructuredData, (const lldb::EventSP &), event_sp); 400b57cec5SDimitry Andric } 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric SBStructuredData::SBStructuredData(lldb_private::StructuredDataImpl *impl) 430b57cec5SDimitry Andric : m_impl_up(impl) { 440b57cec5SDimitry Andric LLDB_RECORD_CONSTRUCTOR(SBStructuredData, 450b57cec5SDimitry Andric (lldb_private::StructuredDataImpl *), impl); 460b57cec5SDimitry Andric } 470b57cec5SDimitry Andric 48*5ffd83dbSDimitry Andric SBStructuredData::~SBStructuredData() = default; 490b57cec5SDimitry Andric 500b57cec5SDimitry Andric SBStructuredData &SBStructuredData:: 510b57cec5SDimitry Andric operator=(const lldb::SBStructuredData &rhs) { 520b57cec5SDimitry Andric LLDB_RECORD_METHOD( 530b57cec5SDimitry Andric lldb::SBStructuredData &, 540b57cec5SDimitry Andric SBStructuredData, operator=,(const lldb::SBStructuredData &), rhs); 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric *m_impl_up = *rhs.m_impl_up; 570b57cec5SDimitry Andric return LLDB_RECORD_RESULT(*this); 580b57cec5SDimitry Andric } 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric lldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream &stream) { 610b57cec5SDimitry Andric LLDB_RECORD_METHOD(lldb::SBError, SBStructuredData, SetFromJSON, 620b57cec5SDimitry Andric (lldb::SBStream &), stream); 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric lldb::SBError error; 650b57cec5SDimitry Andric std::string json_str(stream.GetData()); 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric StructuredData::ObjectSP json_obj = StructuredData::ParseJSON(json_str); 680b57cec5SDimitry Andric m_impl_up->SetObjectSP(json_obj); 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary) 710b57cec5SDimitry Andric error.SetErrorString("Invalid Syntax"); 720b57cec5SDimitry Andric return LLDB_RECORD_RESULT(error); 730b57cec5SDimitry Andric } 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric bool SBStructuredData::IsValid() const { 760b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStructuredData, IsValid); 770b57cec5SDimitry Andric return this->operator bool(); 780b57cec5SDimitry Andric } 790b57cec5SDimitry Andric SBStructuredData::operator bool() const { 800b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStructuredData, operator bool); 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric return m_impl_up->IsValid(); 830b57cec5SDimitry Andric } 840b57cec5SDimitry Andric 850b57cec5SDimitry Andric void SBStructuredData::Clear() { 860b57cec5SDimitry Andric LLDB_RECORD_METHOD_NO_ARGS(void, SBStructuredData, Clear); 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric m_impl_up->Clear(); 890b57cec5SDimitry Andric } 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const { 920b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST(lldb::SBError, SBStructuredData, GetAsJSON, 930b57cec5SDimitry Andric (lldb::SBStream &), stream); 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric SBError error; 960b57cec5SDimitry Andric error.SetError(m_impl_up->GetAsJSON(stream.ref())); 970b57cec5SDimitry Andric return LLDB_RECORD_RESULT(error); 980b57cec5SDimitry Andric } 990b57cec5SDimitry Andric 1000b57cec5SDimitry Andric lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const { 1010b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST(lldb::SBError, SBStructuredData, GetDescription, 1020b57cec5SDimitry Andric (lldb::SBStream &), stream); 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric Status error = m_impl_up->GetDescription(stream.ref()); 1050b57cec5SDimitry Andric SBError sb_error; 1060b57cec5SDimitry Andric sb_error.SetError(error); 1070b57cec5SDimitry Andric return LLDB_RECORD_RESULT(sb_error); 1080b57cec5SDimitry Andric } 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric StructuredDataType SBStructuredData::GetType() const { 1110b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::StructuredDataType, SBStructuredData, 1120b57cec5SDimitry Andric GetType); 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric return (m_impl_up ? m_impl_up->GetType() : eStructuredDataTypeInvalid); 1150b57cec5SDimitry Andric } 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric size_t SBStructuredData::GetSize() const { 1180b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST_NO_ARGS(size_t, SBStructuredData, GetSize); 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric return (m_impl_up ? m_impl_up->GetSize() : 0); 1210b57cec5SDimitry Andric } 1220b57cec5SDimitry Andric 1230b57cec5SDimitry Andric bool SBStructuredData::GetKeys(lldb::SBStringList &keys) const { 1240b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST(bool, SBStructuredData, GetKeys, 1250b57cec5SDimitry Andric (lldb::SBStringList &), keys); 1260b57cec5SDimitry Andric 1270b57cec5SDimitry Andric if (!m_impl_up) 1280b57cec5SDimitry Andric return false; 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric if (GetType() != eStructuredDataTypeDictionary) 1310b57cec5SDimitry Andric return false; 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP(); 1340b57cec5SDimitry Andric if (!obj_sp) 1350b57cec5SDimitry Andric return false; 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric StructuredData::Dictionary *dict = obj_sp->GetAsDictionary(); 1380b57cec5SDimitry Andric // We claimed we were a dictionary, so this can't be null. 1390b57cec5SDimitry Andric assert(dict); 1400b57cec5SDimitry Andric // The return kind of GetKeys is an Array: 1410b57cec5SDimitry Andric StructuredData::ObjectSP array_sp = dict->GetKeys(); 1420b57cec5SDimitry Andric StructuredData::Array *key_arr = array_sp->GetAsArray(); 1430b57cec5SDimitry Andric assert(key_arr); 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric key_arr->ForEach([&keys] (StructuredData::Object *object) -> bool { 1460b57cec5SDimitry Andric llvm::StringRef key = object->GetStringValue(""); 1470b57cec5SDimitry Andric keys.AppendString(key.str().c_str()); 1480b57cec5SDimitry Andric return true; 1490b57cec5SDimitry Andric }); 1500b57cec5SDimitry Andric return true; 1510b57cec5SDimitry Andric } 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric lldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const { 1540b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST(lldb::SBStructuredData, SBStructuredData, 1550b57cec5SDimitry Andric GetValueForKey, (const char *), key); 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric if (!m_impl_up) 1580b57cec5SDimitry Andric return LLDB_RECORD_RESULT(SBStructuredData()); 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric SBStructuredData result; 1610b57cec5SDimitry Andric result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key)); 1620b57cec5SDimitry Andric return LLDB_RECORD_RESULT(result); 1630b57cec5SDimitry Andric } 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric lldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const { 1660b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST(lldb::SBStructuredData, SBStructuredData, 1670b57cec5SDimitry Andric GetItemAtIndex, (size_t), idx); 1680b57cec5SDimitry Andric 1690b57cec5SDimitry Andric if (!m_impl_up) 1700b57cec5SDimitry Andric return LLDB_RECORD_RESULT(SBStructuredData()); 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric SBStructuredData result; 1730b57cec5SDimitry Andric result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx)); 1740b57cec5SDimitry Andric return LLDB_RECORD_RESULT(result); 1750b57cec5SDimitry Andric } 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const { 1780b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST(uint64_t, SBStructuredData, GetIntegerValue, 1790b57cec5SDimitry Andric (uint64_t), fail_value); 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric return (m_impl_up ? m_impl_up->GetIntegerValue(fail_value) : fail_value); 1820b57cec5SDimitry Andric } 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric double SBStructuredData::GetFloatValue(double fail_value) const { 1850b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST(double, SBStructuredData, GetFloatValue, (double), 1860b57cec5SDimitry Andric fail_value); 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric return (m_impl_up ? m_impl_up->GetFloatValue(fail_value) : fail_value); 1890b57cec5SDimitry Andric } 1900b57cec5SDimitry Andric 1910b57cec5SDimitry Andric bool SBStructuredData::GetBooleanValue(bool fail_value) const { 1920b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST(bool, SBStructuredData, GetBooleanValue, (bool), 1930b57cec5SDimitry Andric fail_value); 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric return (m_impl_up ? m_impl_up->GetBooleanValue(fail_value) : fail_value); 1960b57cec5SDimitry Andric } 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const { 199*5ffd83dbSDimitry Andric LLDB_RECORD_CHAR_PTR_METHOD_CONST(size_t, SBStructuredData, GetStringValue, 200*5ffd83dbSDimitry Andric (char *, size_t), dst, "", dst_len); 2010b57cec5SDimitry Andric 2020b57cec5SDimitry Andric return (m_impl_up ? m_impl_up->GetStringValue(dst, dst_len) : 0); 2030b57cec5SDimitry Andric } 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric namespace lldb_private { 2060b57cec5SDimitry Andric namespace repro { 2070b57cec5SDimitry Andric 208*5ffd83dbSDimitry Andric template <> void RegisterMethods<SBStructuredData>(Registry &R) { 2090b57cec5SDimitry Andric LLDB_REGISTER_CONSTRUCTOR(SBStructuredData, ()); 210*5ffd83dbSDimitry Andric LLDB_REGISTER_CONSTRUCTOR(SBStructuredData, (const lldb::SBStructuredData &)); 2110b57cec5SDimitry Andric LLDB_REGISTER_CONSTRUCTOR(SBStructuredData, (const lldb::EventSP &)); 2120b57cec5SDimitry Andric LLDB_REGISTER_CONSTRUCTOR(SBStructuredData, 2130b57cec5SDimitry Andric (lldb_private::StructuredDataImpl *)); 2140b57cec5SDimitry Andric LLDB_REGISTER_METHOD( 2150b57cec5SDimitry Andric lldb::SBStructuredData &, 2160b57cec5SDimitry Andric SBStructuredData, operator=,(const lldb::SBStructuredData &)); 2170b57cec5SDimitry Andric LLDB_REGISTER_METHOD(lldb::SBError, SBStructuredData, SetFromJSON, 2180b57cec5SDimitry Andric (lldb::SBStream &)); 2190b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, IsValid, ()); 2200b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, operator bool, ()); 2210b57cec5SDimitry Andric LLDB_REGISTER_METHOD(void, SBStructuredData, Clear, ()); 2220b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(lldb::SBError, SBStructuredData, GetAsJSON, 2230b57cec5SDimitry Andric (lldb::SBStream &)); 2240b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(lldb::SBError, SBStructuredData, GetDescription, 2250b57cec5SDimitry Andric (lldb::SBStream &)); 2260b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(lldb::StructuredDataType, SBStructuredData, 2270b57cec5SDimitry Andric GetType, ()); 2280b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(size_t, SBStructuredData, GetSize, ()); 2290b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, GetKeys, 2300b57cec5SDimitry Andric (lldb::SBStringList &)); 2310b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(lldb::SBStructuredData, SBStructuredData, 2320b57cec5SDimitry Andric GetValueForKey, (const char *)); 2330b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(lldb::SBStructuredData, SBStructuredData, 2340b57cec5SDimitry Andric GetItemAtIndex, (size_t)); 2350b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(uint64_t, SBStructuredData, GetIntegerValue, 2360b57cec5SDimitry Andric (uint64_t)); 237*5ffd83dbSDimitry Andric LLDB_REGISTER_METHOD_CONST(double, SBStructuredData, GetFloatValue, (double)); 2380b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, GetBooleanValue, (bool)); 239*5ffd83dbSDimitry Andric LLDB_REGISTER_CHAR_PTR_METHOD_CONST(size_t, SBStructuredData, GetStringValue); 2400b57cec5SDimitry Andric } 2410b57cec5SDimitry Andric 242*5ffd83dbSDimitry Andric } // namespace repro 243*5ffd83dbSDimitry Andric } // namespace lldb_private 244