blob: b40952bc1ecae7878e932b97245511a4082fcf48 [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
6<book id="quick-start" xreflabel="Valgrind Quick Start Guide">
7
8 <bookinfo>
9 <title>Valgrind Quick Start Guide</title>
10 </bookinfo>
11
12
13<title>Valgrind Quick Start Guide</title>
14
15<para>The Valgrind distribution has multiple tools. The memory checking
16tool (called Memcheck) can detect many common memory errors such as:
17</para>
18
19<itemizedlist>
20 <listitem><para>touching memory you shouldn't (eg. overrunning heap block
21 boundaries);</para>
22 </listitem>
23 <listitem><para>using values before they have been initialized;</para>
24 </listitem>
25 <listitem><para>incorrect freeing of memory, such as double-freeing heap
26 blocks;</para>
27 </listitem>
28 <listitem><para>memory leaks;</para>
29 </listitem>
30</itemizedlist>
31
32<para>What follows is the minimum information you need to start detecting
33memory errors in your program with Memcheck. Note that this guide applies
34to Valgrind version 2.4.0; some of the information is not quite right for
35earlier versions.</para>
36
deccde45e2005-06-12 10:23:23 +000037<sect1 id="quick-start.prepare"
38 xreflabel="Preparing your program">
njn76c5bfa2005-03-11 04:33:29 +000039<title>Preparing your program</title>
40<para>Compile your program with <computeroutput>-g</computeroutput> to include
41debugging information so that Memcheck's error messages include exact line
42numbers.</para>
43</sect1>
44
deccde45e2005-06-12 10:23:23 +000045<sect1 id="quick-start.mcrun"
46 xreflabel="Running your program under Memcheck">
njn76c5bfa2005-03-11 04:33:29 +000047<title>Running your program under Memcheck</title>
48<para>If you normally run your program like this:
49
50<programlisting>
51 myprog arg1 arg2
52</programlisting>
53
54Use this command line:
55
56<programlisting>
57 valgrind --leak-check=yes myprog arg1 arg2
58</programlisting>
59
60Memcheck is the default tool. The
61<computeroutput>--leak-check</computeroutput> option turns on the memory
62leak detector.</para>
63
64<para>Your program will run much slower (eg. 20 to 30 times) than normal,
65and use a lot more memory. Memcheck will issue messages about memory errors
66and leaks that it detects.</para>
67</sect1>
68
deccde45e2005-06-12 10:23:23 +000069<sect1 id="quick-start.interpret"
70 xreflabel="Interpreting Memcheck's output">
njn76c5bfa2005-03-11 04:33:29 +000071<title>Interpreting Memcheck's output</title>
72<para>Here's an example C program with a memory error and a memory leak.
73
74<programlisting>
75 #include &lt;stdlib.h&gt;
76
77 void f(void)
78 {
79 int* x = malloc(10 * sizeof(int));
80 x[10] = 0; // problem 1: heap block overrun
81 } // problem 2: memory leak -- x not freed
82
83 int main(void)
84 {
85 f();
86 return 0;
87 }
88</programlisting>
89
90Most error messages look like the following, which describes problem 1, the
91heap block overrun:
92
93<programlisting>
94 ==19182== Invalid write of size 4
95 ==19182== at 0x804838F: f (example.c:6)
96 ==19182== by 0x80483AB: main (example.c:11)
97 ==19182== Address 0x1BA45050 is 0 bytes after a block of size 40 alloc'd
98 ==19182== at 0x1B8FF5CD: malloc (vg_replace_malloc.c:130)
99 ==19182== by 0x8048385: f (example.c:5)
100 ==19182== by 0x80483AB: main (example.c:11)
101</programlisting>
102
103Things to notice:
104
105<itemizedlist>
106 <listitem>
107 <para>There is a lot of information in each error message; read it
108 carefully.</para>
109 </listitem>
110
111 <listitem>
112 <para>The 19182 is the process ID; it's usually unimportant.</para>
113 </listitem>
114
115 <listitem>
116 <para>The first line ("Invalid write...") tells you what kind of error it
117 is. Here, the program wrote to some memory it should not have due to a
118 heap block overrun.</para>
119 </listitem>
120
121 <listitem>
122 <para>Below the first line is a stack trace telling you where the problem
123 occurred. Stack traces can get quite large, and be confusing, especially
124 if you are using the C++ STL. Reading them from the bottom up can help.
125 If the stack trace is not big enough, use the
126 <computeroutput>--num-callers</computeroutput> option to make it
127 bigger.</para>
128 </listitem>
129
130 <listitem>
131 <para>The addresses (eg. 0x804838F) are usually unimportant, but occasionally
132 crucial for tracking down weirder bugs.</para>
133 </listitem>
134
135 <listitem>
136 <para>Some error messages have a second component which describes the memory
137 address involved. This one shows that the written memory is just past
138 the end of a block allocated with malloc() on line 7 of example.c.</para>
139 </listitem>
140</itemizedlist>
141
142It's worth fixing errors in the order they are reported, as later
143errors can be caused by earlier errors.</para>
144
145<para>Memory leak messages look like this:
146
147<programlisting>
148 ==19182== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
149 ==19182== at 0x1B8FF5CD: malloc (vg_replace_malloc.c:130)
150 ==19182== by 0x8048385: f (a.c:7)
151 ==19182== by 0x80483AB: main (a.c:14)
152</programlisting>
153
154The stack trace tells you where the leaked memory was allocated.
155Memcheck cannot tell you why the memory leaked, unfortunately. (Ignore the
156"vg_replace_malloc.c", that's an implementation detail.)</para>
157
158<para>There are several kinds of leaks; the two most important categories are:
159
160<itemizedlist>
161 <listitem><para>"definitely lost": your program is leaking memory -- fix
162 it!</para>
163 </listitem>
164
165 <listitem><para>"probably lost": your program is leaking memory, unless
166 you're doing funny things with pointers (such as moving them to point to
167 the middle of a heap block).</para>
168 </listitem>
169</itemizedlist>
170
171If you don't understand an error message, please consult
172<xref linkend="mc-manual.flags"/> in the <xref linkend="manual"/> which has
173examples of all the error messages Memcheck produces.</para>
174</sect1>
175
deccde45e2005-06-12 10:23:23 +0000176<sect1 id="quick-start.caveats" xreflabel="Caveats">
njn76c5bfa2005-03-11 04:33:29 +0000177<title>Caveats</title>
178<para>Memcheck is not perfect; it occasionally produces false positives,
179and there are mechanisms for suppressing these (see
180<xref linkend="manual-core.suppress"/> in the <xref linkend="manual"/>).
181However, it is typically right 99% of the time, so you should be wary of
182ignoring its error messages. After all, you wouldn't ignore warning
183messages produced by a compiler, right?</para>
184
185<para>Memcheck also cannot detect every memory error your program has. For
186example, it can't detect if you overrun the bounds of an array that is
187allocated statically or on the stack.</para>
188</sect1>
189
deccde45e2005-06-12 10:23:23 +0000190<sect1 id="quick-start.info" xreflabel="More Information">
njn76c5bfa2005-03-11 04:33:29 +0000191<title>More information</title>
192<para>Please consult the <xref linkend="FAQ"/> and the
193<xref linkend="manual"/>, which have much more information. Note that the
194other tools in the Valgrind distribution can be invoked with the
195<computeroutput>--tool</computeroutput> option.</para>
196</sect1>
197
198</book>