Path: blob/main/contrib/llvm-project/lldb/source/Plugins/Trace/intel-pt/TraceCursorIntelPT.cpp
39645 views
//===-- TraceCursorIntelPT.cpp --------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "TraceCursorIntelPT.h"9#include "DecodedThread.h"10#include "TraceIntelPT.h"11#include <cstdlib>12#include <optional>1314using namespace lldb;15using namespace lldb_private;16using namespace lldb_private::trace_intel_pt;17using namespace llvm;1819TraceCursorIntelPT::TraceCursorIntelPT(20ThreadSP thread_sp, DecodedThreadSP decoded_thread_sp,21const std::optional<LinuxPerfZeroTscConversion> &tsc_conversion,22std::optional<uint64_t> beginning_of_time_nanos)23: TraceCursor(thread_sp), m_decoded_thread_sp(decoded_thread_sp),24m_tsc_conversion(tsc_conversion),25m_beginning_of_time_nanos(beginning_of_time_nanos) {26Seek(0, lldb::eTraceCursorSeekTypeEnd);27}2829void TraceCursorIntelPT::Next() {30m_pos += IsForwards() ? 1 : -1;31ClearTimingRangesIfInvalid();32}3334void TraceCursorIntelPT::ClearTimingRangesIfInvalid() {35if (m_tsc_range_calculated) {36if (!m_tsc_range || m_pos < 0 || !m_tsc_range->InRange(m_pos)) {37m_tsc_range = std::nullopt;38m_tsc_range_calculated = false;39}40}4142if (m_nanoseconds_range_calculated) {43if (!m_nanoseconds_range || m_pos < 0 ||44!m_nanoseconds_range->InRange(m_pos)) {45m_nanoseconds_range = std::nullopt;46m_nanoseconds_range_calculated = false;47}48}49}5051const std::optional<DecodedThread::TSCRange> &52TraceCursorIntelPT::GetTSCRange() const {53if (!m_tsc_range_calculated) {54m_tsc_range_calculated = true;55m_tsc_range = m_decoded_thread_sp->GetTSCRangeByIndex(m_pos);56}57return m_tsc_range;58}5960const std::optional<DecodedThread::NanosecondsRange> &61TraceCursorIntelPT::GetNanosecondsRange() const {62if (!m_nanoseconds_range_calculated) {63m_nanoseconds_range_calculated = true;64m_nanoseconds_range =65m_decoded_thread_sp->GetNanosecondsRangeByIndex(m_pos);66}67return m_nanoseconds_range;68}6970bool TraceCursorIntelPT::Seek(int64_t offset,71lldb::TraceCursorSeekType origin) {72switch (origin) {73case lldb::eTraceCursorSeekTypeBeginning:74m_pos = offset;75break;76case lldb::eTraceCursorSeekTypeEnd:77m_pos = m_decoded_thread_sp->GetItemsCount() - 1 + offset;78break;79case lldb::eTraceCursorSeekTypeCurrent:80m_pos += offset;81}8283ClearTimingRangesIfInvalid();8485return HasValue();86}8788bool TraceCursorIntelPT::HasValue() const {89return m_pos >= 0 &&90static_cast<uint64_t>(m_pos) < m_decoded_thread_sp->GetItemsCount();91}9293lldb::TraceItemKind TraceCursorIntelPT::GetItemKind() const {94return m_decoded_thread_sp->GetItemKindByIndex(m_pos);95}9697llvm::StringRef TraceCursorIntelPT::GetError() const {98return m_decoded_thread_sp->GetErrorByIndex(m_pos);99}100101lldb::addr_t TraceCursorIntelPT::GetLoadAddress() const {102return m_decoded_thread_sp->GetInstructionLoadAddress(m_pos);103}104105std::optional<uint64_t> TraceCursorIntelPT::GetHWClock() const {106if (const std::optional<DecodedThread::TSCRange> &range = GetTSCRange())107return range->tsc;108return std::nullopt;109}110111std::optional<double> TraceCursorIntelPT::GetWallClockTime() const {112if (const std::optional<DecodedThread::NanosecondsRange> &range =113GetNanosecondsRange())114return range->GetInterpolatedTime(m_pos, *m_beginning_of_time_nanos,115*m_tsc_conversion);116return std::nullopt;117}118119lldb::cpu_id_t TraceCursorIntelPT::GetCPU() const {120return m_decoded_thread_sp->GetCPUByIndex(m_pos);121}122123lldb::TraceEvent TraceCursorIntelPT::GetEventType() const {124return m_decoded_thread_sp->GetEventByIndex(m_pos);125}126127bool TraceCursorIntelPT::GoToId(user_id_t id) {128if (!HasId(id))129return false;130m_pos = id;131ClearTimingRangesIfInvalid();132return true;133}134135bool TraceCursorIntelPT::HasId(lldb::user_id_t id) const {136return id < m_decoded_thread_sp->GetItemsCount();137}138139user_id_t TraceCursorIntelPT::GetId() const { return m_pos; }140141std::optional<std::string> TraceCursorIntelPT::GetSyncPointMetadata() const {142return formatv("offset = 0x{0:x}",143m_decoded_thread_sp->GetSyncPointOffsetByIndex(m_pos))144.str();145}146147148