1 //===-- ConnectionFileDescriptorPosix.cpp -----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #if defined(__APPLE__)
11 // Enable this special support for Apple builds where we can have unlimited
12 // select bounds. We tried switching to poll() and kqueue and we were panicing
13 // the kernel, so we have to stick with select for now.
14 #define _DARWIN_UNLIMITED_SELECT
17 #include "lldb/Host/posix/ConnectionFileDescriptorPosix.h"
18 #include "lldb/Host/Config.h"
19 #include "lldb/Host/IOObject.h"
20 #include "lldb/Host/SocketAddress.h"
21 #include "lldb/Host/Socket.h"
28 #include <sys/types.h>
30 #ifndef LLDB_DISABLE_POSIX
35 // Other libraries and framework includes
36 #include "llvm/Support/ErrorHandling.h"
37 #if defined(__APPLE__)
38 #include "llvm/ADT/SmallVector.h"
41 #include "lldb/lldb-private-log.h"
42 #include "lldb/Core/Communication.h"
43 #include "lldb/Core/Log.h"
44 #include "lldb/Core/Timer.h"
45 #include "lldb/Host/Host.h"
46 #include "lldb/Host/Socket.h"
47 #include "lldb/Interpreter/Args.h"
50 using namespace lldb_private;
52 ConnectionFileDescriptor::ConnectionFileDescriptor(bool child_processes_inherit)
55 , m_mutex(Mutex::eMutexTypeRecursive)
56 , m_shutting_down(false)
57 , m_waiting_for_accept(false)
58 , m_child_processes_inherit(child_processes_inherit)
60 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION | LIBLLDB_LOG_OBJECT));
62 log->Printf("%p ConnectionFileDescriptor::ConnectionFileDescriptor ()", static_cast<void *>(this));
65 ConnectionFileDescriptor::ConnectionFileDescriptor(int fd, bool owns_fd)
68 , m_mutex(Mutex::eMutexTypeRecursive)
69 , m_shutting_down(false)
70 , m_waiting_for_accept(false)
71 , m_child_processes_inherit(false)
73 m_write_sp.reset(new File(fd, owns_fd));
74 m_read_sp.reset(new File(fd, false));
76 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION | LIBLLDB_LOG_OBJECT));
78 log->Printf("%p ConnectionFileDescriptor::ConnectionFileDescriptor (fd = %i, owns_fd = %i)", static_cast<void *>(this), fd,
83 ConnectionFileDescriptor::~ConnectionFileDescriptor()
85 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION | LIBLLDB_LOG_OBJECT));
87 log->Printf("%p ConnectionFileDescriptor::~ConnectionFileDescriptor ()", static_cast<void *>(this));
93 ConnectionFileDescriptor::OpenCommandPipe()
97 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
98 // Make the command file descriptor here:
99 Error result = m_pipe.CreateNew(m_child_processes_inherit);
100 if (!result.Success())
103 log->Printf("%p ConnectionFileDescriptor::OpenCommandPipe () - could not make pipe: %s", static_cast<void *>(this),
109 log->Printf("%p ConnectionFileDescriptor::OpenCommandPipe() - success readfd=%d writefd=%d", static_cast<void *>(this),
110 m_pipe.GetReadFileDescriptor(), m_pipe.GetWriteFileDescriptor());
115 ConnectionFileDescriptor::CloseCommandPipe()
117 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
119 log->Printf("%p ConnectionFileDescriptor::CloseCommandPipe()", static_cast<void *>(this));
125 ConnectionFileDescriptor::IsConnected() const
127 return (m_read_sp && m_read_sp->IsValid()) || (m_write_sp && m_write_sp->IsValid());
131 ConnectionFileDescriptor::Connect(const char *s, Error *error_ptr)
133 Mutex::Locker locker(m_mutex);
134 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
136 log->Printf("%p ConnectionFileDescriptor::Connect (url = '%s')", static_cast<void *>(this), s);
142 if (strstr(s, "listen://") == s)
144 // listen://HOST:PORT
145 return SocketListen(s + strlen("listen://"), error_ptr);
147 else if (strstr(s, "accept://") == s)
150 return NamedSocketAccept(s + strlen("accept://"), error_ptr);
152 else if (strstr(s, "unix-accept://") == s)
155 return NamedSocketAccept(s + strlen("unix-accept://"), error_ptr);
157 else if (strstr(s, "connect://") == s)
159 return ConnectTCP(s + strlen("connect://"), error_ptr);
161 else if (strstr(s, "tcp-connect://") == s)
163 return ConnectTCP(s + strlen("tcp-connect://"), error_ptr);
165 else if (strstr(s, "udp://") == s)
167 return ConnectUDP(s + strlen("udp://"), error_ptr);
169 #ifndef LLDB_DISABLE_POSIX
170 else if (strstr(s, "fd://") == s)
172 // Just passing a native file descriptor within this current process
173 // that is already opened (possibly from a service or other source).
174 s += strlen("fd://");
175 bool success = false;
176 int fd = Args::StringToSInt32(s, -1, 0, &success);
180 // We have what looks to be a valid file descriptor, but we
181 // should make sure it is. We currently are doing this by trying to
182 // get the flags from the file descriptor and making sure it
185 int flags = ::fcntl(fd, F_GETFL, 0);
186 if (flags == -1 || errno == EBADF)
189 error_ptr->SetErrorStringWithFormat("stale file descriptor: %s", s);
192 return eConnectionStatusError;
196 // Don't take ownership of a file descriptor that gets passed
197 // to us since someone else opened the file descriptor and
199 // TODO: Since are using a URL to open connection we should
200 // eventually parse options using the web standard where we
201 // have "fd://123?opt1=value;opt2=value" and we can have an
202 // option be "owns=1" or "owns=0" or something like this to
203 // allow us to specify this. For now, we assume we must
204 // assume we don't own it.
206 std::unique_ptr<Socket> tcp_socket;
207 tcp_socket.reset(new Socket(fd, Socket::ProtocolTcp, false));
208 // Try and get a socket option from this file descriptor to
209 // see if this is a socket and set m_is_socket accordingly.
211 bool is_socket = !!tcp_socket->GetOption(SOL_SOCKET, SO_REUSEADDR, resuse);
214 m_read_sp = std::move(tcp_socket);
215 m_write_sp = m_read_sp;
219 m_read_sp.reset(new File(fd, false));
220 m_write_sp.reset(new File(fd, false));
222 return eConnectionStatusSuccess;
227 error_ptr->SetErrorStringWithFormat("invalid file descriptor: \"fd://%s\"", s);
230 return eConnectionStatusError;
232 else if (strstr(s, "file://") == s)
235 const char *path = s + strlen("file://");
239 fd = ::open(path, O_RDWR);
240 } while (fd == -1 && errno == EINTR);
245 error_ptr->SetErrorToErrno();
246 return eConnectionStatusError;
251 // Set up serial terminal emulation
252 struct termios options;
253 ::tcgetattr(fd, &options);
255 // Set port speed to maximum
256 ::cfsetospeed(&options, B115200);
257 ::cfsetispeed(&options, B115200);
259 // Raw input, disable echo and signals
260 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
262 // Make sure only one character is needed to return from a read
263 options.c_cc[VMIN] = 1;
264 options.c_cc[VTIME] = 0;
266 ::tcsetattr(fd, TCSANOW, &options);
269 int flags = ::fcntl(fd, F_GETFL, 0);
272 if ((flags & O_NONBLOCK) == 0)
275 ::fcntl(fd, F_SETFL, flags);
278 m_read_sp.reset(new File(fd, true));
279 m_write_sp.reset(new File(fd, false));
280 return eConnectionStatusSuccess;
284 error_ptr->SetErrorStringWithFormat("unsupported connection URL: '%s'", s);
285 return eConnectionStatusError;
288 error_ptr->SetErrorString("invalid connect arguments");
289 return eConnectionStatusError;
293 ConnectionFileDescriptor::InterruptRead()
295 size_t bytes_written = 0;
296 Error result = m_pipe.Write("i", 1, bytes_written);
297 return result.Success();
301 ConnectionFileDescriptor::Disconnect(Error *error_ptr)
303 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
305 log->Printf("%p ConnectionFileDescriptor::Disconnect ()", static_cast<void *>(this));
307 ConnectionStatus status = eConnectionStatusSuccess;
312 log->Printf("%p ConnectionFileDescriptor::Disconnect(): Nothing to disconnect", static_cast<void *>(this));
313 return eConnectionStatusSuccess;
316 if (m_read_sp && m_read_sp->IsValid() && m_read_sp->GetFdType() == IOObject::eFDTypeSocket)
317 static_cast<Socket &>(*m_read_sp).PreDisconnect();
319 // Try to get the ConnectionFileDescriptor's mutex. If we fail, that is quite likely
320 // because somebody is doing a blocking read on our file descriptor. If that's the case,
321 // then send the "q" char to the command file channel so the read will wake up and the connection
322 // will then know to shut down.
324 m_shutting_down = true;
326 Mutex::Locker locker;
327 bool got_lock = locker.TryLock(m_mutex);
331 if (m_pipe.CanWrite())
333 size_t bytes_written = 0;
334 Error result = m_pipe.Write("q", 1, bytes_written);
336 log->Printf("%p ConnectionFileDescriptor::Disconnect(): Couldn't get the lock, sent 'q' to %d, error = '%s'.",
337 static_cast<void *>(this), m_pipe.GetWriteFileDescriptor(), result.AsCString());
341 log->Printf("%p ConnectionFileDescriptor::Disconnect(): Couldn't get the lock, but no command pipe is available.",
342 static_cast<void *>(this));
344 locker.Lock(m_mutex);
347 Error error = m_read_sp->Close();
348 Error error2 = m_write_sp->Close();
349 if (error.Fail() || error2.Fail())
350 status = eConnectionStatusError;
352 *error_ptr = error.Fail() ? error : error2;
354 m_shutting_down = false;
359 ConnectionFileDescriptor::Read(void *dst, size_t dst_len, uint32_t timeout_usec, ConnectionStatus &status, Error *error_ptr)
361 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
363 Mutex::Locker locker;
364 bool got_lock = locker.TryLock(m_mutex);
368 log->Printf("%p ConnectionFileDescriptor::Read () failed to get the connection lock.", static_cast<void *>(this));
370 error_ptr->SetErrorString("failed to get the connection lock for read.");
372 status = eConnectionStatusTimedOut;
375 else if (m_shutting_down)
376 return eConnectionStatusError;
378 status = BytesAvailable(timeout_usec, error_ptr);
379 if (status != eConnectionStatusSuccess)
383 size_t bytes_read = dst_len;
384 error = m_read_sp->Read(dst, bytes_read);
388 log->Printf("%p ConnectionFileDescriptor::Read() fd = %" PRIu64 ", dst = %p, dst_len = %" PRIu64 ") => %" PRIu64 ", error = %s",
389 static_cast<void *>(this), static_cast<uint64_t>(m_read_sp->GetWaitableHandle()), static_cast<void *>(dst),
390 static_cast<uint64_t>(dst_len), static_cast<uint64_t>(bytes_read), error.AsCString());
395 error.Clear(); // End-of-file. Do not automatically close; pass along for the end-of-file handlers.
396 status = eConnectionStatusEndOfFile;
404 uint32_t error_value = error.GetError();
407 case EAGAIN: // The file was marked for non-blocking I/O, and no data were ready to be read.
408 if (m_read_sp->GetFdType() == IOObject::eFDTypeSocket)
409 status = eConnectionStatusTimedOut;
411 status = eConnectionStatusSuccess;
414 case EFAULT: // Buf points outside the allocated address space.
415 case EINTR: // A read from a slow device was interrupted before any data arrived by the delivery of a signal.
416 case EINVAL: // The pointer associated with fildes was negative.
417 case EIO: // An I/O error occurred while reading from the file system.
418 // The process group is orphaned.
419 // The file is a regular file, nbyte is greater than 0,
420 // the starting position is before the end-of-file, and
421 // the starting position is greater than or equal to the
422 // offset maximum established for the open file
423 // descriptor associated with fildes.
424 case EISDIR: // An attempt is made to read a directory.
425 case ENOBUFS: // An attempt to allocate a memory buffer fails.
426 case ENOMEM: // Insufficient memory is available.
427 status = eConnectionStatusError;
428 break; // Break to close....
430 case ENOENT: // no such file or directory
431 case EBADF: // fildes is not a valid file or socket descriptor open for reading.
432 case ENXIO: // An action is requested of a device that does not exist..
433 // A requested action cannot be performed by the device.
434 case ECONNRESET: // The connection is closed by the peer during a read attempt on a socket.
435 case ENOTCONN: // A read is attempted on an unconnected socket.
436 status = eConnectionStatusLostConnection;
437 break; // Break to close....
439 case ETIMEDOUT: // A transmission timeout occurs during a read attempt on a socket.
440 status = eConnectionStatusTimedOut;
445 log->Printf("%p ConnectionFileDescriptor::Read (), unexpected error: %s", static_cast<void *>(this),
446 strerror(error_value));
447 status = eConnectionStatusError;
448 break; // Break to close....
457 ConnectionFileDescriptor::Write(const void *src, size_t src_len, ConnectionStatus &status, Error *error_ptr)
459 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
461 log->Printf("%p ConnectionFileDescriptor::Write (src = %p, src_len = %" PRIu64 ")", static_cast<void *>(this),
462 static_cast<const void *>(src), static_cast<uint64_t>(src_len));
467 error_ptr->SetErrorString("not connected");
468 status = eConnectionStatusNoConnection;
474 size_t bytes_sent = src_len;
475 error = m_write_sp->Write(src, bytes_sent);
479 log->Printf("%p ConnectionFileDescriptor::Write(fd = %" PRIu64 ", src = %p, src_len = %" PRIu64 ") => %" PRIu64 " (error = %s)",
480 static_cast<void *>(this), static_cast<uint64_t>(m_write_sp->GetWaitableHandle()), static_cast<const void *>(src),
481 static_cast<uint64_t>(src_len), static_cast<uint64_t>(bytes_sent), error.AsCString());
489 switch (error.GetError())
493 status = eConnectionStatusSuccess;
496 case ECONNRESET: // The connection is closed by the peer during a read attempt on a socket.
497 case ENOTCONN: // A read is attempted on an unconnected socket.
498 status = eConnectionStatusLostConnection;
499 break; // Break to close....
502 status = eConnectionStatusError;
503 break; // Break to close....
509 status = eConnectionStatusSuccess;
513 // This ConnectionFileDescriptor::BytesAvailable() uses select().
516 // - select is consistent across most unix platforms
517 // - The Apple specific version allows for unlimited fds in the fd_sets by
518 // setting the _DARWIN_UNLIMITED_SELECT define prior to including the
519 // required header files.
521 // - on non-Apple platforms, only supports file descriptors up to FD_SETSIZE.
522 // This implementation will assert if it runs into that hard limit to let
523 // users know that another ConnectionFileDescriptor::BytesAvailable() should
524 // be used or a new version of ConnectionFileDescriptor::BytesAvailable()
525 // should be written for the system that is running into the limitations.
527 #if defined(__APPLE__)
528 #define FD_SET_DATA(fds) fds.data()
530 #define FD_SET_DATA(fds) &fds
534 ConnectionFileDescriptor::BytesAvailable(uint32_t timeout_usec, Error *error_ptr)
536 // Don't need to take the mutex here separately since we are only called from Read. If we
537 // ever get used more generally we will need to lock here as well.
539 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_CONNECTION));
541 log->Printf("%p ConnectionFileDescriptor::BytesAvailable (timeout_usec = %u)", static_cast<void *>(this), timeout_usec);
543 struct timeval *tv_ptr;
545 if (timeout_usec == UINT32_MAX)
552 TimeValue time_value;
553 time_value.OffsetWithMicroSeconds(timeout_usec);
554 tv.tv_sec = time_value.seconds();
555 tv.tv_usec = time_value.microseconds();
559 // Make a copy of the file descriptors to make sure we don't
560 // have another thread change these values out from under us
561 // and cause problems in the loop below where like in FS_SET()
562 const IOObject::WaitableHandle handle = m_read_sp->GetWaitableHandle();
563 const int pipe_fd = m_pipe.GetReadFileDescriptor();
565 if (handle != IOObject::kInvalidHandleValue)
567 #if defined(_MSC_VER)
568 // select() won't accept pipes on Windows. The entire Windows codepath needs to be
569 // converted over to using WaitForMultipleObjects and event HANDLEs, but for now at least
570 // this will allow ::select() to not return an error.
571 const bool have_pipe_fd = false;
573 const bool have_pipe_fd = pipe_fd >= 0;
574 #if !defined(__APPLE__)
575 assert(handle < FD_SETSIZE);
577 assert(pipe_fd < FD_SETSIZE);
580 while (handle == m_read_sp->GetWaitableHandle())
582 const int nfds = std::max<int>(handle, pipe_fd) + 1;
583 #if defined(__APPLE__)
584 llvm::SmallVector<fd_set, 1> read_fds;
585 read_fds.resize((nfds / FD_SETSIZE) + 1);
586 for (size_t i = 0; i < read_fds.size(); ++i)
587 FD_ZERO(&read_fds[i]);
588 // FD_SET doesn't bounds check, it just happily walks off the end
589 // but we have taken care of making the extra storage with our
590 // SmallVector of fd_set objects
595 FD_SET(handle, FD_SET_DATA(read_fds));
597 FD_SET(pipe_fd, FD_SET_DATA(read_fds));
605 "%p ConnectionFileDescriptor::BytesAvailable() ::select (nfds=%i, fds={%i, %i}, NULL, NULL, timeout=%p)...",
606 static_cast<void *>(this), nfds, handle, pipe_fd, static_cast<void *>(tv_ptr));
608 log->Printf("%p ConnectionFileDescriptor::BytesAvailable() ::select (nfds=%i, fds={%i}, NULL, NULL, timeout=%p)...",
609 static_cast<void *>(this), nfds, handle, static_cast<void *>(tv_ptr));
612 const int num_set_fds = ::select(nfds, FD_SET_DATA(read_fds), NULL, NULL, tv_ptr);
614 error.SetErrorToErrno();
621 log->Printf("%p ConnectionFileDescriptor::BytesAvailable() ::select (nfds=%i, fds={%i, %i}, NULL, NULL, timeout=%p) "
623 static_cast<void *>(this), nfds, handle, pipe_fd, static_cast<void *>(tv_ptr), num_set_fds,
626 log->Printf("%p ConnectionFileDescriptor::BytesAvailable() ::select (nfds=%i, fds={%i}, NULL, NULL, timeout=%p) => "
628 static_cast<void *>(this), nfds, handle, static_cast<void *>(tv_ptr), num_set_fds, error.AsCString());
636 switch (error.GetError())
638 case EBADF: // One of the descriptor sets specified an invalid descriptor.
639 return eConnectionStatusLostConnection;
641 case EINVAL: // The specified time limit is invalid. One of its components is negative or too large.
642 default: // Other unknown error
643 return eConnectionStatusError;
645 case EAGAIN: // The kernel was (perhaps temporarily) unable to
646 // allocate the requested number of file descriptors,
647 // or we have non-blocking IO
648 case EINTR: // A signal was delivered before the time limit
649 // expired and before any of the selected events
651 break; // Lets keep reading to until we timeout
654 else if (num_set_fds == 0)
656 return eConnectionStatusTimedOut;
658 else if (num_set_fds > 0)
660 if (FD_ISSET(handle, FD_SET_DATA(read_fds)))
661 return eConnectionStatusSuccess;
662 if (have_pipe_fd && FD_ISSET(pipe_fd, FD_SET_DATA(read_fds)))
664 // We got a command to exit. Read the data from that pipe:
670 bytes_read = ::read(pipe_fd, buffer, sizeof(buffer));
671 } while (bytes_read < 0 && errno == EINTR);
677 log->Printf("%p ConnectionFileDescriptor::BytesAvailable() got data: %*s from the command channel.",
678 static_cast<void *>(this), static_cast<int>(bytes_read), buffer);
679 return eConnectionStatusEndOfFile;
681 // Interrupt the current read
682 return eConnectionStatusInterrupted;
690 error_ptr->SetErrorString("not connected");
691 return eConnectionStatusLostConnection;
695 ConnectionFileDescriptor::NamedSocketAccept(const char *socket_name, Error *error_ptr)
697 Socket *socket = nullptr;
698 Error error = Socket::UnixDomainAccept(socket_name, m_child_processes_inherit, socket);
701 m_write_sp.reset(socket);
702 m_read_sp = m_write_sp;
703 return (error.Success()) ? eConnectionStatusSuccess : eConnectionStatusError;
707 ConnectionFileDescriptor::NamedSocketConnect(const char *socket_name, Error *error_ptr)
709 Socket *socket = nullptr;
710 Error error = Socket::UnixDomainConnect(socket_name, m_child_processes_inherit, socket);
713 m_write_sp.reset(socket);
714 m_read_sp = m_write_sp;
715 return (error.Success()) ? eConnectionStatusSuccess : eConnectionStatusError;
719 ConnectionFileDescriptor::SocketListen(const char *s, Error *error_ptr)
721 m_port_predicate.SetValue(0, eBroadcastNever);
723 Socket *socket = nullptr;
724 m_waiting_for_accept = true;
725 Error error = Socket::TcpListen(s, m_child_processes_inherit, socket, &m_port_predicate);
729 return eConnectionStatusError;
731 std::unique_ptr<Socket> listening_socket_up;
733 listening_socket_up.reset(socket);
735 error = listening_socket_up->BlockingAccept(s, m_child_processes_inherit, socket);
736 listening_socket_up.reset();
740 return eConnectionStatusError;
742 m_write_sp.reset(socket);
743 m_read_sp = m_write_sp;
744 return (error.Success()) ? eConnectionStatusSuccess : eConnectionStatusError;
748 ConnectionFileDescriptor::ConnectTCP(const char *s, Error *error_ptr)
750 Socket *socket = nullptr;
751 Error error = Socket::TcpConnect(s, m_child_processes_inherit, socket);
754 m_write_sp.reset(socket);
755 m_read_sp = m_write_sp;
756 return (error.Success()) ? eConnectionStatusSuccess : eConnectionStatusError;
760 ConnectionFileDescriptor::ConnectUDP(const char *s, Error *error_ptr)
762 Socket *send_socket = nullptr;
763 Socket *recv_socket = nullptr;
764 Error error = Socket::UdpConnect(s, m_child_processes_inherit, send_socket, recv_socket);
767 m_write_sp.reset(send_socket);
768 m_read_sp.reset(recv_socket);
769 return (error.Success()) ? eConnectionStatusSuccess : eConnectionStatusError;
773 ConnectionFileDescriptor::GetListeningPort(uint32_t timeout_sec)
775 uint16_t bound_port = 0;
776 if (timeout_sec == UINT32_MAX)
777 m_port_predicate.WaitForValueNotEqualTo(0, bound_port);
780 TimeValue timeout = TimeValue::Now();
781 timeout.OffsetWithSeconds(timeout_sec);
782 m_port_predicate.WaitForValueNotEqualTo(0, bound_port, &timeout);
788 ConnectionFileDescriptor::GetChildProcessesInherit() const
790 return m_child_processes_inherit;
794 ConnectionFileDescriptor::SetChildProcessesInherit(bool child_processes_inherit)
796 m_child_processes_inherit = child_processes_inherit;