LLDB mainline
lldb_private::PseudoConsole Class Reference

#include <PseudoConsole.h>

Public Types

enum class  Mode { ConPTY , Pipe , None }

Public Member Functions

 PseudoConsole ()=default
 ~PseudoConsole ()
 PseudoConsole (const PseudoConsole &)=delete
 PseudoConsole (PseudoConsole &&)=delete
PseudoConsoleoperator= (const PseudoConsole &)=delete
PseudoConsoleoperator= (PseudoConsole &&)=delete
llvm::Error CreateOverlappedPipePair (HANDLE &out_read, HANDLE &out_write, bool inheritable)
 Creates a named pipe pair for overlapped I/O.
llvm::Error OpenPseudoConsole ()
 Creates and opens a new ConPTY instance with a default console size of 80x25.
llvm::Error OpenAnonymousPipes ()
 Creates a pair of anonymous pipes to use for stdio instead of a ConPTY.
void Close ()
 Closes the ConPTY and invalidates its handle, without closing the STDIN and STDOUT pipes.
void ClosePseudoConsolePipes ()
 Closes the STDIN and STDOUT pipe handles and invalidates them.
void CloseAnonymousPipes ()
 Closes the child-side pipe handles (stdin read end and stdout/stderr write end) that were passed to CreateProcessW.
bool IsConnected () const
 Returns whether the ConPTY and its pipes are currently open and valid.
HPCON GetPseudoTerminalHandle ()
 The ConPTY HPCON handle accessor.
HANDLE GetSTDOUTHandle () const
 The STDOUT read HANDLE accessor.
HANDLE GetSTDINHandle () const
 The STDIN write HANDLE accessor.
HANDLE GetChildStdinHandle () const
 The child-side stdin read HANDLE (pipe mode only).
HANDLE GetChildStdoutHandle () const
 The child-side stdout/stderr write HANDLE (pipe mode only).
Mode GetMode () const
llvm::Error DrainInitSequences ()
 Drains initialization sequences from the ConPTY output pipe.
std::mutex & GetMutex ()
 Returns a reference to the mutex used to synchronize access to the ConPTY state.
std::condition_variable & GetCV ()
 Returns a reference to the condition variable used to signal state changes to threads waiting on the ConPTY (e.g.
bool IsStopping () const
 Returns whether the ConPTY is in the process of shutting down.
void SetStopping (bool value)
 Sets the stopping flag to value, signalling to threads waiting on the ConPTY that they should stop.

Protected Attributes

HANDLE m_conpty_handle = ((HANDLE)(long long)-1)
HANDLE m_conpty_output = ((HANDLE)(long long)-1)
HANDLE m_conpty_input = ((HANDLE)(long long)-1)
HANDLE m_pipe_child_stdin = ((HANDLE)(long long)-1)
HANDLE m_pipe_child_stdout = ((HANDLE)(long long)-1)
Mode m_mode = Mode::None
std::mutex m_mutex {}
std::condition_variable m_cv {}
std::atomic< boolm_stopping = false

Detailed Description

Definition at line 24 of file PseudoConsole.h.

Member Enumeration Documentation

◆ Mode

Enumerator
ConPTY 
Pipe 
None 

Definition at line 27 of file PseudoConsole.h.

Constructor & Destructor Documentation

◆ PseudoConsole() [1/3]

lldb_private::PseudoConsole::PseudoConsole ( )
default

◆ ~PseudoConsole()

PseudoConsole::~PseudoConsole ( )

Definition at line 96 of file PseudoConsole.cpp.

References Close(), CloseAnonymousPipes(), and ClosePseudoConsolePipes().

◆ PseudoConsole() [2/3]

lldb_private::PseudoConsole::PseudoConsole ( const PseudoConsole & )
delete

References PseudoConsole().

◆ PseudoConsole() [3/3]

lldb_private::PseudoConsole::PseudoConsole ( PseudoConsole && )
delete

References PseudoConsole().

Member Function Documentation

◆ Close()

void PseudoConsole::Close ( )

Closes the ConPTY and invalidates its handle, without closing the STDIN and STDOUT pipes.

Closing the ConPTY signals EOF to any process currently attached to it.

Definition at line 175 of file PseudoConsole.cpp.

References kernel32, m_conpty_handle, m_cv, m_mutex, and SetStopping().

Referenced by ~PseudoConsole().

◆ CloseAnonymousPipes()

void PseudoConsole::CloseAnonymousPipes ( )

Closes the child-side pipe handles (stdin read end and stdout/stderr write end) that were passed to CreateProcessW.

Must be called after a successful CreateProcessW to avoid keeping the pipes alive indefinitely.

Definition at line 195 of file PseudoConsole.cpp.

References m_pipe_child_stdin, and m_pipe_child_stdout.

Referenced by ~PseudoConsole().

◆ ClosePseudoConsolePipes()

void PseudoConsole::ClosePseudoConsolePipes ( )

Closes the STDIN and STDOUT pipe handles and invalidates them.

Definition at line 185 of file PseudoConsole.cpp.

References m_conpty_input, and m_conpty_output.

Referenced by ~PseudoConsole().

◆ CreateOverlappedPipePair()

llvm::Error PseudoConsole::CreateOverlappedPipePair ( HANDLE & out_read,
HANDLE & out_write,
bool inheritable )

Creates a named pipe pair for overlapped I/O.

The read end is set to non-blocking (PIPE_NOWAIT). On failure any handles that were successfully opened are closed and an error is returned.

Definition at line 68 of file PseudoConsole.cpp.

Referenced by OpenAnonymousPipes(), and OpenPseudoConsole().

◆ DrainInitSequences()

llvm::Error PseudoConsole::DrainInitSequences ( )

Drains initialization sequences from the ConPTY output pipe.

When a process first attaches to a ConPTY, Windows emits VT100/ANSI escape sequences (ESC[2J for clear screen, ESC[H for cursor home and more) as part of the PseudoConsole initialization. To prevent these sequences from appearing in the debugger output (and flushing lldb's shell for instance) we launch a short-lived dummy process that triggers the initialization, then drain all output before launching the actual debuggee.

Definition at line 234 of file PseudoConsole.cpp.

References lldb_private::ProcThreadAttributeList::Create(), error(), m_conpty_handle, m_conpty_output, and lldb_private::ProcThreadAttributeList::SetupPseudoConsole().

Referenced by OpenPseudoConsole().

◆ GetChildStdinHandle()

HANDLE lldb_private::PseudoConsole::GetChildStdinHandle ( ) const
inline

The child-side stdin read HANDLE (pipe mode only).

Definition at line 104 of file PseudoConsole.h.

References m_pipe_child_stdin.

Referenced by lldb_private::ProcessLauncherWindows::LaunchProcess().

◆ GetChildStdoutHandle()

HANDLE lldb_private::PseudoConsole::GetChildStdoutHandle ( ) const
inline

The child-side stdout/stderr write HANDLE (pipe mode only).

Definition at line 107 of file PseudoConsole.h.

References m_pipe_child_stdout.

Referenced by lldb_private::ProcessLauncherWindows::LaunchProcess().

◆ GetCV()

std::condition_variable & lldb_private::PseudoConsole::GetCV ( )
inline

Returns a reference to the condition variable used to signal state changes to threads waiting on the ConPTY (e.g.

waiting for output or shutdown).

Definition at line 127 of file PseudoConsole.h.

References m_cv.

◆ GetMode()

Mode lldb_private::PseudoConsole::GetMode ( ) const
inline

Definition at line 109 of file PseudoConsole.h.

References m_mode.

◆ GetMutex()

std::mutex & lldb_private::PseudoConsole::GetMutex ( )
inline

Returns a reference to the mutex used to synchronize access to the ConPTY state.

Definition at line 123 of file PseudoConsole.h.

References m_mutex.

◆ GetPseudoTerminalHandle()

HPCON lldb_private::PseudoConsole::GetPseudoTerminalHandle ( )
inline

The ConPTY HPCON handle accessor.

This object retains ownership of the HPCON when this accessor is used.

Returns
The ConPTY HPCON handle, or INVALID_HANDLE_VALUE if it is currently invalid.

Definition at line 83 of file PseudoConsole.h.

References m_conpty_handle.

◆ GetSTDINHandle()

HANDLE lldb_private::PseudoConsole::GetSTDINHandle ( ) const
inline

The STDIN write HANDLE accessor.

This object retains ownership of the HANDLE when this accessor is used.

Returns
The STDIN write HANDLE, or INVALID_HANDLE_VALUE if it is currently invalid.

Definition at line 101 of file PseudoConsole.h.

References m_conpty_input.

◆ GetSTDOUTHandle()

HANDLE lldb_private::PseudoConsole::GetSTDOUTHandle ( ) const
inline

The STDOUT read HANDLE accessor.

This object retains ownership of the HANDLE when this accessor is used.

Returns
The STDOUT read HANDLE, or INVALID_HANDLE_VALUE if it is currently invalid.

Definition at line 92 of file PseudoConsole.h.

References m_conpty_output.

◆ IsConnected()

bool PseudoConsole::IsConnected ( ) const

Returns whether the ConPTY and its pipes are currently open and valid.

Definition at line 166 of file PseudoConsole.cpp.

References m_conpty_handle, m_conpty_input, m_conpty_output, m_mode, and Pipe.

◆ IsStopping()

bool lldb_private::PseudoConsole::IsStopping ( ) const
inline

Returns whether the ConPTY is in the process of shutting down.

Returns
A reference to the atomic bool that is set to true when the ConPTY is stopping. Callers should check this in their read/write loops to exit gracefully.

Definition at line 135 of file PseudoConsole.h.

References m_stopping.

◆ OpenAnonymousPipes()

llvm::Error PseudoConsole::OpenAnonymousPipes ( )

Creates a pair of anonymous pipes to use for stdio instead of a ConPTY.

Returns
An llvm::Error if the pipes could not be created.

Definition at line 205 of file PseudoConsole.cpp.

References CreateOverlappedPipePair(), m_conpty_input, m_conpty_output, m_mode, m_pipe_child_stdin, m_pipe_child_stdout, None, and Pipe.

◆ OpenPseudoConsole()

llvm::Error PseudoConsole::OpenPseudoConsole ( )

Creates and opens a new ConPTY instance with a default console size of 80x25.

Also sets up the associated STDIN/STDOUT pipes and drains any initialization sequences emitted by Windows.

Returns
An llvm::Error if the ConPTY could not be created, or if ConPTY is not available on this version of Windows, llvm::Error::success() otherwise.

Definition at line 102 of file PseudoConsole.cpp.

References ConPTY, CreateOverlappedPipePair(), DrainInitSequences(), error(), lldb_private::GetLog(), lldb_private::Host, kernel32, LLDB_LOG_ERROR, m_conpty_handle, m_conpty_input, m_conpty_output, m_mode, and None.

◆ operator=() [1/2]

PseudoConsole & lldb_private::PseudoConsole::operator= ( const PseudoConsole & )
delete

References PseudoConsole().

◆ operator=() [2/2]

PseudoConsole & lldb_private::PseudoConsole::operator= ( PseudoConsole && )
delete

References PseudoConsole().

◆ SetStopping()

void lldb_private::PseudoConsole::SetStopping ( bool value)
inline

Sets the stopping flag to value, signalling to threads waiting on the ConPTY that they should stop.

Definition at line 139 of file PseudoConsole.h.

References m_stopping.

Referenced by Close().

Member Data Documentation

◆ m_conpty_handle

HANDLE lldb_private::PseudoConsole::m_conpty_handle = ((HANDLE)(long long)-1)
protected

◆ m_conpty_input

HANDLE lldb_private::PseudoConsole::m_conpty_input = ((HANDLE)(long long)-1)
protected

◆ m_conpty_output

HANDLE lldb_private::PseudoConsole::m_conpty_output = ((HANDLE)(long long)-1)
protected

◆ m_cv

std::condition_variable lldb_private::PseudoConsole::m_cv {}
protected

Definition at line 150 of file PseudoConsole.h.

Referenced by Close(), and GetCV().

◆ m_mode

Mode lldb_private::PseudoConsole::m_mode = Mode::None
protected

Definition at line 148 of file PseudoConsole.h.

Referenced by GetMode(), IsConnected(), OpenAnonymousPipes(), and OpenPseudoConsole().

◆ m_mutex

std::mutex lldb_private::PseudoConsole::m_mutex {}
protected

Definition at line 149 of file PseudoConsole.h.

Referenced by Close(), and GetMutex().

◆ m_pipe_child_stdin

HANDLE lldb_private::PseudoConsole::m_pipe_child_stdin = ((HANDLE)(long long)-1)
protected

Definition at line 146 of file PseudoConsole.h.

Referenced by CloseAnonymousPipes(), GetChildStdinHandle(), and OpenAnonymousPipes().

◆ m_pipe_child_stdout

HANDLE lldb_private::PseudoConsole::m_pipe_child_stdout = ((HANDLE)(long long)-1)
protected

Definition at line 147 of file PseudoConsole.h.

Referenced by CloseAnonymousPipes(), GetChildStdoutHandle(), and OpenAnonymousPipes().

◆ m_stopping

std::atomic<bool> lldb_private::PseudoConsole::m_stopping = false
protected

Definition at line 151 of file PseudoConsole.h.

Referenced by IsStopping(), and SetStopping().


The documentation for this class was generated from the following files: