LLDB mainline
SBStructuredData.cpp
Go to the documentation of this file.
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
10
11#include "lldb/API/SBDebugger.h"
13#include "lldb/API/SBStream.h"
15#include "lldb/Core/Debugger.h"
19#include "lldb/Utility/Event.h"
21#include "lldb/Utility/Status.h"
22#include "lldb/Utility/Stream.h"
25
26using namespace lldb;
27using namespace lldb_private;
28
29#pragma mark--
30#pragma mark SBStructuredData
31
35
40
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
61 : m_impl_up(new StructuredDataImpl(event_sp)) {
62 LLDB_INSTRUMENT_VA(this, event_sp);
63}
64
69
71
74 LLDB_INSTRUMENT_VA(this, rhs);
75
76 *m_impl_up = *rhs.m_impl_up;
77 return *this;
78}
79
81 new_impl.SetObjectSP(m_impl_up->GetObjectSP());
82}
83
85 LLDB_INSTRUMENT_VA(this, stream);
86
88
91 m_impl_up->SetObjectSP(json_obj);
92
93 static constexpr StructuredDataType unsupported_type[] = {
96 };
97
98 if (!json_obj || llvm::is_contained(unsupported_type, json_obj->GetType()))
99 error = Status::FromErrorString("Invalid Syntax");
100 return error;
101}
102
104 LLDB_INSTRUMENT_VA(this, json);
106 s.Print(json);
107 return SetFromJSON(s);
108}
109
111 LLDB_INSTRUMENT_VA(this);
112 return this->operator bool();
113}
114
115SBStructuredData::operator bool() const {
116 LLDB_INSTRUMENT_VA(this);
117
118 return m_impl_up->IsValid();
119}
120
122 LLDB_INSTRUMENT_VA(this);
123
124 m_impl_up->Clear();
125}
126
128 LLDB_INSTRUMENT_VA(this, stream);
129
131 error.SetError(m_impl_up->GetAsJSON(stream.ref()));
132 return error;
133}
134
136 LLDB_INSTRUMENT_VA(this, stream);
137
138 Status error = m_impl_up->GetDescription(stream.ref());
139 SBError sb_error;
140 sb_error.SetError(std::move(error));
141 return sb_error;
142}
143
145 LLDB_INSTRUMENT_VA(this);
146
147 return m_impl_up->GetType();
148}
149
151 LLDB_INSTRUMENT_VA(this);
152
153 return m_impl_up->GetSize();
154}
155
157 LLDB_INSTRUMENT_VA(this, keys);
158
160 return false;
161
162 StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP();
163 if (!obj_sp)
164 return false;
165
166 StructuredData::Dictionary *dict = obj_sp->GetAsDictionary();
167 // We claimed we were a dictionary, so this can't be null.
168 assert(dict);
169 // The return kind of GetKeys is an Array:
170 StructuredData::ObjectSP array_sp = dict->GetKeys();
171 StructuredData::Array *key_arr = array_sp->GetAsArray();
172 assert(key_arr);
173
174 key_arr->ForEach([&keys](StructuredData::Object *object) -> bool {
175 llvm::StringRef key = object->GetStringValue("");
176 keys->AppendString(key);
177 return true;
178 });
179 return true;
180}
181
183 LLDB_INSTRUMENT_VA(this, key);
184
185 SBStructuredData result;
186 result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key));
187 return result;
188}
189
191 LLDB_INSTRUMENT_VA(this, idx);
192
193 SBStructuredData result;
194 result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx));
195 return result;
196}
197
198uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const {
199 LLDB_INSTRUMENT_VA(this, fail_value);
200
201 return GetUnsignedIntegerValue(fail_value);
202}
203
204uint64_t SBStructuredData::GetUnsignedIntegerValue(uint64_t fail_value) const {
205 LLDB_INSTRUMENT_VA(this, fail_value);
206
207 return m_impl_up->GetIntegerValue(fail_value);
208}
209
210int64_t SBStructuredData::GetSignedIntegerValue(int64_t fail_value) const {
211 LLDB_INSTRUMENT_VA(this, fail_value);
212
213 return m_impl_up->GetIntegerValue(fail_value);
214}
215
216double SBStructuredData::GetFloatValue(double fail_value) const {
217 LLDB_INSTRUMENT_VA(this, fail_value);
218
219 return m_impl_up->GetFloatValue(fail_value);
220}
221
222bool SBStructuredData::GetBooleanValue(bool fail_value) const {
223 LLDB_INSTRUMENT_VA(this, fail_value);
224
225 return m_impl_up->GetBooleanValue(fail_value);
226}
227
228size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const {
229 LLDB_INSTRUMENT_VA(this, dst, dst_len);
230
231 return m_impl_up->GetStringValue(dst, dst_len);
232}
233
239
241 SBStructuredData &value) {
242 LLDB_INSTRUMENT_VA(this, key, value);
243
244 if (StructuredData::ObjectSP obj_sp = value.m_impl_up->GetObjectSP())
245 m_impl_up->SetValueForKey(key, obj_sp);
246}
247
249 LLDB_INSTRUMENT_VA(this, value);
250
251 m_impl_up->SetUnsignedIntegerValue(value);
252}
253
255 LLDB_INSTRUMENT_VA(this, value);
256
257 m_impl_up->SetSignedIntegerValue(value);
258}
259
261 LLDB_INSTRUMENT_VA(this, value);
262
263 m_impl_up->SetFloatValue(value);
264}
265
267 LLDB_INSTRUMENT_VA(this, value);
268
269 m_impl_up->SetBooleanValue(value);
270}
271
272void SBStructuredData::SetStringValue(const char *value) {
273 LLDB_INSTRUMENT_VA(this, value);
274
275 m_impl_up->SetStringValue(value);
276}
277
279 LLDB_INSTRUMENT_VA(this, value);
280
281 m_impl_up->SetGenericValue(value.GetPointer());
282}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_INSTRUMENT_VA(...)
lldb::DebuggerSP m_opaque_sp
Definition SBDebugger.h:699
void SetError(uint32_t err, lldb::ErrorType type)
Definition SBError.cpp:124
lldb::ScriptLanguage GetLanguage() const
lldb::ScriptObjectPtr GetPointer() const
lldb_private::ScriptObject & ref()
void Print(const char *str)
Definition SBStream.cpp:65
lldb_private::Stream & ref()
Definition SBStream.cpp:178
const char * GetData()
Definition SBStream.cpp:44
void SetStringValue(const char *value)
Change the type to string and overwrite the previous data with the new value.
lldb::SBStructuredData GetItemAtIndex(size_t idx) const
Return the value corresponding to an index if this data structure is array.
lldb::SBStructuredData & operator=(const lldb::SBStructuredData &rhs)
int64_t GetSignedIntegerValue(int64_t fail_value=0) const
Return the integer value if this data structure is an integer type.
void SetGenericValue(SBScriptObject value)
Change the type to generic and overwrite the previous data with the new value.
lldb::SBError GetDescription(lldb::SBStream &stream) const
void SetBooleanValue(bool value)
Change the type to boolean and overwrite the previous data with the new value.
size_t GetStringValue(char *dst, size_t dst_len) const
Provides the string value if this data structure is a string type.
lldb::SBScriptObject GetGenericValue() const
Return the generic pointer if this data structure is a generic type.
bool GetBooleanValue(bool fail_value=false) const
Return the boolean value if this data structure is a boolean type.
StructuredDataImplUP m_impl_up
void SetUnsignedIntegerValue(uint64_t value)
Change the type to unsigned integer and overwrite the previous data with the new value.
lldb::StructuredDataType GetType() const
Return the type of data in this data structure.
uint64_t GetUnsignedIntegerValue(uint64_t fail_value=0) const
Return the integer value if this data structure is an integer type.
void SetSignedIntegerValue(int64_t value)
Change the type to signed integer and overwrite the previous data with the new value.
size_t GetSize() const
Return the size (i.e.
bool GetKeys(lldb::SBStringList &keys) const
Fill keys with the keys in this object and return true if this data structure is a dictionary.
void CopyImpl(lldb_private::StructuredDataImpl &new_impl)
lldb::SBError GetAsJSON(lldb::SBStream &stream) const
void SetFloatValue(double value)
Change the type to float and overwrite the previous data with the new value.
lldb::SBStructuredData GetValueForKey(const char *key) const
Return the value corresponding to a key if this data structure is a dictionary type.
lldb::SBError SetFromJSON(lldb::SBStream &stream)
double GetFloatValue(double fail_value=0.0) const
Return the floating point value if this data structure is a floating type.
void SetValueForKey(const char *key, SBStructuredData &value)
Set the value corresponding to a key.
virtual StructuredData::ObjectSP CreateStructuredDataFromScriptObject(ScriptObject obj)
An error handling class.
Definition Status.h:118
static Status FromErrorString(const char *str)
Definition Status.h:141
void AppendString(const std::string &s)
void SetObjectSP(const StructuredData::ObjectSP &obj)
bool ForEach(std::function< bool(Object *object)> const &foreach_callback) const
std::shared_ptr< Object > ObjectSP
static ObjectSP ParseJSON(llvm::StringRef json_text)
A class that represents a running process on the host machine.
@ eScriptLanguageDefault
std::shared_ptr< lldb_private::Event > EventSP
std::unique_ptr< lldb_private::StructuredDataImpl > StructuredDataImplUP
@ eStructuredDataTypeDictionary
@ eStructuredDataTypeInvalid
@ eStructuredDataTypeGeneric