LLDB  mainline
TraceIntelPTJSONStructs.cpp
Go to the documentation of this file.
1 //===-- TraceIntelPTJSONStructs.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 "llvm/Support/JSON.h"
11 #include <string>
12 
13 using namespace lldb;
14 using namespace lldb_private;
15 using namespace lldb_private::trace_intel_pt;
16 using namespace llvm;
17 using namespace llvm::json;
18 
19 namespace lldb_private {
20 namespace trace_intel_pt {
21 
22 Optional<std::vector<lldb::cpu_id_t>> JSONTraceBundleDescription::GetCpuIds() {
23  if (!cpus)
24  return None;
25  std::vector<lldb::cpu_id_t> cpu_ids;
26  for (const JSONCpu &cpu : *cpus)
27  cpu_ids.push_back(cpu.id);
28  return cpu_ids;
29 }
30 
31 json::Value toJSON(const JSONModule &module) {
32  json::Object json_module;
33  json_module["systemPath"] = module.system_path;
34  if (module.file)
35  json_module["file"] = *module.file;
36  json_module["loadAddress"] = toJSON(module.load_address, true);
37  if (module.uuid)
38  json_module["uuid"] = *module.uuid;
39  return std::move(json_module);
40 }
41 
42 bool fromJSON(const json::Value &value, JSONModule &module, Path path) {
43  ObjectMapper o(value, path);
44  return o && o.map("systemPath", module.system_path) &&
45  o.map("file", module.file) &&
46  o.map("loadAddress", module.load_address) &&
47  o.map("uuid", module.uuid);
48 }
49 
50 json::Value toJSON(const JSONThread &thread) {
51  json::Object obj{{"tid", thread.tid}};
52  if (thread.ipt_trace)
53  obj["iptTrace"] = *thread.ipt_trace;
54  return obj;
55 }
56 
57 bool fromJSON(const json::Value &value, JSONThread &thread, Path path) {
58  ObjectMapper o(value, path);
59  return o && o.map("tid", thread.tid) && o.map("iptTrace", thread.ipt_trace);
60 }
61 
62 json::Value toJSON(const JSONProcess &process) {
63  return Object{
64  {"pid", process.pid},
65  {"triple", process.triple},
66  {"threads", process.threads},
67  {"modules", process.modules},
68  };
69 }
70 
71 bool fromJSON(const json::Value &value, JSONProcess &process, Path path) {
72  ObjectMapper o(value, path);
73  return o && o.map("pid", process.pid) && o.map("triple", process.triple) &&
74  o.map("threads", process.threads) && o.map("modules", process.modules);
75 }
76 
77 json::Value toJSON(const JSONCpu &cpu) {
78  return Object{
79  {"id", cpu.id},
80  {"iptTrace", cpu.ipt_trace},
81  {"contextSwitchTrace", cpu.context_switch_trace},
82  };
83 }
84 
85 bool fromJSON(const json::Value &value, JSONCpu &cpu, Path path) {
86  ObjectMapper o(value, path);
87  uint64_t cpu_id;
88  if (!(o && o.map("id", cpu_id) && o.map("iptTrace", cpu.ipt_trace) &&
89  o.map("contextSwitchTrace", cpu.context_switch_trace)))
90  return false;
91  cpu.id = cpu_id;
92  return true;
93 }
94 
95 json::Value toJSON(const pt_cpu &cpu_info) {
96  return Object{
97  {"vendor", cpu_info.vendor == pcv_intel ? "GenuineIntel" : "Unknown"},
98  {"family", cpu_info.family},
99  {"model", cpu_info.model},
100  {"stepping", cpu_info.stepping},
101  };
102 }
103 
104 bool fromJSON(const json::Value &value, pt_cpu &cpu_info, Path path) {
105  ObjectMapper o(value, path);
106  std::string vendor;
107  uint64_t family, model, stepping;
108  if (!(o && o.map("vendor", vendor) && o.map("family", family) &&
109  o.map("model", model) && o.map("stepping", stepping)))
110  return false;
111  cpu_info.vendor = vendor == "GenuineIntel" ? pcv_intel : pcv_unknown;
112  cpu_info.family = family;
113  cpu_info.model = model;
114  cpu_info.stepping = stepping;
115  return true;
116 }
117 
118 json::Value toJSON(const JSONKernel &kernel) {
119  json::Object json_module;
120  if (kernel.load_address)
121  json_module["loadAddress"] = toJSON(*kernel.load_address, true);
122  json_module["file"] = kernel.file;
123  return std::move(json_module);
124 }
125 
126 bool fromJSON(const json::Value &value, JSONKernel &kernel, Path path) {
127  ObjectMapper o(value, path);
128  return o && o.map("loadAddress", kernel.load_address) &&
129  o.map("file", kernel.file);
130 }
131 
132 json::Value toJSON(const JSONTraceBundleDescription &bundle_description) {
133  return Object{
134  {"type", bundle_description.type},
135  {"processes", bundle_description.processes},
136  // We have to do this because the compiler fails at doing it
137  // automatically because pt_cpu is not in a namespace
138  {"cpuInfo", toJSON(bundle_description.cpu_info)},
139  {"cpus", bundle_description.cpus},
140  {"tscPerfZeroConversion", bundle_description.tsc_perf_zero_conversion},
141  {"kernel", bundle_description.kernel}};
142 }
143 
144 bool fromJSON(const json::Value &value,
145  JSONTraceBundleDescription &bundle_description, Path path) {
146  ObjectMapper o(value, path);
147  if (!(o && o.map("processes", bundle_description.processes) &&
148  o.map("type", bundle_description.type) &&
149  o.map("cpus", bundle_description.cpus) &&
150  o.map("tscPerfZeroConversion",
151  bundle_description.tsc_perf_zero_conversion) &&
152  o.map("kernel", bundle_description.kernel)))
153  return false;
154  if (bundle_description.cpus && !bundle_description.tsc_perf_zero_conversion) {
155  path.report(
156  "\"tscPerfZeroConversion\" is required when \"cpus\" is provided");
157  return false;
158  }
159  // We have to do this because the compiler fails at doing it automatically
160  // because pt_cpu is not in a namespace
161  if (!fromJSON(*value.getAsObject()->get("cpuInfo"),
162  bundle_description.cpu_info, path.field("cpuInfo")))
163  return false;
164 
165  // When kernel section is present, this is kernel-only tracing. Thus, throw an
166  // error if the "processes" section is non-empty or the "cpus" section is not
167  // present.
168  if (bundle_description.kernel) {
169  if (bundle_description.processes &&
170  !bundle_description.processes->empty()) {
171  path.report("\"processes\" must be empty when \"kernel\" is provided");
172  return false;
173  }
174  if (!bundle_description.cpus) {
175  path.report("\"cpus\" is required when \"kernel\" is provided");
176  return false;
177  }
178  } else if (!bundle_description.processes) {
179  // Usermode tracing requires processes section.
180  path.report("\"processes\" is required when \"kernel\" is not provided");
181  return false;
182  }
183  return true;
184 }
185 
186 } // namespace trace_intel_pt
187 } // namespace lldb_private
lldb_private::trace_intel_pt::JSONKernel::file
std::string file
Definition: TraceIntelPTJSONStructs.h:49
llvm
Definition: Debugger.h:50
lldb_private::trace_intel_pt::JSONTraceBundleDescription::cpu_info
pt_cpu cpu_info
Definition: TraceIntelPTJSONStructs.h:54
lldb_private::trace_intel_pt::JSONProcess::pid
uint64_t pid
Definition: TraceIntelPTJSONStructs.h:35
lldb_private::trace_intel_pt::JSONTraceBundleDescription::processes
llvm::Optional< std::vector< JSONProcess > > processes
Definition: TraceIntelPTJSONStructs.h:55
lldb_private::trace_intel_pt::JSONModule
Definition: TraceIntelPTJSONStructs.h:22
lldb_private::trace_intel_pt::JSONProcess::threads
std::vector< JSONThread > threads
Definition: TraceIntelPTJSONStructs.h:37
lldb_private::trace_intel_pt::JSONTraceBundleDescription::cpus
llvm::Optional< std::vector< JSONCpu > > cpus
Definition: TraceIntelPTJSONStructs.h:56
TraceIntelPTJSONStructs.h
llvm::json
Definition: Trace.cpp:35
lldb_private::trace_intel_pt::JSONThread::tid
uint64_t tid
Definition: TraceIntelPTJSONStructs.h:30
lldb_private::trace_intel_pt::JSONTraceBundleDescription::kernel
llvm::Optional< JSONKernel > kernel
Definition: TraceIntelPTJSONStructs.h:58
lldb_private::trace_intel_pt::JSONModule::file
llvm::Optional< std::string > file
Definition: TraceIntelPTJSONStructs.h:24
lldb_private::trace_intel_pt::JSONThread
Definition: TraceIntelPTJSONStructs.h:29
lldb_private::trace_intel_pt::JSONModule::load_address
JSONUINT64 load_address
Definition: TraceIntelPTJSONStructs.h:25
lldb_private::trace_intel_pt::JSONCpu::ipt_trace
std::string ipt_trace
Definition: TraceIntelPTJSONStructs.h:43
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::trace_intel_pt::toJSON
json::Value toJSON(const JSONTraceBundleDescription &bundle_description)
Definition: TraceIntelPTJSONStructs.cpp:132
lldb_private::trace_intel_pt::fromJSON
bool fromJSON(const json::Value &value, JSONTraceBundleDescription &bundle_description, Path path)
Definition: TraceIntelPTJSONStructs.cpp:144
lldb_private::trace_intel_pt::JSONCpu
Definition: TraceIntelPTJSONStructs.h:41
lldb_private::trace_intel_pt::JSONCpu::context_switch_trace
std::string context_switch_trace
Definition: TraceIntelPTJSONStructs.h:44
lldb_private::trace_intel_pt::JSONThread::ipt_trace
llvm::Optional< std::string > ipt_trace
Definition: TraceIntelPTJSONStructs.h:31
lldb_private::trace_intel_pt::JSONProcess::triple
llvm::Optional< std::string > triple
Definition: TraceIntelPTJSONStructs.h:36
lldb_private::trace_intel_pt::JSONKernel
Definition: TraceIntelPTJSONStructs.h:47
lldb_private::trace_intel_pt::JSONTraceBundleDescription
Definition: TraceIntelPTJSONStructs.h:52
lldb_private::LLDBLog::Object
@ Object
lldb_private::trace_intel_pt::JSONModule::uuid
llvm::Optional< std::string > uuid
Definition: TraceIntelPTJSONStructs.h:26
lldb_private::trace_intel_pt::JSONTraceBundleDescription::type
std::string type
Definition: TraceIntelPTJSONStructs.h:53
lldb_private::trace_intel_pt::JSONTraceBundleDescription::tsc_perf_zero_conversion
llvm::Optional< LinuxPerfZeroTscConversion > tsc_perf_zero_conversion
Definition: TraceIntelPTJSONStructs.h:57
lldb_private::trace_intel_pt::JSONProcess::modules
std::vector< JSONModule > modules
Definition: TraceIntelPTJSONStructs.h:38
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::trace_intel_pt::JSONCpu::id
lldb::cpu_id_t id
Definition: TraceIntelPTJSONStructs.h:42
lldb_private::trace_intel_pt::JSONKernel::load_address
llvm::Optional< JSONUINT64 > load_address
Definition: TraceIntelPTJSONStructs.h:48
lldb_private::trace_intel_pt
Definition: CommandObjectTraceStartIntelPT.h:18
lldb_private::trace_intel_pt::JSONModule::system_path
std::string system_path
Definition: TraceIntelPTJSONStructs.h:23
lldb_private::trace_intel_pt::JSONProcess
Definition: TraceIntelPTJSONStructs.h:34
lldb
Definition: SBAddress.h:15