Modify "platform connect" to connect to processes as well

The standard remote debugging workflow with gdb is to start the
application on the remote host under gdbserver (e.g.: gdbserver :5039
a.out) and then connect to it with gdb.

The same workflow is supported by debugserver/lldb-gdbserver with a very
similar syntax but to access all features of lldb we need to be
connected also to an lldb-platform instance running on the target.

Before this change this had to be done manually with starting a separate
lldb-platform on the target machine and then connecting to it with lldb
before connecting to the process.

This change modifies the behavior of "platform connect" with
automatically connecting to the process instance if it was started by
the remote platform. With this command replacing gdbserver in a gdb
based worflow is usually as simple as replacing the command to execute
gdbserver with executing lldb-platform.

Differential revision: http://reviews.llvm.org/D14952

llvm-svn: 255016
diff --git a/lldb/tools/lldb-server/lldb-platform.cpp b/lldb/tools/lldb-server/lldb-platform.cpp
index 8fdc472..3292080 100644
--- a/lldb/tools/lldb-server/lldb-platform.cpp
+++ b/lldb/tools/lldb-server/lldb-platform.cpp
@@ -285,6 +285,12 @@
         exit(option_error);
     }
 
+    // Skip any options we consumed with getopt_long_only.
+    argc -= optind;
+    argv += optind;
+    lldb_private::Args inferior_arguments;
+    inferior_arguments.SetArguments(argc, const_cast<const char**>(argv));
+
     const bool children_inherit_listen_socket = false;
     // the test suite makes many connections in parallel, let's not miss any.
     // The highest this should get reasonably is a function of the number
@@ -309,7 +315,7 @@
         error = save_socket_id_to_file(acceptor_up->GetLocalSocketId(), socket_file);
         if (error.Fail())
         {
-            fprintf(stderr, "failed to write socket id to %s: %s", socket_file.GetPath().c_str(), error.AsCString());
+            fprintf(stderr, "failed to write socket id to %s: %s\n", socket_file.GetPath().c_str(), error.AsCString());
             return 1;
         }
     }
@@ -317,7 +323,7 @@
     do {
         GDBRemoteCommunicationServerPlatform platform(acceptor_up->GetSocketProtocol(),
                                                       acceptor_up->GetSocketScheme());
-        
+
         if (port_offset > 0)
             platform.SetPortOffset(port_offset);
 
@@ -365,6 +371,22 @@
 
         if (platform.IsConnected())
         {
+            if (inferior_arguments.GetArgumentCount() > 0)
+            {
+                lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
+                uint16_t port = 0;
+                std::string socket_name;
+                Error error = platform.LaunchGDBServer(inferior_arguments,
+                                                       "", // hostname
+                                                       pid,
+                                                       port,
+                                                       socket_name);
+                if (error.Success())
+                    platform.SetPendingGdbServer(pid, port, socket_name);
+                else
+                    fprintf(stderr, "failed to start gdbserver: %s\n", error.AsCString());
+            }
+
             // After we connected, we need to get an initial ack from...
             if (platform.HandshakeWithClient())
             {