1*f6aab3d8Srobert //===-- IntelPTThreadTraceCollection.h ------------------------ -*- C++ -*-===//
2*f6aab3d8Srobert //
3*f6aab3d8Srobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*f6aab3d8Srobert // See https://llvm.org/LICENSE.txt for license information.
5*f6aab3d8Srobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*f6aab3d8Srobert //
7*f6aab3d8Srobert //===----------------------------------------------------------------------===//
8*f6aab3d8Srobert 
9*f6aab3d8Srobert #ifndef liblldb_IntelPTPerThreadTraceCollection_H_
10*f6aab3d8Srobert #define liblldb_IntelPTPerThreadTraceCollection_H_
11*f6aab3d8Srobert 
12*f6aab3d8Srobert #include "IntelPTSingleBufferTrace.h"
13*f6aab3d8Srobert #include <optional>
14*f6aab3d8Srobert 
15*f6aab3d8Srobert namespace lldb_private {
16*f6aab3d8Srobert namespace process_linux {
17*f6aab3d8Srobert 
18*f6aab3d8Srobert /// Manages a list of thread traces.
19*f6aab3d8Srobert class IntelPTThreadTraceCollection {
20*f6aab3d8Srobert public:
IntelPTThreadTraceCollection()21*f6aab3d8Srobert   IntelPTThreadTraceCollection() {}
22*f6aab3d8Srobert 
23*f6aab3d8Srobert   /// Dispose of all traces
24*f6aab3d8Srobert   void Clear();
25*f6aab3d8Srobert 
26*f6aab3d8Srobert   /// \return
27*f6aab3d8Srobert   ///   \b true if and only if this instance of tracing the provided \p tid.
28*f6aab3d8Srobert   bool TracesThread(lldb::tid_t tid) const;
29*f6aab3d8Srobert 
30*f6aab3d8Srobert   /// \return
31*f6aab3d8Srobert   ///   The total sum of the intel pt trace buffer sizes used by this
32*f6aab3d8Srobert   ///   collection.
33*f6aab3d8Srobert   size_t GetTotalBufferSize() const;
34*f6aab3d8Srobert 
35*f6aab3d8Srobert   /// Execute the provided callback on each thread that is being traced.
36*f6aab3d8Srobert   ///
37*f6aab3d8Srobert   /// \param[in] callback.tid
38*f6aab3d8Srobert   ///   The id of the thread that is being traced.
39*f6aab3d8Srobert   ///
40*f6aab3d8Srobert   /// \param[in] callback.core_trace
41*f6aab3d8Srobert   ///   The single-buffer trace instance for the given core.
42*f6aab3d8Srobert   void ForEachThread(std::function<void(lldb::tid_t tid,
43*f6aab3d8Srobert                                         IntelPTSingleBufferTrace &thread_trace)>
44*f6aab3d8Srobert                          callback);
45*f6aab3d8Srobert 
46*f6aab3d8Srobert   llvm::Expected<IntelPTSingleBufferTrace &> GetTracedThread(lldb::tid_t tid);
47*f6aab3d8Srobert 
48*f6aab3d8Srobert   /// Start tracing the thread given by its \p tid.
49*f6aab3d8Srobert   ///
50*f6aab3d8Srobert   /// \return
51*f6aab3d8Srobert   ///   An error if the operation failed.
52*f6aab3d8Srobert   llvm::Error TraceStart(lldb::tid_t tid,
53*f6aab3d8Srobert                          const TraceIntelPTStartRequest &request);
54*f6aab3d8Srobert 
55*f6aab3d8Srobert   /// Stop tracing the thread given by its \p tid.
56*f6aab3d8Srobert   ///
57*f6aab3d8Srobert   /// \return
58*f6aab3d8Srobert   ///   An error if the given thread is not being traced or tracing couldn't be
59*f6aab3d8Srobert   ///   stopped.
60*f6aab3d8Srobert   llvm::Error TraceStop(lldb::tid_t tid);
61*f6aab3d8Srobert 
62*f6aab3d8Srobert   size_t GetTracedThreadsCount() const;
63*f6aab3d8Srobert 
64*f6aab3d8Srobert   /// \copydoc IntelPTProcessTrace::TryGetBinaryData()
65*f6aab3d8Srobert   llvm::Expected<std::optional<std::vector<uint8_t>>>
66*f6aab3d8Srobert   TryGetBinaryData(const TraceGetBinaryDataRequest &request);
67*f6aab3d8Srobert 
68*f6aab3d8Srobert private:
69*f6aab3d8Srobert   llvm::DenseMap<lldb::tid_t, IntelPTSingleBufferTrace> m_thread_traces;
70*f6aab3d8Srobert   /// Total actual thread buffer size in bytes
71*f6aab3d8Srobert   size_t m_total_buffer_size = 0;
72*f6aab3d8Srobert };
73*f6aab3d8Srobert 
74*f6aab3d8Srobert } // namespace process_linux
75*f6aab3d8Srobert } // namespace lldb_private
76*f6aab3d8Srobert 
77*f6aab3d8Srobert #endif // liblldb_IntelPTPerThreadTraceCollection_H_
78