Chapter 13. 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 13.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.15.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.
Name | Description |
---|---|
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. |
13.1. Installing Valgrind
In Red Hat Developer Toolset, Valgrind is provided by the devtoolset-9-valgrind package and is automatically installed with devtoolset-9-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 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.
13.2. Using Valgrind
To run any of the Valgrind tools on a program you want to profile:
$ scl enable devtoolset-9 'valgrind --tool=tool program argument...'
See Table 13.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-9 '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-9 'bash'
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
13.3. Additional Resources
For more information about Valgrind and its features, 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-9 'man valgrind'
-
Valgrind Documentation — HTML documentation for Valgrind is located at
/opt/rh/devtoolset-9/root/usr/share/doc/devtoolset-9-valgrind-3.15.0/html/index.html
.
Online Documentation
- Red Hat Enterprise Linux 7 Developer Guide — The Developer Guide for Red Hat Enterprise Linux 7 provides more information about Valgrind and its Eclipse plug-in.
- Red Hat Enterprise Linux 7 Performance Tuning Guide — The Performance Tuning Guide for Red Hat Enterprise Linux 7 provide more detailed information about using Valgrind to profile applications.
See Also
- 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 11, 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 12, SystemTap — An introduction to the SystemTap tool and instructions on how to use it to monitor the activities of a running system.
- Chapter 14, OProfile — Instructions on using the OProfile tool to determine which sections of code consume the greatest amount of CPU time and why.
- Chapter 15, Dyninst — Instructions on using the Dyninst library to instrument a user-space executable.