Fundamentals 10 min read

Mastering Valgrind: Installation, Core Tools, and Cross‑Compilation Guide

This article introduces Valgrind, outlines its main debugging tools, provides step‑by‑step installation and usage instructions on Linux, demonstrates memory‑error detection with a sample C program, and explains how to cross‑compile Valgrind for ARM targets.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering Valgrind: Installation, Core Tools, and Cross‑Compilation Guide

What is Valgrind?

Valgrind is an open‑source (GPL‑v2) suite of Linux debugging tools that execute a program on a virtual CPU, allowing detection of memory errors, cache behavior, threading issues, and memory consumption.

Core tools

Memcheck – detects use of uninitialized memory, use‑after‑free, out‑of‑bounds accesses, illegal stack accesses, memory leaks, mismatched allocation/deallocation, and overlapping memcpy destinations.

Cachegrind – simulates L1 I‑cache, L1 D‑cache and L2 cache, reporting hits, misses, memory references and instruction counts per source line, function or module.

Helgrind – applies the Eraser algorithm to find data‑race conditions in multithreaded programs.

Callgrind – records call‑graph information; can be combined with Cachegrind for detailed profiling.

Massif – measures heap and stack memory usage over time, useful for reducing overall memory footprint.

Installation

Check whether Valgrind is already present; if not, install via the system package manager or build from source. sudo apt install valgrind To build from source (example version 3.14.0):

wget http://valgrind.org/downloads/valgrind-3.14.0.tar.bz2
 tar -xjf valgrind-3.14.0.tar.bz2
 cd valgrind-3.14.0
 ./configure
 make
 sudo make install

Basic Memcheck usage

Example program with a heap overflow and a memory leak:

#include <stdlib.h>

void f(void) {
    int *x = malloc(10 * sizeof(int));
    x[10] = 0;   // out‑of‑bounds write
}               // x is never freed → leak

int main(void) {
    f();
    return 0;
}

Compile with debugging symbols and run Memcheck:

gcc -g valgrind_test.c -o valgrind_test
valgrind --tool=memcheck --leak-check=yes ./valgrind_test

Typical output reports an “Invalid write of size 4” at the out‑of‑bounds line and a “definitely lost” leak of 40 bytes, together with the allocation site and process ID.

Leak categories

definitely lost – memory that is certainly leaked and must be freed.

possibly lost – memory that may be leaked.

Cross‑compiling Valgrind for ARM

When targeting an ARM board, the source must be configured for the ARM architecture.

Download the source package (same as above).

Edit configure and replace the line armv7 * ) with armv7 * | arm) to recognise the target.

Generate a Makefile for the target:

./configure --host=arm-linux-gnueabihf \
    CC=/home/LinuxZn/ToolChain/gcc-arm-linux-gnueabihf-6.2.1/bin/arm-linux-gnueabihf-gcc \
    CXX=/home/LinuxZn/ToolChain/gcc-arm-linux-gnueabihf-6.2.1/bin/arm-linux-gnueabihf-g++ \
    --prefix=$PWD/tmp

Build and install:

make
make install

The installation resides in the tmp directory (≈100 MB). Copy this directory to the ARM board.

Run Valgrind on the board, pointing to the temporary installation and setting the library path:

tmp/bin/valgrind --tool=memcheck --leak-check=yes ./valgrind_test
export VALGRIND_LIB="tmp/lib/valgrind"

The VALGRIND_LIB environment variable must be set in each new terminal session so that Valgrind can locate its runtime libraries.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Debugging ToolsMemory Debuggingvalgrindcross-compilation
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.