Path: blob/main/contrib/llvm-project/lldb/source/Plugins/Trace/intel-pt/LibiptDecoder.h
39648 views
//===-- LibiptDecoder.h --======---------------------------------*- C++ -*-===//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#ifndef LLDB_SOURCE_PLUGINS_TRACE_LIBIPT_DECODER_H9#define LLDB_SOURCE_PLUGINS_TRACE_LIBIPT_DECODER_H1011#include "DecodedThread.h"12#include "PerfContextSwitchDecoder.h"13#include "forward-declarations.h"14#include "intel-pt.h"15#include <optional>1617namespace lldb_private {18namespace trace_intel_pt {1920/// This struct represents a contiguous section of a trace that starts at a PSB21/// and ends right before the next PSB or the end of the trace.22struct PSBBlock {23/// The memory offset of a PSB packet that is a synchronization point for the24/// decoder. A decoder normally looks first for a PSB packet and then it25/// starts decoding.26uint64_t psb_offset;27/// The timestamp associated with the PSB packet above.28std::optional<uint64_t> tsc;29/// Size in bytes of this block30uint64_t size;31/// The first ip for this PSB block.32/// This is \a std::nullopt if tracing was disabled when the PSB block was33/// emitted. This means that eventually there's be an enablement event that34/// will come with an ip.35std::optional<lldb::addr_t> starting_ip;36};3738/// This struct represents a continuous execution of a thread in a cpu,39/// delimited by a context switch in and out, and a list of Intel PT subtraces40/// that belong to this execution.41struct IntelPTThreadContinousExecution {42ThreadContinuousExecution thread_execution;43std::vector<PSBBlock> psb_blocks;4445IntelPTThreadContinousExecution(46const ThreadContinuousExecution &thread_execution)47: thread_execution(thread_execution) {}4849/// Comparator by time50bool operator<(const IntelPTThreadContinousExecution &o) const;51};5253/// Decode a raw Intel PT trace for a single thread given in \p buffer and54/// append the decoded instructions and errors in \p decoded_thread. It uses the55/// low level libipt library underneath.56///57/// \return58/// An \a llvm::Error if the decoder couldn't be properly set up.59llvm::Error DecodeSingleTraceForThread(DecodedThread &decoded_thread,60TraceIntelPT &trace_intel_pt,61llvm::ArrayRef<uint8_t> buffer);6263/// Decode a raw Intel PT trace for a single thread that was collected in a per64/// cpu core basis.65///66/// \param[out] decoded_thread67/// All decoded instructions, errors and events will be appended to this68/// object.69///70/// \param[in] trace_intel_pt71/// The main Trace object that contains all the information related to the72/// trace session.73///74/// \param[in] buffers75/// A map from cpu core id to raw intel pt buffers.76///77/// \param[in] executions78/// A list of chunks of timed executions of the same given thread. It is used79/// to identify if some executions have missing intel pt data and also to80/// determine in which core a certain part of the execution ocurred.81///82/// \return83/// An \a llvm::Error if the decoder couldn't be properly set up, i.e. no84/// instructions were attempted to be decoded.85llvm::Error DecodeSystemWideTraceForThread(86DecodedThread &decoded_thread, TraceIntelPT &trace_intel_pt,87const llvm::DenseMap<lldb::cpu_id_t, llvm::ArrayRef<uint8_t>> &buffers,88const std::vector<IntelPTThreadContinousExecution> &executions);8990/// Given an intel pt trace, split it in chunks delimited by PSB packets. Each91/// of these chunks is guaranteed to have been executed continuously.92///93/// \param[in] trace_intel_pt94/// The main Trace object that contains all the information related to the95/// trace session.96///97/// \param[in] buffer98/// The intel pt buffer that belongs to a single thread or to a single cpu99/// core.100///101/// \param[in] expect_tscs102/// If \b true, an error is return if a packet without TSC is found.103///104/// \return105/// A list of continuous executions sorted by time, or an \a llvm::Error in106/// case of failures.107llvm::Expected<std::vector<PSBBlock>>108SplitTraceIntoPSBBlock(TraceIntelPT &trace_intel_pt,109llvm::ArrayRef<uint8_t> buffer, bool expect_tscs);110111/// Find the lowest TSC in the given trace.112///113/// \return114/// The lowest TSC value in this trace if available, \a std::nullopt if the115/// trace is empty or the trace contains no timing information, or an \a116/// llvm::Error if it was not possible to set up the decoder.117llvm::Expected<std::optional<uint64_t>>118FindLowestTSCInTrace(TraceIntelPT &trace_intel_pt,119llvm::ArrayRef<uint8_t> buffer);120121} // namespace trace_intel_pt122} // namespace lldb_private123124#endif // LLDB_SOURCE_PLUGINS_TRACE_LIBIPT_DECODER_H125126127