Subject: Re: bug#70519: 30.0.50; Device for Emacs terminal I/O

From: Eli Zaretskii <[email-masked]>
Date: Tue, 23 Apr 2024 08:32:25 +0300

> From: Helmut Eller <[email-masked]>
> Date: Mon, 22 Apr 2024 22:09:18 +0200
> 
> I'd like to start Emacs under GDB, but so that Emacs doesn't use the
> same terminal as GDB.

You should be able to do that with GDB features.  These include:

  . the 'set inferior-tty' command
  . the 'set new-console' command

The first sets the terminal of the debuggee to the named terminal, the
latter causes GDB to create a new terminal each time you "run" a
debuggee, and force the debuggee to use that new terminal.  These
commands should work for you without any changes to the Emacs sources.

Alternatively, you could start GDB from a different terminal and
attach it to an already running Emacs, but this does not allow you to
debug the Emacs startup code.

> It seems that the --terminal command line switch is there for
> exactly this use case.
> 
> However, it doesn't work.  Emacs parses the command line option and
> replaces stdin and stdout with the correct device, but then in dispnew.c
> it always calls init_tty with 0 as argument for the device name.  That
> simply opens the controlling terminal, i.e. /dev/tty and that is usually
> the same device as the one that GDB uses.
> 
> What would you think of the change below?

I don't think it's the correct change.  For starters, ttyname is
non-portable: on some supported platforms there's no way of getting at
the name of a non-default terminal.

More importantly, we already know the name of the terminal: we used it
in emacs.c when we processed the --terminal switch.  We just "forgot"
it because we didn't save it anywhere.  So one way of fixing this is
to record that name and reuse it in init_tty.  E.g., make DEV_TTY
non-const, and save the actual name there when we process it in
emacs.c.

Adding Paul in case he has comments.