Introduction to Valgrind

Published:
1 minute read

What is Valgrind

It is a robust tool to check C/C++ programs dynamically for various bugs and errors.

Installation and Usage on Ubuntu

sudo apt-get update
sudo apt-get install valgrind

As it is stated in its website, if a program runs like this:

program arg1 arg2

It can be checked like this:

valgrind --leak-check=yes program arg1 arg2

A more complete way to use valgrind is:

export G_SLICE=always-malloc
export G_DEBUG=gc-friendly
valgrind --leak-check=full --tool=memcheck --show-leak-kinds=all --track-origins=yes \
  --num-callers=40 --log-file=valgrind.log $(which <program>) <arguments>

The valgrind website: here A complete manual can be found here

Visualize the call graph with kcachegrind

Install kcachegrind on Ubuntu or Windows WSL:

sudo apt update
sudo apt install kcachegrind

First, collect data with valgrind; then kacachegrind can open the output profile results:

$ valgrind --tool=callgrind --trace-children=yes -v ./application
$ kcachegrind &
# Open <callgrind.out.xxx> from the GUI
# To open graphical apps with GUI on WSL you need a running X server.

Kcachegrind can be used in other ways as well. Check here.

Build and installation on Windows

  • Remember that Valgrind is OS-specific and cannot be installed and run on Windows. However, with Linux subsystem there can be something done.

  • First you need to install Windows Subsystem for Linux

After completing the previous installation and configuration, follow these steps:

  1. Download Valgrind source from here
  2. Install building tools and requirements by:
sudo apt-get install automake
sudo apt-get install build-essential
  1. Go to Valgrind directory and run these commands (if make fails, run it with sudo):
    • Be patient! They will take a while.
./configure
make
make install
  1. (Optional) To clean temporary files:
make clean
make distclean

Valgrind should be ready to use!