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  (const char *), json);
79  s.Print(json);
81 }
82 
85  return this->operator bool();
86 }
87 
88 SBStructuredData::operator bool() const {
90 
91  return m_impl_up->IsValid();
92 }
93 
96 
97  m_impl_up->Clear();
98 }
99 
102  (lldb::SBStream &), stream);
103 
104  SBError error;
105  error.SetError(m_impl_up->GetAsJSON(stream.ref()));
106  return LLDB_RECORD_RESULT(error);
107 }
108 
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 LLDB_RECORD_RESULT(sb_error);
117 }
118 
121  GetType);
122 
123  return m_impl_up->GetType();
124 }
125 
128 
129  return m_impl_up->GetSize();
130 }
131 
134  (lldb::SBStringList &), keys);
135 
137  return false;
138 
139  StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP();
140  if (!obj_sp)
141  return false;
142 
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 
161  GetValueForKey, (const char *), key);
162 
163  SBStructuredData result;
164  result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key));
165  return LLDB_RECORD_RESULT(result);
166 }
167 
170  GetItemAtIndex, (size_t), idx);
171 
172  SBStructuredData result;
173  result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx));
174  return LLDB_RECORD_RESULT(result);
175 }
176 
177 uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const {
179  (uint64_t), fail_value);
180 
181  return m_impl_up->GetIntegerValue(fail_value);
182 }
183 
184 double SBStructuredData::GetFloatValue(double fail_value) const {
186  fail_value);
187 
188  return m_impl_up->GetFloatValue(fail_value);
189 }
190 
191 bool SBStructuredData::GetBooleanValue(bool fail_value) const {
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 {
200  (char *, size_t), dst, "", dst_len);
201 
202  return m_impl_up->GetStringValue(dst, dst_len);
203 }
204 
205 namespace lldb_private {
206 namespace repro {
207 
211  LLDB_REGISTER_CONSTRUCTOR(SBStructuredData, (const lldb::EventSP &));
216  SBStructuredData, operator=,(const lldb::SBStructuredData &));
218  (lldb::SBStream &));
220  (const char *));
221  LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, IsValid, ());
222  LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, operator bool, ());
223  LLDB_REGISTER_METHOD(void, SBStructuredData, Clear, ());
225  (lldb::SBStream &));
227  (lldb::SBStream &));
229  GetType, ());
230  LLDB_REGISTER_METHOD_CONST(size_t, SBStructuredData, GetSize, ());
232  (lldb::SBStringList &));
234  GetValueForKey, (const char *));
236  GetItemAtIndex, (size_t));
237  LLDB_REGISTER_METHOD_CONST(uint64_t, SBStructuredData, GetIntegerValue,
238  (uint64_t));
239  LLDB_REGISTER_METHOD_CONST(double, SBStructuredData, GetFloatValue, (double));
240  LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, GetBooleanValue, (bool));
241  LLDB_REGISTER_CHAR_PTR_METHOD_CONST(size_t, SBStructuredData, GetStringValue);
242 }
243 
244 } // namespace repro
245 } // namespace lldb_private
lldb::SBStringList
Definition: SBStringList.h:16
lldb::StructuredDataType
StructuredDataType
Definition: lldb-enumerations.h:785
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:184
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:109
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:794
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
lldb::SBStream::Print
void void Print(const char *str)
Definition: SBStream.cpp:63
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:168
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:104
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:126
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:100
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:177
StructuredDataPlugin.h
lldb_private::StructuredDataImpl
Definition: StructuredDataImpl.h:26
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
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:132
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:159
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:191
lldb::SBStructuredData::~SBStructuredData
~SBStructuredData()
lldb::SBStructuredData::GetType
lldb::StructuredDataType GetType() const
Return the type of data in this data structure.
Definition: SBStructuredData.cpp:119
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:198
lldb_private::StructuredData::Object
Definition: StructuredData.h:70
lldb
Definition: SBAddress.h:15
lldb::SBStructuredData::Clear
void Clear()
Definition: SBStructuredData.cpp:94
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:208
LLDB_REGISTER_METHOD
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:93
lldb::SBStructuredData::IsValid
bool IsValid() const
Definition: SBStructuredData.cpp:83