Valgrind
From CS371p
Valgrind is a suite of simulation-based debugging and profiling tools for programs running on Linux (x86, amd64, ppc32 and ppc64).
The system consists of a core, which provides a synthetic CPU in software, and a set of tools, each of which performs some kind of debugging, profiling, or similar task. The architecture is modular, so that new tools can be created easily and without disturbing the existing structure.
The name Valgrind comes from the name of the main entrance to Valhalla in Norse mythology.
Contents |
What Valgrind Does
Valgrind is designed to be as non-intrusive as possible. It works directly with existing executables, not needing to recompile, relink, or otherwise modify, the program to be checked.
Regardless of which tool is in use, Valgrind takes control of your program before it starts. Debugging information is read from the executable and associated libraries, so that error messages and other outputs can be phrased in terms of source code locations, when appropriate.
The program is then run on a synthetic CPU provided by the Valgrind core. As new code is executed for the first time, the core hands the code to the selected tool. The tool adds its own instrumentation code to this and hands the result back to the core, which coordinates the continued execution of this instrumented code.
Valgrind simulates every single instruction your program executes. Because of this, the active tool checks, or profiles, not only the code in the application but also in all supporting dynamically-linked (.so-format) libraries.
Tools
The Valgrind tool suite provides a number of debugging and profiling tools, including the most popular Memcheck.
Memcheck
Memcheck detects memory-management problems in programs. All reads and writes of memory are checked, and calls to malloc/new/free/delete are intercepted. As a result, Memcheck can detect the following problems:
- Touching memory you shouldn't (eg. overrunning heap block boundaries, or reading/writing freed memory).
- Using values before they have been initialized.
- Incorrect freeing of memory, such as double-freeing heap blocks.
- Memory leaks.
Problems like these can be difficult to find by other means, often remaining undetected for long periods, then causing occasional, difficult-to-diagnose crashes.
Other Tools
Cachegrind: a profiling tool which produces detailed data on cache (miss) and branch (misprediction) events. Statistics are gathered for the entire program, for each function, and for each line of code, if you need that level of detail.
Callgrind: a profiling tool that shows cost relationships across function calls, optionally with cache simulation similar to Cachegrind. Information gathered by Callgrind can be viewed either with an included command line tool, or by using the KCachegrind GUI. KCachegrind is not part of the Valgrind suite -- it is part of the KDE Desktop Environment.
Massif: a space profiling tool. It allows you to explore in detail which parts of your program allocate memory.
Helgrind: a debugging tool for threaded programs. Helgrind looks for various kinds of synchronisation errors in code that uses the POSIX PThreads API.
In addition, there are a number of "experimental" tools in the codebase. They can be distinguished by the "exp-" prefix on their names. Experimental tools are not subject to the same quality control standards that apply to our production-grade tools (Memcheck, Cachegrind, Callgrind, Massif and Helgrind).
Installation
Download valgrind 3.3.0 (tar.bz2) and extract it.
- Run ./configure, with some options if you wish. The standard options are documented in the INSTALL file. The only interesting one is the usual --prefix=/where/you/want/it/installed.
- Do "make".
- Do "make install", possibly as root if the destination permissions require that.
- See if it works. Try "valgrind ls -l". Either this works, or it bombs out with some complaint. In that case, contact Valgrind(see www.valgrind.org).
Important! Do not move the valgrind installation into a place different from that specified by --prefix at build time. This will cause things to break in subtle ways, mostly when Valgrind handles fork/exec calls.
How to use Valgrind
Preparing your Program
Compile your program with -g to include debuggin information so that exact line number will be included in the error message.
Running your Program
Normal execution command is:
myprog arg1 arg2
Instead, use:
valgrind --tool=tool_name --option=<your_choice> myprog arg1 arg2
For example, to uses Memcheck and turn on leak-check option:
valgrind --tool=memcheck --leak-check=yes myprog arg1 arg2
Output
Upon succesful execution, following commentary is outputed.
==12345== some-message-from-Valgrind
where 12345 is the process ID. Any error messages would be added onto the commentary.
References
[1] http://www.valgrind.org
[2] Wikipedia page on Valgrind
Written by David Chung
