Coverage Report

Created: 2022-01-25 06:29

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/include/lldb/Interpreter/CommandInterpreter.h
Line
Count
Source (jump to first uncovered line)
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
6.40k
  CommandInterpreterRunResult() = default;
37
38
9
  uint32_t GetNumErrors() const { return m_num_errors; }
39
40
882
  lldb::CommandInterpreterResult GetResult() const { return m_result; }
41
42
6.11k
  bool IsResult(lldb::CommandInterpreterResult result) {
43
6.11k
    return m_result == result;
44
6.11k
  }
45
46
protected:
47
  friend CommandInterpreter;
48
49
75
  void IncrementNumberOfErrors() { m_num_errors++; }
50
51
145
  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.
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
0
        m_print_errors(print_errors), m_add_to_history(add_to_history) {}
102
103
958
  CommandInterpreterRunOptions() = default;
104
105
7
  void SetSilent(bool silent) {
106
7
    LazyBool value = silent ? eLazyBoolNo : 
eLazyBoolYes0
;
107
108
7
    m_print_results = value;
109
7
    m_print_errors = value;
110
7
    m_echo_commands = value;
111
7
    m_echo_comment_commands = value;
112
7
    m_add_to_history = value;
113
7
  }
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.
119
21
  bool GetStopOnContinue() const { return DefaultToNo(m_stop_on_continue); }
120
121
21
  void SetStopOnContinue(bool stop_on_continue) {
122
21
    m_stop_on_continue = stop_on_continue ? eLazyBoolYes : 
eLazyBoolNo0
;
123
21
  }
124
125
3
  bool GetStopOnError() const { return DefaultToNo(m_stop_on_error); }
126
127
375
  void SetStopOnError(bool stop_on_error) {
128
375
    m_stop_on_error = stop_on_error ? 
eLazyBoolYes368
:
eLazyBoolNo7
;
129
375
  }
130
131
488
  bool GetStopOnCrash() const { return DefaultToNo(m_stop_on_crash); }
132
133
348
  void SetStopOnCrash(bool stop_on_crash) {
134
348
    m_stop_on_crash = stop_on_crash ? 
eLazyBoolYes187
:
eLazyBoolNo161
;
135
348
  }
136
137
21
  bool GetEchoCommands() const { return DefaultToYes(m_echo_commands); }
138
139
829
  void SetEchoCommands(bool echo_commands) {
140
829
    m_echo_commands = echo_commands ? 
eLazyBoolYes807
:
eLazyBoolNo22
;
141
829
  }
142
143
0
  bool GetEchoCommentCommands() const {
144
0
    return DefaultToYes(m_echo_comment_commands);
145
0
  }
146
147
459
  void SetEchoCommentCommands(bool echo_comments) {
148
459
    m_echo_comment_commands = echo_comments ? 
eLazyBoolYes330
:
eLazyBoolNo129
;
149
459
  }
150
151
21
  bool GetPrintResults() const { return DefaultToYes(m_print_results); }
152
153
482
  void SetPrintResults(bool print_results) {
154
482
    m_print_results = print_results ? 
eLazyBoolYes481
:
eLazyBoolNo1
;
155
482
  }
156
157
3
  bool GetPrintErrors() const { return DefaultToYes(m_print_errors); }
158
159
484
  void SetPrintErrors(bool print_errors) {
160
484
    m_print_errors = print_errors ? 
eLazyBoolYes483
:
eLazyBoolNo1
;
161
484
  }
162
163
39
  bool GetAddToHistory() const { return DefaultToYes(m_add_to_history); }
164
165
23
  void SetAddToHistory(bool add_to_history) {
166
23
    m_add_to_history = add_to_history ? 
eLazyBoolYes0
: eLazyBoolNo;
167
23
  }
168
169
908
  bool GetAutoHandleEvents() const {
170
908
    return DefaultToYes(m_auto_handle_events);
171
908
  }
172
173
454
  void SetAutoHandleEvents(bool auto_handle_events) {
174
454
    m_auto_handle_events = auto_handle_events ? 
eLazyBoolYes453
:
eLazyBoolNo1
;
175
454
  }
176
177
455
  bool GetSpawnThread() const { return DefaultToNo(m_spawn_thread); }
178
179
454
  void SetSpawnThread(bool spawn_thread) {
180
454
    m_spawn_thread = spawn_thread ? 
eLazyBoolYes2
:
eLazyBoolNo452
;
181
454
  }
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:
195
992
  static bool DefaultToYes(LazyBool flag) {
196
992
    switch (flag) {
197
54
    case eLazyBoolNo:
198
54
      return false;
199
938
    default:
200
938
      return true;
201
992
    }
202
992
  }
203
204
967
  static bool DefaultToNo(LazyBool flag) {
205
967
    switch (flag) {
206
23
    case eLazyBoolYes:
207
23
      return true;
208
944
    default:
209
944
      return false;
210
967
    }
211
967
  }
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
    eCommandTypesUserMW  = 0x0004, //< multiword commands (command containers)
237
    eCommandTypesAliases = 0x0008, //< aliases such as "po"
238
    eCommandTypesHidden  = 0x0010, //< commands prefixed with an underscore
239
    eCommandTypesAllThem = 0xFFFF  //< all commands
240
  };
241
242
  CommandInterpreter(Debugger &debugger, bool synchronous_execution);
243
244
5.94k
  ~CommandInterpreter() override = default;
245
246
  // These two functions fill out the Broadcaster interface:
247
248
  static ConstString &GetStaticBroadcasterClass();
249
250
454
  ConstString &GetBroadcasterClass() const override {
251
454
    return GetStaticBroadcasterClass();
252
454
  }
253
254
  void SourceInitFileCwd(CommandReturnObject &result);
255
  void SourceInitFileHome(CommandReturnObject &result, bool is_repl = false);
256
257
  bool AddCommand(llvm::StringRef name, const lldb::CommandObjectSP &cmd_sp,
258
                  bool can_replace);
259
260
  Status AddUserCommand(llvm::StringRef name,
261
                        const lldb::CommandObjectSP &cmd_sp, bool can_replace);
262
263
  lldb::CommandObjectSP GetCommandSPExact(llvm::StringRef cmd,
264
                                          bool include_aliases = false) const;
265
266
  CommandObject *GetCommandObject(llvm::StringRef cmd,
267
                                  StringList *matches = nullptr,
268
                                  StringList *descriptions = nullptr) const;
269
270
  CommandObject *GetUserCommandObject(llvm::StringRef cmd,
271
                                      StringList *matches = nullptr,
272
                                      StringList *descriptions = nullptr) const;
273
274
  /// Determine whether a root level, built-in command with this name exists.
275
  bool CommandExists(llvm::StringRef cmd) const;
276
277
  /// Determine whether an alias command with this name exists
278
  bool AliasExists(llvm::StringRef cmd) const;
279
280
  /// Determine whether a root-level user command with this name exists.
281
  bool UserCommandExists(llvm::StringRef cmd) const;
282
283
  /// Determine whether a root-level user multiword command with this name
284
  /// exists.
285
  bool UserMultiwordCommandExists(llvm::StringRef cmd) const;
286
287
  /// Look up the command pointed to by path encoded in the arguments of
288
  /// the incoming command object.  If all the path components exist
289
  /// and are all actual commands - not aliases, and the leaf command is a
290
  /// multiword command, return the command.  Otherwise return nullptr, and put
291
  /// a useful diagnostic in the Status object.
292
  ///
293
  /// \param[in] path
294
  ///    An Args object holding the path in its arguments
295
  /// \param[in] leaf_is_command
296
  ///    If true, return the container of the leaf name rather than looking up
297
  ///    the whole path as a leaf command.  The leaf needn't exist in this case.
298
  /// \param[in,out] result
299
  ///    If the path is not found, this error shows where we got off track.
300
  /// \return
301
  ///    If found, a pointer to the CommandObjectMultiword pointed to by path,
302
  ///    or to the container of the leaf element is is_leaf_command.
303
  ///    Returns nullptr under two circumstances:
304
  ///      1) The command in not found (check error.Fail)
305
  ///      2) is_leaf is true and the path has only a leaf.  We don't have a
306
  ///         dummy "contains everything MWC, so we return null here, but
307
  ///         in this case error.Success is true.
308
309
  CommandObjectMultiword *VerifyUserMultiwordCmdPath(Args &path,
310
                                                     bool leaf_is_command,
311
                                                     Status &result);
312
313
  CommandAlias *AddAlias(llvm::StringRef alias_name,
314
                         lldb::CommandObjectSP &command_obj_sp,
315
                         llvm::StringRef args_string = llvm::StringRef());
316
317
  // Remove a command if it is removable (python or regex command)
318
  bool RemoveCommand(llvm::StringRef cmd);
319
320
  bool RemoveAlias(llvm::StringRef alias_name);
321
322
  bool GetAliasFullName(llvm::StringRef cmd, std::string &full_name) const;
323
324
  bool RemoveUserMultiword(llvm::StringRef multiword_name);
325
326
  // Do we want to allow top-level user multiword commands to be deleted?
327
0
  void RemoveAllUserMultiword() { m_user_mw_dict.clear(); }
328
329
  bool RemoveUser(llvm::StringRef alias_name);
330
331
2
  void RemoveAllUser() { m_user_dict.clear(); }
332
333
  const CommandAlias *GetAlias(llvm::StringRef alias_name) const;
334
335
  CommandObject *BuildAliasResult(llvm::StringRef alias_name,
336
                                  std::string &raw_input_string,
337
                                  std::string &alias_result,
338
                                  CommandReturnObject &result);
339
340
  bool HandleCommand(const char *command_line, LazyBool add_to_history,
341
                     const ExecutionContext &override_context,
342
                     CommandReturnObject &result);
343
344
  bool HandleCommand(const char *command_line, LazyBool add_to_history,
345
                     CommandReturnObject &result);
346
347
  bool WasInterrupted() const;
348
349
  /// Execute a list of commands in sequence.
350
  ///
351
  /// \param[in] commands
352
  ///    The list of commands to execute.
353
  /// \param[in,out] context
354
  ///    The execution context in which to run the commands.
355
  /// \param[in] options
356
  ///    This object holds the options used to control when to stop, whether to
357
  ///    execute commands,
358
  ///    etc.
359
  /// \param[out] result
360
  ///    This is marked as succeeding with no output if all commands execute
361
  ///    safely,
362
  ///    and failed with some explanation if we aborted executing the commands
363
  ///    at some point.
364
  void HandleCommands(const StringList &commands,
365
                      const ExecutionContext &context,
366
                      const CommandInterpreterRunOptions &options,
367
                      CommandReturnObject &result);
368
369
  void HandleCommands(const StringList &commands,
370
                      const CommandInterpreterRunOptions &options,
371
                      CommandReturnObject &result);
372
373
  /// Execute a list of commands from a file.
374
  ///
375
  /// \param[in] file
376
  ///    The file from which to read in commands.
377
  /// \param[in,out] context
378
  ///    The execution context in which to run the commands.
379
  /// \param[in] options
380
  ///    This object holds the options used to control when to stop, whether to
381
  ///    execute commands,
382
  ///    etc.
383
  /// \param[out] result
384
  ///    This is marked as succeeding with no output if all commands execute
385
  ///    safely,
386
  ///    and failed with some explanation if we aborted executing the commands
387
  ///    at some point.
388
  void HandleCommandsFromFile(FileSpec &file, const ExecutionContext &context,
389
                              const CommandInterpreterRunOptions &options,
390
                              CommandReturnObject &result);
391
392
  void HandleCommandsFromFile(FileSpec &file,
393
                              const CommandInterpreterRunOptions &options,
394
                              CommandReturnObject &result);
395
396
  CommandObject *GetCommandObjectForCommand(llvm::StringRef &command_line);
397
398
  /// Returns the auto-suggestion string that should be added to the given
399
  /// command line.
400
  llvm::Optional<std::string> GetAutoSuggestionForCommand(llvm::StringRef line);
401
402
  // This handles command line completion.
403
  void HandleCompletion(CompletionRequest &request);
404
405
  // This version just returns matches, and doesn't compute the substring. It
406
  // is here so the Help command can call it for the first argument.
407
  void HandleCompletionMatches(CompletionRequest &request);
408
409
  int GetCommandNamesMatchingPartialString(const char *cmd_cstr,
410
                                           bool include_aliases,
411
                                           StringList &matches,
412
                                           StringList &descriptions);
413
414
  void GetHelp(CommandReturnObject &result,
415
               uint32_t types = eCommandTypesAllThem);
416
417
  void GetAliasHelp(const char *alias_name, StreamString &help_string);
418
419
  void OutputFormattedHelpText(Stream &strm, llvm::StringRef prefix,
420
                               llvm::StringRef help_text);
421
422
  void OutputFormattedHelpText(Stream &stream, llvm::StringRef command_word,
423
                               llvm::StringRef separator,
424
                               llvm::StringRef help_text, size_t max_word_len);
425
426
  // this mimics OutputFormattedHelpText but it does perform a much simpler
427
  // formatting, basically ensuring line alignment. This is only good if you
428
  // have some complicated layout for your help text and want as little help as
429
  // reasonable in properly displaying it. Most of the times, you simply want
430
  // to type some text and have it printed in a reasonable way on screen. If
431
  // so, use OutputFormattedHelpText
432
  void OutputHelpText(Stream &stream, llvm::StringRef command_word,
433
                      llvm::StringRef separator, llvm::StringRef help_text,
434
                      uint32_t max_word_len);
435
436
135k
  Debugger &GetDebugger() { return m_debugger; }
437
438
  ExecutionContext GetExecutionContext() const;
439
440
  lldb::PlatformSP GetPlatform(bool prefer_target_platform);
441
442
  const char *ProcessEmbeddedScriptCommands(const char *arg);
443
444
  void UpdatePrompt(llvm::StringRef prompt);
445
446
  bool Confirm(llvm::StringRef message, bool default_answer);
447
448
  void LoadCommandDictionary();
449
450
  void Initialize();
451
452
  void Clear();
453
454
  bool HasCommands() const;
455
456
  bool HasAliases() const;
457
458
  bool HasUserCommands() const;
459
460
  bool HasUserMultiwordCommands() const;
461
462
  bool HasAliasOptions() const;
463
464
  void BuildAliasCommandArgs(CommandObject *alias_cmd_obj,
465
                             const char *alias_name, Args &cmd_args,
466
                             std::string &raw_input_string,
467
                             CommandReturnObject &result);
468
469
  /// Picks the number out of a string of the form "%NNN", otherwise return 0.
470
  int GetOptionArgumentPosition(const char *in_string);
471
472
8.53k
  void SkipLLDBInitFiles(bool skip_lldbinit_files) {
473
8.53k
    m_skip_lldbinit_files = skip_lldbinit_files;
474
8.53k
  }
475
476
8.53k
  void SkipAppInitFiles(bool skip_app_init_files) {
477
8.53k
    m_skip_app_init_files = skip_app_init_files;
478
8.53k
  }
479
480
  bool GetSynchronous();
481
482
  void FindCommandsForApropos(llvm::StringRef word, StringList &commands_found,
483
                              StringList &commands_help,
484
                              bool search_builtin_commands,
485
                              bool search_user_commands,
486
                              bool search_alias_commands,
487
                              bool search_user_mw_commands);
488
489
6
  bool GetBatchCommandMode() { return m_batch_command_mode; }
490
491
4
  bool SetBatchCommandMode(bool value) {
492
4
    const bool old_value = m_batch_command_mode;
493
4
    m_batch_command_mode = value;
494
4
    return old_value;
495
4
  }
496
497
45
  void ChildrenTruncated() {
498
45
    if (m_truncation_warning == eNoTruncation)
499
12
      m_truncation_warning = eUnwarnedTruncation;
500
45
  }
501
502
5.81k
  bool TruncationWarningNecessary() {
503
5.81k
    return (m_truncation_warning == eUnwarnedTruncation);
504
5.81k
  }
505
506
6
  void TruncationWarningGiven() { m_truncation_warning = eWarnedTruncation; }
507
508
6
  const char *TruncationWarningText() {
509
6
    return "*** Some of your variables have more members than the debugger "
510
6
           "will show by default. To show all of them, you can either use the "
511
6
           "--show-all-children option to %s or raise the limit by changing "
512
6
           "the target.max-children-count setting.\n";
513
6
  }
514
515
21
  CommandHistory &GetCommandHistory() { return m_command_history; }
516
517
  bool IsActive();
518
519
  CommandInterpreterRunResult
520
  RunCommandInterpreter(CommandInterpreterRunOptions &options);
521
522
  void GetLLDBCommandsFromIOHandler(const char *prompt,
523
                                    IOHandlerDelegate &delegate,
524
                                    void *baton = nullptr);
525
526
  void GetPythonCommandsFromIOHandler(const char *prompt,
527
                                      IOHandlerDelegate &delegate,
528
                                      void *baton = nullptr);
529
530
  const char *GetCommandPrefix();
531
532
  // Properties
533
  bool GetExpandRegexAliases() const;
534
535
  bool GetPromptOnQuit() const;
536
  void SetPromptOnQuit(bool enable);
537
538
  bool GetSaveSessionOnQuit() const;
539
  void SetSaveSessionOnQuit(bool enable);
540
541
  FileSpec GetSaveSessionDirectory() const;
542
  void SetSaveSessionDirectory(llvm::StringRef path);
543
544
  bool GetEchoCommands() const;
545
  void SetEchoCommands(bool enable);
546
547
  bool GetEchoCommentCommands() const;
548
  void SetEchoCommentCommands(bool enable);
549
550
  bool GetRepeatPreviousCommand() const;
551
552
2
  const CommandObject::CommandMap &GetUserCommands() const {
553
2
    return m_user_dict;
554
2
  }
555
556
2
  const CommandObject::CommandMap &GetUserMultiwordCommands() const {
557
2
    return m_user_mw_dict;
558
2
  }
559
560
1
  const CommandObject::CommandMap &GetCommands() const {
561
1
    return m_command_dict;
562
1
  }
563
564
1
  const CommandObject::CommandMap &GetAliases() const { return m_alias_dict; }
565
566
  /// Specify if the command interpreter should allow that the user can
567
  /// specify a custom exit code when calling 'quit'.
568
  void AllowExitCodeOnQuit(bool allow);
569
570
  /// Sets the exit code for the quit command.
571
  /// \param[in] exit_code
572
  ///     The exit code that the driver should return on exit.
573
  /// \return True if the exit code was successfully set; false if the
574
  ///         interpreter doesn't allow custom exit codes.
575
  /// \see AllowExitCodeOnQuit
576
  LLVM_NODISCARD bool SetQuitExitCode(int exit_code);
577
578
  /// Returns the exit code that the user has specified when running the
579
  /// 'quit' command.
580
  /// \param[out] exited
581
  ///     Set to true if the user has called quit with a custom exit code.
582
  int GetQuitExitCode(bool &exited) const;
583
584
  void ResolveCommand(const char *command_line, CommandReturnObject &result);
585
586
  bool GetStopCmdSourceOnError() const;
587
588
  lldb::IOHandlerSP
589
  GetIOHandler(bool force_create = false,
590
               CommandInterpreterRunOptions *options = nullptr);
591
592
  bool GetSpaceReplPrompts() const;
593
594
  /// Save the current debugger session transcript to a file on disk.
595
  /// \param output_file
596
  ///     The file path to which the session transcript will be written. Since
597
  ///     the argument is optional, an arbitrary temporary file will be create
598
  ///     when no argument is passed.
599
  /// \param result
600
  ///     This is used to pass function output and error messages.
601
  /// \return \b true if the session transcript was successfully written to
602
  /// disk, \b false otherwise.
603
  bool SaveTranscript(CommandReturnObject &result,
604
                      llvm::Optional<std::string> output_file = llvm::None);
605
606
  FileSpec GetCurrentSourceDir();
607
608
protected:
609
  friend class Debugger;
610
611
  // IOHandlerDelegate functions
612
  void IOHandlerInputComplete(IOHandler &io_handler,
613
                              std::string &line) override;
614
615
0
  ConstString IOHandlerGetControlSequence(char ch) override {
616
0
    if (ch == 'd')
617
0
      return ConstString("quit\n");
618
0
    return ConstString();
619
0
  }
620
621
  bool IOHandlerInterrupt(IOHandler &io_handler) override;
622
623
  void GetProcessOutput();
624
625
  bool DidProcessStopAbnormally() const;
626
627
  void SetSynchronous(bool value);
628
629
  lldb::CommandObjectSP GetCommandSP(llvm::StringRef cmd,
630
                                     bool include_aliases = true,
631
                                     bool exact = true,
632
                                     StringList *matches = nullptr,
633
                                     StringList *descriptions = nullptr) const;
634
635
private:
636
  void OverrideExecutionContext(const ExecutionContext &override_context);
637
638
  void RestoreExecutionContext();
639
640
  Status PreprocessCommand(std::string &command);
641
642
  void SourceInitFile(FileSpec file, CommandReturnObject &result);
643
644
  // Completely resolves aliases and abbreviations, returning a pointer to the
645
  // final command object and updating command_line to the fully substituted
646
  // and translated command.
647
  CommandObject *ResolveCommandImpl(std::string &command_line,
648
                                    CommandReturnObject &result);
649
650
  void FindCommandsForApropos(llvm::StringRef word, StringList &commands_found,
651
                              StringList &commands_help,
652
                              const CommandObject::CommandMap &command_map);
653
654
  // An interruptible wrapper around the stream output
655
  void PrintCommandOutput(Stream &stream, llvm::StringRef str);
656
657
  bool EchoCommandNonInteractive(llvm::StringRef line,
658
                                 const Flags &io_handler_flags) const;
659
660
  // A very simple state machine which models the command handling transitions
661
  enum class CommandHandlingState {
662
    eIdle,
663
    eInProgress,
664
    eInterrupted,
665
  };
666
667
  std::atomic<CommandHandlingState> m_command_state{
668
      CommandHandlingState::eIdle};
669
670
  int m_iohandler_nesting_level = 0;
671
672
  void StartHandlingCommand();
673
  void FinishHandlingCommand();
674
  bool InterruptCommand();
675
676
  Debugger &m_debugger; // The debugger session that this interpreter is
677
                        // associated with
678
  // Execution contexts that were temporarily set by some of HandleCommand*
679
  // overloads.
680
  std::stack<ExecutionContext> m_overriden_exe_contexts;
681
  bool m_synchronous_execution;
682
  bool m_skip_lldbinit_files;
683
  bool m_skip_app_init_files;
684
  CommandObject::CommandMap m_command_dict; // Stores basic built-in commands
685
                                            // (they cannot be deleted, removed
686
                                            // or overwritten).
687
  CommandObject::CommandMap
688
      m_alias_dict; // Stores user aliases/abbreviations for commands
689
  CommandObject::CommandMap m_user_dict; // Stores user-defined commands
690
  CommandObject::CommandMap
691
      m_user_mw_dict; // Stores user-defined multiword commands
692
  CommandHistory m_command_history;
693
  std::string m_repeat_command; // Stores the command that will be executed for
694
                                // an empty command string.
695
  lldb::IOHandlerSP m_command_io_handler_sp;
696
  char m_comment_char;
697
  bool m_batch_command_mode;
698
  ChildrenTruncatedWarningStatus m_truncation_warning; // Whether we truncated
699
                                                       // children and whether
700
                                                       // the user has been told
701
702
  // FIXME: Stop using this to control adding to the history and then replace
703
  // this with m_command_source_dirs.size().
704
  uint32_t m_command_source_depth;
705
  /// A stack of directory paths. When not empty, the last one is the directory
706
  /// of the file that's currently sourced.
707
  std::vector<FileSpec> m_command_source_dirs;
708
  std::vector<uint32_t> m_command_source_flags;
709
  CommandInterpreterRunResult m_result;
710
711
  // The exit code the user has requested when calling the 'quit' command.
712
  // No value means the user hasn't set a custom exit code so far.
713
  llvm::Optional<int> m_quit_exit_code;
714
  // If the driver is accepts custom exit codes for the 'quit' command.
715
  bool m_allow_exit_code = false;
716
717
  StreamString m_transcript_stream;
718
};
719
720
} // namespace lldb_private
721
722
#endif // LLDB_INTERPRETER_COMMANDINTERPRETER_H