LLDB  mainline
TraceIntelPT.h
Go to the documentation of this file.
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"
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>
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 
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
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.
151 
152 private:
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
lldb_private::trace_intel_pt::TraceIntelPT::GetPluginName
llvm::StringRef GetPluginName() override
PluginInterface protocol.
Definition: TraceIntelPT.h:31
TraceIntelPTSessionFileParser.h
lldb_private::trace_intel_pt::TraceIntelPT::DoRefreshLiveProcessState
void DoRefreshLiveProcessState(llvm::Expected< TraceGetStateResponse > state) override
Method to be overriden by the plug-in to refresh its own state.
Definition: TraceIntelPT.cpp:188
IntelPTDecoder.h
lldb_private::trace_intel_pt::TraceIntelPT::Start
llvm::Error Start(size_t thread_buffer_size, size_t total_buffer_size_limit, bool enable_tsc, llvm::Optional< size_t > psb_period)
Start tracing a live process.
lldb_private::Process
Definition: Process.h:341
lldb_private::trace_intel_pt::TraceIntelPT::GetSchema
llvm::StringRef GetSchema() override
Definition: TraceIntelPT.cpp:52
lldb_private::trace_intel_pt::TraceIntelPT
Definition: TraceIntelPT.h:21
lldb_private::trace_intel_pt::TraceIntelPT::Dump
void Dump(Stream *s) const override
Dump the trace data that this plug-in has access to.
Definition: TraceIntelPT.cpp:56
lldb_private::Stream
Definition: Stream.h:28
lldb_private::trace_intel_pt::TraceIntelPT::CreateInstanceForLiveProcess
static llvm::Expected< lldb::TraceSP > CreateInstanceForLiveProcess(Process &process)
Definition: TraceIntelPT.cpp:71
lldb_private::trace_intel_pt::TraceIntelPT::m_live_refresh_error
llvm::Optional< std::string > m_live_refresh_error
Error gotten after a failed live process update, if any.
Definition: TraceIntelPT.h:185
lldb_private::trace_intel_pt::TraceIntelPT::GetLiveThreadBuffer
llvm::Expected< std::vector< uint8_t > > GetLiveThreadBuffer(lldb::tid_t tid)
Get the thread buffer content for a live thread.
Definition: TraceIntelPT.cpp:338
lldb_private::trace_intel_pt::TraceIntelPT::GetStartConfigurationHelp
const char * GetStartConfigurationHelp() override
Definition: TraceIntelPT.cpp:214
lldb_private::Trace
Definition: Trace.h:46
lldb_private::FileSpec
Definition: FileSpec.h:56
lldb_private::trace_intel_pt::TraceIntelPT::GetCursor
lldb::TraceCursorUP GetCursor(Thread &thread) override
Get a TraceCursor for the given thread's trace.
Definition: TraceIntelPT.cpp:102
lldb_private::Thread
Definition: Thread.h:60
lldb_private::StructuredData::ObjectSP
std::shared_ptr< Object > ObjectSP
Definition: StructuredData.h:59
lldb_private::trace_intel_pt::TraceIntelPT::GetProcessTraceStartCommand
lldb::CommandObjectSP GetProcessTraceStartCommand(CommandInterpreter &interpreter) override
Get the command handle for the "process trace start" command.
Definition: TraceIntelPT.cpp:30
lldb_private::trace_intel_pt::TraceIntelPT::m_thread_decoders
std::map< lldb::tid_t, std::unique_ptr< ThreadDecoder > > m_thread_decoders
Definition: TraceIntelPT.h:183
lldb_private::trace_intel_pt::TraceIntelPT::CreateInstanceForSessionFile
static llvm::Expected< lldb::TraceSP > CreateInstanceForSessionFile(const llvm::json::Value &trace_session_file, llvm::StringRef session_file_dir, Debugger &debugger)
Create an instance of this class.
Definition: TraceIntelPT.cpp:63
lldb_private::CommandInterpreter
Definition: CommandInterpreter.h:214
lldb_private::trace_intel_pt::TraceIntelPT::GetRawTraceSize
llvm::Optional< size_t > GetRawTraceSize(Thread &thread)
Definition: TraceIntelPT.cpp:117
lldb_private::Debugger
Definition: Debugger.h:70
lldb_private::trace_intel_pt::TraceIntelPT::TraceIntelPT
TraceIntelPT(Process &live_process)
Constructor for live processes.
Definition: TraceIntelPT.h:165
lldb_private::trace_intel_pt::TraceIntelPT::IsTraced
bool IsTraced(lldb::tid_t tid) override
Check if a thread is currently traced by this object.
Definition: TraceIntelPT.cpp:205
lldb-types.h
lldb_private::trace_intel_pt::TraceIntelPT::GetThreadTraceStartCommand
lldb::CommandObjectSP GetThreadTraceStartCommand(CommandInterpreter &interpreter) override
Get the command handle for the "thread trace start" command.
Definition: TraceIntelPT.cpp:36
lldb_private::trace_intel_pt::TraceIntelPT::DumpTraceInfo
void DumpTraceInfo(Thread &thread, Stream &s, bool verbose) override
Dump general info about a given thread's trace.
Definition: TraceIntelPT.cpp:106
lldb_private::trace_intel_pt::TraceIntelPTSessionFileParser
Definition: TraceIntelPTSessionFileParser.h:21
lldb_private::trace_intel_pt::TraceIntelPT::GetLiveProcess
Process * GetLiveProcess()
Get the current traced live process.
Definition: TraceIntelPT.cpp:186
lldb_private::trace_intel_pt::TraceIntelPT::TraceIntelPT
TraceIntelPT(const pt_cpu &cpu_info, const std::vector< lldb::ThreadPostMortemTraceSP > &traced_threads)
lldb_private::trace_intel_pt::TraceIntelPT::~TraceIntelPT
~TraceIntelPT() override=default
lldb_private::trace_intel_pt::TraceIntelPT::GetCPUInfoForLiveProcess
llvm::Expected< pt_cpu > GetCPUInfoForLiveProcess()
Definition: TraceIntelPT.cpp:124
lldb_private::trace_intel_pt::DecodedThreadSP
std::shared_ptr< DecodedThread > DecodedThreadSP
Definition: DecodedThread.h:159
lldb_private::trace_intel_pt::TraceIntelPT::Terminate
static void Terminate()
Definition: TraceIntelPT.cpp:48
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::trace_intel_pt::TraceIntelPT::m_cpu_info
llvm::Optional< pt_cpu > m_cpu_info
It is provided by either a session file or a live process' "cpuInfo" binary data.
Definition: TraceIntelPT.h:182
lldb_private::trace_intel_pt::TraceIntelPT::Initialize
static void Initialize()
Definition: TraceIntelPT.cpp:41
Error
llvm::Error Error
Definition: UdtRecordCompleter.cpp:29
FileSpec.h
lldb_private::trace_intel_pt::TraceIntelPT::GetCPUInfo
llvm::Expected< pt_cpu > GetCPUInfo()
Definition: TraceIntelPT.cpp:176
lldb_private::trace_intel_pt::TraceIntelPT::GetPluginNameStatic
static llvm::StringRef GetPluginNameStatic()
Definition: TraceIntelPT.h:60
lldb_private::trace_intel_pt::TraceIntelPT::Decode
DecodedThreadSP Decode(Thread &thread)
Decode the trace of the given thread that, i.e.
Definition: TraceIntelPT.cpp:87
lldb_private::trace_intel_pt::TraceIntelPT::SaveLiveTraceToDisk
llvm::Error SaveLiveTraceToDisk(FileSpec directory) override
Save the trace of a live process to the specified directory, which will be created if needed.
Definition: TraceIntelPT.cpp:58
lldb::tid_t
uint64_t tid_t
Definition: lldb-types.h:86