LLDB mainline
Functions
TraceIntelPTBundleSaver.cpp File Reference
#include "TraceIntelPTBundleSaver.h"
#include "PerfContextSwitchDecoder.h"
#include "TraceIntelPT.h"
#include "TraceIntelPTConstants.h"
#include "TraceIntelPTJSONStructs.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleList.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/SectionLoadList.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/ThreadList.h"
#include "lldb/lldb-types.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/JSON.h"
#include <fstream>
#include <iostream>
#include <optional>
#include <sstream>
#include <string>

Go to the source code of this file.

Functions

static std::string GetRelativePath (const FileSpec &directory, const FileSpec &path)
 Strip the directory component from the given path.
 
static llvm::Error WriteBytesToDisk (FileSpec &output_file, ArrayRef< uint8_t > data)
 Write a stream of bytes from data to the given output file.
 
static Expected< FileSpecSaveTraceBundleDescription (const llvm::json::Value &trace_bundle_description, const FileSpec &directory)
 Save the trace bundle description JSON object inside the given directory as a file named trace.json.
 
static llvm::Expected< std::vector< JSONThread > > BuildThreadsSection (Process &process, FileSpec directory)
 Build the threads sub-section of the trace bundle description file.
 
static Expected< std::optional< FileSpec > > WriteContextSwitchTrace (TraceIntelPT &trace_ipt, lldb::cpu_id_t cpu_id, const FileSpec &cpus_dir, bool compact)
 
static Expected< FileSpecWriteIntelPTTrace (TraceIntelPT &trace_ipt, lldb::cpu_id_t cpu_id, const FileSpec &cpus_dir)
 
static llvm::Expected< std::optional< std::vector< JSONCpu > > > BuildCpusSection (TraceIntelPT &trace_ipt, FileSpec directory, bool compact)
 
static llvm::Expected< std::vector< JSONModule > > BuildModulesSection (Process &process, FileSpec directory)
 Build modules sub-section of the trace bundle.
 
static llvm::Expected< JSONProcessBuildProcessSection (Process &process, const FileSpec &directory)
 Build the processes section of the trace bundle description object.
 
static llvm::Expected< std::vector< JSONProcess > > BuildProcessesSection (TraceIntelPT &trace_ipt, const FileSpec &directory)
 See BuildProcessSection()
 
static llvm::Expected< JSONKernelBuildKernelSection (TraceIntelPT &trace_ipt, const FileSpec &directory)
 

Function Documentation

◆ BuildCpusSection()

static llvm::Expected< std::optional< std::vector< JSONCpu > > > BuildCpusSection ( TraceIntelPT trace_ipt,
FileSpec  directory,
bool  compact 
)
static

◆ BuildKernelSection()

static llvm::Expected< JSONKernel > BuildKernelSection ( TraceIntelPT trace_ipt,
const FileSpec directory 
)
static

◆ BuildModulesSection()

static llvm::Expected< std::vector< JSONModule > > BuildModulesSection ( Process process,
FileSpec  directory 
)
static

Build modules sub-section of the trace bundle.

The original modules will be copied over to the <directory/modules> folder. Invalid modules are skipped. Copying the modules has the benefit of making these directories self-contained, as the raw traces and modules are part of the output directory and can be sent to another machine, where lldb can load them and replicate exactly the same trace session.

Parameters
[in]processThe process being traced.
[in]directoryThe directory where the modules files will be saved when building the modules section. Example: If a module libbar.so exists in the path /usr/lib/foo/libbar.so, then it will be copied to <directory>/modules/usr/lib/foo/libbar.so.
Returns
The modules section or llvm::Error in case of failures.

Definition at line 247 of file TraceIntelPTBundleSaver.cpp.

References lldb_private::FileSpec::AppendPathComponent(), lldb_private::ConstString::AsCString(), lldb_private::ObjectFile::GetBaseAddress(), lldb_private::FileSpec::GetDirectory(), lldb_private::Target::GetImages(), lldb_private::Address::GetLoadAddress(), lldb_private::ModuleList::GetModuleAtIndex(), lldb_private::FileSpec::GetPath(), GetRelativePath(), lldb_private::Target::GetSectionLoadList(), lldb_private::ModuleList::GetSize(), lldb_private::Process::GetTarget(), lldb_private::SectionLoadList::IsEmpty(), lldb_private::Address::IsValid(), and LLDB_INVALID_ADDRESS.

Referenced by BuildKernelSection(), and BuildProcessSection().

◆ BuildProcessesSection()

static llvm::Expected< std::vector< JSONProcess > > BuildProcessesSection ( TraceIntelPT trace_ipt,
const FileSpec directory 
)
static

◆ BuildProcessSection()

static llvm::Expected< JSONProcess > BuildProcessSection ( Process process,
const FileSpec directory 
)
static

Build the processes section of the trace bundle description object.

Besides returning the processes information, this method saves to disk all modules and raw traces corresponding to the traced threads of the given process.

Parameters
[in]processThe process being traced.
[in]directoryThe directory where files will be saved when building the processes section.
Returns
The processes section or llvm::Error in case of failures.

Definition at line 305 of file TraceIntelPTBundleSaver.cpp.

References BuildModulesSection(), BuildThreadsSection(), lldb_private::Target::GetArchitecture(), lldb_private::Process::GetID(), lldb_private::Process::GetTarget(), and lldb_private::ArchSpec::GetTriple().

Referenced by BuildProcessesSection().

◆ BuildThreadsSection()

static llvm::Expected< std::vector< JSONThread > > BuildThreadsSection ( Process process,
FileSpec  directory 
)
static

Build the threads sub-section of the trace bundle description file.

Any associated binary files are created inside the given directory.

Parameters
[in]processThe process being traced.
[in]directoryThe directory where files will be saved when building the threads section.
Returns
The threads section or llvm::Error in case of failures.

Definition at line 98 of file TraceIntelPTBundleSaver.cpp.

References lldb_private::FileSpec::AppendPathComponent(), lldb_private::FileSpec::GetPath(), GetRelativePath(), lldb_private::Process::GetTarget(), lldb_private::Target::GetTrace(), lldb_private::trace_intel_pt::JSONThread::ipt_trace, lldb_private::IntelPTDataKinds::kIptTrace, lldb_private::Process::Threads(), lldb_private::trace_intel_pt::JSONThread::tid, and WriteBytesToDisk().

Referenced by BuildProcessSection().

◆ GetRelativePath()

static std::string GetRelativePath ( const FileSpec directory,
const FileSpec path 
)
static

Strip the directory component from the given path.

It assumes that directory is a prefix of path.

Definition at line 36 of file TraceIntelPTBundleSaver.cpp.

References lldb_private::FileSpec::GetPath().

Referenced by BuildCpusSection(), BuildModulesSection(), and BuildThreadsSection().

◆ SaveTraceBundleDescription()

static Expected< FileSpec > SaveTraceBundleDescription ( const llvm::json::Value &  trace_bundle_description,
const FileSpec directory 
)
static

Save the trace bundle description JSON object inside the given directory as a file named trace.json.

Parameters
[in]trace_bundle_descriptionThe trace bundle description as JSON Object.
[in]directoryThe directory where the JSON file will be saved.
Returns
A FileSpec pointing to the bundle description file, or an llvm::Error otherwise.

Definition at line 71 of file TraceIntelPTBundleSaver.cpp.

References lldb_private::FileSpec::AppendPathComponent(), and lldb_private::FileSpec::GetPath().

Referenced by lldb_private::trace_intel_pt::TraceIntelPTBundleSaver::SaveToDisk().

◆ WriteBytesToDisk()

static llvm::Error WriteBytesToDisk ( FileSpec output_file,
ArrayRef< uint8_t >  data 
)
static

Write a stream of bytes from data to the given output file.

It creates or overwrites the output file, but not append.

Definition at line 43 of file TraceIntelPTBundleSaver.cpp.

References lldb_private::FileSpec::GetPath().

Referenced by BuildThreadsSection(), WriteContextSwitchTrace(), and WriteIntelPTTrace().

◆ WriteContextSwitchTrace()

static Expected< std::optional< FileSpec > > WriteContextSwitchTrace ( TraceIntelPT trace_ipt,
lldb::cpu_id_t  cpu_id,
const FileSpec cpus_dir,
bool  compact 
)
static
Returns
an llvm::Error in case of failures, std::nullopt if the trace is not written to disk because the trace is empty and the compact flag is present, or the FileSpec of the trace file on disk.

Definition at line 138 of file TraceIntelPTBundleSaver.cpp.

References lldb_private::FileSpec::AppendPathComponent(), lldb_private::trace_intel_pt::FilterProcessesFromContextSwitchTrace(), lldb_private::Trace::GetAllProcesses(), lldb_private::IntelPTDataKinds::kPerfContextSwitchTrace, lldb_private::Trace::OnCpuBinaryDataRead(), and WriteBytesToDisk().

Referenced by BuildCpusSection().

◆ WriteIntelPTTrace()

static Expected< FileSpec > WriteIntelPTTrace ( TraceIntelPT trace_ipt,
lldb::cpu_id_t  cpu_id,
const FileSpec cpus_dir 
)
static