blob: d901f915dad4f72b791e6b1e8746b9c3c815a1e4 [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"
Jim Ingham3815e702018-09-13 21:35:32 +000013#include "lldb/API/SBStringList.h"
Todd Fiala75930012016-08-19 04:21:48 +000014#include "lldb/Core/Event.h"
Ravitheja Addepallyd5d8d912017-04-26 08:48:50 +000015#include "lldb/Core/StructuredDataImpl.h"
Todd Fiala75930012016-08-19 04:21:48 +000016#include "lldb/Target/StructuredDataPlugin.h"
Zachary Turner97206d52017-05-12 04:51:55 +000017#include "lldb/Utility/Status.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000018#include "lldb/Utility/Stream.h"
Pavel Labathf2a8bcc2017-06-27 10:45:31 +000019#include "lldb/Utility/StructuredData.h"
Todd Fiala75930012016-08-19 04:21:48 +000020
21using namespace lldb;
22using namespace lldb_private;
23
Kate Stoneb9c1b512016-09-06 20:57:50 +000024#pragma mark--
Todd Fiala75930012016-08-19 04:21:48 +000025#pragma mark SBStructuredData
26
Todd Fiala2ef442c2016-11-09 23:21:04 +000027SBStructuredData::SBStructuredData() : m_impl_up(new StructuredDataImpl()) {}
Todd Fiala75930012016-08-19 04:21:48 +000028
Kate Stoneb9c1b512016-09-06 20:57:50 +000029SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs)
Todd Fiala2ef442c2016-11-09 23:21:04 +000030 : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up.get())) {}
Kate Stoneb9c1b512016-09-06 20:57:50 +000031
32SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp)
Todd Fiala2ef442c2016-11-09 23:21:04 +000033 : m_impl_up(new StructuredDataImpl(event_sp)) {}
Kate Stoneb9c1b512016-09-06 20:57:50 +000034
Jim Ingham3815e702018-09-13 21:35:32 +000035SBStructuredData::SBStructuredData(lldb_private::StructuredDataImpl *impl)
36 : m_impl_up(impl) {}
37
Kate Stoneb9c1b512016-09-06 20:57:50 +000038SBStructuredData::~SBStructuredData() {}
39
40SBStructuredData &SBStructuredData::
41operator=(const lldb::SBStructuredData &rhs) {
42 *m_impl_up = *rhs.m_impl_up;
43 return *this;
Todd Fiala75930012016-08-19 04:21:48 +000044}
45
Ravitheja Addepallyd5d8d912017-04-26 08:48:50 +000046lldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream &stream) {
47 lldb::SBError error;
48 std::string json_str(stream.GetData());
49
50 StructuredData::ObjectSP json_obj = StructuredData::ParseJSON(json_str);
51 m_impl_up->SetObjectSP(json_obj);
52
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +000053 if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary)
Ravitheja Addepallyd5d8d912017-04-26 08:48:50 +000054 error.SetErrorString("Invalid Syntax");
55 return error;
56}
57
Kate Stoneb9c1b512016-09-06 20:57:50 +000058bool SBStructuredData::IsValid() const { return m_impl_up->IsValid(); }
59
60void SBStructuredData::Clear() { m_impl_up->Clear(); }
61
62SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const {
Ravitheja Addepallyd5d8d912017-04-26 08:48:50 +000063 SBError error;
64 error.SetError(m_impl_up->GetAsJSON(stream.ref()));
65 return error;
Todd Fiala75930012016-08-19 04:21:48 +000066}
67
Kate Stoneb9c1b512016-09-06 20:57:50 +000068lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const {
Zachary Turner97206d52017-05-12 04:51:55 +000069 Status error = m_impl_up->GetDescription(stream.ref());
Todd Fiala2ef442c2016-11-09 23:21:04 +000070 SBError sb_error;
71 sb_error.SetError(error);
72 return sb_error;
Todd Fiala75930012016-08-19 04:21:48 +000073}
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +000074
75StructuredDataType SBStructuredData::GetType() const {
76 return (m_impl_up ? m_impl_up->GetType() : eStructuredDataTypeInvalid);
77}
78
79size_t SBStructuredData::GetSize() const {
80 return (m_impl_up ? m_impl_up->GetSize() : 0);
81}
82
Jim Ingham3815e702018-09-13 21:35:32 +000083bool SBStructuredData::GetKeys(lldb::SBStringList &keys) const {
84 if (!m_impl_up)
85 return false;
86
87 if (GetType() != eStructuredDataTypeDictionary)
88 return false;
89
90 StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP();
91 if (!obj_sp)
92 return false;
93
94 StructuredData::Dictionary *dict = obj_sp->GetAsDictionary();
95 // We claimed we were a dictionary, so this can't be null.
96 assert(dict);
97 // The return kind of GetKeys is an Array:
98 StructuredData::ObjectSP array_sp = dict->GetKeys();
99 StructuredData::Array *key_arr = array_sp->GetAsArray();
100 assert(key_arr);
101
102 key_arr->ForEach([&keys] (StructuredData::Object *object) -> bool {
103 llvm::StringRef key = object->GetStringValue("");
104 keys.AppendString(key.str().c_str());
105 return true;
106 });
107 return true;
108}
109
Abhishek Aggarwal5bfee5f2017-05-29 08:25:46 +0000110lldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const {
111 if (!m_impl_up)
112 return SBStructuredData();
113
114 SBStructuredData result;
115 result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key));
116 return result;
117}
118
119lldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const {
120 if (!m_impl_up)
121 return SBStructuredData();
122
123 SBStructuredData result;
124 result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx));
125 return result;
126}
127
128uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const {
129 return (m_impl_up ? m_impl_up->GetIntegerValue(fail_value) : fail_value);
130}
131
132double SBStructuredData::GetFloatValue(double fail_value) const {
133 return (m_impl_up ? m_impl_up->GetFloatValue(fail_value) : fail_value);
134}
135
136bool SBStructuredData::GetBooleanValue(bool fail_value) const {
137 return (m_impl_up ? m_impl_up->GetBooleanValue(fail_value) : fail_value);
138}
139
140size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const {
141 return (m_impl_up ? m_impl_up->GetStringValue(dst, dst_len) : 0);
142}