xref: /freebsd-src/contrib/llvm-project/lldb/source/API/SBCommandInterpreter.cpp (revision 4824e7fd18a1223177218d4aec1b3c6c5c4a444e)
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 "SBReproducerPrivate.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 LLDB_RECORD_RESULT(*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 LLDB_RECORD_RESULT(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 LLDB_RECORD_RESULT(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 
514   return LLDB_RECORD_RESULT(broadcaster);
515 }
516 
517 const char *SBCommandInterpreter::GetBroadcasterClass() {
518   LLDB_RECORD_STATIC_METHOD_NO_ARGS(const char *, SBCommandInterpreter,
519                                     GetBroadcasterClass);
520 
521   return CommandInterpreter::GetStaticBroadcasterClass().AsCString();
522 }
523 
524 const char *SBCommandInterpreter::GetArgumentTypeAsCString(
525     const lldb::CommandArgumentType arg_type) {
526   LLDB_RECORD_STATIC_METHOD(const char *, SBCommandInterpreter,
527                             GetArgumentTypeAsCString,
528                             (const lldb::CommandArgumentType), arg_type);
529 
530   return CommandObject::GetArgumentTypeAsCString(arg_type);
531 }
532 
533 const char *SBCommandInterpreter::GetArgumentDescriptionAsCString(
534     const lldb::CommandArgumentType arg_type) {
535   LLDB_RECORD_STATIC_METHOD(const char *, SBCommandInterpreter,
536                             GetArgumentDescriptionAsCString,
537                             (const lldb::CommandArgumentType), arg_type);
538 
539   return CommandObject::GetArgumentDescriptionAsCString(arg_type);
540 }
541 
542 bool SBCommandInterpreter::EventIsCommandInterpreterEvent(
543     const lldb::SBEvent &event) {
544   LLDB_RECORD_STATIC_METHOD(bool, SBCommandInterpreter,
545                             EventIsCommandInterpreterEvent,
546                             (const lldb::SBEvent &), event);
547 
548   return event.GetBroadcasterClass() ==
549          SBCommandInterpreter::GetBroadcasterClass();
550 }
551 
552 bool SBCommandInterpreter::SetCommandOverrideCallback(
553     const char *command_name, lldb::CommandOverrideCallback callback,
554     void *baton) {
555   LLDB_RECORD_DUMMY(bool, SBCommandInterpreter, SetCommandOverrideCallback,
556                     (const char *, lldb::CommandOverrideCallback, void *),
557                     command_name, callback, baton);
558 
559   if (command_name && command_name[0] && IsValid()) {
560     llvm::StringRef command_name_str = command_name;
561     CommandObject *cmd_obj =
562         m_opaque_ptr->GetCommandObjectForCommand(command_name_str);
563     if (cmd_obj) {
564       assert(command_name_str.empty());
565       cmd_obj->SetOverrideCallback(callback, baton);
566       return true;
567     }
568   }
569   return false;
570 }
571 
572 lldb::SBCommand SBCommandInterpreter::AddMultiwordCommand(const char *name,
573                                                           const char *help) {
574   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommandInterpreter, AddMultiwordCommand,
575                      (const char *, const char *), name, help);
576 
577   lldb::CommandObjectSP new_command_sp(
578       new CommandObjectMultiword(*m_opaque_ptr, name, help));
579   new_command_sp->GetAsMultiwordCommand()->SetRemovable(true);
580   Status add_error = m_opaque_ptr->AddUserCommand(name, new_command_sp, true);
581   if (add_error.Success())
582     return LLDB_RECORD_RESULT(lldb::SBCommand(new_command_sp));
583   return LLDB_RECORD_RESULT(lldb::SBCommand());
584 }
585 
586 lldb::SBCommand SBCommandInterpreter::AddCommand(
587     const char *name, lldb::SBCommandPluginInterface *impl, const char *help) {
588   LLDB_RECORD_METHOD(
589       lldb::SBCommand, SBCommandInterpreter, AddCommand,
590       (const char *, lldb::SBCommandPluginInterface *, const char *), name,
591       impl, help);
592 
593   return LLDB_RECORD_RESULT(AddCommand(name, impl, help, /*syntax=*/nullptr,
594                                        /*auto_repeat_command=*/""))
595 }
596 
597 lldb::SBCommand
598 SBCommandInterpreter::AddCommand(const char *name,
599                                  lldb::SBCommandPluginInterface *impl,
600                                  const char *help, const char *syntax) {
601   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommandInterpreter, AddCommand,
602                      (const char *, lldb::SBCommandPluginInterface *,
603                       const char *, const char *),
604                      name, impl, help, syntax);
605   return LLDB_RECORD_RESULT(
606       AddCommand(name, impl, help, syntax, /*auto_repeat_command=*/""))
607 }
608 
609 lldb::SBCommand SBCommandInterpreter::AddCommand(
610     const char *name, lldb::SBCommandPluginInterface *impl, const char *help,
611     const char *syntax, const char *auto_repeat_command) {
612   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommandInterpreter, AddCommand,
613                      (const char *, lldb::SBCommandPluginInterface *,
614                       const char *, const char *, const char *),
615                      name, impl, help, syntax, auto_repeat_command);
616 
617   lldb::CommandObjectSP new_command_sp;
618   new_command_sp = std::make_shared<CommandPluginInterfaceImplementation>(
619       *m_opaque_ptr, name, impl, help, syntax, /*flags=*/0,
620       auto_repeat_command);
621 
622   Status add_error = m_opaque_ptr->AddUserCommand(name, new_command_sp, true);
623   if (add_error.Success())
624     return LLDB_RECORD_RESULT(lldb::SBCommand(new_command_sp));
625   return LLDB_RECORD_RESULT(lldb::SBCommand());
626 }
627 
628 SBCommand::SBCommand() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBCommand); }
629 
630 SBCommand::SBCommand(lldb::CommandObjectSP cmd_sp) : m_opaque_sp(cmd_sp) {}
631 
632 bool SBCommand::IsValid() {
633   LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommand, IsValid);
634   return this->operator bool();
635 }
636 SBCommand::operator bool() const {
637   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommand, operator bool);
638 
639   return m_opaque_sp.get() != nullptr;
640 }
641 
642 const char *SBCommand::GetName() {
643   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBCommand, GetName);
644 
645   return (IsValid() ? ConstString(m_opaque_sp->GetCommandName()).AsCString() : nullptr);
646 }
647 
648 const char *SBCommand::GetHelp() {
649   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBCommand, GetHelp);
650 
651   return (IsValid() ? ConstString(m_opaque_sp->GetHelp()).AsCString()
652                     : nullptr);
653 }
654 
655 const char *SBCommand::GetHelpLong() {
656   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBCommand, GetHelpLong);
657 
658   return (IsValid() ? ConstString(m_opaque_sp->GetHelpLong()).AsCString()
659                     : nullptr);
660 }
661 
662 void SBCommand::SetHelp(const char *help) {
663   LLDB_RECORD_METHOD(void, SBCommand, SetHelp, (const char *), help);
664 
665   if (IsValid())
666     m_opaque_sp->SetHelp(help);
667 }
668 
669 void SBCommand::SetHelpLong(const char *help) {
670   LLDB_RECORD_METHOD(void, SBCommand, SetHelpLong, (const char *), help);
671 
672   if (IsValid())
673     m_opaque_sp->SetHelpLong(help);
674 }
675 
676 lldb::SBCommand SBCommand::AddMultiwordCommand(const char *name,
677                                                const char *help) {
678   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommand, AddMultiwordCommand,
679                      (const char *, const char *), name, help);
680 
681   if (!IsValid())
682     return LLDB_RECORD_RESULT(lldb::SBCommand());
683   if (!m_opaque_sp->IsMultiwordObject())
684     return LLDB_RECORD_RESULT(lldb::SBCommand());
685   CommandObjectMultiword *new_command = new CommandObjectMultiword(
686       m_opaque_sp->GetCommandInterpreter(), name, help);
687   new_command->SetRemovable(true);
688   lldb::CommandObjectSP new_command_sp(new_command);
689   if (new_command_sp && m_opaque_sp->LoadSubCommand(name, new_command_sp))
690     return LLDB_RECORD_RESULT(lldb::SBCommand(new_command_sp));
691   return LLDB_RECORD_RESULT(lldb::SBCommand());
692 }
693 
694 lldb::SBCommand SBCommand::AddCommand(const char *name,
695                                       lldb::SBCommandPluginInterface *impl,
696                                       const char *help) {
697   LLDB_RECORD_METHOD(
698       lldb::SBCommand, SBCommand, AddCommand,
699       (const char *, lldb::SBCommandPluginInterface *, const char *), name,
700       impl, help);
701   return LLDB_RECORD_RESULT(AddCommand(name, impl, help, /*syntax=*/nullptr,
702                                        /*auto_repeat_command=*/""))
703 }
704 
705 lldb::SBCommand SBCommand::AddCommand(const char *name,
706                                       lldb::SBCommandPluginInterface *impl,
707                                       const char *help, const char *syntax) {
708   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommand, AddCommand,
709                      (const char *, lldb::SBCommandPluginInterface *,
710                       const char *, const char *),
711                      name, impl, help, syntax);
712   return LLDB_RECORD_RESULT(
713       AddCommand(name, impl, help, syntax, /*auto_repeat_command=*/""))
714 }
715 
716 lldb::SBCommand SBCommand::AddCommand(const char *name,
717                                       lldb::SBCommandPluginInterface *impl,
718                                       const char *help, const char *syntax,
719                                       const char *auto_repeat_command) {
720   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommand, AddCommand,
721                      (const char *, lldb::SBCommandPluginInterface *,
722                       const char *, const char *, const char *),
723                      name, impl, help, syntax, auto_repeat_command);
724 
725   if (!IsValid())
726     return LLDB_RECORD_RESULT(lldb::SBCommand());
727   if (!m_opaque_sp->IsMultiwordObject())
728     return LLDB_RECORD_RESULT(lldb::SBCommand());
729   lldb::CommandObjectSP new_command_sp;
730   new_command_sp = std::make_shared<CommandPluginInterfaceImplementation>(
731       m_opaque_sp->GetCommandInterpreter(), name, impl, help, syntax,
732       /*flags=*/0, auto_repeat_command);
733   if (new_command_sp && m_opaque_sp->LoadSubCommand(name, new_command_sp))
734     return LLDB_RECORD_RESULT(lldb::SBCommand(new_command_sp));
735   return LLDB_RECORD_RESULT(lldb::SBCommand());
736 }
737 
738 uint32_t SBCommand::GetFlags() {
739   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBCommand, GetFlags);
740 
741   return (IsValid() ? m_opaque_sp->GetFlags().Get() : 0);
742 }
743 
744 void SBCommand::SetFlags(uint32_t flags) {
745   LLDB_RECORD_METHOD(void, SBCommand, SetFlags, (uint32_t), flags);
746 
747   if (IsValid())
748     m_opaque_sp->GetFlags().Set(flags);
749 }
750 
751 namespace lldb_private {
752 namespace repro {
753 
754 template <> void RegisterMethods<SBCommandInterpreter>(Registry &R) {
755   LLDB_REGISTER_CONSTRUCTOR(SBCommandInterpreter,
756                             (lldb_private::CommandInterpreter *));
757   LLDB_REGISTER_CONSTRUCTOR(SBCommandInterpreter,
758                             (const lldb::SBCommandInterpreter &));
759   LLDB_REGISTER_METHOD(
760       const lldb::SBCommandInterpreter &,
761       SBCommandInterpreter, operator=,(const lldb::SBCommandInterpreter &));
762   LLDB_REGISTER_METHOD_CONST(bool, SBCommandInterpreter, IsValid, ());
763   LLDB_REGISTER_METHOD_CONST(bool, SBCommandInterpreter, operator bool, ());
764   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, CommandExists,
765                        (const char *));
766   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, AliasExists,
767                        (const char *));
768   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, IsActive, ());
769   LLDB_REGISTER_METHOD_CONST(bool, SBCommandInterpreter, WasInterrupted, ());
770   LLDB_REGISTER_METHOD(const char *, SBCommandInterpreter,
771                        GetIOHandlerControlSequence, (char));
772   LLDB_REGISTER_METHOD(lldb::ReturnStatus, SBCommandInterpreter,
773                        HandleCommand,
774                        (const char *, lldb::SBCommandReturnObject &, bool));
775   LLDB_REGISTER_METHOD(lldb::ReturnStatus, SBCommandInterpreter,
776                        HandleCommand,
777                        (const char *, lldb::SBExecutionContext &,
778                         lldb::SBCommandReturnObject &, bool));
779   LLDB_REGISTER_METHOD(void, SBCommandInterpreter, HandleCommandsFromFile,
780                        (lldb::SBFileSpec &, lldb::SBExecutionContext &,
781                         lldb::SBCommandInterpreterRunOptions &,
782                         lldb::SBCommandReturnObject));
783   LLDB_REGISTER_METHOD(int, SBCommandInterpreter, HandleCompletion,
784                        (const char *, const char *, const char *, int, int,
785                         lldb::SBStringList &));
786   LLDB_REGISTER_METHOD(int, SBCommandInterpreter,
787                        HandleCompletionWithDescriptions,
788                        (const char *, const char *, const char *, int, int,
789                         lldb::SBStringList &, lldb::SBStringList &));
790   LLDB_REGISTER_METHOD(int, SBCommandInterpreter,
791                        HandleCompletionWithDescriptions,
792                        (const char *, uint32_t, int, int,
793                         lldb::SBStringList &, lldb::SBStringList &));
794   LLDB_REGISTER_METHOD(
795       int, SBCommandInterpreter, HandleCompletion,
796       (const char *, uint32_t, int, int, lldb::SBStringList &));
797   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, HasCommands, ());
798   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, HasAliases, ());
799   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, HasAliasOptions, ());
800   LLDB_REGISTER_METHOD(lldb::SBProcess, SBCommandInterpreter, GetProcess, ());
801   LLDB_REGISTER_METHOD(lldb::SBDebugger, SBCommandInterpreter, GetDebugger,
802                        ());
803   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, GetPromptOnQuit, ());
804   LLDB_REGISTER_METHOD(void, SBCommandInterpreter, SetPromptOnQuit, (bool));
805   LLDB_REGISTER_METHOD(void, SBCommandInterpreter, AllowExitCodeOnQuit,
806                        (bool));
807   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, HasCustomQuitExitCode, ());
808   LLDB_REGISTER_METHOD(int, SBCommandInterpreter, GetQuitStatus, ());
809   LLDB_REGISTER_METHOD(void, SBCommandInterpreter, ResolveCommand,
810                        (const char *, lldb::SBCommandReturnObject &));
811   LLDB_REGISTER_METHOD(void, SBCommandInterpreter,
812                        SourceInitFileInHomeDirectory,
813                        (lldb::SBCommandReturnObject &));
814   LLDB_REGISTER_METHOD(void, SBCommandInterpreter,
815                        SourceInitFileInHomeDirectory,
816                        (lldb::SBCommandReturnObject &, bool));
817   LLDB_REGISTER_METHOD(void, SBCommandInterpreter,
818                        SourceInitFileInCurrentWorkingDirectory,
819                        (lldb::SBCommandReturnObject &));
820   LLDB_REGISTER_METHOD(lldb::SBBroadcaster, SBCommandInterpreter,
821                        GetBroadcaster, ());
822   LLDB_REGISTER_STATIC_METHOD(const char *, SBCommandInterpreter,
823                               GetBroadcasterClass, ());
824   LLDB_REGISTER_STATIC_METHOD(const char *, SBCommandInterpreter,
825                               GetArgumentTypeAsCString,
826                               (const lldb::CommandArgumentType));
827   LLDB_REGISTER_STATIC_METHOD(const char *, SBCommandInterpreter,
828                               GetArgumentDescriptionAsCString,
829                               (const lldb::CommandArgumentType));
830   LLDB_REGISTER_STATIC_METHOD(bool, SBCommandInterpreter,
831                               EventIsCommandInterpreterEvent,
832                               (const lldb::SBEvent &));
833   LLDB_REGISTER_METHOD(lldb::SBCommand, SBCommandInterpreter,
834                        AddMultiwordCommand, (const char *, const char *));
835   LLDB_REGISTER_METHOD(
836       lldb::SBCommand, SBCommandInterpreter, AddCommand,
837       (const char *, lldb::SBCommandPluginInterface *, const char *));
838   LLDB_REGISTER_METHOD(lldb::SBCommand, SBCommandInterpreter, AddCommand,
839                        (const char *, lldb::SBCommandPluginInterface *,
840                         const char *, const char *));
841   LLDB_REGISTER_METHOD(lldb::SBCommand, SBCommandInterpreter, AddCommand,
842                        (const char *, lldb::SBCommandPluginInterface *,
843                         const char *, const char *, const char *));
844   LLDB_REGISTER_CONSTRUCTOR(SBCommand, ());
845   LLDB_REGISTER_METHOD(bool, SBCommand, IsValid, ());
846   LLDB_REGISTER_METHOD_CONST(bool, SBCommand, operator bool, ());
847   LLDB_REGISTER_METHOD(const char *, SBCommand, GetName, ());
848   LLDB_REGISTER_METHOD(const char *, SBCommand, GetHelp, ());
849   LLDB_REGISTER_METHOD(const char *, SBCommand, GetHelpLong, ());
850   LLDB_REGISTER_METHOD(void, SBCommand, SetHelp, (const char *));
851   LLDB_REGISTER_METHOD(void, SBCommand, SetHelpLong, (const char *));
852   LLDB_REGISTER_METHOD(lldb::SBCommand, SBCommand, AddMultiwordCommand,
853                        (const char *, const char *));
854   LLDB_REGISTER_METHOD(
855       lldb::SBCommand, SBCommand, AddCommand,
856       (const char *, lldb::SBCommandPluginInterface *, const char *));
857   LLDB_REGISTER_METHOD(lldb::SBCommand, SBCommand, AddCommand,
858                        (const char *, lldb::SBCommandPluginInterface *,
859                         const char *, const char *));
860   LLDB_REGISTER_METHOD(lldb::SBCommand, SBCommand, AddCommand,
861                        (const char *, lldb::SBCommandPluginInterface *,
862                         const char *, const char *, const char *));
863   LLDB_REGISTER_METHOD(uint32_t, SBCommand, GetFlags, ());
864   LLDB_REGISTER_METHOD(void, SBCommand, SetFlags, (uint32_t));
865 }
866 }
867 }
868