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 as soon as we know the process stopped.
41   void ProcessDidStop();
42 
43   /// To be invoked before the process will resume, so that we can capture the
44   /// first instructions after the resume.
45   void ProcessWillResume();
46 
47   /// If "process tracing" is enabled, then trace the given thread.
48   llvm::Error OnThreadCreated(lldb::tid_t tid);
49 
50   /// Stops tracing a tracing upon a destroy event.
51   llvm::Error OnThreadDestroyed(lldb::tid_t tid);
52 
53   /// Implementation of the jLLDBTraceStop packet
54   llvm::Error TraceStop(const TraceStopRequest &request);
55 
56   /// Implementation of the jLLDBTraceStart packet
57   llvm::Error TraceStart(const TraceIntelPTStartRequest &request);
58 
59   /// Implementation of the jLLDBTraceGetState packet
60   llvm::Expected<llvm::json::Value> GetState();
61 
62   /// Implementation of the jLLDBTraceGetBinaryData packet
63   llvm::Expected<std::vector<uint8_t>>
64   GetBinaryData(const TraceGetBinaryDataRequest &request);
65 
66   /// Dispose of all traces
67   void Clear();
68 
69 private:
70   llvm::Error TraceStop(lldb::tid_t tid);
71 
72   /// Start tracing a specific thread.
73   llvm::Error TraceStart(lldb::tid_t tid,
74                          const TraceIntelPTStartRequest &request);
75 
76   /// \return
77   ///   The conversion object between TSC and wall time.
78   llvm::Expected<LinuxPerfZeroTscConversion &>
79   FetchPerfTscConversionParameters();
80 
81   /// The target process.
82   NativeProcessProtocol &m_process;
83   /// Threads traced due to "thread tracing"
84   IntelPTThreadTraceCollection m_thread_traces;
85 
86   /// Only one instance of "process trace" can be active at a given time.
87   /// It might be \b nullptr.
88   IntelPTProcessTraceUP m_process_trace_up;
89 };
90 
91 } // namespace process_linux
92 } // namespace lldb_private
93 
94 #endif // liblldb_IntelPTCollector_H_
95