xref: /llvm-project/lldb/source/Plugins/Process/Linux/IntelPTThreadTraceCollection.cpp (revision 2fe8327406050d2585d2ced910a678e28caefcf5)
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 
TracesThread(lldb::tid_t tid) const17 bool IntelPTThreadTraceCollection::TracesThread(lldb::tid_t tid) const {
18   return m_thread_traces.count(tid);
19 }
20 
TraceStop(lldb::tid_t tid)21 Error IntelPTThreadTraceCollection::TraceStop(lldb::tid_t tid) {
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 
TraceStart(lldb::tid_t tid,const TraceIntelPTStartRequest & request)31 Error IntelPTThreadTraceCollection::TraceStart(
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 =
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(tid, std::move(*trace));
44   return Error::success();
45 }
46 
GetTotalBufferSize() const47 size_t IntelPTThreadTraceCollection::GetTotalBufferSize() const {
48   return m_total_buffer_size;
49 }
50 
ForEachThread(std::function<void (lldb::tid_t tid,IntelPTSingleBufferTrace & thread_trace)> callback)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 &>
GetTracedThread(lldb::tid_t tid)59 IntelPTThreadTraceCollection::GetTracedThread(lldb::tid_t tid) {
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 
Clear()67 void IntelPTThreadTraceCollection::Clear() {
68   m_thread_traces.clear();
69   m_total_buffer_size = 0;
70 }
71 
GetTracedThreadsCount() const72 size_t IntelPTThreadTraceCollection::GetTracedThreadsCount() const {
73   return m_thread_traces.size();
74 }
75 
76 llvm::Expected<std::optional<std::vector<uint8_t>>>
TryGetBinaryData(const TraceGetBinaryDataRequest & request)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(*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 }
93