1 //===-- IntelPTCollector.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_IntelPTCollector_H_
10 #define liblldb_IntelPTCollector_H_
11 
12 #include "Perf.h"
13 
14 #include "IntelPTMultiCoreTrace.h"
15 #include "IntelPTPerThreadProcessTrace.h"
16 #include "IntelPTSingleBufferTrace.h"
17 
18 #include "lldb/Host/common/NativeProcessProtocol.h"
19 #include "lldb/Utility/Status.h"
20 #include "lldb/Utility/TraceIntelPTGDBRemotePackets.h"
21 #include "lldb/lldb-types.h"
22 
23 #include <linux/perf_event.h>
24 #include <sys/mman.h>
25 #include <unistd.h>
26 
27 namespace lldb_private {
28 
29 namespace process_linux {
30 
31 /// Main class that manages intel-pt process and thread tracing.
32 class IntelPTCollector {
33 public:
34   /// \param[in] process
35   ///     Process to be traced.
36   IntelPTCollector(NativeProcessProtocol &process);
37 
38   static bool IsSupported();
39 
40   /// To be invoked whenever the state of the target process has changed.
41   void OnProcessStateChanged(lldb::StateType state);
42 
43   /// If "process tracing" is enabled, then trace the given thread.
44   llvm::Error OnThreadCreated(lldb::tid_t tid);
45 
46   /// Stops tracing a tracing upon a destroy event.
47   llvm::Error OnThreadDestroyed(lldb::tid_t tid);
48 
49   /// Implementation of the jLLDBTraceStop packet
50   llvm::Error TraceStop(const TraceStopRequest &request);
51 
52   /// Implementation of the jLLDBTraceStart packet
53   llvm::Error TraceStart(const TraceIntelPTStartRequest &request);
54 
55   /// Implementation of the jLLDBTraceGetState packet
56   llvm::Expected<llvm::json::Value> GetState();
57 
58   /// Implementation of the jLLDBTraceGetBinaryData packet
59   llvm::Expected<std::vector<uint8_t>>
60   GetBinaryData(const TraceGetBinaryDataRequest &request);
61 
62   /// Dispose of all traces
63   void Clear();
64 
65 private:
66   llvm::Error TraceStop(lldb::tid_t tid);
67 
68   /// Start tracing a specific thread.
69   llvm::Error TraceStart(lldb::tid_t tid,
70                          const TraceIntelPTStartRequest &request);
71 
72   llvm::Expected<IntelPTSingleBufferTrace &> GetTracedThread(lldb::tid_t tid);
73 
74   bool IsProcessTracingEnabled() const;
75 
76   void ClearProcessTracing();
77 
78   NativeProcessProtocol &m_process;
79   /// Threads traced due to "thread tracing"
80   IntelPTThreadTraceCollection m_thread_traces;
81 
82   /// Only one of the following "process tracing" handlers can be active at a
83   /// given time.
84   ///
85   /// \{
86   /// Threads traced due to per-thread "process tracing".  This might be \b
87   /// nullptr.
88   IntelPTPerThreadProcessTraceUP m_per_thread_process_trace_up;
89   /// Cores traced due to per-core "process tracing".  This might be \b nullptr.
90   IntelPTMultiCoreTraceUP m_per_core_process_trace_up;
91   /// \}
92 
93   /// TSC to wall time conversion.
94   TraceTscConversionUP m_tsc_conversion;
95 };
96 
97 } // namespace process_linux
98 } // namespace lldb_private
99 
100 #endif // liblldb_IntelPTCollector_H_
101