Skip to content

[lldb] Gardening in StreamAsynchronousIO (NFC) #127717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lldb/include/lldb/Core/StreamAsynchronousIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@
namespace lldb_private {
class Debugger;

/// A stream meant for asynchronously printing output. Output is buffered until
/// the stream is flushed or destroyed. Printing is handled by the currently
/// active IOHandler, or the debugger's output or error stream if there is none.
class StreamAsynchronousIO : public Stream {
public:
StreamAsynchronousIO(Debugger &debugger, bool for_stdout, bool colors);
enum ForSTDOUT : bool {
STDOUT = true,
STDERR = false,
};

StreamAsynchronousIO(Debugger &debugger, ForSTDOUT for_stdout);

~StreamAsynchronousIO() override;

Expand All @@ -32,7 +40,7 @@ class StreamAsynchronousIO : public Stream {
private:
Debugger &m_debugger;
std::string m_data;
bool m_for_stdout;
ForSTDOUT m_for_stdout;
};

} // namespace lldb_private
Expand Down
6 changes: 4 additions & 2 deletions lldb/source/Core/Debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1321,11 +1321,13 @@ bool Debugger::PopIOHandler(const IOHandlerSP &pop_reader_sp) {
}

StreamSP Debugger::GetAsyncOutputStream() {
return std::make_shared<StreamAsynchronousIO>(*this, true, GetUseColor());
return std::make_shared<StreamAsynchronousIO>(*this,
StreamAsynchronousIO::STDOUT);
}

StreamSP Debugger::GetAsyncErrorStream() {
return std::make_shared<StreamAsynchronousIO>(*this, false, GetUseColor());
return std::make_shared<StreamAsynchronousIO>(*this,
StreamAsynchronousIO::STDERR);
}

void Debugger::RequestInterrupt() {
Expand Down
12 changes: 6 additions & 6 deletions lldb/source/Core/StreamAsynchronousIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@
using namespace lldb;
using namespace lldb_private;

StreamAsynchronousIO::StreamAsynchronousIO(Debugger &debugger, bool for_stdout,
bool colors)
: Stream(0, 4, eByteOrderBig, colors), m_debugger(debugger), m_data(),
m_for_stdout(for_stdout) {}
StreamAsynchronousIO::StreamAsynchronousIO(
Debugger &debugger, StreamAsynchronousIO::ForSTDOUT for_stdout)
: Stream(0, 4, eByteOrderBig, debugger.GetUseColor()), m_debugger(debugger),
m_data(), m_for_stdout(for_stdout) {}

StreamAsynchronousIO::~StreamAsynchronousIO() {
// Flush when we destroy to make sure we display the data
// Flush when we destroy to make sure we display the data.
Flush();
}

void StreamAsynchronousIO::Flush() {
if (!m_data.empty()) {
m_debugger.PrintAsync(m_data.data(), m_data.size(), m_for_stdout);
m_data = std::string();
m_data.clear();
}
}

Expand Down