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 | |
9 | #include "IntelPTThreadTraceCollection.h" |
10 | #include <optional> |
11 | |
12 | using namespace lldb; |
13 | using namespace lldb_private; |
14 | using namespace process_linux; |
15 | using namespace llvm; |
16 | |
17 | bool IntelPTThreadTraceCollection::TracesThread(lldb::tid_t tid) const { |
18 | return m_thread_traces.count(Val: tid); |
19 | } |
20 | |
21 | Error IntelPTThreadTraceCollection::TraceStop(lldb::tid_t tid) { |
22 | auto it = m_thread_traces.find(Val: tid); |
23 | if (it == m_thread_traces.end()) |
24 | return createStringError(EC: inconvertibleErrorCode(), |
25 | Fmt: "Thread %" PRIu64 " not currently traced" , Vals: tid); |
26 | m_total_buffer_size -= it->second.GetIptTraceSize(); |
27 | m_thread_traces.erase(Val: tid); |
28 | return Error::success(); |
29 | } |
30 | |
31 | Error IntelPTThreadTraceCollection::TraceStart( |
32 | lldb::tid_t tid, const TraceIntelPTStartRequest &request) { |
33 | if (TracesThread(tid)) |
34 | return createStringError(EC: inconvertibleErrorCode(), |
35 | Fmt: "Thread %" PRIu64 " already traced" , Vals: tid); |
36 | |
37 | Expected<IntelPTSingleBufferTrace> trace = |
38 | IntelPTSingleBufferTrace::Start(request, tid); |
39 | if (!trace) |
40 | return trace.takeError(); |
41 | |
42 | m_total_buffer_size += trace->GetIptTraceSize(); |
43 | m_thread_traces.try_emplace(Key: tid, Args: std::move(*trace)); |
44 | return Error::success(); |
45 | } |
46 | |
47 | size_t IntelPTThreadTraceCollection::GetTotalBufferSize() const { |
48 | return m_total_buffer_size; |
49 | } |
50 | |
51 | void IntelPTThreadTraceCollection::ForEachThread( |
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 | |
58 | Expected<IntelPTSingleBufferTrace &> |
59 | IntelPTThreadTraceCollection::GetTracedThread(lldb::tid_t tid) { |
60 | auto it = m_thread_traces.find(Val: tid); |
61 | if (it == m_thread_traces.end()) |
62 | return createStringError(EC: inconvertibleErrorCode(), |
63 | Fmt: "Thread %" PRIu64 " not currently traced" , Vals: tid); |
64 | return it->second; |
65 | } |
66 | |
67 | void IntelPTThreadTraceCollection::Clear() { |
68 | m_thread_traces.clear(); |
69 | m_total_buffer_size = 0; |
70 | } |
71 | |
72 | size_t IntelPTThreadTraceCollection::GetTracedThreadsCount() const { |
73 | return m_thread_traces.size(); |
74 | } |
75 | |
76 | llvm::Expected<std::optional<std::vector<uint8_t>>> |
77 | IntelPTThreadTraceCollection::TryGetBinaryData( |
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(tid: *request.tid)) |
85 | return std::nullopt; |
86 | |
87 | if (Expected<IntelPTSingleBufferTrace &> trace = |
88 | GetTracedThread(tid: *request.tid)) |
89 | return trace->GetIptTrace(); |
90 | else |
91 | return trace.takeError(); |
92 | } |
93 | |