Valgrind
   HOME

TheInfoList



OR:

Valgrind () is a
programming tool A programming tool or software development tool is a computer program that is used to develop another computer program, usually by helping the developer manage computer files. For example, a programmer may use a tool called a source code editor ...
for memory debugging,
memory leak In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in a way that memory which is no longer needed is not released. A memory leak may also happen when an objec ...
detection, and profiling. Valgrind was originally designed to be a freely licensed memory debugging tool for
Linux Linux ( ) is a family of open source Unix-like operating systems based on the Linux kernel, an kernel (operating system), operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically package manager, pac ...
on
x86 x86 (also known as 80x86 or the 8086 family) is a family of complex instruction set computer (CISC) instruction set architectures initially developed by Intel, based on the 8086 microprocessor and its 8-bit-external-bus variant, the 8088. Th ...
, but has since evolved to become a generic framework for creating dynamic analysis tools such as checkers and profilers.


Overview

Valgrind is in essence a
virtual machine In computing, a virtual machine (VM) is the virtualization or emulator, emulation of a computer system. Virtual machines are based on computer architectures and provide the functionality of a physical computer. Their implementations may involve ...
using
just-in-time compilation In computing, just-in-time (JIT) compilation (also dynamic translation or run-time compilations) is compilation (of computer code) during execution of a program (at run time) rather than before execution. This may consist of source code transl ...
techniques, including dynamic recompilation. Nothing from the original program ever gets run directly on the host processor. Instead, Valgrind first translates the program into a temporary, simpler form called
intermediate representation An intermediate representation (IR) is the data structure or code used internally by a compiler or virtual machine to represent source code. An IR is designed to be conducive to further processing, such as optimization and translation. A "good" ...
(IR), which is a processor-neutral, static single assignment form-based form. After the conversion, a tool (see below) is free to do whatever transformations it would like on the IR, before Valgrind translates the IR back into machine code and lets the host processor run it. Valgrind recompiles
binary code A binary code represents plain text, text, instruction set, computer processor instructions, or any other data using a two-symbol system. The two-symbol system used is often "0" and "1" from the binary number, binary number system. The binary cod ...
to run on host and target (or simulated) CPUs of the same architecture. It also includes a GDB stub to allow debugging of the target program as it runs in Valgrind, with "monitor commands" that allow querying the Valgrind tool for various information. A considerable amount of performance is lost in these transformations (and usually, the code the tool inserts); usually, code run with Valgrind and the "none" tool (which does nothing to the IR) runs at 20% to 25% of the speed of the normal program.


Tools


Memcheck

There are multiple tools included with Valgrind (and several external ones). The default (and most used) tool is ''Memcheck''. Memcheck inserts extra
instrumentation Instrumentation is a collective term for measuring instruments, used for indicating, measuring, and recording physical quantities. It is also a field of study about the art and science about making measurement instruments, involving the related ...
code around almost all instructions, which keeps track of the ''validity'' (all unallocated memory starts as invalid or "undefined", until it is initialized into a deterministic state, possibly from other memory) and ''addressability'' (whether the memory address in question points to an allocated, non-freed memory block), stored in the so-called ''V bits'' and ''A bits'' respectively. As data is moved around or manipulated, the instrumentation code keeps track of the A and V bits, so they are always correct on a single-bit level. In addition, Memcheck replaces the standar
C++ allocators
and C memory allocator with its own implementation, which also includes ''memory guards'' around all allocated blocks (with the A bits set to "invalid"). This feature enables Memcheck to detect
off-by-one error An off-by-one error or off-by-one bug (known by acronyms OBOE, OBOB, OBO and OB1) is a logic error that involves a number that differs from its intended value by 1. An off-by-one error can sometimes appear in a mathematics, mathematical context. ...
s where a program reads or writes outside an allocated block by a small amount. The problems Memcheck can detect and warn about include the following: * Reading uninitialized memory * Reading/writing invalid memory which may be ** memory that has been free'd ** memory outside of malloc'd blocks ** memory below the stack pointer * Use of incorrect parameters for system calls * Unsafe overlapping memory copies with mem* and str* functions *
Memory leak In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in a way that memory which is no longer needed is not released. A memory leak may also happen when an objec ...
s * Mismatched allocations and deallocations which may be ** mixing C and C++ e.g., malloc and delete ** mixing scalar and array e.g., new and delete[] ** sized deallocation not the same size as allocation ** aligned deallocation not the same alignment as allocation * Use of incorrect alignment * Use of realloc with a size of zero The price of this is lost performance. Programs running under Memcheck usually run 20–30 times slower than running outside Valgrind and use more memory (there is a memory penalty per allocation). Thus, few developers run their code under Memcheck (or any other Valgrind tool) all the time. They most commonly use such tools either to trace down some specific bug, or to verify that there are no latent bugs (of the kind Memcheck can detect) in the code.


Core errors

Part of the core of Valgrind always has to perform some checking on
file descriptor In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket. File descriptors typically h ...
s (for instance to prevent the test executable from affecting Valgrind's log file or other output files). Checking can also be done for more general user errors affecting file descriptors. The kinds of errors that are detected are * closing a file descriptor that is not open * file descriptors that are not closed when the test executable exits * use of a file descriptor that was never created or was closed already Starting with Valgrind 3.24 these errors are handled by Valgrind in the same way as other errors. That means that you can generate and use suppressions with them. Valgrind 3.25 added a feature where you can change the behaviour of functions that create file descriptors. The default behaviour is the same as
POSIX The Portable Operating System Interface (POSIX; ) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines application programming interfaces (APIs), along with comm ...
, which will return the lowest available file descriptor, potentially recycling closed file descriptors. There is the risk that the test executable will accidentally and erroneously use such a recycled file descriptor. Valgrind's --modify-fds option changes the behaviour to no longer respect the POSIX standard. Instead it will try to create a new file descriptor for each request.


Other tools

In addition to Memcheck, Valgrind has several other tools: *''None'', runs the code in the virtual machine without performing any analysis and thus has the smallest possible CPU and memory overhead of all tools. Since Valgrind itself provides a trace back from a segmentation fault, the ''none'' tool provides this traceback at minimal overhead. *''Addrcheck'', similar to Memcheck but with much smaller CPU and memory overhead, thus catching fewer types of bugs. Addrcheck has been removed as of version 3.2.0. *''Massif'', a heap profiler. The separate GUI massif-visualizer visualizes output from Massif. *''Helgrind'' and ''DRD'', detect
race condition A race condition or race hazard is the condition of an electronics, software, or other system where the system's substantive behavior is dependent on the sequence or timing of other uncontrollable events, leading to unexpected or inconsistent ...
s in multithreaded code *''Cachegrind'', a cache profiler. The separate GUI KCacheGrind visualizes output from Cachegrind. *''Callgrind'', a
call graph A call graph (also known as a call multigraph) is a control-flow graph, which represents calling relationships between subroutines in a computer program. Each node represents a procedure and each edge ''(f, g)'' indicates that procedure ''f'' c ...
analyzer created by Josef Weidendorfer, added to Valgrind as of version 3.2.0. KCacheGrind can visualize output from Callgrind. *''DHAT'', dynamic heap analysis tool which analyzes how much memory is allocated and for how long, as well as patterns of memory usage. *''exp-bbv'', a performance simulator that extrapolates performance from a small sample set. ''exp-sgcheck'' (named ''exp-ptrcheck'' prior to version 3.7), was removed in version 3.16.0. It was an experimental tool to find stack and global array overrun errors, which Memcheck cannot find. There are also several externally developed tools available. One such tool is ThreadSanitizer, another detector of
race condition A race condition or race hazard is the condition of an electronics, software, or other system where the system's substantive behavior is dependent on the sequence or timing of other uncontrollable events, leading to unexpected or inconsistent ...
s.


Platforms supported

As of version 3.4.0, Valgrind supports
Linux Linux ( ) is a family of open source Unix-like operating systems based on the Linux kernel, an kernel (operating system), operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically package manager, pac ...
on
x86 x86 (also known as 80x86 or the 8086 family) is a family of complex instruction set computer (CISC) instruction set architectures initially developed by Intel, based on the 8086 microprocessor and its 8-bit-external-bus variant, the 8088. Th ...
,
x86-64 x86-64 (also known as x64, x86_64, AMD64, and Intel 64) is a 64-bit extension of the x86 instruction set architecture, instruction set. It was announced in 1999 and first available in the AMD Opteron family in 2003. It introduces two new ope ...
and
PowerPC PowerPC (with the backronym Performance Optimization With Enhanced RISC – Performance Computing, sometimes abbreviated as PPC) is a reduced instruction set computer (RISC) instruction set architecture (ISA) created by the 1991 Apple Inc., App ...
. Support for Linux on
ARMv7 ARM (stylised in lowercase as arm, formerly an acronym for Advanced RISC Machines and originally Acorn RISC Machine) is a family of reduced instruction set computer, RISC instruction set architectures (ISAs) for central processing unit, com ...
(used for example in certain
smartphone A smartphone is a mobile phone with advanced computing capabilities. It typically has a touchscreen interface, allowing users to access a wide range of applications and services, such as web browsing, email, and social media, as well as multi ...
s) was added in version 3.6.0. Support for Solaris was added in version 3.11.0. Support for was added in version 3.5.0. Support for
FreeBSD FreeBSD is a free-software Unix-like operating system descended from the Berkeley Software Distribution (BSD). The first version was released in 1993 developed from 386BSD, one of the first fully functional and free Unix clones on affordable ...
x86 and amd64 was added in version 3.18.0. Support for FreeBSD aarch64 was added in version 3.23.0. There are unofficial ports to other
Unix-like A Unix-like (sometimes referred to as UN*X, *nix or *NIX) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Uni ...
platforms (like
OpenBSD OpenBSD is a security-focused operating system, security-focused, free software, Unix-like operating system based on the Berkeley Software Distribution (BSD). Theo de Raadt created OpenBSD in 1995 by fork (software development), forking NetBSD ...
,
NetBSD NetBSD is a free and open-source Unix-like operating system based on the Berkeley Software Distribution (BSD). It was the first open-source BSD descendant officially released after 386BSD was fork (software development), forked. It continues to ...
and QNX). From version 3.7.0 the ARM/ Android platform support was added. Since version 3.9.0 there is support for Linux on MIPS64 little and big endian, for MIPS DSP ASE on MIPS32, for
s390x S39 may refer to: Aviation * Blériot-SPAD S.39, a French reconnaissance aircraft * Letov Š-39, a Czechoslovakian sport aircraft * Prineville Airport, in Crook County, Oregon, United States * Sikorsky S-39, an American flying boat Other us ...
Decimal Floating Point instructions, for
POWER8 POWER8 is a family of superscalar multi-core microprocessors based on the Power ISA, announced in August 2013 at the Hot Chips conference. The designs are available for licensing under the OpenPOWER Foundation, which is the first time for suc ...
( Power ISA 2.07) instructions, for Intel
AVX2 Advanced Vector Extensions (AVX, also known as Gesher New Instructions and then Sandy Bridge New Instructions) are SIMD extensions to the x86 instruction set architecture for microprocessors from Intel and Advanced Micro Devices (AMD). They w ...
instructions, for Intel Transactional Synchronization Extensions, both RTM and HLE and initial support for Hardware Transactional Memory on POWER.
RISC-V RISC-V (pronounced "risk-five") is an open standard instruction set architecture (ISA) based on established reduced instruction set computer (RISC) principles. The project commenced in 2010 at the University of California, Berkeley. It transfer ...
since version 3.25.0.


History and development

The name Valgrind refers to the main entrance to
Valhalla In Norse mythology, Valhalla ( , ; , )Orchard (1997:171–172) is described as a majestic hall located in Asgard and presided over by the god Odin. There were five possible realms the soul could travel to after death. The first was Fólkvang ...
in
Norse mythology Norse, Nordic, or Scandinavian mythology, is the body of myths belonging to the North Germanic peoples, stemming from Old Norse religion and continuing after the Christianization of Scandinavia as the Nordic folklore of the modern period. The ...
. During development (before release) the project was named
Heimdall In Norse mythology, Heimdall (from Old Norse Heimdallr; modern Icelandic language, Icelandic Heimdallur) is a Æsir, god. He is the son of Odin and nine mothers. Heimdall keeps watch for invaders and the onset of Ragnarök from his dwelling Himi ...
; however, the name would have conflicted with a security package. The original author of Valgrind is Julian Seward, who in 2006 won a Google-O'Reilly Open Source Award for his work on Valgrind. Several others have also made significant contributions, including Nicholas Nethercote, Bart Van Assche, Florian Krohm, Tom Hughes, Philippe Waroquiers, Mark Wielaard, Paul Floyd, Petar Jovanovic, Carl Love, Cerion Armour-Brown and Ivo Raisr. It is used by a number of Linux-based projects.


Limitations of Memcheck

In addition to the performance penalty, an important limitation of Memcheck is its inability to detect all cases of bounds errors in the use of static or stack-allocated data.Valgrind FAQ
/ref> The following code will pass the ''Memcheck'' tool in Valgrind without incident, despite containing the errors described in the comments: int Static int func(void) The inability to detect all errors involving the access of stack allocated data is especially noteworthy since certain types of stack errors make software vulnerable to the classic stack smashing exploit.


See also

* *
Dynamic program analysis Dynamics (from Greek δυναμικός ''dynamikos'' "powerful", from δύναμις ''dynamis'' " power") or dynamic may refer to: Physics and engineering * Dynamics (mechanics), the study of forces and their effect on motion Brands and en ...
* Pin * DynamoRIO * VOGL * Libumem * AddressSanitizer


Notes


References

* * *


External links

* {{Linux Debuggers Free memory debuggers Free memory management software Free software testing tools Profilers Software testing tools