11#include "../common/ThreadPostMortemTrace.h"
36 auto do_parse = [&]() ->
Error {
46 if (module.
uuid.has_value())
54 return error.ToError();
56 bool load_addr_changed =
false;
59 return Error::success();
61 if (
Error err = do_parse())
62 return createStringError(
63 inconvertibleErrorCode(),
"Error when parsing module %s. %s",
65 return Error::success();
69 const json::Value &value) {
71 raw_string_ostream os(err);
72 root.printErrorContext(value, os);
73 return createStringError(
74 std::errc::invalid_argument,
"%s\n\nContext:\n%s\n\nSchema:\n%s",
83 std::optional<FileSpec> trace_file;
88 std::make_shared<ThreadPostMortemTrace>(process, tid, trace_file);
93Expected<TraceIntelPTBundleLoader::ParsedProcess>
95 llvm::StringRef triple) {
102 return error.ToError();
108 ProcessSP process_sp = target_sp->CreateProcess(
115 return parsed_process;
118Expected<TraceIntelPTBundleLoader::ParsedProcess>
120 Expected<ParsedProcess> parsed_process =
124 return parsed_process.takeError();
126 ProcessSP process_sp = parsed_process->target_sp->GetProcessSP();
129 parsed_process->threads.push_back(
ParseThread(*process_sp, thread));
133 return std::move(err);
136 process_sp->GetThreadList().SetSelectedThreadByIndexID(0);
141 process_sp->DidAttach(process_arch);
143 return parsed_process;
146Expected<TraceIntelPTBundleLoader::ParsedProcess>
149 Expected<ParsedProcess> parsed_process =
153 return parsed_process.takeError();
155 ProcessSP process_sp = parsed_process->target_sp->GetProcessSP();
158 for (
const JSONCpu &cpu : *bundle_description.
cpus) {
161 thread_sp->SetName(formatv(
"kernel_cpu_{0}", cpu.
id).str().c_str());
162 process_sp->GetThreadList().AddThread(thread_sp);
163 parsed_process->threads.push_back(thread_sp);
173 parsed_process->target_sp->GetOrCreateModule(module_spec,
false, &
error);
176 return error.ToError();
179 bundle_description.
kernel->load_address
180 ? bundle_description.
kernel->load_address->value
183 bool load_addr_changed =
false;
184 module_sp->SetLoadAddress(*parsed_process->target_sp, load_address,
false,
187 process_sp->GetThreadList().SetSelectedThreadByIndexID(0);
192 process_sp->DidAttach(process_arch);
194 return parsed_process;
197Expected<std::vector<TraceIntelPTBundleLoader::ParsedProcess>>
200 std::vector<ParsedProcess> parsed_processes;
202 auto HandleError = [&](
Error &&err) {
206 return std::move(err);
211 if (Expected<ParsedProcess> parsed_process =
ParseProcess(process))
212 parsed_processes.push_back(std::move(*parsed_process));
214 return HandleError(parsed_process.takeError());
218 if (bundle_description.
kernel) {
219 if (Expected<ParsedProcess> kernel_process =
221 parsed_processes.push_back(std::move(*kernel_process));
223 return HandleError(kernel_process.takeError());
226 return parsed_processes;
230 static std::string schema;
231 if (schema.empty()) {
235 // CPU information gotten from, for example, /proc/cpuinfo.
237 "vendor": "GenuineIntel" | "unknown",
246 // Optional clang/llvm target triple.
247 // This must be provided if the trace will be created not using the
248 // CLI or on a machine other than where the target was traced.
250 // A list of known threads for the given process. When context switch
251 // data is provided, LLDB will automatically create threads for the
252 // this process whenever it finds new threads when traversing the
253 // context switches, so passing values to this list in this case is
258 // Path to the raw Intel PT buffer file for this thread.
263 "systemPath": string,
264 // Original path of the module at runtime.
266 // Path to a copy of the file if not available at "systemPath".
267 "loadAddress": integer | string decimal | hex string,
268 // Lowest address of the sections of the module loaded on memory.
270 // Build UUID for the file for sanity checks.
278 // Id of this CPU core.
280 // Path to the raw Intel PT buffer for this cpu core.
281 "contextSwitchTrace": string,
282 // Path to the raw perf_event_open context switch trace file for this cpu core.
283 // The perf_event must have been configured with PERF_SAMPLE_TID and
284 // PERF_SAMPLE_TIME, as well as sample_id_all = 1.
287 "tscPerfZeroConversion"?: {
288 // Values used to convert between TSCs and nanoseconds. See the time_zero
289 // section in https://man7.org/linux/man-pages/man2/perf_event_open.2.html
293 "timeShift": integer,
294 "timeZero": integer | string decimal | hex string,
297 "loadAddress"?: integer | string decimal | hex string,
298 // Kernel's image load address. Defaults to 0xffffffff81000000, which
299 // is a load address of x86 architecture if KASLR is not enabled.
301 // Path to the kernel image.
307- All paths are either absolute or relative to folder containing the bundle
309- "cpus" is provided if and only if processes[].threads[].iptTrace is not provided.
310- "tscPerfZeroConversion" must be provided if "cpus" is provided.
311- If "kernel" is provided, then the "processes" section must be empty or not
312 passed at all, and the "cpus" section must be provided. This configuration
313 indicates that the kernel was traced and user processes weren't. Besides
314 that, the kernel is treated as a single process with one thread per CPU
315 core. This doesn't handle actual kernel threads, but instead treats
316 all the instructions executed by the kernel on each core as an
317 individual thread.})";
324 if (!bundle_description.
cpus || !bundle_description.
processes)
325 return Error::success();
328 return createStringError(inconvertibleErrorCode(),
329 "TSC to nanos conversion values are needed when "
330 "context switch information is provided.");
332 DenseMap<lldb::pid_t, JSONProcess *> indexed_processes;
333 DenseMap<JSONProcess *, DenseSet<tid_t>> indexed_threads;
336 indexed_processes[process.
pid] = &process;
338 indexed_threads[&process].insert(thread.
tid);
342 auto proc = indexed_processes.find(pid);
343 if (proc == indexed_processes.end())
345 if (indexed_threads[proc->second].count(tid))
347 indexed_threads[proc->second].insert(tid);
348 proc->second->
threads.push_back({tid, std::nullopt});
351 for (
const JSONCpu &cpu : *bundle_description.
cpus) {
354 [&](ArrayRef<uint8_t> data) ->
Error {
355 Expected<std::vector<ThreadContinuousExecution>> executions =
356 DecodePerfContextSwitchTrace(
357 data, cpu.id, *bundle_description.tsc_perf_zero_conversion);
359 return executions.takeError();
360 for (const ThreadContinuousExecution &execution : *executions)
361 on_thread_seen(execution.pid, execution.tid);
362 return Error::success();
367 return Error::success();
372 std::vector<ParsedProcess> &parsed_processes) {
373 std::vector<ThreadPostMortemTraceSP> threads;
374 std::vector<ProcessSP> processes;
375 for (
const ParsedProcess &parsed_process : parsed_processes) {
376 processes.push_back(parsed_process.target_sp->GetProcessSP());
377 threads.insert(threads.end(), parsed_process.threads.begin(),
378 parsed_process.threads.end());
386 bundle_description, processes, threads, trace_mode);
388 parsed_process.target_sp->SetTrace(trace_instance);
390 return trace_instance;
408 if (bundle_description.
cpus) {
415 if (bundle_description.
kernel) {
416 bundle_description.
kernel->file =
422 json::Path::Root root(
"traceBundle");
430 return std::move(err);
432 if (Expected<std::vector<ParsedProcess>> parsed_processes =
436 return parsed_processes.takeError();
static llvm::raw_ostream & error(Stream &strm)
An architecture specification class.
TargetList & GetTargetList()
Get accessor for the target list.
bool IsRelative() const
Returns true if the filespec represents a relative path.
void PrependPathComponent(llvm::StringRef component)
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
FileSpec & GetPlatformFileSpec()
A plug-in interface definition class for debugging a process.
ThreadList & GetThreadList()
bool DeleteTarget(lldb::TargetSP &target_sp)
Delete a Target object from the list.
Status CreateTarget(Debugger &debugger, llvm::StringRef user_exe_path, llvm::StringRef triple_str, LoadDependentFiles get_dependent_modules, const OptionGroupPlatform *platform_options, lldb::TargetSP &target_sp)
Create a new Target.
lldb::ModuleSP GetOrCreateModule(const ModuleSpec &module_spec, bool notify, Status *error_ptr=nullptr)
Find a binary on the system and return its Module, or return an existing Module that is already in th...
void AddThread(const lldb::ThreadSP &thread_sp)
static llvm::Error OnDataFileRead(FileSpec file, OnBinaryDataReadCallback callback)
Helper method for reading a data file and passing its data to the given callback.
bool SetFromStringRef(llvm::StringRef str)
llvm::Error AugmentThreadsFromContextSwitches(JSONTraceBundleDescription &bundle_description)
When applicable, augment the list of threads in the trace bundle by inspecting the context switch tra...
llvm::Expected< lldb::TraceSP > CreateTraceIntelPTInstance(JSONTraceBundleDescription &bundle_description, std::vector< ParsedProcess > &parsed_processes)
Given a bundle description and a list of fully parsed processes, create an actual Trace instance that...
void NormalizeAllPaths(JSONTraceBundleDescription &bundle_description)
Modifiy the bundle description by normalizing all the paths relative to the session file directory.
llvm::Expected< std::vector< ParsedProcess > > LoadBundle(const JSONTraceBundleDescription &bundle_description)
Create the corresponding Process, Thread and Module objects given this bundle description.
lldb::ThreadPostMortemTraceSP ParseThread(Process &process, const JSONThread &thread)
Create a post-mortem thread associated with the given process using the definition from thread.
llvm::Error CreateJSONError(llvm::json::Path::Root &root, const llvm::json::Value &value)
Create a user-friendly error message upon a JSON-parsing failure using the json::ObjectMapper functio...
llvm::Expected< lldb::TraceSP > Load()
Parse the trace bundle description and create the corresponding Target objects.
static llvm::StringRef GetSchema()
FileSpec NormalizePath(const std::string &path)
Resolve non-absolute paths relative to the bundle folder.
const std::string m_bundle_dir
const llvm::json::Value & m_bundle_description
llvm::Expected< ParsedProcess > CreateEmptyProcess(lldb::pid_t pid, llvm::StringRef triple)
Create an empty Process object with given pid and target.
llvm::Expected< ParsedProcess > ParseProcess(const JSONProcess &process)
Create the corresponding Threads and Process objects given the JSON process definition.
llvm::Expected< ParsedProcess > ParseKernel(const JSONTraceBundleDescription &bundle_description)
Create a kernel process and cpu threads given the JSON kernel definition.
llvm::Error ParseModule(Target &target, const JSONModule &module)
Create a module associated with the given target using the definition from module.
static TraceIntelPTSP CreateInstanceForPostmortemTrace(JSONTraceBundleDescription &bundle_description, llvm::ArrayRef< lldb::ProcessSP > traced_processes, llvm::ArrayRef< lldb::ThreadPostMortemTraceSP > traced_threads, TraceMode trace_mode)
Postmortem trace constructor.
const lldb::pid_t kDefaultKernelProcessID
bool fromJSON(const json::Value &value, JSONModule &module, Path path)
const lldb::addr_t kDefaultKernelLoadAddress
A class that represents a running process on the host machine.
const char * toString(AppleArm64ExceptionClass EC)
std::shared_ptr< lldb_private::Trace > TraceSP
std::shared_ptr< lldb_private::ThreadPostMortemTrace > ThreadPostMortemTraceSP
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::Module > ModuleSP
std::string context_switch_trace
std::optional< std::string > uuid
std::optional< std::string > file
std::optional< std::string > triple
std::vector< JSONModule > modules
std::vector< JSONThread > threads
std::optional< std::string > ipt_trace
std::optional< std::vector< JSONCpu > > cpus
std::optional< LinuxPerfZeroTscConversion > tsc_perf_zero_conversion
std::optional< JSONKernel > kernel
std::optional< std::vector< JSONProcess > > processes
Helper struct holding the objects created when parsing a process.