Home | History | Annotate | Download | only in Interpreter
      1 //===-- CommandInterpreter.h ------------------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #ifndef liblldb_CommandInterpreter_h_
     11 #define liblldb_CommandInterpreter_h_
     12 
     13 // C Includes
     14 // C++ Includes
     15 // Other libraries and framework includes
     16 // Project includes
     17 #include "lldb/lldb-private.h"
     18 #include "lldb/Core/Broadcaster.h"
     19 #include "lldb/Core/Debugger.h"
     20 #include "lldb/Core/Log.h"
     21 #include "lldb/Interpreter/CommandHistory.h"
     22 #include "lldb/Interpreter/CommandObject.h"
     23 #include "lldb/Interpreter/ScriptInterpreter.h"
     24 #include "lldb/Core/Event.h"
     25 #include "lldb/Interpreter/Args.h"
     26 #include "lldb/Core/StringList.h"
     27 
     28 namespace lldb_private {
     29 
     30 class CommandInterpreter :
     31     public Broadcaster,
     32     public Properties
     33 {
     34 public:
     35     typedef std::map<std::string, OptionArgVectorSP> OptionArgMap;
     36 
     37     enum
     38     {
     39         eBroadcastBitThreadShouldExit       = (1 << 0),
     40         eBroadcastBitResetPrompt            = (1 << 1),
     41         eBroadcastBitQuitCommandReceived    = (1 << 2),   // User entered quit
     42         eBroadcastBitAsynchronousOutputData = (1 << 3),
     43         eBroadcastBitAsynchronousErrorData  = (1 << 4)
     44     };
     45 
     46     enum ChildrenTruncatedWarningStatus // tristate boolean to manage children truncation warning
     47     {
     48         eNoTruncation = 0, // never truncated
     49         eUnwarnedTruncation = 1, // truncated but did not notify
     50         eWarnedTruncation = 2 // truncated and notified
     51     };
     52 
     53     enum CommandTypes
     54     {
     55         eCommandTypesBuiltin = 0x0001,  // native commands such as "frame"
     56         eCommandTypesUserDef = 0x0002,  // scripted commands
     57         eCommandTypesAliases = 0x0004,  // aliases such as "po"
     58         eCommandTypesAllThem = 0xFFFF   // all commands
     59     };
     60 
     61     // These two functions fill out the Broadcaster interface:
     62 
     63     static ConstString &GetStaticBroadcasterClass ();
     64 
     65     virtual ConstString &GetBroadcasterClass() const
     66     {
     67         return GetStaticBroadcasterClass();
     68     }
     69 
     70     void
     71     SourceInitFile (bool in_cwd,
     72                     CommandReturnObject &result);
     73 
     74     CommandInterpreter (Debugger &debugger,
     75                         lldb::ScriptLanguage script_language,
     76                         bool synchronous_execution);
     77 
     78     virtual
     79     ~CommandInterpreter ();
     80 
     81     bool
     82     AddCommand (const char *name,
     83                 const lldb::CommandObjectSP &cmd_sp,
     84                 bool can_replace);
     85 
     86     bool
     87     AddUserCommand (std::string name,
     88                     const lldb::CommandObjectSP &cmd_sp,
     89                     bool can_replace);
     90 
     91     lldb::CommandObjectSP
     92     GetCommandSPExact (const char *cmd,
     93                        bool include_aliases);
     94 
     95     CommandObject *
     96     GetCommandObjectExact (const char *cmd_cstr,
     97                            bool include_aliases);
     98 
     99     CommandObject *
    100     GetCommandObject (const char *cmd,
    101                       StringList *matches = NULL);
    102 
    103     bool
    104     CommandExists (const char *cmd);
    105 
    106     bool
    107     AliasExists (const char *cmd);
    108 
    109     bool
    110     UserCommandExists (const char *cmd);
    111 
    112     void
    113     AddAlias (const char *alias_name,
    114               lldb::CommandObjectSP& command_obj_sp);
    115 
    116     bool
    117     RemoveAlias (const char *alias_name);
    118 
    119     bool
    120     GetAliasFullName (const char *cmd, std::string &full_name);
    121 
    122     bool
    123     RemoveUser (const char *alias_name);
    124 
    125     void
    126     RemoveAllUser ()
    127     {
    128         m_user_dict.clear();
    129     }
    130 
    131     OptionArgVectorSP
    132     GetAliasOptions (const char *alias_name);
    133 
    134 
    135     bool
    136     ProcessAliasOptionsArgs (lldb::CommandObjectSP &cmd_obj_sp,
    137                              const char *options_args,
    138                              OptionArgVectorSP &option_arg_vector_sp);
    139 
    140     void
    141     RemoveAliasOptions (const char *alias_name);
    142 
    143     void
    144     AddOrReplaceAliasOptions (const char *alias_name,
    145                               OptionArgVectorSP &option_arg_vector_sp);
    146 
    147     CommandObject *
    148     BuildAliasResult (const char *alias_name,
    149                       std::string &raw_input_string,
    150                       std::string &alias_result,
    151                       CommandReturnObject &result);
    152 
    153     bool
    154     HandleCommand (const char *command_line,
    155                    LazyBool add_to_history,
    156                    CommandReturnObject &result,
    157                    ExecutionContext *override_context = NULL,
    158                    bool repeat_on_empty_command = true,
    159                    bool no_context_switching = false);
    160 
    161     //------------------------------------------------------------------
    162     /// Execute a list of commands in sequence.
    163     ///
    164     /// @param[in] commands
    165     ///    The list of commands to execute.
    166     /// @param[in/out] context
    167     ///    The execution context in which to run the commands.  Can be NULL in which case the default
    168     ///    context will be used.
    169     /// @param[in] stop_on_continue
    170     ///    If \b true execution will end on the first command that causes the process in the
    171     ///    execution context to continue.  If \false, we won't check the execution status.
    172     /// @param[in] stop_on_error
    173     ///    If \b true execution will end on the first command that causes an error.
    174     /// @param[in] echo_commands
    175     ///    If \b true echo the command before executing it.  If \false, execute silently.
    176     /// @param[in] print_results
    177     ///    If \b true print the results of the command after executing it.  If \false, execute silently.
    178     /// @param[out] result
    179     ///    This is marked as succeeding with no output if all commands execute safely,
    180     ///    and failed with some explanation if we aborted executing the commands at some point.
    181     //------------------------------------------------------------------
    182     void
    183     HandleCommands (const StringList &commands,
    184                     ExecutionContext *context,
    185                     bool stop_on_continue,
    186                     bool stop_on_error,
    187                     bool echo_commands,
    188                     bool print_results,
    189                     LazyBool add_to_history,
    190                     CommandReturnObject &result);
    191 
    192     //------------------------------------------------------------------
    193     /// Execute a list of commands from a file.
    194     ///
    195     /// @param[in] file
    196     ///    The file from which to read in commands.
    197     /// @param[in/out] context
    198     ///    The execution context in which to run the commands.  Can be NULL in which case the default
    199     ///    context will be used.
    200     /// @param[in] stop_on_continue
    201     ///    If \b true execution will end on the first command that causes the process in the
    202     ///    execution context to continue.  If \false, we won't check the execution status.
    203     /// @param[in] stop_on_error
    204     ///    If \b true execution will end on the first command that causes an error.
    205     /// @param[in] echo_commands
    206     ///    If \b true echo the command before executing it.  If \false, execute silently.
    207     /// @param[in] print_results
    208     ///    If \b true print the results of the command after executing it.  If \false, execute silently.
    209     /// @param[out] result
    210     ///    This is marked as succeeding with no output if all commands execute safely,
    211     ///    and failed with some explanation if we aborted executing the commands at some point.
    212     //------------------------------------------------------------------
    213     void
    214     HandleCommandsFromFile (FileSpec &file,
    215                             ExecutionContext *context,
    216                             bool stop_on_continue,
    217                             bool stop_on_error,
    218                             bool echo_commands,
    219                             bool print_results,
    220                             LazyBool add_to_history,
    221                             CommandReturnObject &result);
    222 
    223     CommandObject *
    224     GetCommandObjectForCommand (std::string &command_line);
    225 
    226     // This handles command line completion.  You are given a pointer to the command string buffer, to the current cursor,
    227     // and to the end of the string (in case it is not NULL terminated).
    228     // You also passed in an StringList object to fill with the returns.
    229     // The first element of the array will be filled with the string that you would need to insert at
    230     // the cursor point to complete the cursor point to the longest common matching prefix.
    231     // If you want to limit the number of elements returned, set max_return_elements to the number of elements
    232     // you want returned.  Otherwise set max_return_elements to -1.
    233     // If you want to start some way into the match list, then set match_start_point to the desired start
    234     // point.
    235     // Returns:
    236     // -1 if the completion character should be inserted
    237     // -2 if the entire command line should be deleted and replaced with matches.GetStringAtIndex(0)
    238     // INT_MAX if the number of matches is > max_return_elements, but it is expensive to compute.
    239     // Otherwise, returns the number of matches.
    240     //
    241     // FIXME: Only max_return_elements == -1 is supported at present.
    242 
    243     int
    244     HandleCompletion (const char *current_line,
    245                       const char *cursor,
    246                       const char *last_char,
    247                       int match_start_point,
    248                       int max_return_elements,
    249                       StringList &matches);
    250 
    251     // This version just returns matches, and doesn't compute the substring.  It is here so the
    252     // Help command can call it for the first argument.
    253     // word_complete tells whether a the completions are considered a "complete" response (so the
    254     // completer should complete the quote & put a space after the word.
    255 
    256     int
    257     HandleCompletionMatches (Args &input,
    258                              int &cursor_index,
    259                              int &cursor_char_position,
    260                              int match_start_point,
    261                              int max_return_elements,
    262                              bool &word_complete,
    263                              StringList &matches);
    264 
    265 
    266     int
    267     GetCommandNamesMatchingPartialString (const char *cmd_cstr,
    268                                           bool include_aliases,
    269                                           StringList &matches);
    270 
    271     void
    272     GetHelp (CommandReturnObject &result,
    273              uint32_t types = eCommandTypesAllThem);
    274 
    275     void
    276     GetAliasHelp (const char *alias_name,
    277                   const char *command_name,
    278                   StreamString &help_string);
    279 
    280     void
    281     OutputFormattedHelpText (Stream &stream,
    282                              const char *command_word,
    283                              const char *separator,
    284                              const char *help_text,
    285                              size_t max_word_len);
    286 
    287     // this mimics OutputFormattedHelpText but it does perform a much simpler
    288     // formatting, basically ensuring line alignment. This is only good if you have
    289     // some complicated layout for your help text and want as little help as reasonable
    290     // in properly displaying it. Most of the times, you simply want to type some text
    291     // and have it printed in a reasonable way on screen. If so, use OutputFormattedHelpText
    292     void
    293     OutputHelpText (Stream &stream,
    294                     const char *command_word,
    295                     const char *separator,
    296                     const char *help_text,
    297                     uint32_t max_word_len);
    298 
    299     Debugger &
    300     GetDebugger ()
    301     {
    302         return m_debugger;
    303     }
    304 
    305     ExecutionContext
    306     GetExecutionContext()
    307     {
    308         return m_exe_ctx_ref.Lock();
    309     }
    310 
    311     void
    312     UpdateExecutionContext (ExecutionContext *override_context);
    313 
    314     lldb::PlatformSP
    315     GetPlatform (bool prefer_target_platform);
    316 
    317     const char *
    318     ProcessEmbeddedScriptCommands (const char *arg);
    319 
    320     const char *
    321     GetPrompt ();
    322 
    323     void
    324     SetPrompt (const char *);
    325 
    326     bool Confirm (const char *message, bool default_answer);
    327 
    328     static size_t
    329     GetConfirmationInputReaderCallback (void *baton,
    330                                         InputReader &reader,
    331                                         lldb::InputReaderAction action,
    332                                         const char *bytes,
    333                                         size_t bytes_len);
    334 
    335     void
    336     LoadCommandDictionary ();
    337 
    338     void
    339     Initialize ();
    340 
    341     void
    342     SetScriptLanguage (lldb::ScriptLanguage lang);
    343 
    344 
    345     bool
    346     HasCommands ();
    347 
    348     bool
    349     HasAliases ();
    350 
    351     bool
    352     HasUserCommands ();
    353 
    354     bool
    355     HasAliasOptions ();
    356 
    357     void
    358     BuildAliasCommandArgs (CommandObject *alias_cmd_obj,
    359                            const char *alias_name,
    360                            Args &cmd_args,
    361                            std::string &raw_input_string,
    362                            CommandReturnObject &result);
    363 
    364     int
    365     GetOptionArgumentPosition (const char *in_string);
    366 
    367     ScriptInterpreter *
    368     GetScriptInterpreter (bool can_create = true);
    369 
    370     void
    371     SkipLLDBInitFiles (bool skip_lldbinit_files)
    372     {
    373         m_skip_lldbinit_files = skip_lldbinit_files;
    374     }
    375 
    376     void
    377     SkipAppInitFiles (bool skip_app_init_files)
    378     {
    379         m_skip_app_init_files = m_skip_lldbinit_files;
    380     }
    381 
    382     bool
    383     GetSynchronous ();
    384 
    385     size_t
    386     FindLongestCommandWord (CommandObject::CommandMap &dict);
    387 
    388     void
    389     FindCommandsForApropos (const char *word,
    390                             StringList &commands_found,
    391                             StringList &commands_help,
    392                             bool search_builtin_commands,
    393                             bool search_user_commands);
    394 
    395     bool
    396     GetBatchCommandMode () { return m_batch_command_mode; }
    397 
    398     void
    399     SetBatchCommandMode (bool value) { m_batch_command_mode = value; }
    400 
    401     void
    402     ChildrenTruncated ()
    403     {
    404         if (m_truncation_warning == eNoTruncation)
    405             m_truncation_warning = eUnwarnedTruncation;
    406     }
    407 
    408     bool
    409     TruncationWarningNecessary ()
    410     {
    411         return (m_truncation_warning == eUnwarnedTruncation);
    412     }
    413 
    414     void
    415     TruncationWarningGiven ()
    416     {
    417         m_truncation_warning = eWarnedTruncation;
    418     }
    419 
    420     const char *
    421     TruncationWarningText ()
    422     {
    423         return "*** Some of your variables have more members than the debugger will show by default. To show all of them, you can either use the --show-all-children option to %s or raise the limit by changing the target.max-children-count setting.\n";
    424     }
    425 
    426     const CommandHistory&
    427     GetCommandHistory () const
    428     {
    429         return m_command_history;
    430     }
    431 
    432     CommandHistory&
    433     GetCommandHistory ()
    434     {
    435         return m_command_history;
    436     }
    437 
    438     //------------------------------------------------------------------
    439     // Properties
    440     //------------------------------------------------------------------
    441     bool
    442     GetExpandRegexAliases () const;
    443 
    444     bool
    445     GetPromptOnQuit () const;
    446 
    447     bool
    448     GetStopCmdSourceOnError () const;
    449 
    450 protected:
    451     friend class Debugger;
    452 
    453     void
    454     SetSynchronous (bool value);
    455 
    456     lldb::CommandObjectSP
    457     GetCommandSP (const char *cmd, bool include_aliases = true, bool exact = true, StringList *matches = NULL);
    458 
    459 private:
    460 
    461     Error
    462     PreprocessCommand (std::string &command);
    463 
    464     Debugger &m_debugger;                       // The debugger session that this interpreter is associated with
    465     ExecutionContextRef m_exe_ctx_ref;          // The current execution context to use when handling commands
    466     bool m_synchronous_execution;
    467     bool m_skip_lldbinit_files;
    468     bool m_skip_app_init_files;
    469     CommandObject::CommandMap m_command_dict;   // Stores basic built-in commands (they cannot be deleted, removed or overwritten).
    470     CommandObject::CommandMap m_alias_dict;     // Stores user aliases/abbreviations for commands
    471     CommandObject::CommandMap m_user_dict;      // Stores user-defined commands
    472     OptionArgMap m_alias_options;               // Stores any options (with or without arguments) that go with any alias.
    473     CommandHistory m_command_history;
    474     std::string m_repeat_command;               // Stores the command that will be executed for an empty command string.
    475     std::unique_ptr<ScriptInterpreter> m_script_interpreter_ap;
    476     char m_comment_char;
    477     bool m_batch_command_mode;
    478     ChildrenTruncatedWarningStatus m_truncation_warning;    // Whether we truncated children and whether the user has been told
    479     uint32_t m_command_source_depth;
    480 
    481 };
    482 
    483 
    484 } // namespace lldb_private
    485 
    486 #endif  // liblldb_CommandInterpreter_h_
    487