Red Hat Training

A Red Hat training course is available for Red Hat Developer Toolset

Chapter 12. Valgrind

Valgrind is an instrumentation framework that ships with a number of tools for profiling applications. It can be used to detect various memory errors and memory-management problems, such as the use of uninitialized memory or an improper allocation and freeing of memory, or to identify the use of improper arguments in system calls. For a complete list of profiling tools that are distributed with the Red Hat Developer Toolset version of Valgrind, see Table 12.1, “Tools Distributed with Valgrind for Red Hat Developer Toolset”.

Valgrind profiles an application by rewriting it and instrumenting the rewritten binary. This allows you to profile your application without the need to recompile it, but it also makes Valgrind significantly slower than other profilers, especially when performing extremely detailed runs. It is therefore not suited to debugging time-specific issues, or kernel-space debugging.

Red Hat Developer Toolset is distributed with Valgrind 3.13.0. This version is more recent than the version included in the previous release of Red Hat Developer Toolset and provides numerous bug fixes and enhancements.

Table 12.1. Tools Distributed with Valgrind for Red Hat Developer Toolset

NameDescription

Memcheck

Detects memory management problems by intercepting system calls and checking all read and write operations.

Cachegrind

Identifies the sources of cache misses by simulating the level 1 instruction cache (I1), level 1 data cache (D1), and unified level 2 cache (L2).

Callgrind

Generates a call graph representing the function call history.

Helgrind

Detects synchronization errors in multithreaded C, C++, and Fortran programs that use POSIX threading primitives.

DRD

Detects errors in multithreaded C and C++ programs that use POSIX threading primitives or any other threading concepts that are built on top of these POSIX threading primitives.

Massif

Monitors heap and stack usage.

12.1. Installing Valgrind

In Red Hat Developer Toolset, Valgrind is provided by the devtoolset-7-valgrind package and is automatically installed with devtoolset-7-perftools.

For detailed instructions on how to install Red Hat Developer Toolset and related packages to your system, see Section 1.5, “Installing Red Hat Developer Toolset”.

Note

Note that if you use Valgrind in combination with the GNU Debugger, it is recommended that you use the version of GDB that is included in Red Hat Developer Toolset to ensure that all features are fully supported.

12.2. Using Valgrind

To run any of the Valgrind tools on a program you want to profile:

$ scl enable devtoolset-7 'valgrind --tool=tool program argument...'

See Table 12.1, “Tools Distributed with Valgrind for Red Hat Developer Toolset” for a list of tools that are distributed with Valgrind. The argument of the --tool command line option must be specified in lower case, and if this option is omitted, Valgrind uses Memcheck by default. For example, to run Cachegrind on a program to identify the sources of cache misses:

$ scl enable devtoolset-7 'valgrind --tool=cachegrind program argument...'

Note that you can execute any command using the scl utility, causing it to be run with the Red Hat Developer Toolset binaries used in preference to the Red Hat Enterprise Linux system equivalent. This allows you to run a shell session with Red Hat Developer Toolset Valgrind as default:

$ scl enable devtoolset-7 'bash'
Note

To verify the version of Valgrind you are using at any point:

$ which valgrind

Red Hat Developer Toolset’s valgrind executable path will begin with /opt. Alternatively, you can use the following command to confirm that the version number matches that for Red Hat Developer Toolset Valgrind:

$ valgrind --version

12.3. Additional Resources

A detailed description of Valgrind and its features is beyond the scope of this book. For more information, see the resources listed below.

Installed Documentation

  • valgrind(1) — The manual page for the valgrind utility provides detailed information on how to use Valgrind. To display the manual page for the version included in Red Hat Developer Toolset:

    $ scl enable devtoolset-7 'man valgrind'
  • Valgrind DocumentationHTML documentation for Valgrind is located at /opt/rh/devtoolset-7/root/usr/share/doc/devtoolset-7-valgrind-3.13.0/html/index.html.

Online Documentation

See Also

  • Section A.2.10, “Changes in Valgrind” — A comprehensive list of features and improvements over the Red Hat Enterprise Linux version of Valgrind and the version distributed in the previous release of Red Hat Developer Toolset.
  • Chapter 1, Red Hat Developer Toolset — An overview of Red Hat Developer Toolset and more information on how to install it on your system.
  • Chapter 10, memstomp — Instructions on using the memstomp utility to identify calls to library functions with overlapping memory regions that are not allowed by various standards.
  • Chapter 11, SystemTap — An introduction to the SystemTap tool and instructions on how to use it to monitor the activities of a running system.
  • Chapter 13, OProfile — Instructions on using the OProfile tool to determine which sections of code consume the greatest amount of CPU time and why.
  • Chapter 14, Dyninst — Instructions on using the Dyninst library to instrument a user-space executable.