使 valgrind 能够在 c++ 时读取用户输入需要它
我正在尝试使用 valgrind 运行我的 c++ 程序,但是程序中有一些点需要用户从 stdin 输入,但是当我使用 valgrind 运行时,它不会让用户为程序输入任何内容,有没有办法解决这个问题?
到处寻找但没有找到答案。
I am trying to run my c++ program with valgrind, however I have some points in the program which require user input from stdin, but when i run with valgrind, it wont let the user input anything for the program, is there a way around this?
Been searching all around but have not found the answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我还没有尝试过,但我在手册页中发现了这一点:
如果为 valgrind 指定不同的 fd(例如 3)用于输入,会发生什么?
I haven't tried it, but I found this in the man pages:
What happens if you specify a different fd (say, 3) for valgrind to use for input?
这是一个 Linux 示例,其中 cgi 程序 (./myexe) 从 stdin 读取。我们将输入放入文件 mystdin.txt 中。因此 valgrind 可以从终端读取输入,我们执行 --input-fd=3 并告诉 shell 将 /dev/tty 重定向到文件描述符 3。这样我们就可以控制 gdb,我们添加来自 /dev 的 stdin 重定向valgrind 的 --db-command 参数中的 /tty 。这可能是一个更糟糕的例子。希望有帮助。
Here's a linux example where a cgi program (./myexe) reads from stdin. We put the input into a file mystdin. So valgrind can read input from the terminal, we do the --input-fd=3 and tell the shell to redirect /dev/tty to file descriptor 3. So that we can control gdb, we add a redirect of stdin from /dev/tty in the --db-command paramater to valgrind. This is probably a worse case example. Hope it helps.
这是我的答案,就我而言,它解决了我的需求。
编辑:我错了,这个解决方案无效。阅读@Hasturkun 评论。
我有一个程序要求用户通过提示输入密码:
我写一个密码,例如“abcd1234”并按回车键,程序成功结束。好的,但是...我如何使用 Valgrind 对我的程序进行内存检查,并且提示不等待我输入密码并按 Enter 键?
我只是在程序的执行之前用管道放置
echo "fake-password"
,如下所示:这样,程序就不会要求用户在提示符和字符串中输入任何数据“假密码”将由 stdIn 发送到程序,就像您编写它并按 Enter 键一样。
这就是结果:
我希望它对你有用,对我也有用。
This is my answer, in my case it solved my need.
Edit: I was wrong, this solution is not valid. Read @Hasturkun comment.
I have a program that asks the user to enter a password through the prompt:
I write a password, for example "abcd1234" and press enter, the program ends successfully. Ok, but ... how can I do a memory check of my program using Valgrind, and the prompt doesn't wait for me to enter a password and press enter?
I simply put
echo "fake-password"
with a pipe before the exec of my program, like this:In this way, the program will not ask the user to enter any data at the prompt and the string "fake-password" will be sent by the stdIn to the program in the same way as if you had written it and pressed Enter.
This is the result:
I hope it works for you, it worked for me.