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/SBCommandReturnObject.h"
21 #include "lldb/API/SBEvent.h"
22 #include "lldb/API/SBExecutionContext.h"
23 #include "lldb/API/SBListener.h"
24 #include "lldb/API/SBProcess.h"
25 #include "lldb/API/SBStream.h"
26 #include "lldb/API/SBStringList.h"
27 #include "lldb/API/SBTarget.h"
28 
29 #include <memory>
30 
31 using namespace lldb;
32 using namespace lldb_private;
33 
34 SBCommandInterpreterRunOptions::SBCommandInterpreterRunOptions() {
35   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBCommandInterpreterRunOptions);
36 
37   m_opaque_up.reset(new CommandInterpreterRunOptions());
38 }
39 
40 SBCommandInterpreterRunOptions::~SBCommandInterpreterRunOptions() = default;
41 
42 bool SBCommandInterpreterRunOptions::GetStopOnContinue() const {
43   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommandInterpreterRunOptions,
44                                    GetStopOnContinue);
45 
46   return m_opaque_up->GetStopOnContinue();
47 }
48 
49 void SBCommandInterpreterRunOptions::SetStopOnContinue(bool stop_on_continue) {
50   LLDB_RECORD_METHOD(void, SBCommandInterpreterRunOptions, SetStopOnContinue,
51                      (bool), stop_on_continue);
52 
53   m_opaque_up->SetStopOnContinue(stop_on_continue);
54 }
55 
56 bool SBCommandInterpreterRunOptions::GetStopOnError() const {
57   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommandInterpreterRunOptions,
58                                    GetStopOnError);
59 
60   return m_opaque_up->GetStopOnError();
61 }
62 
63 void SBCommandInterpreterRunOptions::SetStopOnError(bool stop_on_error) {
64   LLDB_RECORD_METHOD(void, SBCommandInterpreterRunOptions, SetStopOnError,
65                      (bool), stop_on_error);
66 
67   m_opaque_up->SetStopOnError(stop_on_error);
68 }
69 
70 bool SBCommandInterpreterRunOptions::GetStopOnCrash() const {
71   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommandInterpreterRunOptions,
72                                    GetStopOnCrash);
73 
74   return m_opaque_up->GetStopOnCrash();
75 }
76 
77 void SBCommandInterpreterRunOptions::SetStopOnCrash(bool stop_on_crash) {
78   LLDB_RECORD_METHOD(void, SBCommandInterpreterRunOptions, SetStopOnCrash,
79                      (bool), stop_on_crash);
80 
81   m_opaque_up->SetStopOnCrash(stop_on_crash);
82 }
83 
84 bool SBCommandInterpreterRunOptions::GetEchoCommands() const {
85   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommandInterpreterRunOptions,
86                                    GetEchoCommands);
87 
88   return m_opaque_up->GetEchoCommands();
89 }
90 
91 void SBCommandInterpreterRunOptions::SetEchoCommands(bool echo_commands) {
92   LLDB_RECORD_METHOD(void, SBCommandInterpreterRunOptions, SetEchoCommands,
93                      (bool), echo_commands);
94 
95   m_opaque_up->SetEchoCommands(echo_commands);
96 }
97 
98 bool SBCommandInterpreterRunOptions::GetEchoCommentCommands() const {
99   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommandInterpreterRunOptions,
100                                    GetEchoCommentCommands);
101 
102   return m_opaque_up->GetEchoCommentCommands();
103 }
104 
105 void SBCommandInterpreterRunOptions::SetEchoCommentCommands(bool echo) {
106   LLDB_RECORD_METHOD(void, SBCommandInterpreterRunOptions,
107                      SetEchoCommentCommands, (bool), echo);
108 
109   m_opaque_up->SetEchoCommentCommands(echo);
110 }
111 
112 bool SBCommandInterpreterRunOptions::GetPrintResults() const {
113   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommandInterpreterRunOptions,
114                                    GetPrintResults);
115 
116   return m_opaque_up->GetPrintResults();
117 }
118 
119 void SBCommandInterpreterRunOptions::SetPrintResults(bool print_results) {
120   LLDB_RECORD_METHOD(void, SBCommandInterpreterRunOptions, SetPrintResults,
121                      (bool), print_results);
122 
123   m_opaque_up->SetPrintResults(print_results);
124 }
125 
126 bool SBCommandInterpreterRunOptions::GetAddToHistory() const {
127   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommandInterpreterRunOptions,
128                                    GetAddToHistory);
129 
130   return m_opaque_up->GetAddToHistory();
131 }
132 
133 void SBCommandInterpreterRunOptions::SetAddToHistory(bool add_to_history) {
134   LLDB_RECORD_METHOD(void, SBCommandInterpreterRunOptions, SetAddToHistory,
135                      (bool), add_to_history);
136 
137   m_opaque_up->SetAddToHistory(add_to_history);
138 }
139 
140 lldb_private::CommandInterpreterRunOptions *
141 SBCommandInterpreterRunOptions::get() const {
142   return m_opaque_up.get();
143 }
144 
145 lldb_private::CommandInterpreterRunOptions &
146 SBCommandInterpreterRunOptions::ref() const {
147   return *m_opaque_up;
148 }
149 
150 class CommandPluginInterfaceImplementation : public CommandObjectParsed {
151 public:
152   CommandPluginInterfaceImplementation(CommandInterpreter &interpreter,
153                                        const char *name,
154                                        lldb::SBCommandPluginInterface *backend,
155                                        const char *help = nullptr,
156                                        const char *syntax = nullptr,
157                                        uint32_t flags = 0,
158                                        const char *auto_repeat_command = "")
159       : CommandObjectParsed(interpreter, name, help, syntax, flags),
160         m_backend(backend) {
161     m_auto_repeat_command =
162         auto_repeat_command == nullptr
163             ? llvm::None
164             : llvm::Optional<std::string>(auto_repeat_command);
165   }
166 
167   bool IsRemovable() const override { return true; }
168 
169   /// More documentation is available in lldb::CommandObject::GetRepeatCommand,
170   /// but in short, if nullptr is returned, the previous command will be
171   /// repeated, and if an empty string is returned, no commands will be
172   /// executed.
173   const char *GetRepeatCommand(Args &current_command_args,
174                                uint32_t index) override {
175     if (!m_auto_repeat_command)
176       return nullptr;
177     else
178       return m_auto_repeat_command->c_str();
179   }
180 
181 protected:
182   bool DoExecute(Args &command, CommandReturnObject &result) override {
183     SBCommandReturnObject sb_return(result);
184     SBCommandInterpreter sb_interpreter(&m_interpreter);
185     SBDebugger debugger_sb(m_interpreter.GetDebugger().shared_from_this());
186     bool ret = m_backend->DoExecute(
187         debugger_sb, (char **)command.GetArgumentVector(), sb_return);
188     return ret;
189   }
190   std::shared_ptr<lldb::SBCommandPluginInterface> m_backend;
191   llvm::Optional<std::string> m_auto_repeat_command;
192 };
193 
194 SBCommandInterpreter::SBCommandInterpreter(CommandInterpreter *interpreter)
195     : m_opaque_ptr(interpreter) {
196   LLDB_RECORD_CONSTRUCTOR(SBCommandInterpreter,
197                           (lldb_private::CommandInterpreter *), interpreter);
198 
199 }
200 
201 SBCommandInterpreter::SBCommandInterpreter(const SBCommandInterpreter &rhs)
202     : m_opaque_ptr(rhs.m_opaque_ptr) {
203   LLDB_RECORD_CONSTRUCTOR(SBCommandInterpreter,
204                           (const lldb::SBCommandInterpreter &), rhs);
205 }
206 
207 SBCommandInterpreter::~SBCommandInterpreter() = default;
208 
209 const SBCommandInterpreter &SBCommandInterpreter::
210 operator=(const SBCommandInterpreter &rhs) {
211   LLDB_RECORD_METHOD(
212       const lldb::SBCommandInterpreter &,
213       SBCommandInterpreter, operator=,(const lldb::SBCommandInterpreter &),
214       rhs);
215 
216   m_opaque_ptr = rhs.m_opaque_ptr;
217   return LLDB_RECORD_RESULT(*this);
218 }
219 
220 bool SBCommandInterpreter::IsValid() const {
221   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommandInterpreter, IsValid);
222   return this->operator bool();
223 }
224 SBCommandInterpreter::operator bool() const {
225   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommandInterpreter, operator bool);
226 
227   return m_opaque_ptr != nullptr;
228 }
229 
230 bool SBCommandInterpreter::CommandExists(const char *cmd) {
231   LLDB_RECORD_METHOD(bool, SBCommandInterpreter, CommandExists, (const char *),
232                      cmd);
233 
234   return (((cmd != nullptr) && IsValid()) ? m_opaque_ptr->CommandExists(cmd)
235                                           : false);
236 }
237 
238 bool SBCommandInterpreter::AliasExists(const char *cmd) {
239   LLDB_RECORD_METHOD(bool, SBCommandInterpreter, AliasExists, (const char *),
240                      cmd);
241 
242   return (((cmd != nullptr) && IsValid()) ? m_opaque_ptr->AliasExists(cmd)
243                                           : false);
244 }
245 
246 bool SBCommandInterpreter::IsActive() {
247   LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommandInterpreter, IsActive);
248 
249   return (IsValid() ? m_opaque_ptr->IsActive() : false);
250 }
251 
252 bool SBCommandInterpreter::WasInterrupted() const {
253   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommandInterpreter, WasInterrupted);
254 
255   return (IsValid() ? m_opaque_ptr->WasInterrupted() : false);
256 }
257 
258 const char *SBCommandInterpreter::GetIOHandlerControlSequence(char ch) {
259   LLDB_RECORD_METHOD(const char *, SBCommandInterpreter,
260                      GetIOHandlerControlSequence, (char), ch);
261 
262   return (IsValid()
263               ? m_opaque_ptr->GetDebugger()
264                     .GetTopIOHandlerControlSequence(ch)
265                     .GetCString()
266               : nullptr);
267 }
268 
269 lldb::ReturnStatus
270 SBCommandInterpreter::HandleCommand(const char *command_line,
271                                     SBCommandReturnObject &result,
272                                     bool add_to_history) {
273   LLDB_RECORD_METHOD(lldb::ReturnStatus, SBCommandInterpreter, HandleCommand,
274                      (const char *, lldb::SBCommandReturnObject &, bool),
275                      command_line, result, add_to_history);
276 
277   SBExecutionContext sb_exe_ctx;
278   return HandleCommand(command_line, sb_exe_ctx, result, add_to_history);
279 }
280 
281 lldb::ReturnStatus SBCommandInterpreter::HandleCommand(
282     const char *command_line, SBExecutionContext &override_context,
283     SBCommandReturnObject &result, bool add_to_history) {
284   LLDB_RECORD_METHOD(lldb::ReturnStatus, SBCommandInterpreter, HandleCommand,
285                      (const char *, lldb::SBExecutionContext &,
286                       lldb::SBCommandReturnObject &, bool),
287                      command_line, override_context, result, add_to_history);
288 
289 
290   ExecutionContext ctx, *ctx_ptr;
291   if (override_context.get()) {
292     ctx = override_context.get()->Lock(true);
293     ctx_ptr = &ctx;
294   } else
295     ctx_ptr = nullptr;
296 
297   result.Clear();
298   if (command_line && IsValid()) {
299     result.ref().SetInteractive(false);
300     m_opaque_ptr->HandleCommand(command_line,
301                                 add_to_history ? eLazyBoolYes : eLazyBoolNo,
302                                 result.ref(), ctx_ptr);
303   } else {
304     result->AppendError(
305         "SBCommandInterpreter or the command line is not valid");
306     result->SetStatus(eReturnStatusFailed);
307   }
308 
309 
310   return result.GetStatus();
311 }
312 
313 void SBCommandInterpreter::HandleCommandsFromFile(
314     lldb::SBFileSpec &file, lldb::SBExecutionContext &override_context,
315     lldb::SBCommandInterpreterRunOptions &options,
316     lldb::SBCommandReturnObject result) {
317   LLDB_RECORD_METHOD(void, SBCommandInterpreter, HandleCommandsFromFile,
318                      (lldb::SBFileSpec &, lldb::SBExecutionContext &,
319                       lldb::SBCommandInterpreterRunOptions &,
320                       lldb::SBCommandReturnObject),
321                      file, override_context, options, result);
322 
323   if (!IsValid()) {
324     result->AppendError("SBCommandInterpreter is not valid.");
325     result->SetStatus(eReturnStatusFailed);
326     return;
327   }
328 
329   if (!file.IsValid()) {
330     SBStream s;
331     file.GetDescription(s);
332     result->AppendErrorWithFormat("File is not valid: %s.", s.GetData());
333     result->SetStatus(eReturnStatusFailed);
334   }
335 
336   FileSpec tmp_spec = file.ref();
337   ExecutionContext ctx, *ctx_ptr;
338   if (override_context.get()) {
339     ctx = override_context.get()->Lock(true);
340     ctx_ptr = &ctx;
341   } else
342     ctx_ptr = nullptr;
343 
344   m_opaque_ptr->HandleCommandsFromFile(tmp_spec, ctx_ptr, options.ref(),
345                                        result.ref());
346 }
347 
348 int SBCommandInterpreter::HandleCompletion(
349     const char *current_line, const char *cursor, const char *last_char,
350     int match_start_point, int max_return_elements, SBStringList &matches) {
351   LLDB_RECORD_METHOD(int, SBCommandInterpreter, HandleCompletion,
352                      (const char *, const char *, const char *, int, int,
353                       lldb::SBStringList &),
354                      current_line, cursor, last_char, match_start_point,
355                      max_return_elements, matches);
356 
357   SBStringList dummy_descriptions;
358   return HandleCompletionWithDescriptions(
359       current_line, cursor, last_char, match_start_point, max_return_elements,
360       matches, dummy_descriptions);
361 }
362 
363 int SBCommandInterpreter::HandleCompletionWithDescriptions(
364     const char *current_line, const char *cursor, const char *last_char,
365     int match_start_point, int max_return_elements, SBStringList &matches,
366     SBStringList &descriptions) {
367   LLDB_RECORD_METHOD(int, SBCommandInterpreter,
368                      HandleCompletionWithDescriptions,
369                      (const char *, const char *, const char *, int, int,
370                       lldb::SBStringList &, lldb::SBStringList &),
371                      current_line, cursor, last_char, match_start_point,
372                      max_return_elements, matches, descriptions);
373 
374   // Sanity check the arguments that are passed in: cursor & last_char have to
375   // be within the current_line.
376   if (current_line == nullptr || cursor == nullptr || last_char == nullptr)
377     return 0;
378 
379   if (cursor < current_line || last_char < current_line)
380     return 0;
381 
382   size_t current_line_size = strlen(current_line);
383   if (cursor - current_line > static_cast<ptrdiff_t>(current_line_size) ||
384       last_char - current_line > static_cast<ptrdiff_t>(current_line_size))
385     return 0;
386 
387   if (!IsValid())
388     return 0;
389 
390   lldb_private::StringList lldb_matches, lldb_descriptions;
391   CompletionResult result;
392   CompletionRequest request(current_line, cursor - current_line, result);
393   m_opaque_ptr->HandleCompletion(request);
394   result.GetMatches(lldb_matches);
395   result.GetDescriptions(lldb_descriptions);
396 
397   // Make the result array indexed from 1 again by adding the 'common prefix'
398   // of all completions as element 0. This is done to emulate the old API.
399   if (request.GetParsedLine().GetArgumentCount() == 0) {
400     // If we got an empty string, insert nothing.
401     lldb_matches.InsertStringAtIndex(0, "");
402     lldb_descriptions.InsertStringAtIndex(0, "");
403   } else {
404     // Now figure out if there is a common substring, and if so put that in
405     // element 0, otherwise put an empty string in element 0.
406     std::string command_partial_str = request.GetCursorArgumentPrefix().str();
407 
408     std::string common_prefix = lldb_matches.LongestCommonPrefix();
409     const size_t partial_name_len = command_partial_str.size();
410     common_prefix.erase(0, partial_name_len);
411 
412     // If we matched a unique single command, add a space... Only do this if
413     // the completer told us this was a complete word, however...
414     if (lldb_matches.GetSize() == 1) {
415       char quote_char = request.GetParsedArg().GetQuoteChar();
416       common_prefix =
417           Args::EscapeLLDBCommandArgument(common_prefix, quote_char);
418       if (request.GetParsedArg().IsQuoted())
419         common_prefix.push_back(quote_char);
420       common_prefix.push_back(' ');
421     }
422     lldb_matches.InsertStringAtIndex(0, common_prefix.c_str());
423     lldb_descriptions.InsertStringAtIndex(0, "");
424   }
425 
426   SBStringList temp_matches_list(&lldb_matches);
427   matches.AppendList(temp_matches_list);
428   SBStringList temp_descriptions_list(&lldb_descriptions);
429   descriptions.AppendList(temp_descriptions_list);
430   return result.GetNumberOfResults();
431 }
432 
433 int SBCommandInterpreter::HandleCompletionWithDescriptions(
434     const char *current_line, uint32_t cursor_pos, int match_start_point,
435     int max_return_elements, SBStringList &matches,
436     SBStringList &descriptions) {
437   LLDB_RECORD_METHOD(int, SBCommandInterpreter,
438                      HandleCompletionWithDescriptions,
439                      (const char *, uint32_t, int, int, lldb::SBStringList &,
440                       lldb::SBStringList &),
441                      current_line, cursor_pos, match_start_point,
442                      max_return_elements, matches, descriptions);
443 
444   const char *cursor = current_line + cursor_pos;
445   const char *last_char = current_line + strlen(current_line);
446   return HandleCompletionWithDescriptions(
447       current_line, cursor, last_char, match_start_point, max_return_elements,
448       matches, descriptions);
449 }
450 
451 int SBCommandInterpreter::HandleCompletion(const char *current_line,
452                                            uint32_t cursor_pos,
453                                            int match_start_point,
454                                            int max_return_elements,
455                                            lldb::SBStringList &matches) {
456   LLDB_RECORD_METHOD(int, SBCommandInterpreter, HandleCompletion,
457                      (const char *, uint32_t, int, int, lldb::SBStringList &),
458                      current_line, cursor_pos, match_start_point,
459                      max_return_elements, matches);
460 
461   const char *cursor = current_line + cursor_pos;
462   const char *last_char = current_line + strlen(current_line);
463   return HandleCompletion(current_line, cursor, last_char, match_start_point,
464                           max_return_elements, matches);
465 }
466 
467 bool SBCommandInterpreter::HasCommands() {
468   LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommandInterpreter, HasCommands);
469 
470   return (IsValid() ? m_opaque_ptr->HasCommands() : false);
471 }
472 
473 bool SBCommandInterpreter::HasAliases() {
474   LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommandInterpreter, HasAliases);
475 
476   return (IsValid() ? m_opaque_ptr->HasAliases() : false);
477 }
478 
479 bool SBCommandInterpreter::HasAliasOptions() {
480   LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommandInterpreter, HasAliasOptions);
481 
482   return (IsValid() ? m_opaque_ptr->HasAliasOptions() : false);
483 }
484 
485 SBProcess SBCommandInterpreter::GetProcess() {
486   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBProcess, SBCommandInterpreter, GetProcess);
487 
488   SBProcess sb_process;
489   ProcessSP process_sp;
490   if (IsValid()) {
491     TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
492     if (target_sp) {
493       std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
494       process_sp = target_sp->GetProcessSP();
495       sb_process.SetSP(process_sp);
496     }
497   }
498 
499   return LLDB_RECORD_RESULT(sb_process);
500 }
501 
502 SBDebugger SBCommandInterpreter::GetDebugger() {
503   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBDebugger, SBCommandInterpreter,
504                              GetDebugger);
505 
506   SBDebugger sb_debugger;
507   if (IsValid())
508     sb_debugger.reset(m_opaque_ptr->GetDebugger().shared_from_this());
509 
510   return LLDB_RECORD_RESULT(sb_debugger);
511 }
512 
513 bool SBCommandInterpreter::GetPromptOnQuit() {
514   LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommandInterpreter, GetPromptOnQuit);
515 
516   return (IsValid() ? m_opaque_ptr->GetPromptOnQuit() : false);
517 }
518 
519 void SBCommandInterpreter::SetPromptOnQuit(bool b) {
520   LLDB_RECORD_METHOD(void, SBCommandInterpreter, SetPromptOnQuit, (bool), b);
521 
522   if (IsValid())
523     m_opaque_ptr->SetPromptOnQuit(b);
524 }
525 
526 void SBCommandInterpreter::AllowExitCodeOnQuit(bool allow) {
527   LLDB_RECORD_METHOD(void, SBCommandInterpreter, AllowExitCodeOnQuit, (bool),
528                      allow);
529 
530   if (m_opaque_ptr)
531     m_opaque_ptr->AllowExitCodeOnQuit(allow);
532 }
533 
534 bool SBCommandInterpreter::HasCustomQuitExitCode() {
535   LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommandInterpreter, HasCustomQuitExitCode);
536 
537   bool exited = false;
538   if (m_opaque_ptr)
539     m_opaque_ptr->GetQuitExitCode(exited);
540   return exited;
541 }
542 
543 int SBCommandInterpreter::GetQuitStatus() {
544   LLDB_RECORD_METHOD_NO_ARGS(int, SBCommandInterpreter, GetQuitStatus);
545 
546   bool exited = false;
547   return (m_opaque_ptr ? m_opaque_ptr->GetQuitExitCode(exited) : 0);
548 }
549 
550 void SBCommandInterpreter::ResolveCommand(const char *command_line,
551                                           SBCommandReturnObject &result) {
552   LLDB_RECORD_METHOD(void, SBCommandInterpreter, ResolveCommand,
553                      (const char *, lldb::SBCommandReturnObject &),
554                      command_line, result);
555 
556   result.Clear();
557   if (command_line && IsValid()) {
558     m_opaque_ptr->ResolveCommand(command_line, result.ref());
559   } else {
560     result->AppendError(
561         "SBCommandInterpreter or the command line is not valid");
562     result->SetStatus(eReturnStatusFailed);
563   }
564 }
565 
566 CommandInterpreter *SBCommandInterpreter::get() { return m_opaque_ptr; }
567 
568 CommandInterpreter &SBCommandInterpreter::ref() {
569   assert(m_opaque_ptr);
570   return *m_opaque_ptr;
571 }
572 
573 void SBCommandInterpreter::reset(
574     lldb_private::CommandInterpreter *interpreter) {
575   m_opaque_ptr = interpreter;
576 }
577 
578 void SBCommandInterpreter::SourceInitFileInHomeDirectory(
579     SBCommandReturnObject &result) {
580   LLDB_RECORD_METHOD(void, SBCommandInterpreter, SourceInitFileInHomeDirectory,
581                      (lldb::SBCommandReturnObject &), result);
582 
583   result.Clear();
584   if (IsValid()) {
585     TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
586     std::unique_lock<std::recursive_mutex> lock;
587     if (target_sp)
588       lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
589     m_opaque_ptr->SourceInitFileHome(result.ref());
590   } else {
591     result->AppendError("SBCommandInterpreter is not valid");
592     result->SetStatus(eReturnStatusFailed);
593   }
594 }
595 
596 void SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory(
597     SBCommandReturnObject &result) {
598   LLDB_RECORD_METHOD(void, SBCommandInterpreter,
599                      SourceInitFileInCurrentWorkingDirectory,
600                      (lldb::SBCommandReturnObject &), result);
601 
602   result.Clear();
603   if (IsValid()) {
604     TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
605     std::unique_lock<std::recursive_mutex> lock;
606     if (target_sp)
607       lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
608     m_opaque_ptr->SourceInitFileCwd(result.ref());
609   } else {
610     result->AppendError("SBCommandInterpreter is not valid");
611     result->SetStatus(eReturnStatusFailed);
612   }
613 }
614 
615 SBBroadcaster SBCommandInterpreter::GetBroadcaster() {
616   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBBroadcaster, SBCommandInterpreter,
617                              GetBroadcaster);
618 
619 
620   SBBroadcaster broadcaster(m_opaque_ptr, false);
621 
622 
623   return LLDB_RECORD_RESULT(broadcaster);
624 }
625 
626 const char *SBCommandInterpreter::GetBroadcasterClass() {
627   LLDB_RECORD_STATIC_METHOD_NO_ARGS(const char *, SBCommandInterpreter,
628                                     GetBroadcasterClass);
629 
630   return CommandInterpreter::GetStaticBroadcasterClass().AsCString();
631 }
632 
633 const char *SBCommandInterpreter::GetArgumentTypeAsCString(
634     const lldb::CommandArgumentType arg_type) {
635   LLDB_RECORD_STATIC_METHOD(const char *, SBCommandInterpreter,
636                             GetArgumentTypeAsCString,
637                             (const lldb::CommandArgumentType), arg_type);
638 
639   return CommandObject::GetArgumentTypeAsCString(arg_type);
640 }
641 
642 const char *SBCommandInterpreter::GetArgumentDescriptionAsCString(
643     const lldb::CommandArgumentType arg_type) {
644   LLDB_RECORD_STATIC_METHOD(const char *, SBCommandInterpreter,
645                             GetArgumentDescriptionAsCString,
646                             (const lldb::CommandArgumentType), arg_type);
647 
648   return CommandObject::GetArgumentDescriptionAsCString(arg_type);
649 }
650 
651 bool SBCommandInterpreter::EventIsCommandInterpreterEvent(
652     const lldb::SBEvent &event) {
653   LLDB_RECORD_STATIC_METHOD(bool, SBCommandInterpreter,
654                             EventIsCommandInterpreterEvent,
655                             (const lldb::SBEvent &), event);
656 
657   return event.GetBroadcasterClass() ==
658          SBCommandInterpreter::GetBroadcasterClass();
659 }
660 
661 bool SBCommandInterpreter::SetCommandOverrideCallback(
662     const char *command_name, lldb::CommandOverrideCallback callback,
663     void *baton) {
664   LLDB_RECORD_DUMMY(bool, SBCommandInterpreter, SetCommandOverrideCallback,
665                     (const char *, lldb::CommandOverrideCallback, void *),
666                     command_name, callback, baton);
667 
668   if (command_name && command_name[0] && IsValid()) {
669     llvm::StringRef command_name_str = command_name;
670     CommandObject *cmd_obj =
671         m_opaque_ptr->GetCommandObjectForCommand(command_name_str);
672     if (cmd_obj) {
673       assert(command_name_str.empty());
674       cmd_obj->SetOverrideCallback(callback, baton);
675       return true;
676     }
677   }
678   return false;
679 }
680 
681 lldb::SBCommand SBCommandInterpreter::AddMultiwordCommand(const char *name,
682                                                           const char *help) {
683   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommandInterpreter, AddMultiwordCommand,
684                      (const char *, const char *), name, help);
685 
686   CommandObjectMultiword *new_command =
687       new CommandObjectMultiword(*m_opaque_ptr, name, help);
688   new_command->SetRemovable(true);
689   lldb::CommandObjectSP new_command_sp(new_command);
690   if (new_command_sp &&
691       m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
692     return LLDB_RECORD_RESULT(lldb::SBCommand(new_command_sp));
693   return LLDB_RECORD_RESULT(lldb::SBCommand());
694 }
695 
696 lldb::SBCommand SBCommandInterpreter::AddCommand(
697     const char *name, lldb::SBCommandPluginInterface *impl, const char *help) {
698   LLDB_RECORD_METHOD(
699       lldb::SBCommand, SBCommandInterpreter, AddCommand,
700       (const char *, lldb::SBCommandPluginInterface *, const char *), name,
701       impl, help);
702 
703   return LLDB_RECORD_RESULT(AddCommand(name, impl, help, /*syntax=*/nullptr,
704                                        /*auto_repeat_command=*/""))
705 }
706 
707 lldb::SBCommand
708 SBCommandInterpreter::AddCommand(const char *name,
709                                  lldb::SBCommandPluginInterface *impl,
710                                  const char *help, const char *syntax) {
711   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommandInterpreter, AddCommand,
712                      (const char *, lldb::SBCommandPluginInterface *,
713                       const char *, const char *),
714                      name, impl, help, syntax);
715   return LLDB_RECORD_RESULT(
716       AddCommand(name, impl, help, syntax, /*auto_repeat_command=*/""))
717 }
718 
719 lldb::SBCommand SBCommandInterpreter::AddCommand(
720     const char *name, lldb::SBCommandPluginInterface *impl, const char *help,
721     const char *syntax, const char *auto_repeat_command) {
722   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommandInterpreter, AddCommand,
723                      (const char *, lldb::SBCommandPluginInterface *,
724                       const char *, const char *, const char *),
725                      name, impl, help, syntax, auto_repeat_command);
726 
727   lldb::CommandObjectSP new_command_sp;
728   new_command_sp = std::make_shared<CommandPluginInterfaceImplementation>(
729       *m_opaque_ptr, name, impl, help, syntax, /*flags=*/0,
730       auto_repeat_command);
731 
732   if (new_command_sp &&
733       m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
734     return LLDB_RECORD_RESULT(lldb::SBCommand(new_command_sp));
735   return LLDB_RECORD_RESULT(lldb::SBCommand());
736 }
737 
738 SBCommand::SBCommand() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBCommand); }
739 
740 SBCommand::SBCommand(lldb::CommandObjectSP cmd_sp) : m_opaque_sp(cmd_sp) {}
741 
742 bool SBCommand::IsValid() {
743   LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommand, IsValid);
744   return this->operator bool();
745 }
746 SBCommand::operator bool() const {
747   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommand, operator bool);
748 
749   return m_opaque_sp.get() != nullptr;
750 }
751 
752 const char *SBCommand::GetName() {
753   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBCommand, GetName);
754 
755   return (IsValid() ? ConstString(m_opaque_sp->GetCommandName()).AsCString() : nullptr);
756 }
757 
758 const char *SBCommand::GetHelp() {
759   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBCommand, GetHelp);
760 
761   return (IsValid() ? ConstString(m_opaque_sp->GetHelp()).AsCString()
762                     : nullptr);
763 }
764 
765 const char *SBCommand::GetHelpLong() {
766   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBCommand, GetHelpLong);
767 
768   return (IsValid() ? ConstString(m_opaque_sp->GetHelpLong()).AsCString()
769                     : nullptr);
770 }
771 
772 void SBCommand::SetHelp(const char *help) {
773   LLDB_RECORD_METHOD(void, SBCommand, SetHelp, (const char *), help);
774 
775   if (IsValid())
776     m_opaque_sp->SetHelp(help);
777 }
778 
779 void SBCommand::SetHelpLong(const char *help) {
780   LLDB_RECORD_METHOD(void, SBCommand, SetHelpLong, (const char *), help);
781 
782   if (IsValid())
783     m_opaque_sp->SetHelpLong(help);
784 }
785 
786 lldb::SBCommand SBCommand::AddMultiwordCommand(const char *name,
787                                                const char *help) {
788   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommand, AddMultiwordCommand,
789                      (const char *, const char *), name, help);
790 
791   if (!IsValid())
792     return LLDB_RECORD_RESULT(lldb::SBCommand());
793   if (!m_opaque_sp->IsMultiwordObject())
794     return LLDB_RECORD_RESULT(lldb::SBCommand());
795   CommandObjectMultiword *new_command = new CommandObjectMultiword(
796       m_opaque_sp->GetCommandInterpreter(), name, help);
797   new_command->SetRemovable(true);
798   lldb::CommandObjectSP new_command_sp(new_command);
799   if (new_command_sp && m_opaque_sp->LoadSubCommand(name, new_command_sp))
800     return LLDB_RECORD_RESULT(lldb::SBCommand(new_command_sp));
801   return LLDB_RECORD_RESULT(lldb::SBCommand());
802 }
803 
804 lldb::SBCommand SBCommand::AddCommand(const char *name,
805                                       lldb::SBCommandPluginInterface *impl,
806                                       const char *help) {
807   LLDB_RECORD_METHOD(
808       lldb::SBCommand, SBCommand, AddCommand,
809       (const char *, lldb::SBCommandPluginInterface *, const char *), name,
810       impl, help);
811   return LLDB_RECORD_RESULT(AddCommand(name, impl, help, /*syntax=*/nullptr,
812                                        /*auto_repeat_command=*/""))
813 }
814 
815 lldb::SBCommand SBCommand::AddCommand(const char *name,
816                                       lldb::SBCommandPluginInterface *impl,
817                                       const char *help, const char *syntax) {
818   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommand, AddCommand,
819                      (const char *, lldb::SBCommandPluginInterface *,
820                       const char *, const char *),
821                      name, impl, help, syntax);
822   return LLDB_RECORD_RESULT(
823       AddCommand(name, impl, help, syntax, /*auto_repeat_command=*/""))
824 }
825 
826 lldb::SBCommand SBCommand::AddCommand(const char *name,
827                                       lldb::SBCommandPluginInterface *impl,
828                                       const char *help, const char *syntax,
829                                       const char *auto_repeat_command) {
830   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommand, AddCommand,
831                      (const char *, lldb::SBCommandPluginInterface *,
832                       const char *, const char *, const char *),
833                      name, impl, help, syntax, auto_repeat_command);
834 
835   if (!IsValid())
836     return LLDB_RECORD_RESULT(lldb::SBCommand());
837   if (!m_opaque_sp->IsMultiwordObject())
838     return LLDB_RECORD_RESULT(lldb::SBCommand());
839   lldb::CommandObjectSP new_command_sp;
840   new_command_sp = std::make_shared<CommandPluginInterfaceImplementation>(
841       m_opaque_sp->GetCommandInterpreter(), name, impl, help, syntax,
842       /*flags=*/0, auto_repeat_command);
843   if (new_command_sp && m_opaque_sp->LoadSubCommand(name, new_command_sp))
844     return LLDB_RECORD_RESULT(lldb::SBCommand(new_command_sp));
845   return LLDB_RECORD_RESULT(lldb::SBCommand());
846 }
847 
848 uint32_t SBCommand::GetFlags() {
849   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBCommand, GetFlags);
850 
851   return (IsValid() ? m_opaque_sp->GetFlags().Get() : 0);
852 }
853 
854 void SBCommand::SetFlags(uint32_t flags) {
855   LLDB_RECORD_METHOD(void, SBCommand, SetFlags, (uint32_t), flags);
856 
857   if (IsValid())
858     m_opaque_sp->GetFlags().Set(flags);
859 }
860 
861 namespace lldb_private {
862 namespace repro {
863 
864 template <>
865 void RegisterMethods<SBCommandInterpreterRunOptions>(Registry &R) {
866   LLDB_REGISTER_CONSTRUCTOR(SBCommandInterpreterRunOptions, ());
867   LLDB_REGISTER_METHOD_CONST(bool, SBCommandInterpreterRunOptions,
868                              GetStopOnContinue, ());
869   LLDB_REGISTER_METHOD(void, SBCommandInterpreterRunOptions,
870                        SetStopOnContinue, (bool));
871   LLDB_REGISTER_METHOD_CONST(bool, SBCommandInterpreterRunOptions,
872                              GetStopOnError, ());
873   LLDB_REGISTER_METHOD(void, SBCommandInterpreterRunOptions, SetStopOnError,
874                        (bool));
875   LLDB_REGISTER_METHOD_CONST(bool, SBCommandInterpreterRunOptions,
876                              GetStopOnCrash, ());
877   LLDB_REGISTER_METHOD(void, SBCommandInterpreterRunOptions, SetStopOnCrash,
878                        (bool));
879   LLDB_REGISTER_METHOD_CONST(bool, SBCommandInterpreterRunOptions,
880                              GetEchoCommands, ());
881   LLDB_REGISTER_METHOD(void, SBCommandInterpreterRunOptions, SetEchoCommands,
882                        (bool));
883   LLDB_REGISTER_METHOD_CONST(bool, SBCommandInterpreterRunOptions,
884                              GetEchoCommentCommands, ());
885   LLDB_REGISTER_METHOD(void, SBCommandInterpreterRunOptions,
886                        SetEchoCommentCommands, (bool));
887   LLDB_REGISTER_METHOD_CONST(bool, SBCommandInterpreterRunOptions,
888                              GetPrintResults, ());
889   LLDB_REGISTER_METHOD(void, SBCommandInterpreterRunOptions, SetPrintResults,
890                        (bool));
891   LLDB_REGISTER_METHOD_CONST(bool, SBCommandInterpreterRunOptions,
892                              GetAddToHistory, ());
893   LLDB_REGISTER_METHOD(void, SBCommandInterpreterRunOptions, SetAddToHistory,
894                        (bool));
895   LLDB_REGISTER_CONSTRUCTOR(SBCommandInterpreter,
896                             (lldb_private::CommandInterpreter *));
897   LLDB_REGISTER_CONSTRUCTOR(SBCommandInterpreter,
898                             (const lldb::SBCommandInterpreter &));
899   LLDB_REGISTER_METHOD(
900       const lldb::SBCommandInterpreter &,
901       SBCommandInterpreter, operator=,(const lldb::SBCommandInterpreter &));
902   LLDB_REGISTER_METHOD_CONST(bool, SBCommandInterpreter, IsValid, ());
903   LLDB_REGISTER_METHOD_CONST(bool, SBCommandInterpreter, operator bool, ());
904   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, CommandExists,
905                        (const char *));
906   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, AliasExists,
907                        (const char *));
908   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, IsActive, ());
909   LLDB_REGISTER_METHOD_CONST(bool, SBCommandInterpreter, WasInterrupted, ());
910   LLDB_REGISTER_METHOD(const char *, SBCommandInterpreter,
911                        GetIOHandlerControlSequence, (char));
912   LLDB_REGISTER_METHOD(lldb::ReturnStatus, SBCommandInterpreter,
913                        HandleCommand,
914                        (const char *, lldb::SBCommandReturnObject &, bool));
915   LLDB_REGISTER_METHOD(lldb::ReturnStatus, SBCommandInterpreter,
916                        HandleCommand,
917                        (const char *, lldb::SBExecutionContext &,
918                         lldb::SBCommandReturnObject &, bool));
919   LLDB_REGISTER_METHOD(void, SBCommandInterpreter, HandleCommandsFromFile,
920                        (lldb::SBFileSpec &, lldb::SBExecutionContext &,
921                         lldb::SBCommandInterpreterRunOptions &,
922                         lldb::SBCommandReturnObject));
923   LLDB_REGISTER_METHOD(int, SBCommandInterpreter, HandleCompletion,
924                        (const char *, const char *, const char *, int, int,
925                         lldb::SBStringList &));
926   LLDB_REGISTER_METHOD(int, SBCommandInterpreter,
927                        HandleCompletionWithDescriptions,
928                        (const char *, const char *, const char *, int, int,
929                         lldb::SBStringList &, lldb::SBStringList &));
930   LLDB_REGISTER_METHOD(int, SBCommandInterpreter,
931                        HandleCompletionWithDescriptions,
932                        (const char *, uint32_t, int, int,
933                         lldb::SBStringList &, lldb::SBStringList &));
934   LLDB_REGISTER_METHOD(
935       int, SBCommandInterpreter, HandleCompletion,
936       (const char *, uint32_t, int, int, lldb::SBStringList &));
937   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, HasCommands, ());
938   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, HasAliases, ());
939   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, HasAliasOptions, ());
940   LLDB_REGISTER_METHOD(lldb::SBProcess, SBCommandInterpreter, GetProcess, ());
941   LLDB_REGISTER_METHOD(lldb::SBDebugger, SBCommandInterpreter, GetDebugger,
942                        ());
943   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, GetPromptOnQuit, ());
944   LLDB_REGISTER_METHOD(void, SBCommandInterpreter, SetPromptOnQuit, (bool));
945   LLDB_REGISTER_METHOD(void, SBCommandInterpreter, AllowExitCodeOnQuit,
946                        (bool));
947   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, HasCustomQuitExitCode, ());
948   LLDB_REGISTER_METHOD(int, SBCommandInterpreter, GetQuitStatus, ());
949   LLDB_REGISTER_METHOD(void, SBCommandInterpreter, ResolveCommand,
950                        (const char *, lldb::SBCommandReturnObject &));
951   LLDB_REGISTER_METHOD(void, SBCommandInterpreter,
952                        SourceInitFileInHomeDirectory,
953                        (lldb::SBCommandReturnObject &));
954   LLDB_REGISTER_METHOD(void, SBCommandInterpreter,
955                        SourceInitFileInCurrentWorkingDirectory,
956                        (lldb::SBCommandReturnObject &));
957   LLDB_REGISTER_METHOD(lldb::SBBroadcaster, SBCommandInterpreter,
958                        GetBroadcaster, ());
959   LLDB_REGISTER_STATIC_METHOD(const char *, SBCommandInterpreter,
960                               GetBroadcasterClass, ());
961   LLDB_REGISTER_STATIC_METHOD(const char *, SBCommandInterpreter,
962                               GetArgumentTypeAsCString,
963                               (const lldb::CommandArgumentType));
964   LLDB_REGISTER_STATIC_METHOD(const char *, SBCommandInterpreter,
965                               GetArgumentDescriptionAsCString,
966                               (const lldb::CommandArgumentType));
967   LLDB_REGISTER_STATIC_METHOD(bool, SBCommandInterpreter,
968                               EventIsCommandInterpreterEvent,
969                               (const lldb::SBEvent &));
970   LLDB_REGISTER_METHOD(lldb::SBCommand, SBCommandInterpreter,
971                        AddMultiwordCommand, (const char *, const char *));
972   LLDB_REGISTER_METHOD(
973       lldb::SBCommand, SBCommandInterpreter, AddCommand,
974       (const char *, lldb::SBCommandPluginInterface *, const char *));
975   LLDB_REGISTER_METHOD(lldb::SBCommand, SBCommandInterpreter, AddCommand,
976                        (const char *, lldb::SBCommandPluginInterface *,
977                         const char *, const char *));
978   LLDB_REGISTER_METHOD(lldb::SBCommand, SBCommandInterpreter, AddCommand,
979                        (const char *, lldb::SBCommandPluginInterface *,
980                         const char *, const char *, const char *));
981   LLDB_REGISTER_CONSTRUCTOR(SBCommand, ());
982   LLDB_REGISTER_METHOD(bool, SBCommand, IsValid, ());
983   LLDB_REGISTER_METHOD_CONST(bool, SBCommand, operator bool, ());
984   LLDB_REGISTER_METHOD(const char *, SBCommand, GetName, ());
985   LLDB_REGISTER_METHOD(const char *, SBCommand, GetHelp, ());
986   LLDB_REGISTER_METHOD(const char *, SBCommand, GetHelpLong, ());
987   LLDB_REGISTER_METHOD(void, SBCommand, SetHelp, (const char *));
988   LLDB_REGISTER_METHOD(void, SBCommand, SetHelpLong, (const char *));
989   LLDB_REGISTER_METHOD(lldb::SBCommand, SBCommand, AddMultiwordCommand,
990                        (const char *, const char *));
991   LLDB_REGISTER_METHOD(
992       lldb::SBCommand, SBCommand, AddCommand,
993       (const char *, lldb::SBCommandPluginInterface *, const char *));
994   LLDB_REGISTER_METHOD(lldb::SBCommand, SBCommand, AddCommand,
995                        (const char *, lldb::SBCommandPluginInterface *,
996                         const char *, const char *));
997   LLDB_REGISTER_METHOD(lldb::SBCommand, SBCommand, AddCommand,
998                        (const char *, lldb::SBCommandPluginInterface *,
999                         const char *, const char *, const char *));
1000   LLDB_REGISTER_METHOD(uint32_t, SBCommand, GetFlags, ());
1001   LLDB_REGISTER_METHOD(void, SBCommand, SetFlags, (uint32_t));
1002 }
1003 
1004 }
1005 }
1006