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