xref: /src/contrib/llvm-project/lldb/source/API/SBStructuredData.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1cfca06d7SDimitry Andric //===-- SBStructuredData.cpp ----------------------------------------------===//
214f1b3e8SDimitry Andric //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
614f1b3e8SDimitry Andric //
714f1b3e8SDimitry Andric //===----------------------------------------------------------------------===//
814f1b3e8SDimitry Andric 
914f1b3e8SDimitry Andric #include "lldb/API/SBStructuredData.h"
1014f1b3e8SDimitry Andric 
117fa27ce4SDimitry Andric #include "lldb/API/SBDebugger.h"
127fa27ce4SDimitry Andric #include "lldb/API/SBScriptObject.h"
1314f1b3e8SDimitry Andric #include "lldb/API/SBStream.h"
1494994d37SDimitry Andric #include "lldb/API/SBStringList.h"
157fa27ce4SDimitry Andric #include "lldb/Core/Debugger.h"
165060b64bSDimitry Andric #include "lldb/Core/StructuredDataImpl.h"
177fa27ce4SDimitry Andric #include "lldb/Interpreter/ScriptInterpreter.h"
1814f1b3e8SDimitry Andric #include "lldb/Target/StructuredDataPlugin.h"
1994994d37SDimitry Andric #include "lldb/Utility/Event.h"
207fa27ce4SDimitry Andric #include "lldb/Utility/Instrumentation.h"
21b76161e4SDimitry Andric #include "lldb/Utility/Status.h"
2274a628f7SDimitry Andric #include "lldb/Utility/Stream.h"
237fa27ce4SDimitry Andric #include "lldb/Utility/StringList.h"
241b306c26SDimitry Andric #include "lldb/Utility/StructuredData.h"
2514f1b3e8SDimitry Andric 
2614f1b3e8SDimitry Andric using namespace lldb;
2714f1b3e8SDimitry Andric using namespace lldb_private;
2814f1b3e8SDimitry Andric 
2914f1b3e8SDimitry Andric #pragma mark--
3014f1b3e8SDimitry Andric #pragma mark SBStructuredData
3114f1b3e8SDimitry Andric 
SBStructuredData()325f29bb8aSDimitry Andric SBStructuredData::SBStructuredData() : m_impl_up(new StructuredDataImpl()) {
336f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
345f29bb8aSDimitry Andric }
3514f1b3e8SDimitry Andric 
SBStructuredData(const lldb::SBStructuredData & rhs)3614f1b3e8SDimitry Andric SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs)
37344a3780SDimitry Andric     : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up)) {
386f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
395f29bb8aSDimitry Andric }
4014f1b3e8SDimitry Andric 
SBStructuredData(const lldb::SBScriptObject obj,const lldb::SBDebugger & debugger)417fa27ce4SDimitry Andric SBStructuredData::SBStructuredData(const lldb::SBScriptObject obj,
427fa27ce4SDimitry Andric                                    const lldb::SBDebugger &debugger) {
437fa27ce4SDimitry Andric   LLDB_INSTRUMENT_VA(this, obj, debugger);
447fa27ce4SDimitry Andric 
457fa27ce4SDimitry Andric   if (!obj.IsValid())
467fa27ce4SDimitry Andric     return;
477fa27ce4SDimitry Andric 
487fa27ce4SDimitry Andric   ScriptInterpreter *interpreter =
497fa27ce4SDimitry Andric       debugger.m_opaque_sp->GetScriptInterpreter(true, obj.GetLanguage());
507fa27ce4SDimitry Andric 
517fa27ce4SDimitry Andric   if (!interpreter)
527fa27ce4SDimitry Andric     return;
537fa27ce4SDimitry Andric 
547fa27ce4SDimitry Andric   StructuredDataImplUP impl_up = std::make_unique<StructuredDataImpl>(
557fa27ce4SDimitry Andric       interpreter->CreateStructuredDataFromScriptObject(obj.ref()));
567fa27ce4SDimitry Andric   if (impl_up && impl_up->IsValid())
577fa27ce4SDimitry Andric     m_impl_up.reset(impl_up.release());
587fa27ce4SDimitry Andric }
597fa27ce4SDimitry Andric 
SBStructuredData(const lldb::EventSP & event_sp)6014f1b3e8SDimitry Andric SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp)
615f29bb8aSDimitry Andric     : m_impl_up(new StructuredDataImpl(event_sp)) {
626f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, event_sp);
635f29bb8aSDimitry Andric }
6414f1b3e8SDimitry Andric 
SBStructuredData(const lldb_private::StructuredDataImpl & impl)6577fc4c14SDimitry Andric SBStructuredData::SBStructuredData(const lldb_private::StructuredDataImpl &impl)
6677fc4c14SDimitry Andric     : m_impl_up(new StructuredDataImpl(impl)) {
676f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, impl);
685f29bb8aSDimitry Andric }
6994994d37SDimitry Andric 
70cfca06d7SDimitry Andric SBStructuredData::~SBStructuredData() = default;
7114f1b3e8SDimitry Andric 
7214f1b3e8SDimitry Andric SBStructuredData &SBStructuredData::
operator =(const lldb::SBStructuredData & rhs)7314f1b3e8SDimitry Andric operator=(const lldb::SBStructuredData &rhs) {
746f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
755f29bb8aSDimitry Andric 
7614f1b3e8SDimitry Andric   *m_impl_up = *rhs.m_impl_up;
776f8fc217SDimitry Andric   return *this;
7814f1b3e8SDimitry Andric }
7914f1b3e8SDimitry Andric 
SetFromJSON(lldb::SBStream & stream)805060b64bSDimitry Andric lldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream &stream) {
816f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, stream);
825f29bb8aSDimitry Andric 
835060b64bSDimitry Andric   lldb::SBError error;
845060b64bSDimitry Andric 
857fa27ce4SDimitry Andric   StructuredData::ObjectSP json_obj =
867fa27ce4SDimitry Andric       StructuredData::ParseJSON(stream.GetData());
875060b64bSDimitry Andric   m_impl_up->SetObjectSP(json_obj);
885060b64bSDimitry Andric 
8961b440f5SDimitry Andric   if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary)
905060b64bSDimitry Andric     error.SetErrorString("Invalid Syntax");
916f8fc217SDimitry Andric   return error;
925060b64bSDimitry Andric }
935060b64bSDimitry Andric 
SetFromJSON(const char * json)94344a3780SDimitry Andric lldb::SBError SBStructuredData::SetFromJSON(const char *json) {
956f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, json);
96344a3780SDimitry Andric   lldb::SBStream s;
97344a3780SDimitry Andric   s.Print(json);
986f8fc217SDimitry Andric   return SetFromJSON(s);
99344a3780SDimitry Andric }
100344a3780SDimitry Andric 
IsValid() const1015f29bb8aSDimitry Andric bool SBStructuredData::IsValid() const {
1026f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1035f29bb8aSDimitry Andric   return this->operator bool();
1045f29bb8aSDimitry Andric }
105344a3780SDimitry Andric 
operator bool() const1065f29bb8aSDimitry Andric SBStructuredData::operator bool() const {
1076f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
10814f1b3e8SDimitry Andric 
1095f29bb8aSDimitry Andric   return m_impl_up->IsValid();
1105f29bb8aSDimitry Andric }
1115f29bb8aSDimitry Andric 
Clear()1125f29bb8aSDimitry Andric void SBStructuredData::Clear() {
1136f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1145f29bb8aSDimitry Andric 
1155f29bb8aSDimitry Andric   m_impl_up->Clear();
1165f29bb8aSDimitry Andric }
11714f1b3e8SDimitry Andric 
GetAsJSON(lldb::SBStream & stream) const11814f1b3e8SDimitry Andric SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const {
1196f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, stream);
1205f29bb8aSDimitry Andric 
1215060b64bSDimitry Andric   SBError error;
1225060b64bSDimitry Andric   error.SetError(m_impl_up->GetAsJSON(stream.ref()));
1236f8fc217SDimitry Andric   return error;
12414f1b3e8SDimitry Andric }
12514f1b3e8SDimitry Andric 
GetDescription(lldb::SBStream & stream) const12614f1b3e8SDimitry Andric lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const {
1276f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, stream);
1285f29bb8aSDimitry Andric 
129b76161e4SDimitry Andric   Status error = m_impl_up->GetDescription(stream.ref());
13014f1b3e8SDimitry Andric   SBError sb_error;
13114f1b3e8SDimitry Andric   sb_error.SetError(error);
1326f8fc217SDimitry Andric   return sb_error;
13314f1b3e8SDimitry Andric }
13461b440f5SDimitry Andric 
GetType() const13561b440f5SDimitry Andric StructuredDataType SBStructuredData::GetType() const {
1366f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1375f29bb8aSDimitry Andric 
138344a3780SDimitry Andric   return m_impl_up->GetType();
13961b440f5SDimitry Andric }
14061b440f5SDimitry Andric 
GetSize() const14161b440f5SDimitry Andric size_t SBStructuredData::GetSize() const {
1426f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1435f29bb8aSDimitry Andric 
144344a3780SDimitry Andric   return m_impl_up->GetSize();
14561b440f5SDimitry Andric }
14661b440f5SDimitry Andric 
GetKeys(lldb::SBStringList & keys) const14794994d37SDimitry Andric bool SBStructuredData::GetKeys(lldb::SBStringList &keys) const {
1486f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, keys);
1495f29bb8aSDimitry Andric 
15094994d37SDimitry Andric   if (GetType() != eStructuredDataTypeDictionary)
15194994d37SDimitry Andric     return false;
15294994d37SDimitry Andric 
15394994d37SDimitry Andric   StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP();
15494994d37SDimitry Andric   if (!obj_sp)
15594994d37SDimitry Andric     return false;
15694994d37SDimitry Andric 
15794994d37SDimitry Andric   StructuredData::Dictionary *dict = obj_sp->GetAsDictionary();
15894994d37SDimitry Andric   // We claimed we were a dictionary, so this can't be null.
15994994d37SDimitry Andric   assert(dict);
16094994d37SDimitry Andric   // The return kind of GetKeys is an Array:
16194994d37SDimitry Andric   StructuredData::ObjectSP array_sp = dict->GetKeys();
16294994d37SDimitry Andric   StructuredData::Array *key_arr = array_sp->GetAsArray();
16394994d37SDimitry Andric   assert(key_arr);
16494994d37SDimitry Andric 
16594994d37SDimitry Andric   key_arr->ForEach([&keys](StructuredData::Object *object) -> bool {
16694994d37SDimitry Andric     llvm::StringRef key = object->GetStringValue("");
1677fa27ce4SDimitry Andric     keys->AppendString(key);
16894994d37SDimitry Andric     return true;
16994994d37SDimitry Andric   });
17094994d37SDimitry Andric   return true;
17194994d37SDimitry Andric }
17294994d37SDimitry Andric 
GetValueForKey(const char * key) const17361b440f5SDimitry Andric lldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const {
1746f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, key);
1755f29bb8aSDimitry Andric 
17661b440f5SDimitry Andric   SBStructuredData result;
17761b440f5SDimitry Andric   result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key));
1786f8fc217SDimitry Andric   return result;
17961b440f5SDimitry Andric }
18061b440f5SDimitry Andric 
GetItemAtIndex(size_t idx) const18161b440f5SDimitry Andric lldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const {
1826f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, idx);
1835f29bb8aSDimitry Andric 
18461b440f5SDimitry Andric   SBStructuredData result;
18561b440f5SDimitry Andric   result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx));
1866f8fc217SDimitry Andric   return result;
18761b440f5SDimitry Andric }
18861b440f5SDimitry Andric 
GetIntegerValue(uint64_t fail_value) const18961b440f5SDimitry Andric uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const {
1906f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, fail_value);
1915f29bb8aSDimitry Andric 
1927fa27ce4SDimitry Andric   return GetUnsignedIntegerValue(fail_value);
1937fa27ce4SDimitry Andric }
1947fa27ce4SDimitry Andric 
GetUnsignedIntegerValue(uint64_t fail_value) const1957fa27ce4SDimitry Andric uint64_t SBStructuredData::GetUnsignedIntegerValue(uint64_t fail_value) const {
1967fa27ce4SDimitry Andric   LLDB_INSTRUMENT_VA(this, fail_value);
1977fa27ce4SDimitry Andric 
1987fa27ce4SDimitry Andric   return m_impl_up->GetIntegerValue(fail_value);
1997fa27ce4SDimitry Andric }
2007fa27ce4SDimitry Andric 
GetSignedIntegerValue(int64_t fail_value) const2017fa27ce4SDimitry Andric int64_t SBStructuredData::GetSignedIntegerValue(int64_t fail_value) const {
2027fa27ce4SDimitry Andric   LLDB_INSTRUMENT_VA(this, fail_value);
2037fa27ce4SDimitry Andric 
204344a3780SDimitry Andric   return m_impl_up->GetIntegerValue(fail_value);
20561b440f5SDimitry Andric }
20661b440f5SDimitry Andric 
GetFloatValue(double fail_value) const20761b440f5SDimitry Andric double SBStructuredData::GetFloatValue(double fail_value) const {
2086f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, fail_value);
2095f29bb8aSDimitry Andric 
210344a3780SDimitry Andric   return m_impl_up->GetFloatValue(fail_value);
21161b440f5SDimitry Andric }
21261b440f5SDimitry Andric 
GetBooleanValue(bool fail_value) const21361b440f5SDimitry Andric bool SBStructuredData::GetBooleanValue(bool fail_value) const {
2146f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, fail_value);
2155f29bb8aSDimitry Andric 
216344a3780SDimitry Andric   return m_impl_up->GetBooleanValue(fail_value);
21761b440f5SDimitry Andric }
21861b440f5SDimitry Andric 
GetStringValue(char * dst,size_t dst_len) const21961b440f5SDimitry Andric size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const {
2206f8fc217SDimitry Andric   LLDB_INSTRUMENT_VA(this, dst, dst_len);
2215f29bb8aSDimitry Andric 
222344a3780SDimitry Andric   return m_impl_up->GetStringValue(dst, dst_len);
22361b440f5SDimitry Andric }
2247fa27ce4SDimitry Andric 
GetGenericValue() const2257fa27ce4SDimitry Andric lldb::SBScriptObject SBStructuredData::GetGenericValue() const {
2267fa27ce4SDimitry Andric   LLDB_INSTRUMENT_VA(this);
2277fa27ce4SDimitry Andric 
2287fa27ce4SDimitry Andric   return {m_impl_up->GetGenericValue(), eScriptLanguageDefault};
2297fa27ce4SDimitry Andric }
230