blob: fbb1cd6d0990b0b9a0982c029996c45495008ecd [file] [log] [blame]
njn76c5bfa2005-03-11 04:33:29 +00001<?xml version="1.0"?> <!-- -*- sgml -*- -->
2<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"
4[ <!ENTITY % vg-entities SYSTEM "vg-entities.xml"> %vg-entities; ]>
5
de252c6142005-11-27 04:10:00 +00006<book id="QuickStart" xreflabel="Valgrind Quick Start Guide">
njn76c5bfa2005-03-11 04:33:29 +00007
de53ad6842005-11-19 03:28:10 +00008<bookinfo>
de9bec93c2005-11-25 05:36:48 +00009 <title>The Valgrind Quick Start Guide</title>
10 <releaseinfo>&rel-type; &rel-version; &rel-date;</releaseinfo>
11 <copyright>
12 <year>&vg-lifespan;</year>
13 <holder><ulink url="&vg-developers;">Valgrind Developers</ulink></holder>
14 </copyright>
debad57fc2005-12-03 22:33:29 +000015 <legalnotice>
16 <para>Email: <ulink url="mailto:&vg-vemail;">&vg-vemail;</ulink></para>
17 </legalnotice>
de53ad6842005-11-19 03:28:10 +000018</bookinfo>
njn76c5bfa2005-03-11 04:33:29 +000019
20
de252c6142005-11-27 04:10:00 +000021<article id="quick-start">
de9bec93c2005-11-25 05:36:48 +000022<title>The Valgrind Quick Start Guide</title>
23
24
25<sect1 id="quick-start.intro" xreflabel="Introduction">
26<title>Introduction</title>
njn76c5bfa2005-03-11 04:33:29 +000027
sewardj08e31e22007-05-23 21:58:33 +000028<para>The Valgrind tool suite provides a number of debugging and
29profiling tools. The most popular is
30Memcheck, a memory checking tool which can detect many common
debad57fc2005-12-03 22:33:29 +000031memory errors such as:</para>
njn76c5bfa2005-03-11 04:33:29 +000032
33<itemizedlist>
debad57fc2005-12-03 22:33:29 +000034 <listitem>
sewardj778d7832007-11-22 01:21:56 +000035 <para>Touching memory you shouldn't (eg. overrunning heap block
36 boundaries, or reading/writing freed memory).</para>
debad57fc2005-12-03 22:33:29 +000037 </listitem>
38 <listitem>
sewardj778d7832007-11-22 01:21:56 +000039 <para>Using values before they have been initialized.</para>
debad57fc2005-12-03 22:33:29 +000040 </listitem>
41 <listitem>
sewardj778d7832007-11-22 01:21:56 +000042 <para>Incorrect freeing of memory, such as double-freeing heap
43 blocks.</para>
debad57fc2005-12-03 22:33:29 +000044 </listitem>
45 <listitem>
sewardj778d7832007-11-22 01:21:56 +000046 <para>Memory leaks.</para>
debad57fc2005-12-03 22:33:29 +000047 </listitem>
njn76c5bfa2005-03-11 04:33:29 +000048</itemizedlist>
49
sewardj778d7832007-11-22 01:21:56 +000050<para>Memcheck is only one of the tools in the Valgrind suite.
51Other tools you may find useful are:</para>
52
53<itemizedlist>
54 <listitem>
55 <para>Cachegrind: a profiling tool which produces detailed data on
56 cache (miss) and branch (misprediction) events. Statistics are
sewardje01c5052007-11-25 00:56:51 +000057 gathered for the entire program, for each function, and for each
58 line of code, if you need that level of detail.</para>
sewardj778d7832007-11-22 01:21:56 +000059 </listitem>
60 <listitem>
sewardje01c5052007-11-25 00:56:51 +000061 <para>Callgrind: a profiling tool that shows cost relationships
62 across function calls, optionally with cache simulation similar to
63 Cachegrind. Information gathered by Callgrind can be viewed
64 either with an included command line tool, or by using the
65 KCachegrind GUI. KCachegrind is not part of the Valgrind suite
66 -- it is part of the KDE Desktop Environment.</para>
sewardj778d7832007-11-22 01:21:56 +000067 </listitem>
68 <listitem>
69 <para>Massif: a space profiling tool. It allows you to explore
70 in detail which parts of your program allocate memory.</para>
71 </listitem>
72 <listitem>
73 <para>Helgrind: a debugging tool for threaded programs. Helgrind
74 looks for various kinds of synchronisation errors in code that uses
75 the POSIX PThreads API.</para>
76 </listitem>
77 <listitem>
78 <para>In addition, there are a number of "experimental" tools in
79 the codebase. They can be distinguished by the "exp-" prefix on
80 their names. Experimental tools are not subject to the same
81 quality control standards that apply to our production-grade tools
sewardjaf57d5b2008-12-22 01:00:15 +000082 (Memcheck, Cachegrind, Callgrind, Massif, Helgrind and DRD).</para>
sewardj778d7832007-11-22 01:21:56 +000083 </listitem>
84</itemizedlist>
85
86<para>The rest of this guide discusses only the Memcheck tool. For
sewardje01c5052007-11-25 00:56:51 +000087full documentation on the other tools, and for Memcheck, see the
88Valgrind User Manual.</para>
sewardj778d7832007-11-22 01:21:56 +000089
debad57fc2005-12-03 22:33:29 +000090<para>What follows is the minimum information you need to start
91detecting memory errors in your program with Memcheck. Note that this
sewardjaf57d5b2008-12-22 01:00:15 +000092guide applies to Valgrind version 3.4.0 and later. Some of the
debad57fc2005-12-03 22:33:29 +000093information is not quite right for earlier versions.</para>
njn76c5bfa2005-03-11 04:33:29 +000094
de9bec93c2005-11-25 05:36:48 +000095</sect1>
96
97
98<sect1 id="quick-start.prepare" xreflabel="Preparing your program">
njn76c5bfa2005-03-11 04:33:29 +000099<title>Preparing your program</title>
de9bec93c2005-11-25 05:36:48 +0000100
debad57fc2005-12-03 22:33:29 +0000101<para>Compile your program with <option>-g</option> to include debugging
102information so that Memcheck's error messages include exact line
103numbers. Using <computeroutput>-O0</computeroutput> is also a good
104idea, if you can tolerate the slowdown. With
105<computeroutput>-O1</computeroutput> line numbers in error messages can
106be inaccurate, although generally speaking Memchecking code compiled at
sewardjaf57d5b2008-12-22 01:00:15 +0000107<computeroutput>-O1</computeroutput> works fairly well and is
108recommended. Use of
debad57fc2005-12-03 22:33:29 +0000109<computeroutput>-O2</computeroutput> and above is not recommended as
110Memcheck occasionally reports uninitialised-value errors which don't
111really exist.</para>
112
njn76c5bfa2005-03-11 04:33:29 +0000113</sect1>
114
de9bec93c2005-11-25 05:36:48 +0000115
116<sect1 id="quick-start.mcrun" xreflabel="Running your program under Memcheck">
njn76c5bfa2005-03-11 04:33:29 +0000117<title>Running your program under Memcheck</title>
de9bec93c2005-11-25 05:36:48 +0000118
sewardjaf57d5b2008-12-22 01:00:15 +0000119<para>If you normally run your program like this:</para>
debad57fc2005-12-03 22:33:29 +0000120<programlisting> myprog arg1 arg2
njn76c5bfa2005-03-11 04:33:29 +0000121</programlisting>
122
sewardjaf57d5b2008-12-22 01:00:15 +0000123<para>Use this command line:</para>
debad57fc2005-12-03 22:33:29 +0000124<programlisting> valgrind --leak-check=yes myprog arg1 arg2
njn76c5bfa2005-03-11 04:33:29 +0000125</programlisting>
126
sewardjaf57d5b2008-12-22 01:00:15 +0000127<para>Memcheck is the default tool. The <option>--leak-check</option>
128option turns on the detailed memory leak detector.</para>
njn76c5bfa2005-03-11 04:33:29 +0000129
debad57fc2005-12-03 22:33:29 +0000130<para>Your program will run much slower (eg. 20 to 30 times) than
131normal, and use a lot more memory. Memcheck will issue messages about
132memory errors and leaks that it detects.</para>
133
njn76c5bfa2005-03-11 04:33:29 +0000134</sect1>
135
de9bec93c2005-11-25 05:36:48 +0000136
deccde45e2005-06-12 10:23:23 +0000137<sect1 id="quick-start.interpret"
138 xreflabel="Interpreting Memcheck's output">
njn76c5bfa2005-03-11 04:33:29 +0000139<title>Interpreting Memcheck's output</title>
sewardjaf57d5b2008-12-22 01:00:15 +0000140<para>Here's an example C program with a memory error and a memory
141leak.</para>
njn76c5bfa2005-03-11 04:33:29 +0000142
143<programlisting>
144 #include &lt;stdlib.h&gt;
145
146 void f(void)
147 {
148 int* x = malloc(10 * sizeof(int));
149 x[10] = 0; // problem 1: heap block overrun
150 } // problem 2: memory leak -- x not freed
151
152 int main(void)
153 {
154 f();
155 return 0;
156 }
157</programlisting>
158
sewardjaf57d5b2008-12-22 01:00:15 +0000159<para>Most error messages look like the following, which describes
160problem 1, the heap block overrun:</para>
njn76c5bfa2005-03-11 04:33:29 +0000161
162<programlisting>
163 ==19182== Invalid write of size 4
164 ==19182== at 0x804838F: f (example.c:6)
165 ==19182== by 0x80483AB: main (example.c:11)
166 ==19182== Address 0x1BA45050 is 0 bytes after a block of size 40 alloc'd
167 ==19182== at 0x1B8FF5CD: malloc (vg_replace_malloc.c:130)
168 ==19182== by 0x8048385: f (example.c:5)
169 ==19182== by 0x80483AB: main (example.c:11)
170</programlisting>
171
sewardjaf57d5b2008-12-22 01:00:15 +0000172<para>Things to notice:</para>
njn76c5bfa2005-03-11 04:33:29 +0000173
174<itemizedlist>
debad57fc2005-12-03 22:33:29 +0000175 <listitem>
176 <para>There is a lot of information in each error message; read it
177 carefully.</para>
178 </listitem>
179 <listitem>
180 <para>The 19182 is the process ID; it's usually unimportant.</para>
181 </listitem>
182 <listitem>
183 <para>The first line ("Invalid write...") tells you what kind of
184 error it is. Here, the program wrote to some memory it should not
185 have due to a heap block overrun.</para>
186 </listitem>
187 <listitem>
188 <para>Below the first line is a stack trace telling you where the
189 problem occurred. Stack traces can get quite large, and be
190 confusing, especially if you are using the C++ STL. Reading them
191 from the bottom up can help. If the stack trace is not big enough,
192 use the <option>--num-callers</option> option to make it
193 bigger.</para>
194 </listitem>
195 <listitem>
196 <para>The code addresses (eg. 0x804838F) are usually unimportant, but
197 occasionally crucial for tracking down weirder bugs.</para>
198 </listitem>
199 <listitem>
200 <para>Some error messages have a second component which describes
201 the memory address involved. This one shows that the written memory
202 is just past the end of a block allocated with malloc() on line 5 of
203 example.c.</para>
204 </listitem>
njn76c5bfa2005-03-11 04:33:29 +0000205</itemizedlist>
206
sewardjaf57d5b2008-12-22 01:00:15 +0000207<para>It's worth fixing errors in the order they are reported, as
208later errors can be caused by earlier errors. Failing to do this is a
sewardj778d7832007-11-22 01:21:56 +0000209common cause of difficulty with Memcheck.</para>
njn76c5bfa2005-03-11 04:33:29 +0000210
sewardjaf57d5b2008-12-22 01:00:15 +0000211<para>Memory leak messages look like this:</para>
njn76c5bfa2005-03-11 04:33:29 +0000212
213<programlisting>
214 ==19182== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
215 ==19182== at 0x1B8FF5CD: malloc (vg_replace_malloc.c:130)
njnbb9700d2005-08-15 04:40:57 +0000216 ==19182== by 0x8048385: f (a.c:5)
217 ==19182== by 0x80483AB: main (a.c:11)
njn76c5bfa2005-03-11 04:33:29 +0000218</programlisting>
219
sewardjaf57d5b2008-12-22 01:00:15 +0000220<para>The stack trace tells you where the leaked memory was allocated.
221Memcheck cannot tell you why the memory leaked, unfortunately.
222(Ignore the "vg_replace_malloc.c", that's an implementation
223detail.)</para>
njn76c5bfa2005-03-11 04:33:29 +0000224
debad57fc2005-12-03 22:33:29 +0000225<para>There are several kinds of leaks; the two most important
sewardjaf57d5b2008-12-22 01:00:15 +0000226categories are:</para>
njn76c5bfa2005-03-11 04:33:29 +0000227
228<itemizedlist>
debad57fc2005-12-03 22:33:29 +0000229 <listitem>
230 <para>"definitely lost": your program is leaking memory -- fix
231 it!</para>
232 </listitem>
233 <listitem>
234 <para>"probably lost": your program is leaking memory, unless you're
235 doing funny things with pointers (such as moving them to point to
236 the middle of a heap block).</para>
237 </listitem>
njn76c5bfa2005-03-11 04:33:29 +0000238</itemizedlist>
239
sewardjaf57d5b2008-12-22 01:00:15 +0000240<para>It can be difficult to track down the root causes of
241uninitialised-value errors reported by Memcheck. Try using
242the <option>--track-origins=yes</option> to get extra information.
243This makes Memcheck run slower, but the extra information you get
244often saves a lot of time figuring out where the uninitialised values
245are coming from.</para>
246
247<para>If you don't understand an error message, please consult
debad57fc2005-12-03 22:33:29 +0000248<xref linkend="mc-manual.errormsgs"/> in the <xref linkend="manual"/>
249which has examples of all the error messages Memcheck produces.</para>
250
njn76c5bfa2005-03-11 04:33:29 +0000251</sect1>
252
de9bec93c2005-11-25 05:36:48 +0000253
deccde45e2005-06-12 10:23:23 +0000254<sect1 id="quick-start.caveats" xreflabel="Caveats">
njn76c5bfa2005-03-11 04:33:29 +0000255<title>Caveats</title>
de9bec93c2005-11-25 05:36:48 +0000256
njn76c5bfa2005-03-11 04:33:29 +0000257<para>Memcheck is not perfect; it occasionally produces false positives,
njn2c091b82005-11-18 22:09:47 +0000258and there are mechanisms for suppressing these (see
njn76c5bfa2005-03-11 04:33:29 +0000259<xref linkend="manual-core.suppress"/> in the <xref linkend="manual"/>).
260However, it is typically right 99% of the time, so you should be wary of
261ignoring its error messages. After all, you wouldn't ignore warning
debad57fc2005-12-03 22:33:29 +0000262messages produced by a compiler, right? The suppression mechanism is
263also useful if Memcheck is reporting errors in library code that you
sewardj08e31e22007-05-23 21:58:33 +0000264cannot change. The default suppression set hides a lot of these, but you
debad57fc2005-12-03 22:33:29 +0000265may come across more.</para>
njn76c5bfa2005-03-11 04:33:29 +0000266
sewardj08e31e22007-05-23 21:58:33 +0000267<para>Memcheck cannot detect every memory error your program has.
268For example, it can't detect out-of-range reads or writes to arrays
269that are allocated statically or on the stack. But it should detect many
270errors that could crash your program (eg. cause a segmentation
debad57fc2005-12-03 22:33:29 +0000271fault).</para>
272
sewardj778d7832007-11-22 01:21:56 +0000273<para>Try to make your program so clean that Memcheck reports no
274errors. Once you achieve this state, it is much easier to see when
275changes to the program cause Memcheck to report new errors.
276Experience from several years of Memcheck use shows that it is
277possible to make even huge programs run Memcheck-clean. For example,
278large parts of KDE 3.5.X, and recent versions of OpenOffice.org
279(2.3.0) are Memcheck-clean, or very close to it.</para>
280
281
njn76c5bfa2005-03-11 04:33:29 +0000282</sect1>
283
de9bec93c2005-11-25 05:36:48 +0000284
deccde45e2005-06-12 10:23:23 +0000285<sect1 id="quick-start.info" xreflabel="More Information">
njn76c5bfa2005-03-11 04:33:29 +0000286<title>More information</title>
de9bec93c2005-11-25 05:36:48 +0000287
debad57fc2005-12-03 22:33:29 +0000288<para>Please consult the <xref linkend="FAQ"/> and the
289<xref linkend="manual"/>, which have much more information. Note that
290the other tools in the Valgrind distribution can be invoked with the
291<option>--tool</option> option.</para>
292
njn76c5bfa2005-03-11 04:33:29 +0000293</sect1>
294
de9bec93c2005-11-25 05:36:48 +0000295
296</article>
njn76c5bfa2005-03-11 04:33:29 +0000297</book>