1 //===-- CommandInterpreter.h ------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_INTERPRETER_COMMANDINTERPRETER_H
10 #define LLDB_INTERPRETER_COMMANDINTERPRETER_H
11 
12 #include "lldb/Core/Debugger.h"
13 #include "lldb/Core/IOHandler.h"
14 #include "lldb/Interpreter/CommandAlias.h"
15 #include "lldb/Interpreter/CommandHistory.h"
16 #include "lldb/Interpreter/CommandObject.h"
17 #include "lldb/Interpreter/ScriptInterpreter.h"
18 #include "lldb/Utility/Args.h"
19 #include "lldb/Utility/Broadcaster.h"
20 #include "lldb/Utility/CompletionRequest.h"
21 #include "lldb/Utility/Event.h"
22 #include "lldb/Utility/Log.h"
23 #include "lldb/Utility/StreamString.h"
24 #include "lldb/Utility/StringList.h"
25 #include "lldb/lldb-forward.h"
26 #include "lldb/lldb-private.h"
27 
28 #include <mutex>
29 #include <stack>
30 
31 namespace lldb_private {
32 class CommandInterpreter;
33 
34 class CommandInterpreterRunResult {
35 public:
36   CommandInterpreterRunResult() = default;
37 
GetNumErrors()38   uint32_t GetNumErrors() const { return m_num_errors; }
39 
GetResult()40   lldb::CommandInterpreterResult GetResult() const { return m_result; }
41 
IsResult(lldb::CommandInterpreterResult result)42   bool IsResult(lldb::CommandInterpreterResult result) {
43     return m_result == result;
44   }
45 
46 protected:
47   friend CommandInterpreter;
48 
IncrementNumberOfErrors()49   void IncrementNumberOfErrors() { m_num_errors++; }
50 
SetResult(lldb::CommandInterpreterResult result)51   void SetResult(lldb::CommandInterpreterResult result) { m_result = result; }
52 
53 private:
54   int m_num_errors = 0;
55   lldb::CommandInterpreterResult m_result =
56       lldb::eCommandInterpreterResultSuccess;
57 };
58 
59 class CommandInterpreterRunOptions {
60 public:
61   /// Construct a CommandInterpreterRunOptions object. This class is used to
62   /// control all the instances where we run multiple commands, e.g.
63   /// HandleCommands, HandleCommandsFromFile, RunCommandInterpreter.
64   ///
65   /// The meanings of the options in this object are:
66   ///
67   /// \param[in] stop_on_continue
68   ///    If \b true, execution will end on the first command that causes the
69   ///    process in the execution context to continue. If \b false, we won't
70   ///    check the execution status.
71   /// \param[in] stop_on_error
72   ///    If \b true, execution will end on the first command that causes an
73   ///    error.
74   /// \param[in] stop_on_crash
75   ///    If \b true, when a command causes the target to run, and the end of the
76   ///    run is a signal or exception, stop executing the commands.
77   /// \param[in] echo_commands
78   ///    If \b true, echo the command before executing it. If \b false, execute
79   ///    silently.
80   /// \param[in] echo_comments
81   ///    If \b true, echo command even if it is a pure comment line. If
82   ///    \b false, print no ouput in this case. This setting has an effect only
83   ///    if echo_commands is \b true.
84   /// \param[in] print_results
85   ///    If \b true and the command succeeds, print the results of the command
86   ///    after executing it. If \b false, execute silently.
87   /// \param[in] print_errors
88   ///    If \b true and the command fails, print the results of the command
89   ///    after executing it. If \b false, execute silently.
90   /// \param[in] add_to_history
91   ///    If \b true add the commands to the command history. If \b false, don't
92   ///    add them.
CommandInterpreterRunOptions(LazyBool stop_on_continue,LazyBool stop_on_error,LazyBool stop_on_crash,LazyBool echo_commands,LazyBool echo_comments,LazyBool print_results,LazyBool print_errors,LazyBool add_to_history)93   CommandInterpreterRunOptions(LazyBool stop_on_continue,
94                                LazyBool stop_on_error, LazyBool stop_on_crash,
95                                LazyBool echo_commands, LazyBool echo_comments,
96                                LazyBool print_results, LazyBool print_errors,
97                                LazyBool add_to_history)
98       : m_stop_on_continue(stop_on_continue), m_stop_on_error(stop_on_error),
99         m_stop_on_crash(stop_on_crash), m_echo_commands(echo_commands),
100         m_echo_comment_commands(echo_comments), m_print_results(print_results),
101         m_print_errors(print_errors), m_add_to_history(add_to_history) {}
102 
103   CommandInterpreterRunOptions() = default;
104 
SetSilent(bool silent)105   void SetSilent(bool silent) {
106     LazyBool value = silent ? eLazyBoolNo : eLazyBoolYes;
107 
108     m_print_results = value;
109     m_print_errors = value;
110     m_echo_commands = value;
111     m_echo_comment_commands = value;
112     m_add_to_history = value;
113   }
114   // These return the default behaviors if the behavior is not
115   // eLazyBoolCalculate. But I've also left the ivars public since for
116   // different ways of running the interpreter you might want to force
117   // different defaults...  In that case, just grab the LazyBool ivars directly
118   // and do what you want with eLazyBoolCalculate.
GetStopOnContinue()119   bool GetStopOnContinue() const { return DefaultToNo(m_stop_on_continue); }
120 
SetStopOnContinue(bool stop_on_continue)121   void SetStopOnContinue(bool stop_on_continue) {
122     m_stop_on_continue = stop_on_continue ? eLazyBoolYes : eLazyBoolNo;
123   }
124 
GetStopOnError()125   bool GetStopOnError() const { return DefaultToNo(m_stop_on_error); }
126 
SetStopOnError(bool stop_on_error)127   void SetStopOnError(bool stop_on_error) {
128     m_stop_on_error = stop_on_error ? eLazyBoolYes : eLazyBoolNo;
129   }
130 
GetStopOnCrash()131   bool GetStopOnCrash() const { return DefaultToNo(m_stop_on_crash); }
132 
SetStopOnCrash(bool stop_on_crash)133   void SetStopOnCrash(bool stop_on_crash) {
134     m_stop_on_crash = stop_on_crash ? eLazyBoolYes : eLazyBoolNo;
135   }
136 
GetEchoCommands()137   bool GetEchoCommands() const { return DefaultToYes(m_echo_commands); }
138 
SetEchoCommands(bool echo_commands)139   void SetEchoCommands(bool echo_commands) {
140     m_echo_commands = echo_commands ? eLazyBoolYes : eLazyBoolNo;
141   }
142 
GetEchoCommentCommands()143   bool GetEchoCommentCommands() const {
144     return DefaultToYes(m_echo_comment_commands);
145   }
146 
SetEchoCommentCommands(bool echo_comments)147   void SetEchoCommentCommands(bool echo_comments) {
148     m_echo_comment_commands = echo_comments ? eLazyBoolYes : eLazyBoolNo;
149   }
150 
GetPrintResults()151   bool GetPrintResults() const { return DefaultToYes(m_print_results); }
152 
SetPrintResults(bool print_results)153   void SetPrintResults(bool print_results) {
154     m_print_results = print_results ? eLazyBoolYes : eLazyBoolNo;
155   }
156 
GetPrintErrors()157   bool GetPrintErrors() const { return DefaultToYes(m_print_errors); }
158 
SetPrintErrors(bool print_errors)159   void SetPrintErrors(bool print_errors) {
160     m_print_errors = print_errors ? eLazyBoolYes : eLazyBoolNo;
161   }
162 
GetAddToHistory()163   bool GetAddToHistory() const { return DefaultToYes(m_add_to_history); }
164 
SetAddToHistory(bool add_to_history)165   void SetAddToHistory(bool add_to_history) {
166     m_add_to_history = add_to_history ? eLazyBoolYes : eLazyBoolNo;
167   }
168 
GetAutoHandleEvents()169   bool GetAutoHandleEvents() const {
170     return DefaultToYes(m_auto_handle_events);
171   }
172 
SetAutoHandleEvents(bool auto_handle_events)173   void SetAutoHandleEvents(bool auto_handle_events) {
174     m_auto_handle_events = auto_handle_events ? eLazyBoolYes : eLazyBoolNo;
175   }
176 
GetSpawnThread()177   bool GetSpawnThread() const { return DefaultToNo(m_spawn_thread); }
178 
SetSpawnThread(bool spawn_thread)179   void SetSpawnThread(bool spawn_thread) {
180     m_spawn_thread = spawn_thread ? eLazyBoolYes : eLazyBoolNo;
181   }
182 
183   LazyBool m_stop_on_continue = eLazyBoolCalculate;
184   LazyBool m_stop_on_error = eLazyBoolCalculate;
185   LazyBool m_stop_on_crash = eLazyBoolCalculate;
186   LazyBool m_echo_commands = eLazyBoolCalculate;
187   LazyBool m_echo_comment_commands = eLazyBoolCalculate;
188   LazyBool m_print_results = eLazyBoolCalculate;
189   LazyBool m_print_errors = eLazyBoolCalculate;
190   LazyBool m_add_to_history = eLazyBoolCalculate;
191   LazyBool m_auto_handle_events;
192   LazyBool m_spawn_thread;
193 
194 private:
DefaultToYes(LazyBool flag)195   static bool DefaultToYes(LazyBool flag) {
196     switch (flag) {
197     case eLazyBoolNo:
198       return false;
199     default:
200       return true;
201     }
202   }
203 
DefaultToNo(LazyBool flag)204   static bool DefaultToNo(LazyBool flag) {
205     switch (flag) {
206     case eLazyBoolYes:
207       return true;
208     default:
209       return false;
210     }
211   }
212 };
213 
214 class CommandInterpreter : public Broadcaster,
215                            public Properties,
216                            public IOHandlerDelegate {
217 public:
218   enum {
219     eBroadcastBitThreadShouldExit = (1 << 0),
220     eBroadcastBitResetPrompt = (1 << 1),
221     eBroadcastBitQuitCommandReceived = (1 << 2), // User entered quit
222     eBroadcastBitAsynchronousOutputData = (1 << 3),
223     eBroadcastBitAsynchronousErrorData = (1 << 4)
224   };
225 
226   enum ChildrenTruncatedWarningStatus // tristate boolean to manage children
227                                       // truncation warning
228   { eNoTruncation = 0,                // never truncated
229     eUnwarnedTruncation = 1,          // truncated but did not notify
230     eWarnedTruncation = 2             // truncated and notified
231   };
232 
233   enum CommandTypes {
234     eCommandTypesBuiltin = 0x0001, // native commands such as "frame"
235     eCommandTypesUserDef = 0x0002, // scripted commands
236     eCommandTypesAliases = 0x0004, // aliases such as "po"
237     eCommandTypesHidden = 0x0008,  // commands prefixed with an underscore
238     eCommandTypesAllThem = 0xFFFF  // all commands
239   };
240 
241   CommandInterpreter(Debugger &debugger, bool synchronous_execution);
242 
243   ~CommandInterpreter() override = default;
244 
245   // These two functions fill out the Broadcaster interface:
246 
247   static ConstString &GetStaticBroadcasterClass();
248 
GetBroadcasterClass()249   ConstString &GetBroadcasterClass() const override {
250     return GetStaticBroadcasterClass();
251   }
252 
253   void SourceInitFileCwd(CommandReturnObject &result);
254   void SourceInitFileHome(CommandReturnObject &result, bool is_repl = false);
255 
256   bool AddCommand(llvm::StringRef name, const lldb::CommandObjectSP &cmd_sp,
257                   bool can_replace);
258 
259   bool AddUserCommand(llvm::StringRef name, const lldb::CommandObjectSP &cmd_sp,
260                       bool can_replace);
261 
262   lldb::CommandObjectSP GetCommandSPExact(llvm::StringRef cmd,
263                                           bool include_aliases = false) const;
264 
265   CommandObject *GetCommandObject(llvm::StringRef cmd,
266                                   StringList *matches = nullptr,
267                                   StringList *descriptions = nullptr) const;
268 
269   bool CommandExists(llvm::StringRef cmd) const;
270 
271   bool AliasExists(llvm::StringRef cmd) const;
272 
273   bool UserCommandExists(llvm::StringRef cmd) const;
274 
275   CommandAlias *AddAlias(llvm::StringRef alias_name,
276                          lldb::CommandObjectSP &command_obj_sp,
277                          llvm::StringRef args_string = llvm::StringRef());
278 
279   // Remove a command if it is removable (python or regex command)
280   bool RemoveCommand(llvm::StringRef cmd);
281 
282   bool RemoveAlias(llvm::StringRef alias_name);
283 
284   bool GetAliasFullName(llvm::StringRef cmd, std::string &full_name) const;
285 
286   bool RemoveUser(llvm::StringRef alias_name);
287 
RemoveAllUser()288   void RemoveAllUser() { m_user_dict.clear(); }
289 
290   const CommandAlias *GetAlias(llvm::StringRef alias_name) const;
291 
292   CommandObject *BuildAliasResult(llvm::StringRef alias_name,
293                                   std::string &raw_input_string,
294                                   std::string &alias_result,
295                                   CommandReturnObject &result);
296 
297   bool HandleCommand(const char *command_line, LazyBool add_to_history,
298                      const ExecutionContext &override_context,
299                      CommandReturnObject &result);
300 
301   bool HandleCommand(const char *command_line, LazyBool add_to_history,
302                      CommandReturnObject &result);
303 
304   bool WasInterrupted() const;
305 
306   /// Execute a list of commands in sequence.
307   ///
308   /// \param[in] commands
309   ///    The list of commands to execute.
310   /// \param[in,out] context
311   ///    The execution context in which to run the commands.
312   /// \param[in] options
313   ///    This object holds the options used to control when to stop, whether to
314   ///    execute commands,
315   ///    etc.
316   /// \param[out] result
317   ///    This is marked as succeeding with no output if all commands execute
318   ///    safely,
319   ///    and failed with some explanation if we aborted executing the commands
320   ///    at some point.
321   void HandleCommands(const StringList &commands,
322                       const ExecutionContext &context,
323                       const CommandInterpreterRunOptions &options,
324                       CommandReturnObject &result);
325 
326   void HandleCommands(const StringList &commands,
327                       const CommandInterpreterRunOptions &options,
328                       CommandReturnObject &result);
329 
330   /// Execute a list of commands from a file.
331   ///
332   /// \param[in] file
333   ///    The file from which to read in commands.
334   /// \param[in,out] context
335   ///    The execution context in which to run the commands.
336   /// \param[in] options
337   ///    This object holds the options used to control when to stop, whether to
338   ///    execute commands,
339   ///    etc.
340   /// \param[out] result
341   ///    This is marked as succeeding with no output if all commands execute
342   ///    safely,
343   ///    and failed with some explanation if we aborted executing the commands
344   ///    at some point.
345   void HandleCommandsFromFile(FileSpec &file, const ExecutionContext &context,
346                               const CommandInterpreterRunOptions &options,
347                               CommandReturnObject &result);
348 
349   void HandleCommandsFromFile(FileSpec &file,
350                               const CommandInterpreterRunOptions &options,
351                               CommandReturnObject &result);
352 
353   CommandObject *GetCommandObjectForCommand(llvm::StringRef &command_line);
354 
355   /// Returns the auto-suggestion string that should be added to the given
356   /// command line.
357   llvm::Optional<std::string> GetAutoSuggestionForCommand(llvm::StringRef line);
358 
359   // This handles command line completion.
360   void HandleCompletion(CompletionRequest &request);
361 
362   // This version just returns matches, and doesn't compute the substring. It
363   // is here so the Help command can call it for the first argument.
364   void HandleCompletionMatches(CompletionRequest &request);
365 
366   int GetCommandNamesMatchingPartialString(const char *cmd_cstr,
367                                            bool include_aliases,
368                                            StringList &matches,
369                                            StringList &descriptions);
370 
371   void GetHelp(CommandReturnObject &result,
372                uint32_t types = eCommandTypesAllThem);
373 
374   void GetAliasHelp(const char *alias_name, StreamString &help_string);
375 
376   void OutputFormattedHelpText(Stream &strm, llvm::StringRef prefix,
377                                llvm::StringRef help_text);
378 
379   void OutputFormattedHelpText(Stream &stream, llvm::StringRef command_word,
380                                llvm::StringRef separator,
381                                llvm::StringRef help_text, size_t max_word_len);
382 
383   // this mimics OutputFormattedHelpText but it does perform a much simpler
384   // formatting, basically ensuring line alignment. This is only good if you
385   // have some complicated layout for your help text and want as little help as
386   // reasonable in properly displaying it. Most of the times, you simply want
387   // to type some text and have it printed in a reasonable way on screen. If
388   // so, use OutputFormattedHelpText
389   void OutputHelpText(Stream &stream, llvm::StringRef command_word,
390                       llvm::StringRef separator, llvm::StringRef help_text,
391                       uint32_t max_word_len);
392 
GetDebugger()393   Debugger &GetDebugger() { return m_debugger; }
394 
395   ExecutionContext GetExecutionContext() const;
396 
397   lldb::PlatformSP GetPlatform(bool prefer_target_platform);
398 
399   const char *ProcessEmbeddedScriptCommands(const char *arg);
400 
401   void UpdatePrompt(llvm::StringRef prompt);
402 
403   bool Confirm(llvm::StringRef message, bool default_answer);
404 
405   void LoadCommandDictionary();
406 
407   void Initialize();
408 
409   void Clear();
410 
411   bool HasCommands() const;
412 
413   bool HasAliases() const;
414 
415   bool HasUserCommands() const;
416 
417   bool HasAliasOptions() const;
418 
419   void BuildAliasCommandArgs(CommandObject *alias_cmd_obj,
420                              const char *alias_name, Args &cmd_args,
421                              std::string &raw_input_string,
422                              CommandReturnObject &result);
423 
424   int GetOptionArgumentPosition(const char *in_string);
425 
SkipLLDBInitFiles(bool skip_lldbinit_files)426   void SkipLLDBInitFiles(bool skip_lldbinit_files) {
427     m_skip_lldbinit_files = skip_lldbinit_files;
428   }
429 
SkipAppInitFiles(bool skip_app_init_files)430   void SkipAppInitFiles(bool skip_app_init_files) {
431     m_skip_app_init_files = skip_app_init_files;
432   }
433 
434   bool GetSynchronous();
435 
436   void FindCommandsForApropos(llvm::StringRef word, StringList &commands_found,
437                               StringList &commands_help,
438                               bool search_builtin_commands,
439                               bool search_user_commands,
440                               bool search_alias_commands);
441 
GetBatchCommandMode()442   bool GetBatchCommandMode() { return m_batch_command_mode; }
443 
SetBatchCommandMode(bool value)444   bool SetBatchCommandMode(bool value) {
445     const bool old_value = m_batch_command_mode;
446     m_batch_command_mode = value;
447     return old_value;
448   }
449 
ChildrenTruncated()450   void ChildrenTruncated() {
451     if (m_truncation_warning == eNoTruncation)
452       m_truncation_warning = eUnwarnedTruncation;
453   }
454 
TruncationWarningNecessary()455   bool TruncationWarningNecessary() {
456     return (m_truncation_warning == eUnwarnedTruncation);
457   }
458 
TruncationWarningGiven()459   void TruncationWarningGiven() { m_truncation_warning = eWarnedTruncation; }
460 
TruncationWarningText()461   const char *TruncationWarningText() {
462     return "*** Some of your variables have more members than the debugger "
463            "will show by default. To show all of them, you can either use the "
464            "--show-all-children option to %s or raise the limit by changing "
465            "the target.max-children-count setting.\n";
466   }
467 
GetCommandHistory()468   CommandHistory &GetCommandHistory() { return m_command_history; }
469 
470   bool IsActive();
471 
472   CommandInterpreterRunResult
473   RunCommandInterpreter(CommandInterpreterRunOptions &options);
474 
475   void GetLLDBCommandsFromIOHandler(const char *prompt,
476                                     IOHandlerDelegate &delegate,
477                                     void *baton = nullptr);
478 
479   void GetPythonCommandsFromIOHandler(const char *prompt,
480                                       IOHandlerDelegate &delegate,
481                                       void *baton = nullptr);
482 
483   const char *GetCommandPrefix();
484 
485   // Properties
486   bool GetExpandRegexAliases() const;
487 
488   bool GetPromptOnQuit() const;
489   void SetPromptOnQuit(bool enable);
490 
491   bool GetSaveSessionOnQuit() const;
492   void SetSaveSessionOnQuit(bool enable);
493 
494   FileSpec GetSaveSessionDirectory() const;
495   void SetSaveSessionDirectory(llvm::StringRef path);
496 
497   bool GetEchoCommands() const;
498   void SetEchoCommands(bool enable);
499 
500   bool GetEchoCommentCommands() const;
501   void SetEchoCommentCommands(bool enable);
502 
503   bool GetRepeatPreviousCommand() const;
504 
GetUserCommands()505   const CommandObject::CommandMap &GetUserCommands() const {
506     return m_user_dict;
507   }
508 
GetCommands()509   const CommandObject::CommandMap &GetCommands() const {
510     return m_command_dict;
511   }
512 
GetAliases()513   const CommandObject::CommandMap &GetAliases() const { return m_alias_dict; }
514 
515   /// Specify if the command interpreter should allow that the user can
516   /// specify a custom exit code when calling 'quit'.
517   void AllowExitCodeOnQuit(bool allow);
518 
519   /// Sets the exit code for the quit command.
520   /// \param[in] exit_code
521   ///     The exit code that the driver should return on exit.
522   /// \return True if the exit code was successfully set; false if the
523   ///         interpreter doesn't allow custom exit codes.
524   /// \see AllowExitCodeOnQuit
525   LLVM_NODISCARD bool SetQuitExitCode(int exit_code);
526 
527   /// Returns the exit code that the user has specified when running the
528   /// 'quit' command.
529   /// \param[out] exited
530   ///     Set to true if the user has called quit with a custom exit code.
531   int GetQuitExitCode(bool &exited) const;
532 
533   void ResolveCommand(const char *command_line, CommandReturnObject &result);
534 
535   bool GetStopCmdSourceOnError() const;
536 
537   lldb::IOHandlerSP
538   GetIOHandler(bool force_create = false,
539                CommandInterpreterRunOptions *options = nullptr);
540 
541   bool GetSpaceReplPrompts() const;
542 
543   /// Save the current debugger session transcript to a file on disk.
544   /// \param output_file
545   ///     The file path to which the session transcript will be written. Since
546   ///     the argument is optional, an arbitrary temporary file will be create
547   ///     when no argument is passed.
548   /// \param result
549   ///     This is used to pass function output and error messages.
550   /// \return \b true if the session transcript was successfully written to
551   /// disk, \b false otherwise.
552   bool SaveTranscript(CommandReturnObject &result,
553                       llvm::Optional<std::string> output_file = llvm::None);
554 
555   FileSpec GetCurrentSourceDir();
556 
557 protected:
558   friend class Debugger;
559 
560   // IOHandlerDelegate functions
561   void IOHandlerInputComplete(IOHandler &io_handler,
562                               std::string &line) override;
563 
IOHandlerGetControlSequence(char ch)564   ConstString IOHandlerGetControlSequence(char ch) override {
565     if (ch == 'd')
566       return ConstString("quit\n");
567     return ConstString();
568   }
569 
570   bool IOHandlerInterrupt(IOHandler &io_handler) override;
571 
572   void GetProcessOutput();
573 
574   bool DidProcessStopAbnormally() const;
575 
576   void SetSynchronous(bool value);
577 
578   lldb::CommandObjectSP GetCommandSP(llvm::StringRef cmd,
579                                      bool include_aliases = true,
580                                      bool exact = true,
581                                      StringList *matches = nullptr,
582                                      StringList *descriptions = nullptr) const;
583 
584 private:
585   void OverrideExecutionContext(const ExecutionContext &override_context);
586 
587   void RestoreExecutionContext();
588 
589   Status PreprocessCommand(std::string &command);
590 
591   void SourceInitFile(FileSpec file, CommandReturnObject &result);
592 
593   // Completely resolves aliases and abbreviations, returning a pointer to the
594   // final command object and updating command_line to the fully substituted
595   // and translated command.
596   CommandObject *ResolveCommandImpl(std::string &command_line,
597                                     CommandReturnObject &result);
598 
599   void FindCommandsForApropos(llvm::StringRef word, StringList &commands_found,
600                               StringList &commands_help,
601                               CommandObject::CommandMap &command_map);
602 
603   // An interruptible wrapper around the stream output
604   void PrintCommandOutput(Stream &stream, llvm::StringRef str);
605 
606   bool EchoCommandNonInteractive(llvm::StringRef line,
607                                  const Flags &io_handler_flags) const;
608 
609   // A very simple state machine which models the command handling transitions
610   enum class CommandHandlingState {
611     eIdle,
612     eInProgress,
613     eInterrupted,
614   };
615 
616   std::atomic<CommandHandlingState> m_command_state{
617       CommandHandlingState::eIdle};
618 
619   int m_iohandler_nesting_level = 0;
620 
621   void StartHandlingCommand();
622   void FinishHandlingCommand();
623   bool InterruptCommand();
624 
625   Debugger &m_debugger; // The debugger session that this interpreter is
626                         // associated with
627   // Execution contexts that were temporarily set by some of HandleCommand*
628   // overloads.
629   std::stack<ExecutionContext> m_overriden_exe_contexts;
630   bool m_synchronous_execution;
631   bool m_skip_lldbinit_files;
632   bool m_skip_app_init_files;
633   CommandObject::CommandMap m_command_dict; // Stores basic built-in commands
634                                             // (they cannot be deleted, removed
635                                             // or overwritten).
636   CommandObject::CommandMap
637       m_alias_dict; // Stores user aliases/abbreviations for commands
638   CommandObject::CommandMap m_user_dict; // Stores user-defined commands
639   CommandHistory m_command_history;
640   std::string m_repeat_command; // Stores the command that will be executed for
641                                 // an empty command string.
642   lldb::IOHandlerSP m_command_io_handler_sp;
643   char m_comment_char;
644   bool m_batch_command_mode;
645   ChildrenTruncatedWarningStatus m_truncation_warning; // Whether we truncated
646                                                        // children and whether
647                                                        // the user has been told
648 
649   // FIXME: Stop using this to control adding to the history and then replace
650   // this with m_command_source_dirs.size().
651   uint32_t m_command_source_depth;
652   /// A stack of directory paths. When not empty, the last one is the directory
653   /// of the file that's currently sourced.
654   std::vector<FileSpec> m_command_source_dirs;
655   std::vector<uint32_t> m_command_source_flags;
656   CommandInterpreterRunResult m_result;
657 
658   // The exit code the user has requested when calling the 'quit' command.
659   // No value means the user hasn't set a custom exit code so far.
660   llvm::Optional<int> m_quit_exit_code;
661   // If the driver is accepts custom exit codes for the 'quit' command.
662   bool m_allow_exit_code = false;
663 
664   StreamString m_transcript_stream;
665 };
666 
667 } // namespace lldb_private
668 
669 #endif // LLDB_INTERPRETER_COMMANDINTERPRETER_H
670