aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/llvm-project/lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp')
-rw-r--r--contrib/llvm-project/lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp159
1 files changed, 83 insertions, 76 deletions
diff --git a/contrib/llvm-project/lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp b/contrib/llvm-project/lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
index 067e85972eca..325d854921e3 100644
--- a/contrib/llvm-project/lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
+++ b/contrib/llvm-project/lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
@@ -79,23 +79,22 @@ ConnectionFileDescriptor::ConnectionFileDescriptor(bool child_processes_inherit)
m_child_processes_inherit(child_processes_inherit) {
Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION |
LIBLLDB_LOG_OBJECT));
- if (log)
- log->Printf("%p ConnectionFileDescriptor::ConnectionFileDescriptor ()",
- static_cast<void *>(this));
+ LLDB_LOGF(log, "%p ConnectionFileDescriptor::ConnectionFileDescriptor ()",
+ static_cast<void *>(this));
}
ConnectionFileDescriptor::ConnectionFileDescriptor(int fd, bool owns_fd)
: Connection(), m_pipe(), m_mutex(), m_shutting_down(false),
m_waiting_for_accept(false), m_child_processes_inherit(false) {
- m_write_sp = std::make_shared<File>(fd, owns_fd);
- m_read_sp = std::make_shared<File>(fd, false);
+ m_write_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionWrite, owns_fd);
+ m_read_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionRead, false);
Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION |
LIBLLDB_LOG_OBJECT));
- if (log)
- log->Printf("%p ConnectionFileDescriptor::ConnectionFileDescriptor (fd = "
- "%i, owns_fd = %i)",
- static_cast<void *>(this), fd, owns_fd);
+ LLDB_LOGF(log,
+ "%p ConnectionFileDescriptor::ConnectionFileDescriptor (fd = "
+ "%i, owns_fd = %i)",
+ static_cast<void *>(this), fd, owns_fd);
OpenCommandPipe();
}
@@ -108,9 +107,8 @@ ConnectionFileDescriptor::ConnectionFileDescriptor(Socket *socket)
ConnectionFileDescriptor::~ConnectionFileDescriptor() {
Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION |
LIBLLDB_LOG_OBJECT));
- if (log)
- log->Printf("%p ConnectionFileDescriptor::~ConnectionFileDescriptor ()",
- static_cast<void *>(this));
+ LLDB_LOGF(log, "%p ConnectionFileDescriptor::~ConnectionFileDescriptor ()",
+ static_cast<void *>(this));
Disconnect(nullptr);
CloseCommandPipe();
}
@@ -122,24 +120,23 @@ void ConnectionFileDescriptor::OpenCommandPipe() {
// Make the command file descriptor here:
Status result = m_pipe.CreateNew(m_child_processes_inherit);
if (!result.Success()) {
- if (log)
- log->Printf("%p ConnectionFileDescriptor::OpenCommandPipe () - could not "
- "make pipe: %s",
- static_cast<void *>(this), result.AsCString());
+ LLDB_LOGF(log,
+ "%p ConnectionFileDescriptor::OpenCommandPipe () - could not "
+ "make pipe: %s",
+ static_cast<void *>(this), result.AsCString());
} else {
- if (log)
- log->Printf("%p ConnectionFileDescriptor::OpenCommandPipe() - success "
- "readfd=%d writefd=%d",
- static_cast<void *>(this), m_pipe.GetReadFileDescriptor(),
- m_pipe.GetWriteFileDescriptor());
+ LLDB_LOGF(log,
+ "%p ConnectionFileDescriptor::OpenCommandPipe() - success "
+ "readfd=%d writefd=%d",
+ static_cast<void *>(this), m_pipe.GetReadFileDescriptor(),
+ m_pipe.GetWriteFileDescriptor());
}
}
void ConnectionFileDescriptor::CloseCommandPipe() {
Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
- if (log)
- log->Printf("%p ConnectionFileDescriptor::CloseCommandPipe()",
- static_cast<void *>(this));
+ LLDB_LOGF(log, "%p ConnectionFileDescriptor::CloseCommandPipe()",
+ static_cast<void *>(this));
m_pipe.Close();
}
@@ -153,9 +150,8 @@ ConnectionStatus ConnectionFileDescriptor::Connect(llvm::StringRef path,
Status *error_ptr) {
std::lock_guard<std::recursive_mutex> guard(m_mutex);
Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
- if (log)
- log->Printf("%p ConnectionFileDescriptor::Connect (url = '%s')",
- static_cast<void *>(this), path.str().c_str());
+ LLDB_LOGF(log, "%p ConnectionFileDescriptor::Connect (url = '%s')",
+ static_cast<void *>(this), path.str().c_str());
OpenCommandPipe();
@@ -222,8 +218,10 @@ ConnectionStatus ConnectionFileDescriptor::Connect(llvm::StringRef path,
m_read_sp = std::move(tcp_socket);
m_write_sp = m_read_sp;
} else {
- m_read_sp = std::make_shared<File>(fd, false);
- m_write_sp = std::make_shared<File>(fd, false);
+ m_read_sp =
+ std::make_shared<NativeFile>(fd, File::eOpenOptionRead, false);
+ m_write_sp =
+ std::make_shared<NativeFile>(fd, File::eOpenOptionWrite, false);
}
m_uri = *addr;
return eConnectionStatusSuccess;
@@ -272,8 +270,8 @@ ConnectionStatus ConnectionFileDescriptor::Connect(llvm::StringRef path,
::fcntl(fd, F_SETFL, flags);
}
}
- m_read_sp = std::make_shared<File>(fd, true);
- m_write_sp = std::make_shared<File>(fd, false);
+ m_read_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionRead, true);
+ m_write_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionWrite, false);
return eConnectionStatusSuccess;
}
#endif
@@ -295,17 +293,15 @@ bool ConnectionFileDescriptor::InterruptRead() {
ConnectionStatus ConnectionFileDescriptor::Disconnect(Status *error_ptr) {
Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
- if (log)
- log->Printf("%p ConnectionFileDescriptor::Disconnect ()",
- static_cast<void *>(this));
+ LLDB_LOGF(log, "%p ConnectionFileDescriptor::Disconnect ()",
+ static_cast<void *>(this));
ConnectionStatus status = eConnectionStatusSuccess;
if (!IsConnected()) {
- if (log)
- log->Printf(
- "%p ConnectionFileDescriptor::Disconnect(): Nothing to disconnect",
- static_cast<void *>(this));
+ LLDB_LOGF(
+ log, "%p ConnectionFileDescriptor::Disconnect(): Nothing to disconnect",
+ static_cast<void *>(this));
return eConnectionStatusSuccess;
}
@@ -318,27 +314,28 @@ ConnectionStatus ConnectionFileDescriptor::Disconnect(Status *error_ptr) {
// 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.
-
- m_shutting_down = true;
-
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;
Status result = m_pipe.Write("q", 1, bytes_written);
- if (log)
- log->Printf("%p ConnectionFileDescriptor::Disconnect(): Couldn't get "
- "the lock, sent 'q' to %d, error = '%s'.",
- static_cast<void *>(this), m_pipe.GetWriteFileDescriptor(),
- result.AsCString());
+ 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());
} else if (log) {
- log->Printf("%p ConnectionFileDescriptor::Disconnect(): Couldn't get the "
- "lock, but no command pipe is available.",
- static_cast<void *>(this));
+ LLDB_LOGF(log,
+ "%p ConnectionFileDescriptor::Disconnect(): Couldn't get the "
+ "lock, but no command pipe is available.",
+ static_cast<void *>(this));
}
locker.lock();
}
+ // Prevents reads and writes during shutdown.
+ m_shutting_down = true;
+
Status error = m_read_sp->Close();
Status error2 = m_write_sp->Close();
if (error.Fail() || error2.Fail())
@@ -362,10 +359,10 @@ size_t ConnectionFileDescriptor::Read(void *dst, size_t dst_len,
std::unique_lock<std::recursive_mutex> locker(m_mutex, std::defer_lock);
if (!locker.try_lock()) {
- if (log)
- log->Printf("%p ConnectionFileDescriptor::Read () failed to get the "
- "connection lock.",
- static_cast<void *>(this));
+ LLDB_LOGF(log,
+ "%p ConnectionFileDescriptor::Read () failed to get the "
+ "connection lock.",
+ static_cast<void *>(this));
if (error_ptr)
error_ptr->SetErrorString("failed to get the connection lock for read.");
@@ -374,6 +371,8 @@ size_t ConnectionFileDescriptor::Read(void *dst, size_t dst_len,
}
if (m_shutting_down) {
+ if (error_ptr)
+ error_ptr->SetErrorString("shutting down");
status = eConnectionStatusError;
return 0;
}
@@ -387,12 +386,13 @@ size_t ConnectionFileDescriptor::Read(void *dst, size_t dst_len,
error = m_read_sp->Read(dst, bytes_read);
if (log) {
- log->Printf("%p ConnectionFileDescriptor::Read() fd = %" PRIu64
- ", dst = %p, dst_len = %" PRIu64 ") => %" PRIu64 ", error = %s",
- static_cast<void *>(this),
- static_cast<uint64_t>(m_read_sp->GetWaitableHandle()),
- static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
- static_cast<uint64_t>(bytes_read), error.AsCString());
+ LLDB_LOGF(log,
+ "%p ConnectionFileDescriptor::Read() fd = %" PRIu64
+ ", dst = %p, dst_len = %" PRIu64 ") => %" PRIu64 ", error = %s",
+ static_cast<void *>(this),
+ static_cast<uint64_t>(m_read_sp->GetWaitableHandle()),
+ static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
+ static_cast<uint64_t>(bytes_read), error.AsCString());
}
if (bytes_read == 0) {
@@ -464,11 +464,11 @@ size_t ConnectionFileDescriptor::Write(const void *src, size_t src_len,
ConnectionStatus &status,
Status *error_ptr) {
Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
- if (log)
- log->Printf(
- "%p ConnectionFileDescriptor::Write (src = %p, src_len = %" PRIu64 ")",
- static_cast<void *>(this), static_cast<const void *>(src),
- static_cast<uint64_t>(src_len));
+ 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));
if (!IsConnected()) {
if (error_ptr)
@@ -477,19 +477,26 @@ size_t ConnectionFileDescriptor::Write(const void *src, size_t src_len,
return 0;
}
+ if (m_shutting_down) {
+ if (error_ptr)
+ error_ptr->SetErrorString("shutting down");
+ status = eConnectionStatusError;
+ return 0;
+ }
+
Status error;
size_t bytes_sent = src_len;
error = m_write_sp->Write(src, bytes_sent);
if (log) {
- log->Printf("%p ConnectionFileDescriptor::Write(fd = %" PRIu64
- ", src = %p, src_len = %" PRIu64 ") => %" PRIu64
- " (error = %s)",
- static_cast<void *>(this),
- static_cast<uint64_t>(m_write_sp->GetWaitableHandle()),
- static_cast<const void *>(src), static_cast<uint64_t>(src_len),
- static_cast<uint64_t>(bytes_sent), error.AsCString());
+ LLDB_LOGF(log,
+ "%p ConnectionFileDescriptor::Write(fd = %" PRIu64
+ ", src = %p, src_len = %" PRIu64 ") => %" PRIu64 " (error = %s)",
+ static_cast<void *>(this),
+ static_cast<uint64_t>(m_write_sp->GetWaitableHandle()),
+ static_cast<const void *>(src), static_cast<uint64_t>(src_len),
+ static_cast<uint64_t>(bytes_sent), error.AsCString());
}
if (error_ptr)
@@ -559,7 +566,7 @@ ConnectionFileDescriptor::BytesAvailable(const Timeout<std::micro> &timeout,
select_helper.SetTimeout(*timeout);
select_helper.FDSetRead(handle);
-#if defined(_MSC_VER)
+#if defined(_WIN32)
// select() won't accept pipes on Windows. The entire Windows codepath
// needs to be converted over to using WaitForMultipleObjects and event
// HANDLEs, but for now at least this will allow ::select() to not return
@@ -613,10 +620,10 @@ ConnectionFileDescriptor::BytesAvailable(const Timeout<std::micro> &timeout,
(void)bytes_read;
switch (c) {
case 'q':
- if (log)
- log->Printf("%p ConnectionFileDescriptor::BytesAvailable() "
- "got data: %c from the command channel.",
- static_cast<void *>(this), c);
+ LLDB_LOGF(log,
+ "%p ConnectionFileDescriptor::BytesAvailable() "
+ "got data: %c from the command channel.",
+ static_cast<void *>(this), c);
return eConnectionStatusEndOfFile;
case 'i':
// Interrupt the current read