LLDB  mainline
Public Types | Public Member Functions | Protected Attributes | 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 CloseMasterFileDescriptor ()
 Close the master file descriptor if it is valid. More...
 
void CloseSlaveFileDescriptor ()
 Close the slave 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 GetMasterFileDescriptor () const
 The master file descriptor accessor. More...
 
int GetSlaveFileDescriptor () const
 The slave file descriptor accessor. More...
 
const char * GetSlaveName (char *error_str, size_t error_len) const
 Get the name of the slave pseudo terminal. More...
 
bool OpenFirstAvailableMaster (int oflag, char *error_str, size_t error_len)
 Open the first available pseudo terminal. More...
 
bool OpenSlave (int oflag, char *error_str, size_t error_len)
 Open the slave for the current master pseudo terminal. More...
 
int ReleaseMasterFileDescriptor ()
 Release the master file descriptor. More...
 
int ReleaseSlaveFileDescriptor ()
 Release the slave file descriptor. More...
 

Protected Attributes

int m_master_fd
 The file descriptor for the master. More...
 
int m_slave_fd
 The file descriptor for the slave. More...
 

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()

PseudoTerminal::PseudoTerminal ( )

Default constructor.

Constructs this object with invalid master and slave file descriptors.

Definition at line 36 of file PseudoTerminal.cpp.

◆ ~PseudoTerminal()

PseudoTerminal::~PseudoTerminal ( )

Destructor.

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

Definition at line 45 of file PseudoTerminal.cpp.

References CloseMasterFileDescriptor(), and CloseSlaveFileDescriptor().

Member Function Documentation

◆ CloseMasterFileDescriptor()

void PseudoTerminal::CloseMasterFileDescriptor ( )

Close the master file descriptor if it is valid.

Definition at line 51 of file PseudoTerminal.cpp.

References invalid_fd, and m_master_fd.

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

◆ CloseSlaveFileDescriptor()

void PseudoTerminal::CloseSlaveFileDescriptor ( )

Close the slave file descriptor if it is valid.

Definition at line 59 of file PseudoTerminal.cpp.

References invalid_fd, and m_slave_fd.

Referenced by OpenSlave(), ProcessMonitor::~ProcessMonitor(), 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 master 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 slave pseudo terminal and the master file descriptor will be closed as it is no longer needed by the child process.

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

Parameters
[out]errorAn 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 192 of file PseudoTerminal.cpp.

References CloseMasterFileDescriptor(), ErrnoToStr(), fork(), LLDB_INVALID_PROCESS_ID, m_slave_fd, OpenFirstAvailableMaster(), OpenSlave(), and setsid().

Referenced by lldb_private::darwin_process_launcher::ForkChildForPTraceDebugging(), and ProcessMonitor::~ProcessMonitor().

◆ GetMasterFileDescriptor()

int PseudoTerminal::GetMasterFileDescriptor ( ) const

The master file descriptor accessor.

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

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

Definition at line 257 of file PseudoTerminal.cpp.

References m_master_fd.

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

◆ GetSlaveFileDescriptor()

int PseudoTerminal::GetSlaveFileDescriptor ( ) const

The slave file descriptor accessor.

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

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

Definition at line 263 of file PseudoTerminal.cpp.

References m_slave_fd.

Referenced by HandleFileAction().

◆ GetSlaveName()

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

Get the name of the slave pseudo terminal.

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

Parameters
[out]errorAn 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 slave 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 master pseudo terminal opened or if the call to ptsname() fails.
See also
PseudoTerminal::OpenFirstAvailableMaster()

Definition at line 156 of file PseudoTerminal.cpp.

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

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

◆ OpenFirstAvailableMaster()

bool PseudoTerminal::OpenFirstAvailableMaster ( 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 master file descriptor is stored in this object and can be accessed by calling the PseudoTerminal::GetMasterFileDescriptor() accessor. Clients can call the PseudoTerminal::ReleaseMasterFileDescriptor() accessor function if they wish to use the master file descriptor beyond the lifespan of this object.

If this object still has a valid master 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]errorAn 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 master files descriptor is successfully opened.
  • false if anything goes wrong.
See also
PseudoTerminal::GetMasterFileDescriptor()
PseudoTerminal::ReleaseMasterFileDescriptor()

Definition at line 77 of file PseudoTerminal.cpp.

References CloseMasterFileDescriptor(), ErrnoToStr(), grantpt(), m_master_fd, posix_openpt(), and unlockpt().

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

◆ OpenSlave()

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

Open the slave for the current master pseudo terminal.

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

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

Parameters
[in]oflagFlags to use when calling open(oflag).
[out]errorAn 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 master files descriptor is successfully opened.
  • false if anything goes wrong.
See also
PseudoTerminal::OpenFirstAvailableMaster()
PseudoTerminal::GetSlaveFileDescriptor()
PseudoTerminal::ReleaseSlaveFileDescriptor()

Definition at line 124 of file PseudoTerminal.cpp.

References CloseSlaveFileDescriptor(), ErrnoToStr(), GetSlaveName(), and m_slave_fd.

Referenced by Fork(), and HandleFileAction().

◆ ReleaseMasterFileDescriptor()

int PseudoTerminal::ReleaseMasterFileDescriptor ( )

Release the master file descriptor.

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

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

Definition at line 269 of file PseudoTerminal.cpp.

References invalid_fd, and m_master_fd.

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

◆ ReleaseSlaveFileDescriptor()

int PseudoTerminal::ReleaseSlaveFileDescriptor ( )

Release the slave file descriptor.

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

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

Definition at line 281 of file PseudoTerminal.cpp.

References invalid_fd, and m_slave_fd.

Member Data Documentation

◆ m_master_fd

int lldb_private::PseudoTerminal::m_master_fd
protected

The file descriptor for the master.

Definition at line 208 of file PseudoTerminal.h.

Referenced by CloseMasterFileDescriptor(), GetMasterFileDescriptor(), GetSlaveName(), OpenFirstAvailableMaster(), and ReleaseMasterFileDescriptor().

◆ m_slave_fd

int lldb_private::PseudoTerminal::m_slave_fd
protected

The file descriptor for the slave.

Definition at line 209 of file PseudoTerminal.h.

Referenced by CloseSlaveFileDescriptor(), Fork(), GetSlaveFileDescriptor(), OpenSlave(), and ReleaseSlaveFileDescriptor().


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