11#include "llvm/Support/Format.h"
40 json::ObjectMapper o(value, path);
41 return o && o.map(
"type", bundle.
type);
53static std::optional<V>
Lookup(StringMap<V> &map, StringRef k) {
54 auto it = map.find(k);
60template <
typename V>
static V *
LookupAsPtr(StringMap<V> &map, StringRef k) {
61 auto it = map.find(k);
68template <
typename K,
typename V>
69static std::optional<V>
Lookup(DenseMap<K, StringMap<V>> &map, K k1,
71 auto it = map.find(k1);
74 return Lookup(it->second, k2);
78template <
typename K,
typename V>
79static V *
LookupAsPtr(DenseMap<K, StringMap<V>> &map, K k1, StringRef k2) {
80 auto it = map.find(k1);
88 return createStringError(
89 std::errc::invalid_argument,
90 "no trace plug-in matches the specified type: \"%s\"",
94Expected<lldb::TraceSP>
96 const FileSpec &trace_description_file) {
98 auto buffer_or_error =
99 MemoryBuffer::getFile(trace_description_file.
GetPath());
100 if (!buffer_or_error) {
101 return createStringError(std::errc::invalid_argument,
102 "could not open input file: %s - %s.",
103 trace_description_file.
GetPath().c_str(),
104 buffer_or_error.getError().message().c_str());
107 Expected<json::Value> session_file =
108 json::parse(buffer_or_error.get()->getBuffer().str());
110 return session_file.takeError();
114 debugger, *session_file, trace_description_file.
GetDirectory());
118 Debugger &debugger,
const json::Value &trace_bundle_description,
119 StringRef bundle_dir) {
121 json::Path::Root root(
"traceBundle");
123 return root.getError();
125 if (
auto create_callback =
127 return create_callback(trace_bundle_description, bundle_dir, debugger);
135 return createStringError(inconvertibleErrorCode(),
136 "Can't trace non-live processes");
138 if (
auto create_callback =
140 return create_callback(process);
155 return createStringError(
156 inconvertibleErrorCode(),
157 "Attempted to start tracing without a live process.");
163 return createStringError(
164 inconvertibleErrorCode(),
165 "Attempted to stop tracing without a live process.");
171 return createStringError(
172 inconvertibleErrorCode(),
173 "Attempted to stop tracing without a live process.");
179 return createStringError(
180 inconvertibleErrorCode(),
181 "Attempted to fetch live trace information without a live process.");
185std::optional<uint64_t>
192 llvm::StringRef kind) {
197std::optional<uint64_t>
203Expected<std::vector<uint8_t>>
205 uint64_t expected_size) {
207 return createStringError(
208 inconvertibleErrorCode(),
209 formatv(
"Attempted to fetch live trace data without a live process. "
210 "Data kind = {0}, tid = {1}, cpu id = {2}.",
213 Expected<std::vector<uint8_t>> data =
217 return data.takeError();
219 if (data->size() != expected_size)
220 return createStringError(
221 inconvertibleErrorCode(),
222 formatv(
"Got incomplete live trace data. Data kind = {0}, expected "
223 "size = {1}, actual size = {2}, tid = {3}, cpu id = {4}",
224 request.
kind, expected_size, data->size(), request.
tid,
230Expected<std::vector<uint8_t>>
234 return createStringError(
235 inconvertibleErrorCode(),
236 "Tracing data \"%s\" is not available for thread %" PRIu64
".",
244Expected<std::vector<uint8_t>>
247 return createStringError(
248 inconvertibleErrorCode(),
249 "Attempted to fetch live cpu data without a live process.");
252 return createStringError(
253 inconvertibleErrorCode(),
254 "Tracing data \"%s\" is not available for cpu_id %" PRIu64
".",
255 kind.data(), cpu_id);
258 std::nullopt, cpu_id};
262Expected<std::vector<uint8_t>>
266 return createStringError(
267 inconvertibleErrorCode(),
268 "Tracing data \"%s\" is not available for the process.", kind.data());
290 LLDB_LOG(log,
"Trace::RefreshLiveProcessState invoked");
295 auto do_refresh = [&]() -> Error {
298 return json_string.takeError();
300 Expected<TraceGetStateResponse> live_process_state =
301 json::parse<TraceGetStateResponse>(*json_string,
302 "TraceGetStateResponse");
303 if (!live_process_state)
304 return live_process_state.takeError();
306 if (live_process_state->warnings) {
307 for (std::string &
warning : *live_process_state->warnings)
308 LLDB_LOG(log,
"== Warning when fetching the trace state: {0}",
warning);
312 live_process_state->traced_threads) {
318 LLDB_LOG(log,
"== Found {0} threads being traced",
319 live_process_state->traced_threads.size());
321 if (live_process_state->cpus) {
323 for (
const TraceCpuState &cpu_state : *live_process_state->cpus) {
326 m_storage.live_cpu_data_sizes[cpu_state.
id].insert(
329 LLDB_LOG(log,
"== Found {0} cpu cpus being traced",
330 live_process_state->cpus->size());
333 for (
const TraceBinaryData &item : live_process_state->process_binary_data)
340 if (Error err = do_refresh()) {
342 return m_storage.live_refresh_error->c_str();
349 std::optional<std::vector<lldb::cpu_id_t>> postmortem_cpus) {
350 for (
ProcessSP process_sp : postmortem_processes)
372llvm::Expected<FileSpec>
375 if (std::optional<FileSpec> file =
379 return createStringError(
380 inconvertibleErrorCode(),
381 formatv(
"The thread with tid={0} doesn't have the tracing data {1}",
386 llvm::StringRef kind) {
388 if (std::optional<FileSpec> file =
392 return createStringError(
393 inconvertibleErrorCode(),
394 formatv(
"The cpu with id={0} doesn't have the tracing data {1}", cpu_id,
405 llvm::StringRef kind,
FileSpec file_spec) {
415 return data.takeError();
416 return callback(*data);
420 llvm::StringRef kind,
423 if (std::vector<uint8_t> *cpu_data =
425 return callback(*cpu_data);
429 return data.takeError();
430 auto it = storage.
live_cpu_data[cpu_id].insert({kind, std::move(*data)});
431 return callback(it.first->second);
436 ErrorOr<std::unique_ptr<MemoryBuffer>> trace_or_error =
437 MemoryBuffer::getFile(file.
GetPath());
438 if (std::error_code err = trace_or_error.getError())
439 return createStringError(
440 inconvertibleErrorCode(),
"Failed fetching trace-related file %s. %s",
443 MemoryBuffer &data = **trace_or_error;
444 ArrayRef<uint8_t> array_ref(
445 reinterpret_cast<const uint8_t *
>(data.getBufferStart()),
446 data.getBufferSize());
447 return callback(array_ref);
456 return file.takeError();
461 llvm::StringRef kind,
466 return file.takeError();
480 DenseMap<cpu_id_t, ArrayRef<uint8_t>> buffers;
483 return Error::success();
485 std::function<
Error(std::vector<cpu_id_t>::iterator)> process_cpu =
486 [&](std::vector<cpu_id_t>::iterator cpu_id) -> Error {
487 if (cpu_id == storage.
cpus->end())
488 return callback(buffers);
491 [&](ArrayRef<uint8_t> data) -> Error {
492 buffers.try_emplace(*cpu_id, data);
493 auto next_id = cpu_id;
495 return process_cpu(next_id);
498 return process_cpu(storage.
cpus->begin());
502 llvm::StringRef kind,
513 return *storage.
cpus;
518 std::vector<Process *> processes;
522 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(StringMap< V > &map, StringRef k)
static std::optional< V > Lookup(StringMap< V > &map, StringRef k)
Helper functions for fetching data in maps and returning Optionals or pointers instead of iterators f...
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::StringMap< uint64_t > live_process_data
data kind -> size
llvm::DenseMap< lldb::tid_t, llvm::StringMap< FileSpec > > postmortem_thread_data
Postmortem traces can specific additional data files, which are represented in this variable using a ...
llvm::DenseMap< lldb::tid_t, llvm::StringMap< uint64_t > > live_thread_data
These data kinds are returned by lldb-server when fetching the state of the tracing session.
llvm::DenseMap< lldb::cpu_id_t, llvm::StringMap< std::vector< uint8_t > > > live_cpu_data
cpu id -> data kind -> bytes
std::optional< std::vector< lldb::cpu_id_t > > cpus
The list of cpus being traced.
llvm::DenseMap< lldb::cpu_id_t, llvm::StringMap< FileSpec > > postmortem_cpu_data
cpu id -> data kind -> file
llvm::DenseMap< lldb::cpu_id_t, llvm::StringMap< uint64_t > > live_cpu_data_sizes
cpu id -> data kind -> size
std::vector< Process * > postmortem_processes
Portmortem processes traced by this object if doing non-live tracing.