LLDB mainline
ThreadDecoder.cpp
Go to the documentation of this file.
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
16using namespace lldb;
17using namespace lldb_private;
18using namespace lldb_private::trace_intel_pt;
19using namespace llvm;
20
22 : m_thread_sp(thread_sp), m_trace(trace) {}
23
24Expected<std::optional<uint64_t>> ThreadDecoder::FindLowestTSC() {
25 std::optional<uint64_t> lowest_tsc;
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
40Expected<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
51llvm::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>(
56
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}
llvm::Error Error
auto TimeTask(llvm::StringRef name, C task)
Execute the given task and record its duration.
Definition: TaskTimer.h:36
llvm::Expected< DecodedThreadSP > Decode()
Decode the thread and store the result internally, to avoid recomputations.
llvm::Expected< std::optional< uint64_t > > FindLowestTSC()
std::optional< DecodedThreadSP > m_decoded_thread
Definition: ThreadDecoder.h:53
llvm::Expected< DecodedThreadSP > DoDecode()
ThreadDecoder(const lldb::ThreadSP &thread_sp, TraceIntelPT &trace)
llvm::Error OnThreadBufferRead(lldb::tid_t tid, OnBinaryDataReadCallback callback)
See Trace::OnThreadBinaryDataRead().
ScopedTaskTimer & GetThreadTimer(lldb::tid_t tid)
std::optional< LinuxPerfZeroTscConversion > GetPerfZeroTscConversion()
Get or fetch the values used to convert to and from TSCs and nanos.
std::shared_ptr< DecodedThread > DecodedThreadSP
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::Thread > ThreadSP
Definition: lldb-forward.h:438
Definition: Debugger.h:53