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