1 //===-- CommandObjectTrace.cpp --------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "CommandObjectTrace.h"
11 #include "llvm/Support/JSON.h"
12 #include "llvm/Support/MemoryBuffer.h"
14 #include "lldb/Core/Debugger.h"
15 #include "lldb/Core/PluginManager.h"
16 #include "lldb/Host/OptionParser.h"
17 #include "lldb/Interpreter/CommandInterpreter.h"
18 #include "lldb/Interpreter/CommandObject.h"
19 #include "lldb/Interpreter/CommandOptionArgumentTable.h"
20 #include "lldb/Interpreter/CommandReturnObject.h"
21 #include "lldb/Interpreter/OptionArgParser.h"
22 #include "lldb/Interpreter/OptionGroupFormat.h"
23 #include "lldb/Interpreter/OptionValueBoolean.h"
24 #include "lldb/Interpreter/OptionValueLanguage.h"
25 #include "lldb/Interpreter/OptionValueString.h"
26 #include "lldb/Interpreter/Options.h"
27 #include "lldb/Target/Process.h"
28 #include "lldb/Target/Trace.h"
31 using namespace lldb_private
;
34 // CommandObjectTraceSave
35 #define LLDB_OPTIONS_trace_save
36 #include "CommandOptions.inc"
38 #pragma mark CommandObjectTraceSave
40 class CommandObjectTraceSave
: public CommandObjectParsed
{
42 class CommandOptions
: public Options
{
44 CommandOptions() { OptionParsingStarting(nullptr); }
46 Status
SetOptionValue(uint32_t option_idx
, llvm::StringRef option_arg
,
47 ExecutionContext
*execution_context
) override
{
49 const int short_option
= m_getopt_table
[option_idx
].val
;
51 switch (short_option
) {
57 llvm_unreachable("Unimplemented option");
62 void OptionParsingStarting(ExecutionContext
*execution_context
) override
{
66 llvm::ArrayRef
<OptionDefinition
> GetDefinitions() override
{
67 return llvm::ArrayRef(g_trace_save_options
);
73 Options
*GetOptions() override
{ return &m_options
; }
75 CommandObjectTraceSave(CommandInterpreter
&interpreter
)
76 : CommandObjectParsed(
77 interpreter
, "trace save",
78 "Save the trace of the current target in the specified directory, "
79 "which will be created if needed. "
80 "This directory will contain a trace bundle, with all the "
81 "necessary files the reconstruct the trace session even on a "
82 "different computer. "
83 "Part of this bundle is the bundle description file with the name "
84 "trace.json. This file can be used by the \"trace load\" command "
85 "to load this trace in LLDB."
86 "Note: if the current target contains information of multiple "
87 "processes or targets, they all will be included in the bundle.",
88 "trace save [<cmd-options>] <bundle_directory>",
89 eCommandRequiresProcess
| eCommandTryTargetAPILock
|
90 eCommandProcessMustBeLaunched
| eCommandProcessMustBePaused
|
91 eCommandProcessMustBeTraced
) {
92 CommandArgumentData bundle_dir
{eArgTypeDirectoryName
, eArgRepeatPlain
};
93 m_arguments
.push_back({bundle_dir
});
97 HandleArgumentCompletion(CompletionRequest
&request
,
98 OptionElementVector
&opt_element_vector
) override
{
99 lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
100 GetCommandInterpreter(), lldb::eDiskFileCompletion
, request
, nullptr);
103 ~CommandObjectTraceSave() override
= default;
106 void DoExecute(Args
&command
, CommandReturnObject
&result
) override
{
107 if (command
.size() != 1) {
108 result
.AppendError("a single path to a directory where the trace bundle "
109 "will be created is required");
113 FileSpec
bundle_dir(command
[0].ref());
114 FileSystem::Instance().Resolve(bundle_dir
);
116 ProcessSP process_sp
= m_exe_ctx
.GetProcessSP();
118 TraceSP trace_sp
= process_sp
->GetTarget().GetTrace();
120 if (llvm::Expected
<FileSpec
> desc_file
=
121 trace_sp
->SaveToDisk(bundle_dir
, m_options
.m_compact
)) {
122 result
.AppendMessageWithFormatv(
123 "Trace bundle description file written to: {0}", *desc_file
);
124 result
.SetStatus(eReturnStatusSuccessFinishResult
);
126 result
.AppendError(toString(desc_file
.takeError()));
130 CommandOptions m_options
;
133 // CommandObjectTraceLoad
134 #define LLDB_OPTIONS_trace_load
135 #include "CommandOptions.inc"
137 #pragma mark CommandObjectTraceLoad
139 class CommandObjectTraceLoad
: public CommandObjectParsed
{
141 class CommandOptions
: public Options
{
143 CommandOptions() { OptionParsingStarting(nullptr); }
145 ~CommandOptions() override
= default;
147 Status
SetOptionValue(uint32_t option_idx
, StringRef option_arg
,
148 ExecutionContext
*execution_context
) override
{
150 const int short_option
= m_getopt_table
[option_idx
].val
;
152 switch (short_option
) {
158 llvm_unreachable("Unimplemented option");
163 void OptionParsingStarting(ExecutionContext
*execution_context
) override
{
167 ArrayRef
<OptionDefinition
> GetDefinitions() override
{
168 return ArrayRef(g_trace_load_options
);
171 bool m_verbose
; // Enable verbose logging for debugging purposes.
174 CommandObjectTraceLoad(CommandInterpreter
&interpreter
)
175 : CommandObjectParsed(
176 interpreter
, "trace load",
177 "Load a post-mortem processor trace session from a trace bundle.",
178 "trace load <trace_description_file>") {
179 CommandArgumentData session_file_arg
{eArgTypeFilename
, eArgRepeatPlain
};
180 m_arguments
.push_back({session_file_arg
});
184 HandleArgumentCompletion(CompletionRequest
&request
,
185 OptionElementVector
&opt_element_vector
) override
{
186 lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
187 GetCommandInterpreter(), lldb::eDiskFileCompletion
, request
, nullptr);
190 ~CommandObjectTraceLoad() override
= default;
192 Options
*GetOptions() override
{ return &m_options
; }
195 void DoExecute(Args
&command
, CommandReturnObject
&result
) override
{
196 if (command
.size() != 1) {
197 result
.AppendError("a single path to a JSON file containing a the "
198 "description of the trace bundle is required");
202 const FileSpec
trace_description_file(command
[0].ref());
204 llvm::Expected
<lldb::TraceSP
> trace_or_err
=
205 Trace::LoadPostMortemTraceFromFile(GetDebugger(),
206 trace_description_file
);
209 result
.AppendErrorWithFormat(
210 "%s\n", llvm::toString(trace_or_err
.takeError()).c_str());
214 if (m_options
.m_verbose
) {
215 result
.AppendMessageWithFormatv("loading trace with plugin {0}\n",
216 trace_or_err
.get()->GetPluginName());
219 result
.SetStatus(eReturnStatusSuccessFinishResult
);
222 CommandOptions m_options
;
225 // CommandObjectTraceDump
226 #define LLDB_OPTIONS_trace_dump
227 #include "CommandOptions.inc"
229 #pragma mark CommandObjectTraceDump
231 class CommandObjectTraceDump
: public CommandObjectParsed
{
233 class CommandOptions
: public Options
{
235 CommandOptions() { OptionParsingStarting(nullptr); }
237 ~CommandOptions() override
= default;
239 Status
SetOptionValue(uint32_t option_idx
, llvm::StringRef option_arg
,
240 ExecutionContext
*execution_context
) override
{
242 const int short_option
= m_getopt_table
[option_idx
].val
;
244 switch (short_option
) {
250 llvm_unreachable("Unimplemented option");
255 void OptionParsingStarting(ExecutionContext
*execution_context
) override
{
259 llvm::ArrayRef
<OptionDefinition
> GetDefinitions() override
{
260 return llvm::ArrayRef(g_trace_dump_options
);
263 bool m_verbose
; // Enable verbose logging for debugging purposes.
266 CommandObjectTraceDump(CommandInterpreter
&interpreter
)
267 : CommandObjectParsed(interpreter
, "trace dump",
268 "Dump the loaded processor trace data.",
271 ~CommandObjectTraceDump() override
= default;
273 Options
*GetOptions() override
{ return &m_options
; }
276 void DoExecute(Args
&command
, CommandReturnObject
&result
) override
{
278 // TODO: fill in the dumping code here!
279 if (error
.Success()) {
280 result
.SetStatus(eReturnStatusSuccessFinishResult
);
282 result
.AppendErrorWithFormat("%s\n", error
.AsCString());
286 CommandOptions m_options
;
289 // CommandObjectTraceSchema
290 #define LLDB_OPTIONS_trace_schema
291 #include "CommandOptions.inc"
293 #pragma mark CommandObjectTraceSchema
295 class CommandObjectTraceSchema
: public CommandObjectParsed
{
297 class CommandOptions
: public Options
{
299 CommandOptions() { OptionParsingStarting(nullptr); }
301 ~CommandOptions() override
= default;
303 Status
SetOptionValue(uint32_t option_idx
, llvm::StringRef option_arg
,
304 ExecutionContext
*execution_context
) override
{
306 const int short_option
= m_getopt_table
[option_idx
].val
;
308 switch (short_option
) {
314 llvm_unreachable("Unimplemented option");
319 void OptionParsingStarting(ExecutionContext
*execution_context
) override
{
323 llvm::ArrayRef
<OptionDefinition
> GetDefinitions() override
{
324 return llvm::ArrayRef(g_trace_schema_options
);
327 bool m_verbose
; // Enable verbose logging for debugging purposes.
330 CommandObjectTraceSchema(CommandInterpreter
&interpreter
)
331 : CommandObjectParsed(interpreter
, "trace schema",
332 "Show the schema of the given trace plugin.",
333 "trace schema <plug-in>. Use the plug-in name "
334 "\"all\" to see all schemas.\n") {
335 CommandArgumentData plugin_arg
{eArgTypeNone
, eArgRepeatPlain
};
336 m_arguments
.push_back({plugin_arg
});
339 ~CommandObjectTraceSchema() override
= default;
341 Options
*GetOptions() override
{ return &m_options
; }
344 void DoExecute(Args
&command
, CommandReturnObject
&result
) override
{
346 if (command
.empty()) {
348 "trace schema cannot be invoked without a plug-in as argument");
352 StringRef
plugin_name(command
[0].c_str());
353 if (plugin_name
== "all") {
356 StringRef schema
= PluginManager::GetTraceSchema(index
++);
360 result
.AppendMessage(schema
);
363 if (Expected
<StringRef
> schemaOrErr
=
364 Trace::FindPluginSchema(plugin_name
))
365 result
.AppendMessage(*schemaOrErr
);
367 error
= schemaOrErr
.takeError();
370 if (error
.Success()) {
371 result
.SetStatus(eReturnStatusSuccessFinishResult
);
373 result
.AppendErrorWithFormat("%s\n", error
.AsCString());
377 CommandOptions m_options
;
380 // CommandObjectTrace
382 CommandObjectTrace::CommandObjectTrace(CommandInterpreter
&interpreter
)
383 : CommandObjectMultiword(interpreter
, "trace",
384 "Commands for loading and using processor "
385 "trace information.",
386 "trace [<sub-command-options>]") {
387 LoadSubCommand("load",
388 CommandObjectSP(new CommandObjectTraceLoad(interpreter
)));
389 LoadSubCommand("dump",
390 CommandObjectSP(new CommandObjectTraceDump(interpreter
)));
391 LoadSubCommand("save",
392 CommandObjectSP(new CommandObjectTraceSave(interpreter
)));
393 LoadSubCommand("schema",
394 CommandObjectSP(new CommandObjectTraceSchema(interpreter
)));
397 CommandObjectTrace::~CommandObjectTrace() = default;
399 Expected
<CommandObjectSP
> CommandObjectTraceProxy::DoGetProxyCommandObject() {
400 ProcessSP process_sp
= m_interpreter
.GetExecutionContext().GetProcessSP();
403 return createStringError(inconvertibleErrorCode(),
404 "Process not available.");
405 if (m_live_debug_session_only
&& !process_sp
->IsLiveDebugSession())
406 return createStringError(inconvertibleErrorCode(),
407 "Process must be alive.");
409 if (Expected
<TraceSP
> trace_sp
= process_sp
->GetTarget().GetTraceOrCreate())
410 return GetDelegateCommand(**trace_sp
);
412 return createStringError(inconvertibleErrorCode(),
413 "Tracing is not supported. %s",
414 toString(trace_sp
.takeError()).c_str());
417 CommandObject
*CommandObjectTraceProxy::GetProxyCommandObject() {
418 if (Expected
<CommandObjectSP
> delegate
= DoGetProxyCommandObject()) {
419 m_delegate_sp
= *delegate
;
420 m_delegate_error
.clear();
421 return m_delegate_sp
.get();
423 m_delegate_sp
.reset();
424 m_delegate_error
= toString(delegate
.takeError());