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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
|
//===-- Trace.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 "lldb/Target/Trace.h"
#include "llvm/Support/Format.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Symbol/Function.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/SectionLoadList.h"
#include "lldb/Target/Thread.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Stream.h"
#include <optional>
using namespace lldb;
using namespace lldb_private;
using namespace llvm;
// Helper structs used to extract the type of a JSON trace bundle description
// object without having to parse the entire object.
struct JSONSimpleTraceBundleDescription {
std::string type;
};
namespace llvm {
namespace json {
bool fromJSON(const Value &value, JSONSimpleTraceBundleDescription &bundle,
Path path) {
json::ObjectMapper o(value, path);
return o && o.map("type", bundle.type);
}
} // namespace json
} // namespace llvm
/// Helper functions for fetching data in maps and returning Optionals or
/// pointers instead of iterators for simplicity. It's worth mentioning that the
/// Optionals version can't return the inner data by reference because of
/// limitations in move constructors.
/// \{
template <typename K, typename V>
static std::optional<V> Lookup(DenseMap<K, V> &map, K k) {
auto it = map.find(k);
if (it == map.end())
return std::nullopt;
return it->second;
}
template <typename K, typename V>
static V *LookupAsPtr(DenseMap<K, V> &map, K k) {
auto it = map.find(k);
if (it == map.end())
return nullptr;
return &it->second;
}
/// Similar to the methods above but it looks for an item in a map of maps.
template <typename K1, typename K2, typename V>
static std::optional<V> Lookup(DenseMap<K1, DenseMap<K2, V>> &map, K1 k1,
K2 k2) {
auto it = map.find(k1);
if (it == map.end())
return std::nullopt;
return Lookup(it->second, k2);
}
/// Similar to the methods above but it looks for an item in a map of maps.
template <typename K1, typename K2, typename V>
static V *LookupAsPtr(DenseMap<K1, DenseMap<K2, V>> &map, K1 k1, K2 k2) {
auto it = map.find(k1);
if (it == map.end())
return nullptr;
return LookupAsPtr(it->second, k2);
}
/// \}
static Error createInvalidPlugInError(StringRef plugin_name) {
return createStringError(
std::errc::invalid_argument,
"no trace plug-in matches the specified type: \"%s\"",
plugin_name.data());
}
Expected<lldb::TraceSP>
Trace::LoadPostMortemTraceFromFile(Debugger &debugger,
const FileSpec &trace_description_file) {
auto buffer_or_error =
MemoryBuffer::getFile(trace_description_file.GetPath());
if (!buffer_or_error) {
return createStringError(std::errc::invalid_argument,
"could not open input file: %s - %s.",
trace_description_file.GetPath().c_str(),
buffer_or_error.getError().message().c_str());
}
Expected<json::Value> session_file =
json::parse(buffer_or_error.get()->getBuffer().str());
if (!session_file) {
return session_file.takeError();
}
return Trace::FindPluginForPostMortemProcess(
debugger, *session_file,
trace_description_file.GetDirectory().AsCString());
}
Expected<lldb::TraceSP> Trace::FindPluginForPostMortemProcess(
Debugger &debugger, const json::Value &trace_bundle_description,
StringRef bundle_dir) {
JSONSimpleTraceBundleDescription json_bundle;
json::Path::Root root("traceBundle");
if (!json::fromJSON(trace_bundle_description, json_bundle, root))
return root.getError();
if (auto create_callback =
PluginManager::GetTraceCreateCallback(json_bundle.type))
return create_callback(trace_bundle_description, bundle_dir, debugger);
return createInvalidPlugInError(json_bundle.type);
}
Expected<lldb::TraceSP> Trace::FindPluginForLiveProcess(llvm::StringRef name,
Process &process) {
if (!process.IsLiveDebugSession())
return createStringError(inconvertibleErrorCode(),
"Can't trace non-live processes");
if (auto create_callback =
PluginManager::GetTraceCreateCallbackForLiveProcess(name))
return create_callback(process);
return createInvalidPlugInError(name);
}
Expected<StringRef> Trace::FindPluginSchema(StringRef name) {
StringRef schema = PluginManager::GetTraceSchema(name);
if (!schema.empty())
return schema;
return createInvalidPlugInError(name);
}
Error Trace::Start(const llvm::json::Value &request) {
if (!m_live_process)
return createStringError(
inconvertibleErrorCode(),
"Attempted to start tracing without a live process.");
return m_live_process->TraceStart(request);
}
Error Trace::Stop() {
if (!m_live_process)
return createStringError(
inconvertibleErrorCode(),
"Attempted to stop tracing without a live process.");
return m_live_process->TraceStop(TraceStopRequest(GetPluginName()));
}
Error Trace::Stop(llvm::ArrayRef<lldb::tid_t> tids) {
if (!m_live_process)
return createStringError(
inconvertibleErrorCode(),
"Attempted to stop tracing without a live process.");
return m_live_process->TraceStop(TraceStopRequest(GetPluginName(), tids));
}
Expected<std::string> Trace::GetLiveProcessState() {
if (!m_live_process)
return createStringError(
inconvertibleErrorCode(),
"Attempted to fetch live trace information without a live process.");
return m_live_process->TraceGetState(GetPluginName());
}
std::optional<uint64_t>
Trace::GetLiveThreadBinaryDataSize(lldb::tid_t tid, llvm::StringRef kind) {
Storage &storage = GetUpdatedStorage();
return Lookup(storage.live_thread_data, tid, ConstString(kind));
}
std::optional<uint64_t> Trace::GetLiveCpuBinaryDataSize(lldb::cpu_id_t cpu_id,
llvm::StringRef kind) {
Storage &storage = GetUpdatedStorage();
return Lookup(storage.live_cpu_data_sizes, cpu_id, ConstString(kind));
}
std::optional<uint64_t>
Trace::GetLiveProcessBinaryDataSize(llvm::StringRef kind) {
Storage &storage = GetUpdatedStorage();
return Lookup(storage.live_process_data, ConstString(kind));
}
Expected<std::vector<uint8_t>>
Trace::GetLiveTraceBinaryData(const TraceGetBinaryDataRequest &request,
uint64_t expected_size) {
if (!m_live_process)
return createStringError(
inconvertibleErrorCode(),
formatv("Attempted to fetch live trace data without a live process. "
"Data kind = {0}, tid = {1}, cpu id = {2}.",
request.kind, request.tid, request.cpu_id));
Expected<std::vector<uint8_t>> data =
m_live_process->TraceGetBinaryData(request);
if (!data)
return data.takeError();
if (data->size() != expected_size)
return createStringError(
inconvertibleErrorCode(),
formatv("Got incomplete live trace data. Data kind = {0}, expected "
"size = {1}, actual size = {2}, tid = {3}, cpu id = {4}",
request.kind, expected_size, data->size(), request.tid,
request.cpu_id));
return data;
}
Expected<std::vector<uint8_t>>
Trace::GetLiveThreadBinaryData(lldb::tid_t tid, llvm::StringRef kind) {
std::optional<uint64_t> size = GetLiveThreadBinaryDataSize(tid, kind);
if (!size)
return createStringError(
inconvertibleErrorCode(),
"Tracing data \"%s\" is not available for thread %" PRIu64 ".",
kind.data(), tid);
TraceGetBinaryDataRequest request{GetPluginName().str(), kind.str(), tid,
/*cpu_id=*/std::nullopt};
return GetLiveTraceBinaryData(request, *size);
}
Expected<std::vector<uint8_t>>
Trace::GetLiveCpuBinaryData(lldb::cpu_id_t cpu_id, llvm::StringRef kind) {
if (!m_live_process)
return createStringError(
inconvertibleErrorCode(),
"Attempted to fetch live cpu data without a live process.");
std::optional<uint64_t> size = GetLiveCpuBinaryDataSize(cpu_id, kind);
if (!size)
return createStringError(
inconvertibleErrorCode(),
"Tracing data \"%s\" is not available for cpu_id %" PRIu64 ".",
kind.data(), cpu_id);
TraceGetBinaryDataRequest request{GetPluginName().str(), kind.str(),
/*tid=*/std::nullopt, cpu_id};
return m_live_process->TraceGetBinaryData(request);
}
Expected<std::vector<uint8_t>>
Trace::GetLiveProcessBinaryData(llvm::StringRef kind) {
std::optional<uint64_t> size = GetLiveProcessBinaryDataSize(kind);
if (!size)
return createStringError(
inconvertibleErrorCode(),
"Tracing data \"%s\" is not available for the process.", kind.data());
TraceGetBinaryDataRequest request{GetPluginName().str(), kind.str(),
/*tid=*/std::nullopt,
/*cpu_id*/ std::nullopt};
return GetLiveTraceBinaryData(request, *size);
}
Trace::Storage &Trace::GetUpdatedStorage() {
RefreshLiveProcessState();
return m_storage;
}
const char *Trace::RefreshLiveProcessState() {
if (!m_live_process)
return nullptr;
uint32_t new_stop_id = m_live_process->GetStopID();
if (new_stop_id == m_stop_id)
return nullptr;
Log *log = GetLog(LLDBLog::Target);
LLDB_LOG(log, "Trace::RefreshLiveProcessState invoked");
m_stop_id = new_stop_id;
m_storage = Trace::Storage();
auto do_refresh = [&]() -> Error {
Expected<std::string> json_string = GetLiveProcessState();
if (!json_string)
return json_string.takeError();
Expected<TraceGetStateResponse> live_process_state =
json::parse<TraceGetStateResponse>(*json_string,
"TraceGetStateResponse");
if (!live_process_state)
return live_process_state.takeError();
if (live_process_state->warnings) {
for (std::string &warning : *live_process_state->warnings)
LLDB_LOG(log, "== Warning when fetching the trace state: {0}", warning);
}
for (const TraceThreadState &thread_state :
live_process_state->traced_threads) {
for (const TraceBinaryData &item : thread_state.binary_data)
m_storage.live_thread_data[thread_state.tid].insert(
{ConstString(item.kind), item.size});
}
LLDB_LOG(log, "== Found {0} threads being traced",
live_process_state->traced_threads.size());
if (live_process_state->cpus) {
m_storage.cpus.emplace();
for (const TraceCpuState &cpu_state : *live_process_state->cpus) {
m_storage.cpus->push_back(cpu_state.id);
for (const TraceBinaryData &item : cpu_state.binary_data)
m_storage.live_cpu_data_sizes[cpu_state.id].insert(
{ConstString(item.kind), item.size});
}
LLDB_LOG(log, "== Found {0} cpu cpus being traced",
live_process_state->cpus->size());
}
for (const TraceBinaryData &item : live_process_state->process_binary_data)
m_storage.live_process_data.insert({ConstString(item.kind), item.size});
return DoRefreshLiveProcessState(std::move(*live_process_state),
*json_string);
};
if (Error err = do_refresh()) {
m_storage.live_refresh_error = toString(std::move(err));
return m_storage.live_refresh_error->c_str();
}
return nullptr;
}
Trace::Trace(ArrayRef<ProcessSP> postmortem_processes,
std::optional<std::vector<lldb::cpu_id_t>> postmortem_cpus) {
for (ProcessSP process_sp : postmortem_processes)
m_storage.postmortem_processes.push_back(process_sp.get());
m_storage.cpus = postmortem_cpus;
}
Process *Trace::GetLiveProcess() { return m_live_process; }
ArrayRef<Process *> Trace::GetPostMortemProcesses() {
return m_storage.postmortem_processes;
}
std::vector<Process *> Trace::GetAllProcesses() {
if (Process *proc = GetLiveProcess())
return {proc};
return GetPostMortemProcesses();
}
uint32_t Trace::GetStopID() {
RefreshLiveProcessState();
return m_stop_id;
}
llvm::Expected<FileSpec>
Trace::GetPostMortemThreadDataFile(lldb::tid_t tid, llvm::StringRef kind) {
Storage &storage = GetUpdatedStorage();
if (std::optional<FileSpec> file =
Lookup(storage.postmortem_thread_data, tid, ConstString(kind)))
return *file;
else
return createStringError(
inconvertibleErrorCode(),
formatv("The thread with tid={0} doesn't have the tracing data {1}",
tid, kind));
}
llvm::Expected<FileSpec> Trace::GetPostMortemCpuDataFile(lldb::cpu_id_t cpu_id,
llvm::StringRef kind) {
Storage &storage = GetUpdatedStorage();
if (std::optional<FileSpec> file =
Lookup(storage.postmortem_cpu_data, cpu_id, ConstString(kind)))
return *file;
else
return createStringError(
inconvertibleErrorCode(),
formatv("The cpu with id={0} doesn't have the tracing data {1}", cpu_id,
kind));
}
void Trace::SetPostMortemThreadDataFile(lldb::tid_t tid, llvm::StringRef kind,
FileSpec file_spec) {
Storage &storage = GetUpdatedStorage();
storage.postmortem_thread_data[tid].insert({ConstString(kind), file_spec});
}
void Trace::SetPostMortemCpuDataFile(lldb::cpu_id_t cpu_id,
llvm::StringRef kind, FileSpec file_spec) {
Storage &storage = GetUpdatedStorage();
storage.postmortem_cpu_data[cpu_id].insert({ConstString(kind), file_spec});
}
llvm::Error
Trace::OnLiveThreadBinaryDataRead(lldb::tid_t tid, llvm::StringRef kind,
OnBinaryDataReadCallback callback) {
Expected<std::vector<uint8_t>> data = GetLiveThreadBinaryData(tid, kind);
if (!data)
return data.takeError();
return callback(*data);
}
llvm::Error Trace::OnLiveCpuBinaryDataRead(lldb::cpu_id_t cpu_id,
llvm::StringRef kind,
OnBinaryDataReadCallback callback) {
Storage &storage = GetUpdatedStorage();
if (std::vector<uint8_t> *cpu_data =
LookupAsPtr(storage.live_cpu_data, cpu_id, ConstString(kind)))
return callback(*cpu_data);
Expected<std::vector<uint8_t>> data = GetLiveCpuBinaryData(cpu_id, kind);
if (!data)
return data.takeError();
auto it = storage.live_cpu_data[cpu_id].insert(
{ConstString(kind), std::move(*data)});
return callback(it.first->second);
}
llvm::Error Trace::OnDataFileRead(FileSpec file,
OnBinaryDataReadCallback callback) {
ErrorOr<std::unique_ptr<MemoryBuffer>> trace_or_error =
MemoryBuffer::getFile(file.GetPath());
if (std::error_code err = trace_or_error.getError())
return createStringError(
inconvertibleErrorCode(), "Failed fetching trace-related file %s. %s",
file.GetPath().c_str(), toString(errorCodeToError(err)).c_str());
MemoryBuffer &data = **trace_or_error;
ArrayRef<uint8_t> array_ref(
reinterpret_cast<const uint8_t *>(data.getBufferStart()),
data.getBufferSize());
return callback(array_ref);
}
llvm::Error
Trace::OnPostMortemThreadBinaryDataRead(lldb::tid_t tid, llvm::StringRef kind,
OnBinaryDataReadCallback callback) {
if (Expected<FileSpec> file = GetPostMortemThreadDataFile(tid, kind))
return OnDataFileRead(*file, callback);
else
return file.takeError();
}
llvm::Error
Trace::OnPostMortemCpuBinaryDataRead(lldb::cpu_id_t cpu_id,
llvm::StringRef kind,
OnBinaryDataReadCallback callback) {
if (Expected<FileSpec> file = GetPostMortemCpuDataFile(cpu_id, kind))
return OnDataFileRead(*file, callback);
else
return file.takeError();
}
llvm::Error Trace::OnThreadBinaryDataRead(lldb::tid_t tid, llvm::StringRef kind,
OnBinaryDataReadCallback callback) {
if (m_live_process)
return OnLiveThreadBinaryDataRead(tid, kind, callback);
else
return OnPostMortemThreadBinaryDataRead(tid, kind, callback);
}
llvm::Error
Trace::OnAllCpusBinaryDataRead(llvm::StringRef kind,
OnCpusBinaryDataReadCallback callback) {
DenseMap<cpu_id_t, ArrayRef<uint8_t>> buffers;
Storage &storage = GetUpdatedStorage();
if (!storage.cpus)
return Error::success();
std::function<Error(std::vector<cpu_id_t>::iterator)> process_cpu =
[&](std::vector<cpu_id_t>::iterator cpu_id) -> Error {
if (cpu_id == storage.cpus->end())
return callback(buffers);
return OnCpuBinaryDataRead(*cpu_id, kind,
[&](ArrayRef<uint8_t> data) -> Error {
buffers.try_emplace(*cpu_id, data);
auto next_id = cpu_id;
next_id++;
return process_cpu(next_id);
});
};
return process_cpu(storage.cpus->begin());
}
llvm::Error Trace::OnCpuBinaryDataRead(lldb::cpu_id_t cpu_id,
llvm::StringRef kind,
OnBinaryDataReadCallback callback) {
if (m_live_process)
return OnLiveCpuBinaryDataRead(cpu_id, kind, callback);
else
return OnPostMortemCpuBinaryDataRead(cpu_id, kind, callback);
}
ArrayRef<lldb::cpu_id_t> Trace::GetTracedCpus() {
Storage &storage = GetUpdatedStorage();
if (storage.cpus)
return *storage.cpus;
return {};
}
std::vector<Process *> Trace::GetTracedProcesses() {
std::vector<Process *> processes;
Storage &storage = GetUpdatedStorage();
for (Process *proc : storage.postmortem_processes)
processes.push_back(proc);
if (m_live_process)
processes.push_back(m_live_process);
return processes;
}
|