gpt4 book ai didi

linux - 在 Linux 中以交互方式打开和关闭输出?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:23:48 25 4
gpt4 key购买 nike

在 Linux 上控制输出有 control-s 和 control-t,它们提供了一种暂时停止终端输出然后恢复的方法。此外,在 VMS 上还有 control-O,它可以打开和关闭所有输出。这并没有暂停输出,而是丢弃了它。

Linux 中是否有等效的键盘快捷键?

在调试输出数百万条状态行的程序时,这对我来说在 gdb 中最常出现。如果能够暂时将大部分内容发送到/dev/null 而不是屏幕,然后继续接收输出流,这将非常方便,中间省去了几百万行。

(已编辑:termios(3) 手册页提到了 VDISCARD - 然后说它不会在 POSIX 或 Linux 中工作。所以看起来这对于在 linux 上使用一般命令行是不可能的。不过,gdb 可能仍然能够通过它自己的命令之一丢弃输出。可以吗?)

谢谢。

最佳答案

On VMS in addition there was control-O ...

这个功能在我处理过的任何 UNIX 系统上似乎都不存在(或者也许我只是从来不知道它存在;它记录在例如 FreeBSD man page 中,并被 Solaris 和 HP-UX 文档引用以及)。

gdb might still be able to discard output though, through one of its own commands. Can it?

我不这么认为:GDB 实际上并没有拦截劣质(正在调试)进程的输出,它只是让它运行(在断点之间),劣质输出会转到它要去的地方。

也就是说,可以自己做:

#include <stdio.h>
int main()
{
int i;
for (i = 0; i < 1000; ++i) {
printf("%d\n", i);
}
}

gcc -g foo.c
gdb -q ./a.out

(gdb) break 6
Breakpoint 1 at 0x40053e: file foo.c, line 6.

(gdb) run 20>/dev/null # run the program, file descriptor 20 goes to /dev/null

Starting program: /tmp/a.out 20>/dev/null

Breakpoint 1, main () at foo.c:6
6 printf("%d\n", i);
(gdb) c
Continuing.
0

Breakpoint 1, main () at foo.c:6
6 printf("%d\n", i);

我们现在已经进行了两次迭代。让我们阻止 100 次迭代的进一步输出:

(gdb) call dup2(20, 1)
$1 = 1
(gdb) ign 1 100
Will ignore next 100 crossings of breakpoint 1.
(gdb) c
Continuing.

Breakpoint 1, main () at foo.c:6
6 printf("%d\n", i);
(gdb) p i
$2 = 102

没有输出,如所愿。现在让我们恢复输出:

(gdb) call dup2(2, 1)
$3 = 1
(gdb) ign 1 10
Will ignore next 10 crossings of breakpoint 1.
(gdb) c
Continuing.
102
103
104
105
106
107
108
109
110
111
112

Breakpoint 1, main () at foo.c:6
6 printf("%d\n", i);

输出恢复!

关于linux - 在 Linux 中以交互方式打开和关闭输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38730133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com