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

#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...
 
llvm::Expected< lldb::pid_tFork ()
 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...
 
std::string GetSecondaryName () const
 Get the name of the secondary pseudo terminal. More...
 
llvm::Error OpenFirstAvailablePrimary (int oflag)
 Open the first available pseudo terminal. More...
 
llvm::Error OpenSecondary (int oflag)
 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 = invalid_fd
 The file descriptor for the primary. More...
 
int m_secondary_fd = invalid_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

Default constructor.

Constructs this object with invalid primary and secondary file descriptors.

◆ ~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 40 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 46 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 54 of file PseudoTerminal.cpp.

References invalid_fd, and m_secondary_fd.

Referenced by OpenSecondary(), and ~PseudoTerminal().

◆ Fork()

llvm::Expected< lldb::pid_t > PseudoTerminal::Fork ( )

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:

Returns
Parent process: a child process ID that is greater than zero, or an error if the fork fails. Child process: zero.

Definition at line 120 of file PseudoTerminal.cpp.

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

◆ 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 170 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 176 of file PseudoTerminal.cpp.

References m_secondary_fd.

Referenced by HandleFileAction().

◆ GetSecondaryName()

std::string PseudoTerminal::GetSecondaryName ( ) const

Get the name of the secondary pseudo terminal.

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

Returns
The name of the secondary pseudo terminal.
See also
PseudoTerminal::OpenFirstAvailablePrimary()

Definition at line 102 of file PseudoTerminal.cpp.

References m_primary_fd, PATH_MAX, and ptsname().

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

◆ OpenFirstAvailablePrimary()

llvm::Error PseudoTerminal::OpenFirstAvailablePrimary ( int  oflag)

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.
See also
PseudoTerminal::GetPrimaryFileDescriptor()
PseudoTerminal::ReleasePrimaryFileDescriptor()

Definition at line 61 of file PseudoTerminal.cpp.

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

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

◆ OpenSecondary()

llvm::Error PseudoTerminal::OpenSecondary ( int  oflag)

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).
See also
PseudoTerminal::OpenFirstAvailablePrimary()
PseudoTerminal::GetSecondaryFileDescriptor()
PseudoTerminal::ReleaseSecondaryFileDescriptor()

Definition at line 90 of file PseudoTerminal.cpp.

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

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 184 of file PseudoTerminal.cpp.

References invalid_fd, and m_primary_fd.

Referenced by PlatformPOSIX::DebugProcess(), PlatformAppleSimulator::DebugProcess(), lldb_private::Platform::DebugProcess(), lldb_private::process_gdb_remote::ProcessGDBRemote::DoLaunch(), lldb_private::process_netbsd::NativeProcessNetBSD::Factory::Launch(), lldb_private::process_freebsd::NativeProcessFreeBSD::Factory::Launch(), and lldb_private::process_linux::NativeProcessLinux::Factory::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 196 of file PseudoTerminal.cpp.

References invalid_fd, and m_secondary_fd.

Member Data Documentation

◆ m_primary_fd

int lldb_private::PseudoTerminal::m_primary_fd = invalid_fd
protected

◆ m_secondary_fd

int lldb_private::PseudoTerminal::m_secondary_fd = invalid_fd
protected

The file descriptor for the secondary.

Definition at line 179 of file PseudoTerminal.h.

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


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