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