15ffd83dbSDimitry Andric //===-- ConnectionFileDescriptorPosix.cpp ---------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #if defined(__APPLE__)
100b57cec5SDimitry Andric // Enable this special support for Apple builds where we can have unlimited
110b57cec5SDimitry Andric // select bounds. We tried switching to poll() and kqueue and we were panicing
120b57cec5SDimitry Andric // the kernel, so we have to stick with select for now.
130b57cec5SDimitry Andric #define _DARWIN_UNLIMITED_SELECT
140b57cec5SDimitry Andric #endif
150b57cec5SDimitry Andric
1681ad6265SDimitry Andric #include "lldb/Host/posix/ConnectionFileDescriptorPosix.h"
170b57cec5SDimitry Andric #include "lldb/Host/Config.h"
1806c3fb27SDimitry Andric #include "lldb/Host/FileSystem.h"
190b57cec5SDimitry Andric #include "lldb/Host/Socket.h"
200b57cec5SDimitry Andric #include "lldb/Host/SocketAddress.h"
2181ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.h"
220b57cec5SDimitry Andric #include "lldb/Utility/SelectHelper.h"
230b57cec5SDimitry Andric #include "lldb/Utility/Timeout.h"
240b57cec5SDimitry Andric
25fe6060f1SDimitry Andric #include <cerrno>
26fe6060f1SDimitry Andric #include <cstdlib>
27fe6060f1SDimitry Andric #include <cstring>
280b57cec5SDimitry Andric #include <fcntl.h>
290b57cec5SDimitry Andric #include <sys/types.h>
300b57cec5SDimitry Andric
31480093f4SDimitry Andric #if LLDB_ENABLE_POSIX
320b57cec5SDimitry Andric #include <termios.h>
330b57cec5SDimitry Andric #include <unistd.h>
340b57cec5SDimitry Andric #endif
350b57cec5SDimitry Andric
360b57cec5SDimitry Andric #include <memory>
370b57cec5SDimitry Andric #include <sstream>
380b57cec5SDimitry Andric
390b57cec5SDimitry Andric #include "llvm/Support/Errno.h"
400b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
410b57cec5SDimitry Andric #if defined(__APPLE__)
420b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
430b57cec5SDimitry Andric #endif
440b57cec5SDimitry Andric #include "lldb/Host/Host.h"
450b57cec5SDimitry Andric #include "lldb/Host/Socket.h"
460b57cec5SDimitry Andric #include "lldb/Host/common/TCPSocket.h"
475ffd83dbSDimitry Andric #include "lldb/Host/common/UDPSocket.h"
480b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
490b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h"
500b57cec5SDimitry Andric #include "lldb/Utility/Timer.h"
510b57cec5SDimitry Andric
520b57cec5SDimitry Andric using namespace lldb;
530b57cec5SDimitry Andric using namespace lldb_private;
540b57cec5SDimitry Andric
ConnectionFileDescriptor(bool child_processes_inherit)550b57cec5SDimitry Andric ConnectionFileDescriptor::ConnectionFileDescriptor(bool child_processes_inherit)
560b57cec5SDimitry Andric : Connection(), m_pipe(), m_mutex(), m_shutting_down(false),
57fe6060f1SDimitry Andric
580b57cec5SDimitry Andric m_child_processes_inherit(child_processes_inherit) {
5981ad6265SDimitry Andric Log *log(GetLog(LLDBLog::Connection | LLDBLog::Object));
609dba64beSDimitry Andric LLDB_LOGF(log, "%p ConnectionFileDescriptor::ConnectionFileDescriptor ()",
610b57cec5SDimitry Andric static_cast<void *>(this));
620b57cec5SDimitry Andric }
630b57cec5SDimitry Andric
ConnectionFileDescriptor(int fd,bool owns_fd)640b57cec5SDimitry Andric ConnectionFileDescriptor::ConnectionFileDescriptor(int fd, bool owns_fd)
650b57cec5SDimitry Andric : Connection(), m_pipe(), m_mutex(), m_shutting_down(false),
66349cc55cSDimitry Andric m_child_processes_inherit(false) {
67349cc55cSDimitry Andric m_io_sp =
68349cc55cSDimitry Andric std::make_shared<NativeFile>(fd, File::eOpenOptionReadWrite, owns_fd);
690b57cec5SDimitry Andric
7081ad6265SDimitry Andric Log *log(GetLog(LLDBLog::Connection | LLDBLog::Object));
719dba64beSDimitry Andric LLDB_LOGF(log,
729dba64beSDimitry Andric "%p ConnectionFileDescriptor::ConnectionFileDescriptor (fd = "
730b57cec5SDimitry Andric "%i, owns_fd = %i)",
740b57cec5SDimitry Andric static_cast<void *>(this), fd, owns_fd);
750b57cec5SDimitry Andric OpenCommandPipe();
760b57cec5SDimitry Andric }
770b57cec5SDimitry Andric
ConnectionFileDescriptor(Socket * socket)780b57cec5SDimitry Andric ConnectionFileDescriptor::ConnectionFileDescriptor(Socket *socket)
790b57cec5SDimitry Andric : Connection(), m_pipe(), m_mutex(), m_shutting_down(false),
80349cc55cSDimitry Andric m_child_processes_inherit(false) {
810b57cec5SDimitry Andric InitializeSocket(socket);
820b57cec5SDimitry Andric }
830b57cec5SDimitry Andric
~ConnectionFileDescriptor()840b57cec5SDimitry Andric ConnectionFileDescriptor::~ConnectionFileDescriptor() {
8581ad6265SDimitry Andric Log *log(GetLog(LLDBLog::Connection | LLDBLog::Object));
869dba64beSDimitry Andric LLDB_LOGF(log, "%p ConnectionFileDescriptor::~ConnectionFileDescriptor ()",
870b57cec5SDimitry Andric static_cast<void *>(this));
880b57cec5SDimitry Andric Disconnect(nullptr);
890b57cec5SDimitry Andric CloseCommandPipe();
900b57cec5SDimitry Andric }
910b57cec5SDimitry Andric
OpenCommandPipe()920b57cec5SDimitry Andric void ConnectionFileDescriptor::OpenCommandPipe() {
930b57cec5SDimitry Andric CloseCommandPipe();
940b57cec5SDimitry Andric
9581ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Connection);
960b57cec5SDimitry Andric // Make the command file descriptor here:
970b57cec5SDimitry Andric Status result = m_pipe.CreateNew(m_child_processes_inherit);
980b57cec5SDimitry Andric if (!result.Success()) {
999dba64beSDimitry Andric LLDB_LOGF(log,
1009dba64beSDimitry Andric "%p ConnectionFileDescriptor::OpenCommandPipe () - could not "
1010b57cec5SDimitry Andric "make pipe: %s",
1020b57cec5SDimitry Andric static_cast<void *>(this), result.AsCString());
1030b57cec5SDimitry Andric } else {
1049dba64beSDimitry Andric LLDB_LOGF(log,
1059dba64beSDimitry Andric "%p ConnectionFileDescriptor::OpenCommandPipe() - success "
1060b57cec5SDimitry Andric "readfd=%d writefd=%d",
1070b57cec5SDimitry Andric static_cast<void *>(this), m_pipe.GetReadFileDescriptor(),
1080b57cec5SDimitry Andric m_pipe.GetWriteFileDescriptor());
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric }
1110b57cec5SDimitry Andric
CloseCommandPipe()1120b57cec5SDimitry Andric void ConnectionFileDescriptor::CloseCommandPipe() {
11381ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Connection);
1149dba64beSDimitry Andric LLDB_LOGF(log, "%p ConnectionFileDescriptor::CloseCommandPipe()",
1150b57cec5SDimitry Andric static_cast<void *>(this));
1160b57cec5SDimitry Andric
1170b57cec5SDimitry Andric m_pipe.Close();
1180b57cec5SDimitry Andric }
1190b57cec5SDimitry Andric
IsConnected() const1200b57cec5SDimitry Andric bool ConnectionFileDescriptor::IsConnected() const {
121349cc55cSDimitry Andric return m_io_sp && m_io_sp->IsValid();
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric
Connect(llvm::StringRef path,Status * error_ptr)1240b57cec5SDimitry Andric ConnectionStatus ConnectionFileDescriptor::Connect(llvm::StringRef path,
1250b57cec5SDimitry Andric Status *error_ptr) {
12681ad6265SDimitry Andric return Connect(
12781ad6265SDimitry Andric path, [](llvm::StringRef) {}, error_ptr);
128349cc55cSDimitry Andric }
129349cc55cSDimitry Andric
130349cc55cSDimitry Andric ConnectionStatus
Connect(llvm::StringRef path,socket_id_callback_type socket_id_callback,Status * error_ptr)131349cc55cSDimitry Andric ConnectionFileDescriptor::Connect(llvm::StringRef path,
132349cc55cSDimitry Andric socket_id_callback_type socket_id_callback,
133349cc55cSDimitry Andric Status *error_ptr) {
1340b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
13581ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Connection);
1369dba64beSDimitry Andric LLDB_LOGF(log, "%p ConnectionFileDescriptor::Connect (url = '%s')",
1370b57cec5SDimitry Andric static_cast<void *>(this), path.str().c_str());
1380b57cec5SDimitry Andric
1390b57cec5SDimitry Andric OpenCommandPipe();
1400b57cec5SDimitry Andric
141349cc55cSDimitry Andric if (path.empty()) {
142349cc55cSDimitry Andric if (error_ptr)
143349cc55cSDimitry Andric error_ptr->SetErrorString("invalid connect arguments");
144349cc55cSDimitry Andric return eConnectionStatusError;
145349cc55cSDimitry Andric }
146349cc55cSDimitry Andric
147349cc55cSDimitry Andric llvm::StringRef scheme;
148349cc55cSDimitry Andric std::tie(scheme, path) = path.split("://");
149349cc55cSDimitry Andric
1500b57cec5SDimitry Andric if (!path.empty()) {
151349cc55cSDimitry Andric auto method =
152349cc55cSDimitry Andric llvm::StringSwitch<ConnectionStatus (ConnectionFileDescriptor::*)(
153349cc55cSDimitry Andric llvm::StringRef, socket_id_callback_type, Status *)>(scheme)
154349cc55cSDimitry Andric .Case("listen", &ConnectionFileDescriptor::AcceptTCP)
155349cc55cSDimitry Andric .Cases("accept", "unix-accept",
156349cc55cSDimitry Andric &ConnectionFileDescriptor::AcceptNamedSocket)
157349cc55cSDimitry Andric .Case("unix-abstract-accept",
158349cc55cSDimitry Andric &ConnectionFileDescriptor::AcceptAbstractSocket)
159349cc55cSDimitry Andric .Cases("connect", "tcp-connect",
160349cc55cSDimitry Andric &ConnectionFileDescriptor::ConnectTCP)
161349cc55cSDimitry Andric .Case("udp", &ConnectionFileDescriptor::ConnectUDP)
162349cc55cSDimitry Andric .Case("unix-connect", &ConnectionFileDescriptor::ConnectNamedSocket)
163349cc55cSDimitry Andric .Case("unix-abstract-connect",
164349cc55cSDimitry Andric &ConnectionFileDescriptor::ConnectAbstractSocket)
165480093f4SDimitry Andric #if LLDB_ENABLE_POSIX
166349cc55cSDimitry Andric .Case("fd", &ConnectionFileDescriptor::ConnectFD)
167349cc55cSDimitry Andric .Case("file", &ConnectionFileDescriptor::ConnectFile)
168349cc55cSDimitry Andric .Case("serial", &ConnectionFileDescriptor::ConnectSerialPort)
1690b57cec5SDimitry Andric #endif
170349cc55cSDimitry Andric .Default(nullptr);
171349cc55cSDimitry Andric
172349cc55cSDimitry Andric if (method) {
173349cc55cSDimitry Andric if (error_ptr)
174349cc55cSDimitry Andric *error_ptr = Status();
175349cc55cSDimitry Andric return (this->*method)(path, socket_id_callback, error_ptr);
176349cc55cSDimitry Andric }
177349cc55cSDimitry Andric }
178349cc55cSDimitry Andric
1790b57cec5SDimitry Andric if (error_ptr)
1800b57cec5SDimitry Andric error_ptr->SetErrorStringWithFormat("unsupported connection URL: '%s'",
1810b57cec5SDimitry Andric path.str().c_str());
1820b57cec5SDimitry Andric return eConnectionStatusError;
1830b57cec5SDimitry Andric }
1840b57cec5SDimitry Andric
InterruptRead()1850b57cec5SDimitry Andric bool ConnectionFileDescriptor::InterruptRead() {
1860b57cec5SDimitry Andric size_t bytes_written = 0;
1870b57cec5SDimitry Andric Status result = m_pipe.Write("i", 1, bytes_written);
1880b57cec5SDimitry Andric return result.Success();
1890b57cec5SDimitry Andric }
1900b57cec5SDimitry Andric
Disconnect(Status * error_ptr)1910b57cec5SDimitry Andric ConnectionStatus ConnectionFileDescriptor::Disconnect(Status *error_ptr) {
19281ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Connection);
1939dba64beSDimitry Andric LLDB_LOGF(log, "%p ConnectionFileDescriptor::Disconnect ()",
1940b57cec5SDimitry Andric static_cast<void *>(this));
1950b57cec5SDimitry Andric
1960b57cec5SDimitry Andric ConnectionStatus status = eConnectionStatusSuccess;
1970b57cec5SDimitry Andric
1980b57cec5SDimitry Andric if (!IsConnected()) {
1999dba64beSDimitry Andric LLDB_LOGF(
2009dba64beSDimitry Andric log, "%p ConnectionFileDescriptor::Disconnect(): Nothing to disconnect",
2010b57cec5SDimitry Andric static_cast<void *>(this));
2020b57cec5SDimitry Andric return eConnectionStatusSuccess;
2030b57cec5SDimitry Andric }
2040b57cec5SDimitry Andric
2050b57cec5SDimitry Andric // Try to get the ConnectionFileDescriptor's mutex. If we fail, that is
2060b57cec5SDimitry Andric // quite likely because somebody is doing a blocking read on our file
2070b57cec5SDimitry Andric // descriptor. If that's the case, then send the "q" char to the command
2080b57cec5SDimitry Andric // file channel so the read will wake up and the connection will then know to
2090b57cec5SDimitry Andric // shut down.
2100b57cec5SDimitry Andric std::unique_lock<std::recursive_mutex> locker(m_mutex, std::defer_lock);
2110b57cec5SDimitry Andric if (!locker.try_lock()) {
2120b57cec5SDimitry Andric if (m_pipe.CanWrite()) {
2130b57cec5SDimitry Andric size_t bytes_written = 0;
2140b57cec5SDimitry Andric Status result = m_pipe.Write("q", 1, bytes_written);
2159dba64beSDimitry Andric LLDB_LOGF(log,
2169dba64beSDimitry Andric "%p ConnectionFileDescriptor::Disconnect(): Couldn't get "
2170b57cec5SDimitry Andric "the lock, sent 'q' to %d, error = '%s'.",
2180b57cec5SDimitry Andric static_cast<void *>(this), m_pipe.GetWriteFileDescriptor(),
2190b57cec5SDimitry Andric result.AsCString());
2200b57cec5SDimitry Andric } else if (log) {
2219dba64beSDimitry Andric LLDB_LOGF(log,
2229dba64beSDimitry Andric "%p ConnectionFileDescriptor::Disconnect(): Couldn't get the "
2230b57cec5SDimitry Andric "lock, but no command pipe is available.",
2240b57cec5SDimitry Andric static_cast<void *>(this));
2250b57cec5SDimitry Andric }
2260b57cec5SDimitry Andric locker.lock();
2270b57cec5SDimitry Andric }
2280b57cec5SDimitry Andric
2299dba64beSDimitry Andric // Prevents reads and writes during shutdown.
2309dba64beSDimitry Andric m_shutting_down = true;
2319dba64beSDimitry Andric
232349cc55cSDimitry Andric Status error = m_io_sp->Close();
233349cc55cSDimitry Andric if (error.Fail())
2340b57cec5SDimitry Andric status = eConnectionStatusError;
2350b57cec5SDimitry Andric if (error_ptr)
236349cc55cSDimitry Andric *error_ptr = error;
2370b57cec5SDimitry Andric
2380b57cec5SDimitry Andric // Close any pipes we were using for async interrupts
2390b57cec5SDimitry Andric m_pipe.Close();
2400b57cec5SDimitry Andric
2410b57cec5SDimitry Andric m_uri.clear();
2420b57cec5SDimitry Andric m_shutting_down = false;
2430b57cec5SDimitry Andric return status;
2440b57cec5SDimitry Andric }
2450b57cec5SDimitry Andric
Read(void * dst,size_t dst_len,const Timeout<std::micro> & timeout,ConnectionStatus & status,Status * error_ptr)2460b57cec5SDimitry Andric size_t ConnectionFileDescriptor::Read(void *dst, size_t dst_len,
2470b57cec5SDimitry Andric const Timeout<std::micro> &timeout,
2480b57cec5SDimitry Andric ConnectionStatus &status,
2490b57cec5SDimitry Andric Status *error_ptr) {
25081ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Connection);
2510b57cec5SDimitry Andric
2520b57cec5SDimitry Andric std::unique_lock<std::recursive_mutex> locker(m_mutex, std::defer_lock);
2530b57cec5SDimitry Andric if (!locker.try_lock()) {
2549dba64beSDimitry Andric LLDB_LOGF(log,
2559dba64beSDimitry Andric "%p ConnectionFileDescriptor::Read () failed to get the "
2560b57cec5SDimitry Andric "connection lock.",
2570b57cec5SDimitry Andric static_cast<void *>(this));
2580b57cec5SDimitry Andric if (error_ptr)
2590b57cec5SDimitry Andric error_ptr->SetErrorString("failed to get the connection lock for read.");
2600b57cec5SDimitry Andric
2610b57cec5SDimitry Andric status = eConnectionStatusTimedOut;
2620b57cec5SDimitry Andric return 0;
2630b57cec5SDimitry Andric }
2640b57cec5SDimitry Andric
2650b57cec5SDimitry Andric if (m_shutting_down) {
2669dba64beSDimitry Andric if (error_ptr)
2679dba64beSDimitry Andric error_ptr->SetErrorString("shutting down");
2680b57cec5SDimitry Andric status = eConnectionStatusError;
2690b57cec5SDimitry Andric return 0;
2700b57cec5SDimitry Andric }
2710b57cec5SDimitry Andric
2720b57cec5SDimitry Andric status = BytesAvailable(timeout, error_ptr);
2730b57cec5SDimitry Andric if (status != eConnectionStatusSuccess)
2740b57cec5SDimitry Andric return 0;
2750b57cec5SDimitry Andric
2760b57cec5SDimitry Andric Status error;
2770b57cec5SDimitry Andric size_t bytes_read = dst_len;
278349cc55cSDimitry Andric error = m_io_sp->Read(dst, bytes_read);
2790b57cec5SDimitry Andric
2800b57cec5SDimitry Andric if (log) {
2819dba64beSDimitry Andric LLDB_LOGF(log,
2829dba64beSDimitry Andric "%p ConnectionFileDescriptor::Read() fd = %" PRIu64
2830b57cec5SDimitry Andric ", dst = %p, dst_len = %" PRIu64 ") => %" PRIu64 ", error = %s",
2840b57cec5SDimitry Andric static_cast<void *>(this),
285349cc55cSDimitry Andric static_cast<uint64_t>(m_io_sp->GetWaitableHandle()),
2860b57cec5SDimitry Andric static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
2870b57cec5SDimitry Andric static_cast<uint64_t>(bytes_read), error.AsCString());
2880b57cec5SDimitry Andric }
2890b57cec5SDimitry Andric
2900b57cec5SDimitry Andric if (bytes_read == 0) {
2910b57cec5SDimitry Andric error.Clear(); // End-of-file. Do not automatically close; pass along for
2920b57cec5SDimitry Andric // the end-of-file handlers.
2930b57cec5SDimitry Andric status = eConnectionStatusEndOfFile;
2940b57cec5SDimitry Andric }
2950b57cec5SDimitry Andric
2960b57cec5SDimitry Andric if (error_ptr)
2970b57cec5SDimitry Andric *error_ptr = error;
2980b57cec5SDimitry Andric
2990b57cec5SDimitry Andric if (error.Fail()) {
3000b57cec5SDimitry Andric uint32_t error_value = error.GetError();
3010b57cec5SDimitry Andric switch (error_value) {
3020b57cec5SDimitry Andric case EAGAIN: // The file was marked for non-blocking I/O, and no data were
3030b57cec5SDimitry Andric // ready to be read.
304349cc55cSDimitry Andric if (m_io_sp->GetFdType() == IOObject::eFDTypeSocket)
3050b57cec5SDimitry Andric status = eConnectionStatusTimedOut;
3060b57cec5SDimitry Andric else
3070b57cec5SDimitry Andric status = eConnectionStatusSuccess;
3080b57cec5SDimitry Andric return 0;
3090b57cec5SDimitry Andric
3100b57cec5SDimitry Andric case EFAULT: // Buf points outside the allocated address space.
3110b57cec5SDimitry Andric case EINTR: // A read from a slow device was interrupted before any data
3120b57cec5SDimitry Andric // arrived by the delivery of a signal.
3130b57cec5SDimitry Andric case EINVAL: // The pointer associated with fildes was negative.
3140b57cec5SDimitry Andric case EIO: // An I/O error occurred while reading from the file system.
3150b57cec5SDimitry Andric // The process group is orphaned.
3160b57cec5SDimitry Andric // The file is a regular file, nbyte is greater than 0, the
3170b57cec5SDimitry Andric // starting position is before the end-of-file, and the
3180b57cec5SDimitry Andric // starting position is greater than or equal to the offset
3190b57cec5SDimitry Andric // maximum established for the open file descriptor
3200b57cec5SDimitry Andric // associated with fildes.
3210b57cec5SDimitry Andric case EISDIR: // An attempt is made to read a directory.
3220b57cec5SDimitry Andric case ENOBUFS: // An attempt to allocate a memory buffer fails.
3230b57cec5SDimitry Andric case ENOMEM: // Insufficient memory is available.
3240b57cec5SDimitry Andric status = eConnectionStatusError;
3250b57cec5SDimitry Andric break; // Break to close....
3260b57cec5SDimitry Andric
3270b57cec5SDimitry Andric case ENOENT: // no such file or directory
3280b57cec5SDimitry Andric case EBADF: // fildes is not a valid file or socket descriptor open for
3290b57cec5SDimitry Andric // reading.
3300b57cec5SDimitry Andric case ENXIO: // An action is requested of a device that does not exist..
3310b57cec5SDimitry Andric // A requested action cannot be performed by the device.
3320b57cec5SDimitry Andric case ECONNRESET: // The connection is closed by the peer during a read
3330b57cec5SDimitry Andric // attempt on a socket.
3340b57cec5SDimitry Andric case ENOTCONN: // A read is attempted on an unconnected socket.
3350b57cec5SDimitry Andric status = eConnectionStatusLostConnection;
3360b57cec5SDimitry Andric break; // Break to close....
3370b57cec5SDimitry Andric
3380b57cec5SDimitry Andric case ETIMEDOUT: // A transmission timeout occurs during a read attempt on a
3390b57cec5SDimitry Andric // socket.
3400b57cec5SDimitry Andric status = eConnectionStatusTimedOut;
3410b57cec5SDimitry Andric return 0;
3420b57cec5SDimitry Andric
3430b57cec5SDimitry Andric default:
3440b57cec5SDimitry Andric LLDB_LOG(log, "this = {0}, unexpected error: {1}", this,
3450b57cec5SDimitry Andric llvm::sys::StrError(error_value));
3460b57cec5SDimitry Andric status = eConnectionStatusError;
3470b57cec5SDimitry Andric break; // Break to close....
3480b57cec5SDimitry Andric }
3490b57cec5SDimitry Andric
3500b57cec5SDimitry Andric return 0;
3510b57cec5SDimitry Andric }
3520b57cec5SDimitry Andric return bytes_read;
3530b57cec5SDimitry Andric }
3540b57cec5SDimitry Andric
Write(const void * src,size_t src_len,ConnectionStatus & status,Status * error_ptr)3550b57cec5SDimitry Andric size_t ConnectionFileDescriptor::Write(const void *src, size_t src_len,
3560b57cec5SDimitry Andric ConnectionStatus &status,
3570b57cec5SDimitry Andric Status *error_ptr) {
35881ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Connection);
3599dba64beSDimitry Andric LLDB_LOGF(log,
3609dba64beSDimitry Andric "%p ConnectionFileDescriptor::Write (src = %p, src_len = %" PRIu64
3619dba64beSDimitry Andric ")",
3620b57cec5SDimitry Andric static_cast<void *>(this), static_cast<const void *>(src),
3630b57cec5SDimitry Andric static_cast<uint64_t>(src_len));
3640b57cec5SDimitry Andric
3650b57cec5SDimitry Andric if (!IsConnected()) {
3660b57cec5SDimitry Andric if (error_ptr)
3670b57cec5SDimitry Andric error_ptr->SetErrorString("not connected");
3680b57cec5SDimitry Andric status = eConnectionStatusNoConnection;
3690b57cec5SDimitry Andric return 0;
3700b57cec5SDimitry Andric }
3710b57cec5SDimitry Andric
3729dba64beSDimitry Andric if (m_shutting_down) {
3739dba64beSDimitry Andric if (error_ptr)
3749dba64beSDimitry Andric error_ptr->SetErrorString("shutting down");
3759dba64beSDimitry Andric status = eConnectionStatusError;
3769dba64beSDimitry Andric return 0;
3779dba64beSDimitry Andric }
3789dba64beSDimitry Andric
3790b57cec5SDimitry Andric Status error;
3800b57cec5SDimitry Andric
3810b57cec5SDimitry Andric size_t bytes_sent = src_len;
382349cc55cSDimitry Andric error = m_io_sp->Write(src, bytes_sent);
3830b57cec5SDimitry Andric
3840b57cec5SDimitry Andric if (log) {
3859dba64beSDimitry Andric LLDB_LOGF(log,
3869dba64beSDimitry Andric "%p ConnectionFileDescriptor::Write(fd = %" PRIu64
3879dba64beSDimitry Andric ", src = %p, src_len = %" PRIu64 ") => %" PRIu64 " (error = %s)",
3880b57cec5SDimitry Andric static_cast<void *>(this),
389349cc55cSDimitry Andric static_cast<uint64_t>(m_io_sp->GetWaitableHandle()),
3900b57cec5SDimitry Andric static_cast<const void *>(src), static_cast<uint64_t>(src_len),
3910b57cec5SDimitry Andric static_cast<uint64_t>(bytes_sent), error.AsCString());
3920b57cec5SDimitry Andric }
3930b57cec5SDimitry Andric
3940b57cec5SDimitry Andric if (error_ptr)
3950b57cec5SDimitry Andric *error_ptr = error;
3960b57cec5SDimitry Andric
3970b57cec5SDimitry Andric if (error.Fail()) {
3980b57cec5SDimitry Andric switch (error.GetError()) {
3990b57cec5SDimitry Andric case EAGAIN:
4000b57cec5SDimitry Andric case EINTR:
4010b57cec5SDimitry Andric status = eConnectionStatusSuccess;
4020b57cec5SDimitry Andric return 0;
4030b57cec5SDimitry Andric
4040b57cec5SDimitry Andric case ECONNRESET: // The connection is closed by the peer during a read
4050b57cec5SDimitry Andric // attempt on a socket.
4060b57cec5SDimitry Andric case ENOTCONN: // A read is attempted on an unconnected socket.
4070b57cec5SDimitry Andric status = eConnectionStatusLostConnection;
4080b57cec5SDimitry Andric break; // Break to close....
4090b57cec5SDimitry Andric
4100b57cec5SDimitry Andric default:
4110b57cec5SDimitry Andric status = eConnectionStatusError;
4120b57cec5SDimitry Andric break; // Break to close....
4130b57cec5SDimitry Andric }
4140b57cec5SDimitry Andric
4150b57cec5SDimitry Andric return 0;
4160b57cec5SDimitry Andric }
4170b57cec5SDimitry Andric
4180b57cec5SDimitry Andric status = eConnectionStatusSuccess;
4190b57cec5SDimitry Andric return bytes_sent;
4200b57cec5SDimitry Andric }
4210b57cec5SDimitry Andric
GetURI()4220b57cec5SDimitry Andric std::string ConnectionFileDescriptor::GetURI() { return m_uri; }
4230b57cec5SDimitry Andric
4240b57cec5SDimitry Andric // This ConnectionFileDescriptor::BytesAvailable() uses select() via
4250b57cec5SDimitry Andric // SelectHelper
4260b57cec5SDimitry Andric //
4270b57cec5SDimitry Andric // PROS:
4280b57cec5SDimitry Andric // - select is consistent across most unix platforms
4290b57cec5SDimitry Andric // - The Apple specific version allows for unlimited fds in the fd_sets by
4300b57cec5SDimitry Andric // setting the _DARWIN_UNLIMITED_SELECT define prior to including the
4310b57cec5SDimitry Andric // required header files.
4320b57cec5SDimitry Andric // CONS:
4330b57cec5SDimitry Andric // - on non-Apple platforms, only supports file descriptors up to FD_SETSIZE.
4340b57cec5SDimitry Andric // This implementation will assert if it runs into that hard limit to let
4350b57cec5SDimitry Andric // users know that another ConnectionFileDescriptor::BytesAvailable() should
4360b57cec5SDimitry Andric // be used or a new version of ConnectionFileDescriptor::BytesAvailable()
4370b57cec5SDimitry Andric // should be written for the system that is running into the limitations.
4380b57cec5SDimitry Andric
4390b57cec5SDimitry Andric ConnectionStatus
BytesAvailable(const Timeout<std::micro> & timeout,Status * error_ptr)4400b57cec5SDimitry Andric ConnectionFileDescriptor::BytesAvailable(const Timeout<std::micro> &timeout,
4410b57cec5SDimitry Andric Status *error_ptr) {
4420b57cec5SDimitry Andric // Don't need to take the mutex here separately since we are only called from
4430b57cec5SDimitry Andric // Read. If we ever get used more generally we will need to lock here as
4440b57cec5SDimitry Andric // well.
4450b57cec5SDimitry Andric
44681ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Connection);
4470b57cec5SDimitry Andric LLDB_LOG(log, "this = {0}, timeout = {1}", this, timeout);
4480b57cec5SDimitry Andric
4490b57cec5SDimitry Andric // Make a copy of the file descriptors to make sure we don't have another
4500b57cec5SDimitry Andric // thread change these values out from under us and cause problems in the
4510b57cec5SDimitry Andric // loop below where like in FS_SET()
452349cc55cSDimitry Andric const IOObject::WaitableHandle handle = m_io_sp->GetWaitableHandle();
4530b57cec5SDimitry Andric const int pipe_fd = m_pipe.GetReadFileDescriptor();
4540b57cec5SDimitry Andric
4550b57cec5SDimitry Andric if (handle != IOObject::kInvalidHandleValue) {
4560b57cec5SDimitry Andric SelectHelper select_helper;
4570b57cec5SDimitry Andric if (timeout)
4580b57cec5SDimitry Andric select_helper.SetTimeout(*timeout);
4590b57cec5SDimitry Andric
4600b57cec5SDimitry Andric select_helper.FDSetRead(handle);
4619dba64beSDimitry Andric #if defined(_WIN32)
4620b57cec5SDimitry Andric // select() won't accept pipes on Windows. The entire Windows codepath
4630b57cec5SDimitry Andric // needs to be converted over to using WaitForMultipleObjects and event
4640b57cec5SDimitry Andric // HANDLEs, but for now at least this will allow ::select() to not return
4650b57cec5SDimitry Andric // an error.
4660b57cec5SDimitry Andric const bool have_pipe_fd = false;
4670b57cec5SDimitry Andric #else
4680b57cec5SDimitry Andric const bool have_pipe_fd = pipe_fd >= 0;
4690b57cec5SDimitry Andric #endif
4700b57cec5SDimitry Andric if (have_pipe_fd)
4710b57cec5SDimitry Andric select_helper.FDSetRead(pipe_fd);
4720b57cec5SDimitry Andric
473349cc55cSDimitry Andric while (handle == m_io_sp->GetWaitableHandle()) {
4740b57cec5SDimitry Andric
4750b57cec5SDimitry Andric Status error = select_helper.Select();
4760b57cec5SDimitry Andric
4770b57cec5SDimitry Andric if (error_ptr)
4780b57cec5SDimitry Andric *error_ptr = error;
4790b57cec5SDimitry Andric
4800b57cec5SDimitry Andric if (error.Fail()) {
4810b57cec5SDimitry Andric switch (error.GetError()) {
4820b57cec5SDimitry Andric case EBADF: // One of the descriptor sets specified an invalid
4830b57cec5SDimitry Andric // descriptor.
4840b57cec5SDimitry Andric return eConnectionStatusLostConnection;
4850b57cec5SDimitry Andric
4860b57cec5SDimitry Andric case EINVAL: // The specified time limit is invalid. One of its
4870b57cec5SDimitry Andric // components is negative or too large.
4880b57cec5SDimitry Andric default: // Other unknown error
4890b57cec5SDimitry Andric return eConnectionStatusError;
4900b57cec5SDimitry Andric
4910b57cec5SDimitry Andric case ETIMEDOUT:
4920b57cec5SDimitry Andric return eConnectionStatusTimedOut;
4930b57cec5SDimitry Andric
4940b57cec5SDimitry Andric case EAGAIN: // The kernel was (perhaps temporarily) unable to
4950b57cec5SDimitry Andric // allocate the requested number of file descriptors, or
4960b57cec5SDimitry Andric // we have non-blocking IO
4970b57cec5SDimitry Andric case EINTR: // A signal was delivered before the time limit
4980b57cec5SDimitry Andric // expired and before any of the selected events occurred.
4990b57cec5SDimitry Andric break; // Lets keep reading to until we timeout
5000b57cec5SDimitry Andric }
5010b57cec5SDimitry Andric } else {
5020b57cec5SDimitry Andric if (select_helper.FDIsSetRead(handle))
5030b57cec5SDimitry Andric return eConnectionStatusSuccess;
5040b57cec5SDimitry Andric
5050b57cec5SDimitry Andric if (select_helper.FDIsSetRead(pipe_fd)) {
5060b57cec5SDimitry Andric // There is an interrupt or exit command in the command pipe Read the
5070b57cec5SDimitry Andric // data from that pipe:
5080b57cec5SDimitry Andric char c;
5090b57cec5SDimitry Andric
510349cc55cSDimitry Andric ssize_t bytes_read =
511349cc55cSDimitry Andric llvm::sys::RetryAfterSignal(-1, ::read, pipe_fd, &c, 1);
5120b57cec5SDimitry Andric assert(bytes_read == 1);
513*5f757f3fSDimitry Andric UNUSED_IF_ASSERT_DISABLED(bytes_read);
5140b57cec5SDimitry Andric switch (c) {
5150b57cec5SDimitry Andric case 'q':
5169dba64beSDimitry Andric LLDB_LOGF(log,
5179dba64beSDimitry Andric "%p ConnectionFileDescriptor::BytesAvailable() "
5180b57cec5SDimitry Andric "got data: %c from the command channel.",
5190b57cec5SDimitry Andric static_cast<void *>(this), c);
5200b57cec5SDimitry Andric return eConnectionStatusEndOfFile;
5210b57cec5SDimitry Andric case 'i':
5220b57cec5SDimitry Andric // Interrupt the current read
5230b57cec5SDimitry Andric return eConnectionStatusInterrupted;
5240b57cec5SDimitry Andric }
5250b57cec5SDimitry Andric }
5260b57cec5SDimitry Andric }
5270b57cec5SDimitry Andric }
5280b57cec5SDimitry Andric }
5290b57cec5SDimitry Andric
5300b57cec5SDimitry Andric if (error_ptr)
5310b57cec5SDimitry Andric error_ptr->SetErrorString("not connected");
5320b57cec5SDimitry Andric return eConnectionStatusLostConnection;
5330b57cec5SDimitry Andric }
5340b57cec5SDimitry Andric
AcceptSocket(Socket::SocketProtocol socket_protocol,llvm::StringRef socket_name,llvm::function_ref<void (Socket &)> post_listen_callback,Status * error_ptr)535349cc55cSDimitry Andric lldb::ConnectionStatus ConnectionFileDescriptor::AcceptSocket(
536349cc55cSDimitry Andric Socket::SocketProtocol socket_protocol, llvm::StringRef socket_name,
537349cc55cSDimitry Andric llvm::function_ref<void(Socket &)> post_listen_callback,
5380b57cec5SDimitry Andric Status *error_ptr) {
539349cc55cSDimitry Andric Status error;
540349cc55cSDimitry Andric std::unique_ptr<Socket> listening_socket =
541349cc55cSDimitry Andric Socket::Create(socket_protocol, m_child_processes_inherit, error);
542349cc55cSDimitry Andric Socket *accepted_socket;
543349cc55cSDimitry Andric
544349cc55cSDimitry Andric if (!error.Fail())
545349cc55cSDimitry Andric error = listening_socket->Listen(socket_name, 5);
546349cc55cSDimitry Andric
547349cc55cSDimitry Andric if (!error.Fail()) {
548349cc55cSDimitry Andric post_listen_callback(*listening_socket);
549349cc55cSDimitry Andric error = listening_socket->Accept(accepted_socket);
5500b57cec5SDimitry Andric }
551349cc55cSDimitry Andric
552349cc55cSDimitry Andric if (!error.Fail()) {
553349cc55cSDimitry Andric m_io_sp.reset(accepted_socket);
554349cc55cSDimitry Andric m_uri.assign(socket_name.str());
5550b57cec5SDimitry Andric return eConnectionStatusSuccess;
5560b57cec5SDimitry Andric }
5570b57cec5SDimitry Andric
5580b57cec5SDimitry Andric if (error_ptr)
5590b57cec5SDimitry Andric *error_ptr = error;
5600b57cec5SDimitry Andric return eConnectionStatusError;
5610b57cec5SDimitry Andric }
5620b57cec5SDimitry Andric
5630b57cec5SDimitry Andric lldb::ConnectionStatus
ConnectSocket(Socket::SocketProtocol socket_protocol,llvm::StringRef socket_name,Status * error_ptr)564349cc55cSDimitry Andric ConnectionFileDescriptor::ConnectSocket(Socket::SocketProtocol socket_protocol,
565349cc55cSDimitry Andric llvm::StringRef socket_name,
5660b57cec5SDimitry Andric Status *error_ptr) {
567349cc55cSDimitry Andric Status error;
568349cc55cSDimitry Andric std::unique_ptr<Socket> socket =
569349cc55cSDimitry Andric Socket::Create(socket_protocol, m_child_processes_inherit, error);
570349cc55cSDimitry Andric
571349cc55cSDimitry Andric if (!error.Fail())
572349cc55cSDimitry Andric error = socket->Connect(socket_name);
573349cc55cSDimitry Andric
574349cc55cSDimitry Andric if (!error.Fail()) {
575349cc55cSDimitry Andric m_io_sp = std::move(socket);
576349cc55cSDimitry Andric m_uri.assign(socket_name.str());
577349cc55cSDimitry Andric return eConnectionStatusSuccess;
578349cc55cSDimitry Andric }
579349cc55cSDimitry Andric
5800b57cec5SDimitry Andric if (error_ptr)
5810b57cec5SDimitry Andric *error_ptr = error;
5820b57cec5SDimitry Andric return eConnectionStatusError;
5830b57cec5SDimitry Andric }
584349cc55cSDimitry Andric
AcceptNamedSocket(llvm::StringRef socket_name,socket_id_callback_type socket_id_callback,Status * error_ptr)585349cc55cSDimitry Andric ConnectionStatus ConnectionFileDescriptor::AcceptNamedSocket(
586349cc55cSDimitry Andric llvm::StringRef socket_name, socket_id_callback_type socket_id_callback,
587349cc55cSDimitry Andric Status *error_ptr) {
588349cc55cSDimitry Andric return AcceptSocket(
589349cc55cSDimitry Andric Socket::ProtocolUnixDomain, socket_name,
590349cc55cSDimitry Andric [socket_id_callback, socket_name](Socket &listening_socket) {
591349cc55cSDimitry Andric socket_id_callback(socket_name);
592349cc55cSDimitry Andric },
593349cc55cSDimitry Andric error_ptr);
594349cc55cSDimitry Andric }
595349cc55cSDimitry Andric
ConnectNamedSocket(llvm::StringRef socket_name,socket_id_callback_type socket_id_callback,Status * error_ptr)596349cc55cSDimitry Andric ConnectionStatus ConnectionFileDescriptor::ConnectNamedSocket(
597349cc55cSDimitry Andric llvm::StringRef socket_name, socket_id_callback_type socket_id_callback,
598349cc55cSDimitry Andric Status *error_ptr) {
599349cc55cSDimitry Andric return ConnectSocket(Socket::ProtocolUnixDomain, socket_name, error_ptr);
600349cc55cSDimitry Andric }
601349cc55cSDimitry Andric
AcceptAbstractSocket(llvm::StringRef socket_name,socket_id_callback_type socket_id_callback,Status * error_ptr)602349cc55cSDimitry Andric ConnectionStatus ConnectionFileDescriptor::AcceptAbstractSocket(
603349cc55cSDimitry Andric llvm::StringRef socket_name, socket_id_callback_type socket_id_callback,
604349cc55cSDimitry Andric Status *error_ptr) {
605349cc55cSDimitry Andric return AcceptSocket(
606349cc55cSDimitry Andric Socket::ProtocolUnixAbstract, socket_name,
607349cc55cSDimitry Andric [socket_id_callback, socket_name](Socket &listening_socket) {
608349cc55cSDimitry Andric socket_id_callback(socket_name);
609349cc55cSDimitry Andric },
610349cc55cSDimitry Andric error_ptr);
611349cc55cSDimitry Andric }
612349cc55cSDimitry Andric
ConnectAbstractSocket(llvm::StringRef socket_name,socket_id_callback_type socket_id_callback,Status * error_ptr)613349cc55cSDimitry Andric lldb::ConnectionStatus ConnectionFileDescriptor::ConnectAbstractSocket(
614349cc55cSDimitry Andric llvm::StringRef socket_name, socket_id_callback_type socket_id_callback,
615349cc55cSDimitry Andric Status *error_ptr) {
616349cc55cSDimitry Andric return ConnectSocket(Socket::ProtocolUnixAbstract, socket_name, error_ptr);
6170b57cec5SDimitry Andric }
6180b57cec5SDimitry Andric
6190b57cec5SDimitry Andric ConnectionStatus
AcceptTCP(llvm::StringRef socket_name,socket_id_callback_type socket_id_callback,Status * error_ptr)620349cc55cSDimitry Andric ConnectionFileDescriptor::AcceptTCP(llvm::StringRef socket_name,
621349cc55cSDimitry Andric socket_id_callback_type socket_id_callback,
6220b57cec5SDimitry Andric Status *error_ptr) {
623349cc55cSDimitry Andric ConnectionStatus ret = AcceptSocket(
624349cc55cSDimitry Andric Socket::ProtocolTcp, socket_name,
625349cc55cSDimitry Andric [socket_id_callback](Socket &listening_socket) {
626349cc55cSDimitry Andric uint16_t port =
627349cc55cSDimitry Andric static_cast<TCPSocket &>(listening_socket).GetLocalPortNumber();
628349cc55cSDimitry Andric socket_id_callback(std::to_string(port));
629349cc55cSDimitry Andric },
630349cc55cSDimitry Andric error_ptr);
631349cc55cSDimitry Andric if (ret == eConnectionStatusSuccess)
632349cc55cSDimitry Andric m_uri.assign(
633349cc55cSDimitry Andric static_cast<TCPSocket *>(m_io_sp.get())->GetRemoteConnectionURI());
634349cc55cSDimitry Andric return ret;
6355ffd83dbSDimitry Andric }
6365ffd83dbSDimitry Andric
637349cc55cSDimitry Andric ConnectionStatus
ConnectTCP(llvm::StringRef socket_name,socket_id_callback_type socket_id_callback,Status * error_ptr)638349cc55cSDimitry Andric ConnectionFileDescriptor::ConnectTCP(llvm::StringRef socket_name,
639349cc55cSDimitry Andric socket_id_callback_type socket_id_callback,
6400b57cec5SDimitry Andric Status *error_ptr) {
641349cc55cSDimitry Andric return ConnectSocket(Socket::ProtocolTcp, socket_name, error_ptr);
6420b57cec5SDimitry Andric }
6430b57cec5SDimitry Andric
644349cc55cSDimitry Andric ConnectionStatus
ConnectUDP(llvm::StringRef s,socket_id_callback_type socket_id_callback,Status * error_ptr)645349cc55cSDimitry Andric ConnectionFileDescriptor::ConnectUDP(llvm::StringRef s,
646349cc55cSDimitry Andric socket_id_callback_type socket_id_callback,
6470b57cec5SDimitry Andric Status *error_ptr) {
6480b57cec5SDimitry Andric if (error_ptr)
6495ffd83dbSDimitry Andric *error_ptr = Status();
6505ffd83dbSDimitry Andric llvm::Expected<std::unique_ptr<UDPSocket>> socket =
6515ffd83dbSDimitry Andric Socket::UdpConnect(s, m_child_processes_inherit);
6525ffd83dbSDimitry Andric if (!socket) {
6535ffd83dbSDimitry Andric if (error_ptr)
6545ffd83dbSDimitry Andric *error_ptr = socket.takeError();
6555ffd83dbSDimitry Andric else
65681ad6265SDimitry Andric LLDB_LOG_ERROR(GetLog(LLDBLog::Connection), socket.takeError(),
65781ad6265SDimitry Andric "tcp connect failed: {0}");
6580b57cec5SDimitry Andric return eConnectionStatusError;
6590b57cec5SDimitry Andric }
660349cc55cSDimitry Andric m_io_sp = std::move(*socket);
6615ffd83dbSDimitry Andric m_uri.assign(std::string(s));
6620b57cec5SDimitry Andric return eConnectionStatusSuccess;
6630b57cec5SDimitry Andric }
6640b57cec5SDimitry Andric
665349cc55cSDimitry Andric ConnectionStatus
ConnectFD(llvm::StringRef s,socket_id_callback_type socket_id_callback,Status * error_ptr)666349cc55cSDimitry Andric ConnectionFileDescriptor::ConnectFD(llvm::StringRef s,
667349cc55cSDimitry Andric socket_id_callback_type socket_id_callback,
668349cc55cSDimitry Andric Status *error_ptr) {
669349cc55cSDimitry Andric #if LLDB_ENABLE_POSIX
670349cc55cSDimitry Andric // Just passing a native file descriptor within this current process that
671349cc55cSDimitry Andric // is already opened (possibly from a service or other source).
672349cc55cSDimitry Andric int fd = -1;
673349cc55cSDimitry Andric
674349cc55cSDimitry Andric if (!s.getAsInteger(0, fd)) {
675349cc55cSDimitry Andric // We have what looks to be a valid file descriptor, but we should make
676349cc55cSDimitry Andric // sure it is. We currently are doing this by trying to get the flags
677349cc55cSDimitry Andric // from the file descriptor and making sure it isn't a bad fd.
678349cc55cSDimitry Andric errno = 0;
679349cc55cSDimitry Andric int flags = ::fcntl(fd, F_GETFL, 0);
680349cc55cSDimitry Andric if (flags == -1 || errno == EBADF) {
681349cc55cSDimitry Andric if (error_ptr)
682349cc55cSDimitry Andric error_ptr->SetErrorStringWithFormat("stale file descriptor: %s",
683349cc55cSDimitry Andric s.str().c_str());
684349cc55cSDimitry Andric m_io_sp.reset();
685349cc55cSDimitry Andric return eConnectionStatusError;
686349cc55cSDimitry Andric } else {
687349cc55cSDimitry Andric // Don't take ownership of a file descriptor that gets passed to us
688349cc55cSDimitry Andric // since someone else opened the file descriptor and handed it to us.
689349cc55cSDimitry Andric // TODO: Since are using a URL to open connection we should
690349cc55cSDimitry Andric // eventually parse options using the web standard where we have
691349cc55cSDimitry Andric // "fd://123?opt1=value;opt2=value" and we can have an option be
692349cc55cSDimitry Andric // "owns=1" or "owns=0" or something like this to allow us to specify
693349cc55cSDimitry Andric // this. For now, we assume we must assume we don't own it.
694349cc55cSDimitry Andric
695349cc55cSDimitry Andric std::unique_ptr<TCPSocket> tcp_socket;
696349cc55cSDimitry Andric tcp_socket = std::make_unique<TCPSocket>(fd, false, false);
697349cc55cSDimitry Andric // Try and get a socket option from this file descriptor to see if
698349cc55cSDimitry Andric // this is a socket and set m_is_socket accordingly.
699349cc55cSDimitry Andric int resuse;
700349cc55cSDimitry Andric bool is_socket =
701349cc55cSDimitry Andric !!tcp_socket->GetOption(SOL_SOCKET, SO_REUSEADDR, resuse);
702349cc55cSDimitry Andric if (is_socket)
703349cc55cSDimitry Andric m_io_sp = std::move(tcp_socket);
704349cc55cSDimitry Andric else
705349cc55cSDimitry Andric m_io_sp =
706349cc55cSDimitry Andric std::make_shared<NativeFile>(fd, File::eOpenOptionReadWrite, false);
707349cc55cSDimitry Andric m_uri = s.str();
708349cc55cSDimitry Andric return eConnectionStatusSuccess;
709349cc55cSDimitry Andric }
710349cc55cSDimitry Andric }
711349cc55cSDimitry Andric
712349cc55cSDimitry Andric if (error_ptr)
713349cc55cSDimitry Andric error_ptr->SetErrorStringWithFormat("invalid file descriptor: \"%s\"",
714349cc55cSDimitry Andric s.str().c_str());
715349cc55cSDimitry Andric m_io_sp.reset();
716349cc55cSDimitry Andric return eConnectionStatusError;
717349cc55cSDimitry Andric #endif // LLDB_ENABLE_POSIX
718349cc55cSDimitry Andric llvm_unreachable("this function should be only called w/ LLDB_ENABLE_POSIX");
719349cc55cSDimitry Andric }
720349cc55cSDimitry Andric
ConnectFile(llvm::StringRef s,socket_id_callback_type socket_id_callback,Status * error_ptr)721349cc55cSDimitry Andric ConnectionStatus ConnectionFileDescriptor::ConnectFile(
722349cc55cSDimitry Andric llvm::StringRef s, socket_id_callback_type socket_id_callback,
723349cc55cSDimitry Andric Status *error_ptr) {
724349cc55cSDimitry Andric #if LLDB_ENABLE_POSIX
725349cc55cSDimitry Andric std::string addr_str = s.str();
726349cc55cSDimitry Andric // file:///PATH
72706c3fb27SDimitry Andric int fd = FileSystem::Instance().Open(addr_str.c_str(), O_RDWR);
728349cc55cSDimitry Andric if (fd == -1) {
729349cc55cSDimitry Andric if (error_ptr)
730349cc55cSDimitry Andric error_ptr->SetErrorToErrno();
731349cc55cSDimitry Andric return eConnectionStatusError;
732349cc55cSDimitry Andric }
733349cc55cSDimitry Andric
734349cc55cSDimitry Andric if (::isatty(fd)) {
735349cc55cSDimitry Andric // Set up serial terminal emulation
736349cc55cSDimitry Andric struct termios options;
737349cc55cSDimitry Andric ::tcgetattr(fd, &options);
738349cc55cSDimitry Andric
739349cc55cSDimitry Andric // Set port speed to maximum
740349cc55cSDimitry Andric ::cfsetospeed(&options, B115200);
741349cc55cSDimitry Andric ::cfsetispeed(&options, B115200);
742349cc55cSDimitry Andric
743349cc55cSDimitry Andric // Raw input, disable echo and signals
744349cc55cSDimitry Andric options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
745349cc55cSDimitry Andric
746349cc55cSDimitry Andric // Make sure only one character is needed to return from a read
747349cc55cSDimitry Andric options.c_cc[VMIN] = 1;
748349cc55cSDimitry Andric options.c_cc[VTIME] = 0;
749349cc55cSDimitry Andric
750349cc55cSDimitry Andric llvm::sys::RetryAfterSignal(-1, ::tcsetattr, fd, TCSANOW, &options);
751349cc55cSDimitry Andric }
752349cc55cSDimitry Andric
753349cc55cSDimitry Andric m_io_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionReadWrite, true);
754349cc55cSDimitry Andric return eConnectionStatusSuccess;
755349cc55cSDimitry Andric #endif // LLDB_ENABLE_POSIX
756349cc55cSDimitry Andric llvm_unreachable("this function should be only called w/ LLDB_ENABLE_POSIX");
757349cc55cSDimitry Andric }
758349cc55cSDimitry Andric
ConnectSerialPort(llvm::StringRef s,socket_id_callback_type socket_id_callback,Status * error_ptr)759349cc55cSDimitry Andric ConnectionStatus ConnectionFileDescriptor::ConnectSerialPort(
760349cc55cSDimitry Andric llvm::StringRef s, socket_id_callback_type socket_id_callback,
761349cc55cSDimitry Andric Status *error_ptr) {
762349cc55cSDimitry Andric #if LLDB_ENABLE_POSIX
763349cc55cSDimitry Andric llvm::StringRef path, qs;
764349cc55cSDimitry Andric // serial:///PATH?k1=v1&k2=v2...
765349cc55cSDimitry Andric std::tie(path, qs) = s.split('?');
766349cc55cSDimitry Andric
767349cc55cSDimitry Andric llvm::Expected<SerialPort::Options> serial_options =
768349cc55cSDimitry Andric SerialPort::OptionsFromURL(qs);
769349cc55cSDimitry Andric if (!serial_options) {
770349cc55cSDimitry Andric if (error_ptr)
771349cc55cSDimitry Andric *error_ptr = serial_options.takeError();
772349cc55cSDimitry Andric else
773349cc55cSDimitry Andric llvm::consumeError(serial_options.takeError());
774349cc55cSDimitry Andric return eConnectionStatusError;
775349cc55cSDimitry Andric }
776349cc55cSDimitry Andric
77706c3fb27SDimitry Andric int fd = FileSystem::Instance().Open(path.str().c_str(), O_RDWR);
778349cc55cSDimitry Andric if (fd == -1) {
779349cc55cSDimitry Andric if (error_ptr)
780349cc55cSDimitry Andric error_ptr->SetErrorToErrno();
781349cc55cSDimitry Andric return eConnectionStatusError;
782349cc55cSDimitry Andric }
783349cc55cSDimitry Andric
784349cc55cSDimitry Andric llvm::Expected<std::unique_ptr<SerialPort>> serial_sp = SerialPort::Create(
785349cc55cSDimitry Andric fd, File::eOpenOptionReadWrite, serial_options.get(), true);
786349cc55cSDimitry Andric if (!serial_sp) {
787349cc55cSDimitry Andric if (error_ptr)
788349cc55cSDimitry Andric *error_ptr = serial_sp.takeError();
789349cc55cSDimitry Andric else
790349cc55cSDimitry Andric llvm::consumeError(serial_sp.takeError());
791349cc55cSDimitry Andric return eConnectionStatusError;
792349cc55cSDimitry Andric }
793349cc55cSDimitry Andric m_io_sp = std::move(serial_sp.get());
794349cc55cSDimitry Andric
795349cc55cSDimitry Andric return eConnectionStatusSuccess;
796349cc55cSDimitry Andric #endif // LLDB_ENABLE_POSIX
797349cc55cSDimitry Andric llvm_unreachable("this function should be only called w/ LLDB_ENABLE_POSIX");
7980b57cec5SDimitry Andric }
7990b57cec5SDimitry Andric
GetChildProcessesInherit() const8000b57cec5SDimitry Andric bool ConnectionFileDescriptor::GetChildProcessesInherit() const {
8010b57cec5SDimitry Andric return m_child_processes_inherit;
8020b57cec5SDimitry Andric }
8030b57cec5SDimitry Andric
SetChildProcessesInherit(bool child_processes_inherit)8040b57cec5SDimitry Andric void ConnectionFileDescriptor::SetChildProcessesInherit(
8050b57cec5SDimitry Andric bool child_processes_inherit) {
8060b57cec5SDimitry Andric m_child_processes_inherit = child_processes_inherit;
8070b57cec5SDimitry Andric }
8080b57cec5SDimitry Andric
InitializeSocket(Socket * socket)8090b57cec5SDimitry Andric void ConnectionFileDescriptor::InitializeSocket(Socket *socket) {
810349cc55cSDimitry Andric m_io_sp.reset(socket);
8110b57cec5SDimitry Andric m_uri = socket->GetRemoteConnectionURI();
8120b57cec5SDimitry Andric }
813