1e8d8bef9SDimitry Andric //===-- CommandObjectTrace.cpp --------------------------------------------===//
2e8d8bef9SDimitry Andric //
3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e8d8bef9SDimitry Andric //
7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8e8d8bef9SDimitry Andric 
9e8d8bef9SDimitry Andric #include "CommandObjectTrace.h"
10e8d8bef9SDimitry Andric 
11e8d8bef9SDimitry Andric #include "llvm/Support/JSON.h"
12e8d8bef9SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
13e8d8bef9SDimitry Andric 
14e8d8bef9SDimitry Andric #include "lldb/Core/Debugger.h"
15e8d8bef9SDimitry Andric #include "lldb/Core/PluginManager.h"
16e8d8bef9SDimitry Andric #include "lldb/Host/OptionParser.h"
17e8d8bef9SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
18e8d8bef9SDimitry Andric #include "lldb/Interpreter/CommandObject.h"
19fcaf7f86SDimitry Andric #include "lldb/Interpreter/CommandOptionArgumentTable.h"
20e8d8bef9SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h"
21e8d8bef9SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h"
22e8d8bef9SDimitry Andric #include "lldb/Interpreter/OptionGroupFormat.h"
23e8d8bef9SDimitry Andric #include "lldb/Interpreter/OptionValueBoolean.h"
24e8d8bef9SDimitry Andric #include "lldb/Interpreter/OptionValueLanguage.h"
25e8d8bef9SDimitry Andric #include "lldb/Interpreter/OptionValueString.h"
26e8d8bef9SDimitry Andric #include "lldb/Interpreter/Options.h"
27fe6060f1SDimitry Andric #include "lldb/Target/Process.h"
28e8d8bef9SDimitry Andric #include "lldb/Target/Trace.h"
29e8d8bef9SDimitry Andric 
30e8d8bef9SDimitry Andric using namespace lldb;
31e8d8bef9SDimitry Andric using namespace lldb_private;
32e8d8bef9SDimitry Andric using namespace llvm;
33e8d8bef9SDimitry Andric 
34753f127fSDimitry Andric // CommandObjectTraceSave
35753f127fSDimitry Andric #define LLDB_OPTIONS_trace_save
36753f127fSDimitry Andric #include "CommandOptions.inc"
37753f127fSDimitry Andric 
38753f127fSDimitry Andric #pragma mark CommandObjectTraceSave
39753f127fSDimitry Andric 
40753f127fSDimitry Andric class CommandObjectTraceSave : public CommandObjectParsed {
41753f127fSDimitry Andric public:
42753f127fSDimitry Andric   class CommandOptions : public Options {
43753f127fSDimitry Andric   public:
CommandOptions()44753f127fSDimitry Andric     CommandOptions() { OptionParsingStarting(nullptr); }
45753f127fSDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)46753f127fSDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
47753f127fSDimitry Andric                           ExecutionContext *execution_context) override {
48753f127fSDimitry Andric       Status error;
49753f127fSDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
50753f127fSDimitry Andric 
51753f127fSDimitry Andric       switch (short_option) {
52753f127fSDimitry Andric       case 'c': {
53753f127fSDimitry Andric         m_compact = true;
54753f127fSDimitry Andric         break;
55753f127fSDimitry Andric       }
56753f127fSDimitry Andric       default:
57753f127fSDimitry Andric         llvm_unreachable("Unimplemented option");
58753f127fSDimitry Andric       }
59753f127fSDimitry Andric       return error;
60753f127fSDimitry Andric     }
61753f127fSDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)62753f127fSDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
63753f127fSDimitry Andric       m_compact = false;
64753f127fSDimitry Andric     };
65753f127fSDimitry Andric 
GetDefinitions()66753f127fSDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
67bdd1243dSDimitry Andric       return llvm::ArrayRef(g_trace_save_options);
68753f127fSDimitry Andric     };
69753f127fSDimitry Andric 
70753f127fSDimitry Andric     bool m_compact;
71753f127fSDimitry Andric   };
72753f127fSDimitry Andric 
GetOptions()73753f127fSDimitry Andric   Options *GetOptions() override { return &m_options; }
74753f127fSDimitry Andric 
CommandObjectTraceSave(CommandInterpreter & interpreter)75753f127fSDimitry Andric   CommandObjectTraceSave(CommandInterpreter &interpreter)
76753f127fSDimitry Andric       : CommandObjectParsed(
77753f127fSDimitry Andric             interpreter, "trace save",
78753f127fSDimitry Andric             "Save the trace of the current target in the specified directory, "
79753f127fSDimitry Andric             "which will be created if needed. "
80753f127fSDimitry Andric             "This directory will contain a trace bundle, with all the "
81753f127fSDimitry Andric             "necessary files the reconstruct the trace session even on a "
82753f127fSDimitry Andric             "different computer. "
83753f127fSDimitry Andric             "Part of this bundle is the bundle description file with the name "
84753f127fSDimitry Andric             "trace.json. This file can be used by the \"trace load\" command "
85753f127fSDimitry Andric             "to load this trace in LLDB."
86753f127fSDimitry Andric             "Note: if the current target contains information of multiple "
87753f127fSDimitry Andric             "processes or targets, they all will be included in the bundle.",
88753f127fSDimitry Andric             "trace save [<cmd-options>] <bundle_directory>",
89753f127fSDimitry Andric             eCommandRequiresProcess | eCommandTryTargetAPILock |
90753f127fSDimitry Andric                 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused |
91753f127fSDimitry Andric                 eCommandProcessMustBeTraced) {
92753f127fSDimitry Andric     CommandArgumentData bundle_dir{eArgTypeDirectoryName, eArgRepeatPlain};
93753f127fSDimitry Andric     m_arguments.push_back({bundle_dir});
94753f127fSDimitry Andric   }
95753f127fSDimitry Andric 
96753f127fSDimitry Andric   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)97753f127fSDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
98753f127fSDimitry Andric                            OptionElementVector &opt_element_vector) override {
99fe013be4SDimitry Andric     lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
100fe013be4SDimitry Andric         GetCommandInterpreter(), lldb::eDiskFileCompletion, request, nullptr);
101753f127fSDimitry Andric   }
102753f127fSDimitry Andric 
103753f127fSDimitry Andric   ~CommandObjectTraceSave() override = default;
104753f127fSDimitry Andric 
105753f127fSDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)106*c9157d92SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
107753f127fSDimitry Andric     if (command.size() != 1) {
108753f127fSDimitry Andric       result.AppendError("a single path to a directory where the trace bundle "
109753f127fSDimitry Andric                          "will be created is required");
110*c9157d92SDimitry Andric       return;
111753f127fSDimitry Andric     }
112753f127fSDimitry Andric 
113753f127fSDimitry Andric     FileSpec bundle_dir(command[0].ref());
114753f127fSDimitry Andric     FileSystem::Instance().Resolve(bundle_dir);
115753f127fSDimitry Andric 
116753f127fSDimitry Andric     ProcessSP process_sp = m_exe_ctx.GetProcessSP();
117753f127fSDimitry Andric 
118753f127fSDimitry Andric     TraceSP trace_sp = process_sp->GetTarget().GetTrace();
119753f127fSDimitry Andric 
120753f127fSDimitry Andric     if (llvm::Expected<FileSpec> desc_file =
121753f127fSDimitry Andric             trace_sp->SaveToDisk(bundle_dir, m_options.m_compact)) {
122753f127fSDimitry Andric       result.AppendMessageWithFormatv(
123753f127fSDimitry Andric           "Trace bundle description file written to: {0}", *desc_file);
124753f127fSDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
125753f127fSDimitry Andric     } else {
126753f127fSDimitry Andric       result.AppendError(toString(desc_file.takeError()));
127753f127fSDimitry Andric     }
128753f127fSDimitry Andric   }
129753f127fSDimitry Andric 
130753f127fSDimitry Andric   CommandOptions m_options;
131753f127fSDimitry Andric };
132753f127fSDimitry Andric 
133e8d8bef9SDimitry Andric // CommandObjectTraceLoad
134e8d8bef9SDimitry Andric #define LLDB_OPTIONS_trace_load
135e8d8bef9SDimitry Andric #include "CommandOptions.inc"
136e8d8bef9SDimitry Andric 
137e8d8bef9SDimitry Andric #pragma mark CommandObjectTraceLoad
138e8d8bef9SDimitry Andric 
139e8d8bef9SDimitry Andric class CommandObjectTraceLoad : public CommandObjectParsed {
140e8d8bef9SDimitry Andric public:
141e8d8bef9SDimitry Andric   class CommandOptions : public Options {
142e8d8bef9SDimitry Andric   public:
CommandOptions()14304eeddc0SDimitry Andric     CommandOptions() { OptionParsingStarting(nullptr); }
144e8d8bef9SDimitry Andric 
145e8d8bef9SDimitry Andric     ~CommandOptions() override = default;
146e8d8bef9SDimitry Andric 
SetOptionValue(uint32_t option_idx,StringRef option_arg,ExecutionContext * execution_context)147e8d8bef9SDimitry Andric     Status SetOptionValue(uint32_t option_idx, StringRef option_arg,
148e8d8bef9SDimitry Andric                           ExecutionContext *execution_context) override {
149e8d8bef9SDimitry Andric       Status error;
150e8d8bef9SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
151e8d8bef9SDimitry Andric 
152e8d8bef9SDimitry Andric       switch (short_option) {
153e8d8bef9SDimitry Andric       case 'v': {
154e8d8bef9SDimitry Andric         m_verbose = true;
155e8d8bef9SDimitry Andric         break;
156e8d8bef9SDimitry Andric       }
157e8d8bef9SDimitry Andric       default:
158e8d8bef9SDimitry Andric         llvm_unreachable("Unimplemented option");
159e8d8bef9SDimitry Andric       }
160e8d8bef9SDimitry Andric       return error;
161e8d8bef9SDimitry Andric     }
162e8d8bef9SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)163e8d8bef9SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
164e8d8bef9SDimitry Andric       m_verbose = false;
165e8d8bef9SDimitry Andric     }
166e8d8bef9SDimitry Andric 
GetDefinitions()167e8d8bef9SDimitry Andric     ArrayRef<OptionDefinition> GetDefinitions() override {
168bdd1243dSDimitry Andric       return ArrayRef(g_trace_load_options);
169e8d8bef9SDimitry Andric     }
170e8d8bef9SDimitry Andric 
171e8d8bef9SDimitry Andric     bool m_verbose; // Enable verbose logging for debugging purposes.
172e8d8bef9SDimitry Andric   };
173e8d8bef9SDimitry Andric 
CommandObjectTraceLoad(CommandInterpreter & interpreter)174e8d8bef9SDimitry Andric   CommandObjectTraceLoad(CommandInterpreter &interpreter)
17581ad6265SDimitry Andric       : CommandObjectParsed(
17681ad6265SDimitry Andric             interpreter, "trace load",
17781ad6265SDimitry Andric             "Load a post-mortem processor trace session from a trace bundle.",
178753f127fSDimitry Andric             "trace load <trace_description_file>") {
179753f127fSDimitry Andric     CommandArgumentData session_file_arg{eArgTypeFilename, eArgRepeatPlain};
18081ad6265SDimitry Andric     m_arguments.push_back({session_file_arg});
18181ad6265SDimitry Andric   }
182e8d8bef9SDimitry Andric 
183753f127fSDimitry Andric   void
HandleArgumentCompletion(CompletionRequest & request,OptionElementVector & opt_element_vector)184753f127fSDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
185753f127fSDimitry Andric                            OptionElementVector &opt_element_vector) override {
186fe013be4SDimitry Andric     lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
187fe013be4SDimitry Andric         GetCommandInterpreter(), lldb::eDiskFileCompletion, request, nullptr);
188753f127fSDimitry Andric   }
189753f127fSDimitry Andric 
190e8d8bef9SDimitry Andric   ~CommandObjectTraceLoad() override = default;
191e8d8bef9SDimitry Andric 
GetOptions()192e8d8bef9SDimitry Andric   Options *GetOptions() override { return &m_options; }
193e8d8bef9SDimitry Andric 
194e8d8bef9SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)195*c9157d92SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
196e8d8bef9SDimitry Andric     if (command.size() != 1) {
19781ad6265SDimitry Andric       result.AppendError("a single path to a JSON file containing a the "
19881ad6265SDimitry Andric                          "description of the trace bundle is required");
199*c9157d92SDimitry Andric       return;
200e8d8bef9SDimitry Andric     }
201e8d8bef9SDimitry Andric 
20281ad6265SDimitry Andric     const FileSpec trace_description_file(command[0].ref());
20381ad6265SDimitry Andric 
20481ad6265SDimitry Andric     llvm::Expected<lldb::TraceSP> trace_or_err =
20581ad6265SDimitry Andric         Trace::LoadPostMortemTraceFromFile(GetDebugger(),
20681ad6265SDimitry Andric                                            trace_description_file);
20781ad6265SDimitry Andric 
20881ad6265SDimitry Andric     if (!trace_or_err) {
20981ad6265SDimitry Andric       result.AppendErrorWithFormat(
21081ad6265SDimitry Andric           "%s\n", llvm::toString(trace_or_err.takeError()).c_str());
211*c9157d92SDimitry Andric       return;
212e8d8bef9SDimitry Andric     }
213e8d8bef9SDimitry Andric 
21481ad6265SDimitry Andric     if (m_options.m_verbose) {
215349cc55cSDimitry Andric       result.AppendMessageWithFormatv("loading trace with plugin {0}\n",
21681ad6265SDimitry Andric                                       trace_or_err.get()->GetPluginName());
21781ad6265SDimitry Andric     }
218e8d8bef9SDimitry Andric 
219e8d8bef9SDimitry Andric     result.SetStatus(eReturnStatusSuccessFinishResult);
220e8d8bef9SDimitry Andric   }
221e8d8bef9SDimitry Andric 
222e8d8bef9SDimitry Andric   CommandOptions m_options;
223e8d8bef9SDimitry Andric };
224e8d8bef9SDimitry Andric 
225e8d8bef9SDimitry Andric // CommandObjectTraceDump
226e8d8bef9SDimitry Andric #define LLDB_OPTIONS_trace_dump
227e8d8bef9SDimitry Andric #include "CommandOptions.inc"
228e8d8bef9SDimitry Andric 
229e8d8bef9SDimitry Andric #pragma mark CommandObjectTraceDump
230e8d8bef9SDimitry Andric 
231e8d8bef9SDimitry Andric class CommandObjectTraceDump : public CommandObjectParsed {
232e8d8bef9SDimitry Andric public:
233e8d8bef9SDimitry Andric   class CommandOptions : public Options {
234e8d8bef9SDimitry Andric   public:
CommandOptions()23504eeddc0SDimitry Andric     CommandOptions() { OptionParsingStarting(nullptr); }
236e8d8bef9SDimitry Andric 
237e8d8bef9SDimitry Andric     ~CommandOptions() override = default;
238e8d8bef9SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)239e8d8bef9SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
240e8d8bef9SDimitry Andric                           ExecutionContext *execution_context) override {
241e8d8bef9SDimitry Andric       Status error;
242e8d8bef9SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
243e8d8bef9SDimitry Andric 
244e8d8bef9SDimitry Andric       switch (short_option) {
245e8d8bef9SDimitry Andric       case 'v': {
246e8d8bef9SDimitry Andric         m_verbose = true;
247e8d8bef9SDimitry Andric         break;
248e8d8bef9SDimitry Andric       }
249e8d8bef9SDimitry Andric       default:
250e8d8bef9SDimitry Andric         llvm_unreachable("Unimplemented option");
251e8d8bef9SDimitry Andric       }
252e8d8bef9SDimitry Andric       return error;
253e8d8bef9SDimitry Andric     }
254e8d8bef9SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)255e8d8bef9SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
256e8d8bef9SDimitry Andric       m_verbose = false;
257e8d8bef9SDimitry Andric     }
258e8d8bef9SDimitry Andric 
GetDefinitions()259e8d8bef9SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
260bdd1243dSDimitry Andric       return llvm::ArrayRef(g_trace_dump_options);
261e8d8bef9SDimitry Andric     }
262e8d8bef9SDimitry Andric 
263e8d8bef9SDimitry Andric     bool m_verbose; // Enable verbose logging for debugging purposes.
264e8d8bef9SDimitry Andric   };
265e8d8bef9SDimitry Andric 
CommandObjectTraceDump(CommandInterpreter & interpreter)266e8d8bef9SDimitry Andric   CommandObjectTraceDump(CommandInterpreter &interpreter)
267e8d8bef9SDimitry Andric       : CommandObjectParsed(interpreter, "trace dump",
268e8d8bef9SDimitry Andric                             "Dump the loaded processor trace data.",
26904eeddc0SDimitry Andric                             "trace dump") {}
270e8d8bef9SDimitry Andric 
271e8d8bef9SDimitry Andric   ~CommandObjectTraceDump() override = default;
272e8d8bef9SDimitry Andric 
GetOptions()273e8d8bef9SDimitry Andric   Options *GetOptions() override { return &m_options; }
274e8d8bef9SDimitry Andric 
275e8d8bef9SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)276*c9157d92SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
277e8d8bef9SDimitry Andric     Status error;
278e8d8bef9SDimitry Andric     // TODO: fill in the dumping code here!
279e8d8bef9SDimitry Andric     if (error.Success()) {
280e8d8bef9SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
281e8d8bef9SDimitry Andric     } else {
282e8d8bef9SDimitry Andric       result.AppendErrorWithFormat("%s\n", error.AsCString());
283e8d8bef9SDimitry Andric     }
284e8d8bef9SDimitry Andric   }
285e8d8bef9SDimitry Andric 
286e8d8bef9SDimitry Andric   CommandOptions m_options;
287e8d8bef9SDimitry Andric };
288e8d8bef9SDimitry Andric 
289e8d8bef9SDimitry Andric // CommandObjectTraceSchema
290e8d8bef9SDimitry Andric #define LLDB_OPTIONS_trace_schema
291e8d8bef9SDimitry Andric #include "CommandOptions.inc"
292e8d8bef9SDimitry Andric 
293e8d8bef9SDimitry Andric #pragma mark CommandObjectTraceSchema
294e8d8bef9SDimitry Andric 
295e8d8bef9SDimitry Andric class CommandObjectTraceSchema : public CommandObjectParsed {
296e8d8bef9SDimitry Andric public:
297e8d8bef9SDimitry Andric   class CommandOptions : public Options {
298e8d8bef9SDimitry Andric   public:
CommandOptions()29904eeddc0SDimitry Andric     CommandOptions() { OptionParsingStarting(nullptr); }
300e8d8bef9SDimitry Andric 
301e8d8bef9SDimitry Andric     ~CommandOptions() override = default;
302e8d8bef9SDimitry Andric 
SetOptionValue(uint32_t option_idx,llvm::StringRef option_arg,ExecutionContext * execution_context)303e8d8bef9SDimitry Andric     Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
304e8d8bef9SDimitry Andric                           ExecutionContext *execution_context) override {
305e8d8bef9SDimitry Andric       Status error;
306e8d8bef9SDimitry Andric       const int short_option = m_getopt_table[option_idx].val;
307e8d8bef9SDimitry Andric 
308e8d8bef9SDimitry Andric       switch (short_option) {
309e8d8bef9SDimitry Andric       case 'v': {
310e8d8bef9SDimitry Andric         m_verbose = true;
311e8d8bef9SDimitry Andric         break;
312e8d8bef9SDimitry Andric       }
313e8d8bef9SDimitry Andric       default:
314e8d8bef9SDimitry Andric         llvm_unreachable("Unimplemented option");
315e8d8bef9SDimitry Andric       }
316e8d8bef9SDimitry Andric       return error;
317e8d8bef9SDimitry Andric     }
318e8d8bef9SDimitry Andric 
OptionParsingStarting(ExecutionContext * execution_context)319e8d8bef9SDimitry Andric     void OptionParsingStarting(ExecutionContext *execution_context) override {
320e8d8bef9SDimitry Andric       m_verbose = false;
321e8d8bef9SDimitry Andric     }
322e8d8bef9SDimitry Andric 
GetDefinitions()323e8d8bef9SDimitry Andric     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
324bdd1243dSDimitry Andric       return llvm::ArrayRef(g_trace_schema_options);
325e8d8bef9SDimitry Andric     }
326e8d8bef9SDimitry Andric 
327e8d8bef9SDimitry Andric     bool m_verbose; // Enable verbose logging for debugging purposes.
328e8d8bef9SDimitry Andric   };
329e8d8bef9SDimitry Andric 
CommandObjectTraceSchema(CommandInterpreter & interpreter)330e8d8bef9SDimitry Andric   CommandObjectTraceSchema(CommandInterpreter &interpreter)
331e8d8bef9SDimitry Andric       : CommandObjectParsed(interpreter, "trace schema",
332e8d8bef9SDimitry Andric                             "Show the schema of the given trace plugin.",
333e8d8bef9SDimitry Andric                             "trace schema <plug-in>. Use the plug-in name "
33481ad6265SDimitry Andric                             "\"all\" to see all schemas.\n") {
33581ad6265SDimitry Andric     CommandArgumentData plugin_arg{eArgTypeNone, eArgRepeatPlain};
33681ad6265SDimitry Andric     m_arguments.push_back({plugin_arg});
33781ad6265SDimitry Andric   }
338e8d8bef9SDimitry Andric 
339e8d8bef9SDimitry Andric   ~CommandObjectTraceSchema() override = default;
340e8d8bef9SDimitry Andric 
GetOptions()341e8d8bef9SDimitry Andric   Options *GetOptions() override { return &m_options; }
342e8d8bef9SDimitry Andric 
343e8d8bef9SDimitry Andric protected:
DoExecute(Args & command,CommandReturnObject & result)344*c9157d92SDimitry Andric   void DoExecute(Args &command, CommandReturnObject &result) override {
345e8d8bef9SDimitry Andric     Status error;
346e8d8bef9SDimitry Andric     if (command.empty()) {
347fe6060f1SDimitry Andric       result.AppendError(
348e8d8bef9SDimitry Andric           "trace schema cannot be invoked without a plug-in as argument");
349*c9157d92SDimitry Andric       return;
350e8d8bef9SDimitry Andric     }
351e8d8bef9SDimitry Andric 
352e8d8bef9SDimitry Andric     StringRef plugin_name(command[0].c_str());
353e8d8bef9SDimitry Andric     if (plugin_name == "all") {
354e8d8bef9SDimitry Andric       size_t index = 0;
355e8d8bef9SDimitry Andric       while (true) {
356e8d8bef9SDimitry Andric         StringRef schema = PluginManager::GetTraceSchema(index++);
357e8d8bef9SDimitry Andric         if (schema.empty())
358e8d8bef9SDimitry Andric           break;
359e8d8bef9SDimitry Andric 
360e8d8bef9SDimitry Andric         result.AppendMessage(schema);
361e8d8bef9SDimitry Andric       }
362e8d8bef9SDimitry Andric     } else {
363e8d8bef9SDimitry Andric       if (Expected<StringRef> schemaOrErr =
364e8d8bef9SDimitry Andric               Trace::FindPluginSchema(plugin_name))
365e8d8bef9SDimitry Andric         result.AppendMessage(*schemaOrErr);
366e8d8bef9SDimitry Andric       else
367e8d8bef9SDimitry Andric         error = schemaOrErr.takeError();
368e8d8bef9SDimitry Andric     }
369e8d8bef9SDimitry Andric 
370e8d8bef9SDimitry Andric     if (error.Success()) {
371e8d8bef9SDimitry Andric       result.SetStatus(eReturnStatusSuccessFinishResult);
372e8d8bef9SDimitry Andric     } else {
373e8d8bef9SDimitry Andric       result.AppendErrorWithFormat("%s\n", error.AsCString());
374e8d8bef9SDimitry Andric     }
375e8d8bef9SDimitry Andric   }
376e8d8bef9SDimitry Andric 
377e8d8bef9SDimitry Andric   CommandOptions m_options;
378e8d8bef9SDimitry Andric };
379e8d8bef9SDimitry Andric 
380e8d8bef9SDimitry Andric // CommandObjectTrace
381e8d8bef9SDimitry Andric 
CommandObjectTrace(CommandInterpreter & interpreter)382e8d8bef9SDimitry Andric CommandObjectTrace::CommandObjectTrace(CommandInterpreter &interpreter)
383e8d8bef9SDimitry Andric     : CommandObjectMultiword(interpreter, "trace",
384e8d8bef9SDimitry Andric                              "Commands for loading and using processor "
385e8d8bef9SDimitry Andric                              "trace information.",
386e8d8bef9SDimitry Andric                              "trace [<sub-command-options>]") {
387e8d8bef9SDimitry Andric   LoadSubCommand("load",
388e8d8bef9SDimitry Andric                  CommandObjectSP(new CommandObjectTraceLoad(interpreter)));
389e8d8bef9SDimitry Andric   LoadSubCommand("dump",
390e8d8bef9SDimitry Andric                  CommandObjectSP(new CommandObjectTraceDump(interpreter)));
391753f127fSDimitry Andric   LoadSubCommand("save",
392753f127fSDimitry Andric                  CommandObjectSP(new CommandObjectTraceSave(interpreter)));
393e8d8bef9SDimitry Andric   LoadSubCommand("schema",
394e8d8bef9SDimitry Andric                  CommandObjectSP(new CommandObjectTraceSchema(interpreter)));
395e8d8bef9SDimitry Andric }
396e8d8bef9SDimitry Andric 
397e8d8bef9SDimitry Andric CommandObjectTrace::~CommandObjectTrace() = default;
398fe6060f1SDimitry Andric 
DoGetProxyCommandObject()399fe6060f1SDimitry Andric Expected<CommandObjectSP> CommandObjectTraceProxy::DoGetProxyCommandObject() {
400fe6060f1SDimitry Andric   ProcessSP process_sp = m_interpreter.GetExecutionContext().GetProcessSP();
401fe6060f1SDimitry Andric 
402fe6060f1SDimitry Andric   if (!process_sp)
403fe6060f1SDimitry Andric     return createStringError(inconvertibleErrorCode(),
404fe6060f1SDimitry Andric                              "Process not available.");
405fe6060f1SDimitry Andric   if (m_live_debug_session_only && !process_sp->IsLiveDebugSession())
406fe6060f1SDimitry Andric     return createStringError(inconvertibleErrorCode(),
407fe6060f1SDimitry Andric                              "Process must be alive.");
408fe6060f1SDimitry Andric 
409fe6060f1SDimitry Andric   if (Expected<TraceSP> trace_sp = process_sp->GetTarget().GetTraceOrCreate())
410fe6060f1SDimitry Andric     return GetDelegateCommand(**trace_sp);
411fe6060f1SDimitry Andric   else
412fe6060f1SDimitry Andric     return createStringError(inconvertibleErrorCode(),
413fe6060f1SDimitry Andric                              "Tracing is not supported. %s",
414fe6060f1SDimitry Andric                              toString(trace_sp.takeError()).c_str());
415fe6060f1SDimitry Andric }
416fe6060f1SDimitry Andric 
GetProxyCommandObject()417fe6060f1SDimitry Andric CommandObject *CommandObjectTraceProxy::GetProxyCommandObject() {
418fe6060f1SDimitry Andric   if (Expected<CommandObjectSP> delegate = DoGetProxyCommandObject()) {
419fe6060f1SDimitry Andric     m_delegate_sp = *delegate;
420fe6060f1SDimitry Andric     m_delegate_error.clear();
421fe6060f1SDimitry Andric     return m_delegate_sp.get();
422fe6060f1SDimitry Andric   } else {
423fe6060f1SDimitry Andric     m_delegate_sp.reset();
424fe6060f1SDimitry Andric     m_delegate_error = toString(delegate.takeError());
425fe6060f1SDimitry Andric     return nullptr;
426fe6060f1SDimitry Andric   }
427fe6060f1SDimitry Andric }
428