xref: /freebsd/contrib/llvm-project/lldb/source/Plugins/Trace/intel-pt/ThreadDecoder.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1 //===-- ThreadDecoder.cpp --======-----------------------------------------===//
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-exception
5 //
6 //===----------------------------------------------------------------------===//
7 
8 #include "ThreadDecoder.h"
9 #include "../common/ThreadPostMortemTrace.h"
10 #include "LibiptDecoder.h"
11 #include "TraceIntelPT.h"
12 #include "llvm/Support/MemoryBuffer.h"
13 #include <optional>
14 #include <utility>
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 using namespace lldb_private::trace_intel_pt;
19 using namespace llvm;
20 
ThreadDecoder(const ThreadSP & thread_sp,TraceIntelPT & trace)21 ThreadDecoder::ThreadDecoder(const ThreadSP &thread_sp, TraceIntelPT &trace)
22     : m_thread_sp(thread_sp), m_trace(trace) {}
23 
FindLowestTSC()24 Expected<std::optional<uint64_t>> ThreadDecoder::FindLowestTSC() {
25   std::optional<uint64_t> lowest_tsc;
26   Error err = m_trace.OnThreadBufferRead(
27       m_thread_sp->GetID(), [&](llvm::ArrayRef<uint8_t> data) -> llvm::Error {
28         Expected<std::optional<uint64_t>> tsc =
29             FindLowestTSCInTrace(m_trace, data);
30         if (!tsc)
31           return tsc.takeError();
32         lowest_tsc = *tsc;
33         return Error::success();
34       });
35   if (err)
36     return std::move(err);
37   return lowest_tsc;
38 }
39 
Decode()40 Expected<DecodedThreadSP> ThreadDecoder::Decode() {
41   if (!m_decoded_thread.has_value()) {
42     if (Expected<DecodedThreadSP> decoded_thread = DoDecode()) {
43       m_decoded_thread = *decoded_thread;
44     } else {
45       return decoded_thread.takeError();
46     }
47   }
48   return *m_decoded_thread;
49 }
50 
DoDecode()51 llvm::Expected<DecodedThreadSP> ThreadDecoder::DoDecode() {
52   return m_trace.GetThreadTimer(m_thread_sp->GetID())
53       .TimeTask("Decoding instructions", [&]() -> Expected<DecodedThreadSP> {
54         DecodedThreadSP decoded_thread_sp = std::make_shared<DecodedThread>(
55             m_thread_sp, m_trace.GetPerfZeroTscConversion());
56 
57         Error err = m_trace.OnThreadBufferRead(
58             m_thread_sp->GetID(), [&](llvm::ArrayRef<uint8_t> data) {
59               return DecodeSingleTraceForThread(*decoded_thread_sp, m_trace,
60                                                 data);
61             });
62 
63         if (err)
64           return std::move(err);
65         return decoded_thread_sp;
66       });
67 }
68