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 
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 
28  LLDB_INSTRUMENT_VA(this);
29 }
30 
32  : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up)) {
33  LLDB_INSTRUMENT_VA(this, rhs);
34 }
35 
36 SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp)
37  : m_impl_up(new StructuredDataImpl(event_sp)) {
38  LLDB_INSTRUMENT_VA(this, event_sp);
39 }
40 
42  : m_impl_up(new StructuredDataImpl(impl)) {
43  LLDB_INSTRUMENT_VA(this, impl);
44 }
45 
47 
50  LLDB_INSTRUMENT_VA(this, rhs);
51 
52  *m_impl_up = *rhs.m_impl_up;
53  return *this;
54 }
55 
57  LLDB_INSTRUMENT_VA(this, stream);
58 
60  std::string json_str(stream.GetData());
61 
62  StructuredData::ObjectSP json_obj = StructuredData::ParseJSON(json_str);
63  m_impl_up->SetObjectSP(json_obj);
64 
65  if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary)
66  error.SetErrorString("Invalid Syntax");
67  return error;
68 }
69 
71  LLDB_INSTRUMENT_VA(this, json);
73  s.Print(json);
74  return SetFromJSON(s);
75 }
76 
78  LLDB_INSTRUMENT_VA(this);
79  return this->operator bool();
80 }
81 
82 SBStructuredData::operator bool() const {
83  LLDB_INSTRUMENT_VA(this);
84 
85  return m_impl_up->IsValid();
86 }
87 
89  LLDB_INSTRUMENT_VA(this);
90 
91  m_impl_up->Clear();
92 }
93 
95  LLDB_INSTRUMENT_VA(this, stream);
96 
97  SBError error;
98  error.SetError(m_impl_up->GetAsJSON(stream.ref()));
99  return error;
100 }
101 
103  LLDB_INSTRUMENT_VA(this, stream);
104 
105  Status error = m_impl_up->GetDescription(stream.ref());
106  SBError sb_error;
107  sb_error.SetError(error);
108  return sb_error;
109 }
110 
112  LLDB_INSTRUMENT_VA(this);
113 
114  return m_impl_up->GetType();
115 }
116 
118  LLDB_INSTRUMENT_VA(this);
119 
120  return m_impl_up->GetSize();
121 }
122 
124  LLDB_INSTRUMENT_VA(this, keys);
125 
127  return false;
128 
129  StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP();
130  if (!obj_sp)
131  return false;
132 
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 
150  LLDB_INSTRUMENT_VA(this, key);
151 
152  SBStructuredData result;
153  result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key));
154  return result;
155 }
156 
158  LLDB_INSTRUMENT_VA(this, idx);
159 
160  SBStructuredData result;
161  result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx));
162  return result;
163 }
164 
165 uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const {
166  LLDB_INSTRUMENT_VA(this, fail_value);
167 
168  return m_impl_up->GetIntegerValue(fail_value);
169 }
170 
171 double SBStructuredData::GetFloatValue(double fail_value) const {
172  LLDB_INSTRUMENT_VA(this, fail_value);
173 
174  return m_impl_up->GetFloatValue(fail_value);
175 }
176 
177 bool SBStructuredData::GetBooleanValue(bool fail_value) const {
178  LLDB_INSTRUMENT_VA(this, fail_value);
179 
180  return m_impl_up->GetBooleanValue(fail_value);
181 }
182 
183 size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const {
184  LLDB_INSTRUMENT_VA(this, dst, dst_len);
185 
186  return m_impl_up->GetStringValue(dst, dst_len);
187 }
lldb::SBStringList
Definition: SBStringList.h:16
Instrumentation.h
lldb::StructuredDataType
StructuredDataType
Definition: lldb-enumerations.h:786
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:171
lldb::SBStream::ref
lldb_private::Stream & ref()
Definition: SBStream.cpp:176
lldb_private::StructuredData::Dictionary
Definition: StructuredData.h:352
lldb::SBStringList::AppendString
void AppendString(const char *str)
Definition: SBStringList.cpp:58
lldb::SBStructuredData::GetDescription
lldb::SBError GetDescription(lldb::SBStream &stream) const
Definition: SBStructuredData.cpp:102
lldb_private::StructuredData::Array
Definition: StructuredData.h:165
StructuredData.h
lldb::SBError
Definition: SBError.h:20
lldb::eStructuredDataTypeDictionary
@ eStructuredDataTypeDictionary
Definition: lldb-enumerations.h:795
SBStringList.h
lldb::SBStructuredData::SBStructuredData
SBStructuredData()
Definition: SBStructuredData.cpp:27
lldb::SBStream::Print
void void Print(const char *str)
Definition: SBStream.cpp:64
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:157
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:117
lldb::SBStructuredData::GetAsJSON
lldb::SBError GetAsJSON(lldb::SBStream &stream) const
Definition: SBStructuredData.cpp:94
lldb_private::StructuredData::ObjectSP
std::shared_ptr< Object > ObjectSP
Definition: StructuredData.h:59
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:165
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:95
lldb::SBStructuredData::operator=
lldb::SBStructuredData & operator=(const lldb::SBStructuredData &rhs)
Definition: SBStructuredData.cpp:49
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_private::StructuredData::Dictionary::GetKeys
ArraySP GetKeys() const
Definition: StructuredData.h:378
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:149
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
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:177
lldb::SBStructuredData::~SBStructuredData
~SBStructuredData()
lldb::SBStructuredData::GetType
lldb::StructuredDataType GetType() const
Return the type of data in this data structure.
Definition: SBStructuredData.cpp:111
LLDB_INSTRUMENT_VA
#define LLDB_INSTRUMENT_VA(...)
Definition: Instrumentation.h:98
lldb::SBStructuredData::SetFromJSON
lldb::SBError SetFromJSON(lldb::SBStream &stream)
Definition: SBStructuredData.cpp:56
Stream.h
lldb::SBStream
Definition: SBStream.h:18
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:183
lldb_private::StructuredData::Object
Definition: StructuredData.h:70
lldb
Definition: SBAddress.h:15
lldb::SBStructuredData::Clear
void Clear()
Definition: SBStructuredData.cpp:88
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:44
lldb::SBStructuredData::IsValid
bool IsValid() const
Definition: SBStructuredData.cpp:77