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
264
|
//===-- IntelPTCollector.cpp ----------------------------------------------===//
//
// 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 "IntelPTCollector.h"
#include "Perf.h"
#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
#include "Procfs.h"
#include "lldb/Utility/StreamString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/MathExtras.h"
#include <algorithm>
#include <cstddef>
#include <fcntl.h>
#include <fstream>
#include <linux/perf_event.h>
#include <optional>
#include <sstream>
#include <sys/ioctl.h>
#include <sys/syscall.h>
using namespace lldb;
using namespace lldb_private;
using namespace process_linux;
using namespace llvm;
IntelPTCollector::IntelPTCollector(NativeProcessProtocol &process)
: m_process(process) {}
llvm::Expected<LinuxPerfZeroTscConversion &>
IntelPTCollector::FetchPerfTscConversionParameters() {
if (Expected<LinuxPerfZeroTscConversion> tsc_conversion =
LoadPerfTscConversionParameters())
return *tsc_conversion;
else
return createStringError(inconvertibleErrorCode(),
"Unable to load TSC to wall time conversion: %s",
toString(tsc_conversion.takeError()).c_str());
}
Error IntelPTCollector::TraceStop(lldb::tid_t tid) {
if (m_process_trace_up && m_process_trace_up->TracesThread(tid))
return m_process_trace_up->TraceStop(tid);
return m_thread_traces.TraceStop(tid);
}
Error IntelPTCollector::TraceStop(const TraceStopRequest &request) {
if (request.IsProcessTracing()) {
Clear();
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;
}
}
/// \return
/// some file descriptor in /sys/fs/ associated with the cgroup of the given
/// pid, or \a std::nullopt if the pid is not part of a cgroup.
static std::optional<int> GetCGroupFileDescriptor(lldb::pid_t pid) {
static std::optional<int> fd;
if (fd)
return fd;
std::ifstream ifile;
ifile.open(formatv("/proc/{0}/cgroup", pid));
if (!ifile)
return std::nullopt;
std::string line;
while (std::getline(ifile, line)) {
if (line.find("0:") != 0)
continue;
std::string slice = line.substr(line.find_first_of('/'));
if (slice.empty())
return std::nullopt;
std::string cgroup_file = formatv("/sys/fs/cgroup/{0}", slice);
// This cgroup should for the duration of the target, so we don't need to
// invoke close ourselves.
int maybe_fd = open(cgroup_file.c_str(), O_RDONLY);
if (maybe_fd != -1) {
fd = maybe_fd;
return fd;
}
}
return std::nullopt;
}
Error IntelPTCollector::TraceStart(const TraceIntelPTStartRequest &request) {
if (request.IsProcessTracing()) {
if (m_process_trace_up) {
return createStringError(
inconvertibleErrorCode(),
"Process currently traced. Stop process tracing first");
}
if (request.IsPerCpuTracing()) {
if (m_thread_traces.GetTracedThreadsCount() > 0)
return createStringError(
inconvertibleErrorCode(),
"Threads currently traced. Stop tracing them first.");
// CPU tracing is useless if we can't convert tsc to nanos.
Expected<LinuxPerfZeroTscConversion &> tsc_conversion =
FetchPerfTscConversionParameters();
if (!tsc_conversion)
return tsc_conversion.takeError();
// We force the enablement of TSCs, which is needed for correlating the
// cpu traces.
TraceIntelPTStartRequest effective_request = request;
effective_request.enable_tsc = true;
// We try to use cgroup filtering whenever possible
std::optional<int> cgroup_fd;
if (!request.disable_cgroup_filtering.value_or(false))
cgroup_fd = GetCGroupFileDescriptor(m_process.GetID());
if (Expected<IntelPTProcessTraceUP> trace =
IntelPTMultiCoreTrace::StartOnAllCores(effective_request,
m_process, cgroup_fd)) {
m_process_trace_up = std::move(*trace);
return Error::success();
} else {
return trace.takeError();
}
} else {
std::vector<lldb::tid_t> process_threads;
for (NativeThreadProtocol &thread : m_process.Threads())
process_threads.push_back(thread.GetID());
// per-thread process tracing
if (Expected<IntelPTProcessTraceUP> trace =
IntelPTPerThreadProcessTrace::Start(request, process_threads)) {
m_process_trace_up = std::move(trace.get());
return Error::success();
} else {
return trace.takeError();
}
}
} else {
// individual thread tracing
Error error = Error::success();
for (int64_t tid : *request.tids) {
if (m_process_trace_up && m_process_trace_up->TracesThread(tid))
error = joinErrors(
std::move(error),
createStringError(inconvertibleErrorCode(),
formatv("Thread with tid {0} is currently "
"traced. Stop tracing it first.",
tid)
.str()
.c_str()));
else
error = joinErrors(std::move(error),
m_thread_traces.TraceStart(tid, request));
}
return error;
}
}
void IntelPTCollector::ProcessWillResume() {
if (m_process_trace_up)
m_process_trace_up->ProcessWillResume();
}
void IntelPTCollector::ProcessDidStop() {
if (m_process_trace_up)
m_process_trace_up->ProcessDidStop();
}
Error IntelPTCollector::OnThreadCreated(lldb::tid_t tid) {
if (m_process_trace_up)
return m_process_trace_up->TraceStart(tid);
return Error::success();
}
Error IntelPTCollector::OnThreadDestroyed(lldb::tid_t tid) {
if (m_process_trace_up && m_process_trace_up->TracesThread(tid))
return m_process_trace_up->TraceStop(tid);
else if (m_thread_traces.TracesThread(tid))
return m_thread_traces.TraceStop(tid);
return Error::success();
}
Expected<json::Value> IntelPTCollector::GetState() {
Expected<ArrayRef<uint8_t>> cpu_info = GetProcfsCpuInfo();
if (!cpu_info)
return cpu_info.takeError();
TraceIntelPTGetStateResponse state;
if (m_process_trace_up)
state = m_process_trace_up->GetState();
state.process_binary_data.push_back(
{IntelPTDataKinds::kProcFsCpuInfo, cpu_info->size()});
m_thread_traces.ForEachThread(
[&](lldb::tid_t tid, const IntelPTSingleBufferTrace &thread_trace) {
state.traced_threads.push_back(
{tid,
{{IntelPTDataKinds::kIptTrace, thread_trace.GetIptTraceSize()}}});
});
if (Expected<LinuxPerfZeroTscConversion &> tsc_conversion =
FetchPerfTscConversionParameters())
state.tsc_perf_zero_conversion = *tsc_conversion;
else
state.AddWarning(toString(tsc_conversion.takeError()));
return toJSON(state);
}
Expected<std::vector<uint8_t>>
IntelPTCollector::GetBinaryData(const TraceGetBinaryDataRequest &request) {
if (request.kind == IntelPTDataKinds::kProcFsCpuInfo)
return GetProcfsCpuInfo();
if (m_process_trace_up) {
Expected<std::optional<std::vector<uint8_t>>> data =
m_process_trace_up->TryGetBinaryData(request);
if (!data)
return data.takeError();
if (*data)
return **data;
}
{
Expected<std::optional<std::vector<uint8_t>>> data =
m_thread_traces.TryGetBinaryData(request);
if (!data)
return data.takeError();
if (*data)
return **data;
}
return createStringError(
inconvertibleErrorCode(),
formatv("Can't fetch data kind {0} for cpu_id {1}, tid {2} and "
"\"process tracing\" mode {3}",
request.kind, request.cpu_id, request.tid,
m_process_trace_up ? "enabled" : "not enabled"));
}
bool IntelPTCollector::IsSupported() {
if (Expected<uint32_t> intel_pt_type = GetIntelPTOSEventType()) {
return true;
} else {
llvm::consumeError(intel_pt_type.takeError());
return false;
}
}
void IntelPTCollector::Clear() {
m_process_trace_up.reset();
m_thread_traces.Clear();
}
|