LLDB  mainline
LibiptDecoder.h
Go to the documentation of this file.
1 //===-- LibiptDecoder.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_LIBIPT_DECODER_H
10 #define LLDB_SOURCE_PLUGINS_TRACE_LIBIPT_DECODER_H
11 
12 #include "DecodedThread.h"
14 #include "forward-declarations.h"
15 #include "intel-pt.h"
16 
17 namespace lldb_private {
18 namespace trace_intel_pt {
19 
20 /// This struct represents a contiguous section of a trace that starts at a PSB
21 /// and ends right before the next PSB or the end of the trace.
22 struct PSBBlock {
23  /// The memory offset of a PSB packet that is a synchronization point for the
24  /// decoder. A decoder normally looks first for a PSB packet and then it
25  /// starts decoding.
26  uint64_t psb_offset;
27  /// The timestamp associated with the PSB packet above.
28  llvm::Optional<uint64_t> tsc;
29  /// Size in bytes of this block
30  uint64_t size;
31  /// The first ip for this PSB block.
32  /// This is \a None if tracing was disabled when the PSB block was emitted.
33  /// This means that eventually there's be an enablement event that will come
34  /// with an ip.
35  llvm::Optional<lldb::addr_t> starting_ip;
36 };
37 
38 /// 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 subtraces
40 /// that belong to this execution.
43  std::vector<PSBBlock> psb_blocks;
44 
48 
49  /// Comparator by time
50  bool operator<(const IntelPTThreadContinousExecution &o) const;
51 };
52 
53 /// Decode a raw Intel PT trace for a single thread given in \p buffer and
54 /// append the decoded instructions and errors in \p decoded_thread. It uses the
55 /// low level libipt library underneath.
56 ///
57 /// \return
58 /// An \a llvm::Error if the decoder couldn't be properly set up.
59 llvm::Error DecodeSingleTraceForThread(DecodedThread &decoded_thread,
60  TraceIntelPT &trace_intel_pt,
61  llvm::ArrayRef<uint8_t> buffer);
62 
63 /// Decode a raw Intel PT trace for a single thread that was collected in a per
64 /// cpu core basis.
65 ///
66 /// \param[out] decoded_thread
67 /// All decoded instructions, errors and events will be appended to this
68 /// object.
69 ///
70 /// \param[in] trace_intel_pt
71 /// The main Trace object that contains all the information related to the
72 /// trace session.
73 ///
74 /// \param[in] buffers
75 /// A map from cpu core id to raw intel pt buffers.
76 ///
77 /// \param[in] executions
78 /// A list of chunks of timed executions of the same given thread. It is used
79 /// to identify if some executions have missing intel pt data and also to
80 /// determine in which core a certain part of the execution ocurred.
81 ///
82 /// \return
83 /// An \a llvm::Error if the decoder couldn't be properly set up, i.e. no
84 /// instructions were attempted to be decoded.
86  DecodedThread &decoded_thread, TraceIntelPT &trace_intel_pt,
87  const llvm::DenseMap<lldb::cpu_id_t, llvm::ArrayRef<uint8_t>> &buffers,
88  const std::vector<IntelPTThreadContinousExecution> &executions);
89 
90 /// Given an intel pt trace, split it in chunks delimited by PSB packets. Each
91 /// of these chunks is guaranteed to have been executed continuously.
92 ///
93 /// \param[in] trace_intel_pt
94 /// The main Trace object that contains all the information related to the
95 /// trace session.
96 ///
97 /// \param[in] buffer
98 /// The intel pt buffer that belongs to a single thread or to a single cpu
99 /// core.
100 ///
101 /// \param[in] expect_tscs
102 /// If \b true, an error is return if a packet without TSC is found.
103 ///
104 /// \return
105 /// A list of continuous executions sorted by time, or an \a llvm::Error in
106 /// case of failures.
107 llvm::Expected<std::vector<PSBBlock>>
108 SplitTraceIntoPSBBlock(TraceIntelPT &trace_intel_pt,
109  llvm::ArrayRef<uint8_t> buffer, bool expect_tscs);
110 
111 /// Find the lowest TSC in the given trace.
112 ///
113 /// \return
114 /// The lowest TSC value in this trace if available, \a llvm::None if the
115 /// trace is empty or the trace contains no timing information, or an \a
116 /// llvm::Error if it was not possible to set up the decoder.
117 llvm::Expected<llvm::Optional<uint64_t>>
118 FindLowestTSCInTrace(TraceIntelPT &trace_intel_pt,
119  llvm::ArrayRef<uint8_t> buffer);
120 
121 } // namespace trace_intel_pt
122 } // namespace lldb_private
123 
124 #endif // LLDB_SOURCE_PLUGINS_TRACE_LIBIPT_DECODER_H
lldb_private::trace_intel_pt::PSBBlock::starting_ip
llvm::Optional< lldb::addr_t > starting_ip
The first ip for this PSB block.
Definition: LibiptDecoder.h:35
lldb_private::trace_intel_pt::DecodeSystemWideTraceForThread
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)
Decode a raw Intel PT trace for a single thread that was collected in a per cpu core basis.
PerfContextSwitchDecoder.h
lldb_private::trace_intel_pt::IntelPTThreadContinousExecution::thread_execution
ThreadContinuousExecution thread_execution
Definition: LibiptDecoder.h:42
lldb_private::trace_intel_pt::SplitTraceIntoPSBBlock
llvm::Expected< std::vector< PSBBlock > > SplitTraceIntoPSBBlock(TraceIntelPT &trace_intel_pt, llvm::ArrayRef< uint8_t > buffer, bool expect_tscs)
Given an intel pt trace, split it in chunks delimited by PSB packets.
Definition: LibiptDecoder.cpp:443
DecodedThread.h
lldb_private::trace_intel_pt::PSBBlock::tsc
llvm::Optional< uint64_t > tsc
The timestamp associated with the PSB packet above.
Definition: LibiptDecoder.h:28
forward-declarations.h
lldb_private::trace_intel_pt::PSBBlock::psb_offset
uint64_t psb_offset
The memory offset of a PSB packet that is a synchronization point for the decoder.
Definition: LibiptDecoder.h:26
lldb_private::trace_intel_pt::IntelPTThreadContinousExecution::operator<
bool operator<(const IntelPTThreadContinousExecution &o) const
Comparator by time.
Definition: LibiptDecoder.cpp:429
lldb_private::trace_intel_pt::IntelPTThreadContinousExecution::IntelPTThreadContinousExecution
IntelPTThreadContinousExecution(const ThreadContinuousExecution &thread_execution)
Definition: LibiptDecoder.h:45
uint32_t
lldb_private::trace_intel_pt::IntelPTThreadContinousExecution::psb_blocks
std::vector< PSBBlock > psb_blocks
Definition: LibiptDecoder.h:43
lldb_private::trace_intel_pt::PSBBlock
This struct represents a contiguous section of a trace that starts at a PSB and ends right before the...
Definition: LibiptDecoder.h:22
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
Error
llvm::Error Error
Definition: UdtRecordCompleter.cpp:30
lldb_private::trace_intel_pt::ThreadContinuousExecution
This class indicates the time interval in which a thread was running continuously on a cpu core.
Definition: PerfContextSwitchDecoder.h:23
lldb_private::trace_intel_pt::IntelPTThreadContinousExecution
This struct represents a continuous execution of a thread in a cpu, delimited by a context switch in ...
Definition: LibiptDecoder.h:41
lldb_private::trace_intel_pt::PSBBlock::size
uint64_t size
Size in bytes of this block.
Definition: LibiptDecoder.h:30
lldb_private::trace_intel_pt::FindLowestTSCInTrace
llvm::Expected< llvm::Optional< uint64_t > > FindLowestTSCInTrace(TraceIntelPT &trace_intel_pt, llvm::ArrayRef< uint8_t > buffer)
Find the lowest TSC in the given trace.
lldb_private::trace_intel_pt::DecodeSingleTraceForThread
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 given in buffer and append the decoded instructions a...