180814287SRaphael Isemann //===-- SBStructuredData.cpp ----------------------------------------------===//
275930019STodd Fiala //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
675930019STodd Fiala //
775930019STodd Fiala //===----------------------------------------------------------------------===//
875930019STodd Fiala
975930019STodd Fiala #include "lldb/API/SBStructuredData.h"
10*1755f5b1SJonas Devlieghere #include "lldb/Utility/Instrumentation.h"
1175930019STodd Fiala
1275930019STodd Fiala #include "lldb/API/SBStream.h"
133815e702SJim Ingham #include "lldb/API/SBStringList.h"
14d5d8d91cSRavitheja Addepally #include "lldb/Core/StructuredDataImpl.h"
1575930019STodd Fiala #include "lldb/Target/StructuredDataPlugin.h"
16181b823bSPavel Labath #include "lldb/Utility/Event.h"
1797206d57SZachary Turner #include "lldb/Utility/Status.h"
18bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
19f2a8bccfSPavel Labath #include "lldb/Utility/StructuredData.h"
2075930019STodd Fiala
2175930019STodd Fiala using namespace lldb;
2275930019STodd Fiala using namespace lldb_private;
2375930019STodd Fiala
2475930019STodd Fiala #pragma mark--
2575930019STodd Fiala #pragma mark SBStructuredData
2675930019STodd Fiala
SBStructuredData()27baf5664fSJonas Devlieghere SBStructuredData::SBStructuredData() : m_impl_up(new StructuredDataImpl()) {
28*1755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
29baf5664fSJonas Devlieghere }
3075930019STodd Fiala
SBStructuredData(const lldb::SBStructuredData & rhs)31b9c1b51eSKate Stone SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs)
32c5cf4b8fSDave Lee : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up)) {
33*1755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, rhs);
34baf5664fSJonas Devlieghere }
3575930019STodd Fiala
SBStructuredData(const lldb::EventSP & event_sp)36b9c1b51eSKate Stone SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp)
37baf5664fSJonas Devlieghere : m_impl_up(new StructuredDataImpl(event_sp)) {
38*1755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, event_sp);
39baf5664fSJonas Devlieghere }
4075930019STodd Fiala
SBStructuredData(const lldb_private::StructuredDataImpl & impl)4182de8df2SPavel Labath SBStructuredData::SBStructuredData(const lldb_private::StructuredDataImpl &impl)
4282de8df2SPavel Labath : m_impl_up(new StructuredDataImpl(impl)) {
43*1755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, impl);
44baf5664fSJonas Devlieghere }
453815e702SJim Ingham
46866b7a65SJonas Devlieghere SBStructuredData::~SBStructuredData() = default;
4775930019STodd Fiala
48b9c1b51eSKate Stone SBStructuredData &SBStructuredData::
operator =(const lldb::SBStructuredData & rhs)49b9c1b51eSKate Stone operator=(const lldb::SBStructuredData &rhs) {
50*1755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, rhs);
51baf5664fSJonas Devlieghere
5275930019STodd Fiala *m_impl_up = *rhs.m_impl_up;
53d232abc3SJonas Devlieghere return *this;
5475930019STodd Fiala }
5575930019STodd Fiala
SetFromJSON(lldb::SBStream & stream)56d5d8d91cSRavitheja Addepally lldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream &stream) {
57*1755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, stream);
58baf5664fSJonas Devlieghere
59d5d8d91cSRavitheja Addepally lldb::SBError error;
60d5d8d91cSRavitheja Addepally std::string json_str(stream.GetData());
61d5d8d91cSRavitheja Addepally
62d5d8d91cSRavitheja Addepally StructuredData::ObjectSP json_obj = StructuredData::ParseJSON(json_str);
63d5d8d91cSRavitheja Addepally m_impl_up->SetObjectSP(json_obj);
64d5d8d91cSRavitheja Addepally
655bfee5f1SAbhishek Aggarwal if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary)
66d5d8d91cSRavitheja Addepally error.SetErrorString("Invalid Syntax");
67d232abc3SJonas Devlieghere return error;
68d5d8d91cSRavitheja Addepally }
69d5d8d91cSRavitheja Addepally
SetFromJSON(const char * json)70bf9f21a2SWalter Erquinigo lldb::SBError SBStructuredData::SetFromJSON(const char *json) {
71*1755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, json);
72bf9f21a2SWalter Erquinigo lldb::SBStream s;
73bf9f21a2SWalter Erquinigo s.Print(json);
74d232abc3SJonas Devlieghere return SetFromJSON(s);
75bf9f21a2SWalter Erquinigo }
76bf9f21a2SWalter Erquinigo
IsValid() const77baf5664fSJonas Devlieghere bool SBStructuredData::IsValid() const {
78*1755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
797f5237bcSPavel Labath return this->operator bool();
807f5237bcSPavel Labath }
81bf9f21a2SWalter Erquinigo
operator bool() const827f5237bcSPavel Labath SBStructuredData::operator bool() const {
83*1755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
8475930019STodd Fiala
85baf5664fSJonas Devlieghere return m_impl_up->IsValid();
86baf5664fSJonas Devlieghere }
87baf5664fSJonas Devlieghere
Clear()88baf5664fSJonas Devlieghere void SBStructuredData::Clear() {
89*1755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
90baf5664fSJonas Devlieghere
91baf5664fSJonas Devlieghere m_impl_up->Clear();
92baf5664fSJonas Devlieghere }
9375930019STodd Fiala
GetAsJSON(lldb::SBStream & stream) const94b9c1b51eSKate Stone SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const {
95*1755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, stream);
96baf5664fSJonas Devlieghere
97d5d8d91cSRavitheja Addepally SBError error;
98d5d8d91cSRavitheja Addepally error.SetError(m_impl_up->GetAsJSON(stream.ref()));
99d232abc3SJonas Devlieghere return error;
10075930019STodd Fiala }
10175930019STodd Fiala
GetDescription(lldb::SBStream & stream) const102b9c1b51eSKate Stone lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const {
103*1755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, stream);
104baf5664fSJonas Devlieghere
10597206d57SZachary Turner Status error = m_impl_up->GetDescription(stream.ref());
1062ef442c6STodd Fiala SBError sb_error;
1072ef442c6STodd Fiala sb_error.SetError(error);
108d232abc3SJonas Devlieghere return sb_error;
10975930019STodd Fiala }
1105bfee5f1SAbhishek Aggarwal
GetType() const1115bfee5f1SAbhishek Aggarwal StructuredDataType SBStructuredData::GetType() const {
112*1755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
113baf5664fSJonas Devlieghere
114c5cf4b8fSDave Lee return m_impl_up->GetType();
1155bfee5f1SAbhishek Aggarwal }
1165bfee5f1SAbhishek Aggarwal
GetSize() const1175bfee5f1SAbhishek Aggarwal size_t SBStructuredData::GetSize() const {
118*1755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this);
119baf5664fSJonas Devlieghere
120c5cf4b8fSDave Lee return m_impl_up->GetSize();
1215bfee5f1SAbhishek Aggarwal }
1225bfee5f1SAbhishek Aggarwal
GetKeys(lldb::SBStringList & keys) const1233815e702SJim Ingham bool SBStructuredData::GetKeys(lldb::SBStringList &keys) const {
124*1755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, keys);
125baf5664fSJonas Devlieghere
1263815e702SJim Ingham if (GetType() != eStructuredDataTypeDictionary)
1273815e702SJim Ingham return false;
1283815e702SJim Ingham
1293815e702SJim Ingham StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP();
1303815e702SJim Ingham if (!obj_sp)
1313815e702SJim Ingham return false;
1323815e702SJim Ingham
1333815e702SJim Ingham StructuredData::Dictionary *dict = obj_sp->GetAsDictionary();
1343815e702SJim Ingham // We claimed we were a dictionary, so this can't be null.
1353815e702SJim Ingham assert(dict);
1363815e702SJim Ingham // The return kind of GetKeys is an Array:
1373815e702SJim Ingham StructuredData::ObjectSP array_sp = dict->GetKeys();
1383815e702SJim Ingham StructuredData::Array *key_arr = array_sp->GetAsArray();
1393815e702SJim Ingham assert(key_arr);
1403815e702SJim Ingham
1413815e702SJim Ingham key_arr->ForEach([&keys] (StructuredData::Object *object) -> bool {
1423815e702SJim Ingham llvm::StringRef key = object->GetStringValue("");
1433815e702SJim Ingham keys.AppendString(key.str().c_str());
1443815e702SJim Ingham return true;
1453815e702SJim Ingham });
1463815e702SJim Ingham return true;
1473815e702SJim Ingham }
1483815e702SJim Ingham
GetValueForKey(const char * key) const1495bfee5f1SAbhishek Aggarwal lldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const {
150*1755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, key);
151baf5664fSJonas Devlieghere
1525bfee5f1SAbhishek Aggarwal SBStructuredData result;
1535bfee5f1SAbhishek Aggarwal result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key));
154d232abc3SJonas Devlieghere return result;
1555bfee5f1SAbhishek Aggarwal }
1565bfee5f1SAbhishek Aggarwal
GetItemAtIndex(size_t idx) const1575bfee5f1SAbhishek Aggarwal lldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const {
158*1755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, idx);
159baf5664fSJonas Devlieghere
1605bfee5f1SAbhishek Aggarwal SBStructuredData result;
1615bfee5f1SAbhishek Aggarwal result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx));
162d232abc3SJonas Devlieghere return result;
1635bfee5f1SAbhishek Aggarwal }
1645bfee5f1SAbhishek Aggarwal
GetIntegerValue(uint64_t fail_value) const1655bfee5f1SAbhishek Aggarwal uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const {
166*1755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, fail_value);
167baf5664fSJonas Devlieghere
168c5cf4b8fSDave Lee return m_impl_up->GetIntegerValue(fail_value);
1695bfee5f1SAbhishek Aggarwal }
1705bfee5f1SAbhishek Aggarwal
GetFloatValue(double fail_value) const1715bfee5f1SAbhishek Aggarwal double SBStructuredData::GetFloatValue(double fail_value) const {
172*1755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, fail_value);
173baf5664fSJonas Devlieghere
174c5cf4b8fSDave Lee return m_impl_up->GetFloatValue(fail_value);
1755bfee5f1SAbhishek Aggarwal }
1765bfee5f1SAbhishek Aggarwal
GetBooleanValue(bool fail_value) const1775bfee5f1SAbhishek Aggarwal bool SBStructuredData::GetBooleanValue(bool fail_value) const {
178*1755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, fail_value);
179baf5664fSJonas Devlieghere
180c5cf4b8fSDave Lee return m_impl_up->GetBooleanValue(fail_value);
1815bfee5f1SAbhishek Aggarwal }
1825bfee5f1SAbhishek Aggarwal
GetStringValue(char * dst,size_t dst_len) const1835bfee5f1SAbhishek Aggarwal size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const {
184*1755f5b1SJonas Devlieghere LLDB_INSTRUMENT_VA(this, dst, dst_len);
185baf5664fSJonas Devlieghere
186c5cf4b8fSDave Lee return m_impl_up->GetStringValue(dst, dst_len);
1875bfee5f1SAbhishek Aggarwal }
188