public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Use a shell command as a socket for gdbserver
@ 2015-05-02 22:40 Gabriel Corona
  2015-05-04  7:44 ` Mike Frysinger
  0 siblings, 1 reply; 3+ messages in thread
From: Gabriel Corona @ 2015-05-02 22:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Gabriel Corona

This brings feature parity with gdb in this regard.

This can be used to do gdbserver over unix socket (without redirecting
the inferior stdio to /dev/null):

    $ gdbserver '|socat STDIO UNIX-LISTEN:foo.sock' ./foo

and in gdb:

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

Altneratively, we can initiate a connection to a remote server:

    $ gdbserver '|socat STDIO TCP:gdb.example.com:9000' ./foo

gdb/gdbserver/ChangeLog:

        * remote-utils.c: add support for using a shell command stdio as COMM
        * remote.c: add documentation about this feature
---
 gdb/gdbserver/remote-utils.c | 63 ++++++++++++++++++++++++++++++++++++++++++++
 gdb/gdbserver/server.c       |  3 ++-
 2 files changed, 65 insertions(+), 1 deletion(-)

diff --git a/gdb/gdbserver/remote-utils.c b/gdb/gdbserver/remote-utils.c
index 1de86be..6d88968 100644
--- a/gdb/gdbserver/remote-utils.c
+++ b/gdb/gdbserver/remote-utils.c
@@ -58,6 +58,8 @@
 #endif
 #include <sys/stat.h>
 
+#include "gdb_wait.h"
+
 #if USE_WIN32API
 #include <winsock2.h>
 #endif
@@ -239,6 +241,14 @@ remote_prepare (char *name)
       return;
     }
 
+#ifndef USE_WIN32API
+  if (name[0] == '|')
+    {
+      transport_is_reliable = 1;
+      return;
+    }
+#endif
+
   port_str = strchr (name, ':');
   if (port_str == NULL)
     {
@@ -280,6 +290,48 @@ 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
+
 /* Open a connection to a remote debugger.
    NAME is the filename used for communication.  */
 
@@ -288,6 +340,17 @@ remote_open (char *name)
 {
   char *port_str;
 
+#ifndef USE_WIN32API
+  if (name[0] == '|')
+    {
+      fprintf (stderr, "Remote debugging using shell command\n");
+      remote_desc = open_shell_command (name + 1);
+      enable_async_notification (remote_desc);
+      add_file_handler (remote_desc, handle_serial_event, NULL);
+      return;
+    }
+#endif
+
   port_str = strchr (name, ':');
 #ifdef USE_WIN32API
   if (port_str == NULL)
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index d2e20d9..9ed4049 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -3036,7 +3036,8 @@ gdbserver_usage (FILE *stream)
 	   "\tgdbserver [OPTIONS] --attach COMM PID\n"
 	   "\tgdbserver [OPTIONS] --multi COMM\n"
 	   "\n"
-	   "COMM may either be a tty device (for serial debugging),\n"
+	   "COMM may be a tty device (for serial debugging),\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"
 	   "PROG is the executable program.  ARGS are arguments passed to inferior.\n"
-- 
2.1.4

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Use a shell command as a socket for gdbserver
  2015-05-02 22:40 [PATCH] Use a shell command as a socket for gdbserver Gabriel Corona
@ 2015-05-04  7:44 ` Mike Frysinger
  2015-05-04  9:11   ` Gabriel Corona
  0 siblings, 1 reply; 3+ messages in thread
From: Mike Frysinger @ 2015-05-04  7:44 UTC (permalink / raw)
  To: Gabriel Corona; +Cc: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 963 bytes --]

On 03 May 2015 00:39, Gabriel Corona wrote:
> +static int open_shell_command(char* command)

const, and the * should be next to "command", not "char"

> +  res = socketpair(AF_LOCAL, SOCK_STREAM, 0, sockets);

space before the "(" -- this comes up a lot in your patch

> +  else if (child == 0)
> +    {
> +      if (close (sockets[0]) < 0)
> +	exit (1);
> +      if (dup2 (sockets[1], 0) < 0 || dup2 (sockets[1], 1) < 0)
> +	exit (1);

why not use error() instead of exit(1) ?

shouldn't you close sockets[1] after the dup2 ?

> +      res = fork ();
> +      if (res < 0)
> +	exit (1);
> +      if (res != 0)
> +	exit (0);

the double fork could use a comment

> +    {
> +      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];

shouldn't this close sockets[1] before anything else ?
-mike

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Use a shell command as a socket for gdbserver
  2015-05-04  7:44 ` Mike Frysinger
@ 2015-05-04  9:11   ` Gabriel Corona
  0 siblings, 0 replies; 3+ messages in thread
From: Gabriel Corona @ 2015-05-04  9:11 UTC (permalink / raw)
  To: Mike Frysinger; +Cc: gdb-patches

Hello,

Thanks, I'll fix that.

The other patch (Make gdbserver connect to SOCK_STREAM sockets) has
similar formatting issues that I'll fix as well.

> shouldn't you close sockets[1] after the dup2 ?

Indeed I forgot to close it.

--
Gabriel

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-05-04  9:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-02 22:40 [PATCH] Use a shell command as a socket for gdbserver Gabriel Corona
2015-05-04  7:44 ` Mike Frysinger
2015-05-04  9:11   ` Gabriel Corona

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).