15ffd83dbSDimitry 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) 32fe6060f1SDimitry Andric : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up)) { 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 42*0eae32dcSDimitry Andric SBStructuredData::SBStructuredData(const lldb_private::StructuredDataImpl &impl) 43*0eae32dcSDimitry Andric : m_impl_up(new StructuredDataImpl(impl)) { 440b57cec5SDimitry Andric LLDB_RECORD_CONSTRUCTOR(SBStructuredData, 45*0eae32dcSDimitry Andric (const lldb_private::StructuredDataImpl &), impl); 460b57cec5SDimitry Andric } 470b57cec5SDimitry Andric 485ffd83dbSDimitry 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 75fe6060f1SDimitry Andric lldb::SBError SBStructuredData::SetFromJSON(const char *json) { 76fe6060f1SDimitry Andric LLDB_RECORD_METHOD(lldb::SBError, SBStructuredData, SetFromJSON, 77fe6060f1SDimitry Andric (const char *), json); 78fe6060f1SDimitry Andric lldb::SBStream s; 79fe6060f1SDimitry Andric s.Print(json); 80fe6060f1SDimitry Andric return LLDB_RECORD_RESULT(SetFromJSON(s)); 81fe6060f1SDimitry Andric } 82fe6060f1SDimitry Andric 830b57cec5SDimitry Andric bool SBStructuredData::IsValid() const { 840b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStructuredData, IsValid); 850b57cec5SDimitry Andric return this->operator bool(); 860b57cec5SDimitry Andric } 87fe6060f1SDimitry Andric 880b57cec5SDimitry Andric SBStructuredData::operator bool() const { 890b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStructuredData, operator bool); 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric return m_impl_up->IsValid(); 920b57cec5SDimitry Andric } 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric void SBStructuredData::Clear() { 950b57cec5SDimitry Andric LLDB_RECORD_METHOD_NO_ARGS(void, SBStructuredData, Clear); 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric m_impl_up->Clear(); 980b57cec5SDimitry Andric } 990b57cec5SDimitry Andric 1000b57cec5SDimitry Andric SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const { 1010b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST(lldb::SBError, SBStructuredData, GetAsJSON, 1020b57cec5SDimitry Andric (lldb::SBStream &), stream); 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric SBError error; 1050b57cec5SDimitry Andric error.SetError(m_impl_up->GetAsJSON(stream.ref())); 1060b57cec5SDimitry Andric return LLDB_RECORD_RESULT(error); 1070b57cec5SDimitry Andric } 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const { 1100b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST(lldb::SBError, SBStructuredData, GetDescription, 1110b57cec5SDimitry Andric (lldb::SBStream &), stream); 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric Status error = m_impl_up->GetDescription(stream.ref()); 1140b57cec5SDimitry Andric SBError sb_error; 1150b57cec5SDimitry Andric sb_error.SetError(error); 1160b57cec5SDimitry Andric return LLDB_RECORD_RESULT(sb_error); 1170b57cec5SDimitry Andric } 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric StructuredDataType SBStructuredData::GetType() const { 1200b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::StructuredDataType, SBStructuredData, 1210b57cec5SDimitry Andric GetType); 1220b57cec5SDimitry Andric 123fe6060f1SDimitry Andric return m_impl_up->GetType(); 1240b57cec5SDimitry Andric } 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric size_t SBStructuredData::GetSize() const { 1270b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST_NO_ARGS(size_t, SBStructuredData, GetSize); 1280b57cec5SDimitry Andric 129fe6060f1SDimitry Andric return m_impl_up->GetSize(); 1300b57cec5SDimitry Andric } 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric bool SBStructuredData::GetKeys(lldb::SBStringList &keys) const { 1330b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST(bool, SBStructuredData, GetKeys, 1340b57cec5SDimitry Andric (lldb::SBStringList &), keys); 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric if (GetType() != eStructuredDataTypeDictionary) 1370b57cec5SDimitry Andric return false; 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP(); 1400b57cec5SDimitry Andric if (!obj_sp) 1410b57cec5SDimitry Andric return false; 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric StructuredData::Dictionary *dict = obj_sp->GetAsDictionary(); 1440b57cec5SDimitry Andric // We claimed we were a dictionary, so this can't be null. 1450b57cec5SDimitry Andric assert(dict); 1460b57cec5SDimitry Andric // The return kind of GetKeys is an Array: 1470b57cec5SDimitry Andric StructuredData::ObjectSP array_sp = dict->GetKeys(); 1480b57cec5SDimitry Andric StructuredData::Array *key_arr = array_sp->GetAsArray(); 1490b57cec5SDimitry Andric assert(key_arr); 1500b57cec5SDimitry Andric 1510b57cec5SDimitry Andric key_arr->ForEach([&keys] (StructuredData::Object *object) -> bool { 1520b57cec5SDimitry Andric llvm::StringRef key = object->GetStringValue(""); 1530b57cec5SDimitry Andric keys.AppendString(key.str().c_str()); 1540b57cec5SDimitry Andric return true; 1550b57cec5SDimitry Andric }); 1560b57cec5SDimitry Andric return true; 1570b57cec5SDimitry Andric } 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric lldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const { 1600b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST(lldb::SBStructuredData, SBStructuredData, 1610b57cec5SDimitry Andric GetValueForKey, (const char *), key); 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric SBStructuredData result; 1640b57cec5SDimitry Andric result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key)); 1650b57cec5SDimitry Andric return LLDB_RECORD_RESULT(result); 1660b57cec5SDimitry Andric } 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric lldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const { 1690b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST(lldb::SBStructuredData, SBStructuredData, 1700b57cec5SDimitry Andric GetItemAtIndex, (size_t), idx); 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 181fe6060f1SDimitry Andric return m_impl_up->GetIntegerValue(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 188fe6060f1SDimitry Andric return m_impl_up->GetFloatValue(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 195fe6060f1SDimitry Andric return m_impl_up->GetBooleanValue(fail_value); 1960b57cec5SDimitry Andric } 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const { 1995ffd83dbSDimitry Andric LLDB_RECORD_CHAR_PTR_METHOD_CONST(size_t, SBStructuredData, GetStringValue, 2005ffd83dbSDimitry Andric (char *, size_t), dst, "", dst_len); 2010b57cec5SDimitry Andric 202fe6060f1SDimitry Andric return m_impl_up->GetStringValue(dst, dst_len); 2030b57cec5SDimitry Andric } 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric namespace lldb_private { 2060b57cec5SDimitry Andric namespace repro { 2070b57cec5SDimitry Andric 2085ffd83dbSDimitry Andric template <> void RegisterMethods<SBStructuredData>(Registry &R) { 2090b57cec5SDimitry Andric LLDB_REGISTER_CONSTRUCTOR(SBStructuredData, ()); 2105ffd83dbSDimitry Andric LLDB_REGISTER_CONSTRUCTOR(SBStructuredData, (const lldb::SBStructuredData &)); 2110b57cec5SDimitry Andric LLDB_REGISTER_CONSTRUCTOR(SBStructuredData, (const lldb::EventSP &)); 2120b57cec5SDimitry Andric LLDB_REGISTER_CONSTRUCTOR(SBStructuredData, 213*0eae32dcSDimitry Andric (const 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 &)); 219fe6060f1SDimitry Andric LLDB_REGISTER_METHOD(lldb::SBError, SBStructuredData, SetFromJSON, 220fe6060f1SDimitry Andric (const char *)); 2210b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, IsValid, ()); 2220b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, operator bool, ()); 2230b57cec5SDimitry Andric LLDB_REGISTER_METHOD(void, SBStructuredData, Clear, ()); 2240b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(lldb::SBError, SBStructuredData, GetAsJSON, 2250b57cec5SDimitry Andric (lldb::SBStream &)); 2260b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(lldb::SBError, SBStructuredData, GetDescription, 2270b57cec5SDimitry Andric (lldb::SBStream &)); 2280b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(lldb::StructuredDataType, SBStructuredData, 2290b57cec5SDimitry Andric GetType, ()); 2300b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(size_t, SBStructuredData, GetSize, ()); 2310b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, GetKeys, 2320b57cec5SDimitry Andric (lldb::SBStringList &)); 2330b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(lldb::SBStructuredData, SBStructuredData, 2340b57cec5SDimitry Andric GetValueForKey, (const char *)); 2350b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(lldb::SBStructuredData, SBStructuredData, 2360b57cec5SDimitry Andric GetItemAtIndex, (size_t)); 2370b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(uint64_t, SBStructuredData, GetIntegerValue, 2380b57cec5SDimitry Andric (uint64_t)); 2395ffd83dbSDimitry Andric LLDB_REGISTER_METHOD_CONST(double, SBStructuredData, GetFloatValue, (double)); 2400b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, GetBooleanValue, (bool)); 2415ffd83dbSDimitry Andric LLDB_REGISTER_CHAR_PTR_METHOD_CONST(size_t, SBStructuredData, GetStringValue); 2420b57cec5SDimitry Andric } 2430b57cec5SDimitry Andric 2445ffd83dbSDimitry Andric } // namespace repro 2455ffd83dbSDimitry Andric } // namespace lldb_private 246