/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/API/SBStructuredData.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
32 | 2.20k | SBStructuredData::SBStructuredData() : m_impl_up(new StructuredDataImpl()) { |
33 | 2.20k | LLDB_INSTRUMENT_VA(this); |
34 | 2.20k | } |
35 | | |
36 | | SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs) |
37 | 1.08k | : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up)) { |
38 | 1.08k | LLDB_INSTRUMENT_VA(this, rhs); |
39 | 1.08k | } |
40 | | |
41 | | SBStructuredData::SBStructuredData(const lldb::SBScriptObject obj, |
42 | 1 | const lldb::SBDebugger &debugger) { |
43 | 1 | LLDB_INSTRUMENT_VA(this, obj, debugger); |
44 | | |
45 | 1 | if (!obj.IsValid()) |
46 | 0 | return; |
47 | | |
48 | 1 | ScriptInterpreter *interpreter = |
49 | 1 | debugger.m_opaque_sp->GetScriptInterpreter(true, obj.GetLanguage()); |
50 | | |
51 | 1 | if (!interpreter) |
52 | 0 | return; |
53 | | |
54 | 1 | StructuredDataImplUP impl_up = std::make_unique<StructuredDataImpl>( |
55 | 1 | interpreter->CreateStructuredDataFromScriptObject(obj.ref())); |
56 | 1 | if (impl_up && impl_up->IsValid()) |
57 | 1 | m_impl_up.reset(impl_up.release()); |
58 | 1 | } |
59 | | |
60 | | SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp) |
61 | 0 | : m_impl_up(new StructuredDataImpl(event_sp)) { |
62 | 0 | LLDB_INSTRUMENT_VA(this, event_sp); |
63 | 0 | } |
64 | | |
65 | | SBStructuredData::SBStructuredData(const lldb_private::StructuredDataImpl &impl) |
66 | 51 | : m_impl_up(new StructuredDataImpl(impl)) { |
67 | 51 | LLDB_INSTRUMENT_VA(this, impl); |
68 | 51 | } |
69 | | |
70 | 3.33k | SBStructuredData::~SBStructuredData() = default; |
71 | | |
72 | | SBStructuredData &SBStructuredData:: |
73 | 1.08k | operator=(const lldb::SBStructuredData &rhs) { |
74 | 1.08k | LLDB_INSTRUMENT_VA(this, rhs); |
75 | | |
76 | 1.08k | *m_impl_up = *rhs.m_impl_up; |
77 | 1.08k | return *this; |
78 | 1.08k | } |
79 | | |
80 | 9 | lldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream &stream) { |
81 | 9 | LLDB_INSTRUMENT_VA(this, stream); |
82 | | |
83 | 9 | lldb::SBError error; |
84 | | |
85 | 9 | StructuredData::ObjectSP json_obj = |
86 | 9 | StructuredData::ParseJSON(stream.GetData()); |
87 | 9 | m_impl_up->SetObjectSP(json_obj); |
88 | | |
89 | 9 | if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary) |
90 | 0 | error.SetErrorString("Invalid Syntax"); |
91 | 9 | return error; |
92 | 9 | } |
93 | | |
94 | 0 | lldb::SBError SBStructuredData::SetFromJSON(const char *json) { |
95 | 0 | LLDB_INSTRUMENT_VA(this, json); |
96 | 0 | lldb::SBStream s; |
97 | 0 | s.Print(json); |
98 | 0 | return SetFromJSON(s); |
99 | 0 | } |
100 | | |
101 | 406 | bool SBStructuredData::IsValid() const { |
102 | 406 | LLDB_INSTRUMENT_VA(this); |
103 | 406 | return this->operator bool(); |
104 | 406 | } |
105 | | |
106 | 458 | SBStructuredData::operator bool() const { |
107 | 458 | LLDB_INSTRUMENT_VA(this); |
108 | | |
109 | 458 | return m_impl_up->IsValid(); |
110 | 458 | } |
111 | | |
112 | 0 | void SBStructuredData::Clear() { |
113 | 0 | LLDB_INSTRUMENT_VA(this); |
114 | |
|
115 | 0 | m_impl_up->Clear(); |
116 | 0 | } |
117 | | |
118 | 24 | SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const { |
119 | 24 | LLDB_INSTRUMENT_VA(this, stream); |
120 | | |
121 | 24 | SBError error; |
122 | 24 | error.SetError(m_impl_up->GetAsJSON(stream.ref())); |
123 | 24 | return error; |
124 | 24 | } |
125 | | |
126 | 1 | lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const { |
127 | 1 | LLDB_INSTRUMENT_VA(this, stream); |
128 | | |
129 | 1 | Status error = m_impl_up->GetDescription(stream.ref()); |
130 | 1 | SBError sb_error; |
131 | 1 | sb_error.SetError(error); |
132 | 1 | return sb_error; |
133 | 1 | } |
134 | | |
135 | 55 | StructuredDataType SBStructuredData::GetType() const { |
136 | 55 | LLDB_INSTRUMENT_VA(this); |
137 | | |
138 | 55 | return m_impl_up->GetType(); |
139 | 55 | } |
140 | | |
141 | 72 | size_t SBStructuredData::GetSize() const { |
142 | 72 | LLDB_INSTRUMENT_VA(this); |
143 | | |
144 | 72 | return m_impl_up->GetSize(); |
145 | 72 | } |
146 | | |
147 | 3 | bool SBStructuredData::GetKeys(lldb::SBStringList &keys) const { |
148 | 3 | LLDB_INSTRUMENT_VA(this, keys); |
149 | | |
150 | 3 | if (GetType() != eStructuredDataTypeDictionary) |
151 | 1 | return false; |
152 | | |
153 | 2 | StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP(); |
154 | 2 | if (!obj_sp) |
155 | 0 | return false; |
156 | | |
157 | 2 | StructuredData::Dictionary *dict = obj_sp->GetAsDictionary(); |
158 | | // We claimed we were a dictionary, so this can't be null. |
159 | 2 | assert(dict); |
160 | | // The return kind of GetKeys is an Array: |
161 | 2 | StructuredData::ObjectSP array_sp = dict->GetKeys(); |
162 | 2 | StructuredData::Array *key_arr = array_sp->GetAsArray(); |
163 | 2 | assert(key_arr); |
164 | | |
165 | 4 | key_arr->ForEach([&keys](StructuredData::Object *object) -> bool 2 { |
166 | 4 | llvm::StringRef key = object->GetStringValue(""); |
167 | 4 | keys->AppendString(key); |
168 | 4 | return true; |
169 | 4 | }); |
170 | 2 | return true; |
171 | 2 | } |
172 | | |
173 | 737 | lldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const { |
174 | 737 | LLDB_INSTRUMENT_VA(this, key); |
175 | | |
176 | 737 | SBStructuredData result; |
177 | 737 | result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key)); |
178 | 737 | return result; |
179 | 737 | } |
180 | | |
181 | 141 | lldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const { |
182 | 141 | LLDB_INSTRUMENT_VA(this, idx); |
183 | | |
184 | 141 | SBStructuredData result; |
185 | 141 | result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx)); |
186 | 141 | return result; |
187 | 141 | } |
188 | | |
189 | 1 | uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const { |
190 | 1 | LLDB_INSTRUMENT_VA(this, fail_value); |
191 | | |
192 | 1 | return GetUnsignedIntegerValue(fail_value); |
193 | 1 | } |
194 | | |
195 | 11 | uint64_t SBStructuredData::GetUnsignedIntegerValue(uint64_t fail_value) const { |
196 | 11 | LLDB_INSTRUMENT_VA(this, fail_value); |
197 | | |
198 | 11 | return m_impl_up->GetIntegerValue(fail_value); |
199 | 11 | } |
200 | | |
201 | 1 | int64_t SBStructuredData::GetSignedIntegerValue(int64_t fail_value) const { |
202 | 1 | LLDB_INSTRUMENT_VA(this, fail_value); |
203 | | |
204 | 1 | return m_impl_up->GetIntegerValue(fail_value); |
205 | 1 | } |
206 | | |
207 | 1 | double SBStructuredData::GetFloatValue(double fail_value) const { |
208 | 1 | LLDB_INSTRUMENT_VA(this, fail_value); |
209 | | |
210 | 1 | return m_impl_up->GetFloatValue(fail_value); |
211 | 1 | } |
212 | | |
213 | 93 | bool SBStructuredData::GetBooleanValue(bool fail_value) const { |
214 | 93 | LLDB_INSTRUMENT_VA(this, fail_value); |
215 | | |
216 | 93 | return m_impl_up->GetBooleanValue(fail_value); |
217 | 93 | } |
218 | | |
219 | 497 | size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const { |
220 | 497 | LLDB_INSTRUMENT_VA(this, dst, dst_len); |
221 | | |
222 | 497 | return m_impl_up->GetStringValue(dst, dst_len); |
223 | 497 | } |
224 | | |
225 | 1 | lldb::SBScriptObject SBStructuredData::GetGenericValue() const { |
226 | 1 | LLDB_INSTRUMENT_VA(this); |
227 | | |
228 | 1 | return {m_impl_up->GetGenericValue(), eScriptLanguageDefault}; |
229 | 1 | } |