Valgrind
Table of Contents
Overview
Valgrind is an instrumentation framework for building dynamic analysis tools. There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail.1
Usage
If you don't understand an error message, please consult Explanation of error messages from Memcheck in the Valgrind User Manual which has examples of all the error messages Memcheck produces.
valgrind --tool=toolname program
Replace toolname with the name of the tool you wish to use:
- Memcheck is a memory error detector. It helps you make your programs, particularly those written in C and C++, more correct.
- Cachegrind is a cache and branch-prediction profiler. It helps you make your programs run faster.
- Callgrind is a call-graph generating cache profiler. It has some overlap with Cachegrind, but also gathers some information that Cachegrind does not.
- Helgrind is a thread error detector. It helps you make your multi-threaded programs more correct.
- DRD is also a thread error detector. It is similar to Helgrind but uses different analysis techniques and so may find different problems.
- Massif is a heap profiler. It helps you make your programs use less memory.
- DHAT is a different kind of heap profiler. It helps you understand issues of block lifetimes, block utilisation, and layout inefficiencies.
- SGcheck is an experimental tool that can detect overruns of stack and global arrays. Its functionality is complementary to that of Memcheck: SGcheck finds problems that Memcheck can’t, and vice versa..
- BBV is an experimental SimPoint basic block vector generator. It is useful to people doing computer architecture research and development.
Maunual
/usr/share/doc/valgrind-version/valgrind_manual.pdf
/usr/share/doc/valgrind-version/html/index.html
Profiling Memory Usage with Memcheck
Memcheck is the default Valgrind tool, and can be run with valgrind
program
, without specifying --tool=memcheck
.
valgrind --leak-check=full myprog arg1 arg2
valgrind --leak-check=full --log-file="logfile.out" -v myprog arg1
Some of the options available are:
- –leak-check When enabled, Memcheck searches for memory leaks when the client program finishes. The default value is summary, which outputs the number of leaks found. Other possible values are yes and full, both of which give details of each individual leak, and no, which disables memory leak checking.
- –undef-value-errors When enabled (set to yes), Memcheck reports errors when undefined values are used. When disabled (set to no), undefined value errors are not reported. This is enabled by default. Disabling it speeds up Memcheck slightly.
- –ignore-ranges
Allows the user to specify one or more ranges that Memcheck should
ignore when checking for addressability. Multiple ranges are
delimited by commas, for example,
--ignore-ranges=0xPP-0xQQ,0xRR-0xSS
.
Profiling Cache Usage with Cachegrind
# valgrind --tool=cachegrind program
Cachegrind can gather the following statistics for the entire program, and for each function in the program:
- first-level instruction cache reads (or instructions executed) and read misses, and last-level cache instruction read misses;
- data cache reads (or memory reads), read misses, and last-level cache data read misses;
- data cache writes (or memory writes), write misses, and last-level cache write misses;
- conditional branches executed and mispredicted; and
- indirect branches executed and mispredicted.
his file can be further processed by the accompanying cgannotate tool, like so:
# cg_annotate cachegrind.out.pid
You can also compare the profile files created by Cachegrind to make it simpler to chart program performance before and after a change.
# cg_diff first second
Profiling Heap and Stack Space with Massif
valgrind --tool=massif myprog
Profiling data gathered by Massif is written to a file, which by
default is called massif.out.pid
, where pid is the process ID of the
specified program.
This profiling data can also be graphed with the msprint command, like so:
# ms_print massif.out.pid
This produces a graph showing memory consumption over the program's execution,
massiftool is used to present it in a graphical form, which can be downloaded from http://sourceforge.net/projects/massiftool/. Note that massiftool requries version 4.6 or higher of the Qt library.
Some of the available options are:
- –heap Specifies whether to perform heap profiling. The default value is yes. Heap profiling can be disabled by setting this option to no.
- –heap-admin Specifies the number of bytes per block to use for administration when heap profiling is enabled. The default value is 8 bytes per block.
- –stacks Specifies whether to perform stack profiling. The default value is no (disabled). To enable stack profiling, set this option to yes, but be aware that doing so will greatly slow Massif. Also note that Massif assumes that the main stack has size zero at start-up in order to better indicate the size of the stack portion over which the program being profiled has control.
- –time-unit Specifies the unit of time used for the profiling. There are three valid values for this option: instructions executed (i), the default value, which is useful in most cases; real time (ms, in milliseconds), which can be useful in certain instances; and bytes allocated/deallocated on the heap and/or stack (B), which is useful for very short-run programs, and for testing purposes, because it is the most reproducible across different machines. This option is useful when graphing Massif output with msprint.
Caveats
Memcheck cannot detect every memory error your program has. For example, it can't detect out-of-range reads or writes to arrays that are allocated statically or on the stack. But it should detect many errors that could crash your program (eg. cause a segmentation fault).
Try to make your program so clean that Memcheck reports no errors. Once you achieve this state, it is much easier to see when changes to the program cause Memcheck to report new errors. Experience from several years of Memcheck use shows that it is possible to make even huge programs run Memcheck-clean. For example, large parts of KDE, OpenOffice.org and Firefox are Memcheck-clean, or very close to it.