1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
//===-- IntelPTManager.h -------------------------------------- -*- 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
//
//===----------------------------------------------------------------------===//
#ifndef liblldb_IntelPTManager_H_
#define liblldb_IntelPTManager_H_
#include "lldb/Utility/Status.h"
#include "lldb/Utility/TraceIntelPTGDBRemotePackets.h"
#include "lldb/lldb-types.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include <linux/perf_event.h>
#include <sys/mman.h>
#include <unistd.h>
namespace lldb_private {
namespace process_linux {
/// This class keeps track of one tracing instance of
/// Intel(R) Processor Trace on Linux OS at thread level.
///
/// The kernel interface for us is the perf_event_open.
class IntelPTThreadTrace;
typedef std::unique_ptr<IntelPTThreadTrace> IntelPTThreadTraceUP;
class IntelPTThreadTrace {
class munmap_delete {
size_t m_length;
public:
munmap_delete(size_t length) : m_length(length) {}
void operator()(void *ptr) {
if (m_length)
munmap(ptr, m_length);
}
};
class file_close {
public:
file_close() = default;
void operator()(int *ptr) {
if (ptr == nullptr)
return;
if (*ptr == -1)
return;
close(*ptr);
std::default_delete<int>()(ptr);
}
};
std::unique_ptr<perf_event_mmap_page, munmap_delete> m_mmap_meta;
std::unique_ptr<uint8_t, munmap_delete> m_mmap_aux;
std::unique_ptr<int, file_close> m_fd;
lldb::tid_t m_tid;
/// Start tracing a thread
///
/// \param[in] pid
/// The pid of the process whose thread will be traced.
///
/// \param[in] buffer_size
/// Size of the thread buffer in bytes.
///
/// \param[in] enable_tsc
/// Whether to use enable TSC timestamps or not.
/// More information in TraceIntelPT::GetStartConfigurationHelp().
///
/// \param[in] psb_period
/// This value defines the period in which PSB packets will be generated.
/// More information in TraceIntelPT::GetStartConfigurationHelp().
///
/// \return
/// \a llvm::Error::success if tracing was successful, or an
/// \a llvm::Error otherwise.
llvm::Error StartTrace(lldb::pid_t pid, lldb::tid_t tid, uint64_t buffer_size,
bool enable_tsc, llvm::Optional<size_t> psb_period);
llvm::MutableArrayRef<uint8_t> GetAuxBuffer() const;
llvm::MutableArrayRef<uint8_t> GetDataBuffer() const;
IntelPTThreadTrace()
: m_mmap_meta(nullptr, munmap_delete(0)),
m_mmap_aux(nullptr, munmap_delete(0)), m_fd(nullptr, file_close()) {}
public:
/// Get the content of /proc/cpuinfo that can be later used to decode traces.
static llvm::Expected<llvm::ArrayRef<uint8_t>> GetCPUInfo();
/// Start tracing a thread.
///
/// See \a StartTrace.
///
/// \return
/// A \a IntelPTThreadTrace instance if tracing was successful, or
/// an \a llvm::Error otherwise.
static llvm::Expected<IntelPTThreadTraceUP>
Create(lldb::pid_t pid, lldb::tid_t tid, size_t buffer_size, bool enable_tsc,
llvm::Optional<size_t> psb_period);
/// Read the trace buffer of the currently traced thread.
///
/// \param[in] offset
/// Offset of the data to read.
///
/// \param[in] size
/// Number of bytes to read.
///
/// \return
/// A vector with the requested binary data. The vector will have the
/// size of the requested \a size. Non-available positions will be
/// filled with zeroes.
llvm::Expected<std::vector<uint8_t>> GetIntelPTBuffer(size_t offset,
size_t size) const;
Status ReadPerfTraceAux(llvm::MutableArrayRef<uint8_t> &buffer,
size_t offset = 0) const;
Status ReadPerfTraceData(llvm::MutableArrayRef<uint8_t> &buffer,
size_t offset = 0) const;
/// Get the size in bytes of the aux section of the thread or process traced
/// by this object.
size_t GetTraceBufferSize() const;
/// Read data from a cyclic buffer
///
/// \param[in] [out] buf
/// Destination buffer, the buffer will be truncated to written size.
///
/// \param[in] src
/// Source buffer which must be a cyclic buffer.
///
/// \param[in] src_cyc_index
/// The index pointer (start of the valid data in the cyclic
/// buffer).
///
/// \param[in] offset
/// The offset to begin reading the data in the cyclic buffer.
static void ReadCyclicBuffer(llvm::MutableArrayRef<uint8_t> &dst,
llvm::MutableArrayRef<uint8_t> src,
size_t src_cyc_index, size_t offset);
/// Return the thread-specific part of the jLLDBTraceGetState packet.
TraceThreadState GetState() const;
};
/// Manages a list of thread traces.
class IntelPTThreadTraceCollection {
public:
IntelPTThreadTraceCollection(lldb::pid_t pid) : m_pid(pid) {}
/// Dispose of all traces
void Clear();
bool TracesThread(lldb::tid_t tid) const;
size_t GetTotalBufferSize() const;
std::vector<TraceThreadState> GetThreadStates() const;
llvm::Expected<const IntelPTThreadTrace &>
GetTracedThread(lldb::tid_t tid) const;
llvm::Error TraceStart(lldb::tid_t tid,
const TraceIntelPTStartRequest &request);
llvm::Error TraceStop(lldb::tid_t tid);
private:
lldb::pid_t m_pid;
llvm::DenseMap<lldb::tid_t, IntelPTThreadTraceUP> m_thread_traces;
/// Total actual thread buffer size in bytes
size_t m_total_buffer_size = 0;
};
/// Manages a "process trace" instance.
class IntelPTProcessTrace {
public:
IntelPTProcessTrace(lldb::pid_t pid, const TraceIntelPTStartRequest &request)
: m_thread_traces(pid), m_tracing_params(request) {}
bool TracesThread(lldb::tid_t tid) const;
const IntelPTThreadTraceCollection &GetThreadTraces() const;
llvm::Error TraceStart(lldb::tid_t tid);
llvm::Error TraceStop(lldb::tid_t tid);
private:
IntelPTThreadTraceCollection m_thread_traces;
/// Params used to trace threads when the user started "process tracing".
TraceIntelPTStartRequest m_tracing_params;
};
/// Main class that manages intel-pt process and thread tracing.
class IntelPTManager {
public:
IntelPTManager(lldb::pid_t pid) : m_pid(pid), m_thread_traces(pid) {}
static bool IsSupported();
/// If "process tracing" is enabled, then trace the given thread.
llvm::Error OnThreadCreated(lldb::tid_t tid);
/// Stops tracing a tracing upon a destroy event.
llvm::Error OnThreadDestroyed(lldb::tid_t tid);
/// Implementation of the jLLDBTraceStop packet
llvm::Error TraceStop(const TraceStopRequest &request);
/// Implementation of the jLLDBTraceStart packet
///
/// \param[in] process_threads
/// A list of all threads owned by the process.
llvm::Error TraceStart(const TraceIntelPTStartRequest &request,
const std::vector<lldb::tid_t> &process_threads);
/// Implementation of the jLLDBTraceGetState packet
llvm::Expected<llvm::json::Value> GetState() const;
/// Implementation of the jLLDBTraceGetBinaryData packet
llvm::Expected<std::vector<uint8_t>>
GetBinaryData(const TraceGetBinaryDataRequest &request) const;
/// Dispose of all traces
void Clear();
private:
llvm::Error TraceStop(lldb::tid_t tid);
/// Start tracing a specific thread.
llvm::Error TraceStart(lldb::tid_t tid,
const TraceIntelPTStartRequest &request);
llvm::Expected<const IntelPTThreadTrace &>
GetTracedThread(lldb::tid_t tid) const;
bool IsProcessTracingEnabled() const;
void ClearProcessTracing();
lldb::pid_t m_pid;
/// Threads traced due to "thread tracing"
IntelPTThreadTraceCollection m_thread_traces;
/// Threads traced due to "process tracing". Only one active "process tracing"
/// instance is assumed for a single process.
llvm::Optional<IntelPTProcessTrace> m_process_trace;
};
} // namespace process_linux
} // namespace lldb_private
#endif // liblldb_IntelPTManager_H_
|