[lldb][NFC] Fix all formatting errors in .cpp file headers
Summary:
A *.cpp file header in LLDB (and in LLDB) should like this:
```
//===-- TestUtilities.cpp -------------------------------------------------===//
```
However in LLDB most of our source files have arbitrary changes to this format and
these changes are spreading through LLDB as folks usually just use the existing
source files as templates for their new files (most notably the unnecessary
editor language indicator `-*- C++ -*-` is spreading and in every review
someone is pointing out that this is wrong, resulting in people pointing out that this
is done in the same way in other files).
This patch removes most of these inconsistencies including the editor language indicators,
all the different missing/additional '-' characters, files that center the file name, missing
trailing `===//` (mostly caused by clang-format breaking the line).
Reviewers: aprantl, espindola, jfb, shafik, JDevlieghere
Reviewed By: JDevlieghere
Subscribers: dexonsmith, wuzish, emaste, sdardis, nemanjai, kbarton, MaskRay, atanasyan, arphaman, jfb, abidh, jsji, JDevlieghere, usaxena95, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D73258
2020-01-24 15:23:27 +08:00
|
|
|
//===-- ConnectionFileDescriptorPosix.cpp ---------------------------------===//
|
2010-06-09 00:52:24 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-06-09 00:52:24 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-02-17 06:53:04 +08:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
// Enable this special support for Apple builds where we can have unlimited
|
|
|
|
// select bounds. We tried switching to poll() and kqueue and we were panicing
|
|
|
|
// the kernel, so we have to stick with select for now.
|
|
|
|
#define _DARWIN_UNLIMITED_SELECT
|
|
|
|
#endif
|
|
|
|
|
2022-02-03 20:26:10 +08:00
|
|
|
#include "lldb/Host/posix/ConnectionFileDescriptorPosix.h"
|
2013-08-23 20:44:05 +08:00
|
|
|
#include "lldb/Host/Config.h"
|
2014-08-07 02:16:26 +08:00
|
|
|
#include "lldb/Host/Socket.h"
|
2013-09-04 21:56:11 +08:00
|
|
|
#include "lldb/Host/SocketAddress.h"
|
2022-02-03 20:26:10 +08:00
|
|
|
#include "lldb/Utility/LLDBLog.h"
|
2016-08-11 06:43:48 +08:00
|
|
|
#include "lldb/Utility/SelectHelper.h"
|
2017-06-27 18:33:14 +08:00
|
|
|
#include "lldb/Utility/Timeout.h"
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2021-05-26 18:19:37 +08:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2010-06-09 00:52:24 +08:00
|
|
|
#include <fcntl.h>
|
2013-08-23 20:44:05 +08:00
|
|
|
#include <sys/types.h>
|
2014-08-07 02:16:26 +08:00
|
|
|
|
2019-12-13 02:00:45 +08:00
|
|
|
#if LLDB_ENABLE_POSIX
|
2013-06-24 23:03:16 +08:00
|
|
|
#include <termios.h>
|
2017-07-18 21:14:01 +08:00
|
|
|
#include <unistd.h>
|
2013-09-04 21:56:11 +08:00
|
|
|
#endif
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2019-02-12 07:13:08 +08:00
|
|
|
#include <memory>
|
2015-08-21 07:09:34 +08:00
|
|
|
#include <sstream>
|
|
|
|
|
2017-06-06 22:06:17 +08:00
|
|
|
#include "llvm/Support/Errno.h"
|
2013-09-18 16:09:31 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2013-02-17 06:53:04 +08:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#endif
|
2013-11-21 05:07:01 +08:00
|
|
|
#include "lldb/Host/Host.h"
|
2014-08-07 02:16:26 +08:00
|
|
|
#include "lldb/Host/Socket.h"
|
2015-10-16 07:54:09 +08:00
|
|
|
#include "lldb/Host/common/TCPSocket.h"
|
2020-04-23 19:54:11 +08:00
|
|
|
#include "lldb/Host/common/UDPSocket.h"
|
2017-03-04 04:56:28 +08:00
|
|
|
#include "lldb/Utility/Log.h"
|
2017-02-03 05:39:50 +08:00
|
|
|
#include "lldb/Utility/StreamString.h"
|
2017-06-29 22:32:17 +08:00
|
|
|
#include "lldb/Utility/Timer.h"
|
2013-11-21 05:07:01 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
2014-11-15 00:25:18 +08:00
|
|
|
ConnectionFileDescriptor::ConnectionFileDescriptor(bool child_processes_inherit)
|
2016-05-18 09:59:10 +08:00
|
|
|
: Connection(), m_pipe(), m_mutex(), m_shutting_down(false),
|
2021-06-10 00:25:29 +08:00
|
|
|
|
2016-05-18 09:59:10 +08:00
|
|
|
m_child_processes_inherit(child_processes_inherit) {
|
2022-01-31 22:57:48 +08:00
|
|
|
Log *log(GetLog(LLDBLog::Connection | LLDBLog::Object));
|
2019-07-25 01:56:10 +08:00
|
|
|
LLDB_LOGF(log, "%p ConnectionFileDescriptor::ConnectionFileDescriptor ()",
|
|
|
|
static_cast<void *>(this));
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2014-10-07 05:22:36 +08:00
|
|
|
ConnectionFileDescriptor::ConnectionFileDescriptor(int fd, bool owns_fd)
|
2016-05-18 09:59:10 +08:00
|
|
|
: Connection(), m_pipe(), m_mutex(), m_shutting_down(false),
|
2021-10-22 23:42:23 +08:00
|
|
|
m_child_processes_inherit(false) {
|
2021-10-08 17:54:25 +08:00
|
|
|
m_io_sp =
|
2021-10-07 22:28:53 +08:00
|
|
|
std::make_shared<NativeFile>(fd, File::eOpenOptionReadWrite, owns_fd);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2022-01-31 22:57:48 +08:00
|
|
|
Log *log(GetLog(LLDBLog::Connection | LLDBLog::Object));
|
2019-07-25 01:56:10 +08:00
|
|
|
LLDB_LOGF(log,
|
|
|
|
"%p ConnectionFileDescriptor::ConnectionFileDescriptor (fd = "
|
|
|
|
"%i, owns_fd = %i)",
|
|
|
|
static_cast<void *>(this), fd, owns_fd);
|
2014-10-07 05:22:36 +08:00
|
|
|
OpenCommandPipe();
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2016-05-18 09:59:10 +08:00
|
|
|
ConnectionFileDescriptor::ConnectionFileDescriptor(Socket *socket)
|
|
|
|
: Connection(), m_pipe(), m_mutex(), m_shutting_down(false),
|
2021-10-22 23:42:23 +08:00
|
|
|
m_child_processes_inherit(false) {
|
2015-03-25 20:51:31 +08:00
|
|
|
InitializeSocket(socket);
|
|
|
|
}
|
|
|
|
|
2014-10-07 05:22:36 +08:00
|
|
|
ConnectionFileDescriptor::~ConnectionFileDescriptor() {
|
2022-01-31 22:57:48 +08:00
|
|
|
Log *log(GetLog(LLDBLog::Connection | LLDBLog::Object));
|
2019-07-25 01:56:10 +08:00
|
|
|
LLDB_LOGF(log, "%p ConnectionFileDescriptor::~ConnectionFileDescriptor ()",
|
|
|
|
static_cast<void *>(this));
|
[lldb] NFC modernize codebase with modernize-use-nullptr
Summary:
NFC = [[ https://llvm.org/docs/Lexicon.html#nfc | Non functional change ]]
This commit is the result of modernizing the LLDB codebase by using
`nullptr` instread of `0` or `NULL`. See
https://clang.llvm.org/extra/clang-tidy/checks/modernize-use-nullptr.html
for more information.
This is the command I ran and I to fix and format the code base:
```
run-clang-tidy.py \
-header-filter='.*' \
-checks='-*,modernize-use-nullptr' \
-fix ~/dev/llvm-project/lldb/.* \
-format \
-style LLVM \
-p ~/llvm-builds/debug-ninja-gcc
```
NOTE: There were also changes to `llvm/utils/unittest` but I did not
include them because I felt that maybe this library shall be updated in
isolation somehow.
NOTE: I know this is a rather large commit but it is a nobrainer in most
parts.
Reviewers: martong, espindola, shafik, #lldb, JDevlieghere
Reviewed By: JDevlieghere
Subscribers: arsenm, jvesely, nhaehnle, hiraditya, JDevlieghere, teemperor, rnkovacs, emaste, kubamracek, nemanjai, ki.stfu, javed.absar, arichardson, kbarton, jrtc27, MaskRay, atanasyan, dexonsmith, arphaman, jfb, jsji, jdoerfert, lldb-commits, llvm-commits
Tags: #lldb, #llvm
Differential Revision: https://reviews.llvm.org/D61847
llvm-svn: 361484
2019-05-23 19:14:47 +08:00
|
|
|
Disconnect(nullptr);
|
2014-10-07 05:22:36 +08:00
|
|
|
CloseCommandPipe();
|
2012-07-12 09:17:55 +08:00
|
|
|
}
|
|
|
|
|
2014-10-07 05:22:36 +08:00
|
|
|
void ConnectionFileDescriptor::OpenCommandPipe() {
|
|
|
|
CloseCommandPipe();
|
2012-07-12 09:17:55 +08:00
|
|
|
|
2022-01-31 22:57:48 +08:00
|
|
|
Log *log = GetLog(LLDBLog::Connection);
|
2012-07-12 09:17:55 +08:00
|
|
|
// Make the command file descriptor here:
|
2017-05-12 12:51:55 +08:00
|
|
|
Status result = m_pipe.CreateNew(m_child_processes_inherit);
|
2014-12-18 02:02:19 +08:00
|
|
|
if (!result.Success()) {
|
2019-07-25 01:56:10 +08:00
|
|
|
LLDB_LOGF(log,
|
|
|
|
"%p ConnectionFileDescriptor::OpenCommandPipe () - could not "
|
|
|
|
"make pipe: %s",
|
|
|
|
static_cast<void *>(this), result.AsCString());
|
2014-10-07 05:22:36 +08:00
|
|
|
} else {
|
2019-07-25 01:56:10 +08:00
|
|
|
LLDB_LOGF(log,
|
|
|
|
"%p ConnectionFileDescriptor::OpenCommandPipe() - success "
|
|
|
|
"readfd=%d writefd=%d",
|
|
|
|
static_cast<void *>(this), m_pipe.GetReadFileDescriptor(),
|
|
|
|
m_pipe.GetWriteFileDescriptor());
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2014-10-07 05:22:36 +08:00
|
|
|
void ConnectionFileDescriptor::CloseCommandPipe() {
|
2022-01-31 22:57:48 +08:00
|
|
|
Log *log = GetLog(LLDBLog::Connection);
|
2019-07-25 01:56:10 +08:00
|
|
|
LLDB_LOGF(log, "%p ConnectionFileDescriptor::CloseCommandPipe()",
|
|
|
|
static_cast<void *>(this));
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2014-10-07 05:22:36 +08:00
|
|
|
m_pipe.Close();
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2012-08-09 06:27:52 +08:00
|
|
|
bool ConnectionFileDescriptor::IsConnected() const {
|
2021-10-08 17:54:25 +08:00
|
|
|
return m_io_sp && m_io_sp->IsValid();
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2016-11-18 05:15:14 +08:00
|
|
|
ConnectionStatus ConnectionFileDescriptor::Connect(llvm::StringRef path,
|
2017-05-12 12:51:55 +08:00
|
|
|
Status *error_ptr) {
|
2021-10-22 23:42:23 +08:00
|
|
|
return Connect(path, nullptr, error_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionStatus
|
|
|
|
ConnectionFileDescriptor::Connect(llvm::StringRef path,
|
|
|
|
socket_id_callback_type socket_id_callback,
|
|
|
|
Status *error_ptr) {
|
2015-11-03 09:37:01 +08:00
|
|
|
std::lock_guard<std::recursive_mutex> guard(m_mutex);
|
2022-01-31 22:57:48 +08:00
|
|
|
Log *log = GetLog(LLDBLog::Connection);
|
2019-07-25 01:56:10 +08:00
|
|
|
LLDB_LOGF(log, "%p ConnectionFileDescriptor::Connect (url = '%s')",
|
|
|
|
static_cast<void *>(this), path.str().c_str());
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-11-03 09:37:01 +08:00
|
|
|
OpenCommandPipe();
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2021-10-07 23:58:06 +08:00
|
|
|
if (path.empty()) {
|
|
|
|
if (error_ptr)
|
|
|
|
error_ptr->SetErrorString("invalid connect arguments");
|
|
|
|
return eConnectionStatusError;
|
|
|
|
}
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2021-10-07 23:58:06 +08:00
|
|
|
llvm::StringRef scheme;
|
|
|
|
std::tie(scheme, path) = path.split("://");
|
2014-08-07 02:16:26 +08:00
|
|
|
|
2021-10-07 23:58:06 +08:00
|
|
|
if (!path.empty()) {
|
|
|
|
auto method =
|
|
|
|
llvm::StringSwitch<ConnectionStatus (ConnectionFileDescriptor::*)(
|
2021-10-22 23:42:23 +08:00
|
|
|
llvm::StringRef, socket_id_callback_type, Status *)>(scheme)
|
2021-10-26 04:55:48 +08:00
|
|
|
.Case("listen", &ConnectionFileDescriptor::AcceptTCP)
|
2021-10-07 23:58:06 +08:00
|
|
|
.Cases("accept", "unix-accept",
|
2021-10-26 04:55:48 +08:00
|
|
|
&ConnectionFileDescriptor::AcceptNamedSocket)
|
2021-10-22 23:42:23 +08:00
|
|
|
.Case("unix-abstract-accept",
|
2021-10-26 04:55:48 +08:00
|
|
|
&ConnectionFileDescriptor::AcceptAbstractSocket)
|
2021-10-07 23:58:06 +08:00
|
|
|
.Cases("connect", "tcp-connect",
|
|
|
|
&ConnectionFileDescriptor::ConnectTCP)
|
|
|
|
.Case("udp", &ConnectionFileDescriptor::ConnectUDP)
|
2021-10-26 04:55:48 +08:00
|
|
|
.Case("unix-connect", &ConnectionFileDescriptor::ConnectNamedSocket)
|
2021-10-07 23:58:06 +08:00
|
|
|
.Case("unix-abstract-connect",
|
2021-10-26 04:55:48 +08:00
|
|
|
&ConnectionFileDescriptor::ConnectAbstractSocket)
|
2021-10-07 23:58:06 +08:00
|
|
|
#if LLDB_ENABLE_POSIX
|
|
|
|
.Case("fd", &ConnectionFileDescriptor::ConnectFD)
|
|
|
|
.Case("file", &ConnectionFileDescriptor::ConnectFile)
|
2021-10-08 05:14:23 +08:00
|
|
|
.Case("serial", &ConnectionFileDescriptor::ConnectSerialPort)
|
2014-08-07 02:16:26 +08:00
|
|
|
#endif
|
2021-10-07 23:58:06 +08:00
|
|
|
.Default(nullptr);
|
|
|
|
|
2021-10-26 04:55:48 +08:00
|
|
|
if (method) {
|
|
|
|
if (error_ptr)
|
|
|
|
*error_ptr = Status();
|
2021-10-22 23:42:23 +08:00
|
|
|
return (this->*method)(path, socket_id_callback, error_ptr);
|
2021-10-26 04:55:48 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2021-10-07 23:58:06 +08:00
|
|
|
|
2014-08-07 02:16:26 +08:00
|
|
|
if (error_ptr)
|
2021-10-07 23:58:06 +08:00
|
|
|
error_ptr->SetErrorStringWithFormat("unsupported connection URL: '%s'",
|
|
|
|
path.str().c_str());
|
2014-08-07 02:16:26 +08:00
|
|
|
return eConnectionStatusError;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:07:41 +08:00
|
|
|
bool ConnectionFileDescriptor::InterruptRead() {
|
|
|
|
size_t bytes_written = 0;
|
2017-05-12 12:51:55 +08:00
|
|
|
Status result = m_pipe.Write("i", 1, bytes_written);
|
2012-07-17 09:47:11 +08:00
|
|
|
return result.Success();
|
2015-04-08 02:45:03 +08:00
|
|
|
}
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
ConnectionStatus ConnectionFileDescriptor::Disconnect(Status *error_ptr) {
|
2022-01-31 22:57:48 +08:00
|
|
|
Log *log = GetLog(LLDBLog::Connection);
|
2019-07-25 01:56:10 +08:00
|
|
|
LLDB_LOGF(log, "%p ConnectionFileDescriptor::Disconnect ()",
|
|
|
|
static_cast<void *>(this));
|
2016-05-03 22:07:41 +08:00
|
|
|
|
|
|
|
ConnectionStatus status = eConnectionStatusSuccess;
|
|
|
|
|
2014-08-07 02:16:26 +08:00
|
|
|
if (!IsConnected()) {
|
2019-07-25 01:56:10 +08:00
|
|
|
LLDB_LOGF(
|
|
|
|
log, "%p ConnectionFileDescriptor::Disconnect(): Nothing to disconnect",
|
|
|
|
static_cast<void *>(this));
|
2016-05-03 22:07:41 +08:00
|
|
|
return eConnectionStatusSuccess;
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2021-10-08 17:54:25 +08:00
|
|
|
if (m_io_sp->GetFdType() == IOObject::eFDTypeSocket)
|
|
|
|
static_cast<Socket &>(*m_io_sp).PreDisconnect();
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Try to get the ConnectionFileDescriptor's mutex. If we fail, that is
|
|
|
|
// quite likely because somebody is doing a blocking read on our file
|
|
|
|
// descriptor. If that's the case, then send the "q" char to the command
|
|
|
|
// file channel so the read will wake up and the connection will then know to
|
|
|
|
// shut down.
|
2014-10-07 05:22:36 +08:00
|
|
|
std::unique_lock<std::recursive_mutex> locker(m_mutex, std::defer_lock);
|
|
|
|
if (!locker.try_lock()) {
|
|
|
|
if (m_pipe.CanWrite()) {
|
|
|
|
size_t bytes_written = 0;
|
2017-05-12 12:51:55 +08:00
|
|
|
Status result = m_pipe.Write("q", 1, bytes_written);
|
2019-07-25 01:56:10 +08:00
|
|
|
LLDB_LOGF(log,
|
|
|
|
"%p ConnectionFileDescriptor::Disconnect(): Couldn't get "
|
|
|
|
"the lock, sent 'q' to %d, error = '%s'.",
|
|
|
|
static_cast<void *>(this), m_pipe.GetWriteFileDescriptor(),
|
|
|
|
result.AsCString());
|
2014-10-07 05:22:36 +08:00
|
|
|
} else if (log) {
|
2019-07-25 01:56:10 +08:00
|
|
|
LLDB_LOGF(log,
|
|
|
|
"%p ConnectionFileDescriptor::Disconnect(): Couldn't get the "
|
|
|
|
"lock, but no command pipe is available.",
|
|
|
|
static_cast<void *>(this));
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
2016-05-18 09:59:10 +08:00
|
|
|
locker.lock();
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2019-08-27 09:34:16 +08:00
|
|
|
// Prevents reads and writes during shutdown.
|
|
|
|
m_shutting_down = true;
|
|
|
|
|
2021-10-08 17:54:25 +08:00
|
|
|
Status error = m_io_sp->Close();
|
|
|
|
if (error.Fail())
|
2014-08-07 02:16:26 +08:00
|
|
|
status = eConnectionStatusError;
|
2010-06-09 00:52:24 +08:00
|
|
|
if (error_ptr)
|
2021-10-08 17:54:25 +08:00
|
|
|
*error_ptr = error;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-05-03 22:07:41 +08:00
|
|
|
// Close any pipes we were using for async interrupts
|
2015-05-23 11:54:53 +08:00
|
|
|
m_pipe.Close();
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-01-17 10:20:29 +08:00
|
|
|
m_uri.clear();
|
2012-07-17 09:47:11 +08:00
|
|
|
m_shutting_down = false;
|
2016-05-03 22:07:41 +08:00
|
|
|
return status;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2014-10-07 05:22:36 +08:00
|
|
|
size_t ConnectionFileDescriptor::Read(void *dst, size_t dst_len,
|
2016-11-25 20:22:32 +08:00
|
|
|
const Timeout<std::micro> &timeout,
|
2014-10-07 05:22:36 +08:00
|
|
|
ConnectionStatus &status,
|
2017-05-12 12:51:55 +08:00
|
|
|
Status *error_ptr) {
|
2022-01-31 22:57:48 +08:00
|
|
|
Log *log = GetLog(LLDBLog::Connection);
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-05-18 09:59:10 +08:00
|
|
|
std::unique_lock<std::recursive_mutex> locker(m_mutex, std::defer_lock);
|
|
|
|
if (!locker.try_lock()) {
|
2019-07-25 01:56:10 +08:00
|
|
|
LLDB_LOGF(log,
|
|
|
|
"%p ConnectionFileDescriptor::Read () failed to get the "
|
|
|
|
"connection lock.",
|
|
|
|
static_cast<void *>(this));
|
2010-06-09 00:52:24 +08:00
|
|
|
if (error_ptr)
|
2014-10-07 05:22:36 +08:00
|
|
|
error_ptr->SetErrorString("failed to get the connection lock for read.");
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-07-17 09:47:11 +08:00
|
|
|
status = eConnectionStatusTimedOut;
|
2015-04-08 02:45:03 +08:00
|
|
|
return 0;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2015-04-08 02:45:03 +08:00
|
|
|
if (m_shutting_down) {
|
2019-08-27 09:34:16 +08:00
|
|
|
if (error_ptr)
|
|
|
|
error_ptr->SetErrorString("shutting down");
|
2014-08-07 02:16:26 +08:00
|
|
|
status = eConnectionStatusError;
|
2015-04-08 02:45:03 +08:00
|
|
|
return 0;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2016-11-25 20:22:32 +08:00
|
|
|
status = BytesAvailable(timeout, error_ptr);
|
2014-10-07 05:22:36 +08:00
|
|
|
if (status != eConnectionStatusSuccess)
|
2015-04-08 02:45:03 +08:00
|
|
|
return 0;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error;
|
2016-05-03 22:07:41 +08:00
|
|
|
size_t bytes_read = dst_len;
|
2021-10-08 17:54:25 +08:00
|
|
|
error = m_io_sp->Read(dst, bytes_read);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
if (log) {
|
2019-07-25 01:56:10 +08:00
|
|
|
LLDB_LOGF(log,
|
|
|
|
"%p ConnectionFileDescriptor::Read() fd = %" PRIu64
|
|
|
|
", dst = %p, dst_len = %" PRIu64 ") => %" PRIu64 ", error = %s",
|
|
|
|
static_cast<void *>(this),
|
2021-10-08 17:54:25 +08:00
|
|
|
static_cast<uint64_t>(m_io_sp->GetWaitableHandle()),
|
2019-07-25 01:56:10 +08:00
|
|
|
static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
|
|
|
|
static_cast<uint64_t>(bytes_read), error.AsCString());
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:07:41 +08:00
|
|
|
if (bytes_read == 0) {
|
|
|
|
error.Clear(); // End-of-file. Do not automatically close; pass along for
|
|
|
|
// the end-of-file handlers.
|
2014-10-07 05:22:36 +08:00
|
|
|
status = eConnectionStatusEndOfFile;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2012-07-17 09:47:11 +08:00
|
|
|
if (error_ptr)
|
2010-06-09 00:52:24 +08:00
|
|
|
*error_ptr = error;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
if (error.Fail()) {
|
|
|
|
uint32_t error_value = error.GetError();
|
|
|
|
switch (error_value) {
|
2014-10-07 05:22:36 +08:00
|
|
|
case EAGAIN: // The file was marked for non-blocking I/O, and no data were
|
|
|
|
// ready to be read.
|
2021-10-08 17:54:25 +08:00
|
|
|
if (m_io_sp->GetFdType() == IOObject::eFDTypeSocket)
|
2012-07-17 09:47:11 +08:00
|
|
|
status = eConnectionStatusTimedOut;
|
2016-09-07 04:57:50 +08:00
|
|
|
else
|
2014-10-07 05:22:36 +08:00
|
|
|
status = eConnectionStatusSuccess;
|
2015-04-08 02:45:03 +08:00
|
|
|
return 0;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-07 05:22:36 +08:00
|
|
|
case EFAULT: // Buf points outside the allocated address space.
|
|
|
|
case EINTR: // A read from a slow device was interrupted before any data
|
|
|
|
// arrived by the delivery of a signal.
|
|
|
|
case EINVAL: // The pointer associated with fildes was negative.
|
|
|
|
case EIO: // An I/O error occurred while reading from the file system.
|
|
|
|
// The process group is orphaned.
|
2018-05-01 00:49:04 +08:00
|
|
|
// The file is a regular file, nbyte is greater than 0, the
|
|
|
|
// starting position is before the end-of-file, and the
|
|
|
|
// starting position is greater than or equal to the offset
|
|
|
|
// maximum established for the open file descriptor
|
|
|
|
// associated with fildes.
|
2014-10-07 05:22:36 +08:00
|
|
|
case EISDIR: // An attempt is made to read a directory.
|
|
|
|
case ENOBUFS: // An attempt to allocate a memory buffer fails.
|
|
|
|
case ENOMEM: // Insufficient memory is available.
|
2015-04-08 02:45:03 +08:00
|
|
|
status = eConnectionStatusError;
|
2014-10-07 05:22:36 +08:00
|
|
|
break; // Break to close....
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-07 05:22:36 +08:00
|
|
|
case ENOENT: // no such file or directory
|
|
|
|
case EBADF: // fildes is not a valid file or socket descriptor open for
|
|
|
|
// reading.
|
|
|
|
case ENXIO: // An action is requested of a device that does not exist..
|
|
|
|
// A requested action cannot be performed by the device.
|
|
|
|
case ECONNRESET: // The connection is closed by the peer during a read
|
|
|
|
// attempt on a socket.
|
|
|
|
case ENOTCONN: // A read is attempted on an unconnected socket.
|
|
|
|
status = eConnectionStatusLostConnection;
|
|
|
|
break; // Break to close....
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-07 05:22:36 +08:00
|
|
|
case ETIMEDOUT: // A transmission timeout occurs during a read attempt on a
|
|
|
|
// socket.
|
2011-07-18 04:36:25 +08:00
|
|
|
status = eConnectionStatusTimedOut;
|
2014-10-07 05:22:36 +08:00
|
|
|
return 0;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
default:
|
2017-06-06 22:06:17 +08:00
|
|
|
LLDB_LOG(log, "this = {0}, unexpected error: {1}", this,
|
|
|
|
llvm::sys::StrError(error_value));
|
2014-10-07 05:22:36 +08:00
|
|
|
status = eConnectionStatusError;
|
|
|
|
break; // Break to close....
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2014-10-07 05:22:36 +08:00
|
|
|
return 0;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2014-10-07 05:22:36 +08:00
|
|
|
return bytes_read;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2014-10-07 05:22:36 +08:00
|
|
|
size_t ConnectionFileDescriptor::Write(const void *src, size_t src_len,
|
2016-05-03 22:07:41 +08:00
|
|
|
ConnectionStatus &status,
|
2017-05-12 12:51:55 +08:00
|
|
|
Status *error_ptr) {
|
2022-01-31 22:57:48 +08:00
|
|
|
Log *log = GetLog(LLDBLog::Connection);
|
2019-07-25 01:56:10 +08:00
|
|
|
LLDB_LOGF(log,
|
|
|
|
"%p ConnectionFileDescriptor::Write (src = %p, src_len = %" PRIu64
|
|
|
|
")",
|
|
|
|
static_cast<void *>(this), static_cast<const void *>(src),
|
|
|
|
static_cast<uint64_t>(src_len));
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-07 05:22:36 +08:00
|
|
|
if (!IsConnected()) {
|
2010-06-09 00:52:24 +08:00
|
|
|
if (error_ptr)
|
2013-02-17 06:53:04 +08:00
|
|
|
error_ptr->SetErrorString("not connected");
|
2010-06-09 00:52:24 +08:00
|
|
|
status = eConnectionStatusNoConnection;
|
|
|
|
return 0;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2019-08-27 09:34:16 +08:00
|
|
|
if (m_shutting_down) {
|
|
|
|
if (error_ptr)
|
|
|
|
error_ptr->SetErrorString("shutting down");
|
|
|
|
status = eConnectionStatusError;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-08-07 02:16:26 +08:00
|
|
|
size_t bytes_sent = src_len;
|
2021-10-08 17:54:25 +08:00
|
|
|
error = m_io_sp->Write(src, bytes_sent);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
if (log) {
|
2019-07-25 01:56:10 +08:00
|
|
|
LLDB_LOGF(log,
|
|
|
|
"%p ConnectionFileDescriptor::Write(fd = %" PRIu64
|
|
|
|
", src = %p, src_len = %" PRIu64 ") => %" PRIu64 " (error = %s)",
|
|
|
|
static_cast<void *>(this),
|
2021-10-08 17:54:25 +08:00
|
|
|
static_cast<uint64_t>(m_io_sp->GetWaitableHandle()),
|
2019-07-25 01:56:10 +08:00
|
|
|
static_cast<const void *>(src), static_cast<uint64_t>(src_len),
|
|
|
|
static_cast<uint64_t>(bytes_sent), error.AsCString());
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
if (error_ptr)
|
|
|
|
*error_ptr = error;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
if (error.Fail()) {
|
|
|
|
switch (error.GetError()) {
|
2014-10-07 05:22:36 +08:00
|
|
|
case EAGAIN:
|
|
|
|
case EINTR:
|
|
|
|
status = eConnectionStatusSuccess;
|
2010-06-09 00:52:24 +08:00
|
|
|
return 0;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-10-07 05:22:36 +08:00
|
|
|
case ECONNRESET: // The connection is closed by the peer during a read
|
|
|
|
// attempt on a socket.
|
|
|
|
case ENOTCONN: // A read is attempted on an unconnected socket.
|
|
|
|
status = eConnectionStatusLostConnection;
|
|
|
|
break; // Break to close....
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
default:
|
2014-10-07 05:22:36 +08:00
|
|
|
status = eConnectionStatusError;
|
|
|
|
break; // Break to close....
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-17 10:20:29 +08:00
|
|
|
status = eConnectionStatusSuccess;
|
|
|
|
return bytes_sent;
|
|
|
|
}
|
|
|
|
|
2016-08-11 06:43:48 +08:00
|
|
|
std::string ConnectionFileDescriptor::GetURI() { return m_uri; }
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-08-11 06:43:48 +08:00
|
|
|
// This ConnectionFileDescriptor::BytesAvailable() uses select() via
|
|
|
|
// SelectHelper
|
2013-02-17 06:53:04 +08:00
|
|
|
//
|
|
|
|
// PROS:
|
|
|
|
// - select is consistent across most unix platforms
|
2014-06-11 05:33:43 +08:00
|
|
|
// - The Apple specific version allows for unlimited fds in the fd_sets by
|
2013-02-17 06:53:04 +08:00
|
|
|
// setting the _DARWIN_UNLIMITED_SELECT define prior to including the
|
|
|
|
// required header files.
|
2014-06-12 02:10:41 +08:00
|
|
|
// CONS:
|
|
|
|
// - on non-Apple platforms, only supports file descriptors up to FD_SETSIZE.
|
|
|
|
// This implementation will assert if it runs into that hard limit to let
|
|
|
|
// users know that another ConnectionFileDescriptor::BytesAvailable() should
|
|
|
|
// be used or a new version of ConnectionFileDescriptor::BytesAvailable()
|
|
|
|
// should be written for the system that is running into the limitations.
|
2013-02-17 06:53:04 +08:00
|
|
|
|
2016-11-25 20:22:32 +08:00
|
|
|
ConnectionStatus
|
|
|
|
ConnectionFileDescriptor::BytesAvailable(const Timeout<std::micro> &timeout,
|
2017-05-12 12:51:55 +08:00
|
|
|
Status *error_ptr) {
|
2012-07-17 09:47:11 +08:00
|
|
|
// Don't need to take the mutex here separately since we are only called from
|
2018-05-01 00:49:04 +08:00
|
|
|
// Read. If we ever get used more generally we will need to lock here as
|
|
|
|
// well.
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2022-01-31 22:57:48 +08:00
|
|
|
Log *log = GetLog(LLDBLog::Connection);
|
2017-02-10 19:49:33 +08:00
|
|
|
LLDB_LOG(log, "this = {0}, timeout = {1}", this, timeout);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Make a copy of the file descriptors to make sure we don't have another
|
|
|
|
// thread change these values out from under us and cause problems in the
|
|
|
|
// loop below where like in FS_SET()
|
2021-10-08 17:54:25 +08:00
|
|
|
const IOObject::WaitableHandle handle = m_io_sp->GetWaitableHandle();
|
2014-07-03 05:10:39 +08:00
|
|
|
const int pipe_fd = m_pipe.GetReadFileDescriptor();
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-08-07 02:16:26 +08:00
|
|
|
if (handle != IOObject::kInvalidHandleValue) {
|
2016-08-11 06:43:48 +08:00
|
|
|
SelectHelper select_helper;
|
2016-11-25 20:22:32 +08:00
|
|
|
if (timeout)
|
|
|
|
select_helper.SetTimeout(*timeout);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-08-11 06:43:48 +08:00
|
|
|
select_helper.FDSetRead(handle);
|
2019-09-23 20:03:56 +08:00
|
|
|
#if defined(_WIN32)
|
2014-08-08 07:35:20 +08:00
|
|
|
// select() won't accept pipes on Windows. The entire Windows codepath
|
2018-05-01 00:49:04 +08:00
|
|
|
// needs to be converted over to using WaitForMultipleObjects and event
|
|
|
|
// HANDLEs, but for now at least this will allow ::select() to not return
|
|
|
|
// an error.
|
2014-08-08 07:35:20 +08:00
|
|
|
const bool have_pipe_fd = false;
|
2016-09-07 04:57:50 +08:00
|
|
|
#else
|
2013-02-17 06:53:04 +08:00
|
|
|
const bool have_pipe_fd = pipe_fd >= 0;
|
2016-09-07 04:57:50 +08:00
|
|
|
#endif
|
2016-08-11 06:43:48 +08:00
|
|
|
if (have_pipe_fd)
|
|
|
|
select_helper.FDSetRead(pipe_fd);
|
2013-02-17 06:53:04 +08:00
|
|
|
|
2021-10-08 17:54:25 +08:00
|
|
|
while (handle == m_io_sp->GetWaitableHandle()) {
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error = select_helper.Select();
|
2014-08-07 02:16:26 +08:00
|
|
|
|
2013-02-17 06:53:04 +08:00
|
|
|
if (error_ptr)
|
|
|
|
*error_ptr = error;
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2013-02-17 06:53:04 +08:00
|
|
|
if (error.Fail()) {
|
|
|
|
switch (error.GetError()) {
|
|
|
|
case EBADF: // One of the descriptor sets specified an invalid
|
2014-10-07 05:22:36 +08:00
|
|
|
// descriptor.
|
2013-02-17 06:53:04 +08:00
|
|
|
return eConnectionStatusLostConnection;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-02-17 06:53:04 +08:00
|
|
|
case EINVAL: // The specified time limit is invalid. One of its
|
|
|
|
// components is negative or too large.
|
2014-10-07 05:22:36 +08:00
|
|
|
default: // Other unknown error
|
2013-02-17 06:53:04 +08:00
|
|
|
return eConnectionStatusError;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-08-11 06:43:48 +08:00
|
|
|
case ETIMEDOUT:
|
2013-02-17 06:53:04 +08:00
|
|
|
return eConnectionStatusTimedOut;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-02-17 06:53:04 +08:00
|
|
|
case EAGAIN: // The kernel was (perhaps temporarily) unable to
|
2018-05-01 00:49:04 +08:00
|
|
|
// allocate the requested number of file descriptors, or
|
|
|
|
// we have non-blocking IO
|
2013-02-17 06:53:04 +08:00
|
|
|
case EINTR: // A signal was delivered before the time limit
|
2018-05-01 00:49:04 +08:00
|
|
|
// expired and before any of the selected events occurred.
|
2013-02-17 06:53:04 +08:00
|
|
|
break; // Lets keep reading to until we timeout
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2014-08-07 02:16:26 +08:00
|
|
|
} else {
|
|
|
|
if (select_helper.FDIsSetRead(handle))
|
2014-07-03 05:10:39 +08:00
|
|
|
return eConnectionStatusSuccess;
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2016-08-11 06:43:48 +08:00
|
|
|
if (select_helper.FDIsSetRead(pipe_fd)) {
|
2018-05-01 00:49:04 +08:00
|
|
|
// There is an interrupt or exit command in the command pipe Read the
|
|
|
|
// data from that pipe:
|
2017-07-03 17:25:55 +08:00
|
|
|
char c;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2021-10-22 23:42:23 +08:00
|
|
|
ssize_t bytes_read =
|
|
|
|
llvm::sys::RetryAfterSignal(-1, ::read, pipe_fd, &c, 1);
|
2017-07-03 17:25:55 +08:00
|
|
|
assert(bytes_read == 1);
|
|
|
|
(void)bytes_read;
|
|
|
|
switch (c) {
|
2014-10-07 05:22:36 +08:00
|
|
|
case 'q':
|
2019-07-25 01:56:10 +08:00
|
|
|
LLDB_LOGF(log,
|
|
|
|
"%p ConnectionFileDescriptor::BytesAvailable() "
|
|
|
|
"got data: %c from the command channel.",
|
|
|
|
static_cast<void *>(this), c);
|
2013-02-17 06:53:04 +08:00
|
|
|
return eConnectionStatusEndOfFile;
|
2014-10-07 05:22:36 +08:00
|
|
|
case 'i':
|
|
|
|
// Interrupt the current read
|
|
|
|
return eConnectionStatusInterrupted;
|
2014-05-02 08:45:31 +08:00
|
|
|
}
|
2013-02-17 06:53:04 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2013-02-17 06:53:04 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2014-04-04 12:06:10 +08:00
|
|
|
|
2013-02-17 06:53:04 +08:00
|
|
|
if (error_ptr)
|
|
|
|
error_ptr->SetErrorString("not connected");
|
|
|
|
return eConnectionStatusLostConnection;
|
|
|
|
}
|
|
|
|
|
2021-10-26 04:55:48 +08:00
|
|
|
lldb::ConnectionStatus ConnectionFileDescriptor::AcceptSocket(
|
|
|
|
Socket::SocketProtocol socket_protocol, llvm::StringRef socket_name,
|
|
|
|
llvm::function_ref<void(Socket &)> post_listen_callback,
|
2021-10-22 23:42:23 +08:00
|
|
|
Status *error_ptr) {
|
|
|
|
Status error;
|
2021-10-26 04:55:48 +08:00
|
|
|
std::unique_ptr<Socket> listening_socket =
|
|
|
|
Socket::Create(socket_protocol, m_child_processes_inherit, error);
|
|
|
|
Socket *accepted_socket;
|
2021-10-22 23:42:23 +08:00
|
|
|
|
|
|
|
if (!error.Fail())
|
2021-10-26 04:55:48 +08:00
|
|
|
error = listening_socket->Listen(socket_name, 5);
|
2021-10-22 23:42:23 +08:00
|
|
|
|
|
|
|
if (!error.Fail()) {
|
2021-10-26 04:55:48 +08:00
|
|
|
post_listen_callback(*listening_socket);
|
|
|
|
error = listening_socket->Accept(accepted_socket);
|
2021-10-22 23:42:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!error.Fail()) {
|
2021-10-26 04:55:48 +08:00
|
|
|
m_io_sp.reset(accepted_socket);
|
2021-10-22 23:42:23 +08:00
|
|
|
m_uri.assign(socket_name.str());
|
|
|
|
return eConnectionStatusSuccess;
|
|
|
|
}
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
if (error_ptr)
|
2014-08-07 02:16:26 +08:00
|
|
|
*error_ptr = error;
|
2021-10-22 23:42:23 +08:00
|
|
|
return eConnectionStatusError;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2021-10-26 04:55:48 +08:00
|
|
|
lldb::ConnectionStatus
|
|
|
|
ConnectionFileDescriptor::ConnectSocket(Socket::SocketProtocol socket_protocol,
|
|
|
|
llvm::StringRef socket_name,
|
|
|
|
Status *error_ptr) {
|
2021-10-22 23:42:23 +08:00
|
|
|
Status error;
|
2021-10-26 04:55:48 +08:00
|
|
|
std::unique_ptr<Socket> socket =
|
|
|
|
Socket::Create(socket_protocol, m_child_processes_inherit, error);
|
2021-10-28 05:49:49 +08:00
|
|
|
|
|
|
|
if (!error.Fail())
|
2021-10-26 04:55:48 +08:00
|
|
|
error = socket->Connect(socket_name);
|
2021-10-22 23:42:23 +08:00
|
|
|
|
|
|
|
if (!error.Fail()) {
|
2021-10-26 04:55:48 +08:00
|
|
|
m_io_sp = std::move(socket);
|
2021-10-22 23:42:23 +08:00
|
|
|
m_uri.assign(socket_name.str());
|
|
|
|
return eConnectionStatusSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error_ptr)
|
|
|
|
*error_ptr = error;
|
|
|
|
return eConnectionStatusError;
|
|
|
|
}
|
|
|
|
|
2021-10-26 04:55:48 +08:00
|
|
|
ConnectionStatus ConnectionFileDescriptor::AcceptNamedSocket(
|
2021-10-22 23:42:23 +08:00
|
|
|
llvm::StringRef socket_name, socket_id_callback_type socket_id_callback,
|
|
|
|
Status *error_ptr) {
|
2021-10-26 04:55:48 +08:00
|
|
|
return AcceptSocket(
|
|
|
|
Socket::ProtocolUnixDomain, socket_name,
|
|
|
|
[socket_id_callback, socket_name](Socket &listening_socket) {
|
|
|
|
socket_id_callback(socket_name);
|
|
|
|
},
|
|
|
|
error_ptr);
|
2010-10-19 11:25:40 +08:00
|
|
|
}
|
|
|
|
|
2021-10-26 04:55:48 +08:00
|
|
|
ConnectionStatus ConnectionFileDescriptor::ConnectNamedSocket(
|
|
|
|
llvm::StringRef socket_name, socket_id_callback_type socket_id_callback,
|
2021-10-22 23:42:23 +08:00
|
|
|
Status *error_ptr) {
|
2021-10-26 04:55:48 +08:00
|
|
|
return ConnectSocket(Socket::ProtocolUnixDomain, socket_name, error_ptr);
|
|
|
|
}
|
2011-04-12 13:54:46 +08:00
|
|
|
|
2021-10-26 04:55:48 +08:00
|
|
|
ConnectionStatus ConnectionFileDescriptor::AcceptAbstractSocket(
|
|
|
|
llvm::StringRef socket_name, socket_id_callback_type socket_id_callback,
|
|
|
|
Status *error_ptr) {
|
|
|
|
return AcceptSocket(
|
|
|
|
Socket::ProtocolUnixAbstract, socket_name,
|
|
|
|
[socket_id_callback, socket_name](Socket &listening_socket) {
|
|
|
|
socket_id_callback(socket_name);
|
|
|
|
},
|
|
|
|
error_ptr);
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2021-10-26 04:55:48 +08:00
|
|
|
lldb::ConnectionStatus ConnectionFileDescriptor::ConnectAbstractSocket(
|
|
|
|
llvm::StringRef socket_name, socket_id_callback_type socket_id_callback,
|
|
|
|
Status *error_ptr) {
|
|
|
|
return ConnectSocket(Socket::ProtocolUnixAbstract, socket_name, error_ptr);
|
|
|
|
}
|
2021-10-28 05:49:49 +08:00
|
|
|
|
2021-10-26 04:55:48 +08:00
|
|
|
ConnectionStatus
|
|
|
|
ConnectionFileDescriptor::AcceptTCP(llvm::StringRef socket_name,
|
|
|
|
socket_id_callback_type socket_id_callback,
|
|
|
|
Status *error_ptr) {
|
|
|
|
ConnectionStatus ret = AcceptSocket(
|
|
|
|
Socket::ProtocolTcp, socket_name,
|
|
|
|
[socket_id_callback](Socket &listening_socket) {
|
|
|
|
uint16_t port =
|
|
|
|
static_cast<TCPSocket &>(listening_socket).GetLocalPortNumber();
|
|
|
|
socket_id_callback(std::to_string(port));
|
|
|
|
},
|
|
|
|
error_ptr);
|
|
|
|
if (ret == eConnectionStatusSuccess)
|
|
|
|
m_uri.assign(
|
|
|
|
static_cast<TCPSocket *>(m_io_sp.get())->GetRemoteConnectionURI());
|
|
|
|
return ret;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 23:42:23 +08:00
|
|
|
ConnectionStatus
|
2021-10-26 04:55:48 +08:00
|
|
|
ConnectionFileDescriptor::ConnectTCP(llvm::StringRef socket_name,
|
2021-10-22 23:42:23 +08:00
|
|
|
socket_id_callback_type socket_id_callback,
|
|
|
|
Status *error_ptr) {
|
2021-10-26 04:55:48 +08:00
|
|
|
return ConnectSocket(Socket::ProtocolTcp, socket_name, error_ptr);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 23:42:23 +08:00
|
|
|
ConnectionStatus
|
|
|
|
ConnectionFileDescriptor::ConnectUDP(llvm::StringRef s,
|
|
|
|
socket_id_callback_type socket_id_callback,
|
|
|
|
Status *error_ptr) {
|
2011-07-16 00:31:38 +08:00
|
|
|
if (error_ptr)
|
2020-04-23 19:54:11 +08:00
|
|
|
*error_ptr = Status();
|
|
|
|
llvm::Expected<std::unique_ptr<UDPSocket>> socket =
|
|
|
|
Socket::UdpConnect(s, m_child_processes_inherit);
|
|
|
|
if (!socket) {
|
|
|
|
if (error_ptr)
|
|
|
|
*error_ptr = socket.takeError();
|
|
|
|
else
|
2022-01-31 22:57:48 +08:00
|
|
|
LLDB_LOG_ERROR(GetLog(LLDBLog::Connection), socket.takeError(),
|
|
|
|
"tcp connect failed: {0}");
|
2015-01-17 10:20:29 +08:00
|
|
|
return eConnectionStatusError;
|
|
|
|
}
|
2021-10-08 17:54:25 +08:00
|
|
|
m_io_sp = std::move(*socket);
|
2020-01-29 03:23:46 +08:00
|
|
|
m_uri.assign(std::string(s));
|
2015-01-17 10:20:29 +08:00
|
|
|
return eConnectionStatusSuccess;
|
2011-07-18 04:36:25 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 23:42:23 +08:00
|
|
|
ConnectionStatus
|
|
|
|
ConnectionFileDescriptor::ConnectFD(llvm::StringRef s,
|
|
|
|
socket_id_callback_type socket_id_callback,
|
|
|
|
Status *error_ptr) {
|
2021-10-07 23:58:06 +08:00
|
|
|
#if LLDB_ENABLE_POSIX
|
|
|
|
// Just passing a native file descriptor within this current process that
|
|
|
|
// is already opened (possibly from a service or other source).
|
|
|
|
int fd = -1;
|
|
|
|
|
|
|
|
if (!s.getAsInteger(0, fd)) {
|
|
|
|
// We have what looks to be a valid file descriptor, but we should make
|
|
|
|
// sure it is. We currently are doing this by trying to get the flags
|
|
|
|
// from the file descriptor and making sure it isn't a bad fd.
|
|
|
|
errno = 0;
|
|
|
|
int flags = ::fcntl(fd, F_GETFL, 0);
|
|
|
|
if (flags == -1 || errno == EBADF) {
|
|
|
|
if (error_ptr)
|
|
|
|
error_ptr->SetErrorStringWithFormat("stale file descriptor: %s",
|
|
|
|
s.str().c_str());
|
2021-10-08 17:54:25 +08:00
|
|
|
m_io_sp.reset();
|
2021-10-07 23:58:06 +08:00
|
|
|
return eConnectionStatusError;
|
|
|
|
} else {
|
|
|
|
// Don't take ownership of a file descriptor that gets passed to us
|
|
|
|
// since someone else opened the file descriptor and handed it to us.
|
|
|
|
// TODO: Since are using a URL to open connection we should
|
|
|
|
// eventually parse options using the web standard where we have
|
|
|
|
// "fd://123?opt1=value;opt2=value" and we can have an option be
|
|
|
|
// "owns=1" or "owns=0" or something like this to allow us to specify
|
|
|
|
// this. For now, we assume we must assume we don't own it.
|
|
|
|
|
|
|
|
std::unique_ptr<TCPSocket> tcp_socket;
|
|
|
|
tcp_socket = std::make_unique<TCPSocket>(fd, false, false);
|
|
|
|
// Try and get a socket option from this file descriptor to see if
|
|
|
|
// this is a socket and set m_is_socket accordingly.
|
|
|
|
int resuse;
|
|
|
|
bool is_socket =
|
|
|
|
!!tcp_socket->GetOption(SOL_SOCKET, SO_REUSEADDR, resuse);
|
2021-10-08 17:54:25 +08:00
|
|
|
if (is_socket)
|
|
|
|
m_io_sp = std::move(tcp_socket);
|
|
|
|
else
|
|
|
|
m_io_sp =
|
2021-10-07 23:58:06 +08:00
|
|
|
std::make_shared<NativeFile>(fd, File::eOpenOptionReadWrite, false);
|
|
|
|
m_uri = s.str();
|
|
|
|
return eConnectionStatusSuccess;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error_ptr)
|
|
|
|
error_ptr->SetErrorStringWithFormat("invalid file descriptor: \"%s\"",
|
|
|
|
s.str().c_str());
|
2021-10-08 17:54:25 +08:00
|
|
|
m_io_sp.reset();
|
2021-10-07 23:58:06 +08:00
|
|
|
return eConnectionStatusError;
|
|
|
|
#endif // LLDB_ENABLE_POSIX
|
|
|
|
llvm_unreachable("this function should be only called w/ LLDB_ENABLE_POSIX");
|
|
|
|
}
|
|
|
|
|
2021-10-22 23:42:23 +08:00
|
|
|
ConnectionStatus ConnectionFileDescriptor::ConnectFile(
|
|
|
|
llvm::StringRef s, socket_id_callback_type socket_id_callback,
|
|
|
|
Status *error_ptr) {
|
2021-10-07 23:58:06 +08:00
|
|
|
#if LLDB_ENABLE_POSIX
|
|
|
|
std::string addr_str = s.str();
|
|
|
|
// file:///PATH
|
|
|
|
int fd = llvm::sys::RetryAfterSignal(-1, ::open, addr_str.c_str(), O_RDWR);
|
|
|
|
if (fd == -1) {
|
|
|
|
if (error_ptr)
|
|
|
|
error_ptr->SetErrorToErrno();
|
|
|
|
return eConnectionStatusError;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (::isatty(fd)) {
|
|
|
|
// Set up serial terminal emulation
|
|
|
|
struct termios options;
|
|
|
|
::tcgetattr(fd, &options);
|
|
|
|
|
|
|
|
// Set port speed to maximum
|
|
|
|
::cfsetospeed(&options, B115200);
|
|
|
|
::cfsetispeed(&options, B115200);
|
|
|
|
|
|
|
|
// Raw input, disable echo and signals
|
|
|
|
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
|
|
|
|
|
|
|
|
// Make sure only one character is needed to return from a read
|
|
|
|
options.c_cc[VMIN] = 1;
|
|
|
|
options.c_cc[VTIME] = 0;
|
|
|
|
|
|
|
|
llvm::sys::RetryAfterSignal(-1, ::tcsetattr, fd, TCSANOW, &options);
|
|
|
|
}
|
|
|
|
|
2021-10-22 23:42:23 +08:00
|
|
|
m_io_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionReadWrite, true);
|
2021-10-07 23:58:06 +08:00
|
|
|
return eConnectionStatusSuccess;
|
|
|
|
#endif // LLDB_ENABLE_POSIX
|
|
|
|
llvm_unreachable("this function should be only called w/ LLDB_ENABLE_POSIX");
|
|
|
|
}
|
|
|
|
|
2021-10-22 23:42:23 +08:00
|
|
|
ConnectionStatus ConnectionFileDescriptor::ConnectSerialPort(
|
|
|
|
llvm::StringRef s, socket_id_callback_type socket_id_callback,
|
|
|
|
Status *error_ptr) {
|
2021-10-08 05:14:23 +08:00
|
|
|
#if LLDB_ENABLE_POSIX
|
|
|
|
llvm::StringRef path, qs;
|
|
|
|
// serial:///PATH?k1=v1&k2=v2...
|
|
|
|
std::tie(path, qs) = s.split('?');
|
|
|
|
|
|
|
|
llvm::Expected<SerialPort::Options> serial_options =
|
|
|
|
SerialPort::OptionsFromURL(qs);
|
|
|
|
if (!serial_options) {
|
|
|
|
if (error_ptr)
|
|
|
|
*error_ptr = serial_options.takeError();
|
|
|
|
else
|
|
|
|
llvm::consumeError(serial_options.takeError());
|
|
|
|
return eConnectionStatusError;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = llvm::sys::RetryAfterSignal(-1, ::open, path.str().c_str(), O_RDWR);
|
|
|
|
if (fd == -1) {
|
|
|
|
if (error_ptr)
|
|
|
|
error_ptr->SetErrorToErrno();
|
|
|
|
return eConnectionStatusError;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Expected<std::unique_ptr<SerialPort>> serial_sp = SerialPort::Create(
|
|
|
|
fd, File::eOpenOptionReadWrite, serial_options.get(), true);
|
|
|
|
if (!serial_sp) {
|
|
|
|
if (error_ptr)
|
|
|
|
*error_ptr = serial_sp.takeError();
|
|
|
|
else
|
|
|
|
llvm::consumeError(serial_sp.takeError());
|
|
|
|
return eConnectionStatusError;
|
|
|
|
}
|
|
|
|
m_io_sp = std::move(serial_sp.get());
|
|
|
|
|
|
|
|
return eConnectionStatusSuccess;
|
|
|
|
#endif // LLDB_ENABLE_POSIX
|
|
|
|
llvm_unreachable("this function should be only called w/ LLDB_ENABLE_POSIX");
|
|
|
|
}
|
|
|
|
|
2014-11-15 00:25:18 +08:00
|
|
|
bool ConnectionFileDescriptor::GetChildProcessesInherit() const {
|
|
|
|
return m_child_processes_inherit;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionFileDescriptor::SetChildProcessesInherit(
|
|
|
|
bool child_processes_inherit) {
|
|
|
|
m_child_processes_inherit = child_processes_inherit;
|
|
|
|
}
|
2015-03-25 20:51:31 +08:00
|
|
|
|
|
|
|
void ConnectionFileDescriptor::InitializeSocket(Socket *socket) {
|
2021-10-08 17:54:25 +08:00
|
|
|
m_io_sp.reset(socket);
|
2019-05-31 07:30:35 +08:00
|
|
|
m_uri = socket->GetRemoteConnectionURI();
|
2015-03-25 20:51:31 +08:00
|
|
|
}
|