1 //===-- CommandInterpreter.h ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_CommandInterpreter_h_
11 #define liblldb_CommandInterpreter_h_
15 // Other libraries and framework includes
17 #include "lldb/lldb-private.h"
18 #include "lldb/Core/Broadcaster.h"
19 #include "lldb/Core/Debugger.h"
20 #include "lldb/Core/IOHandler.h"
21 #include "lldb/Core/Log.h"
22 #include "lldb/Interpreter/CommandHistory.h"
23 #include "lldb/Interpreter/CommandObject.h"
24 #include "lldb/Interpreter/ScriptInterpreter.h"
25 #include "lldb/Core/Event.h"
26 #include "lldb/Interpreter/Args.h"
27 #include "lldb/Core/StringList.h"
29 namespace lldb_private {
31 class CommandInterpreter :
34 public IOHandlerDelegate
37 typedef std::map<std::string, OptionArgVectorSP> OptionArgMap;
41 eBroadcastBitThreadShouldExit = (1 << 0),
42 eBroadcastBitResetPrompt = (1 << 1),
43 eBroadcastBitQuitCommandReceived = (1 << 2), // User entered quit
44 eBroadcastBitAsynchronousOutputData = (1 << 3),
45 eBroadcastBitAsynchronousErrorData = (1 << 4)
48 enum ChildrenTruncatedWarningStatus // tristate boolean to manage children truncation warning
50 eNoTruncation = 0, // never truncated
51 eUnwarnedTruncation = 1, // truncated but did not notify
52 eWarnedTruncation = 2 // truncated and notified
57 eCommandTypesBuiltin = 0x0001, // native commands such as "frame"
58 eCommandTypesUserDef = 0x0002, // scripted commands
59 eCommandTypesAliases = 0x0004, // aliases such as "po"
60 eCommandTypesAllThem = 0xFFFF // all commands
63 // These two functions fill out the Broadcaster interface:
65 static ConstString &GetStaticBroadcasterClass ();
67 virtual ConstString &GetBroadcasterClass() const
69 return GetStaticBroadcasterClass();
73 SourceInitFile (bool in_cwd,
74 CommandReturnObject &result);
76 CommandInterpreter (Debugger &debugger,
77 lldb::ScriptLanguage script_language,
78 bool synchronous_execution);
81 ~CommandInterpreter ();
84 AddCommand (const char *name,
85 const lldb::CommandObjectSP &cmd_sp,
89 AddUserCommand (std::string name,
90 const lldb::CommandObjectSP &cmd_sp,
94 GetCommandSPExact (const char *cmd,
95 bool include_aliases);
98 GetCommandObjectExact (const char *cmd_cstr,
99 bool include_aliases);
102 GetCommandObject (const char *cmd,
103 StringList *matches = NULL);
106 CommandExists (const char *cmd);
109 AliasExists (const char *cmd);
112 UserCommandExists (const char *cmd);
115 AddAlias (const char *alias_name,
116 lldb::CommandObjectSP& command_obj_sp);
119 RemoveAlias (const char *alias_name);
122 GetAliasFullName (const char *cmd, std::string &full_name);
125 RemoveUser (const char *alias_name);
134 GetAliasOptions (const char *alias_name);
138 ProcessAliasOptionsArgs (lldb::CommandObjectSP &cmd_obj_sp,
139 const char *options_args,
140 OptionArgVectorSP &option_arg_vector_sp);
143 RemoveAliasOptions (const char *alias_name);
146 AddOrReplaceAliasOptions (const char *alias_name,
147 OptionArgVectorSP &option_arg_vector_sp);
150 BuildAliasResult (const char *alias_name,
151 std::string &raw_input_string,
152 std::string &alias_result,
153 CommandReturnObject &result);
156 HandleCommand (const char *command_line,
157 LazyBool add_to_history,
158 CommandReturnObject &result,
159 ExecutionContext *override_context = NULL,
160 bool repeat_on_empty_command = true,
161 bool no_context_switching = false);
163 //------------------------------------------------------------------
164 /// Execute a list of commands in sequence.
166 /// @param[in] commands
167 /// The list of commands to execute.
168 /// @param[in/out] context
169 /// The execution context in which to run the commands. Can be NULL in which case the default
170 /// context will be used.
171 /// @param[in] stop_on_continue
172 /// If \b true execution will end on the first command that causes the process in the
173 /// execution context to continue. If \false, we won't check the execution status.
174 /// @param[in] stop_on_error
175 /// If \b true execution will end on the first command that causes an error.
176 /// @param[in] echo_commands
177 /// If \b true echo the command before executing it. If \false, execute silently.
178 /// @param[in] print_results
179 /// If \b true print the results of the command after executing it. If \false, execute silently.
180 /// @param[out] result
181 /// This is marked as succeeding with no output if all commands execute safely,
182 /// and failed with some explanation if we aborted executing the commands at some point.
183 //------------------------------------------------------------------
185 HandleCommands (const StringList &commands,
186 ExecutionContext *context,
187 bool stop_on_continue,
191 LazyBool add_to_history,
192 CommandReturnObject &result);
194 //------------------------------------------------------------------
195 /// Execute a list of commands from a file.
198 /// The file from which to read in commands.
199 /// @param[in/out] context
200 /// The execution context in which to run the commands. Can be NULL in which case the default
201 /// context will be used.
202 /// @param[in] stop_on_continue
203 /// If \b true execution will end on the first command that causes the process in the
204 /// execution context to continue. If \false, we won't check the execution status.
205 /// @param[in] stop_on_error
206 /// If \b true execution will end on the first command that causes an error.
207 /// @param[in] echo_commands
208 /// If \b true echo the command before executing it. If \false, execute silently.
209 /// @param[in] print_results
210 /// If \b true print the results of the command after executing it. If \false, execute silently.
211 /// @param[out] result
212 /// This is marked as succeeding with no output if all commands execute safely,
213 /// and failed with some explanation if we aborted executing the commands at some point.
214 //------------------------------------------------------------------
216 HandleCommandsFromFile (FileSpec &file,
217 ExecutionContext *context,
218 LazyBool stop_on_continue,
219 LazyBool stop_on_error,
220 LazyBool echo_commands,
221 LazyBool print_results,
222 LazyBool add_to_history,
223 CommandReturnObject &result);
226 GetCommandObjectForCommand (std::string &command_line);
228 // This handles command line completion. You are given a pointer to the command string buffer, to the current cursor,
229 // and to the end of the string (in case it is not NULL terminated).
230 // You also passed in an StringList object to fill with the returns.
231 // The first element of the array will be filled with the string that you would need to insert at
232 // the cursor point to complete the cursor point to the longest common matching prefix.
233 // If you want to limit the number of elements returned, set max_return_elements to the number of elements
234 // you want returned. Otherwise set max_return_elements to -1.
235 // If you want to start some way into the match list, then set match_start_point to the desired start
238 // -1 if the completion character should be inserted
239 // -2 if the entire command line should be deleted and replaced with matches.GetStringAtIndex(0)
240 // INT_MAX if the number of matches is > max_return_elements, but it is expensive to compute.
241 // Otherwise, returns the number of matches.
243 // FIXME: Only max_return_elements == -1 is supported at present.
246 HandleCompletion (const char *current_line,
248 const char *last_char,
249 int match_start_point,
250 int max_return_elements,
251 StringList &matches);
253 // This version just returns matches, and doesn't compute the substring. It is here so the
254 // Help command can call it for the first argument.
255 // word_complete tells whether a the completions are considered a "complete" response (so the
256 // completer should complete the quote & put a space after the word.
259 HandleCompletionMatches (Args &input,
261 int &cursor_char_position,
262 int match_start_point,
263 int max_return_elements,
265 StringList &matches);
269 GetCommandNamesMatchingPartialString (const char *cmd_cstr,
270 bool include_aliases,
271 StringList &matches);
274 GetHelp (CommandReturnObject &result,
275 uint32_t types = eCommandTypesAllThem);
278 GetAliasHelp (const char *alias_name,
279 const char *command_name,
280 StreamString &help_string);
283 OutputFormattedHelpText (Stream &stream,
284 const char *command_word,
285 const char *separator,
286 const char *help_text,
287 size_t max_word_len);
289 // this mimics OutputFormattedHelpText but it does perform a much simpler
290 // formatting, basically ensuring line alignment. This is only good if you have
291 // some complicated layout for your help text and want as little help as reasonable
292 // in properly displaying it. Most of the times, you simply want to type some text
293 // and have it printed in a reasonable way on screen. If so, use OutputFormattedHelpText
295 OutputHelpText (Stream &stream,
296 const char *command_word,
297 const char *separator,
298 const char *help_text,
299 uint32_t max_word_len);
308 GetExecutionContext()
310 const bool thread_and_frame_only_if_stopped = true;
311 return m_exe_ctx_ref.Lock(thread_and_frame_only_if_stopped);
315 UpdateExecutionContext (ExecutionContext *override_context);
318 GetPlatform (bool prefer_target_platform);
321 ProcessEmbeddedScriptCommands (const char *arg);
324 UpdatePrompt (const char *);
327 Confirm (const char *message,
328 bool default_answer);
331 LoadCommandDictionary ();
337 SetScriptLanguage (lldb::ScriptLanguage lang);
353 BuildAliasCommandArgs (CommandObject *alias_cmd_obj,
354 const char *alias_name,
356 std::string &raw_input_string,
357 CommandReturnObject &result);
360 GetOptionArgumentPosition (const char *in_string);
363 GetScriptInterpreter (bool can_create = true);
366 SkipLLDBInitFiles (bool skip_lldbinit_files)
368 m_skip_lldbinit_files = skip_lldbinit_files;
372 SkipAppInitFiles (bool skip_app_init_files)
374 m_skip_app_init_files = m_skip_lldbinit_files;
381 FindLongestCommandWord (CommandObject::CommandMap &dict);
384 FindCommandsForApropos (const char *word,
385 StringList &commands_found,
386 StringList &commands_help,
387 bool search_builtin_commands,
388 bool search_user_commands);
391 GetBatchCommandMode () { return m_batch_command_mode; }
394 SetBatchCommandMode (bool value) {
395 const bool old_value = m_batch_command_mode;
396 m_batch_command_mode = value;
403 if (m_truncation_warning == eNoTruncation)
404 m_truncation_warning = eUnwarnedTruncation;
408 TruncationWarningNecessary ()
410 return (m_truncation_warning == eUnwarnedTruncation);
414 TruncationWarningGiven ()
416 m_truncation_warning = eWarnedTruncation;
420 TruncationWarningText ()
422 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";
425 const CommandHistory&
426 GetCommandHistory () const
428 return m_command_history;
434 return m_command_history;
441 RunCommandInterpreter (bool auto_handle_events,
445 GetLLDBCommandsFromIOHandler (const char *prompt,
446 IOHandlerDelegate &delegate,
451 GetPythonCommandsFromIOHandler (const char *prompt,
452 IOHandlerDelegate &delegate,
456 //------------------------------------------------------------------
458 //------------------------------------------------------------------
460 GetExpandRegexAliases () const;
463 GetPromptOnQuit () const;
466 GetStopCmdSourceOnError () const;
469 friend class Debugger;
471 //------------------------------------------------------------------
472 // IOHandlerDelegate functions
473 //------------------------------------------------------------------
475 IOHandlerInputComplete (IOHandler &io_handler,
479 GetControlSequence (char ch)
482 return ConstString("quit\n");
483 return ConstString();
490 SetSynchronous (bool value);
492 lldb::CommandObjectSP
493 GetCommandSP (const char *cmd, bool include_aliases = true, bool exact = true, StringList *matches = NULL);
499 PreprocessCommand (std::string &command);
501 Debugger &m_debugger; // The debugger session that this interpreter is associated with
502 ExecutionContextRef m_exe_ctx_ref; // The current execution context to use when handling commands
503 bool m_synchronous_execution;
504 bool m_skip_lldbinit_files;
505 bool m_skip_app_init_files;
506 CommandObject::CommandMap m_command_dict; // Stores basic built-in commands (they cannot be deleted, removed or overwritten).
507 CommandObject::CommandMap m_alias_dict; // Stores user aliases/abbreviations for commands
508 CommandObject::CommandMap m_user_dict; // Stores user-defined commands
509 OptionArgMap m_alias_options; // Stores any options (with or without arguments) that go with any alias.
510 CommandHistory m_command_history;
511 std::string m_repeat_command; // Stores the command that will be executed for an empty command string.
512 std::unique_ptr<ScriptInterpreter> m_script_interpreter_ap;
513 lldb::IOHandlerSP m_command_io_handler_sp;
515 bool m_batch_command_mode;
516 ChildrenTruncatedWarningStatus m_truncation_warning; // Whether we truncated children and whether the user has been told
517 uint32_t m_command_source_depth;
518 std::vector<uint32_t> m_command_source_flags;
523 } // namespace lldb_private
525 #endif // liblldb_CommandInterpreter_h_