4

Background of the question:

I run a command like this:

$ valgrind ./my_program < 1.in

And I get Valgrind's messages about leaks and errors as well as the output stdout and stderr streams of the my_program.

Question:

I would like to redirect/mute all the streams of my_program (both stdout and stderr).

Where's what I've learnt so far:

  • Running > /dev/null doesn't mute the stderr stream of my_program.

  • Running > /dev/null 2> /dev/null mutes all the output stream of my_program all together with Valgrind's messages.

  • According to this thread: How to redirect Valgrind's output to a file? Valgrind is capable of streaming its output directly to a log file using valgrind --log-file="filename".

Possible solution:

I've came up with a solution like this

$ valgrind --log-file="filename" ./my_program < 1.in && cat filename

Is there an easier way to do this?

Community
  • 1
  • 1
Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79

3 Answers3

10

To separate valgrind and application output you can redirect valgrind output to another file descriptor: valgrind --log-fd=9 9>>test.log ./app

doqtor
  • 8,414
  • 2
  • 20
  • 36
  • Could you possibly explain what your command does? I thought I could just find something about `--log-fd` but there is nothing like this in the [Bash Reference Manual](http://www.gnu.org/software/bash/manual/bashref.html) – Mateusz Piotrowski May 26 '15 at 10:14
  • 2
    @MateuszPiotrowski --log-fd is a valgrind option. It tells valgrind to output valgrind stuff into specified file descriptor. I guess the rest should be clear. – doqtor May 26 '15 at 10:26
  • I've managed to write what I wanted thanks to your help: `valgrind --log-fd=9 ./app < test.in 1>/dev/null`. This solution prints Valgrind's messages and hides app's output. – Mateusz Piotrowski May 26 '15 at 13:05
6

This may be a little late to the punch, but I'm running valgrind 3.10.1 on ubuntu, and you can simply use the --log-file=<filename> flag. I.E. run:

valgrind [args] --log-file=<filename> ./my_program
Will C
  • 317
  • 3
  • 8
0

As I understood the question, this is the answer that I found works for me:

valgrind --log-fd=9 [program]  9>&1 1>/dev/null 2>/dev/null

This outputs the valgrind messages to stdout, but the stdout and stderr from the program goes to /dev/null. The order of the redirects is important, as described here.

A "-q" option will also restrict valgrind to errors only.

user4815162342
  • 2,058
  • 3
  • 18
  • 23