Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 1 | //===-- SBStructuredData.cpp ------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/API/SBStructuredData.h" |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 10 | #include "SBReproducerPrivate.h" |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 11 | |
| 12 | #include "lldb/API/SBStream.h" |
Jim Ingham | 3815e70 | 2018-09-13 21:35:32 +0000 | [diff] [blame] | 13 | #include "lldb/API/SBStringList.h" |
Ravitheja Addepally | d5d8d91 | 2017-04-26 08:48:50 +0000 | [diff] [blame] | 14 | #include "lldb/Core/StructuredDataImpl.h" |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 15 | #include "lldb/Target/StructuredDataPlugin.h" |
Pavel Labath | 181b823 | 2018-12-14 15:59:49 +0000 | [diff] [blame] | 16 | #include "lldb/Utility/Event.h" |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 17 | #include "lldb/Utility/Status.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/Stream.h" |
Pavel Labath | f2a8bcc | 2017-06-27 10:45:31 +0000 | [diff] [blame] | 19 | #include "lldb/Utility/StructuredData.h" |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace lldb; |
| 22 | using namespace lldb_private; |
| 23 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 24 | #pragma mark-- |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 25 | #pragma mark SBStructuredData |
| 26 | |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 27 | SBStructuredData::SBStructuredData() : m_impl_up(new StructuredDataImpl()) { |
| 28 | LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBStructuredData); |
| 29 | } |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 30 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 31 | SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs) |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 32 | : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up.get())) { |
| 33 | LLDB_RECORD_CONSTRUCTOR(SBStructuredData, (const lldb::SBStructuredData &), |
| 34 | rhs); |
| 35 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | |
| 37 | SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp) |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 38 | : m_impl_up(new StructuredDataImpl(event_sp)) { |
| 39 | LLDB_RECORD_CONSTRUCTOR(SBStructuredData, (const lldb::EventSP &), event_sp); |
| 40 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | |
Jim Ingham | 3815e70 | 2018-09-13 21:35:32 +0000 | [diff] [blame] | 42 | SBStructuredData::SBStructuredData(lldb_private::StructuredDataImpl *impl) |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 43 | : m_impl_up(impl) { |
| 44 | LLDB_RECORD_CONSTRUCTOR(SBStructuredData, |
| 45 | (lldb_private::StructuredDataImpl *), impl); |
| 46 | } |
Jim Ingham | 3815e70 | 2018-09-13 21:35:32 +0000 | [diff] [blame] | 47 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 48 | SBStructuredData::~SBStructuredData() {} |
| 49 | |
| 50 | SBStructuredData &SBStructuredData:: |
| 51 | operator=(const lldb::SBStructuredData &rhs) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 52 | LLDB_RECORD_METHOD( |
| 53 | lldb::SBStructuredData &, |
| 54 | SBStructuredData, operator=,(const lldb::SBStructuredData &), rhs); |
| 55 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | *m_impl_up = *rhs.m_impl_up; |
| 57 | return *this; |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Ravitheja Addepally | d5d8d91 | 2017-04-26 08:48:50 +0000 | [diff] [blame] | 60 | lldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream &stream) { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 61 | LLDB_RECORD_METHOD(lldb::SBError, SBStructuredData, SetFromJSON, |
| 62 | (lldb::SBStream &), stream); |
| 63 | |
Ravitheja Addepally | d5d8d91 | 2017-04-26 08:48:50 +0000 | [diff] [blame] | 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 | |
Abhishek Aggarwal | 5bfee5f | 2017-05-29 08:25:46 +0000 | [diff] [blame] | 70 | if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary) |
Ravitheja Addepally | d5d8d91 | 2017-04-26 08:48:50 +0000 | [diff] [blame] | 71 | error.SetErrorString("Invalid Syntax"); |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 72 | return LLDB_RECORD_RESULT(error); |
Ravitheja Addepally | d5d8d91 | 2017-04-26 08:48:50 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 75 | bool SBStructuredData::IsValid() const { |
| 76 | LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStructuredData, IsValid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 77 | |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 78 | return m_impl_up->IsValid(); |
| 79 | } |
| 80 | |
| 81 | void SBStructuredData::Clear() { |
| 82 | LLDB_RECORD_METHOD_NO_ARGS(void, SBStructuredData, Clear); |
| 83 | |
| 84 | m_impl_up->Clear(); |
| 85 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 86 | |
| 87 | SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 88 | LLDB_RECORD_METHOD_CONST(lldb::SBError, SBStructuredData, GetAsJSON, |
| 89 | (lldb::SBStream &), stream); |
| 90 | |
Ravitheja Addepally | d5d8d91 | 2017-04-26 08:48:50 +0000 | [diff] [blame] | 91 | SBError error; |
| 92 | error.SetError(m_impl_up->GetAsJSON(stream.ref())); |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 93 | return LLDB_RECORD_RESULT(error); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 97 | LLDB_RECORD_METHOD_CONST(lldb::SBError, SBStructuredData, GetDescription, |
| 98 | (lldb::SBStream &), stream); |
| 99 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 100 | Status error = m_impl_up->GetDescription(stream.ref()); |
Todd Fiala | 2ef442c | 2016-11-09 23:21:04 +0000 | [diff] [blame] | 101 | SBError sb_error; |
| 102 | sb_error.SetError(error); |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 103 | return LLDB_RECORD_RESULT(sb_error); |
Todd Fiala | 7593001 | 2016-08-19 04:21:48 +0000 | [diff] [blame] | 104 | } |
Abhishek Aggarwal | 5bfee5f | 2017-05-29 08:25:46 +0000 | [diff] [blame] | 105 | |
| 106 | StructuredDataType SBStructuredData::GetType() const { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 107 | LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::StructuredDataType, SBStructuredData, |
| 108 | GetType); |
| 109 | |
Abhishek Aggarwal | 5bfee5f | 2017-05-29 08:25:46 +0000 | [diff] [blame] | 110 | return (m_impl_up ? m_impl_up->GetType() : eStructuredDataTypeInvalid); |
| 111 | } |
| 112 | |
| 113 | size_t SBStructuredData::GetSize() const { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 114 | LLDB_RECORD_METHOD_CONST_NO_ARGS(size_t, SBStructuredData, GetSize); |
| 115 | |
Abhishek Aggarwal | 5bfee5f | 2017-05-29 08:25:46 +0000 | [diff] [blame] | 116 | return (m_impl_up ? m_impl_up->GetSize() : 0); |
| 117 | } |
| 118 | |
Jim Ingham | 3815e70 | 2018-09-13 21:35:32 +0000 | [diff] [blame] | 119 | bool SBStructuredData::GetKeys(lldb::SBStringList &keys) const { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 120 | LLDB_RECORD_METHOD_CONST(bool, SBStructuredData, GetKeys, |
| 121 | (lldb::SBStringList &), keys); |
| 122 | |
Jim Ingham | 3815e70 | 2018-09-13 21:35:32 +0000 | [diff] [blame] | 123 | if (!m_impl_up) |
| 124 | return false; |
| 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 | |
Abhishek Aggarwal | 5bfee5f | 2017-05-29 08:25:46 +0000 | [diff] [blame] | 149 | lldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 150 | LLDB_RECORD_METHOD_CONST(lldb::SBStructuredData, SBStructuredData, |
| 151 | GetValueForKey, (const char *), key); |
| 152 | |
Abhishek Aggarwal | 5bfee5f | 2017-05-29 08:25:46 +0000 | [diff] [blame] | 153 | if (!m_impl_up) |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 154 | return LLDB_RECORD_RESULT(SBStructuredData()); |
Abhishek Aggarwal | 5bfee5f | 2017-05-29 08:25:46 +0000 | [diff] [blame] | 155 | |
| 156 | SBStructuredData result; |
| 157 | result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key)); |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 158 | return LLDB_RECORD_RESULT(result); |
Abhishek Aggarwal | 5bfee5f | 2017-05-29 08:25:46 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | lldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 162 | LLDB_RECORD_METHOD_CONST(lldb::SBStructuredData, SBStructuredData, |
| 163 | GetItemAtIndex, (size_t), idx); |
| 164 | |
Abhishek Aggarwal | 5bfee5f | 2017-05-29 08:25:46 +0000 | [diff] [blame] | 165 | if (!m_impl_up) |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 166 | return LLDB_RECORD_RESULT(SBStructuredData()); |
Abhishek Aggarwal | 5bfee5f | 2017-05-29 08:25:46 +0000 | [diff] [blame] | 167 | |
| 168 | SBStructuredData result; |
| 169 | result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx)); |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 170 | return LLDB_RECORD_RESULT(result); |
Abhishek Aggarwal | 5bfee5f | 2017-05-29 08:25:46 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 174 | LLDB_RECORD_METHOD_CONST(uint64_t, SBStructuredData, GetIntegerValue, |
| 175 | (uint64_t), fail_value); |
| 176 | |
Abhishek Aggarwal | 5bfee5f | 2017-05-29 08:25:46 +0000 | [diff] [blame] | 177 | return (m_impl_up ? m_impl_up->GetIntegerValue(fail_value) : fail_value); |
| 178 | } |
| 179 | |
| 180 | double SBStructuredData::GetFloatValue(double fail_value) const { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 181 | LLDB_RECORD_METHOD_CONST(double, SBStructuredData, GetFloatValue, (double), |
| 182 | fail_value); |
| 183 | |
Abhishek Aggarwal | 5bfee5f | 2017-05-29 08:25:46 +0000 | [diff] [blame] | 184 | return (m_impl_up ? m_impl_up->GetFloatValue(fail_value) : fail_value); |
| 185 | } |
| 186 | |
| 187 | bool SBStructuredData::GetBooleanValue(bool fail_value) const { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 188 | LLDB_RECORD_METHOD_CONST(bool, SBStructuredData, GetBooleanValue, (bool), |
| 189 | fail_value); |
| 190 | |
Abhishek Aggarwal | 5bfee5f | 2017-05-29 08:25:46 +0000 | [diff] [blame] | 191 | return (m_impl_up ? m_impl_up->GetBooleanValue(fail_value) : fail_value); |
| 192 | } |
| 193 | |
| 194 | size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const { |
Jonas Devlieghere | baf5664 | 2019-03-06 00:06:00 +0000 | [diff] [blame] | 195 | LLDB_RECORD_METHOD_CONST(size_t, SBStructuredData, GetStringValue, |
| 196 | (char *, size_t), dst, dst_len); |
| 197 | |
Abhishek Aggarwal | 5bfee5f | 2017-05-29 08:25:46 +0000 | [diff] [blame] | 198 | return (m_impl_up ? m_impl_up->GetStringValue(dst, dst_len) : 0); |
| 199 | } |