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/ReproducerInstrumentation.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(const lldb_private::StructuredDataImpl &impl) 43 : m_impl_up(new StructuredDataImpl(impl)) { 44 LLDB_RECORD_CONSTRUCTOR(SBStructuredData, 45 (const 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 *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 error; 73 } 74 75 lldb::SBError SBStructuredData::SetFromJSON(const char *json) { 76 LLDB_RECORD_METHOD(lldb::SBError, SBStructuredData, SetFromJSON, 77 (const char *), json); 78 lldb::SBStream s; 79 s.Print(json); 80 return SetFromJSON(s); 81 } 82 83 bool SBStructuredData::IsValid() const { 84 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStructuredData, IsValid); 85 return this->operator bool(); 86 } 87 88 SBStructuredData::operator bool() const { 89 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStructuredData, operator bool); 90 91 return m_impl_up->IsValid(); 92 } 93 94 void SBStructuredData::Clear() { 95 LLDB_RECORD_METHOD_NO_ARGS(void, SBStructuredData, Clear); 96 97 m_impl_up->Clear(); 98 } 99 100 SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const { 101 LLDB_RECORD_METHOD_CONST(lldb::SBError, SBStructuredData, GetAsJSON, 102 (lldb::SBStream &), stream); 103 104 SBError error; 105 error.SetError(m_impl_up->GetAsJSON(stream.ref())); 106 return error; 107 } 108 109 lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const { 110 LLDB_RECORD_METHOD_CONST(lldb::SBError, SBStructuredData, GetDescription, 111 (lldb::SBStream &), stream); 112 113 Status error = m_impl_up->GetDescription(stream.ref()); 114 SBError sb_error; 115 sb_error.SetError(error); 116 return sb_error; 117 } 118 119 StructuredDataType SBStructuredData::GetType() const { 120 LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::StructuredDataType, SBStructuredData, 121 GetType); 122 123 return m_impl_up->GetType(); 124 } 125 126 size_t SBStructuredData::GetSize() const { 127 LLDB_RECORD_METHOD_CONST_NO_ARGS(size_t, SBStructuredData, GetSize); 128 129 return m_impl_up->GetSize(); 130 } 131 132 bool SBStructuredData::GetKeys(lldb::SBStringList &keys) const { 133 LLDB_RECORD_METHOD_CONST(bool, SBStructuredData, GetKeys, 134 (lldb::SBStringList &), keys); 135 136 if (GetType() != eStructuredDataTypeDictionary) 137 return false; 138 139 StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP(); 140 if (!obj_sp) 141 return false; 142 143 StructuredData::Dictionary *dict = obj_sp->GetAsDictionary(); 144 // We claimed we were a dictionary, so this can't be null. 145 assert(dict); 146 // The return kind of GetKeys is an Array: 147 StructuredData::ObjectSP array_sp = dict->GetKeys(); 148 StructuredData::Array *key_arr = array_sp->GetAsArray(); 149 assert(key_arr); 150 151 key_arr->ForEach([&keys] (StructuredData::Object *object) -> bool { 152 llvm::StringRef key = object->GetStringValue(""); 153 keys.AppendString(key.str().c_str()); 154 return true; 155 }); 156 return true; 157 } 158 159 lldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const { 160 LLDB_RECORD_METHOD_CONST(lldb::SBStructuredData, SBStructuredData, 161 GetValueForKey, (const char *), key); 162 163 SBStructuredData result; 164 result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key)); 165 return result; 166 } 167 168 lldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const { 169 LLDB_RECORD_METHOD_CONST(lldb::SBStructuredData, SBStructuredData, 170 GetItemAtIndex, (size_t), idx); 171 172 SBStructuredData result; 173 result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx)); 174 return result; 175 } 176 177 uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const { 178 LLDB_RECORD_METHOD_CONST(uint64_t, SBStructuredData, GetIntegerValue, 179 (uint64_t), fail_value); 180 181 return m_impl_up->GetIntegerValue(fail_value); 182 } 183 184 double SBStructuredData::GetFloatValue(double fail_value) const { 185 LLDB_RECORD_METHOD_CONST(double, SBStructuredData, GetFloatValue, (double), 186 fail_value); 187 188 return m_impl_up->GetFloatValue(fail_value); 189 } 190 191 bool SBStructuredData::GetBooleanValue(bool fail_value) const { 192 LLDB_RECORD_METHOD_CONST(bool, SBStructuredData, GetBooleanValue, (bool), 193 fail_value); 194 195 return m_impl_up->GetBooleanValue(fail_value); 196 } 197 198 size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const { 199 LLDB_RECORD_CHAR_PTR_METHOD_CONST(size_t, SBStructuredData, GetStringValue, 200 (char *, size_t), dst, "", dst_len); 201 202 return m_impl_up->GetStringValue(dst, dst_len); 203 } 204