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
11#include <cstdint>
12#include <optional>
13#include <vector>
14
15using namespace lldb;
16using namespace lldb_private;
17using namespace process_linux;
18using namespace llvm;
19
23
25 auto it = m_thread_traces.find(tid);
26 if (it == m_thread_traces.end())
27 return createStringError(inconvertibleErrorCode(),
28 "Thread %" PRIu64 " not currently traced", tid);
29 m_total_buffer_size -= it->second.GetIptTraceSize();
30 m_thread_traces.erase(tid);
31 return Error::success();
32}
33
35 lldb::tid_t tid, const TraceIntelPTStartRequest &request) {
36 if (TracesThread(tid))
37 return createStringError(inconvertibleErrorCode(),
38 "Thread %" PRIu64 " already traced", tid);
39
40 Expected<IntelPTSingleBufferTrace> trace =
42 if (!trace)
43 return trace.takeError();
44
45 m_total_buffer_size += trace->GetIptTraceSize();
46 m_thread_traces.try_emplace(tid, std::move(*trace));
47 return Error::success();
48}
49
53
55 std::function<void(lldb::tid_t tid, IntelPTSingleBufferTrace &thread_trace)>
56 callback) {
57 for (auto &it : m_thread_traces)
58 callback(it.first, it.second);
59}
60
61Expected<IntelPTSingleBufferTrace &>
63 auto it = m_thread_traces.find(tid);
64 if (it == m_thread_traces.end())
65 return createStringError(inconvertibleErrorCode(),
66 "Thread %" PRIu64 " not currently traced", tid);
67 return it->second;
68}
69
74
78
79llvm::Expected<std::optional<std::vector<uint8_t>>>
81 const TraceGetBinaryDataRequest &request) {
82 if (!request.tid)
83 return std::nullopt;
84 if (request.kind != IntelPTDataKinds::kIptTrace)
85 return std::nullopt;
86
87 if (!TracesThread(*request.tid))
88 return std::nullopt;
89
90 if (Expected<IntelPTSingleBufferTrace &> trace =
91 GetTracedThread(*request.tid))
92 return trace->GetIptTrace();
93 else
94 return trace.takeError();
95}
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.
uint64_t tid_t
Definition lldb-types.h:85
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.