1 //===-- IntelPTSingleBufferTrace.h ---------------------------- -*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_IntelPTSingleBufferTrace_H_
10 #define liblldb_IntelPTSingleBufferTrace_H_
13 #include "lldb/Utility/TraceIntelPTGDBRemotePackets.h"
14 #include "lldb/lldb-types.h"
15 #include "llvm/Support/Error.h"
18 namespace lldb_private
{
19 namespace process_linux
{
21 llvm::Expected
<uint32_t> GetIntelPTOSEventType();
23 /// This class wraps a single perf event collecting intel pt data in a single
25 class IntelPTSingleBufferTrace
{
27 /// Start tracing using a single Intel PT trace buffer.
29 /// \param[in] request
30 /// Intel PT configuration parameters.
33 /// The tid of the thread to be traced. If \b None, then this traces all
34 /// threads of all processes.
37 /// The CPU core id where to trace. If \b None, then this traces all CPUs.
39 /// \param[in] disabled
40 /// If \b true, then no data is collected until \a Resume is invoked.
41 /// Similarly, if \b false, data is collected right away until \a Pause is
44 /// \param[in] cgroup_fd
45 /// A file descriptor in /sys/fs associated with the cgroup of the process
46 /// to trace. If not \a std::nullopt, then the trace sesion will use cgroup
50 /// A \a IntelPTSingleBufferTrace instance if tracing was successful, or
51 /// an \a llvm::Error otherwise.
52 static llvm::Expected
<IntelPTSingleBufferTrace
>
53 Start(const TraceIntelPTStartRequest
&request
, std::optional
<lldb::tid_t
> tid
,
54 std::optional
<lldb::cpu_id_t
> cpu_id
= std::nullopt
,
55 bool disabled
= false, std::optional
<int> cgroup_fd
= std::nullopt
);
58 /// The bytes requested by a jLLDBTraceGetBinaryData packet that was routed
59 /// to this trace instace.
60 llvm::Expected
<std::vector
<uint8_t>>
61 GetBinaryData(const TraceGetBinaryDataRequest
&request
) const;
63 /// Read the intel pt trace buffer managed by this trace instance. To ensure
64 /// that the data is up-to-date and is not corrupted by read-write race
65 /// conditions, the underlying perf_event is paused during read, and later
66 /// it's returned to its initial state.
69 /// A vector with the requested binary data.
70 llvm::Expected
<std::vector
<uint8_t>> GetIptTrace();
73 /// The total the size in bytes used by the intel pt trace buffer managed
74 /// by this trace instance.
75 size_t GetIptTraceSize() const;
77 /// Resume the collection of this trace.
80 /// An error if the trace couldn't be resumed. If the trace is already
81 /// running, this returns \a Error::success().
84 /// Pause the collection of this trace.
87 /// An error if the trace couldn't be paused. If the trace is already
88 /// paused, this returns \a Error::success().
92 /// The underlying PerfEvent for this trace.
93 const PerfEvent
&GetPerfEvent() const;
96 /// Construct new \a IntelPTSingleBufferThreadTrace. Users are supposed to
97 /// create instances of this class via the \a Start() method and not invoke
98 /// this one directly.
100 /// \param[in] perf_event
101 /// perf event configured for IntelPT.
103 /// \param[in] collection_state
104 /// The initial collection state for the provided perf_event.
105 IntelPTSingleBufferTrace(PerfEvent
&&perf_event
)
106 : m_perf_event(std::move(perf_event
)) {}
108 /// perf event configured for IntelPT.
109 PerfEvent m_perf_event
;
112 } // namespace process_linux
113 } // namespace lldb_private
115 #endif // liblldb_IntelPTSingleBufferTrace_H_