DemiMarie updated this revision to Diff 108214.
DemiMarie added a comment.

Actually use socketpair :)


https://reviews.llvm.org/D33213

Files:
  source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
  tools/lldb-server/lldb-gdbserver.cpp

Index: tools/lldb-server/lldb-gdbserver.cpp
===================================================================
--- tools/lldb-server/lldb-gdbserver.cpp
+++ tools/lldb-server/lldb-gdbserver.cpp
@@ -106,6 +106,7 @@
            // than llgs listening for a connection from address on port.
     {"setsid", no_argument, NULL,
      'S'}, // Call setsid() to make llgs run in its own session.
+    {"fd", required_argument, NULL, 'F'},
     {NULL, 0, NULL, 0}};
 
 //----------------------------------------------------------------------
@@ -132,13 +133,13 @@
                   "[--log-file log-file-name] "
                   "[--log-channels log-channel-list] "
                   "[--setsid] "
+                  "[--fd file-descriptor]"
                   "[--named-pipe named-pipe-path] "
                   "[--native-regs] "
                   "[--attach pid] "
                   "[[HOST]:PORT] "
                   "[-- PROGRAM ARG1 ARG2 ...]\n",
           progname, subcommand);
-  exit(0);
 }
 
 void handle_attach_to_pid(GDBRemoteCommunicationServerLLGS &gdb_server,
@@ -232,10 +233,31 @@
                      GDBRemoteCommunicationServerLLGS &gdb_server,
                      bool reverse_connect, const char *const host_and_port,
                      const char *const progname, const char *const subcommand,
-                     const char *const named_pipe_path, int unnamed_pipe_fd) {
+                     const char *const named_pipe_path, int unnamed_pipe_fd,
+                     int connection_fd) {
   Status error;
 
-  if (host_and_port && host_and_port[0]) {
+  std::unique_ptr<Connection> connection_up;
+  if (connection_fd != -1) {
+    // Build the connection string.
+    char connection_url[512];
+    snprintf(connection_url, sizeof(connection_url), "fd://%d", connection_fd);
+
+    // Create the connection.
+    connection_up.reset(new ConnectionFileDescriptor);
+    auto connection_result = connection_up->Connect(connection_url, &error);
+    if (connection_result != eConnectionStatusSuccess) {
+      fprintf(stderr, "error: failed to connect to client at '%s' "
+                      "(connection status: %d)",
+              connection_url, static_cast<int>(connection_result));
+      exit(-1);
+    }
+    if (error.Fail()) {
+      fprintf(stderr, "error: failed to connect to client at '%s': %s",
+              connection_url, error.AsCString());
+      exit(-1);
+    }
+  } else if (host_and_port && host_and_port[0]) {
     // Parse out host and port.
     std::string final_host_and_port;
     std::string connection_host;
@@ -255,7 +277,6 @@
       connection_portno = StringConvert::ToUInt32(connection_port.c_str(), 0);
     }
 
-    std::unique_ptr<Connection> connection_up;
 
     if (reverse_connect) {
       // llgs will connect to the gdb-remote client.
@@ -328,14 +349,14 @@
       }
       connection_up.reset(conn);
     }
-    error = gdb_server.InitializeConnection(std::move(connection_up));
-    if (error.Fail()) {
-      fprintf(stderr, "Failed to initialize connection: %s\n",
-              error.AsCString());
-      exit(-1);
-    }
-    printf("Connection established.\n");
   }
+  error = gdb_server.InitializeConnection(std::move(connection_up));
+  if (error.Fail()) {
+    fprintf(stderr, "Failed to initialize connection: %s\n",
+            error.AsCString());
+    exit(-1);
+  }
+  printf("Connection established.\n");
 }
 
 //----------------------------------------------------------------------
@@ -364,6 +385,7 @@
       log_channels; // e.g. "lldb process threads:gdb-remote default:linux all"
   int unnamed_pipe_fd = -1;
   bool reverse_connect = false;
+  int connection_fd = -1;
 
   // ProcessLaunchInfo launch_info;
   ProcessAttachInfo attach_info;
@@ -413,6 +435,10 @@
       reverse_connect = true;
       break;
 
+    case 'F':
+      connection_fd = StringConvert::ToUInt32(optarg, -1);
+      break;
+
 #ifndef _WIN32
     case 'S':
       // Put llgs into a new session. Terminals group processes
@@ -472,7 +498,8 @@
   argc -= optind;
   argv += optind;
 
-  if (argc == 0) {
+  if (argc == 0 && connection_fd == -1) {
+    fputs("No arguments\n", stderr);
     display_usage(progname, subcommand);
     exit(255);
   }
@@ -501,7 +528,7 @@
 
   ConnectToRemote(mainloop, gdb_server, reverse_connect, host_and_port,
                   progname, subcommand, named_pipe_path.c_str(),
-                  unnamed_pipe_fd);
+                  unnamed_pipe_fd, connection_fd);
 
   if (!gdb_server.IsConnected()) {
     fprintf(stderr, "no connection information provided, unable to run\n");
Index: source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
===================================================================
--- source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
+++ source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
@@ -3289,7 +3289,7 @@
   }
   return error;
 }
-#if defined(__APPLE__)
+#if !defined(_WIN32)
 #define USE_SOCKETPAIR_FOR_LOCAL_CONNECTION 1
 #endif
 
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to