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