Blob Blame History Raw
From 449dd82becae6533a8e658e76f30f65434f6417f Mon Sep 17 00:00:00 2001
From: Mark Wielaard <mark@klomp.org>
Date: Thu, 17 Aug 2023 15:40:30 +0200
Subject: [PATCH] gdb --multi mode stdout redirecting to stderr

When in stdio mode (talking to gdb through stdin/stdout, not
through a socket), redirect stdout to stderr and close stdin
for the inferior. That way at least some output can be seen,
but there will be no input.

This is workaround till we have real terminal handling.

     * coregrind/vgdb.c (main): Pass in_port to do_multi_mode.
     (do_multi_mode): Pass in_port to fork_and_exec_valgrind.
     (fork_and_exec_valgrind): Close stdin, redirect stdout to
     stderr if in_port <= 0.

https://bugs.kde.org/show_bug.cgi?id=471311
---
 NEWS             |  1 +
 coregrind/vgdb.c | 20 +++++++++++++++++---
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/coregrind/vgdb.c b/coregrind/vgdb.c
index 56a969de7..c024ffca6 100644
--- a/coregrind/vgdb.c
+++ b/coregrind/vgdb.c
@@ -1159,7 +1159,7 @@ static void gdb_relay(int pid, int send_noack_mode, char *q_buf);
    or the errno from the child on failure.  */
 static
 int fork_and_exec_valgrind (int argc, char **argv, const char *working_dir,
-                            pid_t *pid)
+                            int in_port, pid_t *pid)
 {
    int err = 0;
    // We will use a pipe to track what the child does,
@@ -1243,6 +1243,19 @@ int fork_and_exec_valgrind (int argc, char **argv, const char *working_dir,
          }
       }
 
+      /* When in stdio mode (talking to gdb through stdin/stdout, not
+         through a socket), redirect stdout to stderr and close stdin
+         for the inferior. That way at least some output can be seen,
+         but there will be no input.  */
+      if (in_port <= 0) {
+         /* close stdin */
+         close (0);
+         /* open /dev/null as new stdin */
+         open ("/dev/null", O_RDONLY);
+         /* redirect stdout as stderr */
+         dup2 (2, 1);
+      }
+
       /* Try to launch valgrind. Add --vgdb-error=0 to stop immediately so we
          can attach and --launched-with-multi to let valgrind know it doesn't
          need to show a banner how to connect to gdb, we will do that
@@ -1309,7 +1322,7 @@ int fork_and_exec_valgrind (int argc, char **argv, const char *working_dir,
 
 /* Do multi stuff.  */
 static
-void do_multi_mode(int check_trials)
+void do_multi_mode(int check_trials, int in_port)
 {
    char *buf = vmalloc(PBUFSIZ+1);
    char *q_buf = vmalloc(PBUFSIZ+1); //save the qSupported packet sent by gdb
@@ -1459,6 +1472,7 @@ void do_multi_mode(int check_trials)
              int res = fork_and_exec_valgrind (count,
                                                decoded_string,
                                                working_dir,
+                                               in_port,
                                                &valgrind_pid);
 
              if (res == 0) {
@@ -2427,7 +2441,7 @@ int main(int argc, char** argv)
    if (multi_mode) {
       /* check_trails is the --wait argument in seconds, defaulting to 1
        * if not given.  */
-      do_multi_mode (check_trials);
+      do_multi_mode (check_trials, in_port);
    } else if (last_command >= 0) {
       standalone_send_commands(pid, last_command, commands);
    } else {
-- 
2.41.0