问题描述
似乎gdb中的输入重定向在Cygwin中不起作用,例如
It seems that input redirection in gdb does not work in Cygwin e.g
(gdb) run < input.txt
有没有其他方法可以重定向Cygwin的gdb中的输入
Is there other way to redirect input in gdb of Cygwin??
推荐答案
长期存在,但显然很难修复 - 而且可能gdb开发人员喜欢在与更常见的环境(例如Linux)相关的功能/问题上花时间。
Unfortunately this is not possible when running gdb in cygwin. The bug exists for a quote long time, but apparently it's a hard one to fix - and probably the gdb devs prefer spending time on features/issues relevant to more common environments (such as Linux).
有各种可能的解决方法;我喜欢第一个,因为它是最干净,也有用,而不是在cygwin上调试/运行:
There are various possible workarounds; I'd prefer the first one since it's the cleanest and also useful while not debugging / running on cygwin:
- 添加命令行参数,例如
-f无论
与任何
是要读取的文件名。如果参数不存在或设置为-
,从stdin中读取。-f -
选项当然是可选的,但对于接受文件名的参数,它是一个通用标准(只要有意义)处理-
asuse stdin / out。
-
使用gdb hack提及将stdin重新映射到应用程序中手动打开的文件:
- Add a command line argument, e.g.
-f whatever
withwhatever
being the filename to read from. If the argument is not present or set to-
, read from stdin. The-f -
option is optional of course but for arguments accepting filenames it's a common standard (as long as it makes sense) to handle-
as "use stdin/out". Use the gdb hack mentioned here to remap stdin to a manually opened file inside the application:
> gdb yourexecutable
(gdb) break main
(gdb) run
(gdb) call dup2(open("input.txt", 0), 0)
(gdb) continue
在 main
函数上设置断点,然后执行程序,它会在输入 main
后立即断开。然后用于替换stdin fd( 0
)与输入文件的文件描述符。
This sets a breakpoint on the main
function, then executes the program which will break right after entering main
. Then dup2
is used to replace the stdin fd (0
) with a file descriptor of the input file.
这篇关于gdb输入重定向使用cygwin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!