blob: 3cc044a671e250a642290beb1f3f2f9e2f0948bf [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
de53ad6842005-11-19 03:28:10 +00008<bookinfo>
9 <title>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>
15 <legalnotice>
16 <para>Email: <ulink url="mailto:&vg-vemail;">&vg-vemail;</ulink></para>
17 </legalnotice>
18</bookinfo>
njn76c5bfa2005-03-11 04:33:29 +000019
20
21<title>Valgrind Quick Start Guide</title>
22
njn779a2d62005-07-25 00:12:19 +000023<para>The Valgrind distribution has multiple tools. The most popular is the
24memory checking tool (called Memcheck) which can detect many common memory
25errors such as:
njn76c5bfa2005-03-11 04:33:29 +000026</para>
27
28<itemizedlist>
29 <listitem><para>touching memory you shouldn't (eg. overrunning heap block
30 boundaries);</para>
31 </listitem>
32 <listitem><para>using values before they have been initialized;</para>
33 </listitem>
34 <listitem><para>incorrect freeing of memory, such as double-freeing heap
35 blocks;</para>
36 </listitem>
njnffc92b82005-08-15 04:37:34 +000037 <listitem><para>memory leaks.</para>
njn76c5bfa2005-03-11 04:33:29 +000038 </listitem>
39</itemizedlist>
40
41<para>What follows is the minimum information you need to start detecting
42memory errors in your program with Memcheck. Note that this guide applies
njn779a2d62005-07-25 00:12:19 +000043to Valgrind version 2.4.0 and later; some of the information is not quite
44right for earlier versions.</para>
njn76c5bfa2005-03-11 04:33:29 +000045
deccde45e2005-06-12 10:23:23 +000046<sect1 id="quick-start.prepare"
47 xreflabel="Preparing your program">
njn76c5bfa2005-03-11 04:33:29 +000048<title>Preparing your program</title>
49<para>Compile your program with <computeroutput>-g</computeroutput> to include
50debugging information so that Memcheck's error messages include exact line
sewardj053fe982005-11-15 19:51:04 +000051numbers. Using <computeroutput>-O0</computeroutput> is also a good idea, if
52you can tolerate the slowdown. With <computeroutput>-O1</computeroutput>
njn710099c2005-11-15 20:16:45 +000053line numbers in error messages can be inaccurate, although generally speaking
sewardj053fe982005-11-15 19:51:04 +000054Memchecking code compiled at <computeroutput>-O1</computeroutput> works
55fairly well. Use of <computeroutput>-O2</computeroutput> and above is
56not recommended as Memcheck occasionally reports uninitialised-value
57errors which don't really exist.</para>
njn76c5bfa2005-03-11 04:33:29 +000058</sect1>
59
deccde45e2005-06-12 10:23:23 +000060<sect1 id="quick-start.mcrun"
61 xreflabel="Running your program under Memcheck">
njn76c5bfa2005-03-11 04:33:29 +000062<title>Running your program under Memcheck</title>
63<para>If you normally run your program like this:
64
65<programlisting>
66 myprog arg1 arg2
67</programlisting>
68
69Use this command line:
70
71<programlisting>
72 valgrind --leak-check=yes myprog arg1 arg2
73</programlisting>
74
75Memcheck is the default tool. The
njn779a2d62005-07-25 00:12:19 +000076<computeroutput>--leak-check</computeroutput> option turns on the detailed
77memory leak detector.</para>
njn76c5bfa2005-03-11 04:33:29 +000078
79<para>Your program will run much slower (eg. 20 to 30 times) than normal,
80and use a lot more memory. Memcheck will issue messages about memory errors
81and leaks that it detects.</para>
82</sect1>
83
deccde45e2005-06-12 10:23:23 +000084<sect1 id="quick-start.interpret"
85 xreflabel="Interpreting Memcheck's output">
njn76c5bfa2005-03-11 04:33:29 +000086<title>Interpreting Memcheck's output</title>
87<para>Here's an example C program with a memory error and a memory leak.
88
89<programlisting>
90 #include &lt;stdlib.h&gt;
91
92 void f(void)
93 {
94 int* x = malloc(10 * sizeof(int));
95 x[10] = 0; // problem 1: heap block overrun
96 } // problem 2: memory leak -- x not freed
97
98 int main(void)
99 {
100 f();
101 return 0;
102 }
103</programlisting>
104
105Most error messages look like the following, which describes problem 1, the
106heap block overrun:
107
108<programlisting>
109 ==19182== Invalid write of size 4
110 ==19182== at 0x804838F: f (example.c:6)
111 ==19182== by 0x80483AB: main (example.c:11)
112 ==19182== Address 0x1BA45050 is 0 bytes after a block of size 40 alloc'd
113 ==19182== at 0x1B8FF5CD: malloc (vg_replace_malloc.c:130)
114 ==19182== by 0x8048385: f (example.c:5)
115 ==19182== by 0x80483AB: main (example.c:11)
116</programlisting>
117
118Things to notice:
119
120<itemizedlist>
121 <listitem>
122 <para>There is a lot of information in each error message; read it
123 carefully.</para>
124 </listitem>
125
126 <listitem>
127 <para>The 19182 is the process ID; it's usually unimportant.</para>
128 </listitem>
129
130 <listitem>
131 <para>The first line ("Invalid write...") tells you what kind of error it
132 is. Here, the program wrote to some memory it should not have due to a
133 heap block overrun.</para>
134 </listitem>
135
136 <listitem>
137 <para>Below the first line is a stack trace telling you where the problem
138 occurred. Stack traces can get quite large, and be confusing, especially
139 if you are using the C++ STL. Reading them from the bottom up can help.
140 If the stack trace is not big enough, use the
141 <computeroutput>--num-callers</computeroutput> option to make it
142 bigger.</para>
143 </listitem>
144
145 <listitem>
njn1df54d22005-09-27 18:52:39 +0000146 <para>The code addresses (eg. 0x804838F) are usually unimportant, but
147 occasionally crucial for tracking down weirder bugs.</para>
njn76c5bfa2005-03-11 04:33:29 +0000148 </listitem>
149
150 <listitem>
151 <para>Some error messages have a second component which describes the memory
152 address involved. This one shows that the written memory is just past
njn1df54d22005-09-27 18:52:39 +0000153 the end of a block allocated with malloc() on line 5 of example.c.</para>
njn76c5bfa2005-03-11 04:33:29 +0000154 </listitem>
155</itemizedlist>
156
157It's worth fixing errors in the order they are reported, as later
158errors can be caused by earlier errors.</para>
159
160<para>Memory leak messages look like this:
161
162<programlisting>
163 ==19182== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
164 ==19182== at 0x1B8FF5CD: malloc (vg_replace_malloc.c:130)
njnbb9700d2005-08-15 04:40:57 +0000165 ==19182== by 0x8048385: f (a.c:5)
166 ==19182== by 0x80483AB: main (a.c:11)
njn76c5bfa2005-03-11 04:33:29 +0000167</programlisting>
168
169The stack trace tells you where the leaked memory was allocated.
170Memcheck cannot tell you why the memory leaked, unfortunately. (Ignore the
171"vg_replace_malloc.c", that's an implementation detail.)</para>
172
173<para>There are several kinds of leaks; the two most important categories are:
174
175<itemizedlist>
176 <listitem><para>"definitely lost": your program is leaking memory -- fix
177 it!</para>
178 </listitem>
179
180 <listitem><para>"probably lost": your program is leaking memory, unless
181 you're doing funny things with pointers (such as moving them to point to
182 the middle of a heap block).</para>
183 </listitem>
184</itemizedlist>
185
njn2c091b82005-11-18 22:09:47 +0000186If you don't understand an error message, please consult
njn779a2d62005-07-25 00:12:19 +0000187<xref linkend="mc-manual.errormsgs"/> in the <xref linkend="manual"/> which has
njn76c5bfa2005-03-11 04:33:29 +0000188examples of all the error messages Memcheck produces.</para>
189</sect1>
190
deccde45e2005-06-12 10:23:23 +0000191<sect1 id="quick-start.caveats" xreflabel="Caveats">
njn76c5bfa2005-03-11 04:33:29 +0000192<title>Caveats</title>
193<para>Memcheck is not perfect; it occasionally produces false positives,
njn2c091b82005-11-18 22:09:47 +0000194and there are mechanisms for suppressing these (see
njn76c5bfa2005-03-11 04:33:29 +0000195<xref linkend="manual-core.suppress"/> in the <xref linkend="manual"/>).
196However, it is typically right 99% of the time, so you should be wary of
197ignoring its error messages. After all, you wouldn't ignore warning
njn2c091b82005-11-18 22:09:47 +0000198messages produced by a compiler, right? The suppression mechanism is also
199useful if Memcheck is reporting errors in library code that you cannot
200change; the default suppression set hides a lot of these, but you may
201come across more.</para>
njn76c5bfa2005-03-11 04:33:29 +0000202
203<para>Memcheck also cannot detect every memory error your program has. For
204example, it can't detect if you overrun the bounds of an array that is
njn90db4ab2005-08-15 04:44:26 +0000205allocated statically or on the stack. But it should detect every error that
206could crash your program (eg. cause a segmentation fault).</para>
njn76c5bfa2005-03-11 04:33:29 +0000207</sect1>
208
deccde45e2005-06-12 10:23:23 +0000209<sect1 id="quick-start.info" xreflabel="More Information">
njn76c5bfa2005-03-11 04:33:29 +0000210<title>More information</title>
211<para>Please consult the <xref linkend="FAQ"/> and the
212<xref linkend="manual"/>, which have much more information. Note that the
213other tools in the Valgrind distribution can be invoked with the
214<computeroutput>--tool</computeroutput> option.</para>
215</sect1>
216
217</book>