Files
clang-p2996/lldb/source/Plugins/Trace/intel-pt/LibiptDecoder.h
Walter Erquinigo a7d6c3effe [trace] Make events first class items in the trace cursor and rework errors
We want to include events with metadata, like context switches, and this
requires the API to handle events with payloads (e.g. information about
such context switches). Besides this, we want to support multiple
similar events between two consecutive instructions, like multiple
context switches. However, the current implementation is not good for this because
we are defining events as bitmask enums associated with specific
instructions. Thus, we need to decouple instructions from events and
make events actual items in the trace, just like instructions and
errors.

- Add accessors in the TraceCursor to know if an item is an event or not
- Modify from the TraceDumper all the way to DecodedThread to support
- Renamed the paused event to disabled.
- Improved the tsc handling logic. I was using an API for getting the tsc from libipt, but that was an overkill that should be used when not processing events manually, but as we are already processing events, we can more easily get the tscs.
event items. Fortunately this simplified many things
- As part of this refactor, I also fixed and long stating issue, which is that some non decoding errors were being inserted in the decoded thread. I changed this so that TraceIntelPT::Decode returns an error if the decoder couldn't be set up proplerly. Then, errors within a trace are actual anomalies found in between instrutions.

All test pass

Differential Revision: https://reviews.llvm.org/D128576
2022-06-29 09:19:51 -07:00

101 lines
3.9 KiB
C++

//===-- LibiptDecoder.h --======---------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLDB_SOURCE_PLUGINS_TRACE_LIBIPT_DECODER_H
#define LLDB_SOURCE_PLUGINS_TRACE_LIBIPT_DECODER_H
#include "DecodedThread.h"
#include "PerfContextSwitchDecoder.h"
#include "forward-declarations.h"
#include "intel-pt.h"
namespace lldb_private {
namespace trace_intel_pt {
/// This struct represents a point in the intel pt trace that the decoder can start decoding from without errors.
struct IntelPTThreadSubtrace {
/// The memory offset of a PSB packet that is a synchronization point for the decoder. A decoder normally looks first
/// for a PSB packet and then it starts decoding.
uint64_t psb_offset;
/// The timestamp associated with the PSB packet above.
uint64_t tsc;
};
/// This struct represents a continuous execution of a thread in a cpu,
/// delimited by a context switch in and out, and a list of Intel PT subtraces
/// that belong to this execution.
struct IntelPTThreadContinousExecution {
ThreadContinuousExecution thread_execution;
std::vector<IntelPTThreadSubtrace> intelpt_subtraces;
IntelPTThreadContinousExecution(
const ThreadContinuousExecution &thread_execution)
: thread_execution(thread_execution) {}
/// Comparator by time
bool operator<(const IntelPTThreadContinousExecution &o) const;
};
/// Decode a raw Intel PT trace for a single thread given in \p buffer and
/// append the decoded instructions and errors in \p decoded_thread. It uses the
/// low level libipt library underneath.
///
/// \return
/// An \a llvm::Error if the decoder couldn't be properly set up.
llvm::Error DecodeSingleTraceForThread(DecodedThread &decoded_thread,
TraceIntelPT &trace_intel_pt,
llvm::ArrayRef<uint8_t> buffer);
/// Decode a raw Intel PT trace for a single thread that was collected in a per
/// cpu core basis.
///
/// \param[out] decoded_thread
/// All decoded instructions, errors and events will be appended to this
/// object.
///
/// \param[in] trace_intel_pt
/// The main Trace object that contains all the information related to the
/// trace session.
///
/// \param[in] buffers
/// A map from cpu core id to raw intel pt buffers.
///
/// \param[in] executions
/// A list of chunks of timed executions of the same given thread. It is used
/// to identify if some executions have missing intel pt data and also to
/// determine in which core a certain part of the execution ocurred.
///
/// \return
/// An \a llvm::Error if the decoder couldn't be properly set up, i.e. no
/// instructions were attempted to be decoded.
llvm::Error DecodeSystemWideTraceForThread(
DecodedThread &decoded_thread, TraceIntelPT &trace_intel_pt,
const llvm::DenseMap<lldb::cpu_id_t, llvm::ArrayRef<uint8_t>> &buffers,
const std::vector<IntelPTThreadContinousExecution> &executions);
/// Given an intel pt trace, split it in chunks delimited by PSB packets. Each of these chunks
/// is guaranteed to have been executed continuously.
///
/// \param[in] trace_intel_pt
/// The main Trace object that contains all the information related to the trace session.
///
/// \param[in] buffer
/// The intel pt buffer that belongs to a single thread or to a single cpu core.
///
/// \return
/// A list of continuous executions sorted by time, or an \a llvm::Error in case of failures.
llvm::Expected<std::vector<IntelPTThreadSubtrace>>
SplitTraceInContinuousExecutions(TraceIntelPT &trace_intel_pt,
llvm::ArrayRef<uint8_t> buffer);
} // namespace trace_intel_pt
} // namespace lldb_private
#endif // LLDB_SOURCE_PLUGINS_TRACE_LIBIPT_DECODER_H