From d5feadb1e2188886065f2205e161c174e3cad344 Mon Sep 17 00:00:00 2001 From: John Harrison Date: Thu, 30 Jan 2025 14:39:37 -0800 Subject: [PATCH] [lldb-dap] Partially reverting OutputRedirector changes. I just noticed with these changes lldb-dap was using 200% of my CPU and root causing the issue it seems that lldb_private::Pipe::Read() (without a timeout) is using a timeout of `std::chrono::microseconds::zero()` which ends up setting the SelectHelper timeout to `now + 0`. This causes the `lldb_dap::OutputRedirector::RedirectTo()` to turn into a busy loop. Additionally, the 'Read' call is not peforming parital reads and will only invoke the redirect callback once the output buffer is filled. This is not the desired behavior for lldb-dap. Instead we want a write to the FD to result in a callback to send the DAP Output event, which mean we want partial output. To mitigate this, I'm reverting the reading operation to the previous behavior before 873426bea3dd67d80dd10650e64e91c69796614f but keeping the refactored structure and thread management. --- lldb/tools/lldb-dap/OutputRedirector.cpp | 66 +++++++++++++++--------- lldb/tools/lldb-dap/OutputRedirector.h | 6 ++- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/lldb/tools/lldb-dap/OutputRedirector.cpp b/lldb/tools/lldb-dap/OutputRedirector.cpp index 8fcbcfec99c44..7935e17a653be 100644 --- a/lldb/tools/lldb-dap/OutputRedirector.cpp +++ b/lldb/tools/lldb-dap/OutputRedirector.cpp @@ -6,6 +6,9 @@ // //===----------------------------------------------------------------------===/ +#include "OutputRedirector.h" +#include "DAP.h" +#include "llvm/ADT/StringRef.h" #include "llvm/Support/Error.h" #include #if defined(_WIN32) @@ -15,45 +18,59 @@ #include #endif -#include "DAP.h" -#include "OutputRedirector.h" -#include "llvm/ADT/StringRef.h" - using lldb_private::Pipe; -using lldb_private::Status; using llvm::createStringError; using llvm::Error; using llvm::Expected; +using llvm::inconvertibleErrorCode; using llvm::StringRef; namespace lldb_dap { +int OutputRedirector::kInvalidDescriptor = -1; + +OutputRedirector::OutputRedirector() : m_fd(kInvalidDescriptor) {} + Expected OutputRedirector::GetWriteFileDescriptor() { - if (!m_pipe.CanWrite()) + if (m_fd == kInvalidDescriptor) return createStringError(std::errc::bad_file_descriptor, "write handle is not open for writing"); - return m_pipe.GetWriteFileDescriptor(); + return m_fd; } Error OutputRedirector::RedirectTo(std::function callback) { - Status status = m_pipe.CreateNew(/*child_process_inherit=*/false); - if (status.Fail()) - return status.takeError(); + assert(m_fd == kInvalidDescriptor && "Output readirector already started."); + int new_fd[2]; - m_forwarder = std::thread([this, callback]() { - char buffer[OutputBufferSize]; - while (m_pipe.CanRead() && !m_stopped) { - size_t bytes_read; - Status status = m_pipe.Read(&buffer, sizeof(buffer), bytes_read); - if (status.Fail()) - continue; +#if defined(_WIN32) + if (::_pipe(new_fd, OutputBufferSize, O_TEXT) == -1) { +#else + if (::pipe(new_fd) == -1) { +#endif + int error = errno; + return createStringError(inconvertibleErrorCode(), + "Couldn't create new pipe %s", strerror(error)); + } - // EOF detected - if (bytes_read == 0 || m_stopped) + int read_fd = new_fd[0]; + m_fd = new_fd[1]; + m_forwarder = std::thread([this, callback, read_fd]() { + char buffer[OutputBufferSize]; + while (!m_stopped) { + ssize_t bytes_count = ::read(read_fd, &buffer, sizeof(buffer)); + // EOF detected. + if (bytes_count == 0) + break; + if (bytes_count == -1) { + // Skip non-fatal errors. + if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK) + continue; break; + } - callback(StringRef(buffer, bytes_read)); + callback(StringRef(buffer, bytes_count)); } + ::close(read_fd); }); return Error::success(); @@ -62,14 +79,15 @@ Error OutputRedirector::RedirectTo(std::function callback) { void OutputRedirector::Stop() { m_stopped = true; - if (m_pipe.CanWrite()) { + if (m_fd != kInvalidDescriptor) { + int fd = m_fd; + m_fd = kInvalidDescriptor; // Closing the pipe may not be sufficient to wake up the thread in case the // write descriptor is duplicated (to stdout/err or to another process). // Write a null byte to ensure the read call returns. char buf[] = "\0"; - size_t bytes_written; - m_pipe.Write(buf, sizeof(buf), bytes_written); - m_pipe.CloseWriteFileDescriptor(); + ::write(fd, buf, sizeof(buf)); + ::close(fd); m_forwarder.join(); } } diff --git a/lldb/tools/lldb-dap/OutputRedirector.h b/lldb/tools/lldb-dap/OutputRedirector.h index 41ea05c22c691..d2bd39797f3d7 100644 --- a/lldb/tools/lldb-dap/OutputRedirector.h +++ b/lldb/tools/lldb-dap/OutputRedirector.h @@ -20,6 +20,8 @@ namespace lldb_dap { class OutputRedirector { public: + static int kInvalidDescriptor; + /// Creates writable file descriptor that will invoke the given callback on /// each write in a background thread. /// @@ -33,13 +35,13 @@ class OutputRedirector { ~OutputRedirector() { Stop(); } - OutputRedirector() = default; + OutputRedirector(); OutputRedirector(const OutputRedirector &) = delete; OutputRedirector &operator=(const OutputRedirector &) = delete; private: std::atomic m_stopped = false; - lldb_private::Pipe m_pipe; + int m_fd; std::thread m_forwarder; };