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   CreateInstance(const llvm::json::Value &trace_session_file,
49                  llvm::StringRef session_file_dir, Debugger &debugger);
50 
51   static ConstString GetPluginNameStatic();
52 
53   uint32_t GetPluginVersion() override;
54   /// \}
55 
56   llvm::StringRef GetSchema() override;
57 
58   void TraverseInstructions(
59       const Thread &thread, size_t position, TraceDirection direction,
60       std::function<bool(size_t index, llvm::Expected<lldb::addr_t> load_addr)>
61           callback) override;
62 
63   size_t GetInstructionCount(const Thread &thread) override;
64 
65   size_t GetCursorPosition(const Thread &thread) override;
66 
67 private:
68   friend class TraceIntelPTSessionFileParser;
69 
70   /// \param[in] trace_threads
71   ///     ThreadTrace instances, which are not live-processes and whose trace
72   ///     files are fixed.
73   TraceIntelPT(const pt_cpu &pt_cpu,
74                const std::vector<std::shared_ptr<ThreadTrace>> &traced_threads);
75 
76   /// Decode the trace of the given thread that, i.e. recontruct the traced
77   /// instructions. That trace must be managed by this class.
78   ///
79   /// \param[in] thread
80   ///     If \a thread is a \a ThreadTrace, then its internal trace file will be
81   ///     decoded. Live threads are not currently supported.
82   ///
83   /// \return
84   ///     A \a DecodedThread instance if decoding was successful, or a \b
85   ///     nullptr if the thread's trace is not managed by this class.
86   const DecodedThread *Decode(const Thread &thread);
87 
88   pt_cpu m_pt_cpu;
89   std::map<std::pair<lldb::pid_t, lldb::tid_t>, ThreadTraceDecoder>
90       m_trace_threads;
91 };
92 
93 } // namespace trace_intel_pt
94 } // namespace lldb_private
95 
96 #endif // LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TRACEINTELPT_H
97