Remote GDB output

2020-08-04

I've recently come across a fairly useful trick for debugging programs that do a lot of command line IO. As it turns out, it is possible to tell GDB to redirect the standard IO streams of the debugged program to a different terminal session. How this works is that you first determine the underlying terminal device file for the remote window, then pass it to gdb using the -tty flag. Now GDB will run the debugged program with all input and output directed towards the remote terminal.

For example, lets say that you wanted to debug a Vim instance. Usually this would be fairly painful to do since Vim uses ncurses quite heavily, which will keep cluttering your screen when you attempt to step through it in GDB. To redirect its IO streams, do the following:

  1. Open two terminal sessions, these can be terminal windows, tabs in your terminal emulator, tmux sessions/panes, etc., anything that runs a shell, really.
  2. In the second session, run tty && sleep infinity. This will first print the session's underlying terminal file, and then put the shell to sleep indefinitely (to prevent it from also attempting to read from the tty device, which will result in dropped inputs in the debugged program). For the sake of this example, let's assume tty printed out /dev/pts/5.
  3. In the first session, run gdb -tty /dev/pts/5 vim. Now when you start running vim, all input and output would be directed towards the second session, leaving your first session clean for you to interact with gdb.

© 2020 Sahan Fernando. All rights reserved.