2424#include " lldb/Host/ConnectionFileDescriptor.h"
2525#include " lldb/Host/FileSystem.h"
2626#include " lldb/Host/Pipe.h"
27- #include " lldb/Host/Socket.h"
2827#include " lldb/Host/common/NativeProcessProtocol.h"
28+ #include " lldb/Host/common/TCPSocket.h"
2929#include " lldb/Target/Process.h"
3030#include " lldb/Utility/LLDBLog.h"
3131#include " lldb/Utility/Status.h"
@@ -195,18 +195,31 @@ void ConnectToRemote(MainLoop &mainloop,
195195 bool reverse_connect, llvm::StringRef host_and_port,
196196 const char *const progname, const char *const subcommand,
197197 const char *const named_pipe_path, pipe_t unnamed_pipe,
198- int connection_fd) {
198+ shared_fd_t connection_fd) {
199199 Status error;
200200
201201 std::unique_ptr<Connection> connection_up;
202202 std::string url;
203203
204- if (connection_fd != -1 ) {
204+ if (connection_fd != SharedSocket::kInvalidFD ) {
205+ #ifdef _WIN32
206+ NativeSocket sockfd;
207+ error = SharedSocket::GetNativeSocket (connection_fd, sockfd);
208+ if (error.Fail ()) {
209+ llvm::errs () << llvm::formatv (" error: GetNativeSocket failed: {0}\n " ,
210+ error.AsCString ());
211+ exit (-1 );
212+ }
213+ connection_up =
214+ std::unique_ptr<Connection>(new ConnectionFileDescriptor (new TCPSocket (
215+ sockfd, /* should_close=*/ true , /* child_processes_inherit=*/ false )));
216+ #else
205217 url = llvm::formatv (" fd://{0}" , connection_fd).str ();
206218
207219 // Create the connection.
208- #if LLDB_ENABLE_POSIX && !defined _WIN32
220+ #if LLDB_ENABLE_POSIX
209221 ::fcntl (connection_fd, F_SETFD, FD_CLOEXEC);
222+ #endif
210223#endif
211224 } else if (!host_and_port.empty ()) {
212225 llvm::Expected<std::string> url_exp =
@@ -338,7 +351,7 @@ int main_gdbserver(int argc, char *argv[]) {
338351 log_channels; // e.g. "lldb process threads:gdb-remote default:linux all"
339352 lldb::pipe_t unnamed_pipe = LLDB_INVALID_PIPE;
340353 bool reverse_connect = false ;
341- int connection_fd = - 1 ;
354+ shared_fd_t connection_fd = SharedSocket:: kInvalidFD ;
342355
343356 // ProcessLaunchInfo launch_info;
344357 ProcessAttachInfo attach_info;
@@ -404,10 +417,12 @@ int main_gdbserver(int argc, char *argv[]) {
404417 unnamed_pipe = (pipe_t )Arg;
405418 }
406419 if (Args.hasArg (OPT_fd)) {
407- if (!llvm::to_integer (Args.getLastArgValue (OPT_fd), connection_fd)) {
420+ int64_t fd;
421+ if (!llvm::to_integer (Args.getLastArgValue (OPT_fd), fd)) {
408422 WithColor::error () << " invalid '--fd' argument\n " << HelpText;
409423 return 1 ;
410424 }
425+ connection_fd = (shared_fd_t )fd;
411426 }
412427
413428 if (!LLDBServerUtilities::SetupLogging (
@@ -423,7 +438,7 @@ int main_gdbserver(int argc, char *argv[]) {
423438 for (const char *Val : Arg->getValues ())
424439 Inputs.push_back (Val);
425440 }
426- if (Inputs.empty () && connection_fd == - 1 ) {
441+ if (Inputs.empty () && connection_fd == SharedSocket:: kInvalidFD ) {
427442 WithColor::error () << " no connection arguments\n " << HelpText;
428443 return 1 ;
429444 }
@@ -432,7 +447,7 @@ int main_gdbserver(int argc, char *argv[]) {
432447 GDBRemoteCommunicationServerLLGS gdb_server (mainloop, manager);
433448
434449 llvm::StringRef host_and_port;
435- if (!Inputs.empty ()) {
450+ if (!Inputs.empty () && connection_fd == SharedSocket:: kInvalidFD ) {
436451 host_and_port = Inputs.front ();
437452 Inputs.erase (Inputs.begin ());
438453 }
0 commit comments