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:
CommandPluginInterfaceImplementation(CommandInterpreter & interpreter,const char * name,lldb::SBCommandPluginInterface * backend,const char * help=nullptr,const char * syntax=nullptr,uint32_t flags=0,const char * auto_repeat_command="")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 
IsRemovable() const52   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.
GetRepeatCommand(Args & current_command_args,uint32_t index)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:
DoExecute(Args & command,CommandReturnObject & result)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 
SBCommandInterpreter(CommandInterpreter * interpreter)79 SBCommandInterpreter::SBCommandInterpreter(CommandInterpreter *interpreter)
80     : m_opaque_ptr(interpreter) {
81   LLDB_RECORD_CONSTRUCTOR(SBCommandInterpreter,
82                           (lldb_private::CommandInterpreter *), interpreter);
83 
84 }
85 
SBCommandInterpreter(const SBCommandInterpreter & rhs)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::
operator =(const SBCommandInterpreter & rhs)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 
IsValid() const105 bool SBCommandInterpreter::IsValid() const {
106   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommandInterpreter, IsValid);
107   return this->operator bool();
108 }
operator bool() const109 SBCommandInterpreter::operator bool() const {
110   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommandInterpreter, operator bool);
111 
112   return m_opaque_ptr != nullptr;
113 }
114 
CommandExists(const char * cmd)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 
AliasExists(const char * cmd)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 
IsActive()131 bool SBCommandInterpreter::IsActive() {
132   LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommandInterpreter, IsActive);
133 
134   return (IsValid() ? m_opaque_ptr->IsActive() : false);
135 }
136 
WasInterrupted() const137 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 
GetIOHandlerControlSequence(char ch)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
HandleCommand(const char * command_line,SBCommandReturnObject & result,bool add_to_history)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 
HandleCommand(const char * command_line,SBExecutionContext & override_context,SBCommandReturnObject & result,bool add_to_history)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 
HandleCommandsFromFile(lldb::SBFileSpec & file,lldb::SBExecutionContext & override_context,lldb::SBCommandInterpreterRunOptions & options,lldb::SBCommandReturnObject result)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 
HandleCompletion(const char * current_line,const char * cursor,const char * last_char,int match_start_point,int max_return_elements,SBStringList & matches)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 
HandleCompletionWithDescriptions(const char * current_line,const char * cursor,const char * last_char,int match_start_point,int max_return_elements,SBStringList & matches,SBStringList & descriptions)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 
HandleCompletionWithDescriptions(const char * current_line,uint32_t cursor_pos,int match_start_point,int max_return_elements,SBStringList & matches,SBStringList & descriptions)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 
HandleCompletion(const char * current_line,uint32_t cursor_pos,int match_start_point,int max_return_elements,lldb::SBStringList & matches)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 
HasCommands()344 bool SBCommandInterpreter::HasCommands() {
345   LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommandInterpreter, HasCommands);
346 
347   return (IsValid() ? m_opaque_ptr->HasCommands() : false);
348 }
349 
HasAliases()350 bool SBCommandInterpreter::HasAliases() {
351   LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommandInterpreter, HasAliases);
352 
353   return (IsValid() ? m_opaque_ptr->HasAliases() : false);
354 }
355 
HasAliasOptions()356 bool SBCommandInterpreter::HasAliasOptions() {
357   LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommandInterpreter, HasAliasOptions);
358 
359   return (IsValid() ? m_opaque_ptr->HasAliasOptions() : false);
360 }
361 
GetProcess()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 
GetDebugger()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 
GetPromptOnQuit()390 bool SBCommandInterpreter::GetPromptOnQuit() {
391   LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommandInterpreter, GetPromptOnQuit);
392 
393   return (IsValid() ? m_opaque_ptr->GetPromptOnQuit() : false);
394 }
395 
SetPromptOnQuit(bool b)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 
AllowExitCodeOnQuit(bool allow)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 
HasCustomQuitExitCode()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 
GetQuitStatus()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 
ResolveCommand(const char * command_line,SBCommandReturnObject & result)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 
get()442 CommandInterpreter *SBCommandInterpreter::get() { return m_opaque_ptr; }
443 
ref()444 CommandInterpreter &SBCommandInterpreter::ref() {
445   assert(m_opaque_ptr);
446   return *m_opaque_ptr;
447 }
448 
reset(lldb_private::CommandInterpreter * interpreter)449 void SBCommandInterpreter::reset(
450     lldb_private::CommandInterpreter *interpreter) {
451   m_opaque_ptr = interpreter;
452 }
453 
SourceInitFileInHomeDirectory(SBCommandReturnObject & result)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 
SourceInitFileInHomeDirectory(SBCommandReturnObject & result,bool is_repl)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 
SourceInitFileInCurrentWorkingDirectory(SBCommandReturnObject & result)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 
GetBroadcaster()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 
GetBroadcasterClass()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 
GetArgumentTypeAsCString(const lldb::CommandArgumentType arg_type)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 
GetArgumentDescriptionAsCString(const lldb::CommandArgumentType arg_type)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 
EventIsCommandInterpreterEvent(const lldb::SBEvent & event)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 
SetCommandOverrideCallback(const char * command_name,lldb::CommandOverrideCallback callback,void * baton)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 
AddMultiwordCommand(const char * name,const char * help)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   CommandObjectMultiword *new_command =
578       new CommandObjectMultiword(*m_opaque_ptr, name, help);
579   new_command->SetRemovable(true);
580   lldb::CommandObjectSP new_command_sp(new_command);
581   if (new_command_sp &&
582       m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
583     return LLDB_RECORD_RESULT(lldb::SBCommand(new_command_sp));
584   return LLDB_RECORD_RESULT(lldb::SBCommand());
585 }
586 
AddCommand(const char * name,lldb::SBCommandPluginInterface * impl,const char * help)587 lldb::SBCommand SBCommandInterpreter::AddCommand(
588     const char *name, lldb::SBCommandPluginInterface *impl, const char *help) {
589   LLDB_RECORD_METHOD(
590       lldb::SBCommand, SBCommandInterpreter, AddCommand,
591       (const char *, lldb::SBCommandPluginInterface *, const char *), name,
592       impl, help);
593 
594   return LLDB_RECORD_RESULT(AddCommand(name, impl, help, /*syntax=*/nullptr,
595                                        /*auto_repeat_command=*/""))
596 }
597 
598 lldb::SBCommand
AddCommand(const char * name,lldb::SBCommandPluginInterface * impl,const char * help,const char * syntax)599 SBCommandInterpreter::AddCommand(const char *name,
600                                  lldb::SBCommandPluginInterface *impl,
601                                  const char *help, const char *syntax) {
602   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommandInterpreter, AddCommand,
603                      (const char *, lldb::SBCommandPluginInterface *,
604                       const char *, const char *),
605                      name, impl, help, syntax);
606   return LLDB_RECORD_RESULT(
607       AddCommand(name, impl, help, syntax, /*auto_repeat_command=*/""))
608 }
609 
AddCommand(const char * name,lldb::SBCommandPluginInterface * impl,const char * help,const char * syntax,const char * auto_repeat_command)610 lldb::SBCommand SBCommandInterpreter::AddCommand(
611     const char *name, lldb::SBCommandPluginInterface *impl, const char *help,
612     const char *syntax, const char *auto_repeat_command) {
613   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommandInterpreter, AddCommand,
614                      (const char *, lldb::SBCommandPluginInterface *,
615                       const char *, const char *, const char *),
616                      name, impl, help, syntax, auto_repeat_command);
617 
618   lldb::CommandObjectSP new_command_sp;
619   new_command_sp = std::make_shared<CommandPluginInterfaceImplementation>(
620       *m_opaque_ptr, name, impl, help, syntax, /*flags=*/0,
621       auto_repeat_command);
622 
623   if (new_command_sp &&
624       m_opaque_ptr->AddUserCommand(name, new_command_sp, true))
625     return LLDB_RECORD_RESULT(lldb::SBCommand(new_command_sp));
626   return LLDB_RECORD_RESULT(lldb::SBCommand());
627 }
628 
SBCommand()629 SBCommand::SBCommand() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBCommand); }
630 
SBCommand(lldb::CommandObjectSP cmd_sp)631 SBCommand::SBCommand(lldb::CommandObjectSP cmd_sp) : m_opaque_sp(cmd_sp) {}
632 
IsValid()633 bool SBCommand::IsValid() {
634   LLDB_RECORD_METHOD_NO_ARGS(bool, SBCommand, IsValid);
635   return this->operator bool();
636 }
operator bool() const637 SBCommand::operator bool() const {
638   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCommand, operator bool);
639 
640   return m_opaque_sp.get() != nullptr;
641 }
642 
GetName()643 const char *SBCommand::GetName() {
644   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBCommand, GetName);
645 
646   return (IsValid() ? ConstString(m_opaque_sp->GetCommandName()).AsCString() : nullptr);
647 }
648 
GetHelp()649 const char *SBCommand::GetHelp() {
650   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBCommand, GetHelp);
651 
652   return (IsValid() ? ConstString(m_opaque_sp->GetHelp()).AsCString()
653                     : nullptr);
654 }
655 
GetHelpLong()656 const char *SBCommand::GetHelpLong() {
657   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBCommand, GetHelpLong);
658 
659   return (IsValid() ? ConstString(m_opaque_sp->GetHelpLong()).AsCString()
660                     : nullptr);
661 }
662 
SetHelp(const char * help)663 void SBCommand::SetHelp(const char *help) {
664   LLDB_RECORD_METHOD(void, SBCommand, SetHelp, (const char *), help);
665 
666   if (IsValid())
667     m_opaque_sp->SetHelp(help);
668 }
669 
SetHelpLong(const char * help)670 void SBCommand::SetHelpLong(const char *help) {
671   LLDB_RECORD_METHOD(void, SBCommand, SetHelpLong, (const char *), help);
672 
673   if (IsValid())
674     m_opaque_sp->SetHelpLong(help);
675 }
676 
AddMultiwordCommand(const char * name,const char * help)677 lldb::SBCommand SBCommand::AddMultiwordCommand(const char *name,
678                                                const char *help) {
679   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommand, AddMultiwordCommand,
680                      (const char *, const char *), name, help);
681 
682   if (!IsValid())
683     return LLDB_RECORD_RESULT(lldb::SBCommand());
684   if (!m_opaque_sp->IsMultiwordObject())
685     return LLDB_RECORD_RESULT(lldb::SBCommand());
686   CommandObjectMultiword *new_command = new CommandObjectMultiword(
687       m_opaque_sp->GetCommandInterpreter(), name, help);
688   new_command->SetRemovable(true);
689   lldb::CommandObjectSP new_command_sp(new_command);
690   if (new_command_sp && m_opaque_sp->LoadSubCommand(name, new_command_sp))
691     return LLDB_RECORD_RESULT(lldb::SBCommand(new_command_sp));
692   return LLDB_RECORD_RESULT(lldb::SBCommand());
693 }
694 
AddCommand(const char * name,lldb::SBCommandPluginInterface * impl,const char * help)695 lldb::SBCommand SBCommand::AddCommand(const char *name,
696                                       lldb::SBCommandPluginInterface *impl,
697                                       const char *help) {
698   LLDB_RECORD_METHOD(
699       lldb::SBCommand, SBCommand, AddCommand,
700       (const char *, lldb::SBCommandPluginInterface *, const char *), name,
701       impl, help);
702   return LLDB_RECORD_RESULT(AddCommand(name, impl, help, /*syntax=*/nullptr,
703                                        /*auto_repeat_command=*/""))
704 }
705 
AddCommand(const char * name,lldb::SBCommandPluginInterface * impl,const char * help,const char * syntax)706 lldb::SBCommand SBCommand::AddCommand(const char *name,
707                                       lldb::SBCommandPluginInterface *impl,
708                                       const char *help, const char *syntax) {
709   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommand, AddCommand,
710                      (const char *, lldb::SBCommandPluginInterface *,
711                       const char *, const char *),
712                      name, impl, help, syntax);
713   return LLDB_RECORD_RESULT(
714       AddCommand(name, impl, help, syntax, /*auto_repeat_command=*/""))
715 }
716 
AddCommand(const char * name,lldb::SBCommandPluginInterface * impl,const char * help,const char * syntax,const char * auto_repeat_command)717 lldb::SBCommand SBCommand::AddCommand(const char *name,
718                                       lldb::SBCommandPluginInterface *impl,
719                                       const char *help, const char *syntax,
720                                       const char *auto_repeat_command) {
721   LLDB_RECORD_METHOD(lldb::SBCommand, SBCommand, AddCommand,
722                      (const char *, lldb::SBCommandPluginInterface *,
723                       const char *, const char *, const char *),
724                      name, impl, help, syntax, auto_repeat_command);
725 
726   if (!IsValid())
727     return LLDB_RECORD_RESULT(lldb::SBCommand());
728   if (!m_opaque_sp->IsMultiwordObject())
729     return LLDB_RECORD_RESULT(lldb::SBCommand());
730   lldb::CommandObjectSP new_command_sp;
731   new_command_sp = std::make_shared<CommandPluginInterfaceImplementation>(
732       m_opaque_sp->GetCommandInterpreter(), name, impl, help, syntax,
733       /*flags=*/0, auto_repeat_command);
734   if (new_command_sp && m_opaque_sp->LoadSubCommand(name, new_command_sp))
735     return LLDB_RECORD_RESULT(lldb::SBCommand(new_command_sp));
736   return LLDB_RECORD_RESULT(lldb::SBCommand());
737 }
738 
GetFlags()739 uint32_t SBCommand::GetFlags() {
740   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBCommand, GetFlags);
741 
742   return (IsValid() ? m_opaque_sp->GetFlags().Get() : 0);
743 }
744 
SetFlags(uint32_t flags)745 void SBCommand::SetFlags(uint32_t flags) {
746   LLDB_RECORD_METHOD(void, SBCommand, SetFlags, (uint32_t), flags);
747 
748   if (IsValid())
749     m_opaque_sp->GetFlags().Set(flags);
750 }
751 
752 namespace lldb_private {
753 namespace repro {
754 
RegisterMethods(Registry & R)755 template <> void RegisterMethods<SBCommandInterpreter>(Registry &R) {
756   LLDB_REGISTER_CONSTRUCTOR(SBCommandInterpreter,
757                             (lldb_private::CommandInterpreter *));
758   LLDB_REGISTER_CONSTRUCTOR(SBCommandInterpreter,
759                             (const lldb::SBCommandInterpreter &));
760   LLDB_REGISTER_METHOD(
761       const lldb::SBCommandInterpreter &,
762       SBCommandInterpreter, operator=,(const lldb::SBCommandInterpreter &));
763   LLDB_REGISTER_METHOD_CONST(bool, SBCommandInterpreter, IsValid, ());
764   LLDB_REGISTER_METHOD_CONST(bool, SBCommandInterpreter, operator bool, ());
765   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, CommandExists,
766                        (const char *));
767   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, AliasExists,
768                        (const char *));
769   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, IsActive, ());
770   LLDB_REGISTER_METHOD_CONST(bool, SBCommandInterpreter, WasInterrupted, ());
771   LLDB_REGISTER_METHOD(const char *, SBCommandInterpreter,
772                        GetIOHandlerControlSequence, (char));
773   LLDB_REGISTER_METHOD(lldb::ReturnStatus, SBCommandInterpreter,
774                        HandleCommand,
775                        (const char *, lldb::SBCommandReturnObject &, bool));
776   LLDB_REGISTER_METHOD(lldb::ReturnStatus, SBCommandInterpreter,
777                        HandleCommand,
778                        (const char *, lldb::SBExecutionContext &,
779                         lldb::SBCommandReturnObject &, bool));
780   LLDB_REGISTER_METHOD(void, SBCommandInterpreter, HandleCommandsFromFile,
781                        (lldb::SBFileSpec &, lldb::SBExecutionContext &,
782                         lldb::SBCommandInterpreterRunOptions &,
783                         lldb::SBCommandReturnObject));
784   LLDB_REGISTER_METHOD(int, SBCommandInterpreter, HandleCompletion,
785                        (const char *, const char *, const char *, int, int,
786                         lldb::SBStringList &));
787   LLDB_REGISTER_METHOD(int, SBCommandInterpreter,
788                        HandleCompletionWithDescriptions,
789                        (const char *, const char *, const char *, int, int,
790                         lldb::SBStringList &, lldb::SBStringList &));
791   LLDB_REGISTER_METHOD(int, SBCommandInterpreter,
792                        HandleCompletionWithDescriptions,
793                        (const char *, uint32_t, int, int,
794                         lldb::SBStringList &, lldb::SBStringList &));
795   LLDB_REGISTER_METHOD(
796       int, SBCommandInterpreter, HandleCompletion,
797       (const char *, uint32_t, int, int, lldb::SBStringList &));
798   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, HasCommands, ());
799   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, HasAliases, ());
800   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, HasAliasOptions, ());
801   LLDB_REGISTER_METHOD(lldb::SBProcess, SBCommandInterpreter, GetProcess, ());
802   LLDB_REGISTER_METHOD(lldb::SBDebugger, SBCommandInterpreter, GetDebugger,
803                        ());
804   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, GetPromptOnQuit, ());
805   LLDB_REGISTER_METHOD(void, SBCommandInterpreter, SetPromptOnQuit, (bool));
806   LLDB_REGISTER_METHOD(void, SBCommandInterpreter, AllowExitCodeOnQuit,
807                        (bool));
808   LLDB_REGISTER_METHOD(bool, SBCommandInterpreter, HasCustomQuitExitCode, ());
809   LLDB_REGISTER_METHOD(int, SBCommandInterpreter, GetQuitStatus, ());
810   LLDB_REGISTER_METHOD(void, SBCommandInterpreter, ResolveCommand,
811                        (const char *, lldb::SBCommandReturnObject &));
812   LLDB_REGISTER_METHOD(void, SBCommandInterpreter,
813                        SourceInitFileInHomeDirectory,
814                        (lldb::SBCommandReturnObject &));
815   LLDB_REGISTER_METHOD(void, SBCommandInterpreter,
816                        SourceInitFileInHomeDirectory,
817                        (lldb::SBCommandReturnObject &, bool));
818   LLDB_REGISTER_METHOD(void, SBCommandInterpreter,
819                        SourceInitFileInCurrentWorkingDirectory,
820                        (lldb::SBCommandReturnObject &));
821   LLDB_REGISTER_METHOD(lldb::SBBroadcaster, SBCommandInterpreter,
822                        GetBroadcaster, ());
823   LLDB_REGISTER_STATIC_METHOD(const char *, SBCommandInterpreter,
824                               GetBroadcasterClass, ());
825   LLDB_REGISTER_STATIC_METHOD(const char *, SBCommandInterpreter,
826                               GetArgumentTypeAsCString,
827                               (const lldb::CommandArgumentType));
828   LLDB_REGISTER_STATIC_METHOD(const char *, SBCommandInterpreter,
829                               GetArgumentDescriptionAsCString,
830                               (const lldb::CommandArgumentType));
831   LLDB_REGISTER_STATIC_METHOD(bool, SBCommandInterpreter,
832                               EventIsCommandInterpreterEvent,
833                               (const lldb::SBEvent &));
834   LLDB_REGISTER_METHOD(lldb::SBCommand, SBCommandInterpreter,
835                        AddMultiwordCommand, (const char *, const char *));
836   LLDB_REGISTER_METHOD(
837       lldb::SBCommand, SBCommandInterpreter, AddCommand,
838       (const char *, lldb::SBCommandPluginInterface *, const char *));
839   LLDB_REGISTER_METHOD(lldb::SBCommand, SBCommandInterpreter, AddCommand,
840                        (const char *, lldb::SBCommandPluginInterface *,
841                         const char *, const char *));
842   LLDB_REGISTER_METHOD(lldb::SBCommand, SBCommandInterpreter, AddCommand,
843                        (const char *, lldb::SBCommandPluginInterface *,
844                         const char *, const char *, const char *));
845   LLDB_REGISTER_CONSTRUCTOR(SBCommand, ());
846   LLDB_REGISTER_METHOD(bool, SBCommand, IsValid, ());
847   LLDB_REGISTER_METHOD_CONST(bool, SBCommand, operator bool, ());
848   LLDB_REGISTER_METHOD(const char *, SBCommand, GetName, ());
849   LLDB_REGISTER_METHOD(const char *, SBCommand, GetHelp, ());
850   LLDB_REGISTER_METHOD(const char *, SBCommand, GetHelpLong, ());
851   LLDB_REGISTER_METHOD(void, SBCommand, SetHelp, (const char *));
852   LLDB_REGISTER_METHOD(void, SBCommand, SetHelpLong, (const char *));
853   LLDB_REGISTER_METHOD(lldb::SBCommand, SBCommand, AddMultiwordCommand,
854                        (const char *, const char *));
855   LLDB_REGISTER_METHOD(
856       lldb::SBCommand, SBCommand, AddCommand,
857       (const char *, lldb::SBCommandPluginInterface *, const char *));
858   LLDB_REGISTER_METHOD(lldb::SBCommand, SBCommand, AddCommand,
859                        (const char *, lldb::SBCommandPluginInterface *,
860                         const char *, const char *));
861   LLDB_REGISTER_METHOD(lldb::SBCommand, SBCommand, AddCommand,
862                        (const char *, lldb::SBCommandPluginInterface *,
863                         const char *, const char *, const char *));
864   LLDB_REGISTER_METHOD(uint32_t, SBCommand, GetFlags, ());
865   LLDB_REGISTER_METHOD(void, SBCommand, SetFlags, (uint32_t));
866 }
867 }
868 }
869