LLDB  mainline
TraceIntelPTSessionSaver.cpp
Go to the documentation of this file.
1 //===-- TraceIntelPTSessionSaver.cpp --------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "../common/TraceSessionSaver.h"
11 #include "TraceIntelPT.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/ModuleList.h"
15 #include "lldb/Target/Process.h"
17 #include "lldb/Target/Target.h"
18 #include "lldb/Target/ThreadList.h"
19 #include "lldb/lldb-types.h"
20 #include "llvm/ADT/None.h"
21 #include "llvm/Support/Error.h"
22 #include "llvm/Support/JSON.h"
23 
24 #include <fstream>
25 #include <iostream>
26 #include <sstream>
27 #include <string>
28 
29 using namespace lldb;
30 using namespace lldb_private;
31 using namespace lldb_private::trace_intel_pt;
32 using namespace llvm;
33 
34 llvm::Error TraceIntelPTSessionSaver::SaveToDisk(TraceIntelPT &trace_ipt,
35  FileSpec directory) {
36  Process *live_process = trace_ipt.GetLiveProcess();
37  if (live_process == nullptr)
38  return createStringError(inconvertibleErrorCode(),
39  "Saving a trace requires a live process.");
40 
41  if (std::error_code ec =
42  sys::fs::create_directories(directory.GetPath().c_str()))
43  return llvm::errorCodeToError(ec);
44 
45  llvm::Expected<JSONTraceIntelPTTrace> json_intel_pt_trace =
46  BuildTraceSection(trace_ipt);
47  if (!json_intel_pt_trace)
48  return json_intel_pt_trace.takeError();
49 
50  llvm::Expected<JSONTraceSessionBase> json_session_description =
51  TraceSessionSaver::BuildProcessesSection(
52  *live_process,
53  [&](lldb::tid_t tid)
54  -> llvm::Expected<llvm::Optional<std::vector<uint8_t>>> {
55  if (!trace_ipt.IsTraced(tid))
56  return None;
57  return trace_ipt.GetLiveThreadBuffer(tid);
58  },
59  directory);
60 
61  if (!json_session_description)
62  return json_session_description.takeError();
63 
64  JSONTraceIntelPTSession json_intel_pt_session{json_intel_pt_trace.get(),
65  json_session_description.get()};
66 
67  return TraceSessionSaver::WriteSessionToFile(
68  llvm::json::toJSON(json_intel_pt_session), directory);
69 }
70 
71 llvm::Expected<JSONTraceIntelPTTrace>
72 TraceIntelPTSessionSaver::BuildTraceSection(TraceIntelPT &trace_ipt) {
73  llvm::Expected<pt_cpu> cpu_info = trace_ipt.GetCPUInfo();
74  if (!cpu_info)
75  return cpu_info.takeError();
76 
77  return JSONTraceIntelPTTrace{"intel-pt",
78  JSONTraceIntelPTCPUInfo(cpu_info.get())};
79 }
llvm
Definition: Debugger.h:49
TraceIntelPTSessionSaver.h
lldb_private::trace_intel_pt::JSONTraceIntelPTCPUInfo
Definition: TraceIntelPTJSONStructs.h:18
lldb_private::Process
Definition: Process.h:341
Module.h
lldb_private::trace_intel_pt::TraceIntelPT
Definition: TraceIntelPT.h:21
SectionLoadList.h
lldb_private::trace_intel_pt::JSONTraceIntelPTSession
Definition: TraceIntelPTJSONStructs.h:39
lldb_private::trace_intel_pt::TraceIntelPT::GetLiveThreadBuffer
llvm::Expected< std::vector< uint8_t > > GetLiveThreadBuffer(lldb::tid_t tid)
Get the thread buffer content for a live thread.
Definition: TraceIntelPT.cpp:343
Process.h
Target.h
TraceIntelPTJSONStructs.h
lldb_private::FileSpec
Definition: FileSpec.h:56
ModuleList.h
ThreadList.h
lldb_private::trace_intel_pt::TraceIntelPT::IsTraced
bool IsTraced(lldb::tid_t tid) override
Check if a thread is currently traced by this object.
Definition: TraceIntelPT.cpp:210
lldb-types.h
lldb_private::trace_intel_pt::TraceIntelPT::GetLiveProcess
Process * GetLiveProcess()
Get the current traced live process.
Definition: TraceIntelPT.cpp:191
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
Error
llvm::Error Error
Definition: UdtRecordCompleter.cpp:29
lldb_private::trace_intel_pt::TraceIntelPT::GetCPUInfo
llvm::Expected< pt_cpu > GetCPUInfo()
Definition: TraceIntelPT.cpp:181
lldb_private::trace_intel_pt
Definition: CommandObjectTraceStartIntelPT.h:18
lldb_private::FileSpec::GetPath
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:348
lldb
Definition: SBAddress.h:15
lldb_private::trace_intel_pt::JSONTraceIntelPTTrace
Definition: TraceIntelPTJSONStructs.h:34
TraceIntelPT.h
llvm::json::toJSON
llvm::json::Value toJSON(const JSONModule &module)
Definition: TraceJSONStructs.cpp:21
lldb::tid_t
uint64_t tid_t
Definition: lldb-types.h:86