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"

Public Types

enum  { invalid_fd = -1 }
 

Public Member Functions

 PseudoTerminal ()
 Default constructor.
 
 ~PseudoTerminal ()
 Destructor.
 
void ClosePrimaryFileDescriptor ()
 Close the primary file descriptor if it is valid.
 
void CloseSecondaryFileDescriptor ()
 Close the secondary file descriptor if it is valid.
 
llvm::Expected< lldb::pid_tFork ()
 Fork a child process that uses pseudo terminals for its stdio.
 
int GetPrimaryFileDescriptor () const
 The primary file descriptor accessor.
 
int GetSecondaryFileDescriptor () const
 The secondary file descriptor accessor.
 
std::string GetSecondaryName () const
 Get the name of the secondary pseudo terminal.
 
llvm::Error OpenFirstAvailablePrimary (int oflag)
 Open the first available pseudo terminal.
 
llvm::Error OpenSecondary (int oflag)
 Open the secondary for the current primary pseudo terminal.
 
int ReleasePrimaryFileDescriptor ()
 Release the primary file descriptor.
 
int ReleaseSecondaryFileDescriptor ()
 Release the secondary file descriptor.
 

Protected Attributes

int m_primary_fd = invalid_fd
 The file descriptor for the primary.
 
int m_secondary_fd = invalid_fd
 The file descriptor for the secondary.
 

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

References m_primary_fd.

Referenced by lldb_private::PlatformQemuUser::DebugProcess(), 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 195 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 117 of file PseudoTerminal.cpp.

References m_primary_fd, PATH_MAX, UNUSED_IF_ASSERT_DISABLED, and use_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 66 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 95 of file PseudoTerminal.cpp.

References CloseSecondaryFileDescriptor(), GetSecondaryName(), lldb_private::FileSystem::Instance(), m_secondary_fd, and lldb_private::FileSystem::Open().

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

References invalid_fd, and m_primary_fd.

Referenced by PlatformPOSIX::DebugProcess(), lldb_private::Platform::DebugProcess(), lldb_private::PlatformAppleSimulator::DebugProcess(), lldb_private::PlatformQemuUser::DebugProcess(), lldb_private::process_gdb_remote::ProcessGDBRemote::DoLaunch(), lldb_private::process_freebsd::NativeProcessFreeBSD::Manager::Launch(), lldb_private::process_linux::NativeProcessLinux::Manager::Launch(), and lldb_private::process_netbsd::NativeProcessNetBSD::Manager::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 215 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: