Bug 471311

Summary: gdb --multi mode stdout redirecting to stderr
Product: [Developer tools] valgrind Reporter: Mark Wielaard <mark>
Component: generalAssignee: Mark Wielaard <mark>
Status: RESOLVED FIXED    
Severity: normal CC: ahajkova
Priority: NOR    
Version: 3.21.0   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:
Attachments: gdb --multi mode stdout redirecting to stderr

Description Mark Wielaard 2023-06-21 22:09:00 UTC
Sasha is working on a real solution for redirecting all of the stdin/out/err file descriptors:
https://git.sr.ht/~sasshka/binutils-gdb/log/split_fd

But that needs a new gdb that understand the new proposed FdSwitch packet.

As a temporary workaround we can at least redirect stdout to stderr (and close stdin) for the inferior (unless --port is used).

Something like:
/* close stdin */
close (0);
/* open /dev/null as new stdin */
open ("/dev/null", O_RDONLY);
/* redirect stdout as stderr */
dup2 (2, 1);
Comment 1 Mark Wielaard 2023-08-17 13:45:48 UTC
Created attachment 161024 [details]
gdb --multi mode stdout redirecting to stderr

Implementing this is almost trivial, see attached.
Comment 2 Mark Wielaard 2023-08-21 16:03:56 UTC
commit b35a8ed5a9575dbadb7e2a84bd4b6c4ab9eda21a
Author: Mark Wielaard <mark@klomp.org>
Date:   Thu Aug 17 15:40:30 2023 +0200

    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