Download Valgrind Documentation
Transcript
1. Introduction 1.1. An Overview of Valgrind Valgrind is a suite of simulation-based debugging and profiling tools for programs running on Linux (x86, amd64 and ppc32). The system consists of a core, which provides a synthetic CPU in software, and a series 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. A number of useful tools are supplied as standard. In summary, these are: 1. Memcheck detects memory-management problems in your 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: • Use of uninitialised memory • Reading/writing memory after it has been free’d • Reading/writing off the end of malloc’d blocks • Reading/writing inappropriate areas on the stack • Memory leaks -- where pointers to malloc’d blocks are lost forever • Mismatched use of malloc/new/new [] vs free/delete/delete [] • Overlapping src and dst pointers in memcpy() and related functions Problems like these can be difficult to find by other means, often lying undetected for long periods, then causing occasional, difficult-to-diagnose crashes. 2. Cachegrind is a cache profiler. It performs detailed simulation of the I1, D1 and L2 caches in your CPU and so can accurately pinpoint the sources of cache misses in your code. If you desire, it will show the number of cache misses, memory references and instructions accruing to each line of source code, with per-function, per-module and whole-program summaries. If you ask really nicely it will even show counts for each individual machine instruction. On x86 and AMD64, Cachegrind auto-detects your machine’s cache configuration using the CPUID instruction, and so needs no further configuration info, in most cases. Cachegrind is nicely complemented by Josef Weidendorfer’s amazing KCacheGrind visualisation tool (http://kcachegrind.sourceforge.net), a KDE application which presents these profiling results in a graphical and easier-to-understand form. 7