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