[2/2] Make gdbserver connect to SOCK_STREAM sockets
Commit Message
In GDB:
(gdb) target remote |socat STDIO UNIX-LISTEN:foo.sock
In gdbserver:
$ gdbserver foo.sock ./foo
gdb/gdbserver/ChangeLog:
* remote-utils.c: add support for connecting to a SOCK_STREAM socket
* remote.c: add documentation about this feature
---
gdb/gdbserver/remote-utils.c | 42 ++++++++++++++++++++++++++++++++++++++++--
gdb/gdbserver/server.c | 1 +
2 files changed, 41 insertions(+), 2 deletions(-)
Comments
This second patch may not be that useful because gdbserver is launched
first and starts communicating immediately without waiting for a
remote side to connect to the socket. If the other side is launched
too late, gdbserver has already timed-out and the communication fails
with:
> Remote debugging using |socat STDIO UNIX-LISTEN:a.sock
> Ignoring packet error, continuing...
> warning: unrecognized item "timeout" in "qSupported" response
> Ignoring packet error, continuing..
--
Gabriel
@@ -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
@@ -344,6 +347,33 @@ open_shell_command (const char *command)
}
#endif
+#ifndef USE_WIN32API
+static int
+socket_open (const 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. */
@@ -386,10 +416,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;
@@ -3037,6 +3037,7 @@ gdbserver_usage (FILE *stream)
"\tgdbserver [OPTIONS] --multi COMM\n"
"\n"
"COMM may be a tty device (for serial debugging),\n"
+ "an existing pathname stream-oriented socket,\n"
"'|some shell command' to use stdin/stdout of a given shell command,\n"
"HOST:PORT to listen for a TCP connection, or '-' or 'stdio' to use \n"
"stdin/stdout of gdbserver.\n"