167

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?

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
Dinesh
  • 16,014
  • 23
  • 80
  • 122

4 Answers4

456
valgrind --log-file="filename"
fancyPants
  • 50,732
  • 33
  • 89
  • 96
Vasileios Lekakis
  • 5,492
  • 2
  • 15
  • 17
  • 4
    this saves just stderr, is it possible to save both stderr and stdout into same file in the same order as is written in terminal ( i.e. to keep coherence between outputs of tested program and error reported by valgrind) ? – Prokop Hapala Jan 20 '19 at 11:14
90

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).

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • 5
    Thanks a lot :). It worked. Can you please tell me what's with that "2>&1"? – Dinesh Dec 02 '11 at 12:05
  • 11
    @Dinesh: I suggest reading http://www.gnu.org/software/bash/manual/bashref.html#Redirections, which describes the bizarre Bash syntax for doing redirections! – Oliver Charlesworth Dec 02 '11 at 12:08
  • 17
    NOTE: that this suggestion will also send `a.out`'s output to the same log file. If you want to save valgrind's output to the logfile *without* `a.out`'s, you should use the `--log-file` option suggested by Lex. – edam Oct 01 '13 at 13:34
  • This is also a great answer for debugging memory leaks! – Free Url Nov 25 '17 at 00:26
  • If you want to pipe output to another command: `valgrind ./struct -v --leak-check=full 2>&1 | nc -N paste.ubuntu.ir 1337` – EsmaeelE Feb 10 '21 at 00:13
12

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
Ziad
  • 419
  • 3
  • 7
  • 15
5

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>
Herpes Free Engineer
  • 2,425
  • 2
  • 27
  • 34