[Lldb-commits] [lldb] [lldb] Updated lldb-server to spawn the child process and share socket (PR #101283)

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Wed Aug 7 05:57:57 PDT 2024


================
@@ -114,6 +126,175 @@ static Status save_socket_id_to_file(const std::string &socket_id,
   return status;
 }
 
+static void client_handle(GDBRemoteCommunicationServerPlatform &platform,
+                          const lldb_private::Args &args) {
+  if (!platform.IsConnected())
+    return;
+
+  if (args.GetArgumentCount() > 0) {
+    lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
+    std::optional<uint16_t> port;
+    std::string socket_name;
+    Status error = platform.LaunchGDBServer(args,
+                                            "", // 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());
+  }
+
+  bool interrupt = false;
+  bool done = false;
+  Status error;
+  while (!interrupt && !done) {
+    if (platform.GetPacketAndSendResponse(std::nullopt, error, interrupt,
+                                          done) !=
+        GDBRemoteCommunication::PacketResult::Success)
+      break;
+  }
+
+  printf("Disconnected.\n");
+}
+
+static GDBRemoteCommunicationServerPlatform::PortMap gdbserver_portmap;
+static std::mutex gdbserver_portmap_mutex;
+
+static void spawn_process_reaped(lldb::pid_t pid, int signal, int status) {
+  std::lock_guard<std::mutex> guard(gdbserver_portmap_mutex);
+  gdbserver_portmap.FreePortForProcess(pid);
+}
+
+static Status spawn_process(const char *progname, Connection *conn,
+                            uint16_t gdb_port, uint16_t port_offset,
+                            const lldb_private::Args &args,
+                            const std::string &log_file,
+                            const StringRef log_channels) {
+  Status error;
+  const TCPSocket &tcpSocket =
+      static_cast<const TCPSocket &>(*conn->GetReadObject());
+  NativeSocket socket = tcpSocket.GetNativeSocket();
+
+  ProcessLaunchInfo launch_info;
+
+  fd_t fd;
+#ifdef _WIN32
----------------
labath wrote:

I think these two ifdefs in this function could be extracted to a separate class. I'm imagining something with a constructor which takes the `Connection` object (maybe also the ProcessLaunchInfo), and two methods that:
- return the number that should be passed to the subprocess via arguments (the unix version will just pass the connection fd). `GetSendableFD` ?
- finish the sending after the new process is started (unix version would be a noop). `CompleteSending` ?

https://github.com/llvm/llvm-project/pull/101283


More information about the lldb-commits mailing list