11#include "llvm/Support/Format.h"
40 json::ObjectMapper o(value, path);
41 return o && o.map(
"type", bundle.
type);
52template <
typename K,
typename V>
53static std::optional<V>
Lookup(DenseMap<K, V> &map, K k) {
54 auto it = map.find(k);
60template <
typename K,
typename V>
62 auto it = map.find(k);
69template <
typename K1,
typename K2,
typename V>
70static std::optional<V>
Lookup(DenseMap<K1, DenseMap<K2, V>> &map, K1 k1,
72 auto it = map.find(k1);
75 return Lookup(it->second, k2);
79template <
typename K1,
typename K2,
typename V>
80static V *
LookupAsPtr(DenseMap<K1, DenseMap<K2, V>> &map, K1 k1, K2 k2) {
81 auto it = map.find(k1);
89 return createStringError(
90 std::errc::invalid_argument,
91 "no trace plug-in matches the specified type: \"%s\"",
95Expected<lldb::TraceSP>
97 const FileSpec &trace_description_file) {
99 auto buffer_or_error =
100 MemoryBuffer::getFile(trace_description_file.
GetPath());
101 if (!buffer_or_error) {
102 return createStringError(std::errc::invalid_argument,
103 "could not open input file: %s - %s.",
104 trace_description_file.
GetPath().c_str(),
105 buffer_or_error.getError().message().c_str());
108 Expected<json::Value> session_file =
109 json::parse(buffer_or_error.get()->getBuffer().str());
111 return session_file.takeError();
115 debugger, *session_file, trace_description_file.
GetDirectory());
119 Debugger &debugger,
const json::Value &trace_bundle_description,
120 StringRef bundle_dir) {
122 json::Path::Root root(
"traceBundle");
124 return root.getError();
126 if (
auto create_callback =
128 return create_callback(trace_bundle_description, bundle_dir, debugger);
136 return createStringError(inconvertibleErrorCode(),
137 "Can't trace non-live processes");
139 if (
auto create_callback =
141 return create_callback(process);
156 return createStringError(
157 inconvertibleErrorCode(),
158 "Attempted to start tracing without a live process.");
164 return createStringError(
165 inconvertibleErrorCode(),
166 "Attempted to stop tracing without a live process.");
172 return createStringError(
173 inconvertibleErrorCode(),
174 "Attempted to stop tracing without a live process.");
180 return createStringError(
181 inconvertibleErrorCode(),
182 "Attempted to fetch live trace information without a live process.");
186std::optional<uint64_t>
193 llvm::StringRef kind) {
198std::optional<uint64_t>
204Expected<std::vector<uint8_t>>
206 uint64_t expected_size) {
208 return createStringError(
209 inconvertibleErrorCode(),
210 formatv(
"Attempted to fetch live trace data without a live process. "
211 "Data kind = {0}, tid = {1}, cpu id = {2}.",
214 Expected<std::vector<uint8_t>> data =
218 return data.takeError();
220 if (data->size() != expected_size)
221 return createStringError(
222 inconvertibleErrorCode(),
223 formatv(
"Got incomplete live trace data. Data kind = {0}, expected "
224 "size = {1}, actual size = {2}, tid = {3}, cpu id = {4}",
225 request.
kind, expected_size, data->size(), request.
tid,
231Expected<std::vector<uint8_t>>
235 return createStringError(
236 inconvertibleErrorCode(),
237 "Tracing data \"%s\" is not available for thread %" PRIu64
".",
245Expected<std::vector<uint8_t>>
248 return createStringError(
249 inconvertibleErrorCode(),
250 "Attempted to fetch live cpu data without a live process.");
253 return createStringError(
254 inconvertibleErrorCode(),
255 "Tracing data \"%s\" is not available for cpu_id %" PRIu64
".",
256 kind.data(), cpu_id);
259 std::nullopt, cpu_id};
263Expected<std::vector<uint8_t>>
267 return createStringError(
268 inconvertibleErrorCode(),
269 "Tracing data \"%s\" is not available for the process.", kind.data());
291 LLDB_LOG(log,
"Trace::RefreshLiveProcessState invoked");
296 auto do_refresh = [&]() -> Error {
299 return json_string.takeError();
301 Expected<TraceGetStateResponse> live_process_state =
302 json::parse<TraceGetStateResponse>(*json_string,
303 "TraceGetStateResponse");
304 if (!live_process_state)
305 return live_process_state.takeError();
307 if (live_process_state->warnings) {
308 for (std::string &
warning : *live_process_state->warnings)
309 LLDB_LOG(log,
"== Warning when fetching the trace state: {0}",
warning);
313 live_process_state->traced_threads) {
319 LLDB_LOG(log,
"== Found {0} threads being traced",
320 live_process_state->traced_threads.size());
322 if (live_process_state->cpus) {
324 for (
const TraceCpuState &cpu_state : *live_process_state->cpus) {
327 m_storage.live_cpu_data_sizes[cpu_state.
id].insert(
330 LLDB_LOG(log,
"== Found {0} cpu cpus being traced",
331 live_process_state->cpus->size());
334 for (
const TraceBinaryData &item : live_process_state->process_binary_data)
341 if (Error err = do_refresh()) {
343 return m_storage.live_refresh_error->c_str();
350 std::optional<std::vector<lldb::cpu_id_t>> postmortem_cpus) {
351 for (
ProcessSP process_sp : postmortem_processes)
373llvm::Expected<FileSpec>
376 if (std::optional<FileSpec> file =
380 return createStringError(
381 inconvertibleErrorCode(),
382 formatv(
"The thread with tid={0} doesn't have the tracing data {1}",
387 llvm::StringRef kind) {
389 if (std::optional<FileSpec> file =
393 return createStringError(
394 inconvertibleErrorCode(),
395 formatv(
"The cpu with id={0} doesn't have the tracing data {1}", cpu_id,
406 llvm::StringRef kind,
FileSpec file_spec) {
416 return data.takeError();
417 return callback(*data);
421 llvm::StringRef kind,
424 if (std::vector<uint8_t> *cpu_data =
426 return callback(*cpu_data);
430 return data.takeError();
433 return callback(it.first->second);
438 ErrorOr<std::unique_ptr<MemoryBuffer>> trace_or_error =
439 MemoryBuffer::getFile(file.
GetPath());
440 if (std::error_code err = trace_or_error.getError())
441 return createStringError(
442 inconvertibleErrorCode(),
"Failed fetching trace-related file %s. %s",
445 MemoryBuffer &data = **trace_or_error;
446 ArrayRef<uint8_t> array_ref(
447 reinterpret_cast<const uint8_t *
>(data.getBufferStart()),
448 data.getBufferSize());
449 return callback(array_ref);
458 return file.takeError();
463 llvm::StringRef kind,
468 return file.takeError();
482 DenseMap<cpu_id_t, ArrayRef<uint8_t>> buffers;
485 return Error::success();
487 std::function<
Error(std::vector<cpu_id_t>::iterator)> process_cpu =
488 [&](std::vector<cpu_id_t>::iterator cpu_id) -> Error {
489 if (cpu_id == storage.
cpus->end())
490 return callback(buffers);
493 [&](ArrayRef<uint8_t> data) -> Error {
494 buffers.try_emplace(*cpu_id, data);
495 auto next_id = cpu_id;
497 return process_cpu(next_id);
500 return process_cpu(storage.
cpus->begin());
504 llvm::StringRef kind,
515 return *storage.
cpus;
520 std::vector<Process *> processes;
524 processes.push_back(proc);
static llvm::raw_ostream & warning(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
static Error createInvalidPlugInError(StringRef plugin_name)
static V * LookupAsPtr(DenseMap< K, V > &map, K k)
static std::optional< V > Lookup(DenseMap< K, V > &map, K k)
Helper functions for fetching data in maps and returning Optionals or pointers instead of iterators f...
A uniqued constant string class.
A class to manage flag bits.
llvm::StringRef GetDirectory() const
Directory string const get accessor.
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
virtual llvm::StringRef GetPluginName()=0
static llvm::StringRef GetTraceSchema(llvm::StringRef plugin_name)
Get the JSON schema for a trace bundle description file corresponding to the given plugin.
static TraceCreateInstanceForLiveProcess GetTraceCreateCallbackForLiveProcess(llvm::StringRef plugin_name)
static TraceCreateInstanceFromBundle GetTraceCreateCallback(llvm::StringRef plugin_name)
A plug-in interface definition class for debugging a process.
virtual bool IsLiveDebugSession() const
Check if a process is a live debug session, or a corefile/post-mortem.
std::vector< Process * > GetTracedProcesses()
Return the list of processes traced by this instance.
static llvm::Error OnDataFileRead(FileSpec file, OnBinaryDataReadCallback callback)
Helper method for reading a data file and passing its data to the given callback.
llvm::Error OnPostMortemCpuBinaryDataRead(lldb::cpu_id_t cpu_id, llvm::StringRef kind, OnBinaryDataReadCallback callback)
Implementation of OnCpuBinaryDataRead() for post mortem cpus.
virtual llvm::Error Start(StructuredData::ObjectSP configuration=StructuredData::ObjectSP())=0
Start tracing a live process.
llvm::Error OnThreadBinaryDataRead(lldb::tid_t tid, llvm::StringRef kind, OnBinaryDataReadCallback callback)
Fetch binary data associated with a thread, either live or postmortem, and pass it to the given callb...
llvm::Expected< std::vector< uint8_t > > GetLiveCpuBinaryData(lldb::cpu_id_t cpu_id, llvm::StringRef kind)
Get binary data of a live cpu given a data identifier.
void SetPostMortemCpuDataFile(lldb::cpu_id_t cpu_id, llvm::StringRef kind, FileSpec file_spec)
Associate a given cpu with a data file using a data identifier.
llvm::Expected< std::vector< uint8_t > > GetLiveProcessBinaryData(llvm::StringRef kind)
Get binary data of the current process given a data identifier.
llvm::Expected< FileSpec > GetPostMortemThreadDataFile(lldb::tid_t tid, llvm::StringRef kind)
Get the file path containing data of a postmortem thread given a data identifier.
Storage & GetUpdatedStorage()
Get the storage after refreshing the data in the case of a live process.
llvm::Error OnLiveCpuBinaryDataRead(lldb::cpu_id_t cpu, llvm::StringRef kind, OnBinaryDataReadCallback callback)
Implementation of OnLiveBinaryDataRead() for live cpus.
llvm::Expected< std::string > GetLiveProcessState()
Get the current tracing state of a live process and its threads.
static llvm::Expected< lldb::TraceSP > FindPluginForLiveProcess(llvm::StringRef plugin_name, Process &process)
Find a trace plug-in to trace a live process.
std::optional< uint64_t > GetLiveCpuBinaryDataSize(lldb::cpu_id_t cpu_id, llvm::StringRef kind)
Get the size of the data returned by GetLiveCpuBinaryData.
Process * m_live_process
Process traced by this object if doing live tracing. Otherwise it's null.
llvm::Error Stop()
Stop tracing all current and future threads of a live process.
llvm::Error OnAllCpusBinaryDataRead(llvm::StringRef kind, OnCpusBinaryDataReadCallback callback)
Similar to OnCpuBinaryDataRead but this is able to fetch the same data from all cpus at once.
static llvm::Expected< lldb::TraceSP > LoadPostMortemTraceFromFile(Debugger &debugger, const FileSpec &trace_description_file)
Load a trace from a trace description file and create Targets, Processes and Threads based on the con...
Trace(llvm::ArrayRef< lldb::ProcessSP > postmortem_processes, std::optional< std::vector< lldb::cpu_id_t > > postmortem_cpus)
Constructor for post mortem processes.
struct lldb_private::Trace::Storage m_storage
llvm::Error OnPostMortemThreadBinaryDataRead(lldb::tid_t tid, llvm::StringRef kind, OnBinaryDataReadCallback callback)
Implementation of OnThreadBinaryDataRead() for post mortem threads.
static llvm::Expected< llvm::StringRef > FindPluginSchema(llvm::StringRef plugin_name)
Get the schema of a Trace plug-in given its name.
llvm::ArrayRef< lldb::cpu_id_t > GetTracedCpus()
llvm::Expected< std::vector< uint8_t > > GetLiveThreadBinaryData(lldb::tid_t tid, llvm::StringRef kind)
Get binary data of a live thread given a data identifier.
void SetPostMortemThreadDataFile(lldb::tid_t tid, llvm::StringRef kind, FileSpec file_spec)
Associate a given thread with a data file using a data identifier.
std::function< llvm::Error(llvm::ArrayRef< uint8_t > data)> OnBinaryDataReadCallback
const char * RefreshLiveProcessState()
Method to be invoked by the plug-in to refresh the live process state.
std::vector< Process * > GetAllProcesses()
static llvm::Expected< lldb::TraceSP > FindPluginForPostMortemProcess(Debugger &debugger, const llvm::json::Value &bundle_description, llvm::StringRef session_file_dir)
Find a trace plug-in using JSON data.
llvm::Expected< std::vector< uint8_t > > GetLiveTraceBinaryData(const TraceGetBinaryDataRequest &request, uint64_t expected_size)
Dispatcher for live trace data requests with some additional error checking.
Process * GetLiveProcess()
Get the currently traced live process.
llvm::Expected< FileSpec > GetPostMortemCpuDataFile(lldb::cpu_id_t cpu_id, llvm::StringRef kind)
Get the file path containing data of a postmortem cpu given a data identifier.
std::optional< uint64_t > GetLiveProcessBinaryDataSize(llvm::StringRef kind)
Get the size of the data returned by GetLiveProcessBinaryData.
llvm::ArrayRef< Process * > GetPostMortemProcesses()
Get the currently traced postmortem processes.
std::function< llvm::Error( const llvm::DenseMap< lldb::cpu_id_t, llvm::ArrayRef< uint8_t > > &cpu_to_data)> OnCpusBinaryDataReadCallback
llvm::Error OnLiveThreadBinaryDataRead(lldb::tid_t tid, llvm::StringRef kind, OnBinaryDataReadCallback callback)
Implementation of OnThreadBinaryDataRead() for live threads.
virtual llvm::Error DoRefreshLiveProcessState(TraceGetStateResponse state, llvm::StringRef json_response)=0
Method to be overriden by the plug-in to refresh its own state.
llvm::Error OnCpuBinaryDataRead(lldb::cpu_id_t cpu_id, llvm::StringRef kind, OnBinaryDataReadCallback callback)
Fetch binary data associated with a cpu, either live or postmortem, and pass it to the given callback...
std::optional< uint64_t > GetLiveThreadBinaryDataSize(lldb::tid_t tid, llvm::StringRef kind)
Get the size of the data returned by GetLiveThreadBinaryData.
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
std::string toString(FormatterBytecode::OpCodes op)
std::shared_ptr< lldb_private::Process > ProcessSP
bool fromJSON(const llvm::json::Value &value, lldb_private::JSONSection §ion, llvm::json::Path path)
std::string kind
Identifier of data to fetch with jLLDBTraceGetBinaryData.
uint64_t size
Size in bytes for this data.
std::vector< TraceBinaryData > binary_data
List of binary data objects for this core.
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.
jLLDBTraceStop gdb-remote packet
std::vector< TraceBinaryData > binary_data
List of binary data objects for this thread.
We package all the data that can change upon process stops to make sure this contract is very visible...
llvm::DenseMap< lldb::cpu_id_t, llvm::DenseMap< ConstString, uint64_t > > live_cpu_data_sizes
cpu id -> data kind -> size
llvm::DenseMap< lldb::cpu_id_t, llvm::DenseMap< ConstString, std::vector< uint8_t > > > live_cpu_data
cpu id -> data kind -> bytes
llvm::DenseMap< lldb::tid_t, llvm::DenseMap< ConstString, uint64_t > > live_thread_data
These data kinds are returned by lldb-server when fetching the state of the tracing session.
llvm::DenseMap< lldb::tid_t, llvm::DenseMap< ConstString, FileSpec > > postmortem_thread_data
Postmortem traces can specific additional data files, which are represented in this variable using a ...
llvm::DenseMap< ConstString, uint64_t > live_process_data
data kind -> size
std::optional< std::vector< lldb::cpu_id_t > > cpus
The list of cpus being traced.
llvm::DenseMap< lldb::cpu_id_t, llvm::DenseMap< ConstString, FileSpec > > postmortem_cpu_data
cpu id -> data kind -> file
std::vector< Process * > postmortem_processes
Portmortem processes traced by this object if doing non-live tracing.