File indexing completed on 2026-05-10 08:42:46
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLDB_CORE_STRUCTUREDDATAIMPL_H
0010 #define LLDB_CORE_STRUCTUREDDATAIMPL_H
0011
0012 #include "lldb/Target/StructuredDataPlugin.h"
0013 #include "lldb/Utility/Event.h"
0014 #include "lldb/Utility/Status.h"
0015 #include "lldb/Utility/Stream.h"
0016 #include "lldb/Utility/StructuredData.h"
0017 #include "lldb/lldb-enumerations.h"
0018 #include "lldb/lldb-forward.h"
0019 #include "llvm/ADT/StringRef.h"
0020
0021 #pragma mark--
0022 #pragma mark StructuredDataImpl
0023
0024 namespace lldb_private {
0025
0026 class StructuredDataImpl {
0027 public:
0028 StructuredDataImpl() = default;
0029
0030 StructuredDataImpl(const StructuredDataImpl &rhs) = default;
0031
0032 StructuredDataImpl(StructuredData::ObjectSP obj)
0033 : m_data_sp(std::move(obj)) {}
0034
0035 StructuredDataImpl(const lldb::EventSP &event_sp)
0036 : m_plugin_wp(
0037 EventDataStructuredData::GetPluginFromEvent(event_sp.get())),
0038 m_data_sp(EventDataStructuredData::GetObjectFromEvent(event_sp.get())) {
0039 }
0040
0041 ~StructuredDataImpl() = default;
0042
0043 StructuredDataImpl &operator=(const StructuredDataImpl &rhs) = default;
0044
0045 bool IsValid() const { return m_data_sp.get() != nullptr; }
0046
0047 void Clear() {
0048 m_plugin_wp.reset();
0049 m_data_sp.reset();
0050 }
0051
0052 Status GetAsJSON(Stream &stream) const {
0053 if (!m_data_sp)
0054 return Status::FromErrorString("No structured data.");
0055
0056 llvm::json::OStream s(stream.AsRawOstream());
0057 m_data_sp->Serialize(s);
0058 return Status();
0059 }
0060
0061 Status GetDescription(Stream &stream) const {
0062 if (!m_data_sp)
0063 return Status::FromErrorString("Cannot pretty print structured data: "
0064 "no data to print.");
0065
0066
0067 lldb::StructuredDataPluginSP plugin_sp = m_plugin_wp.lock();
0068
0069
0070 if (!plugin_sp) {
0071 if (!m_data_sp)
0072 return Status::FromErrorString("No data to describe.");
0073 m_data_sp->GetDescription(stream);
0074 return Status();
0075 }
0076
0077 return plugin_sp->GetDescription(m_data_sp, stream);
0078 }
0079
0080 StructuredData::ObjectSP GetObjectSP() { return m_data_sp; }
0081
0082 void SetObjectSP(const StructuredData::ObjectSP &obj) { m_data_sp = obj; }
0083
0084 lldb::StructuredDataType GetType() const {
0085 return (m_data_sp ? m_data_sp->GetType() :
0086 lldb::eStructuredDataTypeInvalid);
0087 }
0088
0089 size_t GetSize() const {
0090 if (!m_data_sp)
0091 return 0;
0092
0093 if (m_data_sp->GetType() == lldb::eStructuredDataTypeDictionary) {
0094 auto dict = m_data_sp->GetAsDictionary();
0095 return (dict->GetSize());
0096 } else if (m_data_sp->GetType() == lldb::eStructuredDataTypeArray) {
0097 auto array = m_data_sp->GetAsArray();
0098 return (array->GetSize());
0099 } else
0100 return 0;
0101 }
0102
0103 StructuredData::ObjectSP GetValueForKey(const char *key) const {
0104 if (m_data_sp) {
0105 auto dict = m_data_sp->GetAsDictionary();
0106 if (dict)
0107 return dict->GetValueForKey(llvm::StringRef(key));
0108 }
0109 return StructuredData::ObjectSP();
0110 }
0111
0112 StructuredData::ObjectSP GetItemAtIndex(size_t idx) const {
0113 if (m_data_sp) {
0114 auto array = m_data_sp->GetAsArray();
0115 if (array)
0116 return array->GetItemAtIndex(idx);
0117 }
0118 return StructuredData::ObjectSP();
0119 }
0120
0121 uint64_t GetIntegerValue(uint64_t fail_value = 0) const {
0122 return (m_data_sp ? m_data_sp->GetUnsignedIntegerValue(fail_value)
0123 : fail_value);
0124 }
0125
0126 int64_t GetIntegerValue(int64_t fail_value = 0) const {
0127 return (m_data_sp ? m_data_sp->GetSignedIntegerValue(fail_value)
0128 : fail_value);
0129 }
0130
0131 double GetFloatValue(double fail_value = 0.0) const {
0132 return (m_data_sp ? m_data_sp->GetFloatValue(fail_value) : fail_value);
0133 }
0134
0135 bool GetBooleanValue(bool fail_value = false) const {
0136 return (m_data_sp ? m_data_sp->GetBooleanValue(fail_value) : fail_value);
0137 }
0138
0139 size_t GetStringValue(char *dst, size_t dst_len) const {
0140 if (!m_data_sp)
0141 return 0;
0142
0143 llvm::StringRef result = m_data_sp->GetStringValue();
0144 if (result.empty())
0145 return 0;
0146
0147 if (!dst || !dst_len) {
0148 char s[1];
0149 return (::snprintf(s, 1, "%s", result.data()));
0150 }
0151 return (::snprintf(dst, dst_len, "%s", result.data()));
0152 }
0153
0154 void *GetGenericValue() const {
0155 if (!m_data_sp)
0156 return nullptr;
0157
0158 StructuredData::Generic *generic_data = m_data_sp->GetAsGeneric();
0159 if (!generic_data)
0160 return nullptr;
0161
0162 return generic_data->GetValue();
0163 }
0164
0165 StructuredData::ObjectSP GetObjectSP() const { return m_data_sp; }
0166
0167 private:
0168 lldb::StructuredDataPluginWP m_plugin_wp;
0169 StructuredData::ObjectSP m_data_sp;
0170 };
0171 }
0172 #endif