Gdb:暂时重定向目标标准输出 [英] GDB: redirect target stdout temporarly
本文介绍了Gdb:暂时重定向目标标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我启动gdb时,目标进程打印大量数据,因此我希望将其重定向到NULL,直到某个时间点。
到目前为止,我找到的唯一两种方法是:
运行>文件名
tty文件名
问题是我找不到方法将下级的标准输出恢复到正常。
没有"tty Default"或"Default tty"
谢谢,
当天
推荐答案
我找不到将下级标准输出恢复到正常的方法
您可以这样做:
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
", i);
8 }
9 printf("An important line
");
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
");
(gdb) call fflush(0)
$1 = 0
由于我们即将切换输出,因此希望确保刷新所有缓冲数据。
接下来我们调用open("/dev/tty", O_WRONLY)
。您可以在/usr/include
中通过grep
ping找到O_WRONLY
的值。
(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
所以我们现在有了一个新的文件描述符3
,它将输出到当前终端。最后,我们切换到STDOUT_FILENO
,如下所示:
(gdb) call dup2(3, 1)
$3 = 1
(gdb) c
Continuing.
An important line
[Inferior 1 (process 22625) exited normally]
瞧:终端上打印了"一条重要的行"。
这篇关于Gdb:暂时重定向目标标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文