LLDB  mainline
ConnectionFileDescriptorPosix.cpp
Go to the documentation of this file.
1 //===-- ConnectionFileDescriptorPosix.cpp ---------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #if defined(__APPLE__)
10 // Enable this special support for Apple builds where we can have unlimited
11 // select bounds. We tried switching to poll() and kqueue and we were panicing
12 // the kernel, so we have to stick with select for now.
13 #define _DARWIN_UNLIMITED_SELECT
14 #endif
15 
17 #include "lldb/Host/Config.h"
18 #include "lldb/Host/Socket.h"
21 #include "lldb/Utility/Timeout.h"
22 
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/types.h>
28 
29 #if LLDB_ENABLE_POSIX
30 #include <termios.h>
31 #include <unistd.h>
32 #endif
33 
34 #include <memory>
35 #include <sstream>
36 
37 #include "llvm/Support/Errno.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #if defined(__APPLE__)
40 #include "llvm/ADT/SmallVector.h"
41 #endif
42 #include "lldb/Host/Host.h"
43 #include "lldb/Host/Socket.h"
46 #include "lldb/Utility/Log.h"
48 #include "lldb/Utility/Timer.h"
49 
50 using namespace lldb;
51 using namespace lldb_private;
52 
53 const char *ConnectionFileDescriptor::LISTEN_SCHEME = "listen";
54 const char *ConnectionFileDescriptor::ACCEPT_SCHEME = "accept";
55 const char *ConnectionFileDescriptor::UNIX_ACCEPT_SCHEME = "unix-accept";
56 const char *ConnectionFileDescriptor::CONNECT_SCHEME = "connect";
57 const char *ConnectionFileDescriptor::TCP_CONNECT_SCHEME = "tcp-connect";
58 const char *ConnectionFileDescriptor::UDP_SCHEME = "udp";
59 const char *ConnectionFileDescriptor::UNIX_CONNECT_SCHEME = "unix-connect";
60 const char *ConnectionFileDescriptor::UNIX_ABSTRACT_CONNECT_SCHEME =
61  "unix-abstract-connect";
62 const char *ConnectionFileDescriptor::FD_SCHEME = "fd";
63 const char *ConnectionFileDescriptor::FILE_SCHEME = "file";
64 
65 namespace {
66 
67 llvm::Optional<llvm::StringRef> GetURLAddress(llvm::StringRef url,
68  llvm::StringRef scheme) {
69  if (!url.consume_front(scheme))
70  return llvm::None;
71  if (!url.consume_front("://"))
72  return llvm::None;
73  return url;
74 }
75 }
76 
77 ConnectionFileDescriptor::ConnectionFileDescriptor(bool child_processes_inherit)
78  : Connection(), m_pipe(), m_mutex(), m_shutting_down(false),
79  m_waiting_for_accept(false),
80  m_child_processes_inherit(child_processes_inherit) {
83  LLDB_LOGF(log, "%p ConnectionFileDescriptor::ConnectionFileDescriptor ()",
84  static_cast<void *>(this));
85 }
86 
88  : Connection(), m_pipe(), m_mutex(), m_shutting_down(false),
90  m_write_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionWrite, owns_fd);
91  m_read_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionRead, false);
92 
95  LLDB_LOGF(log,
96  "%p ConnectionFileDescriptor::ConnectionFileDescriptor (fd = "
97  "%i, owns_fd = %i)",
98  static_cast<void *>(this), fd, owns_fd);
100 }
101 
103  : Connection(), m_pipe(), m_mutex(), m_shutting_down(false),
105  InitializeSocket(socket);
106 }
107 
111  LLDB_LOGF(log, "%p ConnectionFileDescriptor::~ConnectionFileDescriptor ()",
112  static_cast<void *>(this));
113  Disconnect(nullptr);
115 }
116 
119 
121  // Make the command file descriptor here:
122  Status result = m_pipe.CreateNew(m_child_processes_inherit);
123  if (!result.Success()) {
124  LLDB_LOGF(log,
125  "%p ConnectionFileDescriptor::OpenCommandPipe () - could not "
126  "make pipe: %s",
127  static_cast<void *>(this), result.AsCString());
128  } else {
129  LLDB_LOGF(log,
130  "%p ConnectionFileDescriptor::OpenCommandPipe() - success "
131  "readfd=%d writefd=%d",
132  static_cast<void *>(this), m_pipe.GetReadFileDescriptor(),
133  m_pipe.GetWriteFileDescriptor());
134  }
135 }
136 
139  LLDB_LOGF(log, "%p ConnectionFileDescriptor::CloseCommandPipe()",
140  static_cast<void *>(this));
141 
142  m_pipe.Close();
143 }
144 
146  return (m_read_sp && m_read_sp->IsValid()) ||
147  (m_write_sp && m_write_sp->IsValid());
148 }
149 
151  Status *error_ptr) {
152  std::lock_guard<std::recursive_mutex> guard(m_mutex);
154  LLDB_LOGF(log, "%p ConnectionFileDescriptor::Connect (url = '%s')",
155  static_cast<void *>(this), path.str().c_str());
156 
157  OpenCommandPipe();
158 
159  if (!path.empty()) {
160  llvm::Optional<llvm::StringRef> addr;
161  if ((addr = GetURLAddress(path, LISTEN_SCHEME))) {
162  // listen://HOST:PORT
163  return SocketListenAndAccept(*addr, error_ptr);
164  } else if ((addr = GetURLAddress(path, ACCEPT_SCHEME))) {
165  // unix://SOCKNAME
166  return NamedSocketAccept(*addr, error_ptr);
167  } else if ((addr = GetURLAddress(path, UNIX_ACCEPT_SCHEME))) {
168  // unix://SOCKNAME
169  return NamedSocketAccept(*addr, error_ptr);
170  } else if ((addr = GetURLAddress(path, CONNECT_SCHEME))) {
171  return ConnectTCP(*addr, error_ptr);
172  } else if ((addr = GetURLAddress(path, TCP_CONNECT_SCHEME))) {
173  return ConnectTCP(*addr, error_ptr);
174  } else if ((addr = GetURLAddress(path, UDP_SCHEME))) {
175  return ConnectUDP(*addr, error_ptr);
176  } else if ((addr = GetURLAddress(path, UNIX_CONNECT_SCHEME))) {
177  // unix-connect://SOCKNAME
178  return NamedSocketConnect(*addr, error_ptr);
179  } else if ((addr = GetURLAddress(path, UNIX_ABSTRACT_CONNECT_SCHEME))) {
180  // unix-abstract-connect://SOCKNAME
181  return UnixAbstractSocketConnect(*addr, error_ptr);
182  }
183 #if LLDB_ENABLE_POSIX
184  else if ((addr = GetURLAddress(path, FD_SCHEME))) {
185  // Just passing a native file descriptor within this current process that
186  // is already opened (possibly from a service or other source).
187  int fd = -1;
188 
189  if (!addr->getAsInteger(0, fd)) {
190  // We have what looks to be a valid file descriptor, but we should make
191  // sure it is. We currently are doing this by trying to get the flags
192  // from the file descriptor and making sure it isn't a bad fd.
193  errno = 0;
194  int flags = ::fcntl(fd, F_GETFL, 0);
195  if (flags == -1 || errno == EBADF) {
196  if (error_ptr)
197  error_ptr->SetErrorStringWithFormat("stale file descriptor: %s",
198  path.str().c_str());
199  m_read_sp.reset();
200  m_write_sp.reset();
201  return eConnectionStatusError;
202  } else {
203  // Don't take ownership of a file descriptor that gets passed to us
204  // since someone else opened the file descriptor and handed it to us.
205  // TODO: Since are using a URL to open connection we should
206  // eventually parse options using the web standard where we have
207  // "fd://123?opt1=value;opt2=value" and we can have an option be
208  // "owns=1" or "owns=0" or something like this to allow us to specify
209  // this. For now, we assume we must assume we don't own it.
210 
211  std::unique_ptr<TCPSocket> tcp_socket;
212  tcp_socket = std::make_unique<TCPSocket>(fd, false, false);
213  // Try and get a socket option from this file descriptor to see if
214  // this is a socket and set m_is_socket accordingly.
215  int resuse;
216  bool is_socket =
217  !!tcp_socket->GetOption(SOL_SOCKET, SO_REUSEADDR, resuse);
218  if (is_socket) {
219  m_read_sp = std::move(tcp_socket);
221  } else {
222  m_read_sp =
223  std::make_shared<NativeFile>(fd, File::eOpenOptionRead, false);
224  m_write_sp =
225  std::make_shared<NativeFile>(fd, File::eOpenOptionWrite, false);
226  }
227  m_uri = std::string(*addr);
229  }
230  }
231 
232  if (error_ptr)
233  error_ptr->SetErrorStringWithFormat("invalid file descriptor: \"%s\"",
234  path.str().c_str());
235  m_read_sp.reset();
236  m_write_sp.reset();
237  return eConnectionStatusError;
238  } else if ((addr = GetURLAddress(path, FILE_SCHEME))) {
239  std::string addr_str = addr->str();
240  // file:///PATH
241  int fd = llvm::sys::RetryAfterSignal(-1, ::open, addr_str.c_str(), O_RDWR);
242  if (fd == -1) {
243  if (error_ptr)
244  error_ptr->SetErrorToErrno();
245  return eConnectionStatusError;
246  }
247 
248  if (::isatty(fd)) {
249  // Set up serial terminal emulation
250  struct termios options;
251  ::tcgetattr(fd, &options);
252 
253  // Set port speed to maximum
254  ::cfsetospeed(&options, B115200);
255  ::cfsetispeed(&options, B115200);
256 
257  // Raw input, disable echo and signals
258  options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
259 
260  // Make sure only one character is needed to return from a read
261  options.c_cc[VMIN] = 1;
262  options.c_cc[VTIME] = 0;
263 
264  llvm::sys::RetryAfterSignal(-1, ::tcsetattr, fd, TCSANOW, &options);
265  }
266 
267  int flags = ::fcntl(fd, F_GETFL, 0);
268  if (flags >= 0) {
269  if ((flags & O_NONBLOCK) == 0) {
270  flags |= O_NONBLOCK;
271  ::fcntl(fd, F_SETFL, flags);
272  }
273  }
274  m_read_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionRead, true);
275  m_write_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionWrite, false);
277  }
278 #endif
279  if (error_ptr)
280  error_ptr->SetErrorStringWithFormat("unsupported connection URL: '%s'",
281  path.str().c_str());
282  return eConnectionStatusError;
283  }
284  if (error_ptr)
285  error_ptr->SetErrorString("invalid connect arguments");
286  return eConnectionStatusError;
287 }
288 
290  size_t bytes_written = 0;
291  Status result = m_pipe.Write("i", 1, bytes_written);
292  return result.Success();
293 }
294 
297  LLDB_LOGF(log, "%p ConnectionFileDescriptor::Disconnect ()",
298  static_cast<void *>(this));
299 
301 
302  if (!IsConnected()) {
303  LLDB_LOGF(
304  log, "%p ConnectionFileDescriptor::Disconnect(): Nothing to disconnect",
305  static_cast<void *>(this));
307  }
308 
309  if (m_read_sp && m_read_sp->IsValid() &&
310  m_read_sp->GetFdType() == IOObject::eFDTypeSocket)
311  static_cast<Socket &>(*m_read_sp).PreDisconnect();
312 
313  // Try to get the ConnectionFileDescriptor's mutex. If we fail, that is
314  // quite likely because somebody is doing a blocking read on our file
315  // descriptor. If that's the case, then send the "q" char to the command
316  // file channel so the read will wake up and the connection will then know to
317  // shut down.
318  std::unique_lock<std::recursive_mutex> locker(m_mutex, std::defer_lock);
319  if (!locker.try_lock()) {
320  if (m_pipe.CanWrite()) {
321  size_t bytes_written = 0;
322  Status result = m_pipe.Write("q", 1, bytes_written);
323  LLDB_LOGF(log,
324  "%p ConnectionFileDescriptor::Disconnect(): Couldn't get "
325  "the lock, sent 'q' to %d, error = '%s'.",
326  static_cast<void *>(this), m_pipe.GetWriteFileDescriptor(),
327  result.AsCString());
328  } else if (log) {
329  LLDB_LOGF(log,
330  "%p ConnectionFileDescriptor::Disconnect(): Couldn't get the "
331  "lock, but no command pipe is available.",
332  static_cast<void *>(this));
333  }
334  locker.lock();
335  }
336 
337  // Prevents reads and writes during shutdown.
338  m_shutting_down = true;
339 
340  Status error = m_read_sp->Close();
341  Status error2 = m_write_sp->Close();
342  if (error.Fail() || error2.Fail())
343  status = eConnectionStatusError;
344  if (error_ptr)
345  *error_ptr = error.Fail() ? error : error2;
346 
347  // Close any pipes we were using for async interrupts
348  m_pipe.Close();
349 
350  m_uri.clear();
351  m_shutting_down = false;
352  return status;
353 }
354 
355 size_t ConnectionFileDescriptor::Read(void *dst, size_t dst_len,
356  const Timeout<std::micro> &timeout,
357  ConnectionStatus &status,
358  Status *error_ptr) {
360 
361  std::unique_lock<std::recursive_mutex> locker(m_mutex, std::defer_lock);
362  if (!locker.try_lock()) {
363  LLDB_LOGF(log,
364  "%p ConnectionFileDescriptor::Read () failed to get the "
365  "connection lock.",
366  static_cast<void *>(this));
367  if (error_ptr)
368  error_ptr->SetErrorString("failed to get the connection lock for read.");
369 
370  status = eConnectionStatusTimedOut;
371  return 0;
372  }
373 
374  if (m_shutting_down) {
375  if (error_ptr)
376  error_ptr->SetErrorString("shutting down");
377  status = eConnectionStatusError;
378  return 0;
379  }
380 
381  status = BytesAvailable(timeout, error_ptr);
382  if (status != eConnectionStatusSuccess)
383  return 0;
384 
385  Status error;
386  size_t bytes_read = dst_len;
387  error = m_read_sp->Read(dst, bytes_read);
388 
389  if (log) {
390  LLDB_LOGF(log,
391  "%p ConnectionFileDescriptor::Read() fd = %" PRIu64
392  ", dst = %p, dst_len = %" PRIu64 ") => %" PRIu64 ", error = %s",
393  static_cast<void *>(this),
394  static_cast<uint64_t>(m_read_sp->GetWaitableHandle()),
395  static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
396  static_cast<uint64_t>(bytes_read), error.AsCString());
397  }
398 
399  if (bytes_read == 0) {
400  error.Clear(); // End-of-file. Do not automatically close; pass along for
401  // the end-of-file handlers.
403  }
404 
405  if (error_ptr)
406  *error_ptr = error;
407 
408  if (error.Fail()) {
409  uint32_t error_value = error.GetError();
410  switch (error_value) {
411  case EAGAIN: // The file was marked for non-blocking I/O, and no data were
412  // ready to be read.
413  if (m_read_sp->GetFdType() == IOObject::eFDTypeSocket)
414  status = eConnectionStatusTimedOut;
415  else
416  status = eConnectionStatusSuccess;
417  return 0;
418 
419  case EFAULT: // Buf points outside the allocated address space.
420  case EINTR: // A read from a slow device was interrupted before any data
421  // arrived by the delivery of a signal.
422  case EINVAL: // The pointer associated with fildes was negative.
423  case EIO: // An I/O error occurred while reading from the file system.
424  // The process group is orphaned.
425  // The file is a regular file, nbyte is greater than 0, the
426  // starting position is before the end-of-file, and the
427  // starting position is greater than or equal to the offset
428  // maximum established for the open file descriptor
429  // associated with fildes.
430  case EISDIR: // An attempt is made to read a directory.
431  case ENOBUFS: // An attempt to allocate a memory buffer fails.
432  case ENOMEM: // Insufficient memory is available.
433  status = eConnectionStatusError;
434  break; // Break to close....
435 
436  case ENOENT: // no such file or directory
437  case EBADF: // fildes is not a valid file or socket descriptor open for
438  // reading.
439  case ENXIO: // An action is requested of a device that does not exist..
440  // A requested action cannot be performed by the device.
441  case ECONNRESET: // The connection is closed by the peer during a read
442  // attempt on a socket.
443  case ENOTCONN: // A read is attempted on an unconnected socket.
445  break; // Break to close....
446 
447  case ETIMEDOUT: // A transmission timeout occurs during a read attempt on a
448  // socket.
449  status = eConnectionStatusTimedOut;
450  return 0;
451 
452  default:
453  LLDB_LOG(log, "this = {0}, unexpected error: {1}", this,
454  llvm::sys::StrError(error_value));
455  status = eConnectionStatusError;
456  break; // Break to close....
457  }
458 
459  return 0;
460  }
461  return bytes_read;
462 }
463 
464 size_t ConnectionFileDescriptor::Write(const void *src, size_t src_len,
465  ConnectionStatus &status,
466  Status *error_ptr) {
468  LLDB_LOGF(log,
469  "%p ConnectionFileDescriptor::Write (src = %p, src_len = %" PRIu64
470  ")",
471  static_cast<void *>(this), static_cast<const void *>(src),
472  static_cast<uint64_t>(src_len));
473 
474  if (!IsConnected()) {
475  if (error_ptr)
476  error_ptr->SetErrorString("not connected");
478  return 0;
479  }
480 
481  if (m_shutting_down) {
482  if (error_ptr)
483  error_ptr->SetErrorString("shutting down");
484  status = eConnectionStatusError;
485  return 0;
486  }
487 
488  Status error;
489 
490  size_t bytes_sent = src_len;
491  error = m_write_sp->Write(src, bytes_sent);
492 
493  if (log) {
494  LLDB_LOGF(log,
495  "%p ConnectionFileDescriptor::Write(fd = %" PRIu64
496  ", src = %p, src_len = %" PRIu64 ") => %" PRIu64 " (error = %s)",
497  static_cast<void *>(this),
498  static_cast<uint64_t>(m_write_sp->GetWaitableHandle()),
499  static_cast<const void *>(src), static_cast<uint64_t>(src_len),
500  static_cast<uint64_t>(bytes_sent), error.AsCString());
501  }
502 
503  if (error_ptr)
504  *error_ptr = error;
505 
506  if (error.Fail()) {
507  switch (error.GetError()) {
508  case EAGAIN:
509  case EINTR:
510  status = eConnectionStatusSuccess;
511  return 0;
512 
513  case ECONNRESET: // The connection is closed by the peer during a read
514  // attempt on a socket.
515  case ENOTCONN: // A read is attempted on an unconnected socket.
517  break; // Break to close....
518 
519  default:
520  status = eConnectionStatusError;
521  break; // Break to close....
522  }
523 
524  return 0;
525  }
526 
527  status = eConnectionStatusSuccess;
528  return bytes_sent;
529 }
530 
531 std::string ConnectionFileDescriptor::GetURI() { return m_uri; }
532 
533 // This ConnectionFileDescriptor::BytesAvailable() uses select() via
534 // SelectHelper
535 //
536 // PROS:
537 // - select is consistent across most unix platforms
538 // - The Apple specific version allows for unlimited fds in the fd_sets by
539 // setting the _DARWIN_UNLIMITED_SELECT define prior to including the
540 // required header files.
541 // CONS:
542 // - on non-Apple platforms, only supports file descriptors up to FD_SETSIZE.
543 // This implementation will assert if it runs into that hard limit to let
544 // users know that another ConnectionFileDescriptor::BytesAvailable() should
545 // be used or a new version of ConnectionFileDescriptor::BytesAvailable()
546 // should be written for the system that is running into the limitations.
547 
550  Status *error_ptr) {
551  // Don't need to take the mutex here separately since we are only called from
552  // Read. If we ever get used more generally we will need to lock here as
553  // well.
554 
556  LLDB_LOG(log, "this = {0}, timeout = {1}", this, timeout);
557 
558  // Make a copy of the file descriptors to make sure we don't have another
559  // thread change these values out from under us and cause problems in the
560  // loop below where like in FS_SET()
561  const IOObject::WaitableHandle handle = m_read_sp->GetWaitableHandle();
562  const int pipe_fd = m_pipe.GetReadFileDescriptor();
563 
564  if (handle != IOObject::kInvalidHandleValue) {
565  SelectHelper select_helper;
566  if (timeout)
567  select_helper.SetTimeout(*timeout);
568 
569  select_helper.FDSetRead(handle);
570 #if defined(_WIN32)
571  // select() won't accept pipes on Windows. The entire Windows codepath
572  // needs to be converted over to using WaitForMultipleObjects and event
573  // HANDLEs, but for now at least this will allow ::select() to not return
574  // an error.
575  const bool have_pipe_fd = false;
576 #else
577  const bool have_pipe_fd = pipe_fd >= 0;
578 #endif
579  if (have_pipe_fd)
580  select_helper.FDSetRead(pipe_fd);
581 
582  while (handle == m_read_sp->GetWaitableHandle()) {
583 
584  Status error = select_helper.Select();
585 
586  if (error_ptr)
587  *error_ptr = error;
588 
589  if (error.Fail()) {
590  switch (error.GetError()) {
591  case EBADF: // One of the descriptor sets specified an invalid
592  // descriptor.
594 
595  case EINVAL: // The specified time limit is invalid. One of its
596  // components is negative or too large.
597  default: // Other unknown error
598  return eConnectionStatusError;
599 
600  case ETIMEDOUT:
602 
603  case EAGAIN: // The kernel was (perhaps temporarily) unable to
604  // allocate the requested number of file descriptors, or
605  // we have non-blocking IO
606  case EINTR: // A signal was delivered before the time limit
607  // expired and before any of the selected events occurred.
608  break; // Lets keep reading to until we timeout
609  }
610  } else {
611  if (select_helper.FDIsSetRead(handle))
613 
614  if (select_helper.FDIsSetRead(pipe_fd)) {
615  // There is an interrupt or exit command in the command pipe Read the
616  // data from that pipe:
617  char c;
618 
619  ssize_t bytes_read = llvm::sys::RetryAfterSignal(-1, ::read, pipe_fd, &c, 1);
620  assert(bytes_read == 1);
621  (void)bytes_read;
622  switch (c) {
623  case 'q':
624  LLDB_LOGF(log,
625  "%p ConnectionFileDescriptor::BytesAvailable() "
626  "got data: %c from the command channel.",
627  static_cast<void *>(this), c);
629  case 'i':
630  // Interrupt the current read
632  }
633  }
634  }
635  }
636  }
637 
638  if (error_ptr)
639  error_ptr->SetErrorString("not connected");
641 }
642 
645  Status *error_ptr) {
646  Socket *socket = nullptr;
647  Status error =
649  if (error_ptr)
650  *error_ptr = error;
651  m_write_sp.reset(socket);
653  if (error.Fail()) {
654  return eConnectionStatusError;
655  }
656  m_uri.assign(std::string(socket_name));
658 }
659 
662  Status *error_ptr) {
663  Socket *socket = nullptr;
664  Status error =
666  if (error_ptr)
667  *error_ptr = error;
668  m_write_sp.reset(socket);
670  if (error.Fail()) {
671  return eConnectionStatusError;
672  }
673  m_uri.assign(std::string(socket_name));
675 }
676 
679  Status *error_ptr) {
680  Socket *socket = nullptr;
682  m_child_processes_inherit, socket);
683  if (error_ptr)
684  *error_ptr = error;
685  m_write_sp.reset(socket);
687  if (error.Fail()) {
688  return eConnectionStatusError;
689  }
690  m_uri.assign(std::string(socket_name));
692 }
693 
696  Status *error_ptr) {
697  if (error_ptr)
698  *error_ptr = Status();
700 
701  m_waiting_for_accept = true;
702  llvm::Expected<std::unique_ptr<TCPSocket>> listening_socket =
704  if (!listening_socket) {
705  if (error_ptr)
706  *error_ptr = listening_socket.takeError();
707  else
709  listening_socket.takeError(), "tcp listen failed: {0}");
710  return eConnectionStatusError;
711  }
712 
713 
714  Socket *accepted_socket;
715  Status error = listening_socket.get()->Accept(accepted_socket);
716  if (error_ptr)
717  *error_ptr = error;
718  if (error.Fail())
719  return eConnectionStatusError;
720 
721  InitializeSocket(accepted_socket);
723 }
724 
726  Status *error_ptr) {
727  if (error_ptr)
728  *error_ptr = Status();
729 
730  llvm::Expected<std::unique_ptr<Socket>> socket =
732  if (!socket) {
733  if (error_ptr)
734  *error_ptr = socket.takeError();
735  else
737  socket.takeError(), "tcp connect failed: {0}");
738  return eConnectionStatusError;
739  }
740  m_write_sp = std::move(*socket);
742  m_uri.assign(std::string(s));
744 }
745 
747  Status *error_ptr) {
748  if (error_ptr)
749  *error_ptr = Status();
750  llvm::Expected<std::unique_ptr<UDPSocket>> socket =
752  if (!socket) {
753  if (error_ptr)
754  *error_ptr = socket.takeError();
755  else
757  socket.takeError(), "tcp connect failed: {0}");
758  return eConnectionStatusError;
759  }
760  m_write_sp = std::move(*socket);
762  m_uri.assign(std::string(s));
764 }
765 
766 uint16_t
768  auto Result = m_port_predicate.WaitForValueNotEqualTo(0, timeout);
769  return Result ? *Result : 0;
770 }
771 
774 }
775 
777  bool child_processes_inherit) {
778  m_child_processes_inherit = child_processes_inherit;
779 }
780 
782  m_write_sp.reset(socket);
784  m_uri = socket->GetRemoteConnectionURI();
785 }
void FDSetRead(lldb::socket_t fd)
#define LIBLLDB_LOG_OBJECT
Definition: Logging.h:25
A class that represents a running process on the host machine.
lldb::ConnectionStatus NamedSocketAccept(llvm::StringRef socket_name, Status *error_ptr)
static Status UnixDomainConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket)
Definition: Socket.cpp:211
#define O_NONBLOCK
static llvm::Expected< std::unique_ptr< Socket > > TcpConnect(llvm::StringRef host_and_port, bool child_processes_inherit)
Definition: Socket.cpp:151
void SetTimeout(const std::chrono::microseconds &timeout)
End-of-file encountered.
bool InterruptRead() override
Interrupts an ongoing Read() operation.
lldb::ConnectionStatus ConnectTCP(llvm::StringRef host_and_port, Status *error_ptr)
static llvm::Expected< std::unique_ptr< TCPSocket > > TcpListen(llvm::StringRef host_and_port, bool child_processes_inherit, Predicate< uint16_t > *predicate, int backlog=5)
Definition: Socket.cpp:170
size_t Read(void *dst, size_t dst_len, const Timeout< std::micro > &timeout, lldb::ConnectionStatus &status, Status *error_ptr) override
The read function that attempts to read from the connection.
lldb::ConnectionStatus NamedSocketConnect(llvm::StringRef socket_name, Status *error_ptr)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:242
void SetChildProcessesInherit(bool child_processes_inherit)
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:265
void SetErrorToErrno()
Set the current error to errno.
Definition: Status.cpp:224
lldb_private::Status Select()
static llvm::raw_ostream & error(Stream &strm)
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
void Clear()
Clear the object state.
Definition: Status.cpp:168
A communication connection class.
Definition: Connection.h:41
lldb::ConnectionStatus Connect(llvm::StringRef s, Status *error_ptr) override
Connect using the connect string url.
static Status UnixDomainAccept(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket)
Definition: Socket.cpp:227
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:242
lldb::ConnectionStatus BytesAvailable(const Timeout< std::micro > &timeout, Status *error_ptr)
lldb::ConnectionStatus UnixAbstractSocketConnect(llvm::StringRef socket_name, Status *error_ptr)
virtual std::string GetRemoteConnectionURI() const
Definition: Socket.h:111
bool Success() const
Test for success condition.
Definition: Status.cpp:288
static const WaitableHandle kInvalidHandleValue
Definition: IOObject.h:30
llvm::Optional< T > WaitForValueNotEqualTo(T value, const Timeout< std::micro > &timeout=llvm::None)
Wait for m_value to not be equal to value.
Definition: Predicate.h:183
bool FDIsSetRead(lldb::socket_t fd) const
static Status UnixAbstractConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket)
Definition: Socket.cpp:243
void SetValue(T value, PredicateBroadcastType broadcast_type)
Value set accessor.
Definition: Predicate.h:90
#define LLDB_LOGF(log,...)
Definition: Log.h:249
static llvm::Expected< std::unique_ptr< UDPSocket > > UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit)
Definition: Socket.cpp:206
lldb::ConnectionStatus Disconnect(Status *error_ptr) override
Disconnect the communications connection if one is currently connected.
Check GetError() for details.
bool Fail() const
Test for error condition.
Definition: Status.cpp:182
uint16_t GetListeningPort(const Timeout< std::micro > &timeout)
Log * GetLogIfAnyCategoriesSet(uint32_t mask)
Definition: Logging.cpp:62
Definition: SBAddress.h:15
#define LIBLLDB_LOG_CONNECTION
Definition: Logging.h:27
ConnectionFileDescriptor(bool child_processes_inherit=false)
std::string GetURI() override
Returns a URI that describes this connection object.
Lost connection while connected to a valid connection.
bool IsConnected() const override
Check if the connection is valid.
No broadcast will be sent when the value is modified.
Definition: Predicate.h:27
lldb::ConnectionStatus SocketListenAndAccept(llvm::StringRef host_and_port, Status *error_ptr)
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Set the current error string to a formatted error string.
Definition: Status.cpp:256
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition: Status.cpp:131
size_t Write(const void *src, size_t src_len, lldb::ConnectionStatus &status, Status *error_ptr) override
The actual write function that attempts to write to the communications protocol.
ConnectionStatus
Connection Status Types.
ValueType GetError() const
Access the error value.
Definition: Status.cpp:175
lldb::ConnectionStatus ConnectUDP(llvm::StringRef args, Status *error_ptr)
An error handling class.
Definition: Status.h:44