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 #include "lldb/Utility/Instrumentation.h"
11
12 #include "lldb/API/SBStream.h"
13 #include "lldb/API/SBStringList.h"
14 #include "lldb/Core/StructuredDataImpl.h"
15 #include "lldb/Target/StructuredDataPlugin.h"
16 #include "lldb/Utility/Event.h"
17 #include "lldb/Utility/Status.h"
18 #include "lldb/Utility/Stream.h"
19 #include "lldb/Utility/StructuredData.h"
20
21 using namespace lldb;
22 using namespace lldb_private;
23
24 #pragma mark--
25 #pragma mark SBStructuredData
26
SBStructuredData()27 SBStructuredData::SBStructuredData() : m_impl_up(new StructuredDataImpl()) {
28 LLDB_INSTRUMENT_VA(this);
29 }
30
SBStructuredData(const lldb::SBStructuredData & rhs)31 SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs)
32 : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up)) {
33 LLDB_INSTRUMENT_VA(this, rhs);
34 }
35
SBStructuredData(const lldb::EventSP & event_sp)36 SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp)
37 : m_impl_up(new StructuredDataImpl(event_sp)) {
38 LLDB_INSTRUMENT_VA(this, event_sp);
39 }
40
SBStructuredData(const lldb_private::StructuredDataImpl & impl)41 SBStructuredData::SBStructuredData(const lldb_private::StructuredDataImpl &impl)
42 : m_impl_up(new StructuredDataImpl(impl)) {
43 LLDB_INSTRUMENT_VA(this, impl);
44 }
45
46 SBStructuredData::~SBStructuredData() = default;
47
48 SBStructuredData &SBStructuredData::
operator =(const lldb::SBStructuredData & rhs)49 operator=(const lldb::SBStructuredData &rhs) {
50 LLDB_INSTRUMENT_VA(this, rhs);
51
52 *m_impl_up = *rhs.m_impl_up;
53 return *this;
54 }
55
SetFromJSON(lldb::SBStream & stream)56 lldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream &stream) {
57 LLDB_INSTRUMENT_VA(this, stream);
58
59 lldb::SBError error;
60 std::string json_str(stream.GetData());
61
62 StructuredData::ObjectSP json_obj = StructuredData::ParseJSON(json_str);
63 m_impl_up->SetObjectSP(json_obj);
64
65 if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary)
66 error.SetErrorString("Invalid Syntax");
67 return error;
68 }
69
SetFromJSON(const char * json)70 lldb::SBError SBStructuredData::SetFromJSON(const char *json) {
71 LLDB_INSTRUMENT_VA(this, json);
72 lldb::SBStream s;
73 s.Print(json);
74 return SetFromJSON(s);
75 }
76
IsValid() const77 bool SBStructuredData::IsValid() const {
78 LLDB_INSTRUMENT_VA(this);
79 return this->operator bool();
80 }
81
operator bool() const82 SBStructuredData::operator bool() const {
83 LLDB_INSTRUMENT_VA(this);
84
85 return m_impl_up->IsValid();
86 }
87
Clear()88 void SBStructuredData::Clear() {
89 LLDB_INSTRUMENT_VA(this);
90
91 m_impl_up->Clear();
92 }
93
GetAsJSON(lldb::SBStream & stream) const94 SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const {
95 LLDB_INSTRUMENT_VA(this, stream);
96
97 SBError error;
98 error.SetError(m_impl_up->GetAsJSON(stream.ref()));
99 return error;
100 }
101
GetDescription(lldb::SBStream & stream) const102 lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const {
103 LLDB_INSTRUMENT_VA(this, stream);
104
105 Status error = m_impl_up->GetDescription(stream.ref());
106 SBError sb_error;
107 sb_error.SetError(error);
108 return sb_error;
109 }
110
GetType() const111 StructuredDataType SBStructuredData::GetType() const {
112 LLDB_INSTRUMENT_VA(this);
113
114 return m_impl_up->GetType();
115 }
116
GetSize() const117 size_t SBStructuredData::GetSize() const {
118 LLDB_INSTRUMENT_VA(this);
119
120 return m_impl_up->GetSize();
121 }
122
GetKeys(lldb::SBStringList & keys) const123 bool SBStructuredData::GetKeys(lldb::SBStringList &keys) const {
124 LLDB_INSTRUMENT_VA(this, keys);
125
126 if (GetType() != eStructuredDataTypeDictionary)
127 return false;
128
129 StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP();
130 if (!obj_sp)
131 return false;
132
133 StructuredData::Dictionary *dict = obj_sp->GetAsDictionary();
134 // We claimed we were a dictionary, so this can't be null.
135 assert(dict);
136 // The return kind of GetKeys is an Array:
137 StructuredData::ObjectSP array_sp = dict->GetKeys();
138 StructuredData::Array *key_arr = array_sp->GetAsArray();
139 assert(key_arr);
140
141 key_arr->ForEach([&keys] (StructuredData::Object *object) -> bool {
142 llvm::StringRef key = object->GetStringValue("");
143 keys.AppendString(key.str().c_str());
144 return true;
145 });
146 return true;
147 }
148
GetValueForKey(const char * key) const149 lldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const {
150 LLDB_INSTRUMENT_VA(this, key);
151
152 SBStructuredData result;
153 result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key));
154 return result;
155 }
156
GetItemAtIndex(size_t idx) const157 lldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const {
158 LLDB_INSTRUMENT_VA(this, idx);
159
160 SBStructuredData result;
161 result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx));
162 return result;
163 }
164
GetIntegerValue(uint64_t fail_value) const165 uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const {
166 LLDB_INSTRUMENT_VA(this, fail_value);
167
168 return m_impl_up->GetIntegerValue(fail_value);
169 }
170
GetFloatValue(double fail_value) const171 double SBStructuredData::GetFloatValue(double fail_value) const {
172 LLDB_INSTRUMENT_VA(this, fail_value);
173
174 return m_impl_up->GetFloatValue(fail_value);
175 }
176
GetBooleanValue(bool fail_value) const177 bool SBStructuredData::GetBooleanValue(bool fail_value) const {
178 LLDB_INSTRUMENT_VA(this, fail_value);
179
180 return m_impl_up->GetBooleanValue(fail_value);
181 }
182
GetStringValue(char * dst,size_t dst_len) const183 size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const {
184 LLDB_INSTRUMENT_VA(this, dst, dst_len);
185
186 return m_impl_up->GetStringValue(dst, dst_len);
187 }
188