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 "IntelPTMultiCoreTrace.h"
13 #include "IntelPTPerThreadProcessTrace.h"
14 #include "IntelPTSingleBufferTrace.h"
15 #include "Perf.h"
16 #include "lldb/Host/common/NativeProcessProtocol.h"
17 #include "lldb/Utility/Status.h"
18 #include "lldb/Utility/TraceIntelPTGDBRemotePackets.h"
19 #include "lldb/lldb-types.h"
20 #include <linux/perf_event.h>
21 #include <sys/mman.h>
22 #include <unistd.h>
23 
24 namespace lldb_private {
25 
26 namespace process_linux {
27 
28 /// Main class that manages intel-pt process and thread tracing.
29 class IntelPTCollector {
30 public:
31   /// \param[in] process
32   ///     Process to be traced.
33   IntelPTCollector(NativeProcessProtocol &process);
34 
35   static bool IsSupported();
36 
37   /// To be invoked as soon as we know the process stopped.
38   void ProcessDidStop();
39 
40   /// To be invoked before the process will resume, so that we can capture the
41   /// first instructions after the resume.
42   void ProcessWillResume();
43 
44   /// If "process tracing" is enabled, then trace the given thread.
45   llvm::Error OnThreadCreated(lldb::tid_t tid);
46 
47   /// Stops tracing a tracing upon a destroy event.
48   llvm::Error OnThreadDestroyed(lldb::tid_t tid);
49 
50   /// Implementation of the jLLDBTraceStop packet
51   llvm::Error TraceStop(const TraceStopRequest &request);
52 
53   /// Implementation of the jLLDBTraceStart packet
54   llvm::Error TraceStart(const TraceIntelPTStartRequest &request);
55 
56   /// Implementation of the jLLDBTraceGetState packet
57   llvm::Expected<llvm::json::Value> GetState();
58 
59   /// Implementation of the jLLDBTraceGetBinaryData packet
60   llvm::Expected<std::vector<uint8_t>>
61   GetBinaryData(const TraceGetBinaryDataRequest &request);
62 
63   /// Dispose of all traces
64   void Clear();
65 
66 private:
67   llvm::Error TraceStop(lldb::tid_t tid);
68 
69   /// Start tracing a specific thread.
70   llvm::Error TraceStart(lldb::tid_t tid,
71                          const TraceIntelPTStartRequest &request);
72 
73   /// \return
74   ///   The conversion object between TSC and wall time.
75   llvm::Expected<LinuxPerfZeroTscConversion &>
76   FetchPerfTscConversionParameters();
77 
78   /// The target process.
79   NativeProcessProtocol &m_process;
80   /// Threads traced due to "thread tracing"
81   IntelPTThreadTraceCollection m_thread_traces;
82 
83   /// Only one instance of "process trace" can be active at a given time.
84   /// It might be \b nullptr.
85   IntelPTProcessTraceUP m_process_trace_up;
86 };
87 
88 } // namespace process_linux
89 } // namespace lldb_private
90 
91 #endif // liblldb_IntelPTCollector_H_
92