Revert to getting a random port and sending that down to debugserver for iOS. The sandboxing is not letting debugserver reverse connect back to lldb.

<rdar://problem/15789865>

llvm-svn: 198963
diff --git a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
index 877205a..20b1495 100644
--- a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
@@ -167,6 +167,34 @@
     
 } // anonymous namespace end
 
+static bool rand_initialized = false;
+
+// TODO Randomly assigning a port is unsafe.  We should get an unused
+// ephemeral port from the kernel and make sure we reserve it before passing
+// it to debugserver.
+
+#if defined (__APPLE__)
+#define LOW_PORT    (IPPORT_RESERVED)
+#define HIGH_PORT   (IPPORT_HIFIRSTAUTO)
+#else
+#define LOW_PORT    (1024u)
+#define HIGH_PORT   (49151u)
+#endif
+
+static inline uint16_t
+get_random_port ()
+{
+    if (!rand_initialized)
+    {
+        time_t seed = time(NULL);
+        
+        rand_initialized = true;
+        srand(seed);
+    }
+    return (rand() % (HIGH_PORT - LOW_PORT)) + LOW_PORT;
+}
+
+
 lldb_private::ConstString
 ProcessGDBRemote::GetPluginNameStatic()
 {
@@ -2514,7 +2542,6 @@
 ProcessGDBRemote::LaunchAndConnectToDebugserver (const ProcessInfo &process_info)
 {
     Error error;
-    uint16_t port = 0;
     if (m_debugserver_pid == LLDB_INVALID_PROCESS_ID)
     {
         // If we locate debugserver, keep that located version around
@@ -2524,7 +2551,20 @@
         debugserver_launch_info.SetMonitorProcessCallback (MonitorDebugserverProcess, this, false);
         debugserver_launch_info.SetUserID(process_info.GetUserID());
 
-        error = m_gdb_comm.StartDebugserverProcess (NULL,
+#if defined (__APPLE__) && defined (__arm__)
+        // On iOS, still do a local connection using a random port
+        const char *hostname = "localhost";
+        uint16_t port = get_random_port ();
+#else
+        // Set hostname being NULL to do the reverse connect where debugserver
+        // will bind to port zero and it will communicate back to us the port
+        // that we will connect to
+        const char *hostname = NULL;
+        uint16_t port = 0;
+#endif
+
+        error = m_gdb_comm.StartDebugserverProcess (hostname,
+                                                    port,
                                                     debugserver_launch_info,
                                                     port);
 
@@ -2552,9 +2592,9 @@
         }
         else
         {
-            char connect_url[128];
-            snprintf (connect_url, sizeof(connect_url), "connect://localhost:%u", port);
-            error = ConnectToDebugserver (connect_url);
+            StreamString connect_url;
+            connect_url.Printf("connect://%s:%u", hostname, port);
+            error = ConnectToDebugserver (connect_url.GetString().c_str());
         }
 
     }