1

我正在尝试调试需要将 EOF 作为输入读取的程序。

但是,当我在 GDB 中运行程序时按 Control-D 发送 EOF 时,GDB 会捕获 EOF 并且不会将其传递给应用程序。

如何使 gdb 将 EOF 发送到应用程序?

4

1 回答 1

1

但是,当程序在 GDB 中运行时,当我按 Control-D 发送 EOF 时,GDB 会捕获 EOF 并且不会将其传递给应用程序。

GDB不做任何这样的事情。

在正常(全停)模式下,应用程序或 GDB 都可以控制终端,但不能同时控制这两者。

如果应用程序正在读取终端输入,那么 Control-D 将导致它读取EOF,而 GDB 不会干扰它。

如果您正在查看(gdb)提示,那么应用程序没有读取输入 - 它已停止 - 发送 Control-D 确实会发送EOF到 GDB。不要那样做。

例子:

gdb -q /bin/cat
Reading symbols from /bin/cat...done.

(gdb) run
Starting program: /bin/cat 
foof     # my input
foof     # cat output
         # Control-D
[Inferior 1 (process 12782) exited normally]  # cat received EOF and exited
(gdb) run
Starting program: /bin/cat 
foof     # my input
foof     # cat output
^C
Program received signal SIGINT, Interrupt.
0x00007ffff7b31ee0 in __read_nocancel () at ../sysdeps/unix/syscall-template.S:82
82  ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) quit   # I typed Control-D, GDB translated that into quit
A debugging session is active.

    Inferior 1 [process 12787] will be killed.

Quit anyway? (y or n) y

更新:

当应用程序正在读取时(不是在 gdb 提示符下),我正在点击 Control-D,并且应用程序不承认它收到了 Control-D。当应用程序尝试读取时,它会读取 0 个字节。

正是应该发生的事情(read返回 0 表示您已到达文件末尾)。如果您期望应用程序读取魔法EOF符号,那么您的期望是错误的——没有这样的符号。

于 2012-10-13T18:44:11.570 回答