xref: /freebsd/contrib/llvm-project/lldb/source/API/SBStructuredData.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===-- SBStructuredData.cpp ------------------------------------*- C++ -*-===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "lldb/API/SBStructuredData.h"
10*0b57cec5SDimitry Andric #include "SBReproducerPrivate.h"
11*0b57cec5SDimitry Andric 
12*0b57cec5SDimitry Andric #include "lldb/API/SBStream.h"
13*0b57cec5SDimitry Andric #include "lldb/API/SBStringList.h"
14*0b57cec5SDimitry Andric #include "lldb/Core/StructuredDataImpl.h"
15*0b57cec5SDimitry Andric #include "lldb/Target/StructuredDataPlugin.h"
16*0b57cec5SDimitry Andric #include "lldb/Utility/Event.h"
17*0b57cec5SDimitry Andric #include "lldb/Utility/Status.h"
18*0b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
19*0b57cec5SDimitry Andric #include "lldb/Utility/StructuredData.h"
20*0b57cec5SDimitry Andric 
21*0b57cec5SDimitry Andric using namespace lldb;
22*0b57cec5SDimitry Andric using namespace lldb_private;
23*0b57cec5SDimitry Andric 
24*0b57cec5SDimitry Andric #pragma mark--
25*0b57cec5SDimitry Andric #pragma mark SBStructuredData
26*0b57cec5SDimitry Andric 
27*0b57cec5SDimitry Andric SBStructuredData::SBStructuredData() : m_impl_up(new StructuredDataImpl()) {
28*0b57cec5SDimitry Andric   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBStructuredData);
29*0b57cec5SDimitry Andric }
30*0b57cec5SDimitry Andric 
31*0b57cec5SDimitry Andric SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs)
32*0b57cec5SDimitry Andric     : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up.get())) {
33*0b57cec5SDimitry Andric   LLDB_RECORD_CONSTRUCTOR(SBStructuredData, (const lldb::SBStructuredData &),
34*0b57cec5SDimitry Andric                           rhs);
35*0b57cec5SDimitry Andric }
36*0b57cec5SDimitry Andric 
37*0b57cec5SDimitry Andric SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp)
38*0b57cec5SDimitry Andric     : m_impl_up(new StructuredDataImpl(event_sp)) {
39*0b57cec5SDimitry Andric   LLDB_RECORD_CONSTRUCTOR(SBStructuredData, (const lldb::EventSP &), event_sp);
40*0b57cec5SDimitry Andric }
41*0b57cec5SDimitry Andric 
42*0b57cec5SDimitry Andric SBStructuredData::SBStructuredData(lldb_private::StructuredDataImpl *impl)
43*0b57cec5SDimitry Andric     : m_impl_up(impl) {
44*0b57cec5SDimitry Andric   LLDB_RECORD_CONSTRUCTOR(SBStructuredData,
45*0b57cec5SDimitry Andric                           (lldb_private::StructuredDataImpl *), impl);
46*0b57cec5SDimitry Andric }
47*0b57cec5SDimitry Andric 
48*0b57cec5SDimitry Andric SBStructuredData::~SBStructuredData() {}
49*0b57cec5SDimitry Andric 
50*0b57cec5SDimitry Andric SBStructuredData &SBStructuredData::
51*0b57cec5SDimitry Andric operator=(const lldb::SBStructuredData &rhs) {
52*0b57cec5SDimitry Andric   LLDB_RECORD_METHOD(
53*0b57cec5SDimitry Andric       lldb::SBStructuredData &,
54*0b57cec5SDimitry Andric       SBStructuredData, operator=,(const lldb::SBStructuredData &), rhs);
55*0b57cec5SDimitry Andric 
56*0b57cec5SDimitry Andric   *m_impl_up = *rhs.m_impl_up;
57*0b57cec5SDimitry Andric   return LLDB_RECORD_RESULT(*this);
58*0b57cec5SDimitry Andric }
59*0b57cec5SDimitry Andric 
60*0b57cec5SDimitry Andric lldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream &stream) {
61*0b57cec5SDimitry Andric   LLDB_RECORD_METHOD(lldb::SBError, SBStructuredData, SetFromJSON,
62*0b57cec5SDimitry Andric                      (lldb::SBStream &), stream);
63*0b57cec5SDimitry Andric 
64*0b57cec5SDimitry Andric   lldb::SBError error;
65*0b57cec5SDimitry Andric   std::string json_str(stream.GetData());
66*0b57cec5SDimitry Andric 
67*0b57cec5SDimitry Andric   StructuredData::ObjectSP json_obj = StructuredData::ParseJSON(json_str);
68*0b57cec5SDimitry Andric   m_impl_up->SetObjectSP(json_obj);
69*0b57cec5SDimitry Andric 
70*0b57cec5SDimitry Andric   if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary)
71*0b57cec5SDimitry Andric     error.SetErrorString("Invalid Syntax");
72*0b57cec5SDimitry Andric   return LLDB_RECORD_RESULT(error);
73*0b57cec5SDimitry Andric }
74*0b57cec5SDimitry Andric 
75*0b57cec5SDimitry Andric bool SBStructuredData::IsValid() const {
76*0b57cec5SDimitry Andric   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStructuredData, IsValid);
77*0b57cec5SDimitry Andric   return this->operator bool();
78*0b57cec5SDimitry Andric }
79*0b57cec5SDimitry Andric SBStructuredData::operator bool() const {
80*0b57cec5SDimitry Andric   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStructuredData, operator bool);
81*0b57cec5SDimitry Andric 
82*0b57cec5SDimitry Andric   return m_impl_up->IsValid();
83*0b57cec5SDimitry Andric }
84*0b57cec5SDimitry Andric 
85*0b57cec5SDimitry Andric void SBStructuredData::Clear() {
86*0b57cec5SDimitry Andric   LLDB_RECORD_METHOD_NO_ARGS(void, SBStructuredData, Clear);
87*0b57cec5SDimitry Andric 
88*0b57cec5SDimitry Andric   m_impl_up->Clear();
89*0b57cec5SDimitry Andric }
90*0b57cec5SDimitry Andric 
91*0b57cec5SDimitry Andric SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const {
92*0b57cec5SDimitry Andric   LLDB_RECORD_METHOD_CONST(lldb::SBError, SBStructuredData, GetAsJSON,
93*0b57cec5SDimitry Andric                            (lldb::SBStream &), stream);
94*0b57cec5SDimitry Andric 
95*0b57cec5SDimitry Andric   SBError error;
96*0b57cec5SDimitry Andric   error.SetError(m_impl_up->GetAsJSON(stream.ref()));
97*0b57cec5SDimitry Andric   return LLDB_RECORD_RESULT(error);
98*0b57cec5SDimitry Andric }
99*0b57cec5SDimitry Andric 
100*0b57cec5SDimitry Andric lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const {
101*0b57cec5SDimitry Andric   LLDB_RECORD_METHOD_CONST(lldb::SBError, SBStructuredData, GetDescription,
102*0b57cec5SDimitry Andric                            (lldb::SBStream &), stream);
103*0b57cec5SDimitry Andric 
104*0b57cec5SDimitry Andric   Status error = m_impl_up->GetDescription(stream.ref());
105*0b57cec5SDimitry Andric   SBError sb_error;
106*0b57cec5SDimitry Andric   sb_error.SetError(error);
107*0b57cec5SDimitry Andric   return LLDB_RECORD_RESULT(sb_error);
108*0b57cec5SDimitry Andric }
109*0b57cec5SDimitry Andric 
110*0b57cec5SDimitry Andric StructuredDataType SBStructuredData::GetType() const {
111*0b57cec5SDimitry Andric   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::StructuredDataType, SBStructuredData,
112*0b57cec5SDimitry Andric                                    GetType);
113*0b57cec5SDimitry Andric 
114*0b57cec5SDimitry Andric   return (m_impl_up ? m_impl_up->GetType() : eStructuredDataTypeInvalid);
115*0b57cec5SDimitry Andric }
116*0b57cec5SDimitry Andric 
117*0b57cec5SDimitry Andric size_t SBStructuredData::GetSize() const {
118*0b57cec5SDimitry Andric   LLDB_RECORD_METHOD_CONST_NO_ARGS(size_t, SBStructuredData, GetSize);
119*0b57cec5SDimitry Andric 
120*0b57cec5SDimitry Andric   return (m_impl_up ? m_impl_up->GetSize() : 0);
121*0b57cec5SDimitry Andric }
122*0b57cec5SDimitry Andric 
123*0b57cec5SDimitry Andric bool SBStructuredData::GetKeys(lldb::SBStringList &keys) const {
124*0b57cec5SDimitry Andric   LLDB_RECORD_METHOD_CONST(bool, SBStructuredData, GetKeys,
125*0b57cec5SDimitry Andric                            (lldb::SBStringList &), keys);
126*0b57cec5SDimitry Andric 
127*0b57cec5SDimitry Andric   if (!m_impl_up)
128*0b57cec5SDimitry Andric     return false;
129*0b57cec5SDimitry Andric 
130*0b57cec5SDimitry Andric   if (GetType() != eStructuredDataTypeDictionary)
131*0b57cec5SDimitry Andric     return false;
132*0b57cec5SDimitry Andric 
133*0b57cec5SDimitry Andric   StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP();
134*0b57cec5SDimitry Andric   if (!obj_sp)
135*0b57cec5SDimitry Andric     return false;
136*0b57cec5SDimitry Andric 
137*0b57cec5SDimitry Andric   StructuredData::Dictionary *dict = obj_sp->GetAsDictionary();
138*0b57cec5SDimitry Andric   // We claimed we were a dictionary, so this can't be null.
139*0b57cec5SDimitry Andric   assert(dict);
140*0b57cec5SDimitry Andric   // The return kind of GetKeys is an Array:
141*0b57cec5SDimitry Andric   StructuredData::ObjectSP array_sp = dict->GetKeys();
142*0b57cec5SDimitry Andric   StructuredData::Array *key_arr = array_sp->GetAsArray();
143*0b57cec5SDimitry Andric   assert(key_arr);
144*0b57cec5SDimitry Andric 
145*0b57cec5SDimitry Andric   key_arr->ForEach([&keys] (StructuredData::Object *object) -> bool {
146*0b57cec5SDimitry Andric     llvm::StringRef key = object->GetStringValue("");
147*0b57cec5SDimitry Andric     keys.AppendString(key.str().c_str());
148*0b57cec5SDimitry Andric     return true;
149*0b57cec5SDimitry Andric   });
150*0b57cec5SDimitry Andric   return true;
151*0b57cec5SDimitry Andric }
152*0b57cec5SDimitry Andric 
153*0b57cec5SDimitry Andric lldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const {
154*0b57cec5SDimitry Andric   LLDB_RECORD_METHOD_CONST(lldb::SBStructuredData, SBStructuredData,
155*0b57cec5SDimitry Andric                            GetValueForKey, (const char *), key);
156*0b57cec5SDimitry Andric 
157*0b57cec5SDimitry Andric   if (!m_impl_up)
158*0b57cec5SDimitry Andric     return LLDB_RECORD_RESULT(SBStructuredData());
159*0b57cec5SDimitry Andric 
160*0b57cec5SDimitry Andric   SBStructuredData result;
161*0b57cec5SDimitry Andric   result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key));
162*0b57cec5SDimitry Andric   return LLDB_RECORD_RESULT(result);
163*0b57cec5SDimitry Andric }
164*0b57cec5SDimitry Andric 
165*0b57cec5SDimitry Andric lldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const {
166*0b57cec5SDimitry Andric   LLDB_RECORD_METHOD_CONST(lldb::SBStructuredData, SBStructuredData,
167*0b57cec5SDimitry Andric                            GetItemAtIndex, (size_t), idx);
168*0b57cec5SDimitry Andric 
169*0b57cec5SDimitry Andric   if (!m_impl_up)
170*0b57cec5SDimitry Andric     return LLDB_RECORD_RESULT(SBStructuredData());
171*0b57cec5SDimitry Andric 
172*0b57cec5SDimitry Andric   SBStructuredData result;
173*0b57cec5SDimitry Andric   result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx));
174*0b57cec5SDimitry Andric   return LLDB_RECORD_RESULT(result);
175*0b57cec5SDimitry Andric }
176*0b57cec5SDimitry Andric 
177*0b57cec5SDimitry Andric uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const {
178*0b57cec5SDimitry Andric   LLDB_RECORD_METHOD_CONST(uint64_t, SBStructuredData, GetIntegerValue,
179*0b57cec5SDimitry Andric                            (uint64_t), fail_value);
180*0b57cec5SDimitry Andric 
181*0b57cec5SDimitry Andric   return (m_impl_up ? m_impl_up->GetIntegerValue(fail_value) : fail_value);
182*0b57cec5SDimitry Andric }
183*0b57cec5SDimitry Andric 
184*0b57cec5SDimitry Andric double SBStructuredData::GetFloatValue(double fail_value) const {
185*0b57cec5SDimitry Andric   LLDB_RECORD_METHOD_CONST(double, SBStructuredData, GetFloatValue, (double),
186*0b57cec5SDimitry Andric                            fail_value);
187*0b57cec5SDimitry Andric 
188*0b57cec5SDimitry Andric   return (m_impl_up ? m_impl_up->GetFloatValue(fail_value) : fail_value);
189*0b57cec5SDimitry Andric }
190*0b57cec5SDimitry Andric 
191*0b57cec5SDimitry Andric bool SBStructuredData::GetBooleanValue(bool fail_value) const {
192*0b57cec5SDimitry Andric   LLDB_RECORD_METHOD_CONST(bool, SBStructuredData, GetBooleanValue, (bool),
193*0b57cec5SDimitry Andric                            fail_value);
194*0b57cec5SDimitry Andric 
195*0b57cec5SDimitry Andric   return (m_impl_up ? m_impl_up->GetBooleanValue(fail_value) : fail_value);
196*0b57cec5SDimitry Andric }
197*0b57cec5SDimitry Andric 
198*0b57cec5SDimitry Andric size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const {
199*0b57cec5SDimitry Andric   LLDB_RECORD_METHOD_CONST(size_t, SBStructuredData, GetStringValue,
200*0b57cec5SDimitry Andric                            (char *, size_t), dst, dst_len);
201*0b57cec5SDimitry Andric 
202*0b57cec5SDimitry Andric   return (m_impl_up ? m_impl_up->GetStringValue(dst, dst_len) : 0);
203*0b57cec5SDimitry Andric }
204*0b57cec5SDimitry Andric 
205*0b57cec5SDimitry Andric namespace lldb_private {
206*0b57cec5SDimitry Andric namespace repro {
207*0b57cec5SDimitry Andric 
208*0b57cec5SDimitry Andric template <>
209*0b57cec5SDimitry Andric void RegisterMethods<SBStructuredData>(Registry &R) {
210*0b57cec5SDimitry Andric   LLDB_REGISTER_CONSTRUCTOR(SBStructuredData, ());
211*0b57cec5SDimitry Andric   LLDB_REGISTER_CONSTRUCTOR(SBStructuredData,
212*0b57cec5SDimitry Andric                             (const lldb::SBStructuredData &));
213*0b57cec5SDimitry Andric   LLDB_REGISTER_CONSTRUCTOR(SBStructuredData, (const lldb::EventSP &));
214*0b57cec5SDimitry Andric   LLDB_REGISTER_CONSTRUCTOR(SBStructuredData,
215*0b57cec5SDimitry Andric                             (lldb_private::StructuredDataImpl *));
216*0b57cec5SDimitry Andric   LLDB_REGISTER_METHOD(
217*0b57cec5SDimitry Andric       lldb::SBStructuredData &,
218*0b57cec5SDimitry Andric       SBStructuredData, operator=,(const lldb::SBStructuredData &));
219*0b57cec5SDimitry Andric   LLDB_REGISTER_METHOD(lldb::SBError, SBStructuredData, SetFromJSON,
220*0b57cec5SDimitry Andric                        (lldb::SBStream &));
221*0b57cec5SDimitry Andric   LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, IsValid, ());
222*0b57cec5SDimitry Andric   LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, operator bool, ());
223*0b57cec5SDimitry Andric   LLDB_REGISTER_METHOD(void, SBStructuredData, Clear, ());
224*0b57cec5SDimitry Andric   LLDB_REGISTER_METHOD_CONST(lldb::SBError, SBStructuredData, GetAsJSON,
225*0b57cec5SDimitry Andric                              (lldb::SBStream &));
226*0b57cec5SDimitry Andric   LLDB_REGISTER_METHOD_CONST(lldb::SBError, SBStructuredData, GetDescription,
227*0b57cec5SDimitry Andric                              (lldb::SBStream &));
228*0b57cec5SDimitry Andric   LLDB_REGISTER_METHOD_CONST(lldb::StructuredDataType, SBStructuredData,
229*0b57cec5SDimitry Andric                              GetType, ());
230*0b57cec5SDimitry Andric   LLDB_REGISTER_METHOD_CONST(size_t, SBStructuredData, GetSize, ());
231*0b57cec5SDimitry Andric   LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, GetKeys,
232*0b57cec5SDimitry Andric                              (lldb::SBStringList &));
233*0b57cec5SDimitry Andric   LLDB_REGISTER_METHOD_CONST(lldb::SBStructuredData, SBStructuredData,
234*0b57cec5SDimitry Andric                              GetValueForKey, (const char *));
235*0b57cec5SDimitry Andric   LLDB_REGISTER_METHOD_CONST(lldb::SBStructuredData, SBStructuredData,
236*0b57cec5SDimitry Andric                              GetItemAtIndex, (size_t));
237*0b57cec5SDimitry Andric   LLDB_REGISTER_METHOD_CONST(uint64_t, SBStructuredData, GetIntegerValue,
238*0b57cec5SDimitry Andric                              (uint64_t));
239*0b57cec5SDimitry Andric   LLDB_REGISTER_METHOD_CONST(double, SBStructuredData, GetFloatValue,
240*0b57cec5SDimitry Andric                              (double));
241*0b57cec5SDimitry Andric   LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, GetBooleanValue, (bool));
242*0b57cec5SDimitry Andric   LLDB_REGISTER_METHOD_CONST(size_t, SBStructuredData, GetStringValue,
243*0b57cec5SDimitry Andric                              (char *, size_t));
244*0b57cec5SDimitry Andric }
245*0b57cec5SDimitry Andric 
246*0b57cec5SDimitry Andric }
247*0b57cec5SDimitry Andric }
248