valgrind
Detect memory leaks and errors
TLDR
Use the (default) Memcheck tool to show a diagnostic of memory usage by program
Use Memcheck to report all possible memory leaks of program in full detail
Use the Cachegrind tool to profile and log CPU cache operations of program
Use the Massif tool to profile and log heap memory and stack usage of program
SYNOPSIS
valgrind [options] program [program-arguments]
PARAMETERS
--tool=toolname
Specifies which Valgrind tool to use (e.g., memcheck, cachegrind, callgrind, helgrind, drd).
--leak-check=summary|full|no
Controls the level of detail in leak reports. summary provides a brief overview, full gives detailed information, and no disables leak checking.
--show-reachable=yes|no
When leak checking is enabled, determines whether or not reachable blocks are shown.
--track-origins=yes|no
Tracks the origin of uninitialized values. Helpful for finding the source of undefined behavior.
--log-file=filename
Specifies the file to which Valgrind's output should be written.
--gen-suppressions=yes|no
Generates suppressions for reported errors. Can be used to ignore known or irrelevant issues.
--suppressions=filename
Specifies a suppression file to use. Allows ignoring known issues by adding a suppression entry.
DESCRIPTION
Valgrind is a powerful suite of tools for debugging and profiling Linux programs. Its most well-known tool, Memcheck, detects memory management problems such as memory leaks, invalid memory accesses (reads and writes), and use of uninitialized memory.
Beyond Memcheck, Valgrind offers other tools like Cachegrind for cache profiling, Callgrind for call graph analysis, Helgrind for thread synchronization error detection, and DRD (Data Race Detector) for finding data races. These tools allow developers to identify and fix a wide range of performance and correctness issues in their code, leading to more robust and efficient software.
Valgrind works by instrumenting the program's code at runtime, providing detailed reports on any detected errors or performance bottlenecks. This makes it an invaluable asset for developers working on complex systems where memory errors can be difficult to track down manually.
CAVEATS
Valgrind slows down program execution significantly due to its instrumentation process. Not suitable for real-time applications during debugging. Also, some system calls or library functions might not be fully supported, leading to false positives or incomplete analysis.
SUPPRESSIONS
Suppressions are used to ignore specific errors reported by Valgrind. This is useful for known issues in system libraries or third-party code that you cannot directly fix. Suppressions can be generated automatically using `--gen-suppressions=yes` and then edited manually to refine the matching criteria.
ENVIRONMENT VARIABLES
Several environment variables can influence Valgrind's behavior, such as VALGRIND_LIB to specify the location of Valgrind's support libraries and VGDB_ERROR to control error handling in conjunction with a debugger like GDB.
HISTORY
Valgrind was initially developed by Julian Seward in 2000. It was conceived to provide a free and open-source alternative to proprietary memory debugging tools. The name 'Valgrind' is derived from Valhalla, the hall of slain warriors in Norse mythology, and Grinder, reflecting its purpose of grinding through code to find errors. Over time, Valgrind has evolved from a memory debugger to a suite of tools supporting various debugging and profiling tasks, becoming an essential tool for software development on Linux and other Unix-like systems.