LLDB mainline
TraceCursorIntelPT.cpp
Go to the documentation of this file.
1//===-- TraceCursorIntelPT.cpp --------------------------------------------===//
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
10#include "DecodedThread.h"
11#include "TraceIntelPT.h"
12#include <cstdlib>
13#include <optional>
14
15using namespace lldb;
16using namespace lldb_private;
17using namespace lldb_private::trace_intel_pt;
18using namespace llvm;
19
21 ThreadSP thread_sp, DecodedThreadSP decoded_thread_sp,
22 const std::optional<LinuxPerfZeroTscConversion> &tsc_conversion,
23 std::optional<uint64_t> beginning_of_time_nanos)
24 : TraceCursor(thread_sp), m_decoded_thread_sp(decoded_thread_sp),
25 m_tsc_conversion(tsc_conversion),
26 m_beginning_of_time_nanos(beginning_of_time_nanos) {
28}
29
31 m_pos += IsForwards() ? 1 : -1;
33}
34
37 if (!m_tsc_range || m_pos < 0 || !m_tsc_range->InRange(m_pos)) {
38 m_tsc_range = std::nullopt;
40 }
41 }
42
44 if (!m_nanoseconds_range || m_pos < 0 ||
45 !m_nanoseconds_range->InRange(m_pos)) {
46 m_nanoseconds_range = std::nullopt;
48 }
49 }
50}
51
52const std::optional<DecodedThread::TSCRange> &
56 m_tsc_range = m_decoded_thread_sp->GetTSCRangeByIndex(m_pos);
57 }
58 return m_tsc_range;
59}
60
61const std::optional<DecodedThread::NanosecondsRange> &
66 m_decoded_thread_sp->GetNanosecondsRangeByIndex(m_pos);
67 }
69}
70
71bool TraceCursorIntelPT::Seek(int64_t offset,
73 switch (origin) {
75 m_pos = offset;
76 break;
78 m_pos = m_decoded_thread_sp->GetItemsCount() - 1 + offset;
79 break;
81 m_pos += offset;
82 }
83
85
86 return HasValue();
87}
88
90 return m_pos >= 0 &&
91 static_cast<uint64_t>(m_pos) < m_decoded_thread_sp->GetItemsCount();
92}
93
95 return m_decoded_thread_sp->GetItemKindByIndex(m_pos);
96}
97
98llvm::StringRef TraceCursorIntelPT::GetError() const {
99 return m_decoded_thread_sp->GetErrorByIndex(m_pos);
100}
101
103 return m_decoded_thread_sp->GetInstructionLoadAddress(m_pos);
104}
105
106std::optional<uint64_t> TraceCursorIntelPT::GetHWClock() const {
107 if (const std::optional<DecodedThread::TSCRange> &range = GetTSCRange())
108 return range->tsc;
109 return std::nullopt;
110}
111
112std::optional<double> TraceCursorIntelPT::GetWallClockTime() const {
113 if (const std::optional<DecodedThread::NanosecondsRange> &range =
115 return range->GetInterpolatedTime(m_pos, *m_beginning_of_time_nanos,
117 return std::nullopt;
118}
119
121 return m_decoded_thread_sp->GetCPUByIndex(m_pos);
122}
123
125 return m_decoded_thread_sp->GetEventByIndex(m_pos);
126}
127
129 if (!HasId(id))
130 return false;
131 m_pos = id;
133 return true;
134}
135
137 return id < m_decoded_thread_sp->GetItemsCount();
138}
139
141
142std::optional<std::string> TraceCursorIntelPT::GetSyncPointMetadata() const {
143 return formatv("offset = 0x{0:x}",
144 m_decoded_thread_sp->GetSyncPointOffsetByIndex(m_pos))
145 .str();
146}
Class used for iterating over the instructions of a thread's trace, among other kinds of information.
Definition: TraceCursor.h:94
bool IsForwards() const
Check if the direction to use in the TraceCursor::Next() method is forwards.
Definition: TraceCursor.cpp:27
void ClearTimingRangesIfInvalid()
Clear the current TSC and nanoseconds ranges if after moving they are not valid anymore.
TraceCursorIntelPT(lldb::ThreadSP thread_sp, DecodedThreadSP decoded_thread_sp, const std::optional< LinuxPerfZeroTscConversion > &tsc_conversion, std::optional< uint64_t > beginning_of_time_nanos)
std::optional< uint64_t > GetHWClock() const override
Get the last hardware clock value that was emitted before the current trace item.
lldb::TraceItemKind GetItemKind() const override
Trace item information (instructions, errors and events)
std::optional< DecodedThread::TSCRange > m_tsc_range
Range of trace items with the same TSC that includes the current trace item, std::nullopt if not calc...
std::optional< double > GetWallClockTime() const override
Get the approximate wall clock time in nanoseconds at which the current trace item was executed.
std::optional< std::string > GetSyncPointMetadata() const override
Get some metadata associated with a synchronization point event.
const std::optional< DecodedThread::NanosecondsRange > & GetNanosecondsRange() const
Get or calculate the TSC range that includes the current trace item.
lldb::cpu_id_t GetCPU() const override
Get the CPU associated with the current trace item.
void Next() override
Move the cursor to the next item (instruction or error).
std::optional< LinuxPerfZeroTscConversion > m_tsc_conversion
Timing information and cached values.
DecodedThreadSP m_decoded_thread_sp
Storage of the actual instructions.
int64_t m_pos
Internal instruction index currently pointing at.
std::optional< uint64_t > m_beginning_of_time_nanos
Lowest nanoseconds timestamp seen in any thread trace, std::nullopt if not available at all.
std::optional< DecodedThread::NanosecondsRange > m_nanoseconds_range
Range of trace items with the same non-interpolated timestamps in nanoseconds that includes the curre...
bool GoToId(lldb::user_id_t id) override
Instruction identifiers:
bool Seek(int64_t offset, lldb::TraceCursorSeekType origin) override
Make the cursor point to an item in the trace based on an origin point and an offset.
const std::optional< DecodedThread::TSCRange > & GetTSCRange() const
Get or calculate the TSC range that includes the current trace item.
bool HasId(lldb::user_id_t id) const override
std::shared_ptr< DecodedThread > DecodedThreadSP
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::Thread > ThreadSP
Definition: lldb-forward.h:445
TraceEvent
Events that might happen during a trace session.
uint32_t cpu_id_t
Definition: lldb-types.h:91
TraceCursorSeekType
Enum to indicate the reference point when invoking TraceCursor::Seek().
@ eTraceCursorSeekTypeCurrent
The current position in the trace.
@ eTraceCursorSeekTypeEnd
The end of the trace, i.e the most recent item.
@ eTraceCursorSeekTypeBeginning
The beginning of the trace, i.e the oldest item.
uint64_t user_id_t
Definition: lldb-types.h:82
uint64_t addr_t
Definition: lldb-types.h:80
Definition: Debugger.h:54