1 //===-- TraceCursorIntelPT.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_TRACECURSORINTELPT_H
10 #define LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TRACECURSORINTELPT_H
11 
12 #include "ThreadDecoder.h"
13 #include <optional>
14 
15 namespace lldb_private {
16 namespace trace_intel_pt {
17 
18 class TraceCursorIntelPT : public TraceCursor {
19 public:
20   TraceCursorIntelPT(
21       lldb::ThreadSP thread_sp, DecodedThreadSP decoded_thread_sp,
22       const std::optional<LinuxPerfZeroTscConversion> &tsc_conversion,
23       std::optional<uint64_t> beginning_of_time_nanos);
24 
25   bool Seek(int64_t offset, lldb::TraceCursorSeekType origin) override;
26 
27   void Next() override;
28 
29   bool HasValue() const override;
30 
31   const char *GetError() const override;
32 
33   lldb::addr_t GetLoadAddress() const override;
34 
35   lldb::TraceEvent GetEventType() const override;
36 
37   lldb::cpu_id_t GetCPU() const override;
38 
39   std::optional<uint64_t> GetHWClock() const override;
40 
41   lldb::TraceItemKind GetItemKind() const override;
42 
43   bool GoToId(lldb::user_id_t id) override;
44 
45   lldb::user_id_t GetId() const override;
46 
47   bool HasId(lldb::user_id_t id) const override;
48 
49   std::optional<double> GetWallClockTime() const override;
50 
51   std::optional<std::string> GetSyncPointMetadata() const override;
52 
53 private:
54   /// Clear the current TSC and nanoseconds ranges if after moving they are not
55   /// valid anymore.
56   void ClearTimingRangesIfInvalid();
57 
58   /// Get or calculate the TSC range that includes the current trace item.
59   const std::optional<DecodedThread::TSCRange> &GetTSCRange() const;
60 
61   /// Get or calculate the TSC range that includes the current trace item.
62   const std::optional<DecodedThread::NanosecondsRange> &
63   GetNanosecondsRange() const;
64 
65   /// Storage of the actual instructions
66   DecodedThreadSP m_decoded_thread_sp;
67   /// Internal instruction index currently pointing at.
68   int64_t m_pos;
69 
70   /// Timing information and cached values.
71   /// \{
72 
73   /// TSC -> nanos conversion utility. \a std::nullopt if not available at all.
74   std::optional<LinuxPerfZeroTscConversion> m_tsc_conversion;
75   /// Lowest nanoseconds timestamp seen in any thread trace, \a std::nullopt if
76   /// not available at all.
77   std::optional<uint64_t> m_beginning_of_time_nanos;
78   /// Range of trace items with the same TSC that includes the current trace
79   /// item, \a std::nullopt if not calculated or not available.
80   std::optional<DecodedThread::TSCRange> mutable m_tsc_range;
81   bool mutable m_tsc_range_calculated = false;
82   /// Range of trace items with the same non-interpolated timestamps in
83   /// nanoseconds that includes the current trace item, \a std::nullopt if not
84   /// calculated or not available.
85   std::optional<DecodedThread::NanosecondsRange> mutable m_nanoseconds_range;
86   bool mutable m_nanoseconds_range_calculated = false;
87   /// \}
88 };
89 
90 } // namespace trace_intel_pt
91 } // namespace lldb_private
92 
93 #endif // LLDB_SOURCE_PLUGINS_TRACE_INTEL_PT_TRACECURSORINTELPT_H
94