问题
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.
回答1:
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?
回答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
来源:https://stackoverflow.com/questions/4973921/making-valgrind-able-to-read-user-input-when-c-needs-it