GDB:暂时重定向目标标准输出

Ita*_*rom 2 linux debugging gdb stdout

当我启动 GDB 时,目标进程会打印大量数据,因此我想将其重定向到 NULL 直到某个时间点。

到目前为止,我发现的唯一两种方法是:

  1. 运行 > 文件名

  2. tty 文件名

问题是我找不到将劣质的标准输出恢复正常的方法。

没有“tty default”或“default tty”

谢谢,

伊泰

Emp*_*ian 5

我找不到将劣等的标准输出恢复正常的方法

您可以这样做:

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
Run Code Online (Sandbox Code Playgroud)

由于我们即将切换输出,我们希望确保刷新所有缓冲数据。

接下来我们调用open("/dev/tty", O_WRONLY). 您可以O_WRONLY通过grepping 在 中找到它的值/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
Run Code Online (Sandbox Code Playgroud)

所以我们现在有一个新的文件描述符3,它将输出到当前终端。最后,我们切换STDOUT_FILENO到它,像这样:

(gdb) call dup2(3, 1)
$3 = 1
(gdb) c
Continuing.
An important line
[Inferior 1 (process 22625) exited normally]
Run Code Online (Sandbox Code Playgroud)

Voilà:终端上打印了“一条重要的线路”。