File: TraceIntelPTJSONStructs.cpp

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (189 lines) | stat: -rw-r--r-- 6,357 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//===-- TraceIntelPTJSONStructs.cpp ---------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "TraceIntelPTJSONStructs.h"
#include "llvm/Support/JSON.h"
#include <optional>
#include <string>

using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::trace_intel_pt;
using namespace llvm;
using namespace llvm::json;

namespace lldb_private {
namespace trace_intel_pt {

std::optional<std::vector<lldb::cpu_id_t>>
JSONTraceBundleDescription::GetCpuIds() {
  if (!cpus)
    return std::nullopt;
  std::vector<lldb::cpu_id_t> cpu_ids;
  for (const JSONCpu &cpu : *cpus)
    cpu_ids.push_back(cpu.id);
  return cpu_ids;
}

json::Value toJSON(const JSONModule &module) {
  json::Object json_module;
  json_module["systemPath"] = module.system_path;
  if (module.file)
    json_module["file"] = *module.file;
  json_module["loadAddress"] = toJSON(module.load_address, true);
  if (module.uuid)
    json_module["uuid"] = *module.uuid;
  return std::move(json_module);
}

bool fromJSON(const json::Value &value, JSONModule &module, Path path) {
  ObjectMapper o(value, path);
  return o && o.map("systemPath", module.system_path) &&
         o.map("file", module.file) &&
         o.map("loadAddress", module.load_address) &&
         o.map("uuid", module.uuid);
}

json::Value toJSON(const JSONThread &thread) {
  json::Object obj{{"tid", thread.tid}};
  if (thread.ipt_trace)
    obj["iptTrace"] = *thread.ipt_trace;
  return obj;
}

bool fromJSON(const json::Value &value, JSONThread &thread, Path path) {
  ObjectMapper o(value, path);
  return o && o.map("tid", thread.tid) && o.map("iptTrace", thread.ipt_trace);
}

json::Value toJSON(const JSONProcess &process) {
  return Object{
      {"pid", process.pid},
      {"triple", process.triple},
      {"threads", process.threads},
      {"modules", process.modules},
  };
}

bool fromJSON(const json::Value &value, JSONProcess &process, Path path) {
  ObjectMapper o(value, path);
  return o && o.map("pid", process.pid) && o.map("triple", process.triple) &&
         o.map("threads", process.threads) && o.map("modules", process.modules);
}

json::Value toJSON(const JSONCpu &cpu) {
  return Object{
      {"id", cpu.id},
      {"iptTrace", cpu.ipt_trace},
      {"contextSwitchTrace", cpu.context_switch_trace},
  };
}

bool fromJSON(const json::Value &value, JSONCpu &cpu, Path path) {
  ObjectMapper o(value, path);
  uint64_t cpu_id;
  if (!(o && o.map("id", cpu_id) && o.map("iptTrace", cpu.ipt_trace) &&
        o.map("contextSwitchTrace", cpu.context_switch_trace)))
    return false;
  cpu.id = cpu_id;
  return true;
}

json::Value toJSON(const pt_cpu &cpu_info) {
  return Object{
      {"vendor", cpu_info.vendor == pcv_intel ? "GenuineIntel" : "Unknown"},
      {"family", cpu_info.family},
      {"model", cpu_info.model},
      {"stepping", cpu_info.stepping},
  };
}

bool fromJSON(const json::Value &value, pt_cpu &cpu_info, Path path) {
  ObjectMapper o(value, path);
  std::string vendor;
  uint64_t family, model, stepping;
  if (!(o && o.map("vendor", vendor) && o.map("family", family) &&
        o.map("model", model) && o.map("stepping", stepping)))
    return false;
  cpu_info.vendor = vendor == "GenuineIntel" ? pcv_intel : pcv_unknown;
  cpu_info.family = family;
  cpu_info.model = model;
  cpu_info.stepping = stepping;
  return true;
}

json::Value toJSON(const JSONKernel &kernel) {
  json::Object json_module;
  if (kernel.load_address)
    json_module["loadAddress"] = toJSON(*kernel.load_address, true);
  json_module["file"] = kernel.file;
  return std::move(json_module);
}

bool fromJSON(const json::Value &value, JSONKernel &kernel, Path path) {
  ObjectMapper o(value, path);
  return o && o.map("loadAddress", kernel.load_address) &&
         o.map("file", kernel.file);
}

json::Value toJSON(const JSONTraceBundleDescription &bundle_description) {
  return Object{
      {"type", bundle_description.type},
      {"processes", bundle_description.processes},
      // We have to do this because the compiler fails at doing it
      // automatically because pt_cpu is not in a namespace
      {"cpuInfo", toJSON(bundle_description.cpu_info)},
      {"cpus", bundle_description.cpus},
      {"tscPerfZeroConversion", bundle_description.tsc_perf_zero_conversion},
      {"kernel", bundle_description.kernel}};
}

bool fromJSON(const json::Value &value,
              JSONTraceBundleDescription &bundle_description, Path path) {
  ObjectMapper o(value, path);
  if (!(o && o.map("processes", bundle_description.processes) &&
        o.map("type", bundle_description.type) &&
        o.map("cpus", bundle_description.cpus) &&
        o.map("tscPerfZeroConversion",
              bundle_description.tsc_perf_zero_conversion) &&
        o.map("kernel", bundle_description.kernel)))
    return false;
  if (bundle_description.cpus && !bundle_description.tsc_perf_zero_conversion) {
    path.report(
        "\"tscPerfZeroConversion\" is required when \"cpus\" is provided");
    return false;
  }
  // We have to do this because the compiler fails at doing it automatically
  // because pt_cpu is not in a namespace
  if (!fromJSON(*value.getAsObject()->get("cpuInfo"),
                bundle_description.cpu_info, path.field("cpuInfo")))
    return false;

  // When kernel section is present, this is kernel-only tracing. Thus, throw an
  // error if the "processes" section is non-empty or the "cpus" section is not
  // present.
  if (bundle_description.kernel) {
    if (bundle_description.processes &&
        !bundle_description.processes->empty()) {
      path.report("\"processes\" must be empty when \"kernel\" is provided");
      return false;
    }
    if (!bundle_description.cpus) {
      path.report("\"cpus\" is required when \"kernel\" is provided");
      return false;
    }
  } else if (!bundle_description.processes) {
    // Usermode tracing requires processes section.
    path.report("\"processes\" is required when \"kernel\" is not provided");
    return false;
  }
  return true;
}

} // namespace trace_intel_pt
} // namespace lldb_private