Bug 462495

Summary: Konsole need to implement openpty mode to support gdb's --tty option
Product: [Applications] konsole Reporter: chiheng.xu
Component: generalAssignee: Konsole Developer <konsole-devel>
Status: REPORTED ---    
Severity: normal CC: ninjalj, towo
Priority: NOR    
Version: unspecified   
Target Milestone: ---   
Platform: unspecified   
OS: All   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description chiheng.xu 2022-12-01 13:10:34 UTC
implement openpty mode.
do not create child process, instead, open a pty, and output the slave name of pty device.
used in gdb debugging.
utilize this gdb command line option:
--tty=TTY          Use TTY for input/output by the program being debugged.

$ OPENPTY= konsole
/dev/pty1

echo hello >/dev/pty1

gdb --tty=/dev/pty1 emacs
run



mintty implementation
https://github.com/xu-chiheng/mintty/commit/f0e80e1876502a83c8e5d1b5c24a4c714a0b0e18
konsole implemention on v4.9.4
https://github.com/xu-chiheng/konsole/commit/f7b94b0756101239279630d06309128d508aaf4f
Comment 1 chiheng.xu 2022-12-02 01:33:10 UTC
use --openpty to activate openpty mode

konsole --openpty
/dev/pty1
Comment 2 ninjalj 2022-12-02 21:40:08 UTC
IIUC, something similar can be achieved via:

konsole --noclose -e /usr/bin/tty
Comment 3 chiheng.xu 2022-12-02 23:48:47 UTC
(In reply to ninjalj from comment #2)
> IIUC, something similar can be achieved via:
> 
> konsole --noclose -e /usr/bin/tty

A human can copy and past such slave name of the pty. How a program, like IDE(Eclipse CDT) get that slave name?
The only way is through stdout.

https://gitlab.gnome.org/GNOME/gnome-terminal/-/issues/7951

https://github.com/mintty/mintty/issues/1187
Comment 4 ninjalj 2022-12-03 00:17:34 UTC
On Linux something like the following would output the tty device name. The sleep may need to be tweaked.

die()
{
    echo "$@" >&2
    exit 1
}

konsole --noclose -e /usr/bin/tty &
konsolepid=$!

sleep 1

for fd in /proc/$konsolepid/fd/* 
do
    [ "x$(readlink $fd)" = x"/dev/ptmx" ] && ptmx=$fd && break
done

[ -z $ptmx ] && die "konsole process doesn't appear to have opened a pty master"

ptmx=${ptmx/fd/fdinfo}
awk -F: '/tty-index/{ print "/dev/pts/"$2+0 }' $ptmx
Comment 5 chiheng.xu 2022-12-03 15:58:22 UTC
Figured out a way to work partially:
mintty.exe --hold always sh.exe -c "tty > 1.txt; read;" &
konsole --no-close sh -e "tty > 1.txt; read;" &
gnome-terminal --hold sh -e "tty > 1.txt; read;" &

gdb --tty=$(cat 1.txt) emacs
run

But there is a problem that emacs can't get input from the terminal, because of the read command.
Comment 6 ninjalj 2022-12-03 21:20:47 UTC
The 'read' is unnecessary. Try:

 konsole --hold -e 'sh -c /usr/bin/tty > konstty'

(in konsole, --hold and --noclose are synonyms) and then:

gdb --tty $(cat konstty) --args emacs -nw

At the gdb prompt type "run" and you should be set to go.
Comment 7 ninjalj 2022-12-04 00:25:51 UTC
Sorry, that should have been:

konsole --hold -e 'sh -c "/usr/bin/tty > konstty"'
Comment 8 chiheng.xu 2022-12-04 02:30:30 UTC
(In reply to ninjalj from comment #7)
> Sorry, that should have been:
> 
> konsole --hold -e 'sh -c "/usr/bin/tty > konstty"'

Have you tried that ? On windows using mintty, this method does not work perfectly.
sometimes the program(emacs) does not respond.
Comment 9 Thomas Wolff 2022-12-04 12:10:29 UTC
I'm trying this with 'mintty -Rt -h alw sleep 99999 &' and running gdb with another screen program (text editor mined) and see no problems.
Comment 10 ninjalj 2022-12-09 23:17:08 UTC
(In reply to chiheng.xu from comment #8)
> (In reply to ninjalj from comment #7)
> > Sorry, that should have been:
> > 
> > konsole --hold -e 'sh -c "/usr/bin/tty > konstty"'
> 
> Have you tried that ? On windows using mintty, this method does not work
> perfectly.
> sometimes the program(emacs) does not respond.

I don't use windows. With this method, you will need to wait for konsole/mintty to open the pty pair and execute 'sh -c "/usr/bin/tty > konstty"'. E.g: you could remove the "konstty" file, and wait for it to have a valid terminal name.

On my testing, I have not seen any problem debugging emacs.

Note that there may be unavoidable problems with SIGINT, see https://lwn.net/Articles/909496/