xref: /freebsd/contrib/llvm-project/lldb/include/lldb/Interpreter/CommandObject.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===-- CommandObject.h -----------------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
95ffd83dbSDimitry Andric #ifndef LLDB_INTERPRETER_COMMANDOBJECT_H
105ffd83dbSDimitry Andric #define LLDB_INTERPRETER_COMMANDOBJECT_H
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include <map>
13bdd1243dSDimitry Andric #include <memory>
14bdd1243dSDimitry Andric #include <optional>
150b57cec5SDimitry Andric #include <string>
160b57cec5SDimitry Andric #include <vector>
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric #include "lldb/Utility/Flags.h"
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric #include "lldb/Interpreter/CommandCompletions.h"
210b57cec5SDimitry Andric #include "lldb/Interpreter/Options.h"
220b57cec5SDimitry Andric #include "lldb/Target/ExecutionContext.h"
230b57cec5SDimitry Andric #include "lldb/Utility/Args.h"
240b57cec5SDimitry Andric #include "lldb/Utility/CompletionRequest.h"
250b57cec5SDimitry Andric #include "lldb/Utility/StringList.h"
260b57cec5SDimitry Andric #include "lldb/lldb-private.h"
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric namespace lldb_private {
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric // This function really deals with CommandObjectLists, but we didn't make a
310b57cec5SDimitry Andric // CommandObjectList class, so I'm sticking it here.  But we really should have
320b57cec5SDimitry Andric // such a class.  Anyway, it looks up the commands in the map that match the
330b57cec5SDimitry Andric // partial string cmd_str, inserts the matches into matches, and returns the
340b57cec5SDimitry Andric // number added.
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric template <typename ValueType>
370b57cec5SDimitry Andric int AddNamesMatchingPartialString(
380b57cec5SDimitry Andric     const std::map<std::string, ValueType> &in_map, llvm::StringRef cmd_str,
390b57cec5SDimitry Andric     StringList &matches, StringList *descriptions = nullptr) {
400b57cec5SDimitry Andric   int number_added = 0;
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric   const bool add_all = cmd_str.empty();
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric   for (auto iter = in_map.begin(), end = in_map.end(); iter != end; iter++) {
455ffd83dbSDimitry Andric     if (add_all || (iter->first.find(std::string(cmd_str), 0) == 0)) {
460b57cec5SDimitry Andric       ++number_added;
470b57cec5SDimitry Andric       matches.AppendString(iter->first.c_str());
480b57cec5SDimitry Andric       if (descriptions)
490b57cec5SDimitry Andric         descriptions->AppendString(iter->second->GetHelp());
500b57cec5SDimitry Andric     }
510b57cec5SDimitry Andric   }
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   return number_added;
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric template <typename ValueType>
FindLongestCommandWord(std::map<std::string,ValueType> & dict)570b57cec5SDimitry Andric size_t FindLongestCommandWord(std::map<std::string, ValueType> &dict) {
580b57cec5SDimitry Andric   auto end = dict.end();
590b57cec5SDimitry Andric   size_t max_len = 0;
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric   for (auto pos = dict.begin(); pos != end; ++pos) {
620b57cec5SDimitry Andric     size_t len = pos->first.size();
630b57cec5SDimitry Andric     if (max_len < len)
640b57cec5SDimitry Andric       max_len = len;
650b57cec5SDimitry Andric   }
660b57cec5SDimitry Andric   return max_len;
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric 
69bdd1243dSDimitry Andric class CommandObject : public std::enable_shared_from_this<CommandObject> {
700b57cec5SDimitry Andric public:
710b57cec5SDimitry Andric   typedef llvm::StringRef(ArgumentHelpCallbackFunction)();
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric   struct ArgumentHelpCallback {
740b57cec5SDimitry Andric     ArgumentHelpCallbackFunction *help_callback;
750b57cec5SDimitry Andric     bool self_formatting;
760b57cec5SDimitry Andric 
operatorArgumentHelpCallback770b57cec5SDimitry Andric     llvm::StringRef operator()() const { return (*help_callback)(); }
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric     explicit operator bool() const { return (help_callback != nullptr); }
800b57cec5SDimitry Andric   };
810b57cec5SDimitry Andric 
82fcaf7f86SDimitry Andric   /// Entries in the main argument information table.
83fcaf7f86SDimitry Andric   struct ArgumentTableEntry {
840b57cec5SDimitry Andric     lldb::CommandArgumentType arg_type;
850b57cec5SDimitry Andric     const char *arg_name;
8606c3fb27SDimitry Andric     lldb::CompletionType completion_type;
87fcaf7f86SDimitry Andric     OptionEnumValues enum_values;
880b57cec5SDimitry Andric     ArgumentHelpCallback help_function;
890b57cec5SDimitry Andric     const char *help_text;
900b57cec5SDimitry Andric   };
910b57cec5SDimitry Andric 
92fcaf7f86SDimitry Andric   /// Used to build individual command argument lists.
93fcaf7f86SDimitry Andric   struct CommandArgumentData {
940b57cec5SDimitry Andric     lldb::CommandArgumentType arg_type;
950b57cec5SDimitry Andric     ArgumentRepetitionType arg_repetition;
96e8d8bef9SDimitry Andric     /// This arg might be associated only with some particular option set(s). By
97e8d8bef9SDimitry Andric     /// default the arg associates to all option sets.
98e8d8bef9SDimitry Andric     uint32_t arg_opt_set_association;
99e8d8bef9SDimitry Andric 
100e8d8bef9SDimitry Andric     CommandArgumentData(lldb::CommandArgumentType type = lldb::eArgTypeNone,
101e8d8bef9SDimitry Andric                         ArgumentRepetitionType repetition = eArgRepeatPlain,
102e8d8bef9SDimitry Andric                         uint32_t opt_set = LLDB_OPT_SET_ALL)
arg_typeCommandArgumentData103e8d8bef9SDimitry Andric         : arg_type(type), arg_repetition(repetition),
104e8d8bef9SDimitry Andric           arg_opt_set_association(opt_set) {}
1050b57cec5SDimitry Andric   };
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric   typedef std::vector<CommandArgumentData>
1080b57cec5SDimitry Andric       CommandArgumentEntry; // Used to build individual command argument lists
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric   typedef std::map<std::string, lldb::CommandObjectSP> CommandMap;
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric   CommandObject(CommandInterpreter &interpreter, llvm::StringRef name,
1130b57cec5SDimitry Andric     llvm::StringRef help = "", llvm::StringRef syntax = "",
1140b57cec5SDimitry Andric                 uint32_t flags = 0);
1150b57cec5SDimitry Andric 
116fe6060f1SDimitry Andric   virtual ~CommandObject() = default;
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric   static const char *
1190b57cec5SDimitry Andric   GetArgumentTypeAsCString(const lldb::CommandArgumentType arg_type);
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric   static const char *
1220b57cec5SDimitry Andric   GetArgumentDescriptionAsCString(const lldb::CommandArgumentType arg_type);
1230b57cec5SDimitry Andric 
GetCommandInterpreter()1240b57cec5SDimitry Andric   CommandInterpreter &GetCommandInterpreter() { return m_interpreter; }
1250b57cec5SDimitry Andric   Debugger &GetDebugger();
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric   virtual llvm::StringRef GetHelp();
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric   virtual llvm::StringRef GetHelpLong();
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric   virtual llvm::StringRef GetSyntax();
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric   llvm::StringRef GetCommandName() const;
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric   virtual void SetHelp(llvm::StringRef str);
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric   virtual void SetHelpLong(llvm::StringRef str);
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric   void SetSyntax(llvm::StringRef str);
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   // override this to return true if you want to enable the user to delete the
1420b57cec5SDimitry Andric   // Command object from the Command dictionary (aliases have their own
1430b57cec5SDimitry Andric   // deletion scheme, so they do not need to care about this)
IsRemovable()1440b57cec5SDimitry Andric   virtual bool IsRemovable() const { return false; }
1450b57cec5SDimitry Andric 
IsMultiwordObject()1460b57cec5SDimitry Andric   virtual bool IsMultiwordObject() { return false; }
1470b57cec5SDimitry Andric 
IsUserCommand()148349cc55cSDimitry Andric   bool IsUserCommand() { return m_is_user_command; }
149349cc55cSDimitry Andric 
SetIsUserCommand(bool is_user)150349cc55cSDimitry Andric   void SetIsUserCommand(bool is_user) { m_is_user_command = is_user; }
151349cc55cSDimitry Andric 
GetAsMultiwordCommand()1520b57cec5SDimitry Andric   virtual CommandObjectMultiword *GetAsMultiwordCommand() { return nullptr; }
1530b57cec5SDimitry Andric 
IsAlias()1540b57cec5SDimitry Andric   virtual bool IsAlias() { return false; }
1550b57cec5SDimitry Andric 
1560b57cec5SDimitry Andric   // override this to return true if your command is somehow a "dash-dash" form
1570b57cec5SDimitry Andric   // of some other command (e.g. po is expr -O --); this is a powerful hint to
1580b57cec5SDimitry Andric   // the help system that one cannot pass options to this command
IsDashDashCommand()1590b57cec5SDimitry Andric   virtual bool IsDashDashCommand() { return false; }
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric   virtual lldb::CommandObjectSP GetSubcommandSP(llvm::StringRef sub_cmd,
1620b57cec5SDimitry Andric                                                 StringList *matches = nullptr) {
1630b57cec5SDimitry Andric     return lldb::CommandObjectSP();
1640b57cec5SDimitry Andric   }
1650b57cec5SDimitry Andric 
GetSubcommandSPExact(llvm::StringRef sub_cmd)166349cc55cSDimitry Andric   virtual lldb::CommandObjectSP GetSubcommandSPExact(llvm::StringRef sub_cmd) {
167349cc55cSDimitry Andric     return lldb::CommandObjectSP();
168349cc55cSDimitry Andric   }
169349cc55cSDimitry Andric 
1700b57cec5SDimitry Andric   virtual CommandObject *GetSubcommandObject(llvm::StringRef sub_cmd,
1710b57cec5SDimitry Andric                                              StringList *matches = nullptr) {
1720b57cec5SDimitry Andric     return nullptr;
1730b57cec5SDimitry Andric   }
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric   void FormatLongHelpText(Stream &output_strm, llvm::StringRef long_help);
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric   void GenerateHelpText(CommandReturnObject &result);
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric   virtual void GenerateHelpText(Stream &result);
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric   // this is needed in order to allow the SBCommand class to transparently try
1820b57cec5SDimitry Andric   // and load subcommands - it will fail on anything but a multiword command,
1830b57cec5SDimitry Andric   // but it avoids us doing type checkings and casts
LoadSubCommand(llvm::StringRef cmd_name,const lldb::CommandObjectSP & command_obj)1840b57cec5SDimitry Andric   virtual bool LoadSubCommand(llvm::StringRef cmd_name,
1850b57cec5SDimitry Andric                               const lldb::CommandObjectSP &command_obj) {
1860b57cec5SDimitry Andric     return false;
1870b57cec5SDimitry Andric   }
1880b57cec5SDimitry Andric 
LoadUserSubcommand(llvm::StringRef cmd_name,const lldb::CommandObjectSP & command_obj,bool can_replace)189349cc55cSDimitry Andric   virtual llvm::Error LoadUserSubcommand(llvm::StringRef cmd_name,
190349cc55cSDimitry Andric                                          const lldb::CommandObjectSP &command_obj,
191349cc55cSDimitry Andric                                          bool can_replace) {
192349cc55cSDimitry Andric     return llvm::createStringError(llvm::inconvertibleErrorCode(),
193349cc55cSDimitry Andric                               "can only add commands to container commands");
194349cc55cSDimitry Andric   }
195349cc55cSDimitry Andric 
1960b57cec5SDimitry Andric   virtual bool WantsRawCommandString() = 0;
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric   // By default, WantsCompletion = !WantsRawCommandString. Subclasses who want
1990b57cec5SDimitry Andric   // raw command string but desire, for example, argument completion should
2000b57cec5SDimitry Andric   // override this method to return true.
WantsCompletion()2010b57cec5SDimitry Andric   virtual bool WantsCompletion() { return !WantsRawCommandString(); }
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric   virtual Options *GetOptions();
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric   static lldb::CommandArgumentType LookupArgumentName(llvm::StringRef arg_name);
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric   static const ArgumentTableEntry *
2080b57cec5SDimitry Andric   FindArgumentDataByType(lldb::CommandArgumentType arg_type);
2090b57cec5SDimitry Andric 
210*0fca6ea1SDimitry Andric   // Sets the argument list for this command to one homogenous argument type,
211*0fca6ea1SDimitry Andric   // with the repeat specified.
212*0fca6ea1SDimitry Andric   void AddSimpleArgumentList(
213*0fca6ea1SDimitry Andric       lldb::CommandArgumentType arg_type,
214*0fca6ea1SDimitry Andric       ArgumentRepetitionType repetition_type = eArgRepeatPlain);
215*0fca6ea1SDimitry Andric 
216*0fca6ea1SDimitry Andric   // Helper function to set BP IDs or ID ranges as the command argument data
217*0fca6ea1SDimitry Andric   // for this command.
218*0fca6ea1SDimitry Andric   // This used to just populate an entry you could add to, but that was never
219*0fca6ea1SDimitry Andric   // used.  If we ever need that we can take optional extra args here.
220*0fca6ea1SDimitry Andric   // Use this to define a simple argument list:
221*0fca6ea1SDimitry Andric   enum IDType { eBreakpointArgs = 0, eWatchpointArgs = 1 };
222*0fca6ea1SDimitry Andric   void AddIDsArgumentData(IDType type);
223*0fca6ea1SDimitry Andric 
2240b57cec5SDimitry Andric   int GetNumArgumentEntries();
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric   CommandArgumentEntry *GetArgumentEntryAtIndex(int idx);
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric   static void GetArgumentHelp(Stream &str, lldb::CommandArgumentType arg_type,
2290b57cec5SDimitry Andric                               CommandInterpreter &interpreter);
2300b57cec5SDimitry Andric 
2310b57cec5SDimitry Andric   static const char *GetArgumentName(lldb::CommandArgumentType arg_type);
2320b57cec5SDimitry Andric 
2330b57cec5SDimitry Andric   // Generates a nicely formatted command args string for help command output.
2340b57cec5SDimitry Andric   // By default, all possible args are taken into account, for example, '<expr
2350b57cec5SDimitry Andric   // | variable-name>'.  This can be refined by passing a second arg specifying
2360b57cec5SDimitry Andric   // which option set(s) we are interested, which could then, for example,
2370b57cec5SDimitry Andric   // produce either '<expr>' or '<variable-name>'.
2380b57cec5SDimitry Andric   void GetFormattedCommandArguments(Stream &str,
2390b57cec5SDimitry Andric                                     uint32_t opt_set_mask = LLDB_OPT_SET_ALL);
2400b57cec5SDimitry Andric 
241*0fca6ea1SDimitry Andric   static bool IsPairType(ArgumentRepetitionType arg_repeat_type);
242*0fca6ea1SDimitry Andric 
243*0fca6ea1SDimitry Andric   static std::optional<ArgumentRepetitionType>
244*0fca6ea1SDimitry Andric     ArgRepetitionFromString(llvm::StringRef string);
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric   bool ParseOptions(Args &args, CommandReturnObject &result);
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric   void SetCommandName(llvm::StringRef name);
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric   /// This default version handles calling option argument completions and then
2510b57cec5SDimitry Andric   /// calls HandleArgumentCompletion if the cursor is on an argument, not an
2520b57cec5SDimitry Andric   /// option. Don't override this method, override HandleArgumentCompletion
2530b57cec5SDimitry Andric   /// instead unless you have special reasons.
2540b57cec5SDimitry Andric   ///
255480093f4SDimitry Andric   /// \param[in,out] request
2560b57cec5SDimitry Andric   ///    The completion request that needs to be answered.
2579dba64beSDimitry Andric   virtual void HandleCompletion(CompletionRequest &request);
2580b57cec5SDimitry Andric 
259*0fca6ea1SDimitry Andric   /// The default version handles argument definitions that have only one
260*0fca6ea1SDimitry Andric   /// argument type, and use one of the argument types that have an entry in
261*0fca6ea1SDimitry Andric   /// the CommonCompletions.  Override this if you have a more complex
262*0fca6ea1SDimitry Andric   /// argument setup.
263*0fca6ea1SDimitry Andric   /// FIXME: we should be able to extend this to more complex argument
264*0fca6ea1SDimitry Andric   /// definitions provided we have completers for all the argument types.
265*0fca6ea1SDimitry Andric   ///
2669dba64beSDimitry Andric   /// The input array contains a parsed version of the line.
2679dba64beSDimitry Andric   ///
2680b57cec5SDimitry Andric   /// We've constructed the map of options and their arguments as well if that
2690b57cec5SDimitry Andric   /// is helpful for the completion.
2700b57cec5SDimitry Andric   ///
271480093f4SDimitry Andric   /// \param[in,out] request
2720b57cec5SDimitry Andric   ///    The completion request that needs to be answered.
2739dba64beSDimitry Andric   virtual void
2740b57cec5SDimitry Andric   HandleArgumentCompletion(CompletionRequest &request,
275*0fca6ea1SDimitry Andric                            OptionElementVector &opt_element_vector);
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric   bool HelpTextContainsWord(llvm::StringRef search_word,
2780b57cec5SDimitry Andric                             bool search_short_help = true,
2790b57cec5SDimitry Andric                             bool search_long_help = true,
2800b57cec5SDimitry Andric                             bool search_syntax = true,
2810b57cec5SDimitry Andric                             bool search_options = true);
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric   /// The flags accessor.
2840b57cec5SDimitry Andric   ///
2850b57cec5SDimitry Andric   /// \return
2860b57cec5SDimitry Andric   ///     A reference to the Flags member variable.
GetFlags()2870b57cec5SDimitry Andric   Flags &GetFlags() { return m_flags; }
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric   /// The flags const accessor.
2900b57cec5SDimitry Andric   ///
2910b57cec5SDimitry Andric   /// \return
2920b57cec5SDimitry Andric   ///     A const reference to the Flags member variable.
GetFlags()2930b57cec5SDimitry Andric   const Flags &GetFlags() const { return m_flags; }
2940b57cec5SDimitry Andric 
2950b57cec5SDimitry Andric   /// Get the command that appropriate for a "repeat" of the current command.
2960b57cec5SDimitry Andric   ///
297480093f4SDimitry Andric   /// \param[in] current_command_args
298480093f4SDimitry Andric   ///    The command arguments.
2990b57cec5SDimitry Andric   ///
300*0fca6ea1SDimitry Andric   /// \param[in] index
301*0fca6ea1SDimitry Andric   ///    This is for internal use - it is how the completion request is tracked
302*0fca6ea1SDimitry Andric   ///    in CommandObjectMultiword, and should otherwise be ignored.
303*0fca6ea1SDimitry Andric   ///
3040b57cec5SDimitry Andric   /// \return
305bdd1243dSDimitry Andric   ///     std::nullopt if there is no special repeat command - it will use the
3060b57cec5SDimitry Andric   ///     current command line.
30781ad6265SDimitry Andric   ///     Otherwise a std::string containing the command to be repeated.
30881ad6265SDimitry Andric   ///     If the string is empty, the command won't be allow repeating.
309bdd1243dSDimitry Andric   virtual std::optional<std::string>
GetRepeatCommand(Args & current_command_args,uint32_t index)31081ad6265SDimitry Andric   GetRepeatCommand(Args &current_command_args, uint32_t index) {
311bdd1243dSDimitry Andric     return std::nullopt;
3120b57cec5SDimitry Andric   }
3130b57cec5SDimitry Andric 
HasOverrideCallback()3140b57cec5SDimitry Andric   bool HasOverrideCallback() const {
3150b57cec5SDimitry Andric     return m_command_override_callback ||
3160b57cec5SDimitry Andric            m_deprecated_command_override_callback;
3170b57cec5SDimitry Andric   }
3180b57cec5SDimitry Andric 
SetOverrideCallback(lldb::CommandOverrideCallback callback,void * baton)3190b57cec5SDimitry Andric   void SetOverrideCallback(lldb::CommandOverrideCallback callback,
3200b57cec5SDimitry Andric                            void *baton) {
3210b57cec5SDimitry Andric     m_deprecated_command_override_callback = callback;
3220b57cec5SDimitry Andric     m_command_override_baton = baton;
3230b57cec5SDimitry Andric   }
3240b57cec5SDimitry Andric 
32506c3fb27SDimitry Andric   void
SetOverrideCallback(lldb_private::CommandOverrideCallbackWithResult callback,void * baton)32606c3fb27SDimitry Andric   SetOverrideCallback(lldb_private::CommandOverrideCallbackWithResult callback,
3270b57cec5SDimitry Andric                       void *baton) {
3280b57cec5SDimitry Andric     m_command_override_callback = callback;
3290b57cec5SDimitry Andric     m_command_override_baton = baton;
3300b57cec5SDimitry Andric   }
3310b57cec5SDimitry Andric 
InvokeOverrideCallback(const char ** argv,CommandReturnObject & result)3320b57cec5SDimitry Andric   bool InvokeOverrideCallback(const char **argv, CommandReturnObject &result) {
3330b57cec5SDimitry Andric     if (m_command_override_callback)
3340b57cec5SDimitry Andric       return m_command_override_callback(m_command_override_baton, argv,
3350b57cec5SDimitry Andric                                          result);
3360b57cec5SDimitry Andric     else if (m_deprecated_command_override_callback)
3370b57cec5SDimitry Andric       return m_deprecated_command_override_callback(m_command_override_baton,
3380b57cec5SDimitry Andric                                                     argv);
3390b57cec5SDimitry Andric     else
3400b57cec5SDimitry Andric       return false;
3410b57cec5SDimitry Andric   }
3420b57cec5SDimitry Andric 
3435f757f3fSDimitry Andric   virtual void Execute(const char *args_string,
3440b57cec5SDimitry Andric                        CommandReturnObject &result) = 0;
3450b57cec5SDimitry Andric 
3460b57cec5SDimitry Andric protected:
3470b57cec5SDimitry Andric   bool ParseOptionsAndNotify(Args &args, CommandReturnObject &result,
3480b57cec5SDimitry Andric                              OptionGroupOptions &group_options,
3490b57cec5SDimitry Andric                              ExecutionContext &exe_ctx);
3500b57cec5SDimitry Andric 
GetInvalidTargetDescription()3510b57cec5SDimitry Andric   virtual const char *GetInvalidTargetDescription() {
3520b57cec5SDimitry Andric     return "invalid target, create a target using the 'target create' command";
3530b57cec5SDimitry Andric   }
3540b57cec5SDimitry Andric 
GetInvalidProcessDescription()3550b57cec5SDimitry Andric   virtual const char *GetInvalidProcessDescription() {
35681ad6265SDimitry Andric     return "Command requires a current process.";
3570b57cec5SDimitry Andric   }
3580b57cec5SDimitry Andric 
GetInvalidThreadDescription()35981ad6265SDimitry Andric   virtual const char *GetInvalidThreadDescription() {
36081ad6265SDimitry Andric     return "Command requires a process which is currently stopped.";
36181ad6265SDimitry Andric   }
3620b57cec5SDimitry Andric 
GetInvalidFrameDescription()36381ad6265SDimitry Andric   virtual const char *GetInvalidFrameDescription() {
36481ad6265SDimitry Andric     return "Command requires a process, which is currently stopped.";
36581ad6265SDimitry Andric   }
3660b57cec5SDimitry Andric 
GetInvalidRegContextDescription()3670b57cec5SDimitry Andric   virtual const char *GetInvalidRegContextDescription() {
36881ad6265SDimitry Andric     return "invalid frame, no registers, command requires a process which is "
36981ad6265SDimitry Andric            "currently stopped.";
3700b57cec5SDimitry Andric   }
3710b57cec5SDimitry Andric 
3720b57cec5SDimitry Andric   // This is for use in the command interpreter, when you either want the
3730b57cec5SDimitry Andric   // selected target, or if no target is present you want to prime the dummy
3740b57cec5SDimitry Andric   // target with entities that will be copied over to new targets.
3759dba64beSDimitry Andric   Target &GetSelectedOrDummyTarget(bool prefer_dummy = false);
3769dba64beSDimitry Andric   Target &GetSelectedTarget();
3779dba64beSDimitry Andric   Target &GetDummyTarget();
3780b57cec5SDimitry Andric 
3790b57cec5SDimitry Andric   // If a command needs to use the "current" thread, use this call. Command
3800b57cec5SDimitry Andric   // objects will have an ExecutionContext to use, and that may or may not have
3810b57cec5SDimitry Andric   // a thread in it.  If it does, you should use that by default, if not, then
3820b57cec5SDimitry Andric   // use the ExecutionContext's target's selected thread, etc... This call
3830b57cec5SDimitry Andric   // insulates you from the details of this calculation.
3840b57cec5SDimitry Andric   Thread *GetDefaultThread();
3850b57cec5SDimitry Andric 
3860b57cec5SDimitry Andric   /// Check the command to make sure anything required by this
3870b57cec5SDimitry Andric   /// command is available.
3880b57cec5SDimitry Andric   ///
3890b57cec5SDimitry Andric   /// \param[out] result
3900b57cec5SDimitry Andric   ///     A command result object, if it is not okay to run the command
3910b57cec5SDimitry Andric   ///     this will be filled in with a suitable error.
3920b57cec5SDimitry Andric   ///
3930b57cec5SDimitry Andric   /// \return
3940b57cec5SDimitry Andric   ///     \b true if it is okay to run this command, \b false otherwise.
3950b57cec5SDimitry Andric   bool CheckRequirements(CommandReturnObject &result);
3960b57cec5SDimitry Andric 
3970b57cec5SDimitry Andric   void Cleanup();
3980b57cec5SDimitry Andric 
3990b57cec5SDimitry Andric   CommandInterpreter &m_interpreter;
4000b57cec5SDimitry Andric   ExecutionContext m_exe_ctx;
4010b57cec5SDimitry Andric   std::unique_lock<std::recursive_mutex> m_api_locker;
4020b57cec5SDimitry Andric   std::string m_cmd_name;
4030b57cec5SDimitry Andric   std::string m_cmd_help_short;
4040b57cec5SDimitry Andric   std::string m_cmd_help_long;
4050b57cec5SDimitry Andric   std::string m_cmd_syntax;
4060b57cec5SDimitry Andric   Flags m_flags;
4070b57cec5SDimitry Andric   std::vector<CommandArgumentEntry> m_arguments;
4080b57cec5SDimitry Andric   lldb::CommandOverrideCallback m_deprecated_command_override_callback;
40906c3fb27SDimitry Andric   lldb_private::CommandOverrideCallbackWithResult m_command_override_callback;
4100b57cec5SDimitry Andric   void *m_command_override_baton;
411349cc55cSDimitry Andric   bool m_is_user_command = false;
4120b57cec5SDimitry Andric };
4130b57cec5SDimitry Andric 
4140b57cec5SDimitry Andric class CommandObjectParsed : public CommandObject {
4150b57cec5SDimitry Andric public:
4160b57cec5SDimitry Andric   CommandObjectParsed(CommandInterpreter &interpreter, const char *name,
4170b57cec5SDimitry Andric                       const char *help = nullptr, const char *syntax = nullptr,
4180b57cec5SDimitry Andric                       uint32_t flags = 0)
CommandObject(interpreter,name,help,syntax,flags)4190b57cec5SDimitry Andric       : CommandObject(interpreter, name, help, syntax, flags) {}
4200b57cec5SDimitry Andric 
4210b57cec5SDimitry Andric   ~CommandObjectParsed() override = default;
4220b57cec5SDimitry Andric 
4235f757f3fSDimitry Andric   void Execute(const char *args_string, CommandReturnObject &result) override;
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric protected:
4265f757f3fSDimitry Andric   virtual void DoExecute(Args &command, CommandReturnObject &result) = 0;
4270b57cec5SDimitry Andric 
WantsRawCommandString()4280b57cec5SDimitry Andric   bool WantsRawCommandString() override { return false; }
4290b57cec5SDimitry Andric };
4300b57cec5SDimitry Andric 
4310b57cec5SDimitry Andric class CommandObjectRaw : public CommandObject {
4320b57cec5SDimitry Andric public:
4330b57cec5SDimitry Andric   CommandObjectRaw(CommandInterpreter &interpreter, llvm::StringRef name,
4340b57cec5SDimitry Andric     llvm::StringRef help = "", llvm::StringRef syntax = "",
4350b57cec5SDimitry Andric                    uint32_t flags = 0)
CommandObject(interpreter,name,help,syntax,flags)4360b57cec5SDimitry Andric       : CommandObject(interpreter, name, help, syntax, flags) {}
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric   ~CommandObjectRaw() override = default;
4390b57cec5SDimitry Andric 
4405f757f3fSDimitry Andric   void Execute(const char *args_string, CommandReturnObject &result) override;
4410b57cec5SDimitry Andric 
4420b57cec5SDimitry Andric protected:
4435f757f3fSDimitry Andric   virtual void DoExecute(llvm::StringRef command,
4440b57cec5SDimitry Andric                          CommandReturnObject &result) = 0;
4450b57cec5SDimitry Andric 
WantsRawCommandString()4460b57cec5SDimitry Andric   bool WantsRawCommandString() override { return true; }
4470b57cec5SDimitry Andric };
4480b57cec5SDimitry Andric 
4490b57cec5SDimitry Andric } // namespace lldb_private
4500b57cec5SDimitry Andric 
4515ffd83dbSDimitry Andric #endif // LLDB_INTERPRETER_COMMANDOBJECT_H
452