xref: /freebsd/contrib/llvm-project/lldb/source/API/SBStructuredData.cpp (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
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"
10*04eeddc0SDimitry Andric #include "lldb/Utility/Instrumentation.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()) {
28*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
290b57cec5SDimitry Andric }
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs)
32fe6060f1SDimitry Andric     : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up)) {
33*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
340b57cec5SDimitry Andric }
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp)
370b57cec5SDimitry Andric     : m_impl_up(new StructuredDataImpl(event_sp)) {
38*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, event_sp);
390b57cec5SDimitry Andric }
400b57cec5SDimitry Andric 
410eae32dcSDimitry Andric SBStructuredData::SBStructuredData(const lldb_private::StructuredDataImpl &impl)
420eae32dcSDimitry Andric     : m_impl_up(new StructuredDataImpl(impl)) {
43*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, impl);
440b57cec5SDimitry Andric }
450b57cec5SDimitry Andric 
465ffd83dbSDimitry Andric SBStructuredData::~SBStructuredData() = default;
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric SBStructuredData &SBStructuredData::
490b57cec5SDimitry Andric operator=(const lldb::SBStructuredData &rhs) {
50*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   *m_impl_up = *rhs.m_impl_up;
53*04eeddc0SDimitry Andric   return *this;
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric lldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream &stream) {
57*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, stream);
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   lldb::SBError error;
600b57cec5SDimitry Andric   std::string json_str(stream.GetData());
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric   StructuredData::ObjectSP json_obj = StructuredData::ParseJSON(json_str);
630b57cec5SDimitry Andric   m_impl_up->SetObjectSP(json_obj);
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary)
660b57cec5SDimitry Andric     error.SetErrorString("Invalid Syntax");
67*04eeddc0SDimitry Andric   return error;
680b57cec5SDimitry Andric }
690b57cec5SDimitry Andric 
70fe6060f1SDimitry Andric lldb::SBError SBStructuredData::SetFromJSON(const char *json) {
71*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, json);
72fe6060f1SDimitry Andric   lldb::SBStream s;
73fe6060f1SDimitry Andric   s.Print(json);
74*04eeddc0SDimitry Andric   return SetFromJSON(s);
75fe6060f1SDimitry Andric }
76fe6060f1SDimitry Andric 
770b57cec5SDimitry Andric bool SBStructuredData::IsValid() const {
78*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
790b57cec5SDimitry Andric   return this->operator bool();
800b57cec5SDimitry Andric }
81fe6060f1SDimitry Andric 
820b57cec5SDimitry Andric SBStructuredData::operator bool() const {
83*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric   return m_impl_up->IsValid();
860b57cec5SDimitry Andric }
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric void SBStructuredData::Clear() {
89*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric   m_impl_up->Clear();
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const {
95*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, stream);
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric   SBError error;
980b57cec5SDimitry Andric   error.SetError(m_impl_up->GetAsJSON(stream.ref()));
99*04eeddc0SDimitry Andric   return error;
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const {
103*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, stream);
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric   Status error = m_impl_up->GetDescription(stream.ref());
1060b57cec5SDimitry Andric   SBError sb_error;
1070b57cec5SDimitry Andric   sb_error.SetError(error);
108*04eeddc0SDimitry Andric   return sb_error;
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric StructuredDataType SBStructuredData::GetType() const {
112*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1130b57cec5SDimitry Andric 
114fe6060f1SDimitry Andric   return m_impl_up->GetType();
1150b57cec5SDimitry Andric }
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric size_t SBStructuredData::GetSize() const {
118*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1190b57cec5SDimitry Andric 
120fe6060f1SDimitry Andric   return m_impl_up->GetSize();
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric bool SBStructuredData::GetKeys(lldb::SBStringList &keys) const {
124*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, keys);
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric   if (GetType() != eStructuredDataTypeDictionary)
1270b57cec5SDimitry Andric     return false;
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric   StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP();
1300b57cec5SDimitry Andric   if (!obj_sp)
1310b57cec5SDimitry Andric     return false;
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric   StructuredData::Dictionary *dict = obj_sp->GetAsDictionary();
1340b57cec5SDimitry Andric   // We claimed we were a dictionary, so this can't be null.
1350b57cec5SDimitry Andric   assert(dict);
1360b57cec5SDimitry Andric   // The return kind of GetKeys is an Array:
1370b57cec5SDimitry Andric   StructuredData::ObjectSP array_sp = dict->GetKeys();
1380b57cec5SDimitry Andric   StructuredData::Array *key_arr = array_sp->GetAsArray();
1390b57cec5SDimitry Andric   assert(key_arr);
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   key_arr->ForEach([&keys] (StructuredData::Object *object) -> bool {
1420b57cec5SDimitry Andric     llvm::StringRef key = object->GetStringValue("");
1430b57cec5SDimitry Andric     keys.AppendString(key.str().c_str());
1440b57cec5SDimitry Andric     return true;
1450b57cec5SDimitry Andric   });
1460b57cec5SDimitry Andric   return true;
1470b57cec5SDimitry Andric }
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric lldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const {
150*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, key);
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric   SBStructuredData result;
1530b57cec5SDimitry Andric   result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key));
154*04eeddc0SDimitry Andric   return result;
1550b57cec5SDimitry Andric }
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric lldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const {
158*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, idx);
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric   SBStructuredData result;
1610b57cec5SDimitry Andric   result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx));
162*04eeddc0SDimitry Andric   return result;
1630b57cec5SDimitry Andric }
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const {
166*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, fail_value);
1670b57cec5SDimitry Andric 
168fe6060f1SDimitry Andric   return m_impl_up->GetIntegerValue(fail_value);
1690b57cec5SDimitry Andric }
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric double SBStructuredData::GetFloatValue(double fail_value) const {
172*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, fail_value);
1730b57cec5SDimitry Andric 
174fe6060f1SDimitry Andric   return m_impl_up->GetFloatValue(fail_value);
1750b57cec5SDimitry Andric }
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric bool SBStructuredData::GetBooleanValue(bool fail_value) const {
178*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, fail_value);
1790b57cec5SDimitry Andric 
180fe6060f1SDimitry Andric   return m_impl_up->GetBooleanValue(fail_value);
1810b57cec5SDimitry Andric }
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const {
184*04eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, dst, dst_len);
1850b57cec5SDimitry Andric 
186fe6060f1SDimitry Andric   return m_impl_up->GetStringValue(dst, dst_len);
1870b57cec5SDimitry Andric }
188