2022-03-15 06:06:55 -07:00
|
|
|
//===-- IntelPTCollector.cpp ------------------------------------------------===//
|
2020-11-09 13:36:26 -08:00
|
|
|
//
|
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2022-03-15 12:22:17 -07:00
|
|
|
#include "IntelPTCollector.h"
|
2020-11-09 13:36:26 -08:00
|
|
|
|
2022-03-15 12:22:17 -07:00
|
|
|
#include "Perf.h"
|
2022-04-27 12:13:40 -07:00
|
|
|
#include "Procfs.h"
|
2020-11-09 13:36:26 -08:00
|
|
|
|
|
|
|
|
#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
|
|
|
|
|
#include "lldb/Host/linux/Support.h"
|
|
|
|
|
#include "lldb/Utility/StreamString.h"
|
|
|
|
|
|
2022-03-15 12:22:17 -07:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <linux/perf_event.h>
|
|
|
|
|
#include <sstream>
|
2020-11-09 13:36:26 -08:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
using namespace process_linux;
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
|
|
/// IntelPTThreadTraceCollection
|
|
|
|
|
|
|
|
|
|
bool IntelPTThreadTraceCollection::TracesThread(lldb::tid_t tid) const {
|
|
|
|
|
return m_thread_traces.count(tid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Error IntelPTThreadTraceCollection::TraceStop(lldb::tid_t tid) {
|
|
|
|
|
auto it = m_thread_traces.find(tid);
|
|
|
|
|
if (it == m_thread_traces.end())
|
|
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
|
"Thread %" PRIu64 " not currently traced", tid);
|
|
|
|
|
m_total_buffer_size -= it->second->GetTraceBufferSize();
|
|
|
|
|
m_thread_traces.erase(tid);
|
|
|
|
|
return Error::success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Error IntelPTThreadTraceCollection::TraceStart(
|
|
|
|
|
lldb::tid_t tid, const TraceIntelPTStartRequest &request) {
|
|
|
|
|
if (TracesThread(tid))
|
|
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
|
"Thread %" PRIu64 " already traced", tid);
|
|
|
|
|
|
2022-04-28 17:11:57 -07:00
|
|
|
Expected<IntelPTSingleBufferTraceUP> trace_up =
|
2022-05-04 13:24:49 -07:00
|
|
|
IntelPTSingleBufferTrace::Start(request, tid, /*core_id=*/None,
|
|
|
|
|
TraceCollectionState::Running);
|
2020-11-09 13:36:26 -08:00
|
|
|
if (!trace_up)
|
|
|
|
|
return trace_up.takeError();
|
|
|
|
|
|
|
|
|
|
m_total_buffer_size += (*trace_up)->GetTraceBufferSize();
|
|
|
|
|
m_thread_traces.try_emplace(tid, std::move(*trace_up));
|
|
|
|
|
return Error::success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t IntelPTThreadTraceCollection::GetTotalBufferSize() const {
|
|
|
|
|
return m_total_buffer_size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<TraceThreadState>
|
|
|
|
|
IntelPTThreadTraceCollection::GetThreadStates() const {
|
|
|
|
|
std::vector<TraceThreadState> states;
|
|
|
|
|
for (const auto &it : m_thread_traces)
|
2022-05-09 21:44:09 -07:00
|
|
|
states.push_back({it.first,
|
2022-04-28 17:11:57 -07:00
|
|
|
{TraceBinaryData{IntelPTDataKinds::kTraceBuffer,
|
2022-05-09 21:44:09 -07:00
|
|
|
it.second->GetTraceBufferSize()}}});
|
2020-11-09 13:36:26 -08:00
|
|
|
return states;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 13:24:49 -07:00
|
|
|
Expected<IntelPTSingleBufferTrace &>
|
|
|
|
|
IntelPTThreadTraceCollection::GetTracedThread(lldb::tid_t tid) {
|
2020-11-09 13:36:26 -08:00
|
|
|
auto it = m_thread_traces.find(tid);
|
|
|
|
|
if (it == m_thread_traces.end())
|
|
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
|
"Thread %" PRIu64 " not currently traced", tid);
|
|
|
|
|
return *it->second.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IntelPTThreadTraceCollection::Clear() {
|
|
|
|
|
m_thread_traces.clear();
|
|
|
|
|
m_total_buffer_size = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 19:10:39 -07:00
|
|
|
size_t IntelPTThreadTraceCollection::GetTracedThreadsCount() const {
|
|
|
|
|
return m_thread_traces.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// IntelPTPerThreadProcessTrace
|
2020-11-09 13:36:26 -08:00
|
|
|
|
2022-05-02 19:10:39 -07:00
|
|
|
bool IntelPTPerThreadProcessTrace::TracesThread(lldb::tid_t tid) const {
|
2020-11-09 13:36:26 -08:00
|
|
|
return m_thread_traces.TracesThread(tid);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 19:10:39 -07:00
|
|
|
Error IntelPTPerThreadProcessTrace::TraceStop(lldb::tid_t tid) {
|
2020-11-09 13:36:26 -08:00
|
|
|
return m_thread_traces.TraceStop(tid);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 19:10:39 -07:00
|
|
|
Error IntelPTPerThreadProcessTrace::TraceStart(lldb::tid_t tid) {
|
2022-04-28 14:00:44 -07:00
|
|
|
if (m_thread_traces.GetTotalBufferSize() +
|
|
|
|
|
m_tracing_params.trace_buffer_size >
|
|
|
|
|
static_cast<size_t>(*m_tracing_params.process_buffer_size_limit))
|
2020-11-09 13:36:26 -08:00
|
|
|
return createStringError(
|
|
|
|
|
inconvertibleErrorCode(),
|
|
|
|
|
"Thread %" PRIu64 " can't be traced as the process trace size limit "
|
|
|
|
|
"has been reached. Consider retracing with a higher "
|
|
|
|
|
"limit.",
|
|
|
|
|
tid);
|
|
|
|
|
|
|
|
|
|
return m_thread_traces.TraceStart(tid, m_tracing_params);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 13:24:49 -07:00
|
|
|
IntelPTThreadTraceCollection &IntelPTPerThreadProcessTrace::GetThreadTraces() {
|
2020-11-09 13:36:26 -08:00
|
|
|
return m_thread_traces;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 06:06:55 -07:00
|
|
|
/// IntelPTCollector
|
2020-11-09 13:36:26 -08:00
|
|
|
|
2022-05-02 19:10:39 -07:00
|
|
|
IntelPTCollector::IntelPTCollector(NativeProcessProtocol &process)
|
|
|
|
|
: m_process(process) {
|
2022-03-22 06:15:56 -07:00
|
|
|
if (Expected<LinuxPerfZeroTscConversion> tsc_conversion =
|
|
|
|
|
LoadPerfTscConversionParameters())
|
|
|
|
|
m_tsc_conversion =
|
|
|
|
|
std::make_unique<LinuxPerfZeroTscConversion>(*tsc_conversion);
|
|
|
|
|
else
|
|
|
|
|
LLDB_LOG_ERROR(GetLog(POSIXLog::Trace), tsc_conversion.takeError(),
|
|
|
|
|
"unable to load TSC to wall time conversion: {0}");
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 06:06:55 -07:00
|
|
|
Error IntelPTCollector::TraceStop(lldb::tid_t tid) {
|
2022-05-02 19:10:39 -07:00
|
|
|
if (m_per_thread_process_trace_up &&
|
|
|
|
|
m_per_thread_process_trace_up->TracesThread(tid))
|
|
|
|
|
return m_per_thread_process_trace_up->TraceStop(tid);
|
|
|
|
|
else if (m_per_core_process_trace_up)
|
|
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
|
"Can't stop tracing an individual thread when "
|
|
|
|
|
"per-core process tracing is enabled.");
|
2020-11-09 13:36:26 -08:00
|
|
|
return m_thread_traces.TraceStop(tid);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 06:06:55 -07:00
|
|
|
Error IntelPTCollector::TraceStop(const TraceStopRequest &request) {
|
2020-11-09 13:36:26 -08:00
|
|
|
if (request.IsProcessTracing()) {
|
2021-06-01 15:34:06 -07:00
|
|
|
Clear();
|
2020-11-09 13:36:26 -08:00
|
|
|
return Error::success();
|
|
|
|
|
} else {
|
|
|
|
|
Error error = Error::success();
|
|
|
|
|
for (int64_t tid : *request.tids)
|
|
|
|
|
error = joinErrors(std::move(error),
|
|
|
|
|
TraceStop(static_cast<lldb::tid_t>(tid)));
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 19:10:39 -07:00
|
|
|
Expected<IntelPTPerThreadProcessTraceUP>
|
|
|
|
|
IntelPTPerThreadProcessTrace::Start(const TraceIntelPTStartRequest &request,
|
|
|
|
|
ArrayRef<lldb::tid_t> current_tids) {
|
|
|
|
|
IntelPTPerThreadProcessTraceUP trace(
|
|
|
|
|
new IntelPTPerThreadProcessTrace(request));
|
|
|
|
|
|
|
|
|
|
Error error = Error::success();
|
|
|
|
|
for (lldb::tid_t tid : current_tids)
|
|
|
|
|
error = joinErrors(std::move(error), trace->TraceStart(tid));
|
|
|
|
|
if (error)
|
|
|
|
|
return std::move(error);
|
|
|
|
|
return trace;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Error IntelPTCollector::TraceStart(const TraceIntelPTStartRequest &request) {
|
2020-11-09 13:36:26 -08:00
|
|
|
if (request.IsProcessTracing()) {
|
|
|
|
|
if (IsProcessTracingEnabled()) {
|
|
|
|
|
return createStringError(
|
|
|
|
|
inconvertibleErrorCode(),
|
|
|
|
|
"Process currently traced. Stop process tracing first");
|
|
|
|
|
}
|
2022-05-02 19:10:39 -07:00
|
|
|
if (request.IsPerCoreTracing()) {
|
|
|
|
|
if (m_thread_traces.GetTracedThreadsCount() > 0)
|
|
|
|
|
return createStringError(
|
|
|
|
|
inconvertibleErrorCode(),
|
|
|
|
|
"Threads currently traced. Stop tracing them first.");
|
|
|
|
|
if (Expected<IntelPTMultiCoreTraceUP> trace =
|
|
|
|
|
IntelPTMultiCoreTrace::StartOnAllCores(request)) {
|
|
|
|
|
m_per_core_process_trace_up = std::move(*trace);
|
|
|
|
|
return Error::success();
|
|
|
|
|
} else {
|
|
|
|
|
return trace.takeError();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
std::vector<lldb::tid_t> process_threads;
|
|
|
|
|
for (size_t i = 0; m_process.GetThreadAtIndex(i); i++)
|
|
|
|
|
process_threads.push_back(m_process.GetThreadAtIndex(i)->GetID());
|
|
|
|
|
|
|
|
|
|
// per-thread process tracing
|
|
|
|
|
if (Expected<IntelPTPerThreadProcessTraceUP> trace =
|
|
|
|
|
IntelPTPerThreadProcessTrace::Start(request, process_threads)) {
|
|
|
|
|
m_per_thread_process_trace_up = std::move(trace.get());
|
|
|
|
|
return Error::success();
|
|
|
|
|
} else {
|
|
|
|
|
return trace.takeError();
|
|
|
|
|
}
|
2022-04-28 14:00:44 -07:00
|
|
|
}
|
2020-11-09 13:36:26 -08:00
|
|
|
} else {
|
2022-05-02 19:10:39 -07:00
|
|
|
// individual thread tracing
|
|
|
|
|
if (m_per_core_process_trace_up)
|
|
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
|
"Process currently traced with per-core "
|
|
|
|
|
"tracing. Stop process tracing first");
|
|
|
|
|
|
2020-11-09 13:36:26 -08:00
|
|
|
Error error = Error::success();
|
|
|
|
|
for (int64_t tid : *request.tids)
|
|
|
|
|
error = joinErrors(std::move(error),
|
|
|
|
|
m_thread_traces.TraceStart(tid, request));
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 13:24:49 -07:00
|
|
|
void IntelPTCollector::OnProcessStateChanged(lldb::StateType state) {
|
|
|
|
|
if (m_per_core_process_trace_up)
|
|
|
|
|
m_per_core_process_trace_up->OnProcessStateChanged(state);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 06:06:55 -07:00
|
|
|
Error IntelPTCollector::OnThreadCreated(lldb::tid_t tid) {
|
2022-05-02 19:10:39 -07:00
|
|
|
if (m_per_thread_process_trace_up)
|
|
|
|
|
return m_per_thread_process_trace_up->TraceStart(tid);
|
|
|
|
|
|
|
|
|
|
return Error::success();
|
2020-11-09 13:36:26 -08:00
|
|
|
}
|
|
|
|
|
|
2022-03-15 06:06:55 -07:00
|
|
|
Error IntelPTCollector::OnThreadDestroyed(lldb::tid_t tid) {
|
2022-05-02 19:10:39 -07:00
|
|
|
if (m_per_thread_process_trace_up &&
|
|
|
|
|
m_per_thread_process_trace_up->TracesThread(tid))
|
|
|
|
|
return m_per_thread_process_trace_up->TraceStop(tid);
|
2020-11-09 13:36:26 -08:00
|
|
|
else if (m_thread_traces.TracesThread(tid))
|
|
|
|
|
return m_thread_traces.TraceStop(tid);
|
|
|
|
|
return Error::success();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 06:06:55 -07:00
|
|
|
Expected<json::Value> IntelPTCollector::GetState() const {
|
2022-04-27 12:13:40 -07:00
|
|
|
Expected<ArrayRef<uint8_t>> cpu_info = GetProcfsCpuInfo();
|
2020-11-09 13:36:26 -08:00
|
|
|
if (!cpu_info)
|
|
|
|
|
return cpu_info.takeError();
|
|
|
|
|
|
|
|
|
|
TraceGetStateResponse state;
|
2022-05-02 19:10:39 -07:00
|
|
|
state.process_binary_data.push_back(
|
2022-05-09 21:44:09 -07:00
|
|
|
{IntelPTDataKinds::kProcFsCpuInfo, cpu_info->size()});
|
2020-11-09 13:36:26 -08:00
|
|
|
|
|
|
|
|
std::vector<TraceThreadState> thread_states =
|
|
|
|
|
m_thread_traces.GetThreadStates();
|
2022-05-02 19:10:39 -07:00
|
|
|
state.traced_threads.insert(state.traced_threads.end(), thread_states.begin(),
|
|
|
|
|
thread_states.end());
|
|
|
|
|
|
|
|
|
|
if (m_per_thread_process_trace_up) {
|
|
|
|
|
thread_states =
|
|
|
|
|
m_per_thread_process_trace_up->GetThreadTraces().GetThreadStates();
|
|
|
|
|
state.traced_threads.insert(state.traced_threads.end(),
|
|
|
|
|
thread_states.begin(), thread_states.end());
|
|
|
|
|
} else if (m_per_core_process_trace_up) {
|
|
|
|
|
for (size_t i = 0; m_process.GetThreadAtIndex(i); i++)
|
|
|
|
|
state.traced_threads.push_back(
|
|
|
|
|
TraceThreadState{m_process.GetThreadAtIndex(i)->GetID(), {}});
|
|
|
|
|
|
|
|
|
|
state.cores.emplace();
|
|
|
|
|
m_per_core_process_trace_up->ForEachCore(
|
|
|
|
|
[&](lldb::core_id_t core_id,
|
|
|
|
|
const IntelPTSingleBufferTrace &core_trace) {
|
|
|
|
|
state.cores->push_back({core_id,
|
|
|
|
|
{{IntelPTDataKinds::kTraceBuffer,
|
|
|
|
|
core_trace.GetTraceBufferSize()}}});
|
|
|
|
|
});
|
2020-11-09 13:36:26 -08:00
|
|
|
}
|
|
|
|
|
return toJSON(state);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 13:24:49 -07:00
|
|
|
Expected<IntelPTSingleBufferTrace &>
|
|
|
|
|
IntelPTCollector::GetTracedThread(lldb::tid_t tid) {
|
2022-05-02 19:10:39 -07:00
|
|
|
if (m_per_thread_process_trace_up &&
|
|
|
|
|
m_per_thread_process_trace_up->TracesThread(tid))
|
|
|
|
|
return m_per_thread_process_trace_up->GetThreadTraces().GetTracedThread(
|
|
|
|
|
tid);
|
2020-11-09 13:36:26 -08:00
|
|
|
return m_thread_traces.GetTracedThread(tid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Expected<std::vector<uint8_t>>
|
2022-05-04 13:24:49 -07:00
|
|
|
IntelPTCollector::GetBinaryData(const TraceGetBinaryDataRequest &request) {
|
2022-04-28 17:11:57 -07:00
|
|
|
if (request.kind == IntelPTDataKinds::kTraceBuffer) {
|
2022-05-04 13:24:49 -07:00
|
|
|
if (Expected<IntelPTSingleBufferTrace &> trace =
|
2020-11-09 13:36:26 -08:00
|
|
|
GetTracedThread(*request.tid))
|
2022-04-28 17:11:57 -07:00
|
|
|
return trace->GetTraceBuffer(request.offset, request.size);
|
2020-11-09 13:36:26 -08:00
|
|
|
else
|
|
|
|
|
return trace.takeError();
|
2022-04-27 12:13:40 -07:00
|
|
|
} else if (request.kind == IntelPTDataKinds::kProcFsCpuInfo) {
|
|
|
|
|
return GetProcfsCpuInfo();
|
2020-11-09 13:36:26 -08:00
|
|
|
}
|
|
|
|
|
return createStringError(inconvertibleErrorCode(),
|
|
|
|
|
"Unsuported trace binary data kind: %s",
|
|
|
|
|
request.kind.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 19:10:39 -07:00
|
|
|
void IntelPTCollector::ClearProcessTracing() {
|
|
|
|
|
m_per_thread_process_trace_up.reset();
|
|
|
|
|
m_per_core_process_trace_up.reset();
|
|
|
|
|
}
|
2020-11-09 13:36:26 -08:00
|
|
|
|
2022-03-15 06:06:55 -07:00
|
|
|
bool IntelPTCollector::IsSupported() {
|
2022-04-28 17:11:57 -07:00
|
|
|
if (Expected<uint32_t> intel_pt_type = GetIntelPTOSEventType()) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
2021-04-22 22:24:05 +02:00
|
|
|
llvm::consumeError(intel_pt_type.takeError());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-09 13:36:26 -08:00
|
|
|
|
2022-03-15 06:06:55 -07:00
|
|
|
bool IntelPTCollector::IsProcessTracingEnabled() const {
|
2022-05-02 19:10:39 -07:00
|
|
|
return (bool)m_per_thread_process_trace_up ||
|
|
|
|
|
(bool)m_per_core_process_trace_up;
|
2020-11-09 13:36:26 -08:00
|
|
|
}
|
|
|
|
|
|
2022-03-15 06:06:55 -07:00
|
|
|
void IntelPTCollector::Clear() {
|
2020-11-09 13:36:26 -08:00
|
|
|
ClearProcessTracing();
|
|
|
|
|
m_thread_traces.Clear();
|
|
|
|
|
}
|