开发者

making valgrind able to read user input when c++ needs it

开发者 https://www.devze.com 2023-02-10 14:00 出处:网络
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 th

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 thi开发者_如何学Pythons?

Been searching all around but have not found the answer.


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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号