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 "SBReproducerPrivate.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 27 SBStructuredData::SBStructuredData() : m_impl_up(new StructuredDataImpl()) { 28 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBStructuredData); 29 } 30 31 SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs) 32 : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up)) { 33 LLDB_RECORD_CONSTRUCTOR(SBStructuredData, (const lldb::SBStructuredData &), 34 rhs); 35 } 36 37 SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp) 38 : m_impl_up(new StructuredDataImpl(event_sp)) { 39 LLDB_RECORD_CONSTRUCTOR(SBStructuredData, (const lldb::EventSP &), event_sp); 40 } 41 42 SBStructuredData::SBStructuredData(lldb_private::StructuredDataImpl *impl) 43 : m_impl_up(impl ? impl : new StructuredDataImpl()) { 44 LLDB_RECORD_CONSTRUCTOR(SBStructuredData, 45 (lldb_private::StructuredDataImpl *), impl); 46 } 47 48 SBStructuredData::~SBStructuredData() = default; 49 50 SBStructuredData &SBStructuredData:: 51 operator=(const lldb::SBStructuredData &rhs) { 52 LLDB_RECORD_METHOD( 53 lldb::SBStructuredData &, 54 SBStructuredData, operator=,(const lldb::SBStructuredData &), rhs); 55 56 *m_impl_up = *rhs.m_impl_up; 57 return LLDB_RECORD_RESULT(*this); 58 } 59 60 lldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream &stream) { 61 LLDB_RECORD_METHOD(lldb::SBError, SBStructuredData, SetFromJSON, 62 (lldb::SBStream &), stream); 63 64 lldb::SBError error; 65 std::string json_str(stream.GetData()); 66 67 StructuredData::ObjectSP json_obj = StructuredData::ParseJSON(json_str); 68 m_impl_up->SetObjectSP(json_obj); 69 70 if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary) 71 error.SetErrorString("Invalid Syntax"); 72 return LLDB_RECORD_RESULT(error); 73 } 74 75 bool SBStructuredData::IsValid() const { 76 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStructuredData, IsValid); 77 return this->operator bool(); 78 } 79 SBStructuredData::operator bool() const { 80 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStructuredData, operator bool); 81 82 return m_impl_up->IsValid(); 83 } 84 85 void SBStructuredData::Clear() { 86 LLDB_RECORD_METHOD_NO_ARGS(void, SBStructuredData, Clear); 87 88 m_impl_up->Clear(); 89 } 90 91 SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const { 92 LLDB_RECORD_METHOD_CONST(lldb::SBError, SBStructuredData, GetAsJSON, 93 (lldb::SBStream &), stream); 94 95 SBError error; 96 error.SetError(m_impl_up->GetAsJSON(stream.ref())); 97 return LLDB_RECORD_RESULT(error); 98 } 99 100 lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const { 101 LLDB_RECORD_METHOD_CONST(lldb::SBError, SBStructuredData, GetDescription, 102 (lldb::SBStream &), stream); 103 104 Status error = m_impl_up->GetDescription(stream.ref()); 105 SBError sb_error; 106 sb_error.SetError(error); 107 return LLDB_RECORD_RESULT(sb_error); 108 } 109 110 StructuredDataType SBStructuredData::GetType() const { 111 LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::StructuredDataType, SBStructuredData, 112 GetType); 113 114 return m_impl_up->GetType(); 115 } 116 117 size_t SBStructuredData::GetSize() const { 118 LLDB_RECORD_METHOD_CONST_NO_ARGS(size_t, SBStructuredData, GetSize); 119 120 return m_impl_up->GetSize(); 121 } 122 123 bool SBStructuredData::GetKeys(lldb::SBStringList &keys) const { 124 LLDB_RECORD_METHOD_CONST(bool, SBStructuredData, GetKeys, 125 (lldb::SBStringList &), keys); 126 127 if (GetType() != eStructuredDataTypeDictionary) 128 return false; 129 130 StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP(); 131 if (!obj_sp) 132 return false; 133 134 StructuredData::Dictionary *dict = obj_sp->GetAsDictionary(); 135 // We claimed we were a dictionary, so this can't be null. 136 assert(dict); 137 // The return kind of GetKeys is an Array: 138 StructuredData::ObjectSP array_sp = dict->GetKeys(); 139 StructuredData::Array *key_arr = array_sp->GetAsArray(); 140 assert(key_arr); 141 142 key_arr->ForEach([&keys] (StructuredData::Object *object) -> bool { 143 llvm::StringRef key = object->GetStringValue(""); 144 keys.AppendString(key.str().c_str()); 145 return true; 146 }); 147 return true; 148 } 149 150 lldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const { 151 LLDB_RECORD_METHOD_CONST(lldb::SBStructuredData, SBStructuredData, 152 GetValueForKey, (const char *), key); 153 154 SBStructuredData result; 155 result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key)); 156 return LLDB_RECORD_RESULT(result); 157 } 158 159 lldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const { 160 LLDB_RECORD_METHOD_CONST(lldb::SBStructuredData, SBStructuredData, 161 GetItemAtIndex, (size_t), idx); 162 163 SBStructuredData result; 164 result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx)); 165 return LLDB_RECORD_RESULT(result); 166 } 167 168 uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const { 169 LLDB_RECORD_METHOD_CONST(uint64_t, SBStructuredData, GetIntegerValue, 170 (uint64_t), fail_value); 171 172 return m_impl_up->GetIntegerValue(fail_value); 173 } 174 175 double SBStructuredData::GetFloatValue(double fail_value) const { 176 LLDB_RECORD_METHOD_CONST(double, SBStructuredData, GetFloatValue, (double), 177 fail_value); 178 179 return m_impl_up->GetFloatValue(fail_value); 180 } 181 182 bool SBStructuredData::GetBooleanValue(bool fail_value) const { 183 LLDB_RECORD_METHOD_CONST(bool, SBStructuredData, GetBooleanValue, (bool), 184 fail_value); 185 186 return m_impl_up->GetBooleanValue(fail_value); 187 } 188 189 size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const { 190 LLDB_RECORD_CHAR_PTR_METHOD_CONST(size_t, SBStructuredData, GetStringValue, 191 (char *, size_t), dst, "", dst_len); 192 193 return m_impl_up->GetStringValue(dst, dst_len); 194 } 195 196 namespace lldb_private { 197 namespace repro { 198 199 template <> void RegisterMethods<SBStructuredData>(Registry &R) { 200 LLDB_REGISTER_CONSTRUCTOR(SBStructuredData, ()); 201 LLDB_REGISTER_CONSTRUCTOR(SBStructuredData, (const lldb::SBStructuredData &)); 202 LLDB_REGISTER_CONSTRUCTOR(SBStructuredData, (const lldb::EventSP &)); 203 LLDB_REGISTER_CONSTRUCTOR(SBStructuredData, 204 (lldb_private::StructuredDataImpl *)); 205 LLDB_REGISTER_METHOD( 206 lldb::SBStructuredData &, 207 SBStructuredData, operator=,(const lldb::SBStructuredData &)); 208 LLDB_REGISTER_METHOD(lldb::SBError, SBStructuredData, SetFromJSON, 209 (lldb::SBStream &)); 210 LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, IsValid, ()); 211 LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, operator bool, ()); 212 LLDB_REGISTER_METHOD(void, SBStructuredData, Clear, ()); 213 LLDB_REGISTER_METHOD_CONST(lldb::SBError, SBStructuredData, GetAsJSON, 214 (lldb::SBStream &)); 215 LLDB_REGISTER_METHOD_CONST(lldb::SBError, SBStructuredData, GetDescription, 216 (lldb::SBStream &)); 217 LLDB_REGISTER_METHOD_CONST(lldb::StructuredDataType, SBStructuredData, 218 GetType, ()); 219 LLDB_REGISTER_METHOD_CONST(size_t, SBStructuredData, GetSize, ()); 220 LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, GetKeys, 221 (lldb::SBStringList &)); 222 LLDB_REGISTER_METHOD_CONST(lldb::SBStructuredData, SBStructuredData, 223 GetValueForKey, (const char *)); 224 LLDB_REGISTER_METHOD_CONST(lldb::SBStructuredData, SBStructuredData, 225 GetItemAtIndex, (size_t)); 226 LLDB_REGISTER_METHOD_CONST(uint64_t, SBStructuredData, GetIntegerValue, 227 (uint64_t)); 228 LLDB_REGISTER_METHOD_CONST(double, SBStructuredData, GetFloatValue, (double)); 229 LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, GetBooleanValue, (bool)); 230 LLDB_REGISTER_CHAR_PTR_METHOD_CONST(size_t, SBStructuredData, GetStringValue); 231 } 232 233 } // namespace repro 234 } // namespace lldb_private 235