LLDB  mainline
StructuredData.cpp
Go to the documentation of this file.
1 //===-- StructuredData.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 
10 #include "lldb/Utility/FileSpec.h"
11 #include "lldb/Utility/Status.h"
12 #include "llvm/Support/MemoryBuffer.h"
13 #include <cerrno>
14 #include <cinttypes>
15 #include <cstdlib>
16 
17 using namespace lldb_private;
18 using namespace llvm;
19 
20 static StructuredData::ObjectSP ParseJSONValue(json::Value &value);
21 static StructuredData::ObjectSP ParseJSONObject(json::Object *object);
22 static StructuredData::ObjectSP ParseJSONArray(json::Array *array);
23 
26  llvm::Expected<json::Value> value = json::parse(json_text);
27  if (!value) {
28  llvm::consumeError(value.takeError());
29  return nullptr;
30  }
31  return ParseJSONValue(*value);
32 }
33 
36  StructuredData::ObjectSP return_sp;
37 
38  auto buffer_or_error = llvm::MemoryBuffer::getFile(input_spec.GetPath());
39  if (!buffer_or_error) {
40  error.SetErrorStringWithFormatv("could not open input file: {0} - {1}.",
41  input_spec.GetPath(),
42  buffer_or_error.getError().message());
43  return return_sp;
44  }
45  llvm::Expected<json::Value> value =
46  json::parse(buffer_or_error.get()->getBuffer().str());
47  if (value)
48  return ParseJSONValue(*value);
49  error.SetErrorString(toString(value.takeError()));
50  return StructuredData::ObjectSP();
51 }
52 
53 static StructuredData::ObjectSP ParseJSONValue(json::Value &value) {
54  if (json::Object *O = value.getAsObject())
55  return ParseJSONObject(O);
56 
57  if (json::Array *A = value.getAsArray())
58  return ParseJSONArray(A);
59 
60  if (auto s = value.getAsString())
61  return std::make_shared<StructuredData::String>(*s);
62 
63  if (auto b = value.getAsBoolean())
64  return std::make_shared<StructuredData::Boolean>(*b);
65 
66  if (auto i = value.getAsInteger())
67  return std::make_shared<StructuredData::Integer>(*i);
68 
69  if (auto d = value.getAsNumber())
70  return std::make_shared<StructuredData::Float>(*d);
71 
72  return StructuredData::ObjectSP();
73 }
74 
75 static StructuredData::ObjectSP ParseJSONObject(json::Object *object) {
76  auto dict_up = std::make_unique<StructuredData::Dictionary>();
77  for (auto &KV : *object) {
78  StringRef key = KV.first;
79  json::Value value = KV.second;
80  if (StructuredData::ObjectSP value_sp = ParseJSONValue(value))
81  dict_up->AddItem(key, value_sp);
82  }
83  return std::move(dict_up);
84 }
85 
86 static StructuredData::ObjectSP ParseJSONArray(json::Array *array) {
87  auto array_up = std::make_unique<StructuredData::Array>();
88  for (json::Value &value : *array) {
89  if (StructuredData::ObjectSP value_sp = ParseJSONValue(value))
90  array_up->AddItem(value_sp);
91  }
92  return std::move(array_up);
93 }
94 
97  if (this->GetType() == lldb::eStructuredDataTypeDictionary) {
98  std::pair<llvm::StringRef, llvm::StringRef> match = path.split('.');
99  std::string key = match.first.str();
100  ObjectSP value = this->GetAsDictionary()->GetValueForKey(key);
101  if (value.get()) {
102  // Do we have additional words to descend? If not, return the value
103  // we're at right now.
104  if (match.second.empty()) {
105  return value;
106  } else {
107  return value->GetObjectForDotSeparatedPath(match.second);
108  }
109  }
110  return ObjectSP();
111  }
112 
113  if (this->GetType() == lldb::eStructuredDataTypeArray) {
114  std::pair<llvm::StringRef, llvm::StringRef> match = path.split('[');
115  if (match.second.empty()) {
116  return this->shared_from_this();
117  }
118  errno = 0;
119  uint64_t val = strtoul(match.second.str().c_str(), nullptr, 10);
120  if (errno == 0) {
121  return this->GetAsArray()->GetItemAtIndex(val);
122  }
123  return ObjectSP();
124  }
125 
126  return this->shared_from_this();
127 }
128 
129 void StructuredData::Object::DumpToStdout(bool pretty_print) const {
130  json::OStream stream(llvm::outs(), pretty_print ? 2 : 0);
131  Serialize(stream);
132 }
133 
134 void StructuredData::Array::Serialize(json::OStream &s) const {
135  s.arrayBegin();
136  for (const auto &item_sp : m_items) {
137  item_sp->Serialize(s);
138  }
139  s.arrayEnd();
140 }
141 
142 void StructuredData::Integer::Serialize(json::OStream &s) const {
143  s.value(static_cast<int64_t>(m_value));
144 }
145 
146 void StructuredData::Float::Serialize(json::OStream &s) const {
147  s.value(m_value);
148 }
149 
150 void StructuredData::Boolean::Serialize(json::OStream &s) const {
151  s.value(m_value);
152 }
153 
154 void StructuredData::String::Serialize(json::OStream &s) const {
155  s.value(m_value);
156 }
157 
158 void StructuredData::Dictionary::Serialize(json::OStream &s) const {
159  s.objectBegin();
160  for (const auto &pair : m_dict) {
161  s.attributeBegin(pair.first.GetStringRef());
162  pair.second->Serialize(s);
163  s.attributeEnd();
164  }
165  s.objectEnd();
166 }
167 
168 void StructuredData::Null::Serialize(json::OStream &s) const {
169  s.value(nullptr);
170 }
171 
172 void StructuredData::Generic::Serialize(json::OStream &s) const {
173  s.value(llvm::formatv("{0:X}", m_object));
174 }
lldb_private::toString
const char * toString(AppleArm64ExceptionClass EC)
Definition: AppleArm64ExceptionClass.h:38
llvm
Definition: Debugger.h:49
StructuredData.h
lldb_private::StructuredData::Array::Serialize
void Serialize(llvm::json::OStream &s) const override
Definition: StructuredData.cpp:134
lldb::eStructuredDataTypeDictionary
@ eStructuredDataTypeDictionary
Definition: lldb-enumerations.h:794
lldb_private::StructuredData::Object::DumpToStdout
void DumpToStdout(bool pretty_print=true) const
Definition: StructuredData.cpp:129
lldb_private::StructuredData::Dictionary::Serialize
void Serialize(llvm::json::OStream &s) const override
Definition: StructuredData.cpp:158
lldb_private::StructuredData::Integer::Serialize
void Serialize(llvm::json::OStream &s) const override
Definition: StructuredData.cpp:142
lldb_private::StructuredData::Null::Serialize
void Serialize(llvm::json::OStream &s) const override
Definition: StructuredData.cpp:168
lldb_private::FileSpec
Definition: FileSpec.h:56
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::StructuredData::ParseJSONFromFile
static ObjectSP ParseJSONFromFile(const FileSpec &file, Status &error)
Definition: StructuredData.cpp:35
lldb_private::StructuredData::ObjectSP
std::shared_ptr< Object > ObjectSP
Definition: StructuredData.h:59
lldb_private::StructuredData::Boolean::Serialize
void Serialize(llvm::json::OStream &s) const override
Definition: StructuredData.cpp:150
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
ParseJSONValue
static StructuredData::ObjectSP ParseJSONValue(json::Value &value)
Definition: StructuredData.cpp:53
lldb_private::Status
Definition: Status.h:44
lldb_private::StructuredData::String::Serialize
void Serialize(llvm::json::OStream &s) const override
Definition: StructuredData.cpp:154
ParseJSONObject
static StructuredData::ObjectSP ParseJSONObject(json::Object *object)
Definition: StructuredData.cpp:75
Status.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::StructuredData::Object::GetObjectForDotSeparatedPath
ObjectSP GetObjectForDotSeparatedPath(llvm::StringRef path)
Definition: StructuredData.cpp:96
FileSpec.h
lldb::eStructuredDataTypeArray
@ eStructuredDataTypeArray
Definition: lldb-enumerations.h:789
lldb_private::StructuredData::ParseJSON
static ObjectSP ParseJSON(const std::string &json_text)
Definition: StructuredData.cpp:25
lldb_private::FileSpec::GetPath
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:346
lldb_private::StructuredData::Generic::Serialize
void Serialize(llvm::json::OStream &s) const override
Definition: StructuredData.cpp:172
lldb_private::StructuredData::Float::Serialize
void Serialize(llvm::json::OStream &s) const override
Definition: StructuredData.cpp:146
ParseJSONArray
static StructuredData::ObjectSP ParseJSONArray(json::Array *array)
Definition: StructuredData.cpp:86