1 //===-- IntelPTSingleBufferTrace.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 liblldb_IntelPTSingleBufferTrace_H_
10 #define liblldb_IntelPTSingleBufferTrace_H_
11 
12 #include "Perf.h"
13 
14 #include "lldb/Utility/TraceIntelPTGDBRemotePackets.h"
15 #include "lldb/lldb-types.h"
16 
17 #include "llvm/Support/Error.h"
18 
19 #include <memory>
20 
21 namespace lldb_private {
22 namespace process_linux {
23 
24 llvm::Expected<uint32_t> GetIntelPTOSEventType();
25 
26 class IntelPTTrace;
27 class IntelPTSingleBufferTrace;
28 
29 using IntelPTThreadTraceUP = std::unique_ptr<IntelPTTrace>;
30 using IntelPTSingleBufferTraceUP = std::unique_ptr<IntelPTSingleBufferTrace>;
31 
32 /// This class wraps a single perf event collecting intel pt data in a single
33 /// buffer.
34 class IntelPTSingleBufferTrace {
35 public:
36   /// Start tracing using a single Intel PT trace buffer.
37   ///
38   /// \param[in] request
39   ///     Intel PT configuration parameters.
40   ///
41   /// \param[in] tid
42   ///     The tid of the thread to be traced.
43   ///
44   /// \return
45   ///   A \a IntelPTSingleBufferTrace instance if tracing was successful, or
46   ///   an \a llvm::Error otherwise.
47   static llvm::Expected<IntelPTSingleBufferTraceUP>
48   Start(const TraceIntelPTStartRequest &request, lldb::tid_t tid);
49 
50   /// \return
51   ///    The bytes requested by a jLLDBTraceGetBinaryData packet that was routed
52   ///    to this trace instace.
53   llvm::Expected<std::vector<uint8_t>>
54   GetBinaryData(const TraceGetBinaryDataRequest &request) const;
55 
56   /// Read the trace buffer managed by this trace instance.
57   ///
58   /// \param[in] offset
59   ///     Offset of the data to read.
60   ///
61   /// \param[in] size
62   ///     Number of bytes to read.
63   ///
64   /// \return
65   ///     A vector with the requested binary data. The vector will have the
66   ///     size of the requested \a size. Non-available positions will be
67   ///     filled with zeroes.
68   llvm::Expected<std::vector<uint8_t>> GetTraceBuffer(size_t offset,
69                                                       size_t size) const;
70 
71   /// \return
72   ///   The total the size in bytes used by the trace buffer managed by this
73   ///   trace instance.
74   size_t GetTraceBufferSize() const;
75 
76 private:
77   /// Construct new \a IntelPTSingleBufferThreadTrace. Users are supposed to
78   /// create instances of this class via the \a Start() method and not invoke
79   /// this one directly.
80   ///
81   /// \param[in] perf_event
82   ///   perf event configured for IntelPT.
83   ///
84   /// \param[in] tid
85   ///   The thread being traced.
86   IntelPTSingleBufferTrace(PerfEvent &&perf_event, lldb::tid_t tid)
87       : m_perf_event(std::move(perf_event)) {}
88 
89   /// perf event configured for IntelPT.
90   PerfEvent m_perf_event;
91 };
92 
93 } // namespace process_linux
94 } // namespace lldb_private
95 
96 #endif // liblldb_IntelPTSingleBufferTrace_H_
97