1 //===-- SBCommandInterpreter.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 
9 #include "lldb/lldb-types.h"
10 
11 #include "lldb/Utility/ReproducerInstrumentation.h"
12 #include "lldb/Interpreter/CommandInterpreter.h"
13 #include "lldb/Interpreter/CommandObjectMultiword.h"
14 #include "lldb/Interpreter/CommandReturnObject.h"
15 #include "lldb/Target/Target.h"
16 #include "lldb/Utility/Listener.h"
17 
18 #include "lldb/API/SBBroadcaster.h"
19 #include "lldb/API/SBCommandInterpreter.h"
20 #include "lldb/API/SBCommandInterpreterRunOptions.h"
21 #include "lldb/API/SBCommandReturnObject.h"
22 #include "lldb/API/SBEvent.h"
23 #include "lldb/API/SBExecutionContext.h"
24 #include "lldb/API/SBListener.h"
25 #include "lldb/API/SBProcess.h"
26 #include "lldb/API/SBStream.h"
27 #include "lldb/API/SBStringList.h"
28 #include "lldb/API/SBTarget.h"
29 
30 #include <memory>
31 
32 using namespace lldb;
33 using namespace lldb_private;
34 
35 class CommandPluginInterfaceImplementation : public CommandObjectParsed {
36 public:
37   CommandPluginInterfaceImplementation(CommandInterpreter &interpreter,
38                                        const char *name,
39                                        lldb::SBCommandPluginInterface *backend,
40                                        const char *help = nullptr,
41                                        const char *syntax = nullptr,
42                                        uint32_t flags = 0,
43                                        const char *auto_repeat_command = "")
44       : CommandObjectParsed(interpreter, name, help, syntax, flags),
45         m_backend(backend) {
46     m_auto_repeat_command =
47         auto_repeat_command == nullptr
48             ? llvm::None
49             : llvm::Optional<std::string>(auto_repeat_command);
50   }
51 
52   bool IsRemovable() const override { return true; }
53 
54   /// More documentation is available in lldb::CommandObject::GetRepeatCommand,
55   /// but in short, if nullptr is returned, the previous command will be
56   /// repeated, and if an empty string is returned, no commands will be
57   /// executed.
58   const char *GetRepeatCommand(Args &current_command_args,
59                                uint32_t index) override {
60     if (!m_auto_repeat_command)
61       return nullptr;
62     else
63       return m_auto_repeat_command->c_str();
64   }
65 
66 protected:
67   bool DoExecute(Args &command, CommandReturnObject &result) override {
68     SBCommandReturnObject sb_return(result);
69     SBCommandInterpreter sb_interpreter(&m_interpreter);
70     SBDebugger debugger_sb(m_interpreter.GetDebugger().shared_from_this());
71     bool ret = m_backend->DoExecute(
72         debugger_sb, command.GetArgumentVector(), sb_return);
73     return ret;
74   }
75   std::shared_ptr<lldb::SBCommandPluginInterface> m_backend;
76   llvm::Optional<std::string> m_auto_repeat_command;
77 };
78 
79 SBCommandInterpreter::SBCommandInterpreter(CommandInterpreter *interpreter)
80     : m_opaque_ptr(interpreter) {
81   LLDB_RECORD_CONSTRUCTOR(SBCommandInterpreter,
82                           (lldb_private::CommandInterpreter *), interpreter);
83 
84 }
85 
86 SBCommandInterpreter::SBCommandInterpreter(const SBCommandInterpreter &rhs)
87     : m_opaque_ptr(rhs.m_opaque_ptr) {
88   LLDB_RECORD_CONSTRUCTOR(SBCommandInterpreter,
89                           (const lldb::SBCommandInterpreter &), rhs);
90 }
91 
92 SBCommandInterpreter::~SBCommandInterpreter() = default;
93 
94 const SBCommandInterpreter &SBCommandInterpreter::
95 operator=(const SBCommandInterpreter &rhs) {
96   LLDB_RECORD_METHOD(
97       const lldb::SBCommandInterpreter &,
98       SBCommandInterpreter, operator=,(const lldb::SBCommandInterpreter &),
99       rhs);
100 
101   m_opaque_ptr = rhs.m_opaque_ptr;
102   return *this;
103 }
104 
105 bool SBCommandInterpreter::IsValid() const {
106   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommandInterpreter, IsValid);
107   return this->operator bool();
108 }
109 SBCommandInterpreter::operator bool() const {
110   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommandInterpreter, operator bool);
111 
112   return m_opaque_ptr != nullptr;
113 }
114 
115 bool SBCommandInterpreter::CommandExists(const char *cmd) {
116   LLDB_RECORD_METHOD(bool, SBCommandInterpreter, CommandExists, (const char *),
117                      cmd);
118 
119   return (((cmd != nullptr) && IsValid()) ? m_opaque_ptr->CommandExists(cmd)
120                                           : false);
121 }
122 
123 bool SBCommandInterpreter::AliasExists(const char *cmd) {
124   LLDB_RECORD_METHOD(bool, SBCommandInterpreter, AliasExists, (const char *),
125                      cmd);
126 
127   return (((cmd != nullptr) && IsValid()) ? m_opaque_ptr->AliasExists(cmd)
128                                           : false);
129 }
130 
131 bool SBCommandInterpreter::IsActive() {
132   LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommandInterpreter, IsActive);
133 
134   return (IsValid() ? m_opaque_ptr->IsActive() : false);
135 }
136 
137 bool SBCommandInterpreter::WasInterrupted() const {
138   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommandInterpreter, WasInterrupted);
139 
140   return (IsValid() ? m_opaque_ptr->WasInterrupted() : false);
141 }
142 
143 const char *SBCommandInterpreter::GetIOHandlerControlSequence(char ch) {
144   LLDB_RECORD_METHOD(const char *, SBCommandInterpreter,
145                      GetIOHandlerControlSequence, (char), ch);
146 
147   return (IsValid()
148               ? m_opaque_ptr->GetDebugger()
149                     .GetTopIOHandlerControlSequence(ch)
150                     .GetCString()
151               : nullptr);
152 }
153 
154 lldb::ReturnStatus
155 SBCommandInterpreter::HandleCommand(const char *command_line,
156                                     SBCommandReturnObject &result,
157                                     bool add_to_history) {
158   LLDB_RECORD_METHOD(lldb::ReturnStatus, SBCommandInterpreter, HandleCommand,
159                      (const char *, lldb::SBCommandReturnObject &, bool),
160                      command_line, result, add_to_history);
161 
162   SBExecutionContext sb_exe_ctx;
163   return HandleCommand(command_line, sb_exe_ctx, result, add_to_history);
164 }
165 
166 lldb::ReturnStatus SBCommandInterpreter::HandleCommand(
167     const char *command_line, SBExecutionContext &override_context,
168     SBCommandReturnObject &result, bool add_to_history) {
169   LLDB_RECORD_METHOD(lldb::ReturnStatus, SBCommandInterpreter, HandleCommand,
170                      (const char *, lldb::SBExecutionContext &,
171                       lldb::SBCommandReturnObject &, bool),
172                      command_line, override_context, result, add_to_history);
173 
174   result.Clear();
175   if (command_line && IsValid()) {
176     result.ref().SetInteractive(false);
177     auto do_add_to_history = add_to_history ? eLazyBoolYes : eLazyBoolNo;
178     if (override_context.get())
179       m_opaque_ptr->HandleCommand(command_line, do_add_to_history,
180                                   override_context.get()->Lock(true),
181                                   result.ref());
182     else
183       m_opaque_ptr->HandleCommand(command_line, do_add_to_history,
184                                   result.ref());
185   } else {
186     result->AppendError(
187         "SBCommandInterpreter or the command line is not valid");
188   }
189 
190   return result.GetStatus();
191 }
192 
193 void SBCommandInterpreter::HandleCommandsFromFile(
194     lldb::SBFileSpec &file, lldb::SBExecutionContext &override_context,
195     lldb::SBCommandInterpreterRunOptions &options,
196     lldb::SBCommandReturnObject result) {
197   LLDB_RECORD_METHOD(void, SBCommandInterpreter, HandleCommandsFromFile,
198                      (lldb::SBFileSpec &, lldb::SBExecutionContext &,
199                       lldb::SBCommandInterpreterRunOptions &,
200                       lldb::SBCommandReturnObject),
201                      file, override_context, options, result);
202 
203   if (!IsValid()) {
204     result->AppendError("SBCommandInterpreter is not valid.");
205     return;
206   }
207 
208   if (!file.IsValid()) {
209     SBStream s;
210     file.GetDescription(s);
211     result->AppendErrorWithFormat("File is not valid: %s.", s.GetData());
212   }
213 
214   FileSpec tmp_spec = file.ref();
215   if (override_context.get())
216     m_opaque_ptr->HandleCommandsFromFile(tmp_spec,
217                                          override_context.get()->Lock(true),
218                                          options.ref(),
219                                          result.ref());
220 
221   else
222     m_opaque_ptr->HandleCommandsFromFile(tmp_spec, options.ref(), result.ref());
223 }
224 
225 int SBCommandInterpreter::HandleCompletion(
226     const char *current_line, const char *cursor, const char *last_char,
227     int match_start_point, int max_return_elements, SBStringList &matches) {
228   LLDB_RECORD_METHOD(int, SBCommandInterpreter, HandleCompletion,
229                      (const char *, const char *, const char *, int, int,
230                       lldb::SBStringList &),
231                      current_line, cursor, last_char, match_start_point,
232                      max_return_elements, matches);
233 
234   SBStringList dummy_descriptions;
235   return HandleCompletionWithDescriptions(
236       current_line, cursor, last_char, match_start_point, max_return_elements,
237       matches, dummy_descriptions);
238 }
239 
240 int SBCommandInterpreter::HandleCompletionWithDescriptions(
241     const char *current_line, const char *cursor, const char *last_char,
242     int match_start_point, int max_return_elements, SBStringList &matches,
243     SBStringList &descriptions) {
244   LLDB_RECORD_METHOD(int, SBCommandInterpreter,
245                      HandleCompletionWithDescriptions,
246                      (const char *, const char *, const char *, int, int,
247                       lldb::SBStringList &, lldb::SBStringList &),
248                      current_line, cursor, last_char, match_start_point,
249                      max_return_elements, matches, descriptions);
250 
251   // Sanity check the arguments that are passed in: cursor & last_char have to
252   // be within the current_line.
253   if (current_line == nullptr || cursor == nullptr || last_char == nullptr)
254     return 0;
255 
256   if (cursor < current_line || last_char < current_line)
257     return 0;
258 
259   size_t current_line_size = strlen(current_line);
260   if (cursor - current_line > static_cast<ptrdiff_t>(current_line_size) ||
261       last_char - current_line > static_cast<ptrdiff_t>(current_line_size))
262     return 0;
263 
264   if (!IsValid())
265     return 0;
266 
267   lldb_private::StringList lldb_matches, lldb_descriptions;
268   CompletionResult result;
269   CompletionRequest request(current_line, cursor - current_line, result);
270   m_opaque_ptr->HandleCompletion(request);
271   result.GetMatches(lldb_matches);
272   result.GetDescriptions(lldb_descriptions);
273 
274   // Make the result array indexed from 1 again by adding the 'common prefix'
275   // of all completions as element 0. This is done to emulate the old API.
276   if (request.GetParsedLine().GetArgumentCount() == 0) {
277     // If we got an empty string, insert nothing.
278     lldb_matches.InsertStringAtIndex(0, "");
279     lldb_descriptions.InsertStringAtIndex(0, "");
280   } else {
281     // Now figure out if there is a common substring, and if so put that in
282     // element 0, otherwise put an empty string in element 0.
283     std::string command_partial_str = request.GetCursorArgumentPrefix().str();
284 
285     std::string common_prefix = lldb_matches.LongestCommonPrefix();
286     const size_t partial_name_len = command_partial_str.size();
287     common_prefix.erase(0, partial_name_len);
288 
289     // If we matched a unique single command, add a space... Only do this if
290     // the completer told us this was a complete word, however...
291     if (lldb_matches.GetSize() == 1) {
292       char quote_char = request.GetParsedArg().GetQuoteChar();
293       common_prefix =
294           Args::EscapeLLDBCommandArgument(common_prefix, quote_char);
295       if (request.GetParsedArg().IsQuoted())
296         common_prefix.push_back(quote_char);
297       common_prefix.push_back(' ');
298     }
299     lldb_matches.InsertStringAtIndex(0, common_prefix.c_str());
300     lldb_descriptions.InsertStringAtIndex(0, "");
301   }
302 
303   SBStringList temp_matches_list(&lldb_matches);
304   matches.AppendList(temp_matches_list);
305   SBStringList temp_descriptions_list(&lldb_descriptions);
306   descriptions.AppendList(temp_descriptions_list);
307   return result.GetNumberOfResults();
308 }
309 
310 int SBCommandInterpreter::HandleCompletionWithDescriptions(
311     const char *current_line, uint32_t cursor_pos, int match_start_point,
312     int max_return_elements, SBStringList &matches,
313     SBStringList &descriptions) {
314   LLDB_RECORD_METHOD(int, SBCommandInterpreter,
315                      HandleCompletionWithDescriptions,
316                      (const char *, uint32_t, int, int, lldb::SBStringList &,
317                       lldb::SBStringList &),
318                      current_line, cursor_pos, match_start_point,
319                      max_return_elements, matches, descriptions);
320 
321   const char *cursor = current_line + cursor_pos;
322   const char *last_char = current_line + strlen(current_line);
323   return HandleCompletionWithDescriptions(
324       current_line, cursor, last_char, match_start_point, max_return_elements,
325       matches, descriptions);
326 }
327 
328 int SBCommandInterpreter::HandleCompletion(const char *current_line,
329                                            uint32_t cursor_pos,
330                                            int match_start_point,
331                                            int max_return_elements,
332                                            lldb::SBStringList &matches) {
333   LLDB_RECORD_METHOD(int, SBCommandInterpreter, HandleCompletion,
334                      (const char *, uint32_t, int, int, lldb::SBStringList &),
335                      current_line, cursor_pos, match_start_point,
336                      max_return_elements, matches);
337 
338   const char *cursor = current_line + cursor_pos;
339   const char *last_char = current_line + strlen(current_line);
340   return HandleCompletion(current_line, cursor, last_char, match_start_point,
341                           max_return_elements, matches);
342 }
343 
344 bool SBCommandInterpreter::HasCommands() {
345   LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommandInterpreter, HasCommands);
346 
347   return (IsValid() ? m_opaque_ptr->HasCommands() : false);
348 }
349 
350 bool SBCommandInterpreter::HasAliases() {
351   LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommandInterpreter, HasAliases);
352 
353   return (IsValid() ? m_opaque_ptr->HasAliases() : false);
354 }
355 
356 bool SBCommandInterpreter::HasAliasOptions() {
357   LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommandInterpreter, HasAliasOptions);
358 
359   return (IsValid() ? m_opaque_ptr->HasAliasOptions() : false);
360 }
361 
362 SBProcess SBCommandInterpreter::GetProcess() {
363   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBProcess, SBCommandInterpreter, GetProcess);
364 
365   SBProcess sb_process;
366   ProcessSP process_sp;
367   if (IsValid()) {
368     TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
369     if (target_sp) {
370       std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
371       process_sp = target_sp->GetProcessSP();
372       sb_process.SetSP(process_sp);
373     }
374   }
375 
376   return sb_process;
377 }
378 
379 SBDebugger SBCommandInterpreter::GetDebugger() {
380   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBDebugger, SBCommandInterpreter,
381                              GetDebugger);
382 
383   SBDebugger sb_debugger;
384   if (IsValid())
385     sb_debugger.reset(m_opaque_ptr->GetDebugger().shared_from_this());
386 
387   return sb_debugger;
388 }
389 
390 bool SBCommandInterpreter::GetPromptOnQuit() {
391   LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommandInterpreter, GetPromptOnQuit);
392 
393   return (IsValid() ? m_opaque_ptr->GetPromptOnQuit() : false);
394 }
395 
396 void SBCommandInterpreter::SetPromptOnQuit(bool b) {
397   LLDB_RECORD_METHOD(void, SBCommandInterpreter, SetPromptOnQuit, (bool), b);
398 
399   if (IsValid())
400     m_opaque_ptr->SetPromptOnQuit(b);
401 }
402 
403 void SBCommandInterpreter::AllowExitCodeOnQuit(bool allow) {
404   LLDB_RECORD_METHOD(void, SBCommandInterpreter, AllowExitCodeOnQuit, (bool),
405                      allow);
406 
407   if (m_opaque_ptr)
408     m_opaque_ptr->AllowExitCodeOnQuit(allow);
409 }
410 
411 bool SBCommandInterpreter::HasCustomQuitExitCode() {
412   LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommandInterpreter, HasCustomQuitExitCode);
413 
414   bool exited = false;
415   if (m_opaque_ptr)
416     m_opaque_ptr->GetQuitExitCode(exited);
417   return exited;
418 }
419 
420 int SBCommandInterpreter::GetQuitStatus() {
421   LLDB_RECORD_METHOD_NO_ARGS(int, SBCommandInterpreter, GetQuitStatus);
422 
423   bool exited = false;
424   return (m_opaque_ptr ? m_opaque_ptr->GetQuitExitCode(exited) : 0);
425 }
426 
427 void SBCommandInterpreter::ResolveCommand(const char *command_line,
428                                           SBCommandReturnObject &result) {
429   LLDB_RECORD_METHOD(void, SBCommandInterpreter, ResolveCommand,
430                      (const char *, lldb::SBCommandReturnObject &),
431                      command_line, result);
432 
433   result.Clear();
434   if (command_line && IsValid()) {
435     m_opaque_ptr->ResolveCommand(command_line, result.ref());
436   } else {
437     result->AppendError(
438         "SBCommandInterpreter or the command line is not valid");
439   }
440 }
441 
442 CommandInterpreter *SBCommandInterpreter::get() { return m_opaque_ptr; }
443 
444 CommandInterpreter &SBCommandInterpreter::ref() {
445   assert(m_opaque_ptr);
446   return *m_opaque_ptr;
447 }
448 
449 void SBCommandInterpreter::reset(
450     lldb_private::CommandInterpreter *interpreter) {
451   m_opaque_ptr = interpreter;
452 }
453 
454 void SBCommandInterpreter::SourceInitFileInHomeDirectory(
455     SBCommandReturnObject &result) {
456   LLDB_RECORD_METHOD(void, SBCommandInterpreter, SourceInitFileInHomeDirectory,
457                      (lldb::SBCommandReturnObject &), result);
458 
459   result.Clear();
460   if (IsValid()) {
461     TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
462     std::unique_lock<std::recursive_mutex> lock;
463     if (target_sp)
464       lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
465     m_opaque_ptr->SourceInitFileHome(result.ref());
466   } else {
467     result->AppendError("SBCommandInterpreter is not valid");
468   }
469 }
470 
471 void SBCommandInterpreter::SourceInitFileInHomeDirectory(
472     SBCommandReturnObject &result, bool is_repl) {
473   LLDB_RECORD_METHOD(void, SBCommandInterpreter, SourceInitFileInHomeDirectory,
474                      (lldb::SBCommandReturnObject &, bool), result, is_repl);
475 
476   result.Clear();
477   if (IsValid()) {
478     TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
479     std::unique_lock<std::recursive_mutex> lock;
480     if (target_sp)
481       lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
482     m_opaque_ptr->SourceInitFileHome(result.ref(), is_repl);
483   } else {
484     result->AppendError("SBCommandInterpreter is not valid");
485   }
486 }
487 
488 void SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory(
489     SBCommandReturnObject &result) {
490   LLDB_RECORD_METHOD(void, SBCommandInterpreter,
491                      SourceInitFileInCurrentWorkingDirectory,
492                      (lldb::SBCommandReturnObject &), result);
493 
494   result.Clear();
495   if (IsValid()) {
496     TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
497     std::unique_lock<std::recursive_mutex> lock;
498     if (target_sp)
499       lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
500     m_opaque_ptr->SourceInitFileCwd(result.ref());
501   } else {
502     result->AppendError("SBCommandInterpreter is not valid");
503   }
504 }
505 
506 SBBroadcaster SBCommandInterpreter::GetBroadcaster() {
507   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBBroadcaster, SBCommandInterpreter,
508                              GetBroadcaster);
509 
510 
511   SBBroadcaster broadcaster(m_opaque_ptr, false);
512 
513   return broadcaster;
514 }
515 
516 const char *SBCommandInterpreter::GetBroadcasterClass() {
517   LLDB_RECORD_STATIC_METHOD_NO_ARGS(const char *, SBCommandInterpreter,
518                                     GetBroadcasterClass);
519 
520   return CommandInterpreter::GetStaticBroadcasterClass().AsCString();
521 }
522 
523 const char *SBCommandInterpreter::GetArgumentTypeAsCString(
524     const lldb::CommandArgumentType arg_type) {
525   LLDB_RECORD_STATIC_METHOD(const char *, SBCommandInterpreter,
526                             GetArgumentTypeAsCString,
527                             (const lldb::CommandArgumentType), arg_type);
528 
529   return CommandObject::GetArgumentTypeAsCString(arg_type);
530 }
531 
532 const char *SBCommandInterpreter::GetArgumentDescriptionAsCString(
533     const lldb::CommandArgumentType arg_type) {
534   LLDB_RECORD_STATIC_METHOD(const char *, SBCommandInterpreter,
535                             GetArgumentDescriptionAsCString,
536                             (const lldb::CommandArgumentType), arg_type);
537 
538   return CommandObject::GetArgumentDescriptionAsCString(arg_type);
539 }
540 
541 bool SBCommandInterpreter::EventIsCommandInterpreterEvent(
542     const lldb::SBEvent &event) {
543   LLDB_RECORD_STATIC_METHOD(bool, SBCommandInterpreter,
544                             EventIsCommandInterpreterEvent,
545                             (const lldb::SBEvent &), event);
546 
547   return event.GetBroadcasterClass() ==
548          SBCommandInterpreter::GetBroadcasterClass();
549 }
550 
551 bool SBCommandInterpreter::SetCommandOverrideCallback(
552     const char *command_name, lldb::CommandOverrideCallback callback,
553     void *baton) {
554   LLDB_RECORD_METHOD(bool, SBCommandInterpreter, SetCommandOverrideCallback,
555                      (const char *, lldb::CommandOverrideCallback, void *),
556                      command_name, callback, baton);
557 
558   if (command_name && command_name[0] && IsValid()) {
559     llvm::StringRef command_name_str = command_name;
560     CommandObject *cmd_obj =
561         m_opaque_ptr->GetCommandObjectForCommand(command_name_str);
562     if (cmd_obj) {
563       assert(command_name_str.empty());
564       cmd_obj->SetOverrideCallback(callback, baton);
565       return true;
566     }
567   }
568   return false;
569 }
570 
571 lldb::SBCommand SBCommandInterpreter::AddMultiwordCommand(const char *name,
572                                                           const char *help) {
573   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommandInterpreter, AddMultiwordCommand,
574                      (const char *, const char *), name, help);
575 
576   lldb::CommandObjectSP new_command_sp(
577       new CommandObjectMultiword(*m_opaque_ptr, name, help));
578   new_command_sp->GetAsMultiwordCommand()->SetRemovable(true);
579   Status add_error = m_opaque_ptr->AddUserCommand(name, new_command_sp, true);
580   if (add_error.Success())
581     return lldb::SBCommand(new_command_sp);
582   return lldb::SBCommand();
583 }
584 
585 lldb::SBCommand SBCommandInterpreter::AddCommand(
586     const char *name, lldb::SBCommandPluginInterface *impl, const char *help) {
587   LLDB_RECORD_METHOD(
588       lldb::SBCommand, SBCommandInterpreter, AddCommand,
589       (const char *, lldb::SBCommandPluginInterface *, const char *), name,
590       impl, help);
591 
592   return AddCommand(name, impl, help, /*syntax=*/nullptr,
593                     /*auto_repeat_command=*/"");
594 }
595 
596 lldb::SBCommand
597 SBCommandInterpreter::AddCommand(const char *name,
598                                  lldb::SBCommandPluginInterface *impl,
599                                  const char *help, const char *syntax) {
600   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommandInterpreter, AddCommand,
601                      (const char *, lldb::SBCommandPluginInterface *,
602                       const char *, const char *),
603                      name, impl, help, syntax);
604   return AddCommand(name, impl, help, syntax, /*auto_repeat_command=*/"");
605 }
606 
607 lldb::SBCommand SBCommandInterpreter::AddCommand(
608     const char *name, lldb::SBCommandPluginInterface *impl, const char *help,
609     const char *syntax, const char *auto_repeat_command) {
610   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommandInterpreter, AddCommand,
611                      (const char *, lldb::SBCommandPluginInterface *,
612                       const char *, const char *, const char *),
613                      name, impl, help, syntax, auto_repeat_command);
614 
615   lldb::CommandObjectSP new_command_sp;
616   new_command_sp = std::make_shared<CommandPluginInterfaceImplementation>(
617       *m_opaque_ptr, name, impl, help, syntax, /*flags=*/0,
618       auto_repeat_command);
619 
620   Status add_error = m_opaque_ptr->AddUserCommand(name, new_command_sp, true);
621   if (add_error.Success())
622     return lldb::SBCommand(new_command_sp);
623   return lldb::SBCommand();
624 }
625 
626 SBCommand::SBCommand() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBCommand); }
627 
628 SBCommand::SBCommand(lldb::CommandObjectSP cmd_sp) : m_opaque_sp(cmd_sp) {}
629 
630 bool SBCommand::IsValid() {
631   LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommand, IsValid);
632   return this->operator bool();
633 }
634 SBCommand::operator bool() const {
635   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommand, operator bool);
636 
637   return m_opaque_sp.get() != nullptr;
638 }
639 
640 const char *SBCommand::GetName() {
641   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBCommand, GetName);
642 
643   return (IsValid() ? ConstString(m_opaque_sp->GetCommandName()).AsCString() : nullptr);
644 }
645 
646 const char *SBCommand::GetHelp() {
647   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBCommand, GetHelp);
648 
649   return (IsValid() ? ConstString(m_opaque_sp->GetHelp()).AsCString()
650                     : nullptr);
651 }
652 
653 const char *SBCommand::GetHelpLong() {
654   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBCommand, GetHelpLong);
655 
656   return (IsValid() ? ConstString(m_opaque_sp->GetHelpLong()).AsCString()
657                     : nullptr);
658 }
659 
660 void SBCommand::SetHelp(const char *help) {
661   LLDB_RECORD_METHOD(void, SBCommand, SetHelp, (const char *), help);
662 
663   if (IsValid())
664     m_opaque_sp->SetHelp(help);
665 }
666 
667 void SBCommand::SetHelpLong(const char *help) {
668   LLDB_RECORD_METHOD(void, SBCommand, SetHelpLong, (const char *), help);
669 
670   if (IsValid())
671     m_opaque_sp->SetHelpLong(help);
672 }
673 
674 lldb::SBCommand SBCommand::AddMultiwordCommand(const char *name,
675                                                const char *help) {
676   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommand, AddMultiwordCommand,
677                      (const char *, const char *), name, help);
678 
679   if (!IsValid())
680     return lldb::SBCommand();
681   if (!m_opaque_sp->IsMultiwordObject())
682     return lldb::SBCommand();
683   CommandObjectMultiword *new_command = new CommandObjectMultiword(
684       m_opaque_sp->GetCommandInterpreter(), name, help);
685   new_command->SetRemovable(true);
686   lldb::CommandObjectSP new_command_sp(new_command);
687   if (new_command_sp && m_opaque_sp->LoadSubCommand(name, new_command_sp))
688     return lldb::SBCommand(new_command_sp);
689   return lldb::SBCommand();
690 }
691 
692 lldb::SBCommand SBCommand::AddCommand(const char *name,
693                                       lldb::SBCommandPluginInterface *impl,
694                                       const char *help) {
695   LLDB_RECORD_METHOD(
696       lldb::SBCommand, SBCommand, AddCommand,
697       (const char *, lldb::SBCommandPluginInterface *, const char *), name,
698       impl, help);
699   return AddCommand(name, impl, help, /*syntax=*/nullptr,
700                     /*auto_repeat_command=*/"");
701 }
702 
703 lldb::SBCommand SBCommand::AddCommand(const char *name,
704                                       lldb::SBCommandPluginInterface *impl,
705                                       const char *help, const char *syntax) {
706   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommand, AddCommand,
707                      (const char *, lldb::SBCommandPluginInterface *,
708                       const char *, const char *),
709                      name, impl, help, syntax);
710   return AddCommand(name, impl, help, syntax, /*auto_repeat_command=*/"");
711 }
712 
713 lldb::SBCommand SBCommand::AddCommand(const char *name,
714                                       lldb::SBCommandPluginInterface *impl,
715                                       const char *help, const char *syntax,
716                                       const char *auto_repeat_command) {
717   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommand, AddCommand,
718                      (const char *, lldb::SBCommandPluginInterface *,
719                       const char *, const char *, const char *),
720                      name, impl, help, syntax, auto_repeat_command);
721 
722   if (!IsValid())
723     return lldb::SBCommand();
724   if (!m_opaque_sp->IsMultiwordObject())
725     return lldb::SBCommand();
726   lldb::CommandObjectSP new_command_sp;
727   new_command_sp = std::make_shared<CommandPluginInterfaceImplementation>(
728       m_opaque_sp->GetCommandInterpreter(), name, impl, help, syntax,
729       /*flags=*/0, auto_repeat_command);
730   if (new_command_sp && m_opaque_sp->LoadSubCommand(name, new_command_sp))
731     return lldb::SBCommand(new_command_sp);
732   return lldb::SBCommand();
733 }
734 
735 uint32_t SBCommand::GetFlags() {
736   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBCommand, GetFlags);
737 
738   return (IsValid() ? m_opaque_sp->GetFlags().Get() : 0);
739 }
740 
741 void SBCommand::SetFlags(uint32_t flags) {
742   LLDB_RECORD_METHOD(void, SBCommand, SetFlags, (uint32_t), flags);
743 
744   if (IsValid())
745     m_opaque_sp->GetFlags().Set(flags);
746 }
747