LLDB  mainline
SBStructuredData.cpp
Go to the documentation of this file.
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 
10 #include "SBReproducerPrivate.h"
11 
12 #include "lldb/API/SBStream.h"
13 #include "lldb/API/SBStringList.h"
16 #include "lldb/Utility/Event.h"
17 #include "lldb/Utility/Status.h"
18 #include "lldb/Utility/Stream.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 #pragma mark--
25 #pragma mark SBStructuredData
26 
29 }
30 
32  : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up)) {
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 
43  : m_impl_up(impl ? impl : new StructuredDataImpl()) {
46 }
47 
49 
54  SBStructuredData, operator=,(const lldb::SBStructuredData &), rhs);
55 
56  *m_impl_up = *rhs.m_impl_up;
57  return LLDB_RECORD_RESULT(*this);
58 }
59 
62  (lldb::SBStream &), stream);
63 
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 
77  return this->operator bool();
78 }
79 SBStructuredData::operator bool() const {
81 
82  return m_impl_up->IsValid();
83 }
84 
87 
88  m_impl_up->Clear();
89 }
90 
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 
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 
112  GetType);
113 
114  return m_impl_up->GetType();
115 }
116 
119 
120  return m_impl_up->GetSize();
121 }
122 
125  (lldb::SBStringList &), keys);
126 
128  return false;
129 
130  StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP();
131  if (!obj_sp)
132  return false;
133 
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 
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 
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 {
170  (uint64_t), fail_value);
171 
172  return m_impl_up->GetIntegerValue(fail_value);
173 }
174 
175 double SBStructuredData::GetFloatValue(double fail_value) const {
177  fail_value);
178 
179  return m_impl_up->GetFloatValue(fail_value);
180 }
181 
182 bool SBStructuredData::GetBooleanValue(bool fail_value) const {
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 {
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 
202  LLDB_REGISTER_CONSTRUCTOR(SBStructuredData, (const lldb::EventSP &));
207  SBStructuredData, operator=,(const lldb::SBStructuredData &));
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, ());
214  (lldb::SBStream &));
216  (lldb::SBStream &));
218  GetType, ());
219  LLDB_REGISTER_METHOD_CONST(size_t, SBStructuredData, GetSize, ());
221  (lldb::SBStringList &));
223  GetValueForKey, (const char *));
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
lldb::SBStringList
Definition: SBStringList.h:16
lldb::StructuredDataType
StructuredDataType
Definition: lldb-enumerations.h:784
lldb::SBStructuredData::GetFloatValue
double GetFloatValue(double fail_value=0.0) const
Return the floating point value if this data structure is a floating type.
Definition: SBStructuredData.cpp:175
lldb::SBStream::ref
lldb_private::Stream & ref()
Definition: SBStream.cpp:178
lldb_private::StructuredData::Dictionary
Definition: StructuredData.h:352
lldb::SBStringList::AppendString
void AppendString(const char *str)
Definition: SBStringList.cpp:62
lldb::SBStructuredData::GetDescription
lldb::SBError GetDescription(lldb::SBStream &stream) const
Definition: SBStructuredData.cpp:100
lldb_private::StructuredData::Array
Definition: StructuredData.h:165
StructuredData.h
LLDB_RECORD_RESULT
#define LLDB_RECORD_RESULT(Result)
Definition: ReproducerInstrumentation.h:211
LLDB_RECORD_METHOD_CONST_NO_ARGS
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:168
lldb::SBError
Definition: SBError.h:20
lldb::eStructuredDataTypeDictionary
@ eStructuredDataTypeDictionary
Definition: lldb-enumerations.h:793
SBStringList.h
LLDB_RECORD_CHAR_PTR_METHOD_CONST
#define LLDB_RECORD_CHAR_PTR_METHOD_CONST(Result, Class, Method, Signature, StrOut,...)
Definition: ReproducerInstrumentation.h:201
LLDB_RECORD_METHOD_NO_ARGS
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:165
lldb::SBStructuredData::SBStructuredData
SBStructuredData()
Definition: SBStructuredData.cpp:27
LLDB_RECORD_CONSTRUCTOR_NO_ARGS
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
Definition: ReproducerInstrumentation.h:136
LLDB_REGISTER_METHOD_CONST
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:98
SBReproducerPrivate.h
lldb::SBStructuredData::GetItemAtIndex
lldb::SBStructuredData GetItemAtIndex(size_t idx) const
Return the value corresponding to an index if this data structure is array.
Definition: SBStructuredData.cpp:159
LLDB_REGISTER_CONSTRUCTOR
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
Definition: ReproducerInstrumentation.h:89
lldb::SBStructuredData::m_impl_up
StructuredDataImplUP m_impl_up
Definition: SBStructuredData.h:102
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
SBStream.h
lldb::SBStructuredData::GetSize
size_t GetSize() const
Return the size (i.e.
Definition: SBStructuredData.cpp:117
LLDB_RECORD_METHOD
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
Definition: ReproducerInstrumentation.h:158
lldb::SBStructuredData::GetAsJSON
lldb::SBError GetAsJSON(lldb::SBStream &stream) const
Definition: SBStructuredData.cpp:91
lldb_private::StructuredData::ObjectSP
std::shared_ptr< Object > ObjectSP
Definition: StructuredData.h:59
lldb_private::StructuredData::Dictionary::GetKeys
ObjectSP GetKeys() const
Definition: StructuredData.h:368
lldb::SBStructuredData::GetIntegerValue
uint64_t GetIntegerValue(uint64_t fail_value=0) const
Return the integer value if this data structure is an integer type.
Definition: SBStructuredData.cpp:168
StructuredDataPlugin.h
lldb_private::StructuredDataImpl
Definition: StructuredDataImpl.h:26
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
lldb::SBError::SetError
void SetError(uint32_t err, lldb::ErrorType type)
Definition: SBError.cpp:97
lldb::SBStructuredData::operator=
lldb::SBStructuredData & operator=(const lldb::SBStructuredData &rhs)
Definition: SBStructuredData.cpp:51
lldb::SBStructuredData::GetKeys
bool GetKeys(lldb::SBStringList &keys) const
Fill keys with the keys in this object and return true if this data structure is a dictionary.
Definition: SBStructuredData.cpp:123
Event.h
LLDB_RECORD_CONSTRUCTOR
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
Definition: ReproducerInstrumentation.h:133
lldb_private::repro::Registry
The registry contains a unique mapping between functions and their ID.
Definition: ReproducerInstrumentation.h:517
lldb::SBStructuredData::GetValueForKey
lldb::SBStructuredData GetValueForKey(const char *key) const
Return the value corresponding to a key if this data structure is a dictionary type.
Definition: SBStructuredData.cpp:150
lldb_private::StructuredData::Object::GetAsArray
Array * GetAsArray()
Definition: StructuredData.h:85
lldb_private::Status
Definition: Status.h:44
lldb::SBStructuredData
Definition: SBStructuredData.h:17
lldb_private::StructuredData::Object::GetAsDictionary
Dictionary * GetAsDictionary()
Definition: StructuredData.h:91
LLDB_REGISTER_CHAR_PTR_METHOD_CONST
#define LLDB_REGISTER_CHAR_PTR_METHOD_CONST(Result, Class, Method)
Definition: ReproducerInstrumentation.h:121
SBStructuredData.h
StructuredDataImpl.h
Status.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb::SBStructuredData::GetBooleanValue
bool GetBooleanValue(bool fail_value=false) const
Return the boolean value if this data structure is a boolean type.
Definition: SBStructuredData.cpp:182
lldb::SBStructuredData::~SBStructuredData
~SBStructuredData()
lldb::SBStructuredData::GetType
lldb::StructuredDataType GetType() const
Return the type of data in this data structure.
Definition: SBStructuredData.cpp:110
lldb::SBStructuredData::SetFromJSON
lldb::SBError SetFromJSON(lldb::SBStream &stream)
Definition: SBStructuredData.cpp:60
Stream.h
lldb::SBStream
Definition: SBStream.h:18
LLDB_RECORD_METHOD_CONST
#define LLDB_RECORD_METHOD_CONST(Result, Class, Method, Signature,...)
Definition: ReproducerInstrumentation.h:161
lldb::SBStructuredData::GetStringValue
size_t GetStringValue(char *dst, size_t dst_len) const
Provides the string value if this data structure is a string type.
Definition: SBStructuredData.cpp:189
lldb_private::StructuredData::Object
Definition: StructuredData.h:70
lldb
Definition: SBAddress.h:15
lldb::SBStructuredData::Clear
void Clear()
Definition: SBStructuredData.cpp:85
lldb_private::StructuredData::Array::ForEach
bool ForEach(std::function< bool(Object *object)> const &foreach_callback) const
Definition: StructuredData.h:172
lldb::SBStream::GetData
const char * GetData()
Definition: SBStream.cpp:43
lldb_private::repro::RegisterMethods< SBStructuredData >
void RegisterMethods< SBStructuredData >(Registry &R)
Definition: SBStructuredData.cpp:199
LLDB_REGISTER_METHOD
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:93
lldb::SBStructuredData::IsValid
bool IsValid() const
Definition: SBStructuredData.cpp:75