0

When I need to configure terminal line settings when the terminal shell is available, I use stty.

When I attach a debugger to a process that's running curses, the current tty settings get in the way of running the debugger (echo is off, newlines do not get translated). I'd like to be able turn on echo and onlcr, so I can properly use the debugger (pdb).

When I suspend this process (Ctrl + z) to check the control settings with stty, these terminal settings have not changed. I think that curses may save/restore these settings upon the suspend / resume signals.

Is there a way to remotely set the terminal settings with a active process without suspending to the parent shell?

  • 1
    Wouldn't it be easier to use a debugger (e.g. gdb) that doesn't use the same terminal as the process being debugged? – dirkt May 14 '17 at 7:04
  • I'm interested at debugging at a higher level, and the only project I could find (pyringe) was unmaintained. – Bryce Guinta May 14 '17 at 9:03
0

The stdout of a process is a file descriptor which is available as a path somewhere on a UNIX system. We will need to find this path to set its tty attributes. Since this is curses, this file descriptor will point to a terminal device (pts/tty).

The first step is to find the pid of the running process. Using pidof, pgrep, or ps aux:

ps aux | grep '<program arguments>'

Once you have the pid of the running process, you can find the path of the terminal device attached to that process using proc (if available) or lsof.


Get tty/pts path using lsof:

 device="$(lsof -X -p <pid> | grep -o '/dev/.*' | grep -v urandom | uniq)"

Which will give you something like:

 device=/dev/pts/4

Here we use the -X option to ignore TCP connections to stop lsof from hanging, and -p to tell lsof that it's being given a pid.


If /proc is available (linux), then you can find a symlink under its pid:

device=/proc/<pid>/fd/1

Now that we know the output device, we can set its tty attributes:

stty onlcr echo icrnl icanon -F $device

By default stty sets the tty attributes of the current terminal's stdin. You can use -F to specify a path.

Now for some reason I had to set these attributes multiple times for them to stick:

while true; do stty onlcr echo icrnl icanon -F /proc/<pid>/fd/1; done

Note: After writing this I realized that ps can list the attached TTY next to the process: ps ao args,tty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.