1145449b1SDimitry Andric //===-- ThreadDecoder.cpp --======-----------------------------------------===//
2145449b1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3145449b1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
4145449b1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5145449b1SDimitry Andric //
6145449b1SDimitry Andric //===----------------------------------------------------------------------===//
7145449b1SDimitry Andric
8145449b1SDimitry Andric #include "ThreadDecoder.h"
9145449b1SDimitry Andric #include "../common/ThreadPostMortemTrace.h"
10145449b1SDimitry Andric #include "LibiptDecoder.h"
11145449b1SDimitry Andric #include "TraceIntelPT.h"
12e3b55780SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
13e3b55780SDimitry Andric #include <optional>
14145449b1SDimitry Andric #include <utility>
15145449b1SDimitry Andric
16145449b1SDimitry Andric using namespace lldb;
17145449b1SDimitry Andric using namespace lldb_private;
18145449b1SDimitry Andric using namespace lldb_private::trace_intel_pt;
19145449b1SDimitry Andric using namespace llvm;
20145449b1SDimitry Andric
ThreadDecoder(const ThreadSP & thread_sp,TraceIntelPT & trace)21145449b1SDimitry Andric ThreadDecoder::ThreadDecoder(const ThreadSP &thread_sp, TraceIntelPT &trace)
22145449b1SDimitry Andric : m_thread_sp(thread_sp), m_trace(trace) {}
23145449b1SDimitry Andric
FindLowestTSC()24e3b55780SDimitry Andric Expected<std::optional<uint64_t>> ThreadDecoder::FindLowestTSC() {
25e3b55780SDimitry Andric std::optional<uint64_t> lowest_tsc;
2608e8dd7bSDimitry Andric Error err = m_trace.OnThreadBufferRead(
2708e8dd7bSDimitry Andric m_thread_sp->GetID(), [&](llvm::ArrayRef<uint8_t> data) -> llvm::Error {
28e3b55780SDimitry Andric Expected<std::optional<uint64_t>> tsc =
29e3b55780SDimitry Andric FindLowestTSCInTrace(m_trace, data);
3008e8dd7bSDimitry Andric if (!tsc)
3108e8dd7bSDimitry Andric return tsc.takeError();
3208e8dd7bSDimitry Andric lowest_tsc = *tsc;
3308e8dd7bSDimitry Andric return Error::success();
3408e8dd7bSDimitry Andric });
3508e8dd7bSDimitry Andric if (err)
3608e8dd7bSDimitry Andric return std::move(err);
3708e8dd7bSDimitry Andric return lowest_tsc;
3808e8dd7bSDimitry Andric }
3908e8dd7bSDimitry Andric
Decode()40145449b1SDimitry Andric Expected<DecodedThreadSP> ThreadDecoder::Decode() {
41e3b55780SDimitry Andric if (!m_decoded_thread.has_value()) {
42145449b1SDimitry Andric if (Expected<DecodedThreadSP> decoded_thread = DoDecode()) {
43145449b1SDimitry Andric m_decoded_thread = *decoded_thread;
44145449b1SDimitry Andric } else {
45145449b1SDimitry Andric return decoded_thread.takeError();
46145449b1SDimitry Andric }
47145449b1SDimitry Andric }
48145449b1SDimitry Andric return *m_decoded_thread;
49145449b1SDimitry Andric }
50145449b1SDimitry Andric
DoDecode()51145449b1SDimitry Andric llvm::Expected<DecodedThreadSP> ThreadDecoder::DoDecode() {
521f917f69SDimitry Andric return m_trace.GetThreadTimer(m_thread_sp->GetID())
53e3b55780SDimitry Andric .TimeTask("Decoding instructions", [&]() -> Expected<DecodedThreadSP> {
5408e8dd7bSDimitry Andric DecodedThreadSP decoded_thread_sp = std::make_shared<DecodedThread>(
5508e8dd7bSDimitry Andric m_thread_sp, m_trace.GetPerfZeroTscConversion());
56145449b1SDimitry Andric
57145449b1SDimitry Andric Error err = m_trace.OnThreadBufferRead(
58145449b1SDimitry Andric m_thread_sp->GetID(), [&](llvm::ArrayRef<uint8_t> data) {
59145449b1SDimitry Andric return DecodeSingleTraceForThread(*decoded_thread_sp, m_trace,
60145449b1SDimitry Andric data);
61145449b1SDimitry Andric });
62145449b1SDimitry Andric
63145449b1SDimitry Andric if (err)
64145449b1SDimitry Andric return std::move(err);
65145449b1SDimitry Andric return decoded_thread_sp;
66145449b1SDimitry Andric });
67145449b1SDimitry Andric }
68