[4/6] gdbserver/linux-low.cc: Connect the inferior to the terminal

Message ID 20231117111840.2040709-5-ahajkova@redhat.com
State New
Headers
Series Add vDefaultInferiorFd feature |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed

Commit Message

Alexandra Petlanova Hajkova Nov. 17, 2023, 11:18 a.m. UTC
  If GDBserver is connected to GDB via stdio and it recieved file
descriptors of the STDIN/OUT/ERR of the terminal GDB is connected to,
GDBserver will start the inferior connected to them.
---
 gdbserver/linux-low.cc | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)
  

Comments

Tom Tromey Dec. 12, 2023, 8:10 p.m. UTC | #1
>>>>> "Alexandra" == Alexandra Hájková <ahajkova@redhat.com> writes:

Alexandra> --- a/gdbserver/linux-low.cc
Alexandra> +++ b/gdbserver/linux-low.cc
Alexandra> @@ -952,6 +952,8 @@ linux_process_target::low_new_thread (lwp_info *info)

If this feature is Linux-specific then I think there has to be a new
target method so that gdbserver doesn't advertise it on platforms where
it won't actually work.

Alexandra> +    if ((fstat(cs.fds[0], &buf) == -1)
Alexandra> +        || (fstat(cs.fds[1], &buf) == -1)
Alexandra> +        || (fstat(cs.fds[2], &buf) == -1))

These are over-parenthesized.

Alexandra> +      write (2, "Fstat failed. Can't connect inferior to the terminal.\n",
Alexandra> +             sizeof ("Fstat failed. Can't connect inferior to the terminal.\n") - 1);

This looked weird but now I see that other code here does this.

Tom
  

Patch

diff --git a/gdbserver/linux-low.cc b/gdbserver/linux-low.cc
index f9001e2fa17..04d72d22050 100644
--- a/gdbserver/linux-low.cc
+++ b/gdbserver/linux-low.cc
@@ -952,6 +952,8 @@  linux_process_target::low_new_thread (lwp_info *info)
 static void
 linux_ptrace_fun ()
 {
+  client_state &cs = get_client_state ();
+
   if (ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0,
 	      (PTRACE_TYPE_ARG4) 0) < 0)
     trace_start_error_with_name ("ptrace");
@@ -959,10 +961,38 @@  linux_ptrace_fun ()
   if (setpgid (0, 0) < 0)
     trace_start_error_with_name ("setpgid");
 
+  /* If GDBserver is connected to GDB via stdio and it recieved the file
+     descriptors for the terminal stdin/out/err from GDB, start the inferior
+     connected to them.  */
+  if (remote_connection_is_stdio () && cs.vDefaultInferiorFd_accepted)
+  {
+    struct stat buf{};
+
+    if ((fstat(cs.fds[0], &buf) == -1)
+        || (fstat(cs.fds[1], &buf) == -1)
+        || (fstat(cs.fds[2], &buf) == -1))
+    {
+      write (2, "Fstat failed. Can't connect inferior to the terminal.\n",
+             sizeof ("Fstat failed. Can't connect inferior to the terminal.\n") - 1);
+      cs.vDefaultInferiorFd_accepted = false;
+    }
+
+    /* Dupped file descriptors will be inherited by the inferior.  */
+    if (!cs.vDefaultInferiorFd_accepted
+        || (dup2 (cs.fds[0], 0) == -1)
+        || (dup2 (cs.fds[1], 1) == -1)
+        || (dup2 (cs.fds[2], 2) == -1))
+      {
+        write (2, "Dup2 failed. Can't connect inferior to the terminal.\n",
+               sizeof ("Dup2 failed. Can't connect inferior to the terminal.\n") - 1);
+        cs.vDefaultInferiorFd_accepted = false;
+      }
+  }
+
   /* If GDBserver is connected to gdb via stdio, redirect the inferior's
      stdout to stderr so that inferior i/o doesn't corrupt the connection.
      Also, redirect stdin to /dev/null.  */
-  if (remote_connection_is_stdio ())
+  if (remote_connection_is_stdio () && !cs.vDefaultInferiorFd_accepted)
     {
       if (close (0) < 0)
 	trace_start_error_with_name ("close");