How to redirect Valgrind's output to a file?

RedirectValgrind

Redirect Problem Overview


While working with Valgrind tool, i need to log the details produced by valgrind tool. How can I accomplish that? I tried something like,

 valgrind a.out | test

and

 valgrind a.out > test

It gave just the program's output and not the valgrind memory error,leak information. Even i am getting like this if the program requires no user interaction (i.e. giving input). If the program need user input even that thing itself won't work.

How can I do this?

Redirect Solutions


Solution 1 - Redirect

valgrind --log-file="filename"

Solution 2 - Redirect

By default, Valgrind writes its output to stderr. So you need to do something like:

valgrind a.out > log.txt 2>&1

Alternatively, you can tell Valgrind to write somewhere else; see http://valgrind.org/docs/manual/manual-core.html#manual-core.comment (but I've never tried this).

Solution 3 - Redirect

You can also set the options --log-fd if you just want to read your logs with a less. For example :

valgrind --log-fd=1 ls | less

Solution 4 - Redirect

In addition to the other answers (particularly by Lekakis), some string replacements can also be used in the option --log-file= as elaborated in the Valgrind's user manual.

Four replacements were available at the time of writing:

  • %p: Prints the current process ID
    • valgrind --log-file="myFile-%p.dat" <application-name>
  • %n: Prints file sequence number unique for the current process
    • valgrind --log-file="myFile-%p-%n.dat" <application-name>
  • %q{ENV}: Prints contents of the environment variable ENV
    • valgrind --log-file="myFile-%q{HOME}.dat" <application-name>
  • %%: Prints %
    • valgrind --log-file="myFile-%%.dat" <application-name>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDineshView Question on Stackoverflow
Solution 1 - RedirectVasileios LekakisView Answer on Stackoverflow
Solution 2 - RedirectOliver CharlesworthView Answer on Stackoverflow
Solution 3 - RedirectZiadView Answer on Stackoverflow
Solution 4 - RedirectHerpes Free EngineerView Answer on Stackoverflow