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