Files
clang-p2996/lldb/source/Utility/TraceIntelPTGDBRemotePackets.cpp
Walter Erquinigo 1f49714d3e [trace][intelpt] Support system-wide tracing [4] - Support per core tracing on lldb-server
This diffs implements per-core tracing on lldb-server. It also includes tests that ensure that tracing can be initiated from the client and that the jLLDBGetState ppacket returns the list of trace buffers per core.

This doesn't include any decoder changes.

Finally, this makes some little changes here and there improving the existing code.

A specific piece of code that can't reliably be tested is when tracing
per core fails due to permissions. In this case we add a
troubleshooting message and this is the manual test:

```
/proc/sys/kernel/perf_event_paranoid set to 1

(lldb) process trace start --per-core-tracing                                         error: perf event syscall failed: Permission denied
 You might need that /proc/sys/kernel/perf_event_paranoid has a value of 0 or -1.
``

Differential Revision: https://reviews.llvm.org/D124858
2022-05-17 12:46:54 -07:00

116 lines
3.9 KiB
C++

//===-- TraceIntelPTGDBRemotePackets.cpp ------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "lldb/Utility/TraceIntelPTGDBRemotePackets.h"
using namespace llvm;
using namespace llvm::json;
namespace lldb_private {
const char *IntelPTDataKinds::kProcFsCpuInfo = "procfsCpuInfo";
const char *IntelPTDataKinds::kTraceBuffer = "traceBuffer";
bool TraceIntelPTStartRequest::IsPerCoreTracing() const {
return per_core_tracing.getValueOr(false);
}
bool fromJSON(const json::Value &value, TraceIntelPTStartRequest &packet,
Path path) {
ObjectMapper o(value, path);
if (!o || !fromJSON(value, (TraceStartRequest &)packet, path) ||
!o.map("enableTsc", packet.enable_tsc) ||
!o.map("psbPeriod", packet.psb_period) ||
!o.map("traceBufferSize", packet.trace_buffer_size))
return false;
if (packet.IsProcessTracing()) {
if (!o.map("processBufferSizeLimit", packet.process_buffer_size_limit) ||
!o.map("perCoreTracing", packet.per_core_tracing))
return false;
}
return true;
}
json::Value toJSON(const TraceIntelPTStartRequest &packet) {
json::Value base = toJSON((const TraceStartRequest &)packet);
json::Object &obj = *base.getAsObject();
obj.try_emplace("traceBufferSize", packet.trace_buffer_size);
obj.try_emplace("processBufferSizeLimit", packet.process_buffer_size_limit);
obj.try_emplace("psbPeriod", packet.psb_period);
obj.try_emplace("enableTsc", packet.enable_tsc);
obj.try_emplace("perCoreTracing", packet.per_core_tracing);
return base;
}
std::chrono::nanoseconds
LinuxPerfZeroTscConversion::Convert(uint64_t raw_counter_value) {
uint64_t quot = raw_counter_value >> m_time_shift;
uint64_t rem_flag = (((uint64_t)1 << m_time_shift) - 1);
uint64_t rem = raw_counter_value & rem_flag;
return std::chrono::nanoseconds{m_time_zero + quot * m_time_mult +
((rem * m_time_mult) >> m_time_shift)};
}
json::Value LinuxPerfZeroTscConversion::toJSON() {
return json::Value(json::Object{
{"kind", "tsc-perf-zero-conversion"},
{"time_mult", m_time_mult},
{"time_shift", m_time_shift},
{"time_zero", m_time_zero},
});
}
bool fromJSON(const json::Value &value, TraceTscConversionUP &tsc_conversion,
json::Path path) {
ObjectMapper o(value, path);
uint64_t time_mult, time_shift, time_zero;
if (!o || !o.map("time_mult", time_mult) ||
!o.map("time_shift", time_shift) || !o.map("time_zero", time_zero))
return false;
tsc_conversion = std::make_unique<LinuxPerfZeroTscConversion>(
static_cast<uint32_t>(time_mult), static_cast<uint16_t>(time_shift),
time_zero);
return true;
}
bool fromJSON(const json::Value &value, TraceIntelPTGetStateResponse &packet,
json::Path path) {
ObjectMapper o(value, path);
if (!o || !fromJSON(value, (TraceGetStateResponse &)packet, path))
return false;
const Object &obj = *(value.getAsObject());
if (const json::Value *counters = obj.get("counters")) {
json::Path subpath = path.field("counters");
ObjectMapper ocounters(*counters, subpath);
if (!ocounters || !ocounters.mapOptional("tsc-perf-zero-conversion",
packet.tsc_conversion))
return false;
}
return true;
}
json::Value toJSON(const TraceIntelPTGetStateResponse &packet) {
json::Value base = toJSON((const TraceGetStateResponse &)packet);
if (packet.tsc_conversion) {
std::vector<json::Value> counters{};
base.getAsObject()->try_emplace(
"counters", json::Object{{"tsc-perf-zero-conversion",
packet.tsc_conversion->toJSON()}});
}
return base;
}
} // namespace lldb_private