GDB: redirect target stdout temporarly
debugging, gdb, linux, stdout
Solution
i cannot find a way to restore the stdout of the inferior back to normal
Here is how you can do it:
Reading symbols from /tmp/./a.out...done.
(gdb) list main
1 #include <stdio.h>
2
3 int main() {
4 int i;
5
6 for (i = 0; i < 1000; ++i) {
7 printf("A line we don't care about: %d\n", i);
8 }
9 printf("An important line\n");
10 return 0;
11 }
(gdb) b 9
Breakpoint 1 at 0x400579: file t.c, line 9.
(gdb) run > /dev/null
Starting program: /tmp/./a.out > /dev/null
Breakpoint 1, main () at t.c:9
9 printf("An important line\n");
(gdb) call fflush(0)
$1 = 0
Since we are about to switch output, we want to make sure to flush any buffered data.
Next we call `open("/dev/tty", O_WRONLY)`. You can find the value of `O_WRONLY` by `grep`ping for it in `/usr/include`.
(gdb) shell grep WRONLY /usr/include/bits/*.h
/usr/include/bits/fcntl.h:#define O_WRONLY 01
(gdb) p open("/dev/tty", 1)
$2 = 3
So we now have a new file descriptor `3` that will output to the current terminal. Finally, we switch `STDOUT_FILENO` to it, like so:
(gdb) call dup2(3, 1)
$3 = 1
(gdb) c
Continuing.
An important line
[Inferior 1 (process 22625) exited normally]
Voilà: "An important line" was printed to the terminal.
Problem
When I launch GDB the targeted process prints a lot of data so I want to redirect it to NULL until a certain point in time. the only two methods i've found so far are: run > filename tty filename the problem is i cannot find a way to restore the stdout of the inferior back to normal. there is no "tty default", or "default tty" Thanks, Itay