[Lldb-commits] [lldb] 62293e7 - Revert "[lldb] [lldb-server] Refactor ConnectToRemote()"

Michał Górny via lldb-commits lldb-commits at lists.llvm.org
Mon Oct 18 13:05:45 PDT 2021


Author: Michał Górny
Date: 2021-10-18T22:04:36+02:00
New Revision: 62293e7d33ff0344b767ac66188981774c8b5197

URL: https://github.com/llvm/llvm-project/commit/62293e7d33ff0344b767ac66188981774c8b5197
DIFF: https://github.com/llvm/llvm-project/commit/62293e7d33ff0344b767ac66188981774c8b5197.diff

LOG: Revert "[lldb] [lldb-server] Refactor ConnectToRemote()"

This reverts commit d8bc7e40ce1cdd8c1a3fac7937ce1ea85c262728.  I've
linked the wrong Differential Revision.

Added: 
    

Modified: 
    lldb/tools/lldb-server/lldb-gdbserver.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/tools/lldb-server/lldb-gdbserver.cpp b/lldb/tools/lldb-server/lldb-gdbserver.cpp
index dce7905e55c75..9e599963c5b72 100644
--- a/lldb/tools/lldb-server/lldb-gdbserver.cpp
+++ b/lldb/tools/lldb-server/lldb-gdbserver.cpp
@@ -199,7 +199,8 @@ void ConnectToRemote(MainLoop &mainloop,
   std::unique_ptr<Connection> connection_up;
   if (connection_fd != -1) {
     // Build the connection string.
-    std::string connection_url = llvm::formatv("fd://{0}", connection_fd).str();
+    char connection_url[512];
+    snprintf(connection_url, sizeof(connection_url), "fd://%d", connection_fd);
 
     // Create the connection.
 #if LLDB_ENABLE_POSIX && !defined _WIN32
@@ -207,22 +208,23 @@ void ConnectToRemote(MainLoop &mainloop,
 #endif
     connection_up.reset(new ConnectionFileDescriptor);
     auto connection_result = connection_up->Connect(connection_url, &error);
-    if (error.Fail()) {
-      llvm::errs() << llvm::formatv(
-          "error: failed to connect to client at '{0}': {1}\n", connection_url,
-          error);
+    if (connection_result != eConnectionStatusSuccess) {
+      fprintf(stderr, "error: failed to connect to client at '%s' "
+                      "(connection status: %d)\n",
+              connection_url, static_cast<int>(connection_result));
       exit(-1);
     }
-    if (connection_result != eConnectionStatusSuccess) {
-      llvm::errs() << llvm::formatv(
-          "error: failed to connect to client at '{0}' "
-          "(connection status: {1})\n",
-          connection_url, static_cast<int>(connection_result));
+    if (error.Fail()) {
+      fprintf(stderr, "error: failed to connect to client at '%s': %s\n",
+              connection_url, error.AsCString());
       exit(-1);
     }
   } else if (!host_and_port.empty()) {
     // Parse out host and port.
     std::string final_host_and_port;
+    std::string connection_host;
+    std::string connection_port;
+    uint32_t connection_portno = 0;
 
     // If host_and_port starts with ':', default the host to be "localhost" and
     // expect the remainder to be the port.
@@ -230,53 +232,55 @@ void ConnectToRemote(MainLoop &mainloop,
       final_host_and_port.append("localhost");
     final_host_and_port.append(host_and_port.str());
 
+    // Note: use rfind, because the host/port may look like "[::1]:12345".
+    const std::string::size_type colon_pos = final_host_and_port.rfind(':');
+    if (colon_pos != std::string::npos) {
+      connection_host = final_host_and_port.substr(0, colon_pos);
+      connection_port = final_host_and_port.substr(colon_pos + 1);
+      // FIXME: improve error handling
+      llvm::to_integer(connection_port, connection_portno);
+    }
+
+
     if (reverse_connect) {
       // llgs will connect to the gdb-remote client.
 
       // Ensure we have a port number for the connection.
-      // Note: use rfind, because the host/port may look like "[::1]:12345".
-      uint32_t connection_portno = 0;
-      const std::string::size_type colon_pos = final_host_and_port.rfind(':');
-      if (colon_pos != std::string::npos)
-        llvm::to_integer(final_host_and_port.substr(colon_pos + 1),
-                         connection_portno);
       if (connection_portno == 0) {
-        llvm::errs() << "error: port number must be specified on when using "
-                        "reverse connect\n";
+        fprintf(stderr, "error: port number must be specified on when using "
+                        "reverse connect\n");
         exit(1);
       }
 
       // Build the connection string.
-      final_host_and_port.insert(0, "connect://");
+      char connection_url[512];
+      snprintf(connection_url, sizeof(connection_url), "connect://%s",
+               final_host_and_port.c_str());
 
       // Create the connection.
       connection_up.reset(new ConnectionFileDescriptor);
-      auto connection_result =
-          connection_up->Connect(final_host_and_port, &error);
-      if (error.Fail()) {
-        llvm::errs() << llvm::formatv(
-            "error: failed to connect to client at '{0}': {1}\n",
-            final_host_and_port, error);
+      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)\n",
+                connection_url, static_cast<int>(connection_result));
         exit(-1);
       }
-      if (connection_result != eConnectionStatusSuccess) {
-        llvm::errs() << llvm::formatv(
-            "error: failed to connect to client at '{0}' "
-            "(connection status: {1})\n",
-            final_host_and_port, static_cast<int>(connection_result));
+      if (error.Fail()) {
+        fprintf(stderr, "error: failed to connect to client at '%s': %s\n",
+                connection_url, error.AsCString());
         exit(-1);
       }
     } else {
       std::unique_ptr<Acceptor> acceptor_up(
           Acceptor::Create(final_host_and_port, false, error));
       if (error.Fail()) {
-        llvm::errs() << llvm::formatv("failed to create acceptor: {0}\n",
-                                      error);
+        fprintf(stderr, "failed to create acceptor: %s\n", error.AsCString());
         exit(1);
       }
       error = acceptor_up->Listen(1);
       if (error.Fail()) {
-        llvm::errs() << llvm::formatv("failed to listen: {0}\n", error);
+        fprintf(stderr, "failed to listen: %s\n", error.AsCString());
         exit(1);
       }
       const std::string socket_id = acceptor_up->GetLocalSocketId();
@@ -285,28 +289,26 @@ void ConnectToRemote(MainLoop &mainloop,
         if (named_pipe_path && named_pipe_path[0]) {
           error = writeSocketIdToPipe(named_pipe_path, socket_id);
           if (error.Fail())
-            llvm::errs() << llvm::formatv(
-                "failed to write to the named peipe '{0}': {1}\n",
-                named_pipe_path, error.AsCString());
+            fprintf(stderr, "failed to write to the named pipe \'%s\': %s\n",
+                    named_pipe_path, error.AsCString());
         }
         // If we have an unnamed pipe to write the socket id back to, do that
         // now.
         else if (unnamed_pipe != LLDB_INVALID_PIPE) {
           error = writeSocketIdToPipe(unnamed_pipe, socket_id);
           if (error.Fail())
-            llvm::errs() << llvm::formatv(
-                "failed to write to the unnamed pipe: {0}\n", error);
+            fprintf(stderr, "failed to write to the unnamed pipe: %s\n",
+                    error.AsCString());
         }
       } else {
-        llvm::errs()
-            << "unable to get the socket id for the listening connection\n";
+        fprintf(stderr,
+                "unable to get the socket id for the listening connection\n");
       }
 
       Connection *conn = nullptr;
       error = acceptor_up->Accept(false, conn);
       if (error.Fail()) {
-        llvm::errs() << llvm::formatv("failed to accept new connection: {0}\n",
-                                      error);
+        printf("failed to accept new connection: %s\n", error.AsCString());
         exit(1);
       }
       connection_up.reset(conn);
@@ -314,10 +316,11 @@ void ConnectToRemote(MainLoop &mainloop,
   }
   error = gdb_server.InitializeConnection(std::move(connection_up));
   if (error.Fail()) {
-    llvm::errs() << llvm::formatv("failed to initialize connection\n", error);
+    fprintf(stderr, "Failed to initialize connection: %s\n",
+            error.AsCString());
     exit(-1);
   }
-  llvm::outs() << "Connection established.\n";
+  printf("Connection established.\n");
 }
 
 namespace {


        


More information about the lldb-commits mailing list