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/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"
28 namespace lldb_private {
30 class CommandInterpreter :
35 typedef std::map<std::string, OptionArgVectorSP> OptionArgMap;
39 eBroadcastBitThreadShouldExit = (1 << 0),
40 eBroadcastBitResetPrompt = (1 << 1),
41 eBroadcastBitQuitCommandReceived = (1 << 2), // User entered quit
42 eBroadcastBitAsynchronousOutputData = (1 << 3),
43 eBroadcastBitAsynchronousErrorData = (1 << 4)
46 enum ChildrenTruncatedWarningStatus // tristate boolean to manage children truncation warning
48 eNoTruncation = 0, // never truncated
49 eUnwarnedTruncation = 1, // truncated but did not notify
50 eWarnedTruncation = 2 // truncated and notified
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
61 // These two functions fill out the Broadcaster interface:
63 static ConstString &GetStaticBroadcasterClass ();
65 virtual ConstString &GetBroadcasterClass() const
67 return GetStaticBroadcasterClass();
71 SourceInitFile (bool in_cwd,
72 CommandReturnObject &result);
74 CommandInterpreter (Debugger &debugger,
75 lldb::ScriptLanguage script_language,
76 bool synchronous_execution);
79 ~CommandInterpreter ();
82 AddCommand (const char *name,
83 const lldb::CommandObjectSP &cmd_sp,
87 AddUserCommand (std::string name,
88 const lldb::CommandObjectSP &cmd_sp,
92 GetCommandSPExact (const char *cmd,
93 bool include_aliases);
96 GetCommandObjectExact (const char *cmd_cstr,
97 bool include_aliases);
100 GetCommandObject (const char *cmd,
101 StringList *matches = NULL);
104 CommandExists (const char *cmd);
107 AliasExists (const char *cmd);
110 UserCommandExists (const char *cmd);
113 AddAlias (const char *alias_name,
114 lldb::CommandObjectSP& command_obj_sp);
117 RemoveAlias (const char *alias_name);
120 GetAliasFullName (const char *cmd, std::string &full_name);
123 RemoveUser (const char *alias_name);
132 GetAliasOptions (const char *alias_name);
136 ProcessAliasOptionsArgs (lldb::CommandObjectSP &cmd_obj_sp,
137 const char *options_args,
138 OptionArgVectorSP &option_arg_vector_sp);
141 RemoveAliasOptions (const char *alias_name);
144 AddOrReplaceAliasOptions (const char *alias_name,
145 OptionArgVectorSP &option_arg_vector_sp);
148 BuildAliasResult (const char *alias_name,
149 std::string &raw_input_string,
150 std::string &alias_result,
151 CommandReturnObject &result);
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);
161 //------------------------------------------------------------------
162 /// Execute a list of commands in sequence.
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 //------------------------------------------------------------------
183 HandleCommands (const StringList &commands,
184 ExecutionContext *context,
185 bool stop_on_continue,
189 LazyBool add_to_history,
190 CommandReturnObject &result);
192 //------------------------------------------------------------------
193 /// Execute a list of commands from a 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 //------------------------------------------------------------------
214 HandleCommandsFromFile (FileSpec &file,
215 ExecutionContext *context,
216 bool stop_on_continue,
220 LazyBool add_to_history,
221 CommandReturnObject &result);
224 GetCommandObjectForCommand (std::string &command_line);
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
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.
241 // FIXME: Only max_return_elements == -1 is supported at present.
244 HandleCompletion (const char *current_line,
246 const char *last_char,
247 int match_start_point,
248 int max_return_elements,
249 StringList &matches);
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.
257 HandleCompletionMatches (Args &input,
259 int &cursor_char_position,
260 int match_start_point,
261 int max_return_elements,
263 StringList &matches);
267 GetCommandNamesMatchingPartialString (const char *cmd_cstr,
268 bool include_aliases,
269 StringList &matches);
272 GetHelp (CommandReturnObject &result,
273 uint32_t types = eCommandTypesAllThem);
276 GetAliasHelp (const char *alias_name,
277 const char *command_name,
278 StreamString &help_string);
281 OutputFormattedHelpText (Stream &stream,
282 const char *command_word,
283 const char *separator,
284 const char *help_text,
285 size_t max_word_len);
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
293 OutputHelpText (Stream &stream,
294 const char *command_word,
295 const char *separator,
296 const char *help_text,
297 uint32_t max_word_len);
306 GetExecutionContext()
308 return m_exe_ctx_ref.Lock();
312 UpdateExecutionContext (ExecutionContext *override_context);
315 GetPlatform (bool prefer_target_platform);
318 ProcessEmbeddedScriptCommands (const char *arg);
324 SetPrompt (const char *);
326 bool Confirm (const char *message, bool default_answer);
329 GetConfirmationInputReaderCallback (void *baton,
331 lldb::InputReaderAction action,
336 LoadCommandDictionary ();
342 SetScriptLanguage (lldb::ScriptLanguage lang);
358 BuildAliasCommandArgs (CommandObject *alias_cmd_obj,
359 const char *alias_name,
361 std::string &raw_input_string,
362 CommandReturnObject &result);
365 GetOptionArgumentPosition (const char *in_string);
368 GetScriptInterpreter (bool can_create = true);
371 SkipLLDBInitFiles (bool skip_lldbinit_files)
373 m_skip_lldbinit_files = skip_lldbinit_files;
377 SkipAppInitFiles (bool skip_app_init_files)
379 m_skip_app_init_files = m_skip_lldbinit_files;
386 FindLongestCommandWord (CommandObject::CommandMap &dict);
389 FindCommandsForApropos (const char *word,
390 StringList &commands_found,
391 StringList &commands_help,
392 bool search_builtin_commands,
393 bool search_user_commands);
396 GetBatchCommandMode () { return m_batch_command_mode; }
399 SetBatchCommandMode (bool value) { m_batch_command_mode = value; }
404 if (m_truncation_warning == eNoTruncation)
405 m_truncation_warning = eUnwarnedTruncation;
409 TruncationWarningNecessary ()
411 return (m_truncation_warning == eUnwarnedTruncation);
415 TruncationWarningGiven ()
417 m_truncation_warning = eWarnedTruncation;
421 TruncationWarningText ()
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";
426 const CommandHistory&
427 GetCommandHistory () const
429 return m_command_history;
435 return m_command_history;
438 //------------------------------------------------------------------
440 //------------------------------------------------------------------
442 GetExpandRegexAliases () const;
445 GetPromptOnQuit () const;
448 GetStopCmdSourceOnError () const;
451 friend class Debugger;
454 SetSynchronous (bool value);
456 lldb::CommandObjectSP
457 GetCommandSP (const char *cmd, bool include_aliases = true, bool exact = true, StringList *matches = NULL);
462 PreprocessCommand (std::string &command);
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;
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;
484 } // namespace lldb_private
486 #endif // liblldb_CommandInterpreter_h_