blob: 9c06015b230ff123b8be23ca6b462e77f250cfc0 [file] [log] [blame]
Todd Fiala75930012016-08-19 04:21:48 +00001//===-- SBStructuredData.cpp ------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Fiala75930012016-08-19 04:21:48 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/API/SBStructuredData.h"
Jonas Devliegherebaf56642019-03-06 00:06:00 +000010#include "SBReproducerPrivate.h"
Todd Fiala75930012016-08-19 04:21:48 +000011
12#include "lldb/API/SBStream.h"
Jim Ingham3815e702018-09-13 21:35:32 +000013#include "lldb/API/SBStringList.h"
Ravitheja Addepallyd5d8d912017-04-26 08:48:50 +000014#include "lldb/Core/StructuredDataImpl.h"
Todd Fiala75930012016-08-19 04:21:48 +000015#include "lldb/Target/StructuredDataPlugin.h"
Pavel Labath181b8232018-12-14 15:59:49 +000016#include "lldb/Utility/Event.h"
Zachary Turner97206d52017-05-12 04:51:55 +000017#include "lldb/Utility/Status.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000018#include "lldb/Utility/Stream.h"
Pavel Labathf2a8bcc2017-06-27 10:45:31 +000019#include "lldb/Utility/StructuredData.h"
Todd Fiala75930012016-08-19 04:21:48 +000020
21using namespace lldb;
22using namespace lldb_private;
23
Kate Stoneb9c1b512016-09-06 20:57:50 +000024#pragma mark--
Todd Fiala75930012016-08-19 04:21:48 +000025#pragma mark SBStructuredData
26
Jonas Devliegherebaf56642019-03-06 00:06:00 +000027SBStructuredData::SBStructuredData() : m_impl_up(new StructuredDataImpl()) {
28 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBStructuredData);
29}
Todd Fiala75930012016-08-19 04:21:48 +000030
Kate Stoneb9c1b512016-09-06 20:57:50 +000031SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs)
Jonas Devliegherebaf56642019-03-06 00:06:00 +000032 : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up.get())) {
33 LLDB_RECORD_CONSTRUCTOR(SBStructuredData, (const lldb::SBStructuredData &),
34 rhs);
35}
Kate Stoneb9c1b512016-09-06 20:57:50 +000036
37SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp)
Jonas Devliegherebaf56642019-03-06 00:06:00 +000038 : m_impl_up(new StructuredDataImpl(event_sp)) {
39 LLDB_RECORD_CONSTRUCTOR(SBStructuredData, (const lldb::EventSP &), event_sp);
40}
Kate Stoneb9c1b512016-09-06 20:57:50 +000041
Jim Ingham3815e702018-09-13 21:35:32 +000042SBStructuredData::SBStructuredData(lldb_private::StructuredDataImpl *impl)
Jonas Devliegherebaf56642019-03-06 00:06:00 +000043 : m_impl_up(impl) {
44 LLDB_RECORD_CONSTRUCTOR(SBStructuredData,
45 (lldb_private::StructuredDataImpl *), impl);
46}
Jim Ingham3815e702018-09-13 21:35:32 +000047
Kate Stoneb9c1b512016-09-06 20:57:50 +000048SBStructuredData::~SBStructuredData() {}
49
50SBStructuredData &SBStructuredData::
51operator=(const lldb::SBStructuredData &rhs) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000052 LLDB_RECORD_METHOD(
53 lldb::SBStructuredData &,
54 SBStructuredData, operator=,(const lldb::SBStructuredData &), rhs);
55
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 *m_impl_up = *rhs.m_impl_up;
57 return *this;
Todd Fiala75930012016-08-19 04:21:48 +000058}
59
Ravitheja Addepallyd5d8d912017-04-26 08:48:50 +000060lldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream &stream) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000061 LLDB_RECORD_METHOD(lldb::SBError, SBStructuredData, SetFromJSON,
62 (lldb::SBStream &), stream);
63
Ravitheja Addepallyd5d8d912017-04-26 08:48:50 +000064 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 Aggarwal5bfee5f2017-05-29 08:25:46 +000070 if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary)
Ravitheja Addepallyd5d8d912017-04-26 08:48:50 +000071 error.SetErrorString("Invalid Syntax");
Jonas Devliegherebaf56642019-03-06 00:06:00 +000072 return LLDB_RECORD_RESULT(error);
Ravitheja Addepallyd5d8d912017-04-26 08:48:50 +000073}
74
Jonas Devliegherebaf56642019-03-06 00:06:00 +000075bool SBStructuredData::IsValid() const {
76 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStructuredData, IsValid);
Kate Stoneb9c1b512016-09-06 20:57:50 +000077
Jonas Devliegherebaf56642019-03-06 00:06:00 +000078 return m_impl_up->IsValid();
79}
80
81void SBStructuredData::Clear() {
82 LLDB_RECORD_METHOD_NO_ARGS(void, SBStructuredData, Clear);
83
84 m_impl_up->Clear();
85}
Kate Stoneb9c1b512016-09-06 20:57:50 +000086
87SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000088 LLDB_RECORD_METHOD_CONST(lldb::SBError, SBStructuredData, GetAsJSON,
89 (lldb::SBStream &), stream);
90
Ravitheja Addepallyd5d8d912017-04-26 08:48:50 +000091 SBError error;
92 error.SetError(m_impl_up->GetAsJSON(stream.ref()));
Jonas Devliegherebaf56642019-03-06 00:06:00 +000093 return LLDB_RECORD_RESULT(error);
Todd Fiala75930012016-08-19 04:21:48 +000094}
95
Kate Stoneb9c1b512016-09-06 20:57:50 +000096lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000097 LLDB_RECORD_METHOD_CONST(lldb::SBError, SBStructuredData, GetDescription,
98 (lldb::SBStream &), stream);
99
Zachary Turner97206d52017-05-12 04:51:55 +0000100 Status error = m_impl_up->GetDescription(stream.ref());
Todd Fiala2ef442c2016-11-09 23:21:04 +0000101 SBError sb_error;
102 sb_error.SetError(error);
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000103 return LLDB_RECORD_RESULT(sb_error);
Todd Fiala75930012016-08-19 04:21:48 +0000104}
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000105
106StructuredDataType SBStructuredData::GetType() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000107 LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::StructuredDataType, SBStructuredData,
108 GetType);
109
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000110 return (m_impl_up ? m_impl_up->GetType() : eStructuredDataTypeInvalid);
111}
112
113size_t SBStructuredData::GetSize() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000114 LLDB_RECORD_METHOD_CONST_NO_ARGS(size_t, SBStructuredData, GetSize);
115
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000116 return (m_impl_up ? m_impl_up->GetSize() : 0);
117}
118
Jim Ingham3815e702018-09-13 21:35:32 +0000119bool SBStructuredData::GetKeys(lldb::SBStringList &keys) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000120 LLDB_RECORD_METHOD_CONST(bool, SBStructuredData, GetKeys,
121 (lldb::SBStringList &), keys);
122
Jim Ingham3815e702018-09-13 21:35:32 +0000123 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 Aggarwal5bfee5f2017-05-29 08:25:46 +0000149lldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000150 LLDB_RECORD_METHOD_CONST(lldb::SBStructuredData, SBStructuredData,
151 GetValueForKey, (const char *), key);
152
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000153 if (!m_impl_up)
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000154 return LLDB_RECORD_RESULT(SBStructuredData());
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000155
156 SBStructuredData result;
157 result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key));
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000158 return LLDB_RECORD_RESULT(result);
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000159}
160
161lldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000162 LLDB_RECORD_METHOD_CONST(lldb::SBStructuredData, SBStructuredData,
163 GetItemAtIndex, (size_t), idx);
164
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000165 if (!m_impl_up)
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000166 return LLDB_RECORD_RESULT(SBStructuredData());
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000167
168 SBStructuredData result;
169 result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx));
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000170 return LLDB_RECORD_RESULT(result);
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000171}
172
173uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000174 LLDB_RECORD_METHOD_CONST(uint64_t, SBStructuredData, GetIntegerValue,
175 (uint64_t), fail_value);
176
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000177 return (m_impl_up ? m_impl_up->GetIntegerValue(fail_value) : fail_value);
178}
179
180double SBStructuredData::GetFloatValue(double fail_value) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000181 LLDB_RECORD_METHOD_CONST(double, SBStructuredData, GetFloatValue, (double),
182 fail_value);
183
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000184 return (m_impl_up ? m_impl_up->GetFloatValue(fail_value) : fail_value);
185}
186
187bool SBStructuredData::GetBooleanValue(bool fail_value) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000188 LLDB_RECORD_METHOD_CONST(bool, SBStructuredData, GetBooleanValue, (bool),
189 fail_value);
190
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000191 return (m_impl_up ? m_impl_up->GetBooleanValue(fail_value) : fail_value);
192}
193
194size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000195 LLDB_RECORD_METHOD_CONST(size_t, SBStructuredData, GetStringValue,
196 (char *, size_t), dst, dst_len);
197
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000198 return (m_impl_up ? m_impl_up->GetStringValue(dst, dst_len) : 0);
199}