aboutsummaryrefslogtreecommitdiff
path: root/lldb/source/Utility/TraceIntelPTGDBRemotePackets.cpp
blob: 7a0ed9c53c65870dd121bd4c60eee593d06ab834 (plain) (blame)
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
//===-- TraceIntelPTGDBRemotePackets.cpp ------------------------*- C++ -*-===//
//
// 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 "lldb/Utility/TraceIntelPTGDBRemotePackets.h"

using namespace llvm;
using namespace llvm::json;

namespace lldb_private {

const char *IntelPTDataKinds::kProcFsCpuInfo = "procfsCpuInfo";
const char *IntelPTDataKinds::kIptTrace = "iptTrace";
const char *IntelPTDataKinds::kPerfContextSwitchTrace =
    "perfContextSwitchTrace";

bool TraceIntelPTStartRequest::IsPerCpuTracing() const {
  return per_cpu_tracing.value_or(false);
}

json::Value toJSON(const JSONUINT64 &uint64, bool hex) {
  if (hex)
    return json::Value(formatv("{0:x+}", uint64.value));
  else
    return json::Value(formatv("{0}", uint64.value));
}

bool fromJSON(const json::Value &value, JSONUINT64 &uint64, Path path) {
  if (Optional<uint64_t> val = value.getAsUINT64()) {
    uint64.value = *val;
    return true;
  } else if (Optional<StringRef> val = value.getAsString()) {
    if (!val->getAsInteger(/*radix=*/0, uint64.value))
      return true;
    path.report("invalid string number");
  }
  path.report("invalid number or string number");
  return false;
}

bool fromJSON(const json::Value &value, TraceIntelPTStartRequest &packet,
              Path path) {
  ObjectMapper o(value, path);
  if (!(o && fromJSON(value, (TraceStartRequest &)packet, path) &&
        o.map("enableTsc", packet.enable_tsc) &&
        o.map("psbPeriod", packet.psb_period) &&
        o.map("iptTraceSize", packet.ipt_trace_size)))
    return false;

  if (packet.IsProcessTracing()) {
    if (!o.map("processBufferSizeLimit", packet.process_buffer_size_limit) ||
        !o.map("perCpuTracing", packet.per_cpu_tracing) ||
        !o.map("disableCgroupTracing", packet.disable_cgroup_filtering))
      return false;
  }
  return true;
}

json::Value toJSON(const TraceIntelPTStartRequest &packet) {
  json::Value base = toJSON((const TraceStartRequest &)packet);
  json::Object &obj = *base.getAsObject();
  obj.try_emplace("iptTraceSize", packet.ipt_trace_size);
  obj.try_emplace("processBufferSizeLimit", packet.process_buffer_size_limit);
  obj.try_emplace("psbPeriod", packet.psb_period);
  obj.try_emplace("enableTsc", packet.enable_tsc);
  obj.try_emplace("perCpuTracing", packet.per_cpu_tracing);
  obj.try_emplace("disableCgroupTracing", packet.disable_cgroup_filtering);
  return base;
}

uint64_t LinuxPerfZeroTscConversion::ToNanos(uint64_t tsc) const {
  uint64_t quot = tsc >> time_shift;
  uint64_t rem_flag = (((uint64_t)1 << time_shift) - 1);
  uint64_t rem = tsc & rem_flag;
  return time_zero.value + quot * time_mult + ((rem * time_mult) >> time_shift);
}

uint64_t LinuxPerfZeroTscConversion::ToTSC(uint64_t nanos) const {
  uint64_t time = nanos - time_zero.value;
  uint64_t quot = time / time_mult;
  uint64_t rem = time % time_mult;
  return (quot << time_shift) + (rem << time_shift) / time_mult;
}

json::Value toJSON(const LinuxPerfZeroTscConversion &packet) {
  return json::Value(json::Object{
      {"timeMult", packet.time_mult},
      {"timeShift", packet.time_shift},
      {"timeZero", toJSON(packet.time_zero, /*hex=*/false)},
  });
}

bool fromJSON(const json::Value &value, LinuxPerfZeroTscConversion &packet,
              json::Path path) {
  ObjectMapper o(value, path);
  uint64_t time_mult, time_shift;
  if (!(o && o.map("timeMult", time_mult) && o.map("timeShift", time_shift) &&
        o.map("timeZero", packet.time_zero)))
    return false;
  packet.time_mult = time_mult;
  packet.time_shift = time_shift;
  return true;
}

bool fromJSON(const json::Value &value, TraceIntelPTGetStateResponse &packet,
              json::Path path) {
  ObjectMapper o(value, path);
  return o && fromJSON(value, (TraceGetStateResponse &)packet, path) &&
         o.map("tscPerfZeroConversion", packet.tsc_perf_zero_conversion) &&
         o.map("usingCgroupFiltering", packet.using_cgroup_filtering);
}

json::Value toJSON(const TraceIntelPTGetStateResponse &packet) {
  json::Value base = toJSON((const TraceGetStateResponse &)packet);
  json::Object &obj = *base.getAsObject();
  obj.insert({"tscPerfZeroConversion", packet.tsc_perf_zero_conversion});
  obj.insert({"usingCgroupFiltering", packet.using_cgroup_filtering});
  return base;
}

} // namespace lldb_private