LLDB  mainline
TraceJSONStructs.cpp
Go to the documentation of this file.
1 //===-- TraceSessionFileStructs.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 
9 #include "TraceJSONStructs.h"
10 #include "ThreadPostMortemTrace.h"
11 #include "lldb/Core/Debugger.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Target/Process.h"
14 #include "lldb/Target/Target.h"
15 #include <sstream>
16 
17 using namespace lldb_private;
18 namespace llvm {
19 namespace json {
20 
21 llvm::json::Value toJSON(const JSONModule &module) {
22  llvm::json::Object json_module;
23  json_module["systemPath"] = module.system_path;
24  if (module.file)
25  json_module["file"] = *module.file;
26  std::ostringstream oss;
27  oss << "0x" << std::hex << module.load_address.value;
28  json_module["loadAddress"] = oss.str();
29  if (module.uuid)
30  json_module["uuid"] = *module.uuid;
31  return std::move(json_module);
32 }
33 
34 llvm::json::Value toJSON(const JSONThread &thread) {
35  return Value(Object{{"tid", thread.tid}, {"traceFile", thread.trace_file}});
36 }
37 
38 llvm::json::Value toJSON(const JSONProcess &process) {
39  llvm::json::Object json_process;
40  json_process["pid"] = process.pid;
41  json_process["triple"] = process.triple;
42 
43  llvm::json::Array threads_arr;
44  for (JSONThread e : process.threads)
45  threads_arr.push_back(toJSON(e));
46 
47  json_process["threads"] = llvm::json::Value(std::move(threads_arr));
48 
49  llvm::json::Array modules_arr;
50  for (JSONModule e : process.modules)
51  modules_arr.push_back(toJSON(e));
52 
53  json_process["modules"] = llvm::json::Value(std::move(modules_arr));
54 
55  return std::move(json_process);
56 }
57 
58 llvm::json::Value toJSON(const JSONTraceSessionBase &session) {
59  llvm::json::Array arr;
60  for (JSONProcess e : session.processes)
61  arr.push_back(toJSON(e));
62 
63  return std::move(arr);
64 }
65 
66 bool fromJSON(const Value &value, JSONAddress &address, Path path) {
67  Optional<StringRef> s = value.getAsString();
68  if (s.hasValue() && !s->getAsInteger(0, address.value))
69  return true;
70 
71  path.report("expected numeric string");
72  return false;
73 }
74 
75 bool fromJSON(const Value &value, JSONModule &module, Path path) {
76  ObjectMapper o(value, path);
77  return o && o.map("systemPath", module.system_path) &&
78  o.map("file", module.file) &&
79  o.map("loadAddress", module.load_address) &&
80  o.map("uuid", module.uuid);
81 }
82 
83 bool fromJSON(const Value &value, JSONThread &thread, Path path) {
84  ObjectMapper o(value, path);
85  return o && o.map("tid", thread.tid) && o.map("traceFile", thread.trace_file);
86 }
87 
88 bool fromJSON(const Value &value, JSONProcess &process, Path path) {
89  ObjectMapper o(value, path);
90  return o && o.map("pid", process.pid) && o.map("triple", process.triple) &&
91  o.map("threads", process.threads) && o.map("modules", process.modules);
92 }
93 
94 bool fromJSON(const Value &value, JSONTracePluginSettings &plugin_settings,
95  Path path) {
96  ObjectMapper o(value, path);
97  return o && o.map("type", plugin_settings.type);
98 }
99 
100 bool fromJSON(const Value &value, JSONTraceSessionBase &session, Path path) {
101  ObjectMapper o(value, path);
102  return o && o.map("processes", session.processes);
103 }
104 
105 } // namespace json
106 } // namespace llvm
lldb_private::toJSON
llvm::json::Value toJSON(const TraceSupportedResponse &packet)
Definition: TraceGDBRemotePackets.cpp:24
llvm
Definition: Debugger.h:49
lldb_private::Value
Definition: Value.h:38
lldb_private::JSONModule::file
llvm::Optional< std::string > file
Definition: TraceJSONStructs.h:23
ThreadPostMortemTrace.h
lldb_private::JSONThread
Definition: TraceJSONStructs.h:28
lldb_private::JSONModule::load_address
JSONAddress load_address
Definition: TraceJSONStructs.h:24
Module.h
Debugger.h
Process.h
lldb_private::JSONTracePluginSettings
Definition: TraceJSONStructs.h:40
Target.h
lldb_private::JSONTracePluginSettings::type
std::string type
Definition: TraceJSONStructs.h:41
lldb_private::JSONThread::trace_file
std::string trace_file
Definition: TraceJSONStructs.h:30
lldb_private::JSONProcess::pid
int64_t pid
Definition: TraceJSONStructs.h:34
lldb_private::JSONTraceSessionBase
Definition: TraceJSONStructs.h:44
lldb_private::fromJSON
bool fromJSON(const llvm::json::Value &value, TraceSupportedResponse &info, llvm::json::Path path)
lldb_private::JSONTraceSessionBase::processes
std::vector< JSONProcess > processes
Definition: TraceJSONStructs.h:45
lldb_private::JSONAddress
Definition: TraceJSONStructs.h:17
lldb_private::JSONProcess::modules
std::vector< JSONModule > modules
Definition: TraceJSONStructs.h:37
lldb_private::JSONAddress::value
lldb::addr_t value
Definition: TraceJSONStructs.h:18
lldb_private::JSONProcess::triple
std::string triple
Definition: TraceJSONStructs.h:35
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::JSONModule
Definition: TraceJSONStructs.h:21
lldb_private::JSONThread::tid
int64_t tid
Definition: TraceJSONStructs.h:29
lldb_private::JSONProcess::threads
std::vector< JSONThread > threads
Definition: TraceJSONStructs.h:36
lldb_private::JSONModule::uuid
llvm::Optional< std::string > uuid
Definition: TraceJSONStructs.h:25
TraceJSONStructs.h
lldb_private::JSONProcess
Definition: TraceJSONStructs.h:33
lldb_private::JSONModule::system_path
std::string system_path
Definition: TraceJSONStructs.h:22