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
11*06c3fb27SDimitry Andric #include "lldb/API/SBDebugger.h"
12*06c3fb27SDimitry Andric #include "lldb/API/SBScriptObject.h"
130b57cec5SDimitry Andric #include "lldb/API/SBStream.h"
140b57cec5SDimitry Andric #include "lldb/API/SBStringList.h"
15*06c3fb27SDimitry Andric #include "lldb/Core/Debugger.h"
160b57cec5SDimitry Andric #include "lldb/Core/StructuredDataImpl.h"
17*06c3fb27SDimitry Andric #include "lldb/Interpreter/ScriptInterpreter.h"
180b57cec5SDimitry Andric #include "lldb/Target/StructuredDataPlugin.h"
190b57cec5SDimitry Andric #include "lldb/Utility/Event.h"
20*06c3fb27SDimitry Andric #include "lldb/Utility/Instrumentation.h"
210b57cec5SDimitry Andric #include "lldb/Utility/Status.h"
220b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
23*06c3fb27SDimitry Andric #include "lldb/Utility/StringList.h"
240b57cec5SDimitry Andric #include "lldb/Utility/StructuredData.h"
250b57cec5SDimitry Andric
260b57cec5SDimitry Andric using namespace lldb;
270b57cec5SDimitry Andric using namespace lldb_private;
280b57cec5SDimitry Andric
290b57cec5SDimitry Andric #pragma mark--
300b57cec5SDimitry Andric #pragma mark SBStructuredData
310b57cec5SDimitry Andric
SBStructuredData()320b57cec5SDimitry Andric SBStructuredData::SBStructuredData() : m_impl_up(new StructuredDataImpl()) {
3304eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this);
340b57cec5SDimitry Andric }
350b57cec5SDimitry Andric
SBStructuredData(const lldb::SBStructuredData & rhs)360b57cec5SDimitry Andric SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs)
37fe6060f1SDimitry Andric : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up)) {
3804eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
390b57cec5SDimitry Andric }
400b57cec5SDimitry Andric
SBStructuredData(const lldb::SBScriptObject obj,const lldb::SBDebugger & debugger)41*06c3fb27SDimitry Andric SBStructuredData::SBStructuredData(const lldb::SBScriptObject obj,
42*06c3fb27SDimitry Andric const lldb::SBDebugger &debugger) {
43*06c3fb27SDimitry Andric LLDB_INSTRUMENT_VA(this, obj, debugger);
44*06c3fb27SDimitry Andric
45*06c3fb27SDimitry Andric if (!obj.IsValid())
46*06c3fb27SDimitry Andric return;
47*06c3fb27SDimitry Andric
48*06c3fb27SDimitry Andric ScriptInterpreter *interpreter =
49*06c3fb27SDimitry Andric debugger.m_opaque_sp->GetScriptInterpreter(true, obj.GetLanguage());
50*06c3fb27SDimitry Andric
51*06c3fb27SDimitry Andric if (!interpreter)
52*06c3fb27SDimitry Andric return;
53*06c3fb27SDimitry Andric
54*06c3fb27SDimitry Andric StructuredDataImplUP impl_up = std::make_unique<StructuredDataImpl>(
55*06c3fb27SDimitry Andric interpreter->CreateStructuredDataFromScriptObject(obj.ref()));
56*06c3fb27SDimitry Andric if (impl_up && impl_up->IsValid())
57*06c3fb27SDimitry Andric m_impl_up.reset(impl_up.release());
58*06c3fb27SDimitry Andric }
59*06c3fb27SDimitry Andric
SBStructuredData(const lldb::EventSP & event_sp)600b57cec5SDimitry Andric SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp)
610b57cec5SDimitry Andric : m_impl_up(new StructuredDataImpl(event_sp)) {
6204eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, event_sp);
630b57cec5SDimitry Andric }
640b57cec5SDimitry Andric
SBStructuredData(const lldb_private::StructuredDataImpl & impl)650eae32dcSDimitry Andric SBStructuredData::SBStructuredData(const lldb_private::StructuredDataImpl &impl)
660eae32dcSDimitry Andric : m_impl_up(new StructuredDataImpl(impl)) {
6704eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, impl);
680b57cec5SDimitry Andric }
690b57cec5SDimitry Andric
705ffd83dbSDimitry Andric SBStructuredData::~SBStructuredData() = default;
710b57cec5SDimitry Andric
720b57cec5SDimitry Andric SBStructuredData &SBStructuredData::
operator =(const lldb::SBStructuredData & rhs)730b57cec5SDimitry Andric operator=(const lldb::SBStructuredData &rhs) {
7404eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
750b57cec5SDimitry Andric
760b57cec5SDimitry Andric *m_impl_up = *rhs.m_impl_up;
7704eeddc0SDimitry Andric return *this;
780b57cec5SDimitry Andric }
790b57cec5SDimitry Andric
SetFromJSON(lldb::SBStream & stream)800b57cec5SDimitry Andric lldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream &stream) {
8104eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, stream);
820b57cec5SDimitry Andric
830b57cec5SDimitry Andric lldb::SBError error;
840b57cec5SDimitry Andric
85*06c3fb27SDimitry Andric StructuredData::ObjectSP json_obj =
86*06c3fb27SDimitry Andric StructuredData::ParseJSON(stream.GetData());
870b57cec5SDimitry Andric m_impl_up->SetObjectSP(json_obj);
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary)
900b57cec5SDimitry Andric error.SetErrorString("Invalid Syntax");
9104eeddc0SDimitry Andric return error;
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric
SetFromJSON(const char * json)94fe6060f1SDimitry Andric lldb::SBError SBStructuredData::SetFromJSON(const char *json) {
9504eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, json);
96fe6060f1SDimitry Andric lldb::SBStream s;
97fe6060f1SDimitry Andric s.Print(json);
9804eeddc0SDimitry Andric return SetFromJSON(s);
99fe6060f1SDimitry Andric }
100fe6060f1SDimitry Andric
IsValid() const1010b57cec5SDimitry Andric bool SBStructuredData::IsValid() const {
10204eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this);
1030b57cec5SDimitry Andric return this->operator bool();
1040b57cec5SDimitry Andric }
105fe6060f1SDimitry Andric
operator bool() const1060b57cec5SDimitry Andric SBStructuredData::operator bool() const {
10704eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this);
1080b57cec5SDimitry Andric
1090b57cec5SDimitry Andric return m_impl_up->IsValid();
1100b57cec5SDimitry Andric }
1110b57cec5SDimitry Andric
Clear()1120b57cec5SDimitry Andric void SBStructuredData::Clear() {
11304eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this);
1140b57cec5SDimitry Andric
1150b57cec5SDimitry Andric m_impl_up->Clear();
1160b57cec5SDimitry Andric }
1170b57cec5SDimitry Andric
GetAsJSON(lldb::SBStream & stream) const1180b57cec5SDimitry Andric SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const {
11904eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, stream);
1200b57cec5SDimitry Andric
1210b57cec5SDimitry Andric SBError error;
1220b57cec5SDimitry Andric error.SetError(m_impl_up->GetAsJSON(stream.ref()));
12304eeddc0SDimitry Andric return error;
1240b57cec5SDimitry Andric }
1250b57cec5SDimitry Andric
GetDescription(lldb::SBStream & stream) const1260b57cec5SDimitry Andric lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const {
12704eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, stream);
1280b57cec5SDimitry Andric
1290b57cec5SDimitry Andric Status error = m_impl_up->GetDescription(stream.ref());
1300b57cec5SDimitry Andric SBError sb_error;
1310b57cec5SDimitry Andric sb_error.SetError(error);
13204eeddc0SDimitry Andric return sb_error;
1330b57cec5SDimitry Andric }
1340b57cec5SDimitry Andric
GetType() const1350b57cec5SDimitry Andric StructuredDataType SBStructuredData::GetType() const {
13604eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this);
1370b57cec5SDimitry Andric
138fe6060f1SDimitry Andric return m_impl_up->GetType();
1390b57cec5SDimitry Andric }
1400b57cec5SDimitry Andric
GetSize() const1410b57cec5SDimitry Andric size_t SBStructuredData::GetSize() const {
14204eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this);
1430b57cec5SDimitry Andric
144fe6060f1SDimitry Andric return m_impl_up->GetSize();
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric
GetKeys(lldb::SBStringList & keys) const1470b57cec5SDimitry Andric bool SBStructuredData::GetKeys(lldb::SBStringList &keys) const {
14804eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, keys);
1490b57cec5SDimitry Andric
1500b57cec5SDimitry Andric if (GetType() != eStructuredDataTypeDictionary)
1510b57cec5SDimitry Andric return false;
1520b57cec5SDimitry Andric
1530b57cec5SDimitry Andric StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP();
1540b57cec5SDimitry Andric if (!obj_sp)
1550b57cec5SDimitry Andric return false;
1560b57cec5SDimitry Andric
1570b57cec5SDimitry Andric StructuredData::Dictionary *dict = obj_sp->GetAsDictionary();
1580b57cec5SDimitry Andric // We claimed we were a dictionary, so this can't be null.
1590b57cec5SDimitry Andric assert(dict);
1600b57cec5SDimitry Andric // The return kind of GetKeys is an Array:
1610b57cec5SDimitry Andric StructuredData::ObjectSP array_sp = dict->GetKeys();
1620b57cec5SDimitry Andric StructuredData::Array *key_arr = array_sp->GetAsArray();
1630b57cec5SDimitry Andric assert(key_arr);
1640b57cec5SDimitry Andric
1650b57cec5SDimitry Andric key_arr->ForEach([&keys](StructuredData::Object *object) -> bool {
1660b57cec5SDimitry Andric llvm::StringRef key = object->GetStringValue("");
167*06c3fb27SDimitry Andric keys->AppendString(key);
1680b57cec5SDimitry Andric return true;
1690b57cec5SDimitry Andric });
1700b57cec5SDimitry Andric return true;
1710b57cec5SDimitry Andric }
1720b57cec5SDimitry Andric
GetValueForKey(const char * key) const1730b57cec5SDimitry Andric lldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const {
17404eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, key);
1750b57cec5SDimitry Andric
1760b57cec5SDimitry Andric SBStructuredData result;
1770b57cec5SDimitry Andric result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key));
17804eeddc0SDimitry Andric return result;
1790b57cec5SDimitry Andric }
1800b57cec5SDimitry Andric
GetItemAtIndex(size_t idx) const1810b57cec5SDimitry Andric lldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const {
18204eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, idx);
1830b57cec5SDimitry Andric
1840b57cec5SDimitry Andric SBStructuredData result;
1850b57cec5SDimitry Andric result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx));
18604eeddc0SDimitry Andric return result;
1870b57cec5SDimitry Andric }
1880b57cec5SDimitry Andric
GetIntegerValue(uint64_t fail_value) const1890b57cec5SDimitry Andric uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const {
19004eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, fail_value);
1910b57cec5SDimitry Andric
192*06c3fb27SDimitry Andric return GetUnsignedIntegerValue(fail_value);
193*06c3fb27SDimitry Andric }
194*06c3fb27SDimitry Andric
GetUnsignedIntegerValue(uint64_t fail_value) const195*06c3fb27SDimitry Andric uint64_t SBStructuredData::GetUnsignedIntegerValue(uint64_t fail_value) const {
196*06c3fb27SDimitry Andric LLDB_INSTRUMENT_VA(this, fail_value);
197*06c3fb27SDimitry Andric
198*06c3fb27SDimitry Andric return m_impl_up->GetIntegerValue(fail_value);
199*06c3fb27SDimitry Andric }
200*06c3fb27SDimitry Andric
GetSignedIntegerValue(int64_t fail_value) const201*06c3fb27SDimitry Andric int64_t SBStructuredData::GetSignedIntegerValue(int64_t fail_value) const {
202*06c3fb27SDimitry Andric LLDB_INSTRUMENT_VA(this, fail_value);
203*06c3fb27SDimitry Andric
204fe6060f1SDimitry Andric return m_impl_up->GetIntegerValue(fail_value);
2050b57cec5SDimitry Andric }
2060b57cec5SDimitry Andric
GetFloatValue(double fail_value) const2070b57cec5SDimitry Andric double SBStructuredData::GetFloatValue(double fail_value) const {
20804eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, fail_value);
2090b57cec5SDimitry Andric
210fe6060f1SDimitry Andric return m_impl_up->GetFloatValue(fail_value);
2110b57cec5SDimitry Andric }
2120b57cec5SDimitry Andric
GetBooleanValue(bool fail_value) const2130b57cec5SDimitry Andric bool SBStructuredData::GetBooleanValue(bool fail_value) const {
21404eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, fail_value);
2150b57cec5SDimitry Andric
216fe6060f1SDimitry Andric return m_impl_up->GetBooleanValue(fail_value);
2170b57cec5SDimitry Andric }
2180b57cec5SDimitry Andric
GetStringValue(char * dst,size_t dst_len) const2190b57cec5SDimitry Andric size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const {
22004eeddc0SDimitry Andric LLDB_INSTRUMENT_VA(this, dst, dst_len);
2210b57cec5SDimitry Andric
222fe6060f1SDimitry Andric return m_impl_up->GetStringValue(dst, dst_len);
2230b57cec5SDimitry Andric }
224*06c3fb27SDimitry Andric
GetGenericValue() const225*06c3fb27SDimitry Andric lldb::SBScriptObject SBStructuredData::GetGenericValue() const {
226*06c3fb27SDimitry Andric LLDB_INSTRUMENT_VA(this);
227*06c3fb27SDimitry Andric
228*06c3fb27SDimitry Andric return {m_impl_up->GetGenericValue(), eScriptLanguageDefault};
229*06c3fb27SDimitry Andric }
230