Free and Open Source Debugger - Valgrind

Valgrind is a suite of open source debugging and profiling tools designed to test analyze programs and memory usages. The set of tools which it provides allows to detects memory-management problems, cache profiling, memory heap profiling, thread debugging and error detection in multithreaded programs of C/C++. Valgrind was initially released as a free memory debugging tool for Linux on x86, but now has become a generic framework for program debugging and memory leaks identification. It also allows creating debugging tools such as checkers and profilers. It works on major operating systems such as Linux, Solaris, Android, and macOS.

Valgrind is managed and released by The Valgrind Developers. Its latest stable version, at the time of this writing, is Valgrind 3.12.0 and was released in October 2016. It is freely available under the GNU General Public License, version 2.

It is virtual machine using just-in-time (JIT) compilation techniques, including dynamic recompilation. It first translates the program into simpler form called Intermediate Representation (IR) after which, any of its tool can convert the IR as required before Valgrind translates the IR back into machine code to be executed. GDB can also be included in Valgrind to debug and query the debugging of a program.

Valgrind includes variety of tools for memory management and program debugging purposes. The important tools of Valgrind suite are:

Memcheck

It is a memory management program primary designed for C/C++ programs. It supervises all memory read/writes and intercepts calls to malloc, new, free, and delete. It detects illegal access to memory, unacceptable use of uninitialized variables, memory leaks, bad freeing of heaps, etc. It reports these errors, on their occurrence, with their source line number and also a stack trace of the functions called to reach that line. A program monitored under Memcheck runs 10 to 30 times slower compare to the normal speed.

Cachegrind

It is a cache profiler program that can work with any programming language. It performs detailed simulation of the L1, D1 and L2 caches of CPU and accurately identifies the exact locations of cache misses in the code. It identifies the number of memory references and instructions executed for each line of source code, cache misses, for per-function, per-module and whole-program summaries. A program monitored under Cachegrind runs 20 to 100 times slower compare to the normal speed.

Callgrind

It is a cache profiler program similar to Cachegrind but provide additional information about callgraphs. It was developed by Josef Weidendorfer and added in Valgrind distribution 3.2.0. It can also be used to visualize Cachegrind's output.

Massif

It is a heap profiler program that performs detailed heap profiling by capturing regular snapshots of a program's heap. The heap usage over time is shown using a graph, including information about which parts of the program are responsible for the most memory allocations. Further, a text of html file provides additional information about the heap profile. A program monitored under Massif runs 20 times slower compare to the normal speed.

Helgrind

It is a thread debugger program designed to find data races in multithreaded programs and can detect locking order violations between threads. It checks memory locations which are accessed by more than one (POSIX p-) thread, but for which no consistently used (pthread_mutex_) lock can be found. This could be because of lack of synchronization between threads, and could lead to complex timing-dependent problems.

DRD

It detects errors in multithreaded C and C++ programs. The tool also works for other programming languages that uses the POSIX threading primitives or that uses threading concepts built on top of the POSIX threading primitives. It requires less memory to perform its analysis compare to Helgrind but cannot identify locking order violations.


More on Free and Open Source software, their fundamentals, philosophy, development models and business models in ...


To Order this book click here