1 //===-- TraceIntelPTGDBRemotePackets.cpp ------------------------*- C++ -*-===//
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 "lldb/Utility/TraceIntelPTGDBRemotePackets.h"
10 
11 using namespace llvm;
12 using namespace llvm::json;
13 
14 namespace lldb_private {
15 
16 const char *IntelPTDataKinds::kProcFsCpuInfo = "procfsCpuInfo";
17 const char *IntelPTDataKinds::kTraceBuffer = "traceBuffer";
18 const char *IntelPTDataKinds::kPerfContextSwitchTrace =
19     "perfContextSwitchTrace";
20 
21 bool TraceIntelPTStartRequest::IsPerCoreTracing() const {
22   return per_core_tracing.getValueOr(false);
23 }
24 
25 bool fromJSON(const json::Value &value, TraceIntelPTStartRequest &packet,
26               Path path) {
27   ObjectMapper o(value, path);
28   if (!o || !fromJSON(value, (TraceStartRequest &)packet, path) ||
29       !o.map("enableTsc", packet.enable_tsc) ||
30       !o.map("psbPeriod", packet.psb_period) ||
31       !o.map("traceBufferSize", packet.trace_buffer_size))
32     return false;
33 
34   if (packet.IsProcessTracing()) {
35     if (!o.map("processBufferSizeLimit", packet.process_buffer_size_limit) ||
36         !o.map("perCoreTracing", packet.per_core_tracing))
37       return false;
38   }
39   return true;
40 }
41 
42 json::Value toJSON(const TraceIntelPTStartRequest &packet) {
43   json::Value base = toJSON((const TraceStartRequest &)packet);
44   json::Object &obj = *base.getAsObject();
45   obj.try_emplace("traceBufferSize", packet.trace_buffer_size);
46   obj.try_emplace("processBufferSizeLimit", packet.process_buffer_size_limit);
47   obj.try_emplace("psbPeriod", packet.psb_period);
48   obj.try_emplace("enableTsc", packet.enable_tsc);
49   obj.try_emplace("perCoreTracing", packet.per_core_tracing);
50   return base;
51 }
52 
53 uint64_t LinuxPerfZeroTscConversion::ToNanos(uint64_t tsc) const {
54   uint64_t quot = tsc >> time_shift;
55   uint64_t rem_flag = (((uint64_t)1 << time_shift) - 1);
56   uint64_t rem = tsc & rem_flag;
57   return time_zero + quot * time_mult + ((rem * time_mult) >> time_shift);
58 }
59 
60 uint64_t LinuxPerfZeroTscConversion::ToTSC(uint64_t nanos) const {
61   uint64_t time = nanos - time_zero;
62   uint64_t quot = time / time_mult;
63   uint64_t rem = time % time_mult;
64   return (quot << time_shift) + (rem << time_shift) / time_mult;
65 }
66 
67 json::Value toJSON(const LinuxPerfZeroTscConversion &packet) {
68   return json::Value(json::Object{
69       {"timeMult", packet.time_mult},
70       {"timeShift", packet.time_shift},
71       {"timeZero", packet.time_zero},
72   });
73 }
74 
75 bool fromJSON(const json::Value &value, LinuxPerfZeroTscConversion &packet,
76               json::Path path) {
77   ObjectMapper o(value, path);
78   uint64_t time_mult, time_shift, time_zero;
79   if (!o || !o.map("timeMult", time_mult) || !o.map("timeShift", time_shift) ||
80       !o.map("timeZero", time_zero))
81     return false;
82   packet.time_mult = time_mult;
83   packet.time_zero = time_zero;
84   packet.time_shift = time_shift;
85   return true;
86 }
87 
88 bool fromJSON(const json::Value &value, TraceIntelPTGetStateResponse &packet,
89               json::Path path) {
90   ObjectMapper o(value, path);
91   return o && fromJSON(value, (TraceGetStateResponse &)packet, path) &&
92          o.map("tscPerfZeroConversion", packet.tsc_perf_zero_conversion);
93 }
94 
95 json::Value toJSON(const TraceIntelPTGetStateResponse &packet) {
96   json::Value base = toJSON((const TraceGetStateResponse &)packet);
97   base.getAsObject()->insert(
98       {"tscPerfZeroConversion", packet.tsc_perf_zero_conversion});
99   return base;
100 }
101 
102 } // namespace lldb_private
103