使 valgrind 能够在 c++ 时读取用户输入需要它

发布于 2024-10-17 07:30:47 字数 129 浏览 9 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2024-10-24 07:30:47

我还没有尝试过,但我在手册页中发现了这一点:

--input-fd=<number> [default: 0, stdin]
              Specify the file descriptor to use for reading  input  from  the
              user.  This  is  used whenever valgrind needs to prompt the user
              for a decision.

如果为 valgrind 指定不同的 fd(例如 3)用于输入,会发生什么?

I haven't tried it, but I found this in the man pages:

--input-fd=<number> [default: 0, stdin]
              Specify the file descriptor to use for reading  input  from  the
              user.  This  is  used whenever valgrind needs to prompt the user
              for a decision.

What happens if you specify a different fd (say, 3) for valgrind to use for input?

云醉月微眠 2024-10-24 07:30:47

这是一个 Linux 示例,其中 cgi 程序 (./myexe) 从 stdin 读取。我们将输入放入文件 mystdin.txt 中。因此 valgrind 可以从终端读取输入,我们执行 --input-fd=3 并告诉 shell 将 /dev/tty 重定向到文件描述符 3。这样我们就可以控制 gdb,我们添加来自 /dev 的 stdin 重定向valgrind 的 --db-command 参数中的 /tty 。这可能是一个更糟糕的例子。希望有帮助。

valgrind --input-fd=3 --db-command='gdb -nw %f %p < /dev/tty' --db-attach=yes ./myexe < mystdin  3</dev/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.

valgrind --input-fd=3 --db-command='gdb -nw %f %p < /dev/tty' --db-attach=yes ./myexe < mystdin  3</dev/tty
独守阴晴ぅ圆缺 2024-10-24 07:30:47

这是我的答案,就我而言,它解决了我的需求。

编辑:我错了,这个解决方案无效。阅读@Hasturkun 评论。

我有一个程序要求用户通过提示输入密码:

./my_program
Password:

我写一个密码,例如“abcd1234”并按回车键,程序成功结束。好的,但是...我如何使用 Valgrind 对我的程序进行内存检查,并且提示不等待我输入密码并按 Enter 键?

我只是在程序的执行之前用管道放置 echo "fake-password" ,如下所示:

valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all -v echo "fake-password" | ./my_program > /dev/null

这样,程序就不会要求用户在提示符和字符串中输入任何数据“假密码”将由 stdIn 发送到程序,就像您编写它并按 Enter 键一样。

这就是结果:

==907664== Memcheck, a memory error detector
==907664== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==907664== Using Valgrind-3.15.0-608cb11914-20190413 and LibVEX; rerun with -h for copyright info
==907664== Command: echo fake-password
==907664== 
--907664-- Valgrind options:
--907664--    --tool=memcheck
--907664--    --leak-check=full
--907664--    --show-leak-kinds=all
--907664--    -v
...
...
--907664-- REDIR: 0x49f2650 (libc.so.6:__mempcpy_avx_unaligned_erms) redirected to 0x4843660 (mempcpy)
--907664-- REDIR: 0x49f2670 (libc.so.6:__memcpy_avx_unaligned_erms) redirected to 0x48429f0 (memmove)
--907664-- REDIR: 0x4901850 (libc.so.6:free) redirected to 0x483c9d0 (free)
==907664== 
==907664== HEAP SUMMARY:
==907664==     in use at exit: 0 bytes in 0 blocks
==907664==   total heap usage: 31 allocs, 31 frees, 8,161 bytes allocated
==907664== 
==907664== All heap blocks were freed -- no leaks are possible
==907664== 
==907664== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

我希望它对你有用,对我也有用。

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:

./my_program
Password:

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:

valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all -v echo "fake-password" | ./my_program > /dev/null

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:

==907664== Memcheck, a memory error detector
==907664== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==907664== Using Valgrind-3.15.0-608cb11914-20190413 and LibVEX; rerun with -h for copyright info
==907664== Command: echo fake-password
==907664== 
--907664-- Valgrind options:
--907664--    --tool=memcheck
--907664--    --leak-check=full
--907664--    --show-leak-kinds=all
--907664--    -v
...
...
--907664-- REDIR: 0x49f2650 (libc.so.6:__mempcpy_avx_unaligned_erms) redirected to 0x4843660 (mempcpy)
--907664-- REDIR: 0x49f2670 (libc.so.6:__memcpy_avx_unaligned_erms) redirected to 0x48429f0 (memmove)
--907664-- REDIR: 0x4901850 (libc.so.6:free) redirected to 0x483c9d0 (free)
==907664== 
==907664== HEAP SUMMARY:
==907664==     in use at exit: 0 bytes in 0 blocks
==907664==   total heap usage: 31 allocs, 31 frees, 8,161 bytes allocated
==907664== 
==907664== All heap blocks were freed -- no leaks are possible
==907664== 
==907664== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

I hope it works for you, it worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文