175930019STodd Fiala //===-- SBStructuredData.cpp ------------------------------------*- C++ -*-===// 275930019STodd Fiala // 3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 675930019STodd Fiala // 775930019STodd Fiala //===----------------------------------------------------------------------===// 875930019STodd Fiala 975930019STodd Fiala #include "lldb/API/SBStructuredData.h" 1075930019STodd Fiala 1175930019STodd Fiala #include "lldb/API/SBStream.h" 123815e702SJim Ingham #include "lldb/API/SBStringList.h" 13d5d8d91cSRavitheja Addepally #include "lldb/Core/StructuredDataImpl.h" 1475930019STodd Fiala #include "lldb/Target/StructuredDataPlugin.h" 15181b823bSPavel Labath #include "lldb/Utility/Event.h" 1697206d57SZachary Turner #include "lldb/Utility/Status.h" 17bf9a7730SZachary Turner #include "lldb/Utility/Stream.h" 18f2a8bccfSPavel Labath #include "lldb/Utility/StructuredData.h" 1975930019STodd Fiala 2075930019STodd Fiala using namespace lldb; 2175930019STodd Fiala using namespace lldb_private; 2275930019STodd Fiala 2375930019STodd Fiala #pragma mark-- 2475930019STodd Fiala #pragma mark SBStructuredData 2575930019STodd Fiala 262ef442c6STodd Fiala SBStructuredData::SBStructuredData() : m_impl_up(new StructuredDataImpl()) {} 2775930019STodd Fiala 28b9c1b51eSKate Stone SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs) 292ef442c6STodd Fiala : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up.get())) {} 3075930019STodd Fiala 31b9c1b51eSKate Stone SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp) 322ef442c6STodd Fiala : m_impl_up(new StructuredDataImpl(event_sp)) {} 3375930019STodd Fiala 343815e702SJim Ingham SBStructuredData::SBStructuredData(lldb_private::StructuredDataImpl *impl) 353815e702SJim Ingham : m_impl_up(impl) {} 363815e702SJim Ingham 37b9c1b51eSKate Stone SBStructuredData::~SBStructuredData() {} 3875930019STodd Fiala 39b9c1b51eSKate Stone SBStructuredData &SBStructuredData:: 40b9c1b51eSKate Stone operator=(const lldb::SBStructuredData &rhs) { 4175930019STodd Fiala *m_impl_up = *rhs.m_impl_up; 4275930019STodd Fiala return *this; 4375930019STodd Fiala } 4475930019STodd Fiala 45d5d8d91cSRavitheja Addepally lldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream &stream) { 46d5d8d91cSRavitheja Addepally lldb::SBError error; 47d5d8d91cSRavitheja Addepally std::string json_str(stream.GetData()); 48d5d8d91cSRavitheja Addepally 49d5d8d91cSRavitheja Addepally StructuredData::ObjectSP json_obj = StructuredData::ParseJSON(json_str); 50d5d8d91cSRavitheja Addepally m_impl_up->SetObjectSP(json_obj); 51d5d8d91cSRavitheja Addepally 525bfee5f1SAbhishek Aggarwal if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary) 53d5d8d91cSRavitheja Addepally error.SetErrorString("Invalid Syntax"); 54d5d8d91cSRavitheja Addepally return error; 55d5d8d91cSRavitheja Addepally } 56d5d8d91cSRavitheja Addepally 57b9c1b51eSKate Stone bool SBStructuredData::IsValid() const { return m_impl_up->IsValid(); } 5875930019STodd Fiala 59b9c1b51eSKate Stone void SBStructuredData::Clear() { m_impl_up->Clear(); } 6075930019STodd Fiala 61b9c1b51eSKate Stone SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const { 62d5d8d91cSRavitheja Addepally SBError error; 63d5d8d91cSRavitheja Addepally error.SetError(m_impl_up->GetAsJSON(stream.ref())); 64d5d8d91cSRavitheja Addepally return error; 6575930019STodd Fiala } 6675930019STodd Fiala 67b9c1b51eSKate Stone lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const { 6897206d57SZachary Turner Status error = m_impl_up->GetDescription(stream.ref()); 692ef442c6STodd Fiala SBError sb_error; 702ef442c6STodd Fiala sb_error.SetError(error); 712ef442c6STodd Fiala return sb_error; 7275930019STodd Fiala } 735bfee5f1SAbhishek Aggarwal 745bfee5f1SAbhishek Aggarwal StructuredDataType SBStructuredData::GetType() const { 755bfee5f1SAbhishek Aggarwal return (m_impl_up ? m_impl_up->GetType() : eStructuredDataTypeInvalid); 765bfee5f1SAbhishek Aggarwal } 775bfee5f1SAbhishek Aggarwal 785bfee5f1SAbhishek Aggarwal size_t SBStructuredData::GetSize() const { 795bfee5f1SAbhishek Aggarwal return (m_impl_up ? m_impl_up->GetSize() : 0); 805bfee5f1SAbhishek Aggarwal } 815bfee5f1SAbhishek Aggarwal 823815e702SJim Ingham bool SBStructuredData::GetKeys(lldb::SBStringList &keys) const { 833815e702SJim Ingham if (!m_impl_up) 843815e702SJim Ingham return false; 853815e702SJim Ingham 863815e702SJim Ingham if (GetType() != eStructuredDataTypeDictionary) 873815e702SJim Ingham return false; 883815e702SJim Ingham 893815e702SJim Ingham StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP(); 903815e702SJim Ingham if (!obj_sp) 913815e702SJim Ingham return false; 923815e702SJim Ingham 933815e702SJim Ingham StructuredData::Dictionary *dict = obj_sp->GetAsDictionary(); 943815e702SJim Ingham // We claimed we were a dictionary, so this can't be null. 953815e702SJim Ingham assert(dict); 963815e702SJim Ingham // The return kind of GetKeys is an Array: 973815e702SJim Ingham StructuredData::ObjectSP array_sp = dict->GetKeys(); 983815e702SJim Ingham StructuredData::Array *key_arr = array_sp->GetAsArray(); 993815e702SJim Ingham assert(key_arr); 1003815e702SJim Ingham 1013815e702SJim Ingham key_arr->ForEach([&keys] (StructuredData::Object *object) -> bool { 1023815e702SJim Ingham llvm::StringRef key = object->GetStringValue(""); 1033815e702SJim Ingham keys.AppendString(key.str().c_str()); 1043815e702SJim Ingham return true; 1053815e702SJim Ingham }); 1063815e702SJim Ingham return true; 1073815e702SJim Ingham } 1083815e702SJim Ingham 1095bfee5f1SAbhishek Aggarwal lldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const { 1105bfee5f1SAbhishek Aggarwal if (!m_impl_up) 1115bfee5f1SAbhishek Aggarwal return SBStructuredData(); 1125bfee5f1SAbhishek Aggarwal 1135bfee5f1SAbhishek Aggarwal SBStructuredData result; 1145bfee5f1SAbhishek Aggarwal result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key)); 1155bfee5f1SAbhishek Aggarwal return result; 1165bfee5f1SAbhishek Aggarwal } 1175bfee5f1SAbhishek Aggarwal 1185bfee5f1SAbhishek Aggarwal lldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const { 1195bfee5f1SAbhishek Aggarwal if (!m_impl_up) 1205bfee5f1SAbhishek Aggarwal return SBStructuredData(); 1215bfee5f1SAbhishek Aggarwal 1225bfee5f1SAbhishek Aggarwal SBStructuredData result; 1235bfee5f1SAbhishek Aggarwal result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx)); 1245bfee5f1SAbhishek Aggarwal return result; 1255bfee5f1SAbhishek Aggarwal } 1265bfee5f1SAbhishek Aggarwal 1275bfee5f1SAbhishek Aggarwal uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const { 1285bfee5f1SAbhishek Aggarwal return (m_impl_up ? m_impl_up->GetIntegerValue(fail_value) : fail_value); 1295bfee5f1SAbhishek Aggarwal } 1305bfee5f1SAbhishek Aggarwal 1315bfee5f1SAbhishek Aggarwal double SBStructuredData::GetFloatValue(double fail_value) const { 1325bfee5f1SAbhishek Aggarwal return (m_impl_up ? m_impl_up->GetFloatValue(fail_value) : fail_value); 1335bfee5f1SAbhishek Aggarwal } 1345bfee5f1SAbhishek Aggarwal 1355bfee5f1SAbhishek Aggarwal bool SBStructuredData::GetBooleanValue(bool fail_value) const { 1365bfee5f1SAbhishek Aggarwal return (m_impl_up ? m_impl_up->GetBooleanValue(fail_value) : fail_value); 1375bfee5f1SAbhishek Aggarwal } 1385bfee5f1SAbhishek Aggarwal 1395bfee5f1SAbhishek Aggarwal size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const { 1405bfee5f1SAbhishek Aggarwal return (m_impl_up ? m_impl_up->GetStringValue(dst, dst_len) : 0); 1415bfee5f1SAbhishek Aggarwal } 142