blob: d9ea072186a9bb3fe9ae31b00103683bd86b5df3 [file] [log] [blame]
Todd Fiala75930012016-08-19 04:21:48 +00001//===-- 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
19using namespace lldb;
20using namespace lldb_private;
21
Kate Stoneb9c1b512016-09-06 20:57:50 +000022#pragma mark--
Todd Fiala2ef442c2016-11-09 23:21:04 +000023#pragma mark StructuredDataImpl
Todd Fiala75930012016-08-19 04:21:48 +000024
Todd Fiala2ef442c2016-11-09 23:21:04 +000025class StructuredDataImpl {
Todd Fiala75930012016-08-19 04:21:48 +000026public:
Todd Fiala2ef442c2016-11-09 23:21:04 +000027 StructuredDataImpl() : m_plugin_wp(), m_data_sp() {}
Todd Fiala75930012016-08-19 04:21:48 +000028
Todd Fiala2ef442c2016-11-09 23:21:04 +000029 StructuredDataImpl(const StructuredDataImpl &rhs) = default;
Kate Stoneb9c1b512016-09-06 20:57:50 +000030
Todd Fiala2ef442c2016-11-09 23:21:04 +000031 StructuredDataImpl(const EventSP &event_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +000032 : m_plugin_wp(
33 EventDataStructuredData::GetPluginFromEvent(event_sp.get())),
34 m_data_sp(EventDataStructuredData::GetObjectFromEvent(event_sp.get())) {
35 }
36
Todd Fiala2ef442c2016-11-09 23:21:04 +000037 ~StructuredDataImpl() = default;
Kate Stoneb9c1b512016-09-06 20:57:50 +000038
Todd Fiala2ef442c2016-11-09 23:21:04 +000039 StructuredDataImpl &operator=(const StructuredDataImpl &rhs) = default;
Kate Stoneb9c1b512016-09-06 20:57:50 +000040
41 bool IsValid() const { return m_data_sp.get() != nullptr; }
42
43 void Clear() {
44 m_plugin_wp.reset();
45 m_data_sp.reset();
46 }
47
Todd Fiala2ef442c2016-11-09 23:21:04 +000048 SBError GetAsJSON(lldb_private::Stream &stream) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +000049 SBError sb_error;
50
51 if (!m_data_sp) {
52 sb_error.SetErrorString("No structured data.");
53 return sb_error;
Todd Fiala75930012016-08-19 04:21:48 +000054 }
55
Todd Fiala2ef442c2016-11-09 23:21:04 +000056 m_data_sp->Dump(stream);
Kate Stoneb9c1b512016-09-06 20:57:50 +000057 return sb_error;
58 }
Todd Fiala75930012016-08-19 04:21:48 +000059
Todd Fiala2ef442c2016-11-09 23:21:04 +000060 Error GetDescription(lldb_private::Stream &stream) const {
61 Error error;
Kate Stoneb9c1b512016-09-06 20:57:50 +000062
63 if (!m_data_sp) {
Todd Fiala2ef442c2016-11-09 23:21:04 +000064 error.SetErrorString("Cannot pretty print structured data: "
65 "no data to print.");
66 return error;
Todd Fiala75930012016-08-19 04:21:48 +000067 }
68
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 // Grab the plugin.
70 auto plugin_sp = StructuredDataPluginSP(m_plugin_wp);
71 if (!plugin_sp) {
Todd Fiala2ef442c2016-11-09 23:21:04 +000072 error.SetErrorString("Cannot pretty print structured data: "
73 "plugin doesn't exist.");
74 return error;
Todd Fiala75930012016-08-19 04:21:48 +000075 }
76
Kate Stoneb9c1b512016-09-06 20:57:50 +000077 // Get the data's description.
Todd Fiala2ef442c2016-11-09 23:21:04 +000078 return plugin_sp->GetDescription(m_data_sp, stream);
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 }
Todd Fiala75930012016-08-19 04:21:48 +000080
81private:
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 StructuredDataPluginWP m_plugin_wp;
83 StructuredData::ObjectSP m_data_sp;
Todd Fiala75930012016-08-19 04:21:48 +000084};
85
Kate Stoneb9c1b512016-09-06 20:57:50 +000086#pragma mark--
Todd Fiala75930012016-08-19 04:21:48 +000087#pragma mark SBStructuredData
88
Todd Fiala2ef442c2016-11-09 23:21:04 +000089SBStructuredData::SBStructuredData() : m_impl_up(new StructuredDataImpl()) {}
Todd Fiala75930012016-08-19 04:21:48 +000090
Kate Stoneb9c1b512016-09-06 20:57:50 +000091SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs)
Todd Fiala2ef442c2016-11-09 23:21:04 +000092 : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up.get())) {}
Kate Stoneb9c1b512016-09-06 20:57:50 +000093
94SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp)
Todd Fiala2ef442c2016-11-09 23:21:04 +000095 : m_impl_up(new StructuredDataImpl(event_sp)) {}
Kate Stoneb9c1b512016-09-06 20:57:50 +000096
97SBStructuredData::~SBStructuredData() {}
98
99SBStructuredData &SBStructuredData::
100operator=(const lldb::SBStructuredData &rhs) {
101 *m_impl_up = *rhs.m_impl_up;
102 return *this;
Todd Fiala75930012016-08-19 04:21:48 +0000103}
104
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105bool SBStructuredData::IsValid() const { return m_impl_up->IsValid(); }
106
107void SBStructuredData::Clear() { m_impl_up->Clear(); }
108
109SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const {
Todd Fiala2ef442c2016-11-09 23:21:04 +0000110 return m_impl_up->GetAsJSON(stream.ref());
Todd Fiala75930012016-08-19 04:21:48 +0000111}
112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const {
Todd Fiala2ef442c2016-11-09 23:21:04 +0000114 Error error = m_impl_up->GetDescription(stream.ref());
115 SBError sb_error;
116 sb_error.SetError(error);
117 return sb_error;
Todd Fiala75930012016-08-19 04:21:48 +0000118}