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
34}
35
37 : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up)) {
38 LLDB_INSTRUMENT_VA(this, rhs);
39}
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
66 : m_impl_up(new StructuredDataImpl(impl)) {
67 LLDB_INSTRUMENT_VA(this, impl);
68}
69
71
74 LLDB_INSTRUMENT_VA(this, rhs);
75
76 *m_impl_up = *rhs.m_impl_up;
77 return *this;
78}
79
81 LLDB_INSTRUMENT_VA(this, stream);
82
84
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
95 LLDB_INSTRUMENT_VA(this, json);
97 s.Print(json);
98 return SetFromJSON(s);
99}
100
102 LLDB_INSTRUMENT_VA(this);
103 return this->operator bool();
104}
105
106SBStructuredData::operator bool() const {
107 LLDB_INSTRUMENT_VA(this);
108
109 return m_impl_up->IsValid();
110}
111
113 LLDB_INSTRUMENT_VA(this);
114
115 m_impl_up->Clear();
116}
117
119 LLDB_INSTRUMENT_VA(this, stream);
120
122 error.SetError(m_impl_up->GetAsJSON(stream.ref()));
123 return error;
124}
125
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
136 LLDB_INSTRUMENT_VA(this);
137
138 return m_impl_up->GetType();
139}
140
142 LLDB_INSTRUMENT_VA(this);
143
144 return m_impl_up->GetSize();
145}
146
148 LLDB_INSTRUMENT_VA(this, keys);
149
151 return false;
152
153 StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP();
154 if (!obj_sp)
155 return false;
156
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
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
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
189uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const {
190 LLDB_INSTRUMENT_VA(this, fail_value);
191
192 return GetUnsignedIntegerValue(fail_value);
193}
194
195uint64_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
201int64_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
207double SBStructuredData::GetFloatValue(double fail_value) const {
208 LLDB_INSTRUMENT_VA(this, fail_value);
209
210 return m_impl_up->GetFloatValue(fail_value);
211}
212
213bool SBStructuredData::GetBooleanValue(bool fail_value) const {
214 LLDB_INSTRUMENT_VA(this, fail_value);
215
216 return m_impl_up->GetBooleanValue(fail_value);
217}
218
219size_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
226 LLDB_INSTRUMENT_VA(this);
227
228 return {m_impl_up->GetGenericValue(), eScriptLanguageDefault};
229}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_INSTRUMENT_VA(...)
lldb::DebuggerSP m_opaque_sp
Definition: SBDebugger.h:505
void SetError(uint32_t err, lldb::ErrorType type)
Definition: SBError.cpp:106
lldb::ScriptLanguage GetLanguage() const
lldb_private::ScriptObject & ref()
void Print(const char *str)
Definition: SBStream.cpp:65
lldb_private::Stream & ref()
Definition: SBStream.cpp:177
const char * GetData()
Definition: SBStream.cpp:44
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.
lldb::SBError GetDescription(lldb::SBStream &stream) const
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
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.
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.
lldb::SBError GetAsJSON(lldb::SBStream &stream) const
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.
virtual StructuredData::ObjectSP CreateStructuredDataFromScriptObject(ScriptObject obj)
An error handling class.
Definition: Status.h:44
void AppendString(const std::string &s)
Definition: StringList.cpp:43
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.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
@ eScriptLanguageDefault
std::shared_ptr< lldb_private::Event > EventSP
Definition: lldb-forward.h:337
std::unique_ptr< lldb_private::StructuredDataImpl > StructuredDataImplUP
Definition: lldb-forward.h:424
@ eStructuredDataTypeDictionary