blob: 69655bdbf06308bdffc6018be26187176302d3b8 [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>
35 <para>touching memory you shouldn't (eg. overrunning heap block
njn76c5bfa2005-03-11 04:33:29 +000036 boundaries);</para>
debad57fc2005-12-03 22:33:29 +000037 </listitem>
38 <listitem>
39 <para>using values before they have been initialized;</para>
40 </listitem>
41 <listitem>
42 <para>incorrect freeing of memory, such as double-freeing heap
43 blocks;</para>
44 </listitem>
45 <listitem>
46 <para>memory leaks.</para>
47 </listitem>
njn76c5bfa2005-03-11 04:33:29 +000048</itemizedlist>
49
debad57fc2005-12-03 22:33:29 +000050<para>What follows is the minimum information you need to start
51detecting memory errors in your program with Memcheck. Note that this
sewardj08e31e22007-05-23 21:58:33 +000052guide applies to Valgrind version 2.4.0 and later. Some of the
debad57fc2005-12-03 22:33:29 +000053information is not quite right for earlier versions.</para>
njn76c5bfa2005-03-11 04:33:29 +000054
de9bec93c2005-11-25 05:36:48 +000055</sect1>
56
57
58<sect1 id="quick-start.prepare" xreflabel="Preparing your program">
njn76c5bfa2005-03-11 04:33:29 +000059<title>Preparing your program</title>
de9bec93c2005-11-25 05:36:48 +000060
debad57fc2005-12-03 22:33:29 +000061<para>Compile your program with <option>-g</option> to include debugging
62information so that Memcheck's error messages include exact line
63numbers. Using <computeroutput>-O0</computeroutput> is also a good
64idea, if you can tolerate the slowdown. With
65<computeroutput>-O1</computeroutput> line numbers in error messages can
66be inaccurate, although generally speaking Memchecking code compiled at
67<computeroutput>-O1</computeroutput> works fairly well. Use of
68<computeroutput>-O2</computeroutput> and above is not recommended as
69Memcheck occasionally reports uninitialised-value errors which don't
70really exist.</para>
71
njn76c5bfa2005-03-11 04:33:29 +000072</sect1>
73
de9bec93c2005-11-25 05:36:48 +000074
75<sect1 id="quick-start.mcrun" xreflabel="Running your program under Memcheck">
njn76c5bfa2005-03-11 04:33:29 +000076<title>Running your program under Memcheck</title>
de9bec93c2005-11-25 05:36:48 +000077
njn76c5bfa2005-03-11 04:33:29 +000078<para>If you normally run your program like this:
debad57fc2005-12-03 22:33:29 +000079<programlisting> myprog arg1 arg2
njn76c5bfa2005-03-11 04:33:29 +000080</programlisting>
81
82Use this command line:
debad57fc2005-12-03 22:33:29 +000083<programlisting> valgrind --leak-check=yes myprog arg1 arg2
njn76c5bfa2005-03-11 04:33:29 +000084</programlisting>
85
debad57fc2005-12-03 22:33:29 +000086Memcheck is the default tool. The <option>--leak-check</option> option
87turns on the detailed memory leak detector.</para>
njn76c5bfa2005-03-11 04:33:29 +000088
debad57fc2005-12-03 22:33:29 +000089<para>Your program will run much slower (eg. 20 to 30 times) than
90normal, and use a lot more memory. Memcheck will issue messages about
91memory errors and leaks that it detects.</para>
92
njn76c5bfa2005-03-11 04:33:29 +000093</sect1>
94
de9bec93c2005-11-25 05:36:48 +000095
deccde45e2005-06-12 10:23:23 +000096<sect1 id="quick-start.interpret"
97 xreflabel="Interpreting Memcheck's output">
njn76c5bfa2005-03-11 04:33:29 +000098<title>Interpreting Memcheck's output</title>
99<para>Here's an example C program with a memory error and a memory leak.
100
101<programlisting>
102 #include &lt;stdlib.h&gt;
103
104 void f(void)
105 {
106 int* x = malloc(10 * sizeof(int));
107 x[10] = 0; // problem 1: heap block overrun
108 } // problem 2: memory leak -- x not freed
109
110 int main(void)
111 {
112 f();
113 return 0;
114 }
115</programlisting>
116
debad57fc2005-12-03 22:33:29 +0000117Most error messages look like the following, which describes problem 1,
118the heap block overrun:
njn76c5bfa2005-03-11 04:33:29 +0000119
120<programlisting>
121 ==19182== Invalid write of size 4
122 ==19182== at 0x804838F: f (example.c:6)
123 ==19182== by 0x80483AB: main (example.c:11)
124 ==19182== Address 0x1BA45050 is 0 bytes after a block of size 40 alloc'd
125 ==19182== at 0x1B8FF5CD: malloc (vg_replace_malloc.c:130)
126 ==19182== by 0x8048385: f (example.c:5)
127 ==19182== by 0x80483AB: main (example.c:11)
128</programlisting>
129
130Things to notice:
131
132<itemizedlist>
debad57fc2005-12-03 22:33:29 +0000133 <listitem>
134 <para>There is a lot of information in each error message; read it
135 carefully.</para>
136 </listitem>
137 <listitem>
138 <para>The 19182 is the process ID; it's usually unimportant.</para>
139 </listitem>
140 <listitem>
141 <para>The first line ("Invalid write...") tells you what kind of
142 error it is. Here, the program wrote to some memory it should not
143 have due to a heap block overrun.</para>
144 </listitem>
145 <listitem>
146 <para>Below the first line is a stack trace telling you where the
147 problem occurred. Stack traces can get quite large, and be
148 confusing, especially if you are using the C++ STL. Reading them
149 from the bottom up can help. If the stack trace is not big enough,
150 use the <option>--num-callers</option> option to make it
151 bigger.</para>
152 </listitem>
153 <listitem>
154 <para>The code addresses (eg. 0x804838F) are usually unimportant, but
155 occasionally crucial for tracking down weirder bugs.</para>
156 </listitem>
157 <listitem>
158 <para>Some error messages have a second component which describes
159 the memory address involved. This one shows that the written memory
160 is just past the end of a block allocated with malloc() on line 5 of
161 example.c.</para>
162 </listitem>
njn76c5bfa2005-03-11 04:33:29 +0000163</itemizedlist>
164
debad57fc2005-12-03 22:33:29 +0000165It's worth fixing errors in the order they are reported, as later errors
166can be caused by earlier errors.</para>
njn76c5bfa2005-03-11 04:33:29 +0000167
168<para>Memory leak messages look like this:
169
170<programlisting>
171 ==19182== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
172 ==19182== at 0x1B8FF5CD: malloc (vg_replace_malloc.c:130)
njnbb9700d2005-08-15 04:40:57 +0000173 ==19182== by 0x8048385: f (a.c:5)
174 ==19182== by 0x80483AB: main (a.c:11)
njn76c5bfa2005-03-11 04:33:29 +0000175</programlisting>
176
177The stack trace tells you where the leaked memory was allocated.
debad57fc2005-12-03 22:33:29 +0000178Memcheck cannot tell you why the memory leaked, unfortunately. (Ignore
179the "vg_replace_malloc.c", that's an implementation detail.)</para>
njn76c5bfa2005-03-11 04:33:29 +0000180
debad57fc2005-12-03 22:33:29 +0000181<para>There are several kinds of leaks; the two most important
182categories are:
njn76c5bfa2005-03-11 04:33:29 +0000183
184<itemizedlist>
debad57fc2005-12-03 22:33:29 +0000185 <listitem>
186 <para>"definitely lost": your program is leaking memory -- fix
187 it!</para>
188 </listitem>
189 <listitem>
190 <para>"probably lost": your program is leaking memory, unless you're
191 doing funny things with pointers (such as moving them to point to
192 the middle of a heap block).</para>
193 </listitem>
njn76c5bfa2005-03-11 04:33:29 +0000194</itemizedlist>
195
njn2c091b82005-11-18 22:09:47 +0000196If you don't understand an error message, please consult
debad57fc2005-12-03 22:33:29 +0000197<xref linkend="mc-manual.errormsgs"/> in the <xref linkend="manual"/>
198which has examples of all the error messages Memcheck produces.</para>
199
njn76c5bfa2005-03-11 04:33:29 +0000200</sect1>
201
de9bec93c2005-11-25 05:36:48 +0000202
deccde45e2005-06-12 10:23:23 +0000203<sect1 id="quick-start.caveats" xreflabel="Caveats">
njn76c5bfa2005-03-11 04:33:29 +0000204<title>Caveats</title>
de9bec93c2005-11-25 05:36:48 +0000205
njn76c5bfa2005-03-11 04:33:29 +0000206<para>Memcheck is not perfect; it occasionally produces false positives,
njn2c091b82005-11-18 22:09:47 +0000207and there are mechanisms for suppressing these (see
njn76c5bfa2005-03-11 04:33:29 +0000208<xref linkend="manual-core.suppress"/> in the <xref linkend="manual"/>).
209However, it is typically right 99% of the time, so you should be wary of
210ignoring its error messages. After all, you wouldn't ignore warning
debad57fc2005-12-03 22:33:29 +0000211messages produced by a compiler, right? The suppression mechanism is
212also useful if Memcheck is reporting errors in library code that you
sewardj08e31e22007-05-23 21:58:33 +0000213cannot change. The default suppression set hides a lot of these, but you
debad57fc2005-12-03 22:33:29 +0000214may come across more.</para>
njn76c5bfa2005-03-11 04:33:29 +0000215
sewardj08e31e22007-05-23 21:58:33 +0000216<para>Memcheck cannot detect every memory error your program has.
217For example, it can't detect out-of-range reads or writes to arrays
218that are allocated statically or on the stack. But it should detect many
219errors that could crash your program (eg. cause a segmentation
debad57fc2005-12-03 22:33:29 +0000220fault).</para>
221
njn76c5bfa2005-03-11 04:33:29 +0000222</sect1>
223
de9bec93c2005-11-25 05:36:48 +0000224
deccde45e2005-06-12 10:23:23 +0000225<sect1 id="quick-start.info" xreflabel="More Information">
njn76c5bfa2005-03-11 04:33:29 +0000226<title>More information</title>
de9bec93c2005-11-25 05:36:48 +0000227
debad57fc2005-12-03 22:33:29 +0000228<para>Please consult the <xref linkend="FAQ"/> and the
229<xref linkend="manual"/>, which have much more information. Note that
230the other tools in the Valgrind distribution can be invoked with the
231<option>--tool</option> option.</para>
232
njn76c5bfa2005-03-11 04:33:29 +0000233</sect1>
234
de9bec93c2005-11-25 05:36:48 +0000235
236</article>
njn76c5bfa2005-03-11 04:33:29 +0000237</book>