diff --git a/lldb/include/lldb/Host/windows/MainLoopWindows.h b/lldb/include/lldb/Host/windows/MainLoopWindows.h index 53df815255c3d..705e7e78ba48a 100644 --- a/lldb/include/lldb/Host/windows/MainLoopWindows.h +++ b/lldb/include/lldb/Host/windows/MainLoopWindows.h @@ -17,6 +17,8 @@ namespace lldb_private { +using handle_t = void *; + // Windows-specific implementation of the MainLoopBase class. It can monitor // socket descriptors for readability using WSAEventSelect. Non-socket file // descriptors are not supported. @@ -33,15 +35,15 @@ class MainLoopWindows : public MainLoopBase { class IOEvent { public: - IOEvent(IOObject::WaitableHandle event) : m_event(event) {} + IOEvent(handle_t event) : m_event(event) {} virtual ~IOEvent() {} virtual void WillPoll() {} virtual void DidPoll() {} virtual void Disarm() {} - IOObject::WaitableHandle GetHandle() { return m_event; } + handle_t GetHandle() { return m_event; } protected: - IOObject::WaitableHandle m_event; + handle_t m_event; }; using IOEventUP = std::unique_ptr; diff --git a/lldb/include/lldb/lldb-types.h b/lldb/include/lldb/lldb-types.h index 15781ad626efb..4c07e5abc4424 100644 --- a/lldb/include/lldb/lldb-types.h +++ b/lldb/include/lldb/lldb-types.h @@ -42,12 +42,15 @@ typedef void *rwlock_t; typedef void *process_t; // Process type is HANDLE typedef void *thread_t; // Host thread type typedef void *file_t; // Host file type -typedef unsigned int __w64 socket_t; // Host socket type +typedef uintptr_t socket_t; // Host socket type typedef void *thread_arg_t; // Host thread argument type typedef unsigned thread_result_t; // Host thread result type typedef thread_result_t (*thread_func_t)(void *); // Host thread function type typedef void *pipe_t; // Host pipe type is HANDLE +// printf macro for file_t +#define PRIuFT PRIuPTR + #else #include @@ -63,6 +66,9 @@ typedef void *thread_result_t; // Host thread result type typedef void *(*thread_func_t)(void *); // Host thread function type typedef int pipe_t; // Host pipe type +// printf macro for file_t +#define PRIuFT PRIi32 + #endif // _WIN32 #define LLDB_INVALID_PROCESS ((lldb::process_t)-1) diff --git a/lldb/source/Host/common/JSONTransport.cpp b/lldb/source/Host/common/JSONTransport.cpp index bf269ffa45966..546c12c8f7114 100644 --- a/lldb/source/Host/common/JSONTransport.cpp +++ b/lldb/source/Host/common/JSONTransport.cpp @@ -42,7 +42,8 @@ ReadFull(IOObject &descriptor, size_t length, if (timeout && timeout_supported) { SelectHelper sh; sh.SetTimeout(*timeout); - sh.FDSetRead((lldb::socket_t)descriptor.GetWaitableHandle()); + sh.FDSetRead( + reinterpret_cast(descriptor.GetWaitableHandle())); Status status = sh.Select(); if (status.Fail()) { // Convert timeouts into a specific error. diff --git a/lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp b/lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp index 44a3ed2e59d5f..83eb0c56853b3 100644 --- a/lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp +++ b/lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp @@ -273,10 +273,10 @@ size_t ConnectionFileDescriptor::Read(void *dst, size_t dst_len, if (log) { LLDB_LOGF(log, - "%p ConnectionFileDescriptor::Read() fd = %" PRIu64 + "%p ConnectionFileDescriptor::Read() fd = %" PRIuFT ", dst = %p, dst_len = %" PRIu64 ") => %" PRIu64 ", error = %s", static_cast(this), - static_cast(m_io_sp->GetWaitableHandle()), + static_cast(m_io_sp->GetWaitableHandle()), static_cast(dst), static_cast(dst_len), static_cast(bytes_read), error.AsCString()); } @@ -377,10 +377,10 @@ size_t ConnectionFileDescriptor::Write(const void *src, size_t src_len, if (log) { LLDB_LOGF(log, - "%p ConnectionFileDescriptor::Write(fd = %" PRIu64 + "%p ConnectionFileDescriptor::Write(fd = %" PRIuFT ", src = %p, src_len = %" PRIu64 ") => %" PRIu64 " (error = %s)", static_cast(this), - static_cast(m_io_sp->GetWaitableHandle()), + static_cast(m_io_sp->GetWaitableHandle()), static_cast(src), static_cast(src_len), static_cast(bytes_sent), error.AsCString()); } @@ -452,7 +452,7 @@ ConnectionFileDescriptor::BytesAvailable(const Timeout &timeout, select_helper.SetTimeout(*timeout); // FIXME: Migrate to MainLoop. - select_helper.FDSetRead((lldb::socket_t)handle); + select_helper.FDSetRead(reinterpret_cast(handle)); #if defined(_WIN32) // select() won't accept pipes on Windows. The entire Windows codepath // needs to be converted over to using WaitForMultipleObjects and event diff --git a/lldb/source/Host/windows/MainLoopWindows.cpp b/lldb/source/Host/windows/MainLoopWindows.cpp index abb4345b011e4..91a8bc805c6b4 100644 --- a/lldb/source/Host/windows/MainLoopWindows.cpp +++ b/lldb/source/Host/windows/MainLoopWindows.cpp @@ -9,6 +9,7 @@ #include "lldb/Host/windows/MainLoopWindows.h" #include "lldb/Host/Config.h" #include "lldb/Host/Socket.h" +#include "lldb/Host/windows/windows.h" #include "lldb/Utility/Status.h" #include "llvm/Config/llvm-config.h" #include "llvm/Support/Casting.h" @@ -18,6 +19,8 @@ #include #include #include +#include +#include #include #include @@ -39,9 +42,8 @@ namespace { class PipeEvent : public MainLoopWindows::IOEvent { public: explicit PipeEvent(HANDLE handle) - : IOEvent((IOObject::WaitableHandle)CreateEventW( - NULL, /*bManualReset=*/FALSE, - /*bInitialState=*/FALSE, NULL)), + : IOEvent(CreateEventW(NULL, /*bManualReset=*/FALSE, + /*bInitialState=*/FALSE, NULL)), m_handle(handle), m_ready(CreateEventW(NULL, /*bManualReset=*/FALSE, /*bInitialState=*/FALSE, NULL)) { assert(m_event && m_ready); @@ -53,12 +55,12 @@ class PipeEvent : public MainLoopWindows::IOEvent { SetEvent(m_ready); // Keep trying to cancel ReadFile() until the thread exits. do { - CancelIoEx((HANDLE)m_handle, /*lpOverlapped=*/NULL); + CancelIoEx(m_handle, /*lpOverlapped=*/NULL); } while (WaitForSingleObject(m_monitor_thread.native_handle(), 1) == WAIT_TIMEOUT); m_monitor_thread.join(); } - CloseHandle((HANDLE)m_event); + CloseHandle(m_event); CloseHandle(m_ready); } @@ -107,7 +109,7 @@ class PipeEvent : public MainLoopWindows::IOEvent { continue; } - SetEvent((HANDLE)m_event); + SetEvent(m_event); // Wait until the current read is consumed before doing the next read. WaitForSingleObject(m_ready, INFINITE); @@ -124,15 +126,15 @@ class PipeEvent : public MainLoopWindows::IOEvent { class SocketEvent : public MainLoopWindows::IOEvent { public: explicit SocketEvent(SOCKET socket) - : IOEvent((IOObject::WaitableHandle)WSACreateEvent()), m_socket(socket) { + : IOEvent(WSACreateEvent()), m_socket(socket) { assert(m_event != WSA_INVALID_EVENT); } - ~SocketEvent() override { WSACloseEvent((HANDLE)m_event); } + ~SocketEvent() override { WSACloseEvent(m_event); } void WillPoll() { - int result = WSAEventSelect(m_socket, (HANDLE)m_event, - FD_READ | FD_ACCEPT | FD_CLOSE); + int result = + WSAEventSelect(m_socket, m_event, FD_READ | FD_ACCEPT | FD_CLOSE); assert(result == 0); UNUSED_IF_ASSERT_DISABLED(result); } @@ -143,7 +145,7 @@ class SocketEvent : public MainLoopWindows::IOEvent { UNUSED_IF_ASSERT_DISABLED(result); } - void Disarm() override { WSAResetEvent((HANDLE)m_event); } + void Disarm() override { WSAResetEvent(m_event); } SOCKET m_socket; }; @@ -167,7 +169,7 @@ llvm::Expected MainLoopWindows::Poll() { events.reserve(m_read_fds.size() + 1); for (auto &[_, fd_info] : m_read_fds) { fd_info.event->WillPoll(); - events.push_back((HANDLE)fd_info.event->GetHandle()); + events.push_back(fd_info.event->GetHandle()); } events.push_back(m_interrupt_event); @@ -206,16 +208,21 @@ MainLoopWindows::RegisterReadObject(const IOObjectSP &object_sp, return nullptr; } - if (object_sp->GetFdType() == IOObject::eFDTypeSocket) + if (object_sp->GetFdType() == IOObject::eFDTypeSocket) { m_read_fds[waitable_handle] = { - std::make_unique((SOCKET)waitable_handle), callback}; - else if (GetFileType(waitable_handle) == FILE_TYPE_PIPE) - m_read_fds[waitable_handle] = { - std::make_unique((HANDLE)waitable_handle), callback}; - else { - error = Status::FromErrorStringWithFormat("Unsupported file type %d", - GetFileType(waitable_handle)); - return nullptr; + std::make_unique( + reinterpret_cast(waitable_handle)), + callback}; + } else { + DWORD file_type = GetFileType(waitable_handle); + if (file_type != FILE_TYPE_PIPE) { + error = Status::FromErrorStringWithFormat("Unsupported file type %d", + file_type); + return nullptr; + } + + m_read_fds[waitable_handle] = {std::make_unique(waitable_handle), + callback}; } return CreateReadHandle(object_sp); diff --git a/lldb/source/Utility/SelectHelper.cpp b/lldb/source/Utility/SelectHelper.cpp index 34c1b62228fdc..3044cdae110ed 100644 --- a/lldb/source/Utility/SelectHelper.cpp +++ b/lldb/source/Utility/SelectHelper.cpp @@ -177,7 +177,7 @@ lldb_private::Status SelectHelper::Select() { #endif // Set the FD bits in the fdsets for read/write/error for (auto &pair : m_fd_map) { - const lldb::socket_t fd = pair.first; + lldb::socket_t fd = pair.first; if (pair.second.read_set) FD_SET(fd, read_fdset_ptr);