LLDB mainline
IntelPTThreadTraceCollection.cpp
Go to the documentation of this file.
1//===-- IntelPTThreadTraceCollection.cpp ----------------------------------===//
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
10#include <optional>
11
12using namespace lldb;
13using namespace lldb_private;
14using namespace process_linux;
15using namespace llvm;
16
18 return m_thread_traces.count(tid);
19}
20
22 auto it = m_thread_traces.find(tid);
23 if (it == m_thread_traces.end())
24 return createStringError(inconvertibleErrorCode(),
25 "Thread %" PRIu64 " not currently traced", tid);
26 m_total_buffer_size -= it->second.GetIptTraceSize();
27 m_thread_traces.erase(tid);
28 return Error::success();
29}
30
32 lldb::tid_t tid, const TraceIntelPTStartRequest &request) {
33 if (TracesThread(tid))
34 return createStringError(inconvertibleErrorCode(),
35 "Thread %" PRIu64 " already traced", tid);
36
37 Expected<IntelPTSingleBufferTrace> trace =
39 if (!trace)
40 return trace.takeError();
41
42 m_total_buffer_size += trace->GetIptTraceSize();
43 m_thread_traces.try_emplace(tid, std::move(*trace));
44 return Error::success();
45}
46
49}
50
52 std::function<void(lldb::tid_t tid, IntelPTSingleBufferTrace &thread_trace)>
53 callback) {
54 for (auto &it : m_thread_traces)
55 callback(it.first, it.second);
56}
57
58Expected<IntelPTSingleBufferTrace &>
60 auto it = m_thread_traces.find(tid);
61 if (it == m_thread_traces.end())
62 return createStringError(inconvertibleErrorCode(),
63 "Thread %" PRIu64 " not currently traced", tid);
64 return it->second;
65}
66
68 m_thread_traces.clear();
70}
71
73 return m_thread_traces.size();
74}
75
76llvm::Expected<std::optional<std::vector<uint8_t>>>
78 const TraceGetBinaryDataRequest &request) {
79 if (!request.tid)
80 return std::nullopt;
81 if (request.kind != IntelPTDataKinds::kIptTrace)
82 return std::nullopt;
83
84 if (!TracesThread(*request.tid))
85 return std::nullopt;
86
87 if (Expected<IntelPTSingleBufferTrace &> trace =
88 GetTracedThread(*request.tid))
89 return trace->GetIptTrace();
90 else
91 return trace.takeError();
92}
llvm::Error Error
This class wraps a single perf event collecting intel pt data in a single buffer.
static llvm::Expected< IntelPTSingleBufferTrace > Start(const TraceIntelPTStartRequest &request, std::optional< lldb::tid_t > tid, std::optional< lldb::cpu_id_t > cpu_id=std::nullopt, bool disabled=false, std::optional< int > cgroup_fd=std::nullopt)
Start tracing using a single Intel PT trace buffer.
llvm::DenseMap< lldb::tid_t, IntelPTSingleBufferTrace > m_thread_traces
llvm::Error TraceStop(lldb::tid_t tid)
Stop tracing the thread given by its tid.
llvm::Error TraceStart(lldb::tid_t tid, const TraceIntelPTStartRequest &request)
Start tracing the thread given by its tid.
size_t m_total_buffer_size
Total actual thread buffer size in bytes.
llvm::Expected< IntelPTSingleBufferTrace & > GetTracedThread(lldb::tid_t tid)
llvm::Expected< std::optional< std::vector< uint8_t > > > TryGetBinaryData(const TraceGetBinaryDataRequest &request)
void ForEachThread(std::function< void(lldb::tid_t tid, IntelPTSingleBufferTrace &thread_trace)> callback)
Execute the provided callback on each thread that is being traced.
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
uint64_t tid_t
Definition: lldb-types.h:82
Definition: Debugger.h:53
jLLDBTraceGetBinaryData gdb-remote packet
std::optional< lldb::tid_t > tid
Optional tid if the data is related to a thread.
std::string kind
Identifier for the data.