njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 1 | <?xml version="1.0"?> <!-- -*- sgml -*- --> |
| 2 | <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" |
| 3 | "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"> |
| 4 | |
| 5 | <chapter id="manual-intro" xreflabel="Introduction"> |
| 6 | <title>Introduction</title> |
| 7 | |
| 8 | <sect1 id="manual-intro.overview" xreflabel="An Overview of Valgrind"> |
| 9 | <title>An Overview of Valgrind</title> |
| 10 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame^] | 11 | <para>Valgrind is a suite of simulation-based debugging and profiling |
| 12 | tools for programs running on Linux (x86, amd64 and ppc32). The system |
| 13 | consists of a core, which provides a synthetic CPU in software, and a |
| 14 | series of tools, each of which performs some kind of debugging, |
| 15 | profiling, or similar task. The architecture is modular, so that new |
| 16 | tools can be created easily and without disturbing the existing |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 17 | structure.</para> |
| 18 | |
| 19 | <para>A number of useful tools are supplied as standard. In |
| 20 | summary, these are:</para> |
| 21 | |
| 22 | <orderedlist> |
| 23 | |
| 24 | <listitem> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame^] | 25 | <para><command>Memcheck</command> detects memory-management problems |
| 26 | in your programs. All reads and writes of memory are checked, and |
| 27 | calls to malloc/new/free/delete are intercepted. As a result, |
| 28 | Memcheck can detect the following problems:</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 29 | |
| 30 | <itemizedlist> |
| 31 | <listitem> |
| 32 | <para>Use of uninitialised memory</para> |
| 33 | </listitem> |
| 34 | <listitem> |
| 35 | <para>Reading/writing memory after it has been |
| 36 | free'd</para> |
| 37 | </listitem> |
| 38 | <listitem> |
| 39 | <para>Reading/writing off the end of malloc'd |
| 40 | blocks</para> |
| 41 | </listitem> |
| 42 | <listitem> |
| 43 | <para>Reading/writing inappropriate areas on the |
| 44 | stack</para> |
| 45 | </listitem> |
| 46 | <listitem> |
| 47 | <para>Memory leaks -- where pointers to malloc'd |
| 48 | blocks are lost forever</para> |
| 49 | </listitem> |
| 50 | <listitem> |
| 51 | <para>Mismatched use of malloc/new/new [] vs |
| 52 | free/delete/delete []</para> |
| 53 | </listitem> |
| 54 | <listitem> |
| 55 | <para>Overlapping <computeroutput>src</computeroutput> and |
| 56 | <computeroutput>dst</computeroutput> pointers in |
| 57 | <computeroutput>memcpy()</computeroutput> and related |
sewardj | 053fe98 | 2005-11-15 19:51:04 +0000 | [diff] [blame] | 58 | functions</para></listitem> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 59 | </itemizedlist> |
| 60 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame^] | 61 | <para>Problems like these can be difficult to find by other means, |
| 62 | often lying undetected for long periods, then causing occasional, |
| 63 | difficult-to-diagnose crashes.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 64 | </listitem> |
| 65 | |
| 66 | <listitem> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame^] | 67 | <para><command>Addrcheck</command> is a lightweight version of |
| 68 | Memcheck. It is identical to Memcheck except for the single detail |
| 69 | that it does not do any uninitialised-value checks. All of the |
| 70 | other checks -- primarily the fine-grained address checking -- are |
| 71 | still done. The downside of this is that you don't catch the |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 72 | uninitialised-value errors that Memcheck can find.</para> |
| 73 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame^] | 74 | <para>But the upside is significant: programs run about twice as |
| 75 | fast as they do on Memcheck, and a lot less memory is used. It |
| 76 | still finds reads/writes of freed memory, memory off the end of |
| 77 | blocks and in other invalid places, bugs which you really want to |
| 78 | find before release!</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 79 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame^] | 80 | <para>Because Addrcheck is lighter and faster than Memcheck, you can |
| 81 | run more programs for longer, and so you may be able to cover more |
| 82 | test scenarios. Addrcheck was created because one of us (Julian) |
| 83 | wanted to be able to run a complete KDE desktop session with |
| 84 | checking. As of early November 2002, we have been able to run |
| 85 | KDE-3.0.3 on a 1.7 GHz P4 with 512 MB of memory, using Addrcheck. |
| 86 | Although the result is not stellar, it's quite usable, and it seems |
| 87 | plausible to run KDE for long periods at a time like this, |
| 88 | collecting up all the addressing errors that appear.</para> |
sewardj | 053fe98 | 2005-11-15 19:51:04 +0000 | [diff] [blame] | 89 | |
| 90 | <para>NOTE: Addrcheck is not available in Valgrind 3.1.X. We hope |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame^] | 91 | to reinstate its functionality in later releases. For now, use |
sewardj | 053fe98 | 2005-11-15 19:51:04 +0000 | [diff] [blame] | 92 | Memcheck instead.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 93 | </listitem> |
| 94 | |
| 95 | <listitem> |
| 96 | <para><command>Cachegrind</command> is a cache profiler. It |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame^] | 97 | performs detailed simulation of the I1, D1 and L2 caches in your CPU |
| 98 | and so can accurately pinpoint the sources of cache misses in your |
| 99 | code. If you desire, it will show the number of cache misses, |
| 100 | memory references and instructions accruing to each line of source |
| 101 | code, with per-function, per-module and whole-program summaries. If |
| 102 | you ask really nicely it will even show counts for each individual |
| 103 | machine instruction.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 104 | |
njn | 779a2d6 | 2005-07-25 00:12:19 +0000 | [diff] [blame] | 105 | <para>On x86 and AMD64, Cachegrind auto-detects your machine's cache |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame^] | 106 | configuration using the <computeroutput>CPUID</computeroutput> |
| 107 | instruction, and so needs no further configuration info, in most |
| 108 | cases.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 109 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame^] | 110 | <para>Cachegrind is nicely complemented by Josef Weidendorfer's |
| 111 | amazing KCacheGrind visualisation tool |
de | 252c614 | 2005-11-27 04:10:00 +0000 | [diff] [blame] | 112 | (<ulink url="http://kcachegrind.sourceforge.net/cgi-bin/show.cgi/KcacheGrindIndex">http://kcachegrind.sourceforge.net</ulink>), |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 113 | a KDE application which presents these profiling results in a |
| 114 | graphical and easier-to-understand form.</para> |
| 115 | </listitem> |
| 116 | |
| 117 | <listitem> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame^] | 118 | <para><command>Helgrind</command> finds data races in multithreaded |
| 119 | programs. Helgrind looks for memory locations which are accessed by |
| 120 | more than one (POSIX p-)thread, but for which no consistently used |
| 121 | (pthread_mutex_)lock can be found. Such locations are indicative of |
| 122 | missing synchronisation between threads, and could cause |
| 123 | hard-to-find timing-dependent problems.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 124 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame^] | 125 | <para>Helgrind ("Hell's Gate", in Norse mythology) implements the |
| 126 | so-called "Eraser" data-race-detection algorithm, along with various |
| 127 | refinements (thread-segment lifetimes) which reduce the number of |
| 128 | false errors it reports. It is as yet somewhat of an experimental |
| 129 | tool, so your feedback is especially welcomed here.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 130 | |
| 131 | <para>Helgrind has been hacked on extensively by Jeremy |
| 132 | Fitzhardinge, and we have him to thank for getting it to a |
| 133 | releasable state.</para> |
sewardj | 053fe98 | 2005-11-15 19:51:04 +0000 | [diff] [blame] | 134 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame^] | 135 | <para>NOTE: Helgrind is, unfortunately, not available in Valgrind |
| 136 | 3.1.X, as a result of threading changes that happened in the 2.4.0 |
| 137 | release. We hope to reinstate its functionality in a future 3.2.0 |
| 138 | release.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 139 | </listitem> |
| 140 | |
| 141 | </orderedlist> |
| 142 | |
| 143 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame^] | 144 | <para>A couple of minor tools (<command>Lackey</command> and |
| 145 | <command>Nulgrind</command>) are also supplied. These aren't |
| 146 | particularly useful -- they exist to illustrate how to create simple |
| 147 | tools and to help the valgrind developers in various ways. Nulgrind is |
| 148 | the null tool -- it adds no instrumentation. Lackey is a simple example |
| 149 | tool which counts instructions, memory accesses, and the number of |
sewardj | 053fe98 | 2005-11-15 19:51:04 +0000 | [diff] [blame] | 150 | integer and floating point operations your program does.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 151 | |
njn | 779a2d6 | 2005-07-25 00:12:19 +0000 | [diff] [blame] | 152 | <para>Valgrind is closely tied to details of the CPU and operating |
| 153 | system, and to a lesser extent, the compiler and basic C libraries. |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame^] | 154 | Nonetheless, as of version 3.1.0 it supports several platforms: |
| 155 | x86/Linux (mature), AMD64/Linux (maturing), and PPC32/Linux (immature |
| 156 | but works well). Valgrind uses the standard Unix |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 157 | <computeroutput>./configure</computeroutput>, |
| 158 | <computeroutput>make</computeroutput>, <computeroutput>make |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame^] | 159 | install</computeroutput> mechanism, and we have attempted to ensure that |
| 160 | it works on machines with kernel 2.4 or 2.6 and glibc |
sewardj | 053fe98 | 2005-11-15 19:51:04 +0000 | [diff] [blame] | 161 | 2.2.X--2.3.X.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 162 | |
| 163 | <para>Valgrind is licensed under the <xref linkend="license.gpl"/>, |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame^] | 164 | version 2. The <computeroutput>valgrind/*.h</computeroutput> headers |
| 165 | that you may wish to include in your code (eg. |
| 166 | <filename>valgrind.h</filename>, <filename>memcheck.h</filename>) are |
| 167 | distributed under a BSD-style license, so you may include them in your |
| 168 | code without worrying about license conflicts. Some of the PThreads |
| 169 | test cases, <filename>pth_*.c</filename>, are taken from "Pthreads |
| 170 | Programming" by Bradford Nichols, Dick Buttlar & Jacqueline Proulx |
| 171 | Farrell, ISBN 1-56592-115-1, published by O'Reilly & Associates, |
| 172 | Inc.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 173 | |
| 174 | </sect1> |
| 175 | |
| 176 | |
| 177 | <sect1 id="manual-intro.navigation" xreflabel="How to navigate this manual"> |
| 178 | <title>How to navigate this manual</title> |
| 179 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame^] | 180 | <para>The Valgrind distribution consists of the Valgrind core, upon |
| 181 | which are built Valgrind tools, which do different kinds of debugging |
| 182 | and profiling. This manual is structured similarly.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 183 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame^] | 184 | <para>First, we describe the Valgrind core, how to use it, and the flags |
| 185 | it supports. Then, each tool has its own chapter in this manual. You |
| 186 | only need to read the documentation for the core and for the tool(s) you |
| 187 | actually use, although you may find it helpful to be at least a little |
| 188 | bit familar with what all tools do. If you're new to all this, you |
| 189 | probably want to run the Memcheck tool. If you want to write a new |
| 190 | tool, read <xref linkend="writing-tools"/>.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 191 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame^] | 192 | <para>Be aware that the core understands some command line flags, and |
| 193 | the tools have their own flags which they know about. This means there |
| 194 | is no central place describing all the flags that are accepted -- you |
| 195 | have to read the flags documentation both for |
| 196 | <xref linkend="manual-core"/> and for the tool you want to use.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 197 | |
| 198 | </sect1> |
| 199 | |
| 200 | </chapter> |