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