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.14.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
Detects memory management problems by intercepting system calls and checking all read and write operations.
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).
Generates a call graph representing the function call history.
Detects synchronization errors in multithreaded C, C++, and Fortran programs that use POSIX threading primitives.
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.
Monitors heap and stack usage.
12.1. Installing Valgrind
In Red Hat Developer Toolset, Valgrind is provided by the devtoolset-8-valgrind package and is automatically installed with devtoolset-8-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.
12.2. Using Valgrind
To run any of the Valgrind tools on a program you want to profile:
scl enable devtoolset-8 '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-8 '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-8 'bash'
To verify the version of Valgrind you are using at any point:
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:
12.3. Additional Resources
For more information about Valgrind and its features, see the resources listed below.
valgrind(1) — The manual page for the
valgrindutility 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-8 'man valgrind'
Valgrind Documentation — HTML documentation for Valgrind is located at
- Red Hat Enterprise Linux 6 Developer Guide and Red Hat Enterprise Linux 7 Developer Guide — The Developer Guides for Red Hat Enterprise Linux 6 and 7 provide more information about Valgrind and its Eclipse plug-in.
- Red Hat Enterprise Linux 6 Performance Tuning Guide and Red Hat Enterprise Linux 7 Performance Tuning Guide — The Performance Tuning Guides for Red Hat Enterprise Linux 6 and 7 provide more detailed information about using Valgrind to profile applications.
- 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.