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 
16 #include "lldb/Host/Config.h"
17 #include "lldb/Host/Socket.h"
21 #include "lldb/Utility/Timeout.h"
22 
23 #include <cerrno>
24 #include <cstdlib>
25 #include <cstring>
26 #include <fcntl.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 ConnectionFileDescriptor::ConnectionFileDescriptor(bool child_processes_inherit)
54  : Connection(), m_pipe(), m_mutex(), m_shutting_down(false),
55 
56  m_child_processes_inherit(child_processes_inherit) {
59  LLDB_LOGF(log, "%p ConnectionFileDescriptor::ConnectionFileDescriptor ()",
60  static_cast<void *>(this));
61 }
62 
64  : Connection(), m_pipe(), m_mutex(), m_shutting_down(false),
65  m_child_processes_inherit(false) {
66  m_io_sp =
67  std::make_shared<NativeFile>(fd, File::eOpenOptionReadWrite, owns_fd);
68 
71  LLDB_LOGF(log,
72  "%p ConnectionFileDescriptor::ConnectionFileDescriptor (fd = "
73  "%i, owns_fd = %i)",
74  static_cast<void *>(this), fd, owns_fd);
76 }
77 
79  : Connection(), m_pipe(), m_mutex(), m_shutting_down(false),
80  m_child_processes_inherit(false) {
81  InitializeSocket(socket);
82 }
83 
87  LLDB_LOGF(log, "%p ConnectionFileDescriptor::~ConnectionFileDescriptor ()",
88  static_cast<void *>(this));
89  Disconnect(nullptr);
91 }
92 
95 
97  // Make the command file descriptor here:
98  Status result = m_pipe.CreateNew(m_child_processes_inherit);
99  if (!result.Success()) {
100  LLDB_LOGF(log,
101  "%p ConnectionFileDescriptor::OpenCommandPipe () - could not "
102  "make pipe: %s",
103  static_cast<void *>(this), result.AsCString());
104  } else {
105  LLDB_LOGF(log,
106  "%p ConnectionFileDescriptor::OpenCommandPipe() - success "
107  "readfd=%d writefd=%d",
108  static_cast<void *>(this), m_pipe.GetReadFileDescriptor(),
109  m_pipe.GetWriteFileDescriptor());
110  }
111 }
112 
115  LLDB_LOGF(log, "%p ConnectionFileDescriptor::CloseCommandPipe()",
116  static_cast<void *>(this));
117 
118  m_pipe.Close();
119 }
120 
122  return m_io_sp && m_io_sp->IsValid();
123 }
124 
126  Status *error_ptr) {
127  return Connect(path, nullptr, error_ptr);
128 }
129 
132  socket_id_callback_type socket_id_callback,
133  Status *error_ptr) {
134  std::lock_guard<std::recursive_mutex> guard(m_mutex);
136  LLDB_LOGF(log, "%p ConnectionFileDescriptor::Connect (url = '%s')",
137  static_cast<void *>(this), path.str().c_str());
138 
139  OpenCommandPipe();
140 
141  if (path.empty()) {
142  if (error_ptr)
143  error_ptr->SetErrorString("invalid connect arguments");
144  return eConnectionStatusError;
145  }
146 
147  llvm::StringRef scheme;
148  std::tie(scheme, path) = path.split("://");
149 
150  if (!path.empty()) {
151  auto method =
152  llvm::StringSwitch<ConnectionStatus (ConnectionFileDescriptor::*)(
153  llvm::StringRef, socket_id_callback_type, Status *)>(scheme)
154  .Case("listen", &ConnectionFileDescriptor::AcceptTCP)
155  .Cases("accept", "unix-accept",
157  .Case("unix-abstract-accept",
159  .Cases("connect", "tcp-connect",
162  .Case("unix-connect", &ConnectionFileDescriptor::ConnectNamedSocket)
163  .Case("unix-abstract-connect",
165 #if LLDB_ENABLE_POSIX
169 #endif
170  .Default(nullptr);
171 
172  if (method) {
173  if (error_ptr)
174  *error_ptr = Status();
175  return (this->*method)(path, socket_id_callback, error_ptr);
176  }
177  }
178 
179  if (error_ptr)
180  error_ptr->SetErrorStringWithFormat("unsupported connection URL: '%s'",
181  path.str().c_str());
182  return eConnectionStatusError;
183 }
184 
186  size_t bytes_written = 0;
187  Status result = m_pipe.Write("i", 1, bytes_written);
188  return result.Success();
189 }
190 
193  LLDB_LOGF(log, "%p ConnectionFileDescriptor::Disconnect ()",
194  static_cast<void *>(this));
195 
197 
198  if (!IsConnected()) {
199  LLDB_LOGF(
200  log, "%p ConnectionFileDescriptor::Disconnect(): Nothing to disconnect",
201  static_cast<void *>(this));
203  }
204 
205  if (m_io_sp->GetFdType() == IOObject::eFDTypeSocket)
206  static_cast<Socket &>(*m_io_sp).PreDisconnect();
207 
208  // Try to get the ConnectionFileDescriptor's mutex. If we fail, that is
209  // quite likely because somebody is doing a blocking read on our file
210  // descriptor. If that's the case, then send the "q" char to the command
211  // file channel so the read will wake up and the connection will then know to
212  // shut down.
213  std::unique_lock<std::recursive_mutex> locker(m_mutex, std::defer_lock);
214  if (!locker.try_lock()) {
215  if (m_pipe.CanWrite()) {
216  size_t bytes_written = 0;
217  Status result = m_pipe.Write("q", 1, bytes_written);
218  LLDB_LOGF(log,
219  "%p ConnectionFileDescriptor::Disconnect(): Couldn't get "
220  "the lock, sent 'q' to %d, error = '%s'.",
221  static_cast<void *>(this), m_pipe.GetWriteFileDescriptor(),
222  result.AsCString());
223  } else if (log) {
224  LLDB_LOGF(log,
225  "%p ConnectionFileDescriptor::Disconnect(): Couldn't get the "
226  "lock, but no command pipe is available.",
227  static_cast<void *>(this));
228  }
229  locker.lock();
230  }
231 
232  // Prevents reads and writes during shutdown.
233  m_shutting_down = true;
234 
235  Status error = m_io_sp->Close();
236  if (error.Fail())
237  status = eConnectionStatusError;
238  if (error_ptr)
239  *error_ptr = error;
240 
241  // Close any pipes we were using for async interrupts
242  m_pipe.Close();
243 
244  m_uri.clear();
245  m_shutting_down = false;
246  return status;
247 }
248 
249 size_t ConnectionFileDescriptor::Read(void *dst, size_t dst_len,
250  const Timeout<std::micro> &timeout,
251  ConnectionStatus &status,
252  Status *error_ptr) {
254 
255  std::unique_lock<std::recursive_mutex> locker(m_mutex, std::defer_lock);
256  if (!locker.try_lock()) {
257  LLDB_LOGF(log,
258  "%p ConnectionFileDescriptor::Read () failed to get the "
259  "connection lock.",
260  static_cast<void *>(this));
261  if (error_ptr)
262  error_ptr->SetErrorString("failed to get the connection lock for read.");
263 
264  status = eConnectionStatusTimedOut;
265  return 0;
266  }
267 
268  if (m_shutting_down) {
269  if (error_ptr)
270  error_ptr->SetErrorString("shutting down");
271  status = eConnectionStatusError;
272  return 0;
273  }
274 
275  status = BytesAvailable(timeout, error_ptr);
276  if (status != eConnectionStatusSuccess)
277  return 0;
278 
279  Status error;
280  size_t bytes_read = dst_len;
281  error = m_io_sp->Read(dst, bytes_read);
282 
283  if (log) {
284  LLDB_LOGF(log,
285  "%p ConnectionFileDescriptor::Read() fd = %" PRIu64
286  ", dst = %p, dst_len = %" PRIu64 ") => %" PRIu64 ", error = %s",
287  static_cast<void *>(this),
288  static_cast<uint64_t>(m_io_sp->GetWaitableHandle()),
289  static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
290  static_cast<uint64_t>(bytes_read), error.AsCString());
291  }
292 
293  if (bytes_read == 0) {
294  error.Clear(); // End-of-file. Do not automatically close; pass along for
295  // the end-of-file handlers.
297  }
298 
299  if (error_ptr)
300  *error_ptr = error;
301 
302  if (error.Fail()) {
303  uint32_t error_value = error.GetError();
304  switch (error_value) {
305  case EAGAIN: // The file was marked for non-blocking I/O, and no data were
306  // ready to be read.
307  if (m_io_sp->GetFdType() == IOObject::eFDTypeSocket)
308  status = eConnectionStatusTimedOut;
309  else
310  status = eConnectionStatusSuccess;
311  return 0;
312 
313  case EFAULT: // Buf points outside the allocated address space.
314  case EINTR: // A read from a slow device was interrupted before any data
315  // arrived by the delivery of a signal.
316  case EINVAL: // The pointer associated with fildes was negative.
317  case EIO: // An I/O error occurred while reading from the file system.
318  // The process group is orphaned.
319  // The file is a regular file, nbyte is greater than 0, the
320  // starting position is before the end-of-file, and the
321  // starting position is greater than or equal to the offset
322  // maximum established for the open file descriptor
323  // associated with fildes.
324  case EISDIR: // An attempt is made to read a directory.
325  case ENOBUFS: // An attempt to allocate a memory buffer fails.
326  case ENOMEM: // Insufficient memory is available.
327  status = eConnectionStatusError;
328  break; // Break to close....
329 
330  case ENOENT: // no such file or directory
331  case EBADF: // fildes is not a valid file or socket descriptor open for
332  // reading.
333  case ENXIO: // An action is requested of a device that does not exist..
334  // A requested action cannot be performed by the device.
335  case ECONNRESET: // The connection is closed by the peer during a read
336  // attempt on a socket.
337  case ENOTCONN: // A read is attempted on an unconnected socket.
339  break; // Break to close....
340 
341  case ETIMEDOUT: // A transmission timeout occurs during a read attempt on a
342  // socket.
343  status = eConnectionStatusTimedOut;
344  return 0;
345 
346  default:
347  LLDB_LOG(log, "this = {0}, unexpected error: {1}", this,
348  llvm::sys::StrError(error_value));
349  status = eConnectionStatusError;
350  break; // Break to close....
351  }
352 
353  return 0;
354  }
355  return bytes_read;
356 }
357 
358 size_t ConnectionFileDescriptor::Write(const void *src, size_t src_len,
359  ConnectionStatus &status,
360  Status *error_ptr) {
362  LLDB_LOGF(log,
363  "%p ConnectionFileDescriptor::Write (src = %p, src_len = %" PRIu64
364  ")",
365  static_cast<void *>(this), static_cast<const void *>(src),
366  static_cast<uint64_t>(src_len));
367 
368  if (!IsConnected()) {
369  if (error_ptr)
370  error_ptr->SetErrorString("not connected");
372  return 0;
373  }
374 
375  if (m_shutting_down) {
376  if (error_ptr)
377  error_ptr->SetErrorString("shutting down");
378  status = eConnectionStatusError;
379  return 0;
380  }
381 
382  Status error;
383 
384  size_t bytes_sent = src_len;
385  error = m_io_sp->Write(src, bytes_sent);
386 
387  if (log) {
388  LLDB_LOGF(log,
389  "%p ConnectionFileDescriptor::Write(fd = %" PRIu64
390  ", src = %p, src_len = %" PRIu64 ") => %" PRIu64 " (error = %s)",
391  static_cast<void *>(this),
392  static_cast<uint64_t>(m_io_sp->GetWaitableHandle()),
393  static_cast<const void *>(src), static_cast<uint64_t>(src_len),
394  static_cast<uint64_t>(bytes_sent), error.AsCString());
395  }
396 
397  if (error_ptr)
398  *error_ptr = error;
399 
400  if (error.Fail()) {
401  switch (error.GetError()) {
402  case EAGAIN:
403  case EINTR:
404  status = eConnectionStatusSuccess;
405  return 0;
406 
407  case ECONNRESET: // The connection is closed by the peer during a read
408  // attempt on a socket.
409  case ENOTCONN: // A read is attempted on an unconnected socket.
411  break; // Break to close....
412 
413  default:
414  status = eConnectionStatusError;
415  break; // Break to close....
416  }
417 
418  return 0;
419  }
420 
421  status = eConnectionStatusSuccess;
422  return bytes_sent;
423 }
424 
426 
427 // This ConnectionFileDescriptor::BytesAvailable() uses select() via
428 // SelectHelper
429 //
430 // PROS:
431 // - select is consistent across most unix platforms
432 // - The Apple specific version allows for unlimited fds in the fd_sets by
433 // setting the _DARWIN_UNLIMITED_SELECT define prior to including the
434 // required header files.
435 // CONS:
436 // - on non-Apple platforms, only supports file descriptors up to FD_SETSIZE.
437 // This implementation will assert if it runs into that hard limit to let
438 // users know that another ConnectionFileDescriptor::BytesAvailable() should
439 // be used or a new version of ConnectionFileDescriptor::BytesAvailable()
440 // should be written for the system that is running into the limitations.
441 
444  Status *error_ptr) {
445  // Don't need to take the mutex here separately since we are only called from
446  // Read. If we ever get used more generally we will need to lock here as
447  // well.
448 
450  LLDB_LOG(log, "this = {0}, timeout = {1}", this, timeout);
451 
452  // Make a copy of the file descriptors to make sure we don't have another
453  // thread change these values out from under us and cause problems in the
454  // loop below where like in FS_SET()
455  const IOObject::WaitableHandle handle = m_io_sp->GetWaitableHandle();
456  const int pipe_fd = m_pipe.GetReadFileDescriptor();
457 
458  if (handle != IOObject::kInvalidHandleValue) {
459  SelectHelper select_helper;
460  if (timeout)
461  select_helper.SetTimeout(*timeout);
462 
463  select_helper.FDSetRead(handle);
464 #if defined(_WIN32)
465  // select() won't accept pipes on Windows. The entire Windows codepath
466  // needs to be converted over to using WaitForMultipleObjects and event
467  // HANDLEs, but for now at least this will allow ::select() to not return
468  // an error.
469  const bool have_pipe_fd = false;
470 #else
471  const bool have_pipe_fd = pipe_fd >= 0;
472 #endif
473  if (have_pipe_fd)
474  select_helper.FDSetRead(pipe_fd);
475 
476  while (handle == m_io_sp->GetWaitableHandle()) {
477 
478  Status error = select_helper.Select();
479 
480  if (error_ptr)
481  *error_ptr = error;
482 
483  if (error.Fail()) {
484  switch (error.GetError()) {
485  case EBADF: // One of the descriptor sets specified an invalid
486  // descriptor.
488 
489  case EINVAL: // The specified time limit is invalid. One of its
490  // components is negative or too large.
491  default: // Other unknown error
492  return eConnectionStatusError;
493 
494  case ETIMEDOUT:
496 
497  case EAGAIN: // The kernel was (perhaps temporarily) unable to
498  // allocate the requested number of file descriptors, or
499  // we have non-blocking IO
500  case EINTR: // A signal was delivered before the time limit
501  // expired and before any of the selected events occurred.
502  break; // Lets keep reading to until we timeout
503  }
504  } else {
505  if (select_helper.FDIsSetRead(handle))
507 
508  if (select_helper.FDIsSetRead(pipe_fd)) {
509  // There is an interrupt or exit command in the command pipe Read the
510  // data from that pipe:
511  char c;
512 
513  ssize_t bytes_read =
514  llvm::sys::RetryAfterSignal(-1, ::read, pipe_fd, &c, 1);
515  assert(bytes_read == 1);
516  (void)bytes_read;
517  switch (c) {
518  case 'q':
519  LLDB_LOGF(log,
520  "%p ConnectionFileDescriptor::BytesAvailable() "
521  "got data: %c from the command channel.",
522  static_cast<void *>(this), c);
524  case 'i':
525  // Interrupt the current read
527  }
528  }
529  }
530  }
531  }
532 
533  if (error_ptr)
534  error_ptr->SetErrorString("not connected");
536 }
537 
539  Socket::SocketProtocol socket_protocol, llvm::StringRef socket_name,
540  llvm::function_ref<void(Socket &)> post_listen_callback,
541  Status *error_ptr) {
542  Status error;
543  std::unique_ptr<Socket> listening_socket =
545  Socket *accepted_socket;
546 
547  if (!error.Fail())
548  error = listening_socket->Listen(socket_name, 5);
549 
550  if (!error.Fail()) {
551  post_listen_callback(*listening_socket);
552  error = listening_socket->Accept(accepted_socket);
553  }
554 
555  if (!error.Fail()) {
556  m_io_sp.reset(accepted_socket);
557  m_uri.assign(socket_name.str());
559  }
560 
561  if (error_ptr)
562  *error_ptr = error;
563  return eConnectionStatusError;
564 }
565 
568  llvm::StringRef socket_name,
569  Status *error_ptr) {
570  Status error;
571  std::unique_ptr<Socket> socket =
573 
574  if (!error.Fail())
575  error = socket->Connect(socket_name);
576 
577  if (!error.Fail()) {
578  m_io_sp = std::move(socket);
579  m_uri.assign(socket_name.str());
581  }
582 
583  if (error_ptr)
584  *error_ptr = error;
585  return eConnectionStatusError;
586 }
587 
589  llvm::StringRef socket_name, socket_id_callback_type socket_id_callback,
590  Status *error_ptr) {
591  return AcceptSocket(
592  Socket::ProtocolUnixDomain, socket_name,
593  [socket_id_callback, socket_name](Socket &listening_socket) {
594  socket_id_callback(socket_name);
595  },
596  error_ptr);
597 }
598 
600  llvm::StringRef socket_name, socket_id_callback_type socket_id_callback,
601  Status *error_ptr) {
602  return ConnectSocket(Socket::ProtocolUnixDomain, socket_name, error_ptr);
603 }
604 
606  llvm::StringRef socket_name, socket_id_callback_type socket_id_callback,
607  Status *error_ptr) {
608  return AcceptSocket(
609  Socket::ProtocolUnixAbstract, socket_name,
610  [socket_id_callback, socket_name](Socket &listening_socket) {
611  socket_id_callback(socket_name);
612  },
613  error_ptr);
614 }
615 
617  llvm::StringRef socket_name, socket_id_callback_type socket_id_callback,
618  Status *error_ptr) {
619  return ConnectSocket(Socket::ProtocolUnixAbstract, socket_name, error_ptr);
620 }
621 
623 ConnectionFileDescriptor::AcceptTCP(llvm::StringRef socket_name,
624  socket_id_callback_type socket_id_callback,
625  Status *error_ptr) {
627  Socket::ProtocolTcp, socket_name,
628  [socket_id_callback](Socket &listening_socket) {
629  uint16_t port =
630  static_cast<TCPSocket &>(listening_socket).GetLocalPortNumber();
631  socket_id_callback(std::to_string(port));
632  },
633  error_ptr);
634  if (ret == eConnectionStatusSuccess)
635  m_uri.assign(
636  static_cast<TCPSocket *>(m_io_sp.get())->GetRemoteConnectionURI());
637  return ret;
638 }
639 
641 ConnectionFileDescriptor::ConnectTCP(llvm::StringRef socket_name,
642  socket_id_callback_type socket_id_callback,
643  Status *error_ptr) {
644  return ConnectSocket(Socket::ProtocolTcp, socket_name, error_ptr);
645 }
646 
649  socket_id_callback_type socket_id_callback,
650  Status *error_ptr) {
651  if (error_ptr)
652  *error_ptr = Status();
653  llvm::Expected<std::unique_ptr<UDPSocket>> socket =
655  if (!socket) {
656  if (error_ptr)
657  *error_ptr = socket.takeError();
658  else
660  socket.takeError(), "tcp connect failed: {0}");
661  return eConnectionStatusError;
662  }
663  m_io_sp = std::move(*socket);
664  m_uri.assign(std::string(s));
666 }
667 
670  socket_id_callback_type socket_id_callback,
671  Status *error_ptr) {
672 #if LLDB_ENABLE_POSIX
673  // Just passing a native file descriptor within this current process that
674  // is already opened (possibly from a service or other source).
675  int fd = -1;
676 
677  if (!s.getAsInteger(0, fd)) {
678  // We have what looks to be a valid file descriptor, but we should make
679  // sure it is. We currently are doing this by trying to get the flags
680  // from the file descriptor and making sure it isn't a bad fd.
681  errno = 0;
682  int flags = ::fcntl(fd, F_GETFL, 0);
683  if (flags == -1 || errno == EBADF) {
684  if (error_ptr)
685  error_ptr->SetErrorStringWithFormat("stale file descriptor: %s",
686  s.str().c_str());
687  m_io_sp.reset();
688  return eConnectionStatusError;
689  } else {
690  // Don't take ownership of a file descriptor that gets passed to us
691  // since someone else opened the file descriptor and handed it to us.
692  // TODO: Since are using a URL to open connection we should
693  // eventually parse options using the web standard where we have
694  // "fd://123?opt1=value;opt2=value" and we can have an option be
695  // "owns=1" or "owns=0" or something like this to allow us to specify
696  // this. For now, we assume we must assume we don't own it.
697 
698  std::unique_ptr<TCPSocket> tcp_socket;
699  tcp_socket = std::make_unique<TCPSocket>(fd, false, false);
700  // Try and get a socket option from this file descriptor to see if
701  // this is a socket and set m_is_socket accordingly.
702  int resuse;
703  bool is_socket =
704  !!tcp_socket->GetOption(SOL_SOCKET, SO_REUSEADDR, resuse);
705  if (is_socket)
706  m_io_sp = std::move(tcp_socket);
707  else
708  m_io_sp =
709  std::make_shared<NativeFile>(fd, File::eOpenOptionReadWrite, false);
710  m_uri = s.str();
712  }
713  }
714 
715  if (error_ptr)
716  error_ptr->SetErrorStringWithFormat("invalid file descriptor: \"%s\"",
717  s.str().c_str());
718  m_io_sp.reset();
719  return eConnectionStatusError;
720 #endif // LLDB_ENABLE_POSIX
721  llvm_unreachable("this function should be only called w/ LLDB_ENABLE_POSIX");
722 }
723 
725  llvm::StringRef s, socket_id_callback_type socket_id_callback,
726  Status *error_ptr) {
727 #if LLDB_ENABLE_POSIX
728  std::string addr_str = s.str();
729  // file:///PATH
730  int fd = llvm::sys::RetryAfterSignal(-1, ::open, addr_str.c_str(), O_RDWR);
731  if (fd == -1) {
732  if (error_ptr)
733  error_ptr->SetErrorToErrno();
734  return eConnectionStatusError;
735  }
736 
737  if (::isatty(fd)) {
738  // Set up serial terminal emulation
739  struct termios options;
740  ::tcgetattr(fd, &options);
741 
742  // Set port speed to maximum
743  ::cfsetospeed(&options, B115200);
744  ::cfsetispeed(&options, B115200);
745 
746  // Raw input, disable echo and signals
747  options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
748 
749  // Make sure only one character is needed to return from a read
750  options.c_cc[VMIN] = 1;
751  options.c_cc[VTIME] = 0;
752 
753  llvm::sys::RetryAfterSignal(-1, ::tcsetattr, fd, TCSANOW, &options);
754  }
755 
756  m_io_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionReadWrite, true);
758 #endif // LLDB_ENABLE_POSIX
759  llvm_unreachable("this function should be only called w/ LLDB_ENABLE_POSIX");
760 }
761 
763  llvm::StringRef s, socket_id_callback_type socket_id_callback,
764  Status *error_ptr) {
765 #if LLDB_ENABLE_POSIX
766  llvm::StringRef path, qs;
767  // serial:///PATH?k1=v1&k2=v2...
768  std::tie(path, qs) = s.split('?');
769 
770  llvm::Expected<SerialPort::Options> serial_options =
772  if (!serial_options) {
773  if (error_ptr)
774  *error_ptr = serial_options.takeError();
775  else
776  llvm::consumeError(serial_options.takeError());
777  return eConnectionStatusError;
778  }
779 
780  int fd = llvm::sys::RetryAfterSignal(-1, ::open, path.str().c_str(), O_RDWR);
781  if (fd == -1) {
782  if (error_ptr)
783  error_ptr->SetErrorToErrno();
784  return eConnectionStatusError;
785  }
786 
787  llvm::Expected<std::unique_ptr<SerialPort>> serial_sp = SerialPort::Create(
788  fd, File::eOpenOptionReadWrite, serial_options.get(), true);
789  if (!serial_sp) {
790  if (error_ptr)
791  *error_ptr = serial_sp.takeError();
792  else
793  llvm::consumeError(serial_sp.takeError());
794  return eConnectionStatusError;
795  }
796  m_io_sp = std::move(serial_sp.get());
797 
799 #endif // LLDB_ENABLE_POSIX
800  llvm_unreachable("this function should be only called w/ LLDB_ENABLE_POSIX");
801 }
802 
805 }
806 
808  bool child_processes_inherit) {
809  m_child_processes_inherit = child_processes_inherit;
810 }
811 
813  m_io_sp.reset(socket);
814  m_uri = socket->GetRemoteConnectionURI();
815 }
lldb_private::ConnectionFileDescriptor::GetURI
std::string GetURI() override
Returns a URI that describes this connection object.
Definition: ConnectionFileDescriptorPosix.cpp:425
lldb_private::Connection
Definition: Connection.h:41
SelectHelper.h
lldb_private::ConnectionFileDescriptor::InitializeSocket
void InitializeSocket(Socket *socket)
Definition: ConnectionFileDescriptorPosix.cpp:812
lldb_private::Socket::GetRemoteConnectionURI
virtual std::string GetRemoteConnectionURI() const
Definition: Socket.h:106
lldb_private::Socket::SocketProtocol
SocketProtocol
Definition: Socket.h:43
Host.h
lldb_private::ConnectionFileDescriptor::SetChildProcessesInherit
void SetChildProcessesInherit(bool child_processes_inherit)
Definition: ConnectionFileDescriptorPosix.cpp:807
lldb_private::SerialPort::Create
static llvm::Expected< std::unique_ptr< SerialPort > > Create(int fd, OpenOptions options, Options serial_options, bool transfer_ownership)
Definition: File.cpp:828
lldb_private::Socket
Definition: Socket.h:41
lldb_private::File::eOpenOptionReadWrite
@ eOpenOptionReadWrite
Definition: File.h:52
LLDB_LOGF
#define LLDB_LOGF(log,...)
Definition: Log.h:247
lldb_private::ConnectionFileDescriptor::BytesAvailable
lldb::ConnectionStatus BytesAvailable(const Timeout< std::micro > &timeout, Status *error_ptr)
Definition: ConnectionFileDescriptorPosix.cpp:443
lldb_private::ConnectionFileDescriptor::AcceptSocket
lldb::ConnectionStatus AcceptSocket(Socket::SocketProtocol socket_protocol, llvm::StringRef socket_name, llvm::function_ref< void(Socket &)> post_listen_callback, Status *error_ptr)
Definition: ConnectionFileDescriptorPosix.cpp:538
SelectHelper::FDIsSetRead
bool FDIsSetRead(lldb::socket_t fd) const
Definition: SelectHelper.cpp:61
lldb_private::ConnectionFileDescriptor::m_shutting_down
std::atomic< bool > m_shutting_down
Definition: ConnectionFileDescriptorPosix.h:135
lldb_private::ConnectionFileDescriptor::OpenCommandPipe
void OpenCommandPipe()
Definition: ConnectionFileDescriptorPosix.cpp:93
SelectHelper
Definition: SelectHelper.h:20
lldb_private::Socket::Create
static std::unique_ptr< Socket > Create(const SocketProtocol protocol, bool child_processes_inherit, Status &error)
Definition: Socket.cpp:105
lldb_private::ConnectionFileDescriptor::socket_id_callback_type
llvm::function_ref< void(llvm::StringRef local_socket_id)> socket_id_callback_type
Definition: ConnectionFileDescriptorPosix.h:32
SelectHelper::Select
lldb_private::Status Select()
Definition: SelectHelper.cpp:93
SelectHelper::SetTimeout
void SetTimeout(const std::chrono::microseconds &timeout)
Definition: SelectHelper.cpp:44
lldb::eConnectionStatusNoConnection
@ eConnectionStatusNoConnection
No connection.
Definition: lldb-enumerations.h:300
lldb::eConnectionStatusEndOfFile
@ eConnectionStatusEndOfFile
End-of-file encountered.
Definition: lldb-enumerations.h:297
lldb_private::ConnectionFileDescriptor::m_pipe
Pipe m_pipe
Definition: ConnectionFileDescriptorPosix.h:133
lldb_private::ConnectionFileDescriptor::ConnectFile
lldb::ConnectionStatus ConnectFile(llvm::StringRef args, socket_id_callback_type socket_id_callback, Status *error_ptr)
Definition: ConnectionFileDescriptorPosix.cpp:724
lldb_private::ConnectionFileDescriptor::ConnectNamedSocket
lldb::ConnectionStatus ConnectNamedSocket(llvm::StringRef socket_name, socket_id_callback_type socket_id_callback, Status *error_ptr)
Definition: ConnectionFileDescriptorPosix.cpp:599
lldb_private::ConnectionFileDescriptor::m_uri
std::string m_uri
Definition: ConnectionFileDescriptorPosix.h:140
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::Status::Success
bool Success() const
Test for success condition.
Definition: Status.cpp:288
lldb_private::ConnectionFileDescriptor::ConnectFD
lldb::ConnectionStatus ConnectFD(llvm::StringRef args, socket_id_callback_type socket_id_callback, Status *error_ptr)
Definition: ConnectionFileDescriptorPosix.cpp:669
lldb_private::ConnectionFileDescriptor::m_mutex
std::recursive_mutex m_mutex
Definition: ConnectionFileDescriptorPosix.h:134
Log.h
lldb_private::ConnectionFileDescriptor::ConnectSocket
lldb::ConnectionStatus ConnectSocket(Socket::SocketProtocol socket_protocol, llvm::StringRef socket_name, Status *error_ptr)
Definition: ConnectionFileDescriptorPosix.cpp:567
Socket.h
lldb_private::Status::SetErrorStringWithFormat
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Set the current error string to a formatted error string.
Definition: Status.cpp:256
StreamString.h
Timer.h
lldb::eConnectionStatusTimedOut
@ eConnectionStatusTimedOut
Request timed out.
Definition: lldb-enumerations.h:299
lldb_private::GetLogIfAllCategoriesSet
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
lldb::ConnectionStatus
ConnectionStatus
Connection Status Types.
Definition: lldb-enumerations.h:295
lldb::eConnectionStatusInterrupted
@ eConnectionStatusInterrupted
Interrupted read.
Definition: lldb-enumerations.h:303
lldb_private::Socket::ProtocolUnixDomain
@ ProtocolUnixDomain
Definition: Socket.h:46
SelectHelper::FDSetRead
void FDSetRead(lldb::socket_t fd)
Definition: SelectHelper.cpp:49
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::ConnectionFileDescriptor::GetChildProcessesInherit
bool GetChildProcessesInherit() const
Definition: ConnectionFileDescriptorPosix.cpp:803
lldb_private::SerialPort::OptionsFromURL
static llvm::Expected< Options > OptionsFromURL(llvm::StringRef urlqs)
Definition: File.cpp:773
lldb_private::ConnectionFileDescriptor::m_io_sp
lldb::IOObjectSP m_io_sp
Definition: ConnectionFileDescriptorPosix.h:131
lldb_private::IOObject::WaitableHandle
int WaitableHandle
Definition: IOObject.h:29
lldb_private::ConnectionFileDescriptor::Connect
lldb::ConnectionStatus Connect(llvm::StringRef url, Status *error_ptr) override
Connect using the connect string url.
Definition: ConnectionFileDescriptorPosix.cpp:125
lldb_private::ConnectionFileDescriptor::ConnectAbstractSocket
lldb::ConnectionStatus ConnectAbstractSocket(llvm::StringRef socket_name, socket_id_callback_type socket_id_callback, Status *error_ptr)
Definition: ConnectionFileDescriptorPosix.cpp:616
lldb_private::ConnectionFileDescriptor::Disconnect
lldb::ConnectionStatus Disconnect(Status *error_ptr) override
Disconnect the communications connection if one is currently connected.
Definition: ConnectionFileDescriptorPosix.cpp:191
lldb_private::ConnectionFileDescriptor::IsConnected
bool IsConnected() const override
Check if the connection is valid.
Definition: ConnectionFileDescriptorPosix.cpp:121
SocketAddress.h
lldb_private::ConnectionFileDescriptor::ConnectTCP
lldb::ConnectionStatus ConnectTCP(llvm::StringRef host_and_port, socket_id_callback_type socket_id_callback, Status *error_ptr)
Definition: ConnectionFileDescriptorPosix.cpp:641
lldb_private::Socket::ProtocolUnixAbstract
@ ProtocolUnixAbstract
Definition: Socket.h:47
lldb_private::Status
Definition: Status.h:44
lldb_private::Socket::ProtocolTcp
@ ProtocolTcp
Definition: Socket.h:44
LIBLLDB_LOG_OBJECT
#define LIBLLDB_LOG_OBJECT
Definition: Logging.h:25
lldb_private::Timeout< std::micro >
lldb_private::Socket::UdpConnect
static llvm::Expected< std::unique_ptr< UDPSocket > > UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit)
Definition: Socket.cpp:182
lldb_private::ConnectionFileDescriptor::AcceptAbstractSocket
lldb::ConnectionStatus AcceptAbstractSocket(llvm::StringRef socket_name, socket_id_callback_type socket_id_callback, Status *error_ptr)
Definition: ConnectionFileDescriptorPosix.cpp:605
uint32_t
lldb_private::Socket::PreDisconnect
virtual Status PreDisconnect()
lldb_private::ConnectionFileDescriptor::ConnectUDP
lldb::ConnectionStatus ConnectUDP(llvm::StringRef args, socket_id_callback_type socket_id_callback, Status *error_ptr)
Definition: ConnectionFileDescriptorPosix.cpp:648
lldb::eConnectionStatusError
@ eConnectionStatusError
Check GetError() for details.
Definition: lldb-enumerations.h:298
lldb_private::Status::SetErrorString
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:242
Timeout.h
uint16_t
lldb_private::ConnectionFileDescriptor::m_child_processes_inherit
bool m_child_processes_inherit
Definition: ConnectionFileDescriptorPosix.h:138
LIBLLDB_LOG_CONNECTION
#define LIBLLDB_LOG_CONNECTION
Definition: Logging.h:27
lldb_private::Status::SetErrorToErrno
void SetErrorToErrno()
Set the current error to errno.
Definition: Status.cpp:224
TCPSocket.h
LLDB_LOG
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:240
lldb::eConnectionStatusSuccess
@ eConnectionStatusSuccess
Success.
Definition: lldb-enumerations.h:296
lldb_private::ConnectionFileDescriptor::InterruptRead
bool InterruptRead() override
Interrupts an ongoing Read() operation.
Definition: ConnectionFileDescriptorPosix.cpp:185
lldb_private::TCPSocket
Definition: TCPSocket.h:17
lldb_private::TCPSocket::GetRemoteConnectionURI
std::string GetRemoteConnectionURI() const override
Definition: TCPSocket.cpp:130
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::ConnectionFileDescriptor::ConnectionFileDescriptor
ConnectionFileDescriptor(bool child_processes_inherit=false)
Definition: ConnectionFileDescriptorPosix.cpp:53
lldb_private::ConnectionFileDescriptor::AcceptTCP
lldb::ConnectionStatus AcceptTCP(llvm::StringRef host_and_port, socket_id_callback_type socket_id_callback, Status *error_ptr)
Definition: ConnectionFileDescriptorPosix.cpp:623
ConnectionFileDescriptorPosix.h
lldb_private::ConnectionFileDescriptor::Write
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.
Definition: ConnectionFileDescriptorPosix.cpp:358
lldb::eConnectionStatusLostConnection
@ eConnectionStatusLostConnection
Lost connection while connected to a valid connection.
Definition: lldb-enumerations.h:301
lldb_private::ConnectionFileDescriptor::CloseCommandPipe
void CloseCommandPipe()
Definition: ConnectionFileDescriptorPosix.cpp:113
lldb_private::Log
Definition: Log.h:49
lldb_private::ConnectionFileDescriptor::ConnectSerialPort
lldb::ConnectionStatus ConnectSerialPort(llvm::StringRef args, socket_id_callback_type socket_id_callback, Status *error_ptr)
Definition: ConnectionFileDescriptorPosix.cpp:762
lldb_private::GetLogIfAnyCategoriesSet
Log * GetLogIfAnyCategoriesSet(uint32_t mask)
Definition: Logging.cpp:62
UDPSocket.h
lldb_private::IOObject::kInvalidHandleValue
static const WaitableHandle kInvalidHandleValue
Definition: IOObject.h:30
lldb_private::ConnectionFileDescriptor::AcceptNamedSocket
lldb::ConnectionStatus AcceptNamedSocket(llvm::StringRef socket_name, socket_id_callback_type socket_id_callback, Status *error_ptr)
Definition: ConnectionFileDescriptorPosix.cpp:588
lldb_private::ConnectionFileDescriptor
Definition: ConnectionFileDescriptorPosix.h:29
lldb
Definition: SBAddress.h:15
LLDB_LOG_ERROR
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:263
lldb_private::ConnectionFileDescriptor::~ConnectionFileDescriptor
~ConnectionFileDescriptor() override
Definition: ConnectionFileDescriptorPosix.cpp:84
lldb_private::ConnectionFileDescriptor::Read
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.
Definition: ConnectionFileDescriptorPosix.cpp:249
lldb_private::IOObject::eFDTypeSocket
@ eFDTypeSocket
Definition: IOObject.h:24
lldb_private::Status::AsCString
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition: Status.cpp:131