1 //===-- TraceIntelPTGDBRemotePackets.h --------------------------*- 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 #ifndef LLDB_UTILITY_TRACEINTELPTGDBREMOTEPACKETS_H
10 #define LLDB_UTILITY_TRACEINTELPTGDBREMOTEPACKETS_H
11 
12 #include "lldb/Utility/TraceGDBRemotePackets.h"
13 
14 #include "llvm/Support/JSON.h"
15 
16 #include <chrono>
17 
18 /// See docs/lldb-gdb-remote.txt for more information.
19 ///
20 /// Do not use system-dependent types, like size_t, because they might cause
21 /// issues when compiling on arm.
22 namespace lldb_private {
23 
24 // List of data kinds used by jLLDBGetState and jLLDBGetBinaryData.
25 struct IntelPTDataKinds {
26   static const char *kProcFsCpuInfo;
27   static const char *kIptTrace;
28   static const char *kPerfContextSwitchTrace;
29 };
30 
31 /// jLLDBTraceStart gdb-remote packet
32 /// \{
33 struct TraceIntelPTStartRequest : TraceStartRequest {
34   /// Size in bytes to use for each thread's trace buffer.
35   uint64_t ipt_trace_size;
36 
37   /// Whether to enable TSC
38   bool enable_tsc;
39 
40   /// PSB packet period
41   llvm::Optional<uint64_t> psb_period;
42 
43   /// Required when doing "process tracing".
44   ///
45   /// Limit in bytes on all the thread traces started by this "process trace"
46   /// instance. When a thread is about to be traced and the limit would be hit,
47   /// then a "tracing" stop event is triggered.
48   llvm::Optional<uint64_t> process_buffer_size_limit;
49 
50   /// Whether to have a trace buffer per thread or per cpu cpu.
51   llvm::Optional<bool> per_cpu_tracing;
52 
53   /// Disable the cgroup filtering that is automatically applied in per cpu
54   /// mode.
55   llvm::Optional<bool> disable_cgroup_filtering;
56 
57   bool IsPerCpuTracing() const;
58 };
59 
60 bool fromJSON(const llvm::json::Value &value, TraceIntelPTStartRequest &packet,
61               llvm::json::Path path);
62 
63 llvm::json::Value toJSON(const TraceIntelPTStartRequest &packet);
64 /// \}
65 
66 /// Helper structure to help parse long numbers that can't
67 /// be easily represented by a JSON number that is compatible with
68 /// Javascript (52 bits) or that can also be represented as hex.
69 ///
70 /// \{
71 struct JSONUINT64 {
72   uint64_t value;
73 };
74 
75 llvm::json::Value toJSON(const JSONUINT64 &uint64, bool hex);
76 
77 bool fromJSON(const llvm::json::Value &value, JSONUINT64 &uint64,
78               llvm::json::Path path);
79 /// \}
80 
81 /// jLLDBTraceGetState gdb-remote packet
82 /// \{
83 
84 /// TSC to wall time conversion values defined in the Linux perf_event_open API
85 /// when the capibilities cap_user_time and cap_user_time_zero are set. See the
86 /// See the documentation of `time_zero` in
87 /// https://man7.org/linux/man-pages/man2/perf_event_open.2.html for more
88 /// information.
89 struct LinuxPerfZeroTscConversion {
90   /// Convert TSC value to nanosecond wall time. The beginning of time (0
91   /// nanoseconds) is defined by the kernel at boot time and has no particularly
92   /// useful meaning. On the other hand, this value is constant for an entire
93   /// trace session.
94   /// See 'time_zero' section of
95   /// https://man7.org/linux/man-pages/man2/perf_event_open.2.html
96   ///
97   /// \param[in] tsc
98   ///   The TSC value to be converted.
99   ///
100   /// \return
101   ///   Nanosecond wall time.
102   uint64_t ToNanos(uint64_t tsc) const;
103 
104   uint64_t ToTSC(uint64_t nanos) const;
105 
106   uint32_t time_mult;
107   uint16_t time_shift;
108   JSONUINT64 time_zero;
109 };
110 
111 struct TraceIntelPTGetStateResponse : TraceGetStateResponse {
112   /// The TSC to wall time conversion if it exists, otherwise \b nullptr.
113   llvm::Optional<LinuxPerfZeroTscConversion> tsc_perf_zero_conversion;
114   bool using_cgroup_filtering = false;
115 };
116 
117 bool fromJSON(const llvm::json::Value &value,
118               LinuxPerfZeroTscConversion &packet, llvm::json::Path path);
119 
120 llvm::json::Value toJSON(const LinuxPerfZeroTscConversion &packet);
121 
122 bool fromJSON(const llvm::json::Value &value,
123               TraceIntelPTGetStateResponse &packet, llvm::json::Path path);
124 
125 llvm::json::Value toJSON(const TraceIntelPTGetStateResponse &packet);
126 /// \}
127 
128 } // namespace lldb_private
129 
130 #endif // LLDB_UTILITY_TRACEINTELPTGDBREMOTEPACKETS_H
131