1 //===-- TraceIntelPT.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 LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TRACEINTELPT_H 10 #define LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TRACEINTELPT_H 11 12 #include "IntelPTDecoder.h" 13 #include "TraceIntelPTSessionFileParser.h" 14 #include "lldb/Utility/FileSpec.h" 15 #include "lldb/lldb-types.h" 16 #include "llvm/Support/raw_ostream.h" 17 18 namespace lldb_private { 19 namespace trace_intel_pt { 20 21 class TraceIntelPT : public Trace { 22 public: 23 void Dump(Stream *s) const override; 24 25 llvm::Error SaveLiveTraceToDisk(FileSpec directory) override; 26 27 ~TraceIntelPT() override = default; 28 29 /// PluginInterface protocol 30 /// \{ 31 llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); } 32 33 static void Initialize(); 34 35 static void Terminate(); 36 37 /// Create an instance of this class. 38 /// 39 /// \param[in] trace_session_file 40 /// The contents of the trace session file. See \a Trace::FindPlugin. 41 /// 42 /// \param[in] session_file_dir 43 /// The path to the directory that contains the session file. It's used to 44 /// resolved relative paths in the session file. 45 /// 46 /// \param[in] debugger 47 /// The debugger instance where new Targets will be created as part of the 48 /// JSON data parsing. 49 /// 50 /// \return 51 /// A trace instance or an error in case of failures. 52 static llvm::Expected<lldb::TraceSP> 53 CreateInstanceForSessionFile(const llvm::json::Value &trace_session_file, 54 llvm::StringRef session_file_dir, 55 Debugger &debugger); 56 57 static llvm::Expected<lldb::TraceSP> 58 CreateInstanceForLiveProcess(Process &process); 59 60 static llvm::StringRef GetPluginNameStatic() { return "intel-pt"; } 61 /// \} 62 63 lldb::CommandObjectSP 64 GetProcessTraceStartCommand(CommandInterpreter &interpreter) override; 65 66 lldb::CommandObjectSP 67 GetThreadTraceStartCommand(CommandInterpreter &interpreter) override; 68 69 llvm::StringRef GetSchema() override; 70 71 lldb::TraceCursorUP GetCursor(Thread &thread) override; 72 73 void DumpTraceInfo(Thread &thread, Stream &s, bool verbose) override; 74 75 llvm::Optional<size_t> GetRawTraceSize(Thread &thread); 76 77 void DoRefreshLiveProcessState( 78 llvm::Expected<TraceGetStateResponse> state) override; 79 80 bool IsTraced(lldb::tid_t tid) override; 81 82 const char *GetStartConfigurationHelp() override; 83 84 /// Start tracing a live process. 85 /// 86 /// \param[in] thread_buffer_size 87 /// Trace size per thread in bytes. 88 /// 89 /// \param[in] total_buffer_size_limit 90 /// Maximum total trace size per process in bytes. 91 /// More information in TraceIntelPT::GetStartConfigurationHelp(). 92 /// 93 /// \param[in] enable_tsc 94 /// Whether to use enable TSC timestamps or not. 95 /// More information in TraceIntelPT::GetStartConfigurationHelp(). 96 /// 97 /// \param[in] psb_period 98 /// 99 /// This value defines the period in which PSB packets will be generated. 100 /// More information in TraceIntelPT::GetStartConfigurationHelp(); 101 /// 102 /// \return 103 /// \a llvm::Error::success if the operation was successful, or 104 /// \a llvm::Error otherwise. 105 llvm::Error Start(size_t thread_buffer_size, size_t total_buffer_size_limit, 106 bool enable_tsc, llvm::Optional<size_t> psb_period); 107 108 /// \copydoc Trace::Start 109 llvm::Error Start(StructuredData::ObjectSP configuration = 110 StructuredData::ObjectSP()) override; 111 112 /// Start tracing live threads. 113 /// 114 /// \param[in] tids 115 /// Threads to trace. 116 /// 117 /// \param[in] thread_buffer_size 118 /// Trace size per thread in bytes. 119 /// 120 /// \param[in] enable_tsc 121 /// Whether to use enable TSC timestamps or not. 122 /// More information in TraceIntelPT::GetStartConfigurationHelp(). 123 /// 124 /// \param[in] psb_period 125 /// 126 /// This value defines the period in which PSB packets will be generated. 127 /// More information in TraceIntelPT::GetStartConfigurationHelp(). 128 /// 129 /// \return 130 /// \a llvm::Error::success if the operation was successful, or 131 /// \a llvm::Error otherwise. 132 llvm::Error Start(llvm::ArrayRef<lldb::tid_t> tids, size_t thread_buffer_size, 133 bool enable_tsc, llvm::Optional<size_t> psb_period); 134 135 /// \copydoc Trace::Start 136 llvm::Error Start(llvm::ArrayRef<lldb::tid_t> tids, 137 StructuredData::ObjectSP configuration = 138 StructuredData::ObjectSP()) override; 139 140 /// Get the thread buffer content for a live thread 141 llvm::Expected<std::vector<uint8_t>> GetLiveThreadBuffer(lldb::tid_t tid); 142 143 llvm::Expected<pt_cpu> GetCPUInfo(); 144 145 /// Get the current traced live process. 146 /// 147 /// \return 148 /// The current traced live process. If it's not a live process, 149 /// return \a nullptr. 150 Process *GetLiveProcess(); 151 152 private: 153 friend class TraceIntelPTSessionFileParser; 154 155 llvm::Expected<pt_cpu> GetCPUInfoForLiveProcess(); 156 157 /// \param[in] trace_threads 158 /// ThreadTrace instances, which are not live-processes and whose trace 159 /// files are fixed. 160 TraceIntelPT( 161 const pt_cpu &cpu_info, 162 const std::vector<lldb::ThreadPostMortemTraceSP> &traced_threads); 163 164 /// Constructor for live processes 165 TraceIntelPT(Process &live_process) 166 : Trace(live_process), m_thread_decoders(){}; 167 168 /// Decode the trace of the given thread that, i.e. recontruct the traced 169 /// instructions. 170 /// 171 /// \param[in] thread 172 /// If \a thread is a \a ThreadTrace, then its internal trace file will be 173 /// decoded. Live threads are not currently supported. 174 /// 175 /// \return 176 /// A \a DecodedThread shared pointer with the decoded instructions. Any 177 /// errors are embedded in the instruction list. 178 DecodedThreadSP Decode(Thread &thread); 179 180 /// It is provided by either a session file or a live process' "cpuInfo" 181 /// binary data. 182 llvm::Optional<pt_cpu> m_cpu_info; 183 std::map<lldb::tid_t, std::unique_ptr<ThreadDecoder>> m_thread_decoders; 184 /// Error gotten after a failed live process update, if any. 185 llvm::Optional<std::string> m_live_refresh_error; 186 }; 187 188 } // namespace trace_intel_pt 189 } // namespace lldb_private 190 191 #endif // LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TRACEINTELPT_H 192