14#include "llvm/ADT/StringRef.h"
15#include "llvm/Support/Error.h"
16#include "llvm/Support/MathExtras.h"
20#include <linux/perf_event.h>
23#include <sys/syscall.h>
33llvm::Expected<LinuxPerfZeroTscConversion &>
35 if (Expected<LinuxPerfZeroTscConversion> tsc_conversion =
37 return *tsc_conversion;
39 return createStringError(inconvertibleErrorCode(),
40 "Unable to load TSC to wall time conversion: %s",
41 toString(tsc_conversion.takeError()).c_str());
53 return Error::success();
55 Error
error = Error::success();
56 for (int64_t tid : *request.
tids)
67 static std::optional<int> fd;
72 ifile.open(formatv(
"/proc/{0}/cgroup", pid));
77 while (std::getline(ifile, line)) {
78 if (line.find(
"0:") != 0)
81 std::string slice = line.substr(line.find_first_of(
'/'));
84 std::string cgroup_file = formatv(
"/sys/fs/cgroup/{0}", slice);
87 int maybe_fd = open(cgroup_file.c_str(), O_RDONLY);
99 return createStringError(
100 inconvertibleErrorCode(),
101 "Process currently traced. Stop process tracing first");
105 return createStringError(
106 inconvertibleErrorCode(),
107 "Threads currently traced. Stop tracing them first.");
109 Expected<LinuxPerfZeroTscConversion &> tsc_conversion =
112 return tsc_conversion.takeError();
120 std::optional<int> cgroup_fd;
124 if (Expected<IntelPTProcessTraceUP> trace =
128 return Error::success();
130 return trace.takeError();
133 std::vector<lldb::tid_t> process_threads;
135 process_threads.push_back(thread.GetID());
138 if (Expected<IntelPTProcessTraceUP> trace =
141 return Error::success();
143 return trace.takeError();
148 Error
error = Error::success();
149 for (int64_t tid : *request.
tids) {
153 createStringError(inconvertibleErrorCode(),
154 formatv(
"Thread with tid {0} is currently "
155 "traced. Stop tracing it first.",
181 return Error::success();
189 return Error::success();
195 return cpu_info.takeError();
211 if (Expected<LinuxPerfZeroTscConversion &> tsc_conversion =
219Expected<std::vector<uint8_t>>
225 Expected<std::optional<std::vector<uint8_t>>> data =
228 return data.takeError();
234 Expected<std::optional<std::vector<uint8_t>>> data =
237 return data.takeError();
242 return createStringError(
243 inconvertibleErrorCode(),
244 formatv(
"Can't fetch data kind {0} for cpu_id {1}, tid {2} and "
245 "\"process tracing\" mode {3}",
254 llvm::consumeError(intel_pt_type.takeError());
static llvm::raw_ostream & error(Stream &strm)
static std::optional< int > GetCGroupFileDescriptor(lldb::pid_t pid)
llvm::Expected< LinuxPerfZeroTscConversion & > FetchPerfTscConversionParameters()
llvm::Expected< std::vector< uint8_t > > GetBinaryData(const TraceGetBinaryDataRequest &request)
Implementation of the jLLDBTraceGetBinaryData packet.
llvm::Error TraceStop(const TraceStopRequest &request)
Implementation of the jLLDBTraceStop packet.
IntelPTProcessTraceUP m_process_trace_up
Only one instance of "process trace" can be active at a given time.
NativeProcessProtocol & m_process
The target process.
static bool IsSupported()
llvm::Error TraceStart(const TraceIntelPTStartRequest &request)
Implementation of the jLLDBTraceStart packet.
IntelPTThreadTraceCollection m_thread_traces
Threads traced due to "thread tracing".
llvm::Error OnThreadCreated(lldb::tid_t tid)
If "process tracing" is enabled, then trace the given thread.
IntelPTCollector(NativeProcessProtocol &process)
llvm::Error OnThreadDestroyed(lldb::tid_t tid)
Stops tracing a tracing upon a destroy event.
void Clear()
Dispose of all traces.
llvm::Expected< llvm::json::Value > GetState()
Implementation of the jLLDBTraceGetState packet.
void ProcessWillResume()
To be invoked before the process will resume, so that we can capture the first instructions after the...
void ProcessDidStop()
To be invoked as soon as we know the process stopped.
static llvm::Expected< std::unique_ptr< IntelPTMultiCoreTrace > > StartOnAllCores(const TraceIntelPTStartRequest &request, NativeProcessProtocol &process, std::optional< int > cgroup_fd=std::nullopt)
Start tracing all CPU cores.
static llvm::Expected< std::unique_ptr< IntelPTPerThreadProcessTrace > > Start(const TraceIntelPTStartRequest &request, llvm::ArrayRef< lldb::tid_t > current_tids)
Start tracing the current process by tracing each of its tids individually.
This class wraps a single perf event collecting intel pt data in a single buffer.
size_t GetIptTraceSize() const
llvm::Expected< uint32_t > GetIntelPTOSEventType()
Return the Linux perf event type for Intel PT.
llvm::Expected< LinuxPerfZeroTscConversion > LoadPerfTscConversionParameters()
Load PerfTscConversionParameters from perf_event_mmap_page, if available.
llvm::Expected< llvm::ArrayRef< uint8_t > > GetProcfsCpuInfo()
A class that represents a running process on the host machine.
llvm::json::Value toJSON(const Diagnostics::Report &report)
Render a diagnostics report as JSON, for diagnostics dump's terminal output.
std::string toString(FormatterBytecode::OpCodes op)
static const char * kIptTrace
static const char * kProcFsCpuInfo
jLLDBTraceGetBinaryData gdb-remote packet
std::optional< lldb::cpu_id_t > cpu_id
Optional core id if the data is related to a cpu core.
std::optional< lldb::tid_t > tid
Optional tid if the data is related to a thread.
std::string kind
Identifier for the data.
std::vector< TraceThreadState > traced_threads
std::vector< TraceBinaryData > process_binary_data
void AddWarning(llvm::StringRef warning)
std::optional< LinuxPerfZeroTscConversion > tsc_perf_zero_conversion
The TSC to wall time conversion if it exists, otherwise nullptr.
jLLDBTraceStart gdb-remote packet
bool IsPerCpuTracing() const
bool enable_tsc
Whether to enable TSC.
std::optional< bool > disable_cgroup_filtering
Disable the cgroup filtering that is automatically applied in per cpu mode.
bool IsProcessTracing() const
jLLDBTraceStart
std::optional< std::vector< lldb::tid_t > > tids
If std::nullopt, then this starts tracing the whole process.
jLLDBTraceStop gdb-remote packet
bool IsProcessTracing() const
std::optional< std::vector< lldb::tid_t > > tids
If std::nullopt, then this stops tracing the whole process.