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"
13 #include "PerfContextSwitchDecoder.h"
14 #include "forward-declarations.h"
15 
16 #include "intel-pt.h"
17 
18 namespace lldb_private {
19 namespace trace_intel_pt {
20 
21 /// This struct represents a point in the intel pt trace that the decoder can start decoding from without errors.
22 struct IntelPTThreadSubtrace {
23   /// The memory offset of a PSB packet that is a synchronization point for the decoder. A decoder normally looks first
24   /// for a PSB packet and then it starts decoding.
25   uint64_t psb_offset;
26   /// The timestamp associated with the PSB packet above.
27   uint64_t tsc;
28 };
29 
30 /// This struct represents a continuous execution of a thread in a cpu,
31 /// delimited by a context switch in and out, and a list of Intel PT subtraces
32 /// that belong to this execution.
33 struct IntelPTThreadContinousExecution {
34   ThreadContinuousExecution thread_execution;
35   std::vector<IntelPTThreadSubtrace> intelpt_subtraces;
36 
37   IntelPTThreadContinousExecution(
38       const ThreadContinuousExecution &thread_execution)
39       : thread_execution(thread_execution) {}
40 
41   /// Comparator by time
42   bool operator<(const IntelPTThreadContinousExecution &o) const;
43 };
44 
45 /// Decode a raw Intel PT trace for a single thread given in \p buffer and append the decoded
46 /// instructions and errors in \p decoded_thread. It uses the low level libipt
47 /// library underneath.
48 void DecodeSingleTraceForThread(DecodedThread &decoded_thread, TraceIntelPT &trace_intel_pt,
49                  llvm::ArrayRef<uint8_t> buffer);
50 
51 /// Decode a raw Intel PT trace for a single thread that was collected in a per
52 /// cpu core basis.
53 ///
54 /// \param[out] decoded_thread
55 ///   All decoded instructions, errors and events will be appended to this
56 ///   object.
57 ///
58 /// \param[in] trace_intel_pt
59 ///   The main Trace object that contains all the information related to the
60 ///   trace session.
61 ///
62 /// \param[in] buffers
63 ///   A map from cpu core id to raw intel pt buffers.
64 ///
65 /// \param[in] executions
66 ///   A list of chunks of timed executions of the same given thread. It is used
67 ///   to identify if some executions have missing intel pt data and also to
68 ///   determine in which core a certain part of the execution ocurred.
69 void DecodeSystemWideTraceForThread(
70     DecodedThread &decoded_thread, TraceIntelPT &trace_intel_pt,
71     const llvm::DenseMap<lldb::cpu_id_t, llvm::ArrayRef<uint8_t>> &buffers,
72     const std::vector<IntelPTThreadContinousExecution> &executions);
73 
74 /// Given an intel pt trace, split it in chunks delimited by PSB packets. Each of these chunks
75 /// is guaranteed to have been executed continuously.
76 ///
77 /// \param[in] trace_intel_pt
78 ///   The main Trace object that contains all the information related to the trace session.
79 ///
80 /// \param[in] buffer
81 ///   The intel pt buffer that belongs to a single thread or to a single cpu core.
82 ///
83 /// \return
84 ///   A list of continuous executions sorted by time, or an \a llvm::Error in case of failures.
85 llvm::Expected<std::vector<IntelPTThreadSubtrace>>
86 SplitTraceInContinuousExecutions(TraceIntelPT &trace_intel_pt,
87                                  llvm::ArrayRef<uint8_t> buffer);
88 
89 } // namespace trace_intel_pt
90 } // namespace lldb_private
91 
92 #endif // LLDB_SOURCE_PLUGINS_TRACE_LIBIPT_DECODER_H
93