public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: "Alexandra Hájková" <ahajkova@redhat.com>
To: gdb-patches@sourceware.org
Cc: ahajkova@redhat.com
Subject: [PATCH 2/6] gdb/ser-pipe.c: Duplicate the file descriptors
Date: Fri, 17 Nov 2023 12:18:36 +0100	[thread overview]
Message-ID: <20231117111840.2040709-3-ahajkova@redhat.com> (raw)
In-Reply-To: <20231117111840.2040709-1-ahajkova@redhat.com>

Duplicate the numbers of STDOUT/STDIN/STDERR file descriptors
GDB is connected to. Preserved numbers of the file descriptors
could be then sent to the GDBserver. If GDBserver is run locally
and will accept he numbers of the file descriptors, it can start
the inferior connected to the same STDIN/OUT/ERR, GDB is connected to.
---
 gdb/ser-pipe.c | 25 +++++++++++++++++++++++++
 gdb/serial.c   |  4 ++++
 gdb/serial.h   |  4 ++++
 3 files changed, 33 insertions(+)

diff --git a/gdb/ser-pipe.c b/gdb/ser-pipe.c
index 47ccd33cece..84253cf2e2c 100644
--- a/gdb/ser-pipe.c
+++ b/gdb/ser-pipe.c
@@ -77,6 +77,16 @@ pipe_open (struct serial *scb, const char *name)
       return -1;
     }
 
+  /* Preserve STDIN/STDOUT/STDERR so they won't be closed on
+     exec later, after we fork.  */
+  int saved_stdin = dup (STDIN_FILENO);
+  int saved_stdout = dup (STDOUT_FILENO);
+  int saved_stderr = dup (STDERR_FILENO);
+
+  scb->fds[0] = saved_stdin;
+  scb->fds[1] = saved_stdout;
+  scb->fds[2] = saved_stderr;
+
   /* Create the child process to run the command in.  Note that the
      apparent call to vfork() below *might* actually be a call to
      fork() due to the fact that autoconf will ``#define vfork fork''
@@ -90,6 +100,12 @@ pipe_open (struct serial *scb, const char *name)
       close (pdes[1]);
       close (err_pdes[0]);
       close (err_pdes[1]);
+      close (saved_stdout);
+      close (saved_stdin);
+      close (saved_stderr);
+      scb->fds[0] = -1;
+      scb->fds[1] = -1;
+      scb->fds[2] = -1;
       return -1;
     }
 
@@ -128,6 +144,10 @@ pipe_open (struct serial *scb, const char *name)
 	  close (err_pdes[1]);
 	}
 
+      mark_fd_no_cloexec (saved_stdout);
+      mark_fd_no_cloexec (saved_stdin);
+      mark_fd_no_cloexec (saved_stderr);
+
       close_most_fds ();
 
       const char *shellfile = get_shell ();
@@ -139,6 +159,11 @@ pipe_open (struct serial *scb, const char *name)
   close (pdes[1]);
   if (err_pdes[1] != -1)
     close (err_pdes[1]);
+
+  close (saved_stdout);
+  close (saved_stdin);
+  close (saved_stderr);
+
   /* :end chunk */
   state = XNEW (struct pipe_state);
   state->pid = pid;
diff --git a/gdb/serial.c b/gdb/serial.c
index 8a8bab46ead..a2bbe9a972d 100644
--- a/gdb/serial.c
+++ b/gdb/serial.c
@@ -182,6 +182,10 @@ new_serial (const struct serial_ops *ops)
 
   scb->ops = ops;
 
+  scb->fds[0] = -1;
+  scb->fds[1] = -1;
+  scb->fds[2] = -1;
+
   scb->bufp = scb->buf;
   scb->error_fd = -1;
   scb->refcnt = 1;
diff --git a/gdb/serial.h b/gdb/serial.h
index 3b861200302..113f7360a55 100644
--- a/gdb/serial.h
+++ b/gdb/serial.h
@@ -254,6 +254,10 @@ struct serial
     int async_state;		/* Async internal state.  */
     void *async_context;	/* Async event thread's context */
     serial_event_ftype *async_handler;/* Async event handler */
+
+    /* File descriptors for preserved STDIN/STDOUT/STDERR
+       to be sent to GDBserver when run locally.  */
+    int fds[3];
   };
 
 struct serial_ops
-- 
2.41.0


  parent reply	other threads:[~2023-11-17 11:18 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-17 11:18 [PATCH 0/6] Add vDefaultInferiorFd feature Alexandra Hájková
2023-11-17 11:18 ` [PATCH 1/6] gdb.server/non-existing-program.exp: Use gdbserver_start Alexandra Hájková
2023-11-17 11:18 ` Alexandra Hájková [this message]
2023-12-12 19:42   ` [PATCH 2/6] gdb/ser-pipe.c: Duplicate the file descriptors Tom Tromey
2023-11-17 11:18 ` [PATCH 3/6] Add new vDefaultInferiorFd packet Alexandra Hájková
2023-11-17 12:09   ` Eli Zaretskii
2023-12-12 20:03   ` Tom Tromey
2023-11-17 11:18 ` [PATCH 4/6] gdbserver/linux-low.cc: Connect the inferior to the terminal Alexandra Hájková
2023-12-12 20:10   ` Tom Tromey
2023-11-17 11:18 ` [PATCH 5/6] remote.c: Add terminal handling functions Alexandra Hájková
2023-12-12 20:11   ` Tom Tromey
2023-11-17 11:18 ` [PATCH 6/6] Add defaultinf.exp test to the testsuite Alexandra Hájková
2023-11-27 10:01 ` [PATCH 0/6] Add vDefaultInferiorFd feature Alexandra Petlanova Hajkova
2023-12-01 20:22 ` Tom Tromey
2023-12-04 11:08   ` Andrew Burgess
2023-12-04 12:11   ` Alexandra Petlanova Hajkova
2023-12-05 16:00     ` Tom Tromey
2023-12-08 13:06       ` Andrew Burgess
2023-12-12 20:14   ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231117111840.2040709-3-ahajkova@redhat.com \
    --to=ahajkova@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).