Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:41

0001 //===-- SBCommandInterpreter.h ----------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_API_SBCOMMANDINTERPRETER_H
0010 #define LLDB_API_SBCOMMANDINTERPRETER_H
0011 
0012 #include <memory>
0013 
0014 #include "lldb/API/SBDebugger.h"
0015 #include "lldb/API/SBDefines.h"
0016 #include "lldb/API/SBStructuredData.h"
0017 
0018 namespace lldb_private {
0019 class CommandPluginInterfaceImplementation;
0020 }
0021 
0022 namespace lldb {
0023 
0024 class SBCommandInterpreter {
0025 public:
0026   enum {
0027     eBroadcastBitThreadShouldExit = (1 << 0),
0028     eBroadcastBitResetPrompt = (1 << 1),
0029     eBroadcastBitQuitCommandReceived = (1 << 2), // User entered quit
0030     eBroadcastBitAsynchronousOutputData = (1 << 3),
0031     eBroadcastBitAsynchronousErrorData = (1 << 4)
0032   };
0033 
0034   SBCommandInterpreter();
0035   SBCommandInterpreter(const lldb::SBCommandInterpreter &rhs);
0036 
0037   ~SBCommandInterpreter();
0038 
0039   const lldb::SBCommandInterpreter &
0040   operator=(const lldb::SBCommandInterpreter &rhs);
0041 
0042   static const char *
0043   GetArgumentTypeAsCString(const lldb::CommandArgumentType arg_type);
0044 
0045   static const char *
0046   GetArgumentDescriptionAsCString(const lldb::CommandArgumentType arg_type);
0047 
0048   static bool EventIsCommandInterpreterEvent(const lldb::SBEvent &event);
0049 
0050   explicit operator bool() const;
0051 
0052   bool IsValid() const;
0053 
0054   /// Return whether a built-in command with the passed in
0055   /// name or command path exists.
0056   ///
0057   /// \param[in] cmd
0058   ///   The command or command path to search for.
0059   ///
0060   /// \return
0061   ///   \b true if the command exists, \b false otherwise.
0062   bool CommandExists(const char *cmd);
0063 
0064   /// Return whether a user defined command with the passed in
0065   /// name or command path exists.
0066   ///
0067   /// \param[in] cmd
0068   ///   The command or command path to search for.
0069   ///
0070   /// \return
0071   ///   \b true if the command exists, \b false otherwise.
0072   bool UserCommandExists(const char *cmd);
0073 
0074   /// Return whether the passed in name or command path
0075   /// exists and is an alias to some other command.
0076   ///
0077   /// \param[in] cmd
0078   ///   The command or command path to search for.
0079   ///
0080   /// \return
0081   ///   \b true if the command exists, \b false otherwise.
0082   bool AliasExists(const char *cmd);
0083 
0084   lldb::SBBroadcaster GetBroadcaster();
0085 
0086   static const char *GetBroadcasterClass();
0087 
0088   bool HasCommands();
0089 
0090   bool HasAliases();
0091 
0092   bool HasAliasOptions();
0093 
0094   bool IsInteractive();
0095 
0096   lldb::SBProcess GetProcess();
0097 
0098   lldb::SBDebugger GetDebugger();
0099 
0100 #ifndef SWIG
0101   lldb::SBCommand AddMultiwordCommand(const char *name, const char *help);
0102 
0103   /// Add a new command to the lldb::CommandInterpreter.
0104   ///
0105   /// The new command won't support autorepeat. If you need this functionality,
0106   /// use the override of this function that accepts the \a auto_repeat_command
0107   /// parameter.
0108   ///
0109   /// \param[in] name
0110   ///     The name of the command.
0111   ///
0112   /// \param[in] impl
0113   ///     The handler of this command.
0114   ///
0115   /// \param[in] help
0116   ///     The general description to show as part of the help message of this
0117   ///     command.
0118   ///
0119   /// \return
0120   ///     A lldb::SBCommand representing the newly created command.
0121   lldb::SBCommand AddCommand(const char *name,
0122                              lldb::SBCommandPluginInterface *impl,
0123                              const char *help);
0124 
0125   /// Add a new command to the lldb::CommandInterpreter.
0126   ///
0127   /// The new command won't support autorepeat. If you need this functionality,
0128   /// use the override of this function that accepts the \a auto_repeat_command
0129   /// parameter.
0130   ///
0131   /// \param[in] name
0132   ///     The name of the command.
0133   ///
0134   /// \param[in] impl
0135   ///     The handler of this command.
0136   ///
0137   /// \param[in] help
0138   ///     The general description to show as part of the help message of this
0139   ///     command.
0140   ///
0141   /// \param[in] syntax
0142   ///     The syntax to show as part of the help message of this command. This
0143   ///     could include a description of the different arguments and flags this
0144   ///     command accepts.
0145   ///
0146   /// \return
0147   ///     A lldb::SBCommand representing the newly created command.
0148   lldb::SBCommand AddCommand(const char *name,
0149                              lldb::SBCommandPluginInterface *impl,
0150                              const char *help, const char *syntax);
0151 
0152   /// Add a new command to the lldb::CommandInterpreter.
0153   ///
0154   /// \param[in] name
0155   ///     The name of the command.
0156   ///
0157   /// \param[in] impl
0158   ///     The handler of this command.
0159   ///
0160   /// \param[in] help
0161   ///     The general description to show as part of the help message of this
0162   ///     command.
0163   ///
0164   /// \param[in] syntax
0165   ///     The syntax to show as part of the help message of this command. This
0166   ///     could include a description of the different arguments and flags this
0167   ///     command accepts.
0168   ///
0169   /// \param[in] auto_repeat_command
0170   ///     Autorepeating is triggered when the user presses Enter successively
0171   ///     after executing a command. If \b nullptr is provided, the previous
0172   ///     exact command will be repeated. If \b "" is provided, autorepeating
0173   ///     is disabled. Otherwise, the provided string is used as a repeat
0174   ///     command.
0175   ///
0176   /// \return
0177   ///     A lldb::SBCommand representing the newly created command.
0178   lldb::SBCommand AddCommand(const char *name,
0179                              lldb::SBCommandPluginInterface *impl,
0180                              const char *help, const char *syntax,
0181                              const char *auto_repeat_command);
0182   void SourceInitFileInGlobalDirectory(lldb::SBCommandReturnObject &result);
0183 #endif
0184 
0185 
0186   void SourceInitFileInHomeDirectory(lldb::SBCommandReturnObject &result);
0187   void SourceInitFileInHomeDirectory(lldb::SBCommandReturnObject &result,
0188                                      bool is_repl);
0189 
0190   void
0191   SourceInitFileInCurrentWorkingDirectory(lldb::SBCommandReturnObject &result);
0192 
0193   lldb::ReturnStatus HandleCommand(const char *command_line,
0194                                    lldb::SBCommandReturnObject &result,
0195                                    bool add_to_history = false);
0196 
0197   lldb::ReturnStatus HandleCommand(const char *command_line,
0198                                    SBExecutionContext &exe_ctx,
0199                                    SBCommandReturnObject &result,
0200                                    bool add_to_history = false);
0201 
0202   void HandleCommandsFromFile(lldb::SBFileSpec &file,
0203                               lldb::SBExecutionContext &override_context,
0204                               lldb::SBCommandInterpreterRunOptions &options,
0205                               lldb::SBCommandReturnObject result);
0206 
0207   // The pointer based interface is not useful in SWIG, since the cursor &
0208   // last_char arguments are string pointers INTO current_line and you can't do
0209   // that in a scripting language interface in general...
0210 
0211   // In either case, the way this works is that the you give it a line and
0212   // cursor position in the line.  The function will return the number of
0213   // completions.  The matches list will contain number_of_completions + 1
0214   // elements.  The first element is the common substring after the cursor
0215   // position for all the matches.  The rest of the elements are the matches.
0216   // The first element is useful if you are emulating the common shell behavior
0217   // where the tab completes to the string that is common among all the
0218   // matches, then you should first check if the first element is non-empty,
0219   // and if so just insert it and move the cursor to the end of the insertion.
0220   // The next tab will return an empty common substring, and a list of choices
0221   // (if any), at which point you should display the choices and let the user
0222   // type further to disambiguate.
0223 
0224 #ifndef SWIG
0225   int HandleCompletion(const char *current_line, const char *cursor,
0226                        const char *last_char, int match_start_point,
0227                        int max_return_elements, lldb::SBStringList &matches);
0228 #endif
0229 
0230   int HandleCompletion(const char *current_line, uint32_t cursor_pos,
0231                        int match_start_point, int max_return_elements,
0232                        lldb::SBStringList &matches);
0233 
0234   // Same as HandleCompletion, but also fills out `descriptions` with
0235   // descriptions for each match.
0236 #ifndef SWIG
0237   int HandleCompletionWithDescriptions(
0238       const char *current_line, const char *cursor, const char *last_char,
0239       int match_start_point, int max_return_elements,
0240       lldb::SBStringList &matches, lldb::SBStringList &descriptions);
0241 #endif
0242 
0243   int HandleCompletionWithDescriptions(const char *current_line,
0244                                        uint32_t cursor_pos,
0245                                        int match_start_point,
0246                                        int max_return_elements,
0247                                        lldb::SBStringList &matches,
0248                                        lldb::SBStringList &descriptions);
0249 
0250   /// Returns whether an interrupt flag was raised either by the SBDebugger - 
0251   /// when the function is not running on the RunCommandInterpreter thread, or
0252   /// by SBCommandInterpreter::InterruptCommand if it is.  If your code is doing
0253   /// interruptible work, check this API periodically, and interrupt if it 
0254   /// returns true.
0255   bool WasInterrupted() const;
0256   
0257   /// Interrupts the command currently executing in the RunCommandInterpreter
0258   /// thread.
0259   ///
0260   /// \return
0261   ///   \b true if there was a command in progress to recieve the interrupt.
0262   ///   \b false if there's no command currently in flight.
0263   bool InterruptCommand();
0264 
0265   // Catch commands before they execute by registering a callback that will get
0266   // called when the command gets executed. This allows GUI or command line
0267   // interfaces to intercept a command and stop it from happening
0268   bool SetCommandOverrideCallback(const char *command_name,
0269                                   lldb::CommandOverrideCallback callback,
0270                                   void *baton);
0271 
0272   /// Return true if the command interpreter is the active IO handler.
0273   ///
0274   /// This indicates that any input coming into the debugger handles will
0275   /// go to the command interpreter and will result in LLDB command line
0276   /// commands being executed.
0277   bool IsActive();
0278 
0279   /// Get the string that needs to be written to the debugger stdin file
0280   /// handle when a control character is typed.
0281   ///
0282   /// Some GUI programs will intercept "control + char" sequences and want
0283   /// to have them do what normally would happen when using a real
0284   /// terminal, so this function allows GUI programs to emulate this
0285   /// functionality.
0286   ///
0287   /// \param[in] ch
0288   ///     The character that was typed along with the control key
0289   ///
0290   /// \return
0291   ///     The string that should be written into the file handle that is
0292   ///     feeding the input stream for the debugger, or nullptr if there is
0293   ///     no string for this control key.
0294   const char *GetIOHandlerControlSequence(char ch);
0295 
0296   bool GetPromptOnQuit();
0297 
0298   void SetPromptOnQuit(bool b);
0299 
0300   /// Sets whether the command interpreter should allow custom exit codes
0301   /// for the 'quit' command.
0302   void AllowExitCodeOnQuit(bool allow);
0303 
0304   /// Returns true if the user has called the 'quit' command with a custom exit
0305   /// code.
0306   bool HasCustomQuitExitCode();
0307 
0308   /// Returns the exit code that the user has specified when running the
0309   /// 'quit' command. Returns 0 if the user hasn't called 'quit' at all or
0310   /// without a custom exit code.
0311   int GetQuitStatus();
0312 
0313   /// Resolve the command just as HandleCommand would, expanding abbreviations
0314   /// and aliases.  If successful, result->GetOutput has the full expansion.
0315   void ResolveCommand(const char *command_line, SBCommandReturnObject &result);
0316 
0317   SBStructuredData GetStatistics();
0318 
0319   /// Returns a list of handled commands, output and error. Each element in
0320   /// the list is a dictionary with the following keys/values:
0321   /// - "command" (string): The command that was given by the user.
0322   /// - "commandName" (string): The name of the executed command.
0323   /// - "commandArguments" (string): The arguments of the executed command.
0324   /// - "output" (string): The output of the command. Empty ("") if no output.
0325   /// - "error" (string): The error of the command. Empty ("") if no error.
0326   /// - "durationInSeconds" (float): The time it took to execute the command.
0327   /// - "timestampInEpochSeconds" (int): The timestamp when the command is
0328   ///   executed.
0329   ///
0330   /// Turn on settings `interpreter.save-transcript` for LLDB to populate
0331   /// this list. Otherwise this list is empty.
0332   SBStructuredData GetTranscript();
0333 
0334 protected:
0335   friend class lldb_private::CommandPluginInterfaceImplementation;
0336 
0337   /// Access using SBDebugger::GetCommandInterpreter();
0338   SBCommandInterpreter(lldb_private::CommandInterpreter *interpreter_ptr);
0339   lldb_private::CommandInterpreter &ref();
0340 
0341   lldb_private::CommandInterpreter *get();
0342 
0343   void reset(lldb_private::CommandInterpreter *);
0344 
0345 private:
0346   friend class SBDebugger;
0347 
0348   lldb_private::CommandInterpreter *m_opaque_ptr;
0349 };
0350 
0351 #ifndef SWIG
0352 class SBCommandPluginInterface {
0353 public:
0354   virtual ~SBCommandPluginInterface() = default;
0355 
0356   virtual bool DoExecute(lldb::SBDebugger /*debugger*/, char ** /*command*/,
0357                          lldb::SBCommandReturnObject & /*result*/) {
0358     return false;
0359   }
0360 };
0361 
0362 class SBCommand {
0363 public:
0364   SBCommand();
0365 
0366   explicit operator bool() const;
0367 
0368   bool IsValid();
0369 
0370   const char *GetName();
0371 
0372   const char *GetHelp();
0373 
0374   const char *GetHelpLong();
0375 
0376   void SetHelp(const char *);
0377 
0378   void SetHelpLong(const char *);
0379 
0380   uint32_t GetFlags();
0381 
0382   void SetFlags(uint32_t flags);
0383 
0384   lldb::SBCommand AddMultiwordCommand(const char *name,
0385                                       const char *help = nullptr);
0386 
0387   /// Add a new subcommand to the lldb::SBCommand.
0388   ///
0389   /// The new command won't support autorepeat. If you need this functionality,
0390   /// use the override of this function that accepts the \a auto_repeat
0391   /// parameter.
0392   ///
0393   /// \param[in] name
0394   ///     The name of the command.
0395   ///
0396   /// \param[in] impl
0397   ///     The handler of this command.
0398   ///
0399   /// \param[in] help
0400   ///     The general description to show as part of the help message of this
0401   ///     command.
0402   ///
0403   /// \return
0404   ///     A lldb::SBCommand representing the newly created command.
0405   lldb::SBCommand AddCommand(const char *name,
0406                              lldb::SBCommandPluginInterface *impl,
0407                              const char *help = nullptr);
0408 
0409   /// Add a new subcommand to the lldb::SBCommand.
0410   ///
0411   /// The new command won't support autorepeat. If you need this functionality,
0412   /// use the override of this function that accepts the \a auto_repeat_command
0413   /// parameter.
0414   ///
0415   /// \param[in] name
0416   ///     The name of the command.
0417   ///
0418   /// \param[in] impl
0419   ///     The handler of this command.
0420   ///
0421   /// \param[in] help
0422   ///     The general description to show as part of the help message of this
0423   ///     command.
0424   ///
0425   /// \param[in] syntax
0426   ///     The syntax to show as part of the help message of this command. This
0427   ///     could include a description of the different arguments and flags this
0428   ///     command accepts.
0429   ///
0430   /// \return
0431   ///     A lldb::SBCommand representing the newly created command.
0432   lldb::SBCommand AddCommand(const char *name,
0433                              lldb::SBCommandPluginInterface *impl,
0434                              const char *help, const char *syntax);
0435 
0436   /// Add a new subcommand to the lldb::SBCommand.
0437   ///
0438   /// The new command won't support autorepeat. If you need this functionality,
0439   /// use the override of this function that accepts the \a auto_repeat_command
0440   /// parameter.
0441   ///
0442   /// \param[in] name
0443   ///     The name of the command.
0444   ///
0445   /// \param[in] impl
0446   ///     The handler of this command.
0447   ///
0448   /// \param[in] help
0449   ///     The general description to show as part of the help message of this
0450   ///     command.
0451   ///
0452   /// \param[in] syntax
0453   ///     The syntax to show as part of the help message of this command. This
0454   ///     could include a description of the different arguments and flags this
0455   ///     command accepts.
0456   ///
0457   /// \param[in] auto_repeat_command
0458   ///     Autorepeating is triggered when the user presses Enter successively
0459   ///     after executing a command. If \b nullptr is provided, the previous
0460   ///     exact command will be repeated. If \b "" is provided, autorepeating
0461   ///     is disabled. Otherwise, the provided string is used as a repeat
0462   ///     command.
0463   ///
0464   /// \return
0465   ///     A lldb::SBCommand representing the newly created command.
0466   lldb::SBCommand AddCommand(const char *name,
0467                              lldb::SBCommandPluginInterface *impl,
0468                              const char *help, const char *syntax,
0469                              const char *auto_repeat_command);
0470 
0471 private:
0472   friend class SBDebugger;
0473   friend class SBCommandInterpreter;
0474 
0475   SBCommand(lldb::CommandObjectSP cmd_sp);
0476 
0477   lldb::CommandObjectSP m_opaque_sp;
0478 };
0479 #endif
0480 
0481 } // namespace lldb
0482 
0483 #endif // LLDB_API_SBCOMMANDINTERPRETER_H