[PATCH] Make gdbserver connect to SOCK_STREAM sockets

Gabriel Corona gabriel.corona@enst-bretagne.fr
Sat May 2 22:53:00 GMT 2015


In GDB:

    (gdb) target remote |socat STDIO UNIX-LISTEN:foo.sock

In gdbserver:

   $ gdbserver foo.sock ./foo

Conflicts:
	gdb/gdbserver/remote-utils.c
---
 gdb/gdbserver/remote-utils.c | 86 ++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 84 insertions(+), 2 deletions(-)

diff --git a/gdb/gdbserver/remote-utils.c b/gdb/gdbserver/remote-utils.c
index 1de86be..b1b6c7f 100644
--- a/gdb/gdbserver/remote-utils.c
+++ b/gdb/gdbserver/remote-utils.c
@@ -36,6 +36,9 @@
 #if HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
 #endif
+#if HAVE_SYS_UN_H
+#include <sys/un.h>
+#endif
 #if HAVE_NETDB_H
 #include <netdb.h>
 #endif
@@ -280,6 +283,77 @@ remote_prepare (char *name)
   transport_is_reliable = 1;
 }
 
+#ifndef USE_WIN32API
+static int open_shell_command(char* command)
+{
+  int sockets[2];
+  int res;
+  pid_t child, pid;
+
+  res = socketpair(AF_LOCAL, SOCK_STREAM, 0, sockets);
+  if (res < 0)
+    error ("Could not get socketpair.");
+  child = fork();
+  if (child < 0)
+    {
+      error ("Could not fork.");
+    }
+  else if (child == 0)
+    {
+      if (close (sockets[0]) < 0)
+	exit (1);
+      if (dup2 (sockets[1], 0) < 0 || dup2 (sockets[1], 1) < 0)
+	exit (1);
+      res = fork ();
+      if (res < 0)
+	exit (1);
+      if (res != 0)
+	exit (0);
+      execl ("/bin/sh", "sh", "-c", command, NULL);
+      exit (1);
+    }
+  else
+    {
+      signal (SIGPIPE, SIG_IGN);
+      while ((pid = waitpid (child, NULL, 0)) < 0 && errno == EINTR);
+      if (pid < 0)
+	error ("Could not wait for child.");
+      close (sockets[1]);
+      return sockets[0];
+     }
+  return -1;
+}
+#endif
+
+#ifndef USE_WIN32API
+int socket_open (char* name)
+{
+  int sock;
+  struct sockaddr_un addr;
+
+  if (strlen (name) >= sizeof (addr.sun_path))
+    {
+      return -1;
+    }
+
+  sock = socket (AF_UNIX, SOCK_STREAM, 0);
+  if (sock < 0)
+    {
+      return -1;
+    }
+
+  addr.sun_family = AF_UNIX;
+  strcpy (addr.sun_path, name);
+  if (connect (sock, (const struct sockaddr *) &addr,
+	       sizeof (struct sockaddr_un)) < 0)
+    {
+      close (sock);
+      return -1;
+    }
+  return sock;
+}
+#endif
+
 /* Open a connection to a remote debugger.
    NAME is the filename used for communication.  */
 
@@ -311,10 +385,18 @@ remote_open (char *name)
   else if (port_str == NULL)
     {
       struct stat statbuf;
+      int res;
 
-      if (stat (name, &statbuf) == 0
+      res = stat (name, &statbuf);
+      if (res == 0
 	  && (S_ISCHR (statbuf.st_mode) || S_ISFIFO (statbuf.st_mode)))
-	remote_desc = open (name, O_RDWR);
+	{
+	  remote_desc = open (name, O_RDWR);
+	}
+      else if (res == 0 && S_ISSOCK (statbuf.st_mode))
+	{
+	  remote_desc = socket_open (name);
+	}
       else
 	{
 	  errno = EINVAL;
-- 
2.1.4



More information about the Gdb-patches mailing list