DEV Community

nullity
nullity

Posted on

gdb trick: automatically redirecct output to another terminal

tty command in gdb can direct output to another terminal.
Like https://stackoverflow.com/questions/8963208/gdb-display-output-of-target-application-in-a-separate-window

But I want it to be set automatically.

Method

Create a script ~/.gdbtty.sh
Its content

#!/bin/bash
gnome-terminal -- bash -c "tty > ~/.gdbtty.txt; exec bash" &
sleep 0.5  # Give it time to open
TTY=$(cat ~/.gdbtty.txt)
echo "set inferior-tty $TTY" > ~/.gdbtty.txt
Enter fullscreen mode Exit fullscreen mode

And edit ~/.gdbinit as

shell ~/.gdbtty.sh
source ~/.gdbtty.txt
Enter fullscreen mode Exit fullscreen mode

Some shortcomings

It's good but with some tiny shortcomings that I currently can't solve.

  1. I cannot control where the output terminal would popup. (But even I can control, I have no idea where is the ideal position for me)
  2. The output terminal may show a harmless warning "warning: GDB: Failed to set controlling terminal: Operation not permitted"

Top comments (0)