It’s occasionally handy to be able to separate an application’s console I/O from that of gdb. Perhaps you’re debugging a particularly chatty application that’s preventing you from seeing what’s going on with gdb. Or maybe you’re debugging an ncurses application, a situation where gdb’s terminal I/O can actually interfere with the application’s UI.
Fortunately, there’s a mechanism that allows your application’s console I/O to be handled in a completely different terminal than that of gdb. The gdb command is called “tty” (not to be confused with the command line tool “tty” which we’ll also be using here).
Step 1: Figure out the device file for the terminal where you want your app’s console I/O to be handled. In that terminal…
skirk@dormouse:~$ tty /dev/pts/18
Note: It probably goes without saying, but the device path will vary from terminal to terminal.
Step 2: Put the shell in that terminal to sleep so it’s not competing with your application for I/O.
skirk@dormouse:~$ sleep 1000000
Step 3: In a second terminal, fire up gdb with your application.
skirk@dormouse:~$ gdb ./myapp GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1 Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from ./ttytest...done. (gdb)
Step 4: Within gdb, set the device file path for terminal where your app’s I/O should be handled.
(gdb) tty /dev/pts/18
Step 5: Debug your app! Note: You may see a warning about GDB not being able to set the controlling terminal. This is safe to ignore.