1 //===-- ThreadDecoder.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_THREAD_DECODER_H
10 #define LLDB_SOURCE_PLUGINS_TRACE_THREAD_DECODER_H
11 
12 #include "DecodedThread.h"
13 #include "forward-declarations.h"
14 #include "intel-pt.h"
15 #include "lldb/Target/Process.h"
16 #include "lldb/Utility/FileSpec.h"
17 #include <optional>
18 
19 namespace lldb_private {
20 namespace trace_intel_pt {
21 
22 /// Class that handles the decoding of a thread and caches the result.
23 class ThreadDecoder {
24 public:
25   /// \param[in] thread_sp
26   ///     The thread whose intel pt trace buffer will be decoded.
27   ///
28   /// \param[in] trace
29   ///     The main Trace object who owns this decoder and its data.
30   ThreadDecoder(const lldb::ThreadSP &thread_sp, TraceIntelPT &trace);
31 
32   /// Decode the thread and store the result internally, to avoid
33   /// recomputations.
34   ///
35   /// \return
36   ///     A \a DecodedThread instance.
37   llvm::Expected<DecodedThreadSP> Decode();
38 
39   /// \return
40   ///     The lowest TSC value in this trace if available, \a std::nullopt if
41   ///     the trace is empty or the trace contains no timing information, or an
42   ///     \a llvm::Error if it was not possible to set up the decoder.
43   llvm::Expected<std::optional<uint64_t>> FindLowestTSC();
44 
45   ThreadDecoder(const ThreadDecoder &other) = delete;
46   ThreadDecoder &operator=(const ThreadDecoder &other) = delete;
47 
48 private:
49   llvm::Expected<DecodedThreadSP> DoDecode();
50 
51   lldb::ThreadSP m_thread_sp;
52   TraceIntelPT &m_trace;
53   std::optional<DecodedThreadSP> m_decoded_thread;
54 };
55 
56 } // namespace trace_intel_pt
57 } // namespace lldb_private
58 
59 #endif // LLDB_SOURCE_PLUGINS_TRACE_THREAD_DECODER_H
60