Project

General

Profile

Getting started with valgrind

Overview.

Valgrind (val-grinned) is a programmer's tool for investigating many problems with programs, including:
  • Memory-related errors (leaks, buffer overruns, unintialized variables, use-after-free, double-free, _etc.).
  • Memory use over a program's lifetime.
  • Memory access patterns (cache use, etc).
  • Issues with parallelism.

You are most likely to need it for finding memory-related errors.

Setup.

ups list -aK+ valgrind
setup valgrind <version>

Simple invocation.

valgrind --tool=memcheck --leak-check=no --track-origins=yes --num-callers=50 --log-file=valgrind.out --suppressions=${ROOTSYS}/etc/valgrind-root.supp <cmd> <args>...

Breakdown of options.

  • --tool=memcheck
    Choose the valgrind tool to use. Other options are (e.g.) cachegrind, callgrind, helgrind, drd, massif, lackey, none, exp-sgcheck, exp-bbv, exp-dhat. See the valgrind manual for full details.
  • --leak-check=no
    Deactivate leak-checking, which is expensive. Remove this option if you actually need to look for leaks, but the program run will take significantly longer to complete.
  • --track-origins=yes
    Where applicable, show the origin of the problem, not just where the final effects were felt (e.g. Definition and free locations of a variable used after being freed).
  • --num-callers=50
    Stack depth.
  • --log-file=valgrind.out
    Send valgrind-specific output to the specified file, leaving program-specific output ot go to its original destination.
  • --suppressions=${ROOTSYS}/etc/valgrind-root.supp
    Some programs generate valgrind problem reports that the program author has been determined to not be an issue. This option loads a file describing such "non-problems" so they may be ignored by valgrind.

~/.valgrindrc.

Default options may be placed in a file ~/.valgrindrc. Options specific to a particular tool may be tagged for that tool. Note that environment variables are not iterpreted from this file, so its utility for specifying e.g. suppression files is somewhat limited.

Example ~/.valgrindrc file.

--memcheck:leak-check=no
--memcheck:num-callers=50
--memcheck:track-origins=yes
--log-file=valgrind.out

Other uses and information sources.

Valgrind has many more uses than have been presented here. See:

  • The valgrind manual
  • valgrind --help
  • ${VALGRIND_DIR}/share/man/man1
    for more details.