1 //===-- IntelPTMultiCoreTrace.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 "IntelPTMultiCoreTrace.h" 10 11 #include "Procfs.h" 12 13 #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" 14 15 using namespace lldb; 16 using namespace lldb_private; 17 using namespace process_linux; 18 using namespace llvm; 19 20 static bool IsTotalBufferLimitReached(ArrayRef<cpu_id_t> cores, 21 const TraceIntelPTStartRequest &request) { 22 uint64_t required = cores.size() * request.ipt_trace_size; 23 uint64_t limit = request.process_buffer_size_limit.value_or( 24 std::numeric_limits<uint64_t>::max()); 25 return required > limit; 26 } 27 28 static Error IncludePerfEventParanoidMessageInError(Error &&error) { 29 return createStringError( 30 inconvertibleErrorCode(), 31 "%s\nYou might need to rerun as sudo or to set " 32 "/proc/sys/kernel/perf_event_paranoid to a value of 0 or -1.", 33 toString(std::move(error)).c_str()); 34 } 35 36 Expected<std::unique_ptr<IntelPTMultiCoreTrace>> 37 IntelPTMultiCoreTrace::StartOnAllCores(const TraceIntelPTStartRequest &request, 38 NativeProcessProtocol &process, 39 Optional<int> cgroup_fd) { 40 Expected<ArrayRef<cpu_id_t>> cpu_ids = GetAvailableLogicalCoreIDs(); 41 if (!cpu_ids) 42 return cpu_ids.takeError(); 43 44 if (IsTotalBufferLimitReached(*cpu_ids, request)) 45 return createStringError( 46 inconvertibleErrorCode(), 47 "The process can't be traced because the process trace size limit " 48 "has been reached. Consider retracing with a higher limit."); 49 50 DenseMap<cpu_id_t, std::pair<IntelPTSingleBufferTrace, ContextSwitchTrace>> 51 traces; 52 53 for (cpu_id_t cpu_id : *cpu_ids) { 54 Expected<IntelPTSingleBufferTrace> core_trace = 55 IntelPTSingleBufferTrace::Start(request, /*tid=*/None, cpu_id, 56 /*disabled=*/true, cgroup_fd); 57 if (!core_trace) 58 return IncludePerfEventParanoidMessageInError(core_trace.takeError()); 59 60 if (Expected<PerfEvent> context_switch_trace = 61 CreateContextSwitchTracePerfEvent(cpu_id, 62 &core_trace->GetPerfEvent())) { 63 traces.try_emplace(cpu_id, 64 std::make_pair(std::move(*core_trace), 65 std::move(*context_switch_trace))); 66 } else { 67 return context_switch_trace.takeError(); 68 } 69 } 70 71 return std::unique_ptr<IntelPTMultiCoreTrace>( 72 new IntelPTMultiCoreTrace(std::move(traces), process, (bool)cgroup_fd)); 73 } 74 75 void IntelPTMultiCoreTrace::ForEachCore( 76 std::function<void(cpu_id_t cpu_id, IntelPTSingleBufferTrace &core_trace)> 77 callback) { 78 for (auto &it : m_traces_per_core) 79 callback(it.first, it.second.first); 80 } 81 82 void IntelPTMultiCoreTrace::ForEachCore( 83 std::function<void(cpu_id_t cpu_id, IntelPTSingleBufferTrace &intelpt_trace, 84 ContextSwitchTrace &context_switch_trace)> 85 callback) { 86 for (auto &it : m_traces_per_core) 87 callback(it.first, it.second.first, it.second.second); 88 } 89 90 void IntelPTMultiCoreTrace::ProcessDidStop() { 91 ForEachCore([](cpu_id_t cpu_id, IntelPTSingleBufferTrace &core_trace) { 92 if (Error err = core_trace.Pause()) { 93 LLDB_LOG_ERROR(GetLog(POSIXLog::Trace), std::move(err), 94 "Unable to pause the core trace for core {0}", cpu_id); 95 } 96 }); 97 } 98 99 void IntelPTMultiCoreTrace::ProcessWillResume() { 100 ForEachCore([](cpu_id_t cpu_id, IntelPTSingleBufferTrace &core_trace) { 101 if (Error err = core_trace.Resume()) { 102 LLDB_LOG_ERROR(GetLog(POSIXLog::Trace), std::move(err), 103 "Unable to resume the core trace for core {0}", cpu_id); 104 } 105 }); 106 } 107 108 TraceIntelPTGetStateResponse IntelPTMultiCoreTrace::GetState() { 109 TraceIntelPTGetStateResponse state; 110 state.using_cgroup_filtering = m_using_cgroup_filtering; 111 112 for (NativeThreadProtocol &thread : m_process.Threads()) 113 state.traced_threads.push_back( 114 TraceThreadState{thread.GetID(), {}}); 115 116 state.cpus.emplace(); 117 ForEachCore([&](lldb::cpu_id_t cpu_id, 118 const IntelPTSingleBufferTrace &core_trace, 119 const ContextSwitchTrace &context_switch_trace) { 120 state.cpus->push_back( 121 {cpu_id, 122 {{IntelPTDataKinds::kIptTrace, core_trace.GetIptTraceSize()}, 123 {IntelPTDataKinds::kPerfContextSwitchTrace, 124 context_switch_trace.GetEffectiveDataBufferSize()}}}); 125 }); 126 127 return state; 128 } 129 130 bool IntelPTMultiCoreTrace::TracesThread(lldb::tid_t tid) const { 131 // All the process' threads are being traced automatically. 132 return (bool)m_process.GetThreadByID(tid); 133 } 134 135 llvm::Error IntelPTMultiCoreTrace::TraceStart(lldb::tid_t tid) { 136 // All the process' threads are being traced automatically. 137 if (!TracesThread(tid)) 138 return createStringError( 139 inconvertibleErrorCode(), 140 "Thread %" PRIu64 " is not part of the target process", tid); 141 return Error::success(); 142 } 143 144 Error IntelPTMultiCoreTrace::TraceStop(lldb::tid_t tid) { 145 return createStringError(inconvertibleErrorCode(), 146 "Can't stop tracing an individual thread when " 147 "per-cpu process tracing is enabled."); 148 } 149 150 Expected<Optional<std::vector<uint8_t>>> 151 IntelPTMultiCoreTrace::TryGetBinaryData( 152 const TraceGetBinaryDataRequest &request) { 153 if (!request.cpu_id) 154 return None; 155 auto it = m_traces_per_core.find(*request.cpu_id); 156 if (it == m_traces_per_core.end()) 157 return createStringError( 158 inconvertibleErrorCode(), 159 formatv("Core {0} is not being traced", *request.cpu_id)); 160 161 if (request.kind == IntelPTDataKinds::kIptTrace) 162 return it->second.first.GetIptTrace(); 163 if (request.kind == IntelPTDataKinds::kPerfContextSwitchTrace) 164 return it->second.second.GetReadOnlyDataBuffer(); 165 return None; 166 } 167