diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2022-07-04 19:20:19 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2023-02-08 19:02:26 +0000 |
commit | 81ad626541db97eb356e2c1d4a20eb2a26a766ab (patch) | |
tree | 311b6a8987c32b1e1dcbab65c54cfac3fdb56175 /contrib/llvm-project/lldb/source/Interpreter/CommandInterpreter.cpp | |
parent | 5fff09660e06a66bed6482da9c70df328e16bbb6 (diff) | |
parent | 145449b1e420787bb99721a429341fa6be3adfb6 (diff) |
Diffstat (limited to 'contrib/llvm-project/lldb/source/Interpreter/CommandInterpreter.cpp')
-rw-r--r-- | contrib/llvm-project/lldb/source/Interpreter/CommandInterpreter.cpp | 117 |
1 files changed, 80 insertions, 37 deletions
diff --git a/contrib/llvm-project/lldb/source/Interpreter/CommandInterpreter.cpp b/contrib/llvm-project/lldb/source/Interpreter/CommandInterpreter.cpp index 59c23716bf89..6ef209b20fc6 100644 --- a/contrib/llvm-project/lldb/source/Interpreter/CommandInterpreter.cpp +++ b/contrib/llvm-project/lldb/source/Interpreter/CommandInterpreter.cpp @@ -45,6 +45,7 @@ #include "lldb/Core/Debugger.h" #include "lldb/Core/PluginManager.h" #include "lldb/Core/StreamFile.h" +#include "lldb/Utility/LLDBLog.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/Reproducer.h" #include "lldb/Utility/State.h" @@ -83,6 +84,10 @@ #include "llvm/Support/PrettyStackTrace.h" #include "llvm/Support/ScopedPrinter.h" +#if defined(__APPLE__) +#include <TargetConditionals.h> +#endif + using namespace lldb; using namespace lldb_private; @@ -123,7 +128,8 @@ CommandInterpreter::CommandInterpreter(Debugger &debugger, m_debugger(debugger), m_synchronous_execution(true), m_skip_lldbinit_files(false), m_skip_app_init_files(false), m_comment_char('#'), m_batch_command_mode(false), - m_truncation_warning(eNoTruncation), m_command_source_depth(0) { + m_truncation_warning(eNoOmission), m_max_depth_warning(eNoOmission), + m_command_source_depth(0) { SetEventName(eBroadcastBitThreadShouldExit, "thread-should-exit"); SetEventName(eBroadcastBitResetPrompt, "reset-prompt"); SetEventName(eBroadcastBitQuitCommandReceived, "quit"); @@ -206,7 +212,7 @@ bool CommandInterpreter::SetQuitExitCode(int exit_code) { } int CommandInterpreter::GetQuitExitCode(bool &exited) const { - exited = m_quit_exit_code.hasValue(); + exited = m_quit_exit_code.has_value(); if (exited) return *m_quit_exit_code; return 0; @@ -239,6 +245,12 @@ bool CommandInterpreter::GetRepeatPreviousCommand() const { nullptr, idx, g_interpreter_properties[idx].default_uint_value != 0); } +bool CommandInterpreter::GetRequireCommandOverwrite() const { + const uint32_t idx = ePropertyRequireCommandOverwrite; + return m_collection_sp->GetPropertyAtIndexAsBoolean( + nullptr, idx, g_interpreter_properties[idx].default_uint_value != 0); +} + void CommandInterpreter::Initialize() { LLDB_SCOPED_TIMER(); @@ -433,7 +445,7 @@ void CommandInterpreter::Initialize() { if (cmd_obj_sp) { alias_arguments_vector_sp = std::make_shared<OptionArgVector>(); #if defined(__APPLE__) -#if defined(TARGET_OS_IPHONE) +#if TARGET_OS_IPHONE AddAlias("r", cmd_obj_sp, "--"); AddAlias("run", cmd_obj_sp, "--"); #else @@ -1434,7 +1446,7 @@ void CommandInterpreter::GetHelp(CommandReturnObject &result, result.AppendMessage("Current user-defined container commands:"); result.AppendMessage(""); max_len = FindLongestCommandWord(m_user_mw_dict); - for (pos = m_user_dict.begin(); pos != m_user_mw_dict.end(); ++pos) { + for (pos = m_user_mw_dict.begin(); pos != m_user_mw_dict.end(); ++pos) { OutputFormattedHelpText(result.GetOutputStream(), pos->first, "--", pos->second->GetHelp(), max_len); } @@ -1825,7 +1837,7 @@ bool CommandInterpreter::HandleCommand(const char *command_line, std::string command_string(command_line); std::string original_command_string(command_line); - Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_COMMANDS)); + Log *log = GetLog(LLDBLog::Commands); llvm::PrettyStackTraceFormat stack_trace("HandleCommand(command = \"%s\")", command_line); @@ -1943,16 +1955,21 @@ bool CommandInterpreter::HandleCommand(const char *command_line, // arguments. if (cmd_obj != nullptr) { - if (add_to_history) { + // If we got here when empty_command was true, then this command is a + // stored "repeat command" which we should give a chance to produce it's + // repeat command, even though we don't add repeat commands to the history. + if (add_to_history || empty_command) { Args command_args(command_string); - const char *repeat_command = cmd_obj->GetRepeatCommand(command_args, 0); - if (repeat_command != nullptr) - m_repeat_command.assign(repeat_command); + llvm::Optional<std::string> repeat_command = + cmd_obj->GetRepeatCommand(command_args, 0); + if (repeat_command) + m_repeat_command.assign(*repeat_command); else m_repeat_command.assign(original_command_string); + } + if (add_to_history) m_command_history.AppendString(original_command_string); - } std::string remainder; const std::size_t actual_cmd_name_len = cmd_obj->GetCommandName().size(); @@ -2374,6 +2391,21 @@ void CommandInterpreter::SourceInitFileHome(CommandReturnObject &result, SourceInitFile(FileSpec(init_file.str()), result); } +void CommandInterpreter::SourceInitFileGlobal(CommandReturnObject &result) { +#ifdef LLDB_GLOBAL_INIT_DIRECTORY + if (!m_skip_lldbinit_files) { + FileSpec init_file(LLDB_GLOBAL_INIT_DIRECTORY); + if (init_file) + init_file.MakeAbsolute(HostInfo::GetShlibDir()); + + init_file.AppendPathComponent("lldbinit"); + SourceInitFile(init_file, result); + return; + } +#endif + result.SetStatus(eReturnStatusSuccessFinishNoResult); +} + const char *CommandInterpreter::GetCommandPrefix() { const char *prefix = GetDebugger().GetIOHandlerCommandPrefix(); return prefix == nullptr ? "" : prefix; @@ -2950,28 +2982,27 @@ bool CommandInterpreter::WasInterrupted() const { return was_interrupted; } -void CommandInterpreter::PrintCommandOutput(Stream &stream, - llvm::StringRef str) { +void CommandInterpreter::PrintCommandOutput(IOHandler &io_handler, + llvm::StringRef str, + bool is_stdout) { + + lldb::StreamFileSP stream = is_stdout ? io_handler.GetOutputStreamFileSP() + : io_handler.GetErrorStreamFileSP(); // Split the output into lines and poll for interrupt requests - const char *data = str.data(); - size_t size = str.size(); - while (size > 0 && !WasInterrupted()) { - size_t chunk_size = 0; - for (; chunk_size < size; ++chunk_size) { - lldbassert(data[chunk_size] != '\0'); - if (data[chunk_size] == '\n') { - ++chunk_size; - break; - } + while (!str.empty() && !WasInterrupted()) { + llvm::StringRef line; + std::tie(line, str) = str.split('\n'); + { + std::lock_guard<std::recursive_mutex> guard(io_handler.GetOutputMutex()); + stream->Write(line.data(), line.size()); + stream->Write("\n", 1); } - chunk_size = stream.Write(data, chunk_size); - lldbassert(size >= chunk_size); - data += chunk_size; - size -= chunk_size; - } - if (size > 0) { - stream.Printf("\n... Interrupted.\n"); } + + std::lock_guard<std::recursive_mutex> guard(io_handler.GetOutputMutex()); + if (!str.empty()) + stream->Printf("\n... Interrupted.\n"); + stream->Flush(); } bool CommandInterpreter::EchoCommandNonInteractive( @@ -3007,16 +3038,24 @@ void CommandInterpreter::IOHandlerInputComplete(IOHandler &io_handler, // When using a non-interactive file handle (like when sourcing commands // from a file) we need to echo the command out so we don't just see the // command output and no command... - if (EchoCommandNonInteractive(line, io_handler.GetFlags())) + if (EchoCommandNonInteractive(line, io_handler.GetFlags())) { + std::lock_guard<std::recursive_mutex> guard(io_handler.GetOutputMutex()); io_handler.GetOutputStreamFileSP()->Printf( "%s%s\n", io_handler.GetPrompt(), line.c_str()); + } } StartHandlingCommand(); - OverrideExecutionContext(m_debugger.GetSelectedExecutionContext()); - auto finalize = llvm::make_scope_exit([this]() { - RestoreExecutionContext(); + ExecutionContext exe_ctx = m_debugger.GetSelectedExecutionContext(); + bool pushed_exe_ctx = false; + if (exe_ctx.HasTargetScope()) { + OverrideExecutionContext(exe_ctx); + pushed_exe_ctx = true; + } + auto finalize = llvm::make_scope_exit([this, pushed_exe_ctx]() { + if (pushed_exe_ctx) + RestoreExecutionContext(); }); lldb_private::CommandReturnObject result(m_debugger.GetUseColor()); @@ -3031,13 +3070,13 @@ void CommandInterpreter::IOHandlerInputComplete(IOHandler &io_handler, if (!result.GetImmediateOutputStream()) { llvm::StringRef output = result.GetOutputData(); - PrintCommandOutput(*io_handler.GetOutputStreamFileSP(), output); + PrintCommandOutput(io_handler, output, true); } // Now emit the command error text from the command we just executed if (!result.GetImmediateErrorStream()) { llvm::StringRef error = result.GetErrorData(); - PrintCommandOutput(*io_handler.GetErrorStreamFileSP(), error); + PrintCommandOutput(io_handler, error, false); } } @@ -3122,8 +3161,8 @@ bool CommandInterpreter::SaveTranscript( } auto error_out = [&](llvm::StringRef error_message, std::string description) { - LLDB_LOG(GetLogIfAllCategoriesSet(LIBLLDB_LOG_COMMANDS), "{0} ({1}:{2})", - error_message, output_file, description); + LLDB_LOG(GetLog(LLDBLog::Commands), "{0} ({1}:{2})", error_message, + output_file, description); result.AppendErrorWithFormatv( "Failed to save session's transcripts to {0}!", *output_file); return false; @@ -3156,6 +3195,10 @@ bool CommandInterpreter::SaveTranscript( return true; } +bool CommandInterpreter::IsInteractive() { + return (GetIOHandler() ? GetIOHandler()->GetIsInteractive() : false); +} + FileSpec CommandInterpreter::GetCurrentSourceDir() { if (m_command_source_dirs.empty()) return {}; |