fjricci updated this revision to Diff 47681.
fjricci added a comment.

Refactor to use listen urls


http://reviews.llvm.org/D17099

Files:
  source/Interpreter/CommandInterpreter.cpp
  source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
  source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h
  source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp

Index: source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
===================================================================
--- source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
+++ source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
@@ -723,10 +723,28 @@
     if (error.Fail())
         return error;
 
-    error = ConnectToDebugserver (remote_url);
+    if (strncmp(remote_url, "listen", strlen("listen")) == 0)
+    {
+        error = m_gdb_comm.WaitForDebugserver(remote_url);
+        if (error.Fail())
+            return error;
 
-    if (error.Fail())
+        error = ConnectToDebugserver(nullptr);
+        if (error.Fail())
+            return error;
+    }
+    else if (strncmp(remote_url, "connect", strlen("connect")) == 0)
+    {
+        error = ConnectToDebugserver(remote_url);
+        if (error.Fail())
+            return error;
+    }
+    else
+    {
+        error.SetErrorStringWithFormat ("Invalid connection url: %s", remote_url);
         return error;
+    }
+
     StartAsyncThread ();
 
     lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID ();
Index: source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h
===================================================================
--- source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h
+++ source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h
@@ -161,6 +161,9 @@
         return m_packet_timeout * TimeValue::MicroSecPerSec;
     }
 
+    Error
+    WaitForDebugserver(const char *url);
+
     //------------------------------------------------------------------
     // Start a debugserver instance on the current host using the
     // supplied connection URL.
@@ -339,7 +342,7 @@
     DecompressPacket ();
 
     Error
-    StartListenThread (const char *hostname = "127.0.0.1", uint16_t port = 0);
+    StartListenThread(const char *listen_url = "listen://127.0.0.1:0");
 
     bool
     JoinListenThread ();
Index: source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
===================================================================
--- source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
+++ source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
@@ -1066,20 +1066,15 @@
 }
 
 Error
-GDBRemoteCommunication::StartListenThread (const char *hostname, uint16_t port)
+GDBRemoteCommunication::StartListenThread(const char *listen_url)
 {
     Error error;
     if (m_listen_thread.IsJoinable())
     {
         error.SetErrorString("listen thread already running");
     }
     else
     {
-        char listen_url[512];
-        if (hostname && hostname[0])
-            snprintf(listen_url, sizeof(listen_url), "listen://%s:%i", hostname, port);
-        else
-            snprintf(listen_url, sizeof(listen_url), "listen://%i", port);
         m_listen_url = listen_url;
         SetConnection(new ConnectionFileDescriptor());
         m_listen_thread = ThreadLauncher::LaunchThread(listen_url, GDBRemoteCommunication::ListenThread, this, &error);
@@ -1112,11 +1107,37 @@
 }
 
 Error
-GDBRemoteCommunication::StartDebugserverProcess (const char *url,
-                                                 Platform *platform,
-                                                 ProcessLaunchInfo &launch_info,
-                                                 uint16_t *port,
-                                                 const Args& inferior_args)
+GDBRemoteCommunication::WaitForDebugserver(const char *url)
+{
+    Error error;
+    Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
+    if (log)
+        log->Printf("GDBRemoteCommunication::%s(url=%s)", __FUNCTION__, url ? url : "<empty>");
+
+    error = StartListenThread(url);
+    if (error.Fail())
+    {
+        if (log)
+            log->Printf("GDBRemoteCommunication::%s() unable to start listen thread: %s", __FUNCTION__,
+                        error.AsCString());
+        return error;
+    }
+
+    // Make sure we actually connect with the debugserver...
+    JoinListenThread();
+
+    if (error.Fail())
+    {
+        if (log)
+            log->Printf("GDBRemoteCommunication::%s() failed: %s", __FUNCTION__, error.AsCString());
+    }
+
+    return error;
+}
+
+Error
+GDBRemoteCommunication::StartDebugserverProcess(const char *url, Platform *platform, ProcessLaunchInfo &launch_info,
+                                                uint16_t *port, const Args &inferior_args)
 {
     Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
     if (log)
@@ -1258,7 +1279,7 @@
             {
                 // No host and port given, so lets listen on our end and make the debugserver
                 // connect to us..
-                error = StartListenThread ("127.0.0.1", 0);
+                error = StartListenThread("listen://127.0.0.1:0");
                 if (error.Fail())
                 {
                     if (log)
Index: source/Interpreter/CommandInterpreter.cpp
===================================================================
--- source/Interpreter/CommandInterpreter.cpp
+++ source/Interpreter/CommandInterpreter.cpp
@@ -624,18 +624,21 @@
         }
     }
 
-    std::unique_ptr<CommandObjectRegexCommand>
-    connect_gdb_remote_cmd_ap(new CommandObjectRegexCommand (*this,
-                                                             "gdb-remote",
-                                                             "Connect to a remote GDB server.  If no hostname is provided, localhost is assumed.",
-                                                             "gdb-remote [<hostname>:]<portnum>",
-                                                             2,
-                                                             0,
-                                                             false));
+    std::unique_ptr<CommandObjectRegexCommand> connect_gdb_remote_cmd_ap(
+        new CommandObjectRegexCommand(*this, "gdb-remote", "Connect to a remote GDB server.  If no "
+                                                           "hostname is provided, localhost is assumed.",
+                                      "gdb-remote [<hostname>:]<portnum> [reverse]", 2, 0, false));
     if (connect_gdb_remote_cmd_ap.get())
     {
-        if (connect_gdb_remote_cmd_ap->AddRegexCommand("^([^:]+:[[:digit:]]+)$", "process connect --plugin gdb-remote connect://%1") &&
-            connect_gdb_remote_cmd_ap->AddRegexCommand("^([[:digit:]]+)$", "process connect --plugin gdb-remote connect://localhost:%1"))
+        if (connect_gdb_remote_cmd_ap->AddRegexCommand("^([^:]+:[[:digit:]]+)$",
+                                                       "process connect --plugin gdb-remote connect://%1") &&
+            connect_gdb_remote_cmd_ap->AddRegexCommand("^([[:digit:]]+)$",
+                                                       "process connect --plugin gdb-remote connect://localhost:%1") &&
+            connect_gdb_remote_cmd_ap->AddRegexCommand("^([^:]+:[[:digit:]]+) reverse$",
+                                                       "process connect --plugin gdb-remote listen://%1") &&
+            connect_gdb_remote_cmd_ap->AddRegexCommand("^([[:digit:]]+) reverse$",
+                                                       "process connect --plugin gdb-remote "
+                                                       "listen://localhost:%1"))
         {
             CommandObjectSP command_sp(connect_gdb_remote_cmd_ap.release());
             m_command_dict[command_sp->GetCommandName ()] = command_sp;
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to