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);
 
 
   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) {
 
  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,
 
 
  120    Debugger &debugger, 
const json::Value &trace_bundle_description,
 
  121    StringRef bundle_dir) {
 
  123  json::Path::Root root(
"traceBundle");
 
  125    return root.getError();
 
  127  if (
auto create_callback =
 
  129    return create_callback(trace_bundle_description, bundle_dir, debugger);
 
 
  138                             "Can't trace non-live processes");
 
  140  if (
auto create_callback =
 
  142    return create_callback(process);
 
 
  158        inconvertibleErrorCode(),
 
  159        "Attempted to start tracing without a live process.");
 
 
  166        inconvertibleErrorCode(),
 
  167        "Attempted to stop tracing without a live process.");
 
 
  174        inconvertibleErrorCode(),
 
  175        "Attempted to stop tracing without a live process.");
 
 
  182        inconvertibleErrorCode(),
 
  183        "Attempted to fetch live trace information without a live process.");
 
 
  187std::optional<uint64_t>
 
  194                                                        llvm::StringRef kind) {
 
 
  199std::optional<uint64_t>
 
  205Expected<std::vector<uint8_t>>
 
  207                              uint64_t expected_size) {
 
  210        inconvertibleErrorCode(),
 
  211        formatv(
"Attempted to fetch live trace data without a live process. " 
  212                "Data kind = {0}, tid = {1}, cpu id = {2}.",
 
  215  Expected<std::vector<uint8_t>> data =
 
  219    return data.takeError();
 
  221  if (data->size() != expected_size)
 
  223        inconvertibleErrorCode(),
 
  224        formatv(
"Got incomplete live trace data. Data kind = {0}, expected " 
  225                "size = {1}, actual size = {2}, tid = {3}, cpu id = {4}",
 
  226                request.
kind, expected_size, data->size(), request.
tid,
 
 
  232Expected<std::vector<uint8_t>>
 
  237        inconvertibleErrorCode(),
 
  238        "Tracing data \"%s\" is not available for thread %" PRIu64 
".",
 
 
  246Expected<std::vector<uint8_t>>
 
  250        inconvertibleErrorCode(),
 
  251        "Attempted to fetch live cpu data without a live process.");
 
  255        inconvertibleErrorCode(),
 
  256        "Tracing data \"%s\" is not available for cpu_id %" PRIu64 
".",
 
  257        kind.data(), cpu_id);
 
  260                                    std::nullopt, cpu_id};
 
 
  264Expected<std::vector<uint8_t>>
 
  269        inconvertibleErrorCode(),
 
  270        "Tracing data \"%s\" is not available for the process.", kind.data());
 
 
  292  LLDB_LOG(log, 
"Trace::RefreshLiveProcessState invoked");
 
  297  auto do_refresh = [&]() -> Error {
 
  300      return json_string.takeError();
 
  302    Expected<TraceGetStateResponse> live_process_state =
 
  303        json::parse<TraceGetStateResponse>(*json_string,
 
  304                                           "TraceGetStateResponse");
 
  305    if (!live_process_state)
 
  306      return live_process_state.takeError();
 
  308    if (live_process_state->warnings) {
 
  309      for (std::string &
warning : *live_process_state->warnings)
 
  310        LLDB_LOG(log, 
"== Warning when fetching the trace state: {0}", 
warning);
 
  314         live_process_state->traced_threads) {
 
  320    LLDB_LOG(log, 
"== Found {0} threads being traced",
 
  321             live_process_state->traced_threads.size());
 
  323    if (live_process_state->cpus) {
 
  325      for (
const TraceCpuState &cpu_state : *live_process_state->cpus) {
 
  328          m_storage.live_cpu_data_sizes[cpu_state.
id].insert(
 
  331      LLDB_LOG(log, 
"== Found {0} cpu cpus being traced",
 
  332               live_process_state->cpus->size());
 
  335    for (
const TraceBinaryData &item : live_process_state->process_binary_data)
 
  342  if (Error err = do_refresh()) {
 
  344    return m_storage.live_refresh_error->c_str();
 
 
  351             std::optional<std::vector<lldb::cpu_id_t>> postmortem_cpus) {
 
  352  for (
ProcessSP process_sp : postmortem_processes)
 
  374llvm::Expected<FileSpec>
 
  377  if (std::optional<FileSpec> file =
 
  382        inconvertibleErrorCode(),
 
  383        formatv(
"The thread with tid={0} doesn't have the tracing data {1}",
 
 
  388                                                         llvm::StringRef kind) {
 
  390  if (std::optional<FileSpec> file =
 
  395        inconvertibleErrorCode(),
 
  396        formatv(
"The cpu with id={0} doesn't have the tracing data {1}", cpu_id,
 
 
  407                                     llvm::StringRef kind, 
FileSpec file_spec) {
 
 
  417    return data.takeError();
 
  418  return callback(*data);
 
 
  422                                           llvm::StringRef kind,
 
  425  if (std::vector<uint8_t> *cpu_data =
 
  427    return callback(*cpu_data);
 
  431    return data.takeError();
 
  434  return callback(it.first->second);
 
 
  439  ErrorOr<std::unique_ptr<MemoryBuffer>> trace_or_error =
 
  440      MemoryBuffer::getFile(file.
GetPath());
 
  441  if (std::error_code err = trace_or_error.getError())
 
  443        inconvertibleErrorCode(), 
"Failed fetching trace-related file %s. %s",
 
  446  MemoryBuffer &data = **trace_or_error;
 
  447  ArrayRef<uint8_t> array_ref(
 
  448      reinterpret_cast<const uint8_t *
>(data.getBufferStart()),
 
  449      data.getBufferSize());
 
  450  return callback(array_ref);
 
 
  459    return file.takeError();
 
 
  464                                     llvm::StringRef kind,
 
  469    return file.takeError();
 
 
  483  DenseMap<cpu_id_t, ArrayRef<uint8_t>> buffers;
 
  486    return Error::success();
 
  488  std::function<
Error(std::vector<cpu_id_t>::iterator)> process_cpu =
 
  489      [&](std::vector<cpu_id_t>::iterator cpu_id) -> Error {
 
  490    if (cpu_id == storage.
cpus->end())
 
  491      return callback(buffers);
 
  494                               [&](ArrayRef<uint8_t> data) -> Error {
 
  495                                 buffers.try_emplace(*cpu_id, data);
 
  496                                 auto next_id = cpu_id;
 
  498                                 return process_cpu(next_id);
 
  501  return process_cpu(storage.
cpus->begin());
 
 
  505                                       llvm::StringRef kind,
 
 
  516    return *storage.
cpus;
 
 
  521  std::vector<Process *> processes;
 
  525    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 llvm::Error createStringError(const char *format, Args &&...args)
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.
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
A class to manage flag bits.
const ConstString & 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.
const char * toString(AppleArm64ExceptionClass EC)
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.