1 //===-- SBStructuredData.cpp ----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "lldb/API/SBStructuredData.h"
10
11 #include "lldb/API/SBDebugger.h"
12 #include "lldb/API/SBScriptObject.h"
13 #include "lldb/API/SBStream.h"
14 #include "lldb/API/SBStringList.h"
15 #include "lldb/Core/Debugger.h"
16 #include "lldb/Core/StructuredDataImpl.h"
17 #include "lldb/Interpreter/ScriptInterpreter.h"
18 #include "lldb/Target/StructuredDataPlugin.h"
19 #include "lldb/Utility/Event.h"
20 #include "lldb/Utility/Instrumentation.h"
21 #include "lldb/Utility/Status.h"
22 #include "lldb/Utility/Stream.h"
23 #include "lldb/Utility/StringList.h"
24 #include "lldb/Utility/StructuredData.h"
25
26 using namespace lldb;
27 using namespace lldb_private;
28
29 #pragma mark--
30 #pragma mark SBStructuredData
31
SBStructuredData()32 SBStructuredData::SBStructuredData() : m_impl_up(new StructuredDataImpl()) {
33 LLDB_INSTRUMENT_VA(this);
34 }
35
SBStructuredData(const lldb::SBStructuredData & rhs)36 SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs)
37 : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up)) {
38 LLDB_INSTRUMENT_VA(this, rhs);
39 }
40
SBStructuredData(const lldb::SBScriptObject obj,const lldb::SBDebugger & debugger)41 SBStructuredData::SBStructuredData(const lldb::SBScriptObject obj,
42 const lldb::SBDebugger &debugger) {
43 LLDB_INSTRUMENT_VA(this, obj, debugger);
44
45 if (!obj.IsValid())
46 return;
47
48 ScriptInterpreter *interpreter =
49 debugger.m_opaque_sp->GetScriptInterpreter(true, obj.GetLanguage());
50
51 if (!interpreter)
52 return;
53
54 StructuredDataImplUP impl_up = std::make_unique<StructuredDataImpl>(
55 interpreter->CreateStructuredDataFromScriptObject(obj.ref()));
56 if (impl_up && impl_up->IsValid())
57 m_impl_up.reset(impl_up.release());
58 }
59
SBStructuredData(const lldb::EventSP & event_sp)60 SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp)
61 : m_impl_up(new StructuredDataImpl(event_sp)) {
62 LLDB_INSTRUMENT_VA(this, event_sp);
63 }
64
SBStructuredData(const lldb_private::StructuredDataImpl & impl)65 SBStructuredData::SBStructuredData(const lldb_private::StructuredDataImpl &impl)
66 : m_impl_up(new StructuredDataImpl(impl)) {
67 LLDB_INSTRUMENT_VA(this, impl);
68 }
69
70 SBStructuredData::~SBStructuredData() = default;
71
72 SBStructuredData &SBStructuredData::
operator =(const lldb::SBStructuredData & rhs)73 operator=(const lldb::SBStructuredData &rhs) {
74 LLDB_INSTRUMENT_VA(this, rhs);
75
76 *m_impl_up = *rhs.m_impl_up;
77 return *this;
78 }
79
SetFromJSON(lldb::SBStream & stream)80 lldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream &stream) {
81 LLDB_INSTRUMENT_VA(this, stream);
82
83 lldb::SBError error;
84
85 StructuredData::ObjectSP json_obj =
86 StructuredData::ParseJSON(stream.GetData());
87 m_impl_up->SetObjectSP(json_obj);
88
89 if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary)
90 error.SetErrorString("Invalid Syntax");
91 return error;
92 }
93
SetFromJSON(const char * json)94 lldb::SBError SBStructuredData::SetFromJSON(const char *json) {
95 LLDB_INSTRUMENT_VA(this, json);
96 lldb::SBStream s;
97 s.Print(json);
98 return SetFromJSON(s);
99 }
100
IsValid() const101 bool SBStructuredData::IsValid() const {
102 LLDB_INSTRUMENT_VA(this);
103 return this->operator bool();
104 }
105
operator bool() const106 SBStructuredData::operator bool() const {
107 LLDB_INSTRUMENT_VA(this);
108
109 return m_impl_up->IsValid();
110 }
111
Clear()112 void SBStructuredData::Clear() {
113 LLDB_INSTRUMENT_VA(this);
114
115 m_impl_up->Clear();
116 }
117
GetAsJSON(lldb::SBStream & stream) const118 SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const {
119 LLDB_INSTRUMENT_VA(this, stream);
120
121 SBError error;
122 error.SetError(m_impl_up->GetAsJSON(stream.ref()));
123 return error;
124 }
125
GetDescription(lldb::SBStream & stream) const126 lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const {
127 LLDB_INSTRUMENT_VA(this, stream);
128
129 Status error = m_impl_up->GetDescription(stream.ref());
130 SBError sb_error;
131 sb_error.SetError(error);
132 return sb_error;
133 }
134
GetType() const135 StructuredDataType SBStructuredData::GetType() const {
136 LLDB_INSTRUMENT_VA(this);
137
138 return m_impl_up->GetType();
139 }
140
GetSize() const141 size_t SBStructuredData::GetSize() const {
142 LLDB_INSTRUMENT_VA(this);
143
144 return m_impl_up->GetSize();
145 }
146
GetKeys(lldb::SBStringList & keys) const147 bool SBStructuredData::GetKeys(lldb::SBStringList &keys) const {
148 LLDB_INSTRUMENT_VA(this, keys);
149
150 if (GetType() != eStructuredDataTypeDictionary)
151 return false;
152
153 StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP();
154 if (!obj_sp)
155 return false;
156
157 StructuredData::Dictionary *dict = obj_sp->GetAsDictionary();
158 // We claimed we were a dictionary, so this can't be null.
159 assert(dict);
160 // The return kind of GetKeys is an Array:
161 StructuredData::ObjectSP array_sp = dict->GetKeys();
162 StructuredData::Array *key_arr = array_sp->GetAsArray();
163 assert(key_arr);
164
165 key_arr->ForEach([&keys](StructuredData::Object *object) -> bool {
166 llvm::StringRef key = object->GetStringValue("");
167 keys->AppendString(key);
168 return true;
169 });
170 return true;
171 }
172
GetValueForKey(const char * key) const173 lldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const {
174 LLDB_INSTRUMENT_VA(this, key);
175
176 SBStructuredData result;
177 result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key));
178 return result;
179 }
180
GetItemAtIndex(size_t idx) const181 lldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const {
182 LLDB_INSTRUMENT_VA(this, idx);
183
184 SBStructuredData result;
185 result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx));
186 return result;
187 }
188
GetIntegerValue(uint64_t fail_value) const189 uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const {
190 LLDB_INSTRUMENT_VA(this, fail_value);
191
192 return GetUnsignedIntegerValue(fail_value);
193 }
194
GetUnsignedIntegerValue(uint64_t fail_value) const195 uint64_t SBStructuredData::GetUnsignedIntegerValue(uint64_t fail_value) const {
196 LLDB_INSTRUMENT_VA(this, fail_value);
197
198 return m_impl_up->GetIntegerValue(fail_value);
199 }
200
GetSignedIntegerValue(int64_t fail_value) const201 int64_t SBStructuredData::GetSignedIntegerValue(int64_t fail_value) const {
202 LLDB_INSTRUMENT_VA(this, fail_value);
203
204 return m_impl_up->GetIntegerValue(fail_value);
205 }
206
GetFloatValue(double fail_value) const207 double SBStructuredData::GetFloatValue(double fail_value) const {
208 LLDB_INSTRUMENT_VA(this, fail_value);
209
210 return m_impl_up->GetFloatValue(fail_value);
211 }
212
GetBooleanValue(bool fail_value) const213 bool SBStructuredData::GetBooleanValue(bool fail_value) const {
214 LLDB_INSTRUMENT_VA(this, fail_value);
215
216 return m_impl_up->GetBooleanValue(fail_value);
217 }
218
GetStringValue(char * dst,size_t dst_len) const219 size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const {
220 LLDB_INSTRUMENT_VA(this, dst, dst_len);
221
222 return m_impl_up->GetStringValue(dst, dst_len);
223 }
224
GetGenericValue() const225 lldb::SBScriptObject SBStructuredData::GetGenericValue() const {
226 LLDB_INSTRUMENT_VA(this);
227
228 return {m_impl_up->GetGenericValue(), eScriptLanguageDefault};
229 }
230