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