CRC Wiki
CRC Wiki
Log in

Valgrind

From CRC Wiki

General Description

"Valgrind is an instrumentation framework for building dynamic analysis tools. There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail. You can also use Valgrind to build new tools." -- from valgrind.org

Basic Usage

To use Valgrind, the binary must be compiled with the debug output turned on, e.g.

gcc -g example.c -o myprogram

To run your program through the debugger with basic memory checking:

valgrind <path to file>/myprogram

This will produce output that should look something like the following:

Valgrind-no-op-01.png

Here we see the errors caused by attempting to acces beyond the bounds of our array.

Valgrind-no-op-02.png

Specifically, the debugger identifies what type of error(green), where the error occured(orange) and where the memory was originally allocated(blue).

Valgrind-no-op-03.png

At the end of the output, we see a summary of the heap memory used during execution. For example we can tell total amounts of memory allocated or lost. To see a more comprehensive output for memory, we can run the debugger this way:

valgrind --leak-check=full <path to file>/myprogram

Which should produce output like the following:

Valgrind-leak-check-full-01.png

As we can see the report on the heap memory is a bit longer.

Valgrind-leak-check-full-02.png

The debugger now reports exactly how much memory has been lost, along with where in the program the memory was originally allocated.

To debug MPI code with memory checking:

mpirun -np <num processes> valgrind --leak-check=full <path to file>/filename <args>

What has been demonstrated is only a small part of what Valgrind can do. Another tool, for example, allows examination of cache performance. If you are interested in more, please see the documentation located on the official website.

To see examples of Valgrind submission scripts please follow this link:

Additional Information

For multi process code you need to load the GNU version of Valgrind:

module load valgrind/3.6.1-gnu

This will work with code compiled with either the Intel or GNU compilers.

Additional Files

Source for the above serial examples may be found here.

Source for the MPI examples may be found here.

Video icon.jpeg Valgrind Demo

Further Information

See the official website: Valgrind

The User Guide, v.3.6.1, may be found here