1 //===-- SBStructuredData.cpp ------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/API/SBStructuredData.h" 11 12 #include "lldb/API/SBStream.h" 13 #include "lldb/Core/Error.h" 14 #include "lldb/Core/Event.h" 15 #include "lldb/Core/Stream.h" 16 #include "lldb/Core/StructuredData.h" 17 #include "lldb/Target/StructuredDataPlugin.h" 18 19 using namespace lldb; 20 using namespace lldb_private; 21 22 #pragma mark -- 23 #pragma mark Impl 24 25 class SBStructuredData::Impl 26 { 27 public: 28 29 Impl() : 30 m_plugin_wp(), 31 m_data_sp() 32 { 33 } 34 35 Impl(const Impl &rhs) = default; 36 37 Impl(const EventSP &event_sp) : 38 m_plugin_wp(EventDataStructuredData::GetPluginFromEvent(event_sp.get())), 39 m_data_sp(EventDataStructuredData::GetObjectFromEvent(event_sp.get())) 40 { 41 } 42 43 ~Impl() = default; 44 45 Impl& 46 operator =(const Impl &rhs) = default; 47 48 bool 49 IsValid() const 50 { 51 return m_data_sp.get() != nullptr; 52 } 53 54 void 55 Clear() 56 { 57 m_plugin_wp.reset(); 58 m_data_sp.reset(); 59 } 60 61 SBError 62 GetAsJSON(lldb::SBStream &stream) const 63 { 64 SBError sb_error; 65 66 if (!m_data_sp) 67 { 68 sb_error.SetErrorString("No structured data."); 69 return sb_error; 70 } 71 72 m_data_sp->Dump(stream.ref()); 73 return sb_error; 74 } 75 76 lldb::SBError 77 GetDescription(lldb::SBStream &stream) const 78 { 79 SBError sb_error; 80 81 if (!m_data_sp) 82 { 83 sb_error.SetErrorString("Cannot pretty print structured data: " 84 "no data to print."); 85 return sb_error; 86 } 87 88 // Grab the plugin. 89 auto plugin_sp = StructuredDataPluginSP(m_plugin_wp); 90 if (!plugin_sp) 91 { 92 sb_error.SetErrorString("Cannot pretty print structured data: " 93 "plugin doesn't exist."); 94 return sb_error; 95 } 96 97 // Get the data's description. 98 auto error = plugin_sp->GetDescription(m_data_sp, stream.ref()); 99 if (!error.Success()) 100 sb_error.SetError(error); 101 102 return sb_error; 103 } 104 105 private: 106 107 StructuredDataPluginWP m_plugin_wp; 108 StructuredData::ObjectSP m_data_sp; 109 110 }; 111 112 #pragma mark -- 113 #pragma mark SBStructuredData 114 115 116 SBStructuredData::SBStructuredData() : 117 m_impl_up(new Impl()) 118 { 119 } 120 121 SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs) : 122 m_impl_up(new Impl(*rhs.m_impl_up.get())) 123 { 124 } 125 126 SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp) : 127 m_impl_up(new Impl(event_sp)) 128 { 129 } 130 131 SBStructuredData::~SBStructuredData() 132 { 133 } 134 135 SBStructuredData & 136 SBStructuredData::operator =(const lldb::SBStructuredData &rhs) 137 { 138 *m_impl_up = *rhs.m_impl_up; 139 return *this; 140 } 141 142 bool 143 SBStructuredData::IsValid() const 144 { 145 return m_impl_up->IsValid(); 146 } 147 148 void 149 SBStructuredData::Clear() 150 { 151 m_impl_up->Clear(); 152 } 153 154 SBError 155 SBStructuredData::GetAsJSON(lldb::SBStream &stream) const 156 { 157 return m_impl_up->GetAsJSON(stream); 158 } 159 160 lldb::SBError 161 SBStructuredData::GetDescription(lldb::SBStream &stream) const 162 { 163 return m_impl_up->GetDescription(stream); 164 } 165 166