LLDB  mainline
Public Types | Public Member Functions | Protected Attributes | Private Member Functions | List of all members
lldb_private::PseudoTerminal Class Reference

A pseudo terminal helper class. More...

#include "lldb/Host/PseudoTerminal.h"

Collaboration diagram for lldb_private::PseudoTerminal:
Collaboration graph
[legend]

Public Types

enum  { invalid_fd = -1 }
 

Public Member Functions

 PseudoTerminal ()
 Default constructor. More...
 
 ~PseudoTerminal ()
 Destructor. More...
 
void ClosePrimaryFileDescriptor ()
 Close the primary file descriptor if it is valid. More...
 
void CloseSecondaryFileDescriptor ()
 Close the secondary file descriptor if it is valid. More...
 
lldb::pid_t Fork (char *error_str, size_t error_len)
 Fork a child process that uses pseudo terminals for its stdio. More...
 
int GetPrimaryFileDescriptor () const
 The primary file descriptor accessor. More...
 
int GetSecondaryFileDescriptor () const
 The secondary file descriptor accessor. More...
 
const char * GetSecondaryName (char *error_str, size_t error_len) const
 Get the name of the secondary pseudo terminal. More...
 
bool OpenFirstAvailablePrimary (int oflag, char *error_str, size_t error_len)
 Open the first available pseudo terminal. More...
 
bool OpenSecondary (int oflag, char *error_str, size_t error_len)
 Open the secondary for the current primary pseudo terminal. More...
 
int ReleasePrimaryFileDescriptor ()
 Release the primary file descriptor. More...
 
int ReleaseSecondaryFileDescriptor ()
 Release the secondary file descriptor. More...
 

Protected Attributes

int m_primary_fd
 The file descriptor for the primary. More...
 
int m_secondary_fd
 The file descriptor for the secondary. More...
 

Private Member Functions

 PseudoTerminal (const PseudoTerminal &)=delete
 
const PseudoTerminaloperator= (const PseudoTerminal &)=delete
 

Detailed Description

A pseudo terminal helper class.

The pseudo terminal class abstracts the use of pseudo terminals on the host system.

Definition at line 24 of file PseudoTerminal.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
invalid_fd 

Invalid file descriptor value.

Definition at line 26 of file PseudoTerminal.h.

Constructor & Destructor Documentation

◆ PseudoTerminal() [1/2]

PseudoTerminal::PseudoTerminal ( )

Default constructor.

Constructs this object with invalid primary and secondary file descriptors.

Definition at line 36 of file PseudoTerminal.cpp.

◆ ~PseudoTerminal()

PseudoTerminal::~PseudoTerminal ( )

Destructor.

The destructor will close the primary and secondary file descriptors if they are valid and ownership has not been released using one of:

Definition at line 45 of file PseudoTerminal.cpp.

References ClosePrimaryFileDescriptor(), and CloseSecondaryFileDescriptor().

◆ PseudoTerminal() [2/2]

lldb_private::PseudoTerminal::PseudoTerminal ( const PseudoTerminal )
privatedelete

Member Function Documentation

◆ ClosePrimaryFileDescriptor()

void PseudoTerminal::ClosePrimaryFileDescriptor ( )

Close the primary file descriptor if it is valid.

Definition at line 51 of file PseudoTerminal.cpp.

References invalid_fd, and m_primary_fd.

Referenced by Fork(), OpenFirstAvailablePrimary(), and ~PseudoTerminal().

◆ CloseSecondaryFileDescriptor()

void PseudoTerminal::CloseSecondaryFileDescriptor ( )

Close the secondary file descriptor if it is valid.

Definition at line 59 of file PseudoTerminal.cpp.

References invalid_fd, and m_secondary_fd.

Referenced by ProcessMonitor::Launch(), OpenSecondary(), and ~PseudoTerminal().

◆ Fork()

lldb::pid_t PseudoTerminal::Fork ( char *  error_str,
size_t  error_len 
)

Fork a child process that uses pseudo terminals for its stdio.

In the parent process, a call to this function results in a pid being returned. If the pid is valid, the primary file descriptor can be used for read/write access to stdio of the child process.

In the child process the stdin/stdout/stderr will already be routed to the secondary pseudo terminal and the primary file descriptor will be closed as it is no longer needed by the child process.

This class will close the file descriptors for the primary/secondary when the destructor is called. The file handles can be released using either:

Parameters
[out]error_strAn pointer to an error that can describe any errors that occur. This can be NULL if no error status is desired.
Returns
Parent process: a child process ID that is greater than zero, or -1 if the fork fails. Child process: zero.

Definition at line 194 of file PseudoTerminal.cpp.

References ClosePrimaryFileDescriptor(), ErrnoToStr(), fork(), LLDB_INVALID_PROCESS_ID, m_secondary_fd, OpenFirstAvailablePrimary(), OpenSecondary(), and setsid().

Referenced by ProcessMonitor::Launch().

◆ GetPrimaryFileDescriptor()

int PseudoTerminal::GetPrimaryFileDescriptor ( ) const

The primary file descriptor accessor.

This object retains ownership of the primary file descriptor when this accessor is used. Users can call the member function PseudoTerminal::ReleasePrimaryFileDescriptor() if this object should release ownership of the secondary file descriptor.

Returns
The primary file descriptor, or PseudoTerminal::invalid_fd if the primary file descriptor is not currently valid.
See also
PseudoTerminal::ReleasePrimaryFileDescriptor()

Definition at line 259 of file PseudoTerminal.cpp.

References m_primary_fd.

Referenced by lldb_private::process_gdb_remote::ProcessGDBRemote::DoLaunch(), and HandleFileAction().

◆ GetSecondaryFileDescriptor()

int PseudoTerminal::GetSecondaryFileDescriptor ( ) const

The secondary file descriptor accessor.

This object retains ownership of the secondary file descriptor when this accessor is used. Users can call the member function PseudoTerminal::ReleaseSecondaryFileDescriptor() if this object should release ownership of the secondary file descriptor.

Returns
The secondary file descriptor, or PseudoTerminal::invalid_fd if the secondary file descriptor is not currently valid.
See also
PseudoTerminal::ReleaseSecondaryFileDescriptor()

Definition at line 265 of file PseudoTerminal.cpp.

References m_secondary_fd.

Referenced by HandleFileAction().

◆ GetSecondaryName()

const char * PseudoTerminal::GetSecondaryName ( char *  error_str,
size_t  error_len 
) const

Get the name of the secondary pseudo terminal.

A primary pseudo terminal should already be valid prior to calling this function.

Parameters
[out]error_strAn pointer to an error that can describe any errors that occur. This can be NULL if no error status is desired.
Returns
The name of the secondary pseudo terminal as a NULL terminated C. This string that comes from static memory, so a copy of the string should be made as subsequent calls can change this value. NULL is returned if this object doesn't have a valid primary pseudo terminal opened or if the call to ptsname() fails.
See also
PseudoTerminal::OpenFirstAvailablePrimary()

Definition at line 158 of file PseudoTerminal.cpp.

References ErrnoToStr(), m_primary_fd, and ptsname().

Referenced by ProcessFreeBSD::DoLaunch(), lldb_private::process_gdb_remote::ProcessGDBRemote::DoLaunch(), HandleFileAction(), and OpenSecondary().

◆ OpenFirstAvailablePrimary()

bool PseudoTerminal::OpenFirstAvailablePrimary ( int  oflag,
char *  error_str,
size_t  error_len 
)

Open the first available pseudo terminal.

Opens the first available pseudo terminal with oflag as the permissions. The opened primary file descriptor is stored in this object and can be accessed by calling the PseudoTerminal::GetPrimaryFileDescriptor() accessor. Clients can call the PseudoTerminal::ReleasePrimaryFileDescriptor() accessor function if they wish to use the primary file descriptor beyond the lifespan of this object.

If this object still has a valid primary file descriptor when its destructor is called, it will close it.

Parameters
[in]oflagFlags to use when calling posix_openpt(oflag). A value of "O_RDWR|O_NOCTTY" is suggested.
[out]error_strAn pointer to an error that can describe any errors that occur. This can be NULL if no error status is desired.
Returns
true when the primary files descriptor is successfully opened. false if anything goes wrong.
See also
PseudoTerminal::GetPrimaryFileDescriptor()
PseudoTerminal::ReleasePrimaryFileDescriptor()

Definition at line 77 of file PseudoTerminal.cpp.

References ClosePrimaryFileDescriptor(), ErrnoToStr(), grantpt(), m_primary_fd, posix_openpt(), and unlockpt().

Referenced by lldb_private::process_gdb_remote::ProcessGDBRemote::DoLaunch(), and Fork().

◆ OpenSecondary()

bool PseudoTerminal::OpenSecondary ( int  oflag,
char *  error_str,
size_t  error_len 
)

Open the secondary for the current primary pseudo terminal.

A primary pseudo terminal should already be valid prior to calling this function. The opened secondary file descriptor is stored in this object and can be accessed by calling the PseudoTerminal::GetSecondaryFileDescriptor() accessor. Clients can call the PseudoTerminal::ReleaseSecondaryFileDescriptor() accessor function if they wish to use the secondary file descriptor beyond the lifespan of this object.

If this object still has a valid secondary file descriptor when its destructor is called, it will close it.

Parameters
[in]oflagFlags to use when calling open(oflag).
[out]error_strAn pointer to an error that can describe any errors that occur. This can be NULL if no error status is desired.
Returns
true when the primary files descriptor is successfully opened. false if anything goes wrong.
See also
PseudoTerminal::OpenFirstAvailablePrimary()
PseudoTerminal::GetSecondaryFileDescriptor()
PseudoTerminal::ReleaseSecondaryFileDescriptor()

Definition at line 124 of file PseudoTerminal.cpp.

References CloseSecondaryFileDescriptor(), ErrnoToStr(), GetSecondaryName(), and m_secondary_fd.

Referenced by Fork(), and HandleFileAction().

◆ operator=()

const PseudoTerminal& lldb_private::PseudoTerminal::operator= ( const PseudoTerminal )
privatedelete

◆ ReleasePrimaryFileDescriptor()

int PseudoTerminal::ReleasePrimaryFileDescriptor ( )

Release the primary file descriptor.

Releases ownership of the primary pseudo terminal file descriptor without closing it. The destructor for this class will close the primary file descriptor if the ownership isn't released using this call and the primary file descriptor has been opened.

Returns
The primary file descriptor, or PseudoTerminal::invalid_fd if the mast file descriptor is not currently valid.

Definition at line 273 of file PseudoTerminal.cpp.

References invalid_fd, and m_primary_fd.

Referenced by PlatformAppleSimulator::DebugProcess(), lldb_private::platform_linux::PlatformLinux::DebugProcess(), lldb_private::platform_netbsd::PlatformNetBSD::DebugProcess(), lldb_private::Platform::DebugProcess(), lldb_private::process_gdb_remote::ProcessGDBRemote::DoLaunch(), lldb_private::process_netbsd::NativeProcessNetBSD::Factory::Launch(), lldb_private::process_linux::NativeProcessLinux::Factory::Launch(), and ProcessMonitor::Launch().

◆ ReleaseSecondaryFileDescriptor()

int PseudoTerminal::ReleaseSecondaryFileDescriptor ( )

Release the secondary file descriptor.

Release ownership of the secondary pseudo terminal file descriptor without closing it. The destructor for this class will close the secondary file descriptor if the ownership isn't released using this call and the secondary file descriptor has been opened.

Returns
The secondary file descriptor, or PseudoTerminal::invalid_fd if the secondary file descriptor is not currently valid.

Definition at line 285 of file PseudoTerminal.cpp.

References invalid_fd, and m_secondary_fd.

Member Data Documentation

◆ m_primary_fd

int lldb_private::PseudoTerminal::m_primary_fd
protected

◆ m_secondary_fd

int lldb_private::PseudoTerminal::m_secondary_fd
protected

The file descriptor for the secondary.

Definition at line 210 of file PseudoTerminal.h.

Referenced by CloseSecondaryFileDescriptor(), Fork(), GetSecondaryFileDescriptor(), OpenSecondary(), and ReleaseSecondaryFileDescriptor().


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