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

3

5

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.

Casals answered 11/2, 2011 at 20:51 Comment(3)
you should expand what you mean by valgrind won't let the user input anything. I've used valgrind to debug programs that read from stdin numerous times.Calvin
what i mean is, i start my program and it needs user input to start, (entering a number in stdin), it also outputs some text to the console...the text does not show, and i cannot enter any input on the console when running with valgrind...Casals
@Daneil boil this down to a small reproducer and post the code here. I do not see that behavior, even when debugging large applications.Calvin
O
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?

Oquendo answered 11/2, 2011 at 21:12 Comment(3)
I have tried but nothing, i should probably also mention that i dont get any output to the screen either when valgrind is runningCasals
@Daniel: Read this and see if it's your problem: freelists.org/post/programmingblind/…Oquendo
i guess he is redirecting output to another console, unfortunatly in the enviroment i am in I do not have permition to do that.. is there no other way?Casals
N
2

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
Nutria answered 1/12, 2011 at 1:42 Comment(0)
C
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.

Cabin answered 2/12, 2021 at 16:16 Comment(3)
You just ran valgrind on echo fake-password, I'm guessing that wasn't what you wanted to do.Maxi
Ops! You are right @Hasturkun, I was wrong. The message from "total heap usage" has confused me, since it is almost the same result that I expect from my program. Now I'm curious how this would be done Any ideas to get this right? Thank you for the correction ;)Cabin
Generally, if you're not running Valgrind with the --gen-suppressions=yes flag, it shouldn't steal away your input. As the other answers indicated, you can switch away what Valgrind reads from. Assuming that stdin is still in place, you can pipe your data right through Valgrind, e.g. echo hello world | valgrind... since it won't be reading stdin itself.Maxi

© 2022 - 2024 — McMap. All rights reserved.