Switch local launching of debugserver over to always use a FIFO in order to handshake with the launched debugserver.

This helps ensure that the launched debugserver is ready and listening for a connection. Prior to this we had a race condition.

Consolidate the launching of debugserver into a single place: a static function in GDBRemoteCommunication.

llvm-svn: 196401
diff --git a/lldb/tools/debugserver/source/RNBSocket.cpp b/lldb/tools/debugserver/source/RNBSocket.cpp
index 67dc009..448db257 100644
--- a/lldb/tools/debugserver/source/RNBSocket.cpp
+++ b/lldb/tools/debugserver/source/RNBSocket.cpp
@@ -120,20 +120,6 @@
         return rnb_err;
     }
 
-    if (callback && port == 0)
-    {
-        // We were asked to listen on port zero which means we
-        // must now read the actual port that was given to us 
-        // as port zero is a special code for "find an open port
-        // for me".
-        socklen_t sa_len = sizeof (sa);
-        if (getsockname(listen_fd, (struct sockaddr *)&sa, &sa_len) == 0)
-        {
-            port = ntohs (sa.sin_port);
-            callback (callback_baton, port);
-        }
-    }
-
     error = ::listen (listen_fd, 5);
     if (error == -1)
         err.SetError(errno, DNBError::POSIX);
@@ -146,6 +132,27 @@
         ClosePort (listen_fd, false);
         return rnb_err;
     }
+    
+    if (callback)
+    {
+        // We were asked to listen on port zero which means we
+        // must now read the actual port that was given to us
+        // as port zero is a special code for "find an open port
+        // for me".
+        if (port == 0)
+        {
+            socklen_t sa_len = sizeof (sa);
+            if (getsockname(listen_fd, (struct sockaddr *)&sa, &sa_len) == 0)
+            {
+                port = ntohs (sa.sin_port);
+                callback (callback_baton, port);
+            }
+        }
+        else
+        {
+            callback (callback_baton, port);
+        }
+    }
 
     struct sockaddr_in accept_addr;
     ::memset (&accept_addr, 0, sizeof accept_addr);