blob: a53bf86e999358eadc613012080856e62d801ffb [file] [log] [blame]
njn3e986b22004-11-30 10:43:45 +00001<?xml version="1.0"?> <!-- -*- sgml -*- -->
2<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
de03e0e7c2005-12-03 23:02:33 +00003 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
4
njn3e986b22004-11-30 10:43:45 +00005
njn05a89172009-07-29 02:36:21 +00006<chapter id="mc-manual" xreflabel="Memcheck: a memory error detector">
7<title>Memcheck: a memory error detector</title>
njn3e986b22004-11-30 10:43:45 +00008
de03e0e7c2005-12-03 23:02:33 +00009<para>To use this tool, you may specify <option>--tool=memcheck</option>
10on the Valgrind command line. You don't have to, though, since Memcheck
11is the default tool.</para>
njn3e986b22004-11-30 10:43:45 +000012
13
njn05a89172009-07-29 02:36:21 +000014<sect1 id="mc-manual.overview" xreflabel="Overview">
15<title>Overview</title>
njn3e986b22004-11-30 10:43:45 +000016
njn05a89172009-07-29 02:36:21 +000017<para>Memcheck is a memory error detector. It can detect the following
18problems that are common in C and C++ programs.</para>
njn3e986b22004-11-30 10:43:45 +000019
20<itemizedlist>
21 <listitem>
njn05a89172009-07-29 02:36:21 +000022 <para>Accessing memory you shouldn't, e.g. overrunning and underrunning
23 heap blocks, overrunning the top of the stack, and accessing memory after
24 it has been freed.</para>
njn3e986b22004-11-30 10:43:45 +000025 </listitem>
njn05a89172009-07-29 02:36:21 +000026
njn3e986b22004-11-30 10:43:45 +000027 <listitem>
njn05a89172009-07-29 02:36:21 +000028 <para>Using undefined values, i.e. values that have not been initialised,
29 or that have been derived from other undefined values.</para>
njn3e986b22004-11-30 10:43:45 +000030 </listitem>
njn05a89172009-07-29 02:36:21 +000031
njn3e986b22004-11-30 10:43:45 +000032 <listitem>
njn05a89172009-07-29 02:36:21 +000033 <para>Incorrect freeing of heap memory, such as double-freeing heap
34 blocks, or mismatched use of
bartaf25f672009-06-26 19:03:53 +000035 <function>malloc</function>/<computeroutput>new</computeroutput>/<computeroutput>new[]</computeroutput>
36 versus
37 <function>free</function>/<computeroutput>delete</computeroutput>/<computeroutput>delete[]</computeroutput></para>
njn3e986b22004-11-30 10:43:45 +000038 </listitem>
njn05a89172009-07-29 02:36:21 +000039
njn3e986b22004-11-30 10:43:45 +000040 <listitem>
41 <para>Overlapping <computeroutput>src</computeroutput> and
42 <computeroutput>dst</computeroutput> pointers in
njn2f7eebe2009-08-05 06:34:27 +000043 <computeroutput>memcpy</computeroutput> and related
njn05a89172009-07-29 02:36:21 +000044 functions.</para>
45 </listitem>
46
47 <listitem>
48 <para>Memory leaks.</para>
njn3e986b22004-11-30 10:43:45 +000049 </listitem>
njn3e986b22004-11-30 10:43:45 +000050</itemizedlist>
51
njn05a89172009-07-29 02:36:21 +000052<para>Problems like these can be difficult to find by other means,
53often remaining undetected for long periods, then causing occasional,
54difficult-to-diagnose crashes.</para>
55
njn3e986b22004-11-30 10:43:45 +000056</sect1>
57
58
59
njn3e986b22004-11-30 10:43:45 +000060<sect1 id="mc-manual.errormsgs"
61 xreflabel="Explanation of error messages from Memcheck">
62<title>Explanation of error messages from Memcheck</title>
63
njnc1abdcb2009-08-05 05:11:02 +000064<para>Memcheck issues a range of error messages. This section presents a
65quick summary of what error messages mean. The precise behaviour of the
66error-checking machinery is described in <xref
67linkend="mc-manual.machine"/>.</para>
njn3e986b22004-11-30 10:43:45 +000068
69
70<sect2 id="mc-manual.badrw"
71 xreflabel="Illegal read / Illegal write errors">
72<title>Illegal read / Illegal write errors</title>
73
74<para>For example:</para>
75<programlisting><![CDATA[
76Invalid read of size 4
77 at 0x40F6BBCC: (within /usr/lib/libpng.so.2.1.0.9)
78 by 0x40F6B804: (within /usr/lib/libpng.so.2.1.0.9)
sewardj08e31e22007-05-23 21:58:33 +000079 by 0x40B07FF4: read_png_image(QImageIO *) (kernel/qpngio.cpp:326)
njn3e986b22004-11-30 10:43:45 +000080 by 0x40AC751B: QImageIO::read() (kernel/qimage.cpp:3621)
njn21f91952005-03-12 22:14:42 +000081 Address 0xBFFFF0E0 is not stack'd, malloc'd or free'd
njn3e986b22004-11-30 10:43:45 +000082]]></programlisting>
83
de03e0e7c2005-12-03 23:02:33 +000084<para>This happens when your program reads or writes memory at a place
85which Memcheck reckons it shouldn't. In this example, the program did a
864-byte read at address 0xBFFFF0E0, somewhere within the system-supplied
87library libpng.so.2.1.0.9, which was called from somewhere else in the
88same library, called from line 326 of <filename>qpngio.cpp</filename>,
89and so on.</para>
njn3e986b22004-11-30 10:43:45 +000090
de03e0e7c2005-12-03 23:02:33 +000091<para>Memcheck tries to establish what the illegal address might relate
92to, since that's often useful. So, if it points into a block of memory
93which has already been freed, you'll be informed of this, and also where
njn7316df22009-08-04 01:16:01 +000094the block was freed. Likewise, if it should turn out to be just off
95the end of a heap block, a common result of off-by-one-errors in
de03e0e7c2005-12-03 23:02:33 +000096array subscripting, you'll be informed of this fact, and also where the
njn2f7eebe2009-08-05 06:34:27 +000097block was allocated. If you use the <option><xref
98linkend="opt.read-var-info"/></option> option Memcheck will run more slowly
99but may give a more detailed description of any illegal address.</para>
njn3e986b22004-11-30 10:43:45 +0000100
de03e0e7c2005-12-03 23:02:33 +0000101<para>In this example, Memcheck can't identify the address. Actually
102the address is on the stack, but, for some reason, this is not a valid
103stack address -- it is below the stack pointer and that isn't allowed.
njn7316df22009-08-04 01:16:01 +0000104In this particular case it's probably caused by GCC generating invalid
105code, a known bug in some ancient versions of GCC.</para>
njn3e986b22004-11-30 10:43:45 +0000106
de03e0e7c2005-12-03 23:02:33 +0000107<para>Note that Memcheck only tells you that your program is about to
108access memory at an illegal address. It can't stop the access from
109happening. So, if your program makes an access which normally would
110result in a segmentation fault, you program will still suffer the same
111fate -- but you will get a message from Memcheck immediately prior to
112this. In this particular example, reading junk on the stack is
113non-fatal, and the program stays alive.</para>
njn3e986b22004-11-30 10:43:45 +0000114
115</sect2>
116
117
118
119<sect2 id="mc-manual.uninitvals"
120 xreflabel="Use of uninitialised values">
121<title>Use of uninitialised values</title>
122
123<para>For example:</para>
124<programlisting><![CDATA[
125Conditional jump or move depends on uninitialised value(s)
126 at 0x402DFA94: _IO_vfprintf (_itoa.h:49)
127 by 0x402E8476: _IO_printf (printf.c:36)
128 by 0x8048472: main (tests/manuel1.c:8)
njn3e986b22004-11-30 10:43:45 +0000129]]></programlisting>
130
de03e0e7c2005-12-03 23:02:33 +0000131<para>An uninitialised-value use error is reported when your program
132uses a value which hasn't been initialised -- in other words, is
133undefined. Here, the undefined value is used somewhere inside the
njn2f7eebe2009-08-05 06:34:27 +0000134<function>printf</function> machinery of the C library. This error was
135reported when running the following small program:</para>
njn3e986b22004-11-30 10:43:45 +0000136<programlisting><![CDATA[
137int main()
138{
139 int x;
140 printf ("x = %d\n", x);
141}]]></programlisting>
142
de03e0e7c2005-12-03 23:02:33 +0000143<para>It is important to understand that your program can copy around
144junk (uninitialised) data as much as it likes. Memcheck observes this
145and keeps track of the data, but does not complain. A complaint is
146issued only when your program attempts to make use of uninitialised
njn2f7eebe2009-08-05 06:34:27 +0000147data in a way that might affect your program's externally-visible behaviour.
148In this example, <varname>x</varname> is uninitialised. Memcheck observes
149the value being passed to <function>_IO_printf</function> and thence to
150<function>_IO_vfprintf</function>, but makes no comment. However,
151<function>_IO_vfprintf</function> has to examine the value of
152<varname>x</varname> so it can turn it into the corresponding ASCII string,
153and it is at this point that Memcheck complains.</para>
njn3e986b22004-11-30 10:43:45 +0000154
155<para>Sources of uninitialised data tend to be:</para>
156<itemizedlist>
157 <listitem>
de03e0e7c2005-12-03 23:02:33 +0000158 <para>Local variables in procedures which have not been initialised,
159 as in the example above.</para>
njn3e986b22004-11-30 10:43:45 +0000160 </listitem>
161 <listitem>
njn7316df22009-08-04 01:16:01 +0000162 <para>The contents of heap blocks (allocated with
163 <function>malloc</function>, <function>new</function>, or a similar
164 function) before you (or a constructor) write something there.
165 </para>
njn3e986b22004-11-30 10:43:45 +0000166 </listitem>
167</itemizedlist>
168
sewardjcd0f2bd2008-05-04 23:06:28 +0000169<para>To see information on the sources of uninitialised data in your
njna3311642009-08-10 01:29:14 +0000170program, use the <option>--track-origins=yes</option> option. This
sewardjcd0f2bd2008-05-04 23:06:28 +0000171makes Memcheck run more slowly, but can make it much easier to track down
172the root causes of uninitialised value errors.</para>
173
njn3e986b22004-11-30 10:43:45 +0000174</sect2>
175
176
177
njn2f7eebe2009-08-05 06:34:27 +0000178<sect2 id="mc-manual.bad-syscall-args"
179 xreflabel="Use of uninitialised or unaddressable values in system
180 calls">
181<title>Use of uninitialised or unaddressable values in system
182 calls</title>
183
184<para>Memcheck checks all parameters to system calls:
185<itemizedlist>
186 <listitem>
187 <para>It checks all the direct parameters themselves, whether they are
188 initialised.</para>
189 </listitem>
190 <listitem>
191 <para>Also, if a system call needs to read from a buffer provided by
192 your program, Memcheck checks that the entire buffer is addressable
193 and its contents are initialised.</para>
194 </listitem>
195 <listitem>
196 <para>Also, if the system call needs to write to a user-supplied
197 buffer, Memcheck checks that the buffer is addressable.</para>
198 </listitem>
199</itemizedlist>
200</para>
201
202<para>After the system call, Memcheck updates its tracked information to
203precisely reflect any changes in memory state caused by the system
204call.</para>
205
206<para>Here's an example of two system calls with invalid parameters:</para>
207<programlisting><![CDATA[
208 #include <stdlib.h>
209 #include <unistd.h>
210 int main( void )
211 {
212 char* arr = malloc(10);
213 int* arr2 = malloc(sizeof(int));
214 write( 1 /* stdout */, arr, 10 );
215 exit(arr2[0]);
216 }
217]]></programlisting>
218
219<para>You get these complaints ...</para>
220<programlisting><![CDATA[
221 Syscall param write(buf) points to uninitialised byte(s)
222 at 0x25A48723: __write_nocancel (in /lib/tls/libc-2.3.3.so)
223 by 0x259AFAD3: __libc_start_main (in /lib/tls/libc-2.3.3.so)
224 by 0x8048348: (within /auto/homes/njn25/grind/head4/a.out)
225 Address 0x25AB8028 is 0 bytes inside a block of size 10 alloc'd
226 at 0x259852B0: malloc (vg_replace_malloc.c:130)
227 by 0x80483F1: main (a.c:5)
228
229 Syscall param exit(error_code) contains uninitialised byte(s)
230 at 0x25A21B44: __GI__exit (in /lib/tls/libc-2.3.3.so)
231 by 0x8048426: main (a.c:8)
232]]></programlisting>
233
234<para>... because the program has (a) written uninitialised junk
235from the heap block to the standard output, and (b) passed an
236uninitialised value to <function>exit</function>. Note that the first
237error refers to the memory pointed to by
238<computeroutput>buf</computeroutput> (not
239<computeroutput>buf</computeroutput> itself), but the second error
240refers directly to <computeroutput>exit</computeroutput>'s argument
241<computeroutput>arr2[0]</computeroutput>.</para>
242
243</sect2>
244
245
njn3e986b22004-11-30 10:43:45 +0000246<sect2 id="mc-manual.badfrees" xreflabel="Illegal frees">
247<title>Illegal frees</title>
248
249<para>For example:</para>
250<programlisting><![CDATA[
251Invalid free()
252 at 0x4004FFDF: free (vg_clientmalloc.c:577)
253 by 0x80484C7: main (tests/doublefree.c:10)
njn21f91952005-03-12 22:14:42 +0000254 Address 0x3807F7B4 is 0 bytes inside a block of size 177 free'd
njn3e986b22004-11-30 10:43:45 +0000255 at 0x4004FFDF: free (vg_clientmalloc.c:577)
256 by 0x80484C7: main (tests/doublefree.c:10)
njn3e986b22004-11-30 10:43:45 +0000257]]></programlisting>
258
bartaf25f672009-06-26 19:03:53 +0000259<para>Memcheck keeps track of the blocks allocated by your program
260with <function>malloc</function>/<computeroutput>new</computeroutput>,
261so it can know exactly whether or not the argument to
262<function>free</function>/<computeroutput>delete</computeroutput> is
263legitimate or not. Here, this test program has freed the same block
264twice. As with the illegal read/write errors, Memcheck attempts to
njn7316df22009-08-04 01:16:01 +0000265make sense of the address freed. If, as here, the address is one
bartaf25f672009-06-26 19:03:53 +0000266which has previously been freed, you wil be told that -- making
njn2f7eebe2009-08-05 06:34:27 +0000267duplicate frees of the same block easy to spot. You will also get this
268message if you try to free a pointer that doesn't point to the start of a
269heap block.</para>
njn3e986b22004-11-30 10:43:45 +0000270
271</sect2>
272
273
274<sect2 id="mc-manual.rudefn"
njn2f7eebe2009-08-05 06:34:27 +0000275 xreflabel="When a heap block is freed with an inappropriate deallocation
njn3e986b22004-11-30 10:43:45 +0000276function">
njn2f7eebe2009-08-05 06:34:27 +0000277<title>When a heap block is freed with an inappropriate deallocation
njn3e986b22004-11-30 10:43:45 +0000278function</title>
279
280<para>In the following example, a block allocated with
de03e0e7c2005-12-03 23:02:33 +0000281<function>new[]</function> has wrongly been deallocated with
282<function>free</function>:</para>
njn3e986b22004-11-30 10:43:45 +0000283<programlisting><![CDATA[
284Mismatched free() / delete / delete []
285 at 0x40043249: free (vg_clientfuncs.c:171)
286 by 0x4102BB4E: QGArray::~QGArray(void) (tools/qgarray.cpp:149)
287 by 0x4C261C41: PptDoc::~PptDoc(void) (include/qmemarray.h:60)
288 by 0x4C261F0E: PptXml::~PptXml(void) (pptxml.cc:44)
njn21f91952005-03-12 22:14:42 +0000289 Address 0x4BB292A8 is 0 bytes inside a block of size 64 alloc'd
sewardj08e31e22007-05-23 21:58:33 +0000290 at 0x4004318C: operator new[](unsigned int) (vg_clientfuncs.c:152)
njn3e986b22004-11-30 10:43:45 +0000291 by 0x4C21BC15: KLaola::readSBStream(int) const (klaola.cc:314)
292 by 0x4C21C155: KLaola::stream(KLaola::OLENode const *) (klaola.cc:416)
293 by 0x4C21788F: OLEFilter::convert(QCString const &) (olefilter.cc:272)
294]]></programlisting>
295
de03e0e7c2005-12-03 23:02:33 +0000296<para>In <literal>C++</literal> it's important to deallocate memory in a
297way compatible with how it was allocated. The deal is:</para>
njn3e986b22004-11-30 10:43:45 +0000298<itemizedlist>
299 <listitem>
300 <para>If allocated with
de03e0e7c2005-12-03 23:02:33 +0000301 <function>malloc</function>,
302 <function>calloc</function>,
303 <function>realloc</function>,
304 <function>valloc</function> or
305 <function>memalign</function>, you must
306 deallocate with <function>free</function>.</para>
njn3e986b22004-11-30 10:43:45 +0000307 </listitem>
308 <listitem>
de03e0e7c2005-12-03 23:02:33 +0000309 <para>If allocated with <function>new</function>, you must deallocate
310 with <function>delete</function>.</para>
njn3e986b22004-11-30 10:43:45 +0000311 </listitem>
njn2f7eebe2009-08-05 06:34:27 +0000312 <listitem>
313 <para>If allocated with <function>new[]</function>, you must
314 deallocate with <function>delete[]</function>.</para>
315 </listitem>
njn3e986b22004-11-30 10:43:45 +0000316</itemizedlist>
317
de03e0e7c2005-12-03 23:02:33 +0000318<para>The worst thing is that on Linux apparently it doesn't matter if
sewardj08e31e22007-05-23 21:58:33 +0000319you do mix these up, but the same program may then crash on a
320different platform, Solaris for example. So it's best to fix it
321properly. According to the KDE folks "it's amazing how many C++
322programmers don't know this".</para>
njn3e986b22004-11-30 10:43:45 +0000323
sewardj08e31e22007-05-23 21:58:33 +0000324<para>The reason behind the requirement is as follows. In some C++
325implementations, <function>delete[]</function> must be used for
326objects allocated by <function>new[]</function> because the compiler
327stores the size of the array and the pointer-to-member to the
328destructor of the array's content just before the pointer actually
njn2f7eebe2009-08-05 06:34:27 +0000329returned. <function>delete</function> doesn't account for this and will get
330confused, possibly corrupting the heap.</para>
de03e0e7c2005-12-03 23:02:33 +0000331
njn3e986b22004-11-30 10:43:45 +0000332</sect2>
333
334
335
njn3e986b22004-11-30 10:43:45 +0000336<sect2 id="mc-manual.overlap"
337 xreflabel="Overlapping source and destination blocks">
338<title>Overlapping source and destination blocks</title>
339
340<para>The following C library functions copy some data from one
341memory block to another (or something similar):
njn2f7eebe2009-08-05 06:34:27 +0000342<function>memcpy</function>,
343<function>strcpy</function>,
344<function>strncpy</function>,
345<function>strcat</function>,
346<function>strncat</function>.
de03e0e7c2005-12-03 23:02:33 +0000347The blocks pointed to by their <computeroutput>src</computeroutput> and
348<computeroutput>dst</computeroutput> pointers aren't allowed to overlap.
njn2f7eebe2009-08-05 06:34:27 +0000349The POSIX standards have wording along the lines "If copying takes place
350between objects that overlap, the behavior is undefined." Therefore,
351Memcheck checks for this.
352</para>
njn3e986b22004-11-30 10:43:45 +0000353
354<para>For example:</para>
355<programlisting><![CDATA[
356==27492== Source and destination overlap in memcpy(0xbffff294, 0xbffff280, 21)
357==27492== at 0x40026CDC: memcpy (mc_replace_strmem.c:71)
358==27492== by 0x804865A: main (overlap.c:40)
njn3e986b22004-11-30 10:43:45 +0000359]]></programlisting>
360
de03e0e7c2005-12-03 23:02:33 +0000361<para>You don't want the two blocks to overlap because one of them could
sewardj08e31e22007-05-23 21:58:33 +0000362get partially overwritten by the copying.</para>
njn3e986b22004-11-30 10:43:45 +0000363
njnccad0b82005-07-19 00:48:55 +0000364<para>You might think that Memcheck is being overly pedantic reporting
de03e0e7c2005-12-03 23:02:33 +0000365this in the case where <computeroutput>dst</computeroutput> is less than
366<computeroutput>src</computeroutput>. For example, the obvious way to
njn2f7eebe2009-08-05 06:34:27 +0000367implement <function>memcpy</function> is by copying from the first
de03e0e7c2005-12-03 23:02:33 +0000368byte to the last. However, the optimisation guides of some
369architectures recommend copying from the last byte down to the first.
njn2f7eebe2009-08-05 06:34:27 +0000370Also, some implementations of <function>memcpy</function> zero
de03e0e7c2005-12-03 23:02:33 +0000371<computeroutput>dst</computeroutput> before copying, because zeroing the
372destination's cache line(s) can improve performance.</para>
njnccad0b82005-07-19 00:48:55 +0000373
de03e0e7c2005-12-03 23:02:33 +0000374<para>The moral of the story is: if you want to write truly portable
375code, don't make any assumptions about the language
376implementation.</para>
njnccad0b82005-07-19 00:48:55 +0000377
njn3e986b22004-11-30 10:43:45 +0000378</sect2>
379
380
njnab5b7142005-08-16 02:20:17 +0000381<sect2 id="mc-manual.leaks" xreflabel="Memory leak detection">
382<title>Memory leak detection</title>
383
njn2f7eebe2009-08-05 06:34:27 +0000384<para>Memcheck keeps track of all heap blocks issued in response to
bartaf25f672009-06-26 19:03:53 +0000385calls to
njn2f7eebe2009-08-05 06:34:27 +0000386<function>malloc</function>/<function>new</function> et al.
bartaf25f672009-06-26 19:03:53 +0000387So when the program exits, it knows which blocks have not been freed.
njnab5b7142005-08-16 02:20:17 +0000388</para>
389
de03e0e7c2005-12-03 23:02:33 +0000390<para>If <option>--leak-check</option> is set appropriately, for each
njn8225cc02009-03-09 22:52:24 +0000391remaining block, Memcheck determines if the block is reachable from pointers
392within the root-set. The root-set consists of (a) general purpose registers
393of all threads, and (b) initialised, aligned, pointer-sized data words in
394accessible client memory, including stacks.</para>
395
396<para>There are two ways a block can be reached. The first is with a
njn389f5702009-07-15 07:18:16 +0000397"start-pointer", i.e. a pointer to the start of the block. The second is with
398an "interior-pointer", i.e. a pointer to the middle of the block. There are
philippeab1fce92013-09-29 13:47:32 +0000399several ways we know of that an interior-pointer can occur:</para>
njn389f5702009-07-15 07:18:16 +0000400
401<itemizedlist>
402 <listitem>
403 <para>The pointer might have originally been a start-pointer and have been
njn7c02ba72011-01-04 23:46:07 +0000404 moved along deliberately (or not deliberately) by the program. In
405 particular, this can happen if your program uses tagged pointers, i.e.
406 if it uses the bottom one, two or three bits of a pointer, which are
407 normally always zero due to alignment, in order to store extra
408 information.</para>
njn389f5702009-07-15 07:18:16 +0000409 </listitem>
410
411 <listitem>
412 <para>It might be a random junk value in memory, entirely unrelated, just
413 a coincidence.</para>
414 </listitem>
415
416 <listitem>
417 <para>It might be a pointer to an array of C++ objects (which possess
418 destructors) allocated with <computeroutput>new[]</computeroutput>. In
419 this case, some compilers store a "magic cookie" containing the array
420 length at the start of the allocated block, and return a pointer to just
421 past that magic cookie, i.e. an interior-pointer.
422 See <ulink url="http://theory.uwinnipeg.ca/gnu/gcc/gxxint_14.html">this
423 page</ulink> for more information.</para>
424 </listitem>
philippeab1fce92013-09-29 13:47:32 +0000425
426 <listitem>
427 <para>It might be a pointer to the inner char array of a C++
428 <computeroutput>std::string</computeroutput>. For example, some
429 compilers add 3 words at the beginning of the std::string to
430 store the length, the capacity and a reference count before the
431 memory containing the array of characters. They return a pointer
432 just after these 3 words, pointing at the char array.</para>
433 </listitem>
434
435 <listitem>
436 <para>It might be a pointer to an inner part of a C++ object using
437 multiple inheritance. </para>
438 </listitem>
bart9d6d2a92009-07-19 09:19:58 +0000439</itemizedlist>
njn8225cc02009-03-09 22:52:24 +0000440
philippeab1fce92013-09-29 13:47:32 +0000441<para>You can optionally activate heuristics to use during the leak
442search to detect the interior pointers corresponding to
443the <computeroutput>newarray</computeroutput>,
444<computeroutput>stdstring</computeroutput> and
445<computeroutput>multipleinheritance</computeroutput> cases. If the
446heuristic detects that an interior pointer corresponds to such a case,
447the block will be considered as reachable by the interior
448pointer. In other words, the interior pointer will be treated
449as if it were a start pointer.</para>
450
451
njn8225cc02009-03-09 22:52:24 +0000452<para>With that in mind, consider the nine possible cases described by the
453following figure.</para>
454
455<programlisting><![CDATA[
philippe2193a7c2012-12-08 17:54:16 +0000456 Pointer chain AAA Leak Case BBB Leak Case
457 ------------- ------------- -------------
njn8225cc02009-03-09 22:52:24 +0000458(1) RRR ------------> BBB DR
459(2) RRR ---> AAA ---> BBB DR IR
460(3) RRR BBB DL
461(4) RRR AAA ---> BBB DL IL
462(5) RRR ------?-----> BBB (y)DR, (n)DL
463(6) RRR ---> AAA -?-> BBB DR (y)IR, (n)DL
464(7) RRR -?-> AAA ---> BBB (y)DR, (n)DL (y)IR, (n)IL
465(8) RRR -?-> AAA -?-> BBB (y)DR, (n)DL (y,y)IR, (n,y)IL, (_,n)DL
466(9) RRR AAA -?-> BBB DL (y)IL, (n)DL
467
468Pointer chain legend:
469- RRR: a root set node or DR block
470- AAA, BBB: heap blocks
471- --->: a start-pointer
472- -?->: an interior-pointer
473
philippe2193a7c2012-12-08 17:54:16 +0000474Leak Case legend:
njn8225cc02009-03-09 22:52:24 +0000475- DR: Directly reachable
476- IR: Indirectly reachable
477- DL: Directly lost
478- IL: Indirectly lost
479- (y)XY: it's XY if the interior-pointer is a real pointer
480- (n)XY: it's XY if the interior-pointer is not a real pointer
481- (_)XY: it's XY in either case
482]]></programlisting>
483
484<para>Every possible case can be reduced to one of the above nine. Memcheck
485merges some of these cases in its output, resulting in the following four
philippe2193a7c2012-12-08 17:54:16 +0000486leak kinds.</para>
njn8225cc02009-03-09 22:52:24 +0000487
njnab5b7142005-08-16 02:20:17 +0000488
489<itemizedlist>
490
491 <listitem>
njn8225cc02009-03-09 22:52:24 +0000492 <para>"Still reachable". This covers cases 1 and 2 (for the BBB blocks)
493 above. A start-pointer or chain of start-pointers to the block is
494 found. Since the block is still pointed at, the programmer could, at
philippe2193a7c2012-12-08 17:54:16 +0000495 least in principle, have freed it before program exit. "Still reachable"
496 blocks are very common and arguably not a problem. So, by default,
497 Memcheck won't report such blocks individually.</para>
njnab5b7142005-08-16 02:20:17 +0000498 </listitem>
499
500 <listitem>
njn8225cc02009-03-09 22:52:24 +0000501 <para>"Definitely lost". This covers case 3 (for the BBB blocks) above.
502 This means that no pointer to the block can be found. The block is
503 classified as "lost", because the programmer could not possibly have
504 freed it at program exit, since no pointer to it exists. This is likely
505 a symptom of having lost the pointer at some earlier point in the
506 program. Such cases should be fixed by the programmer.</para>
njnab5b7142005-08-16 02:20:17 +0000507 </listitem>
508
njn8225cc02009-03-09 22:52:24 +0000509 <listitem>
510 <para>"Indirectly lost". This covers cases 4 and 9 (for the BBB blocks)
511 above. This means that the block is lost, not because there are no
512 pointers to it, but rather because all the blocks that point to it are
513 themselves lost. For example, if you have a binary tree and the root
514 node is lost, all its children nodes will be indirectly lost. Because
515 the problem will disappear if the definitely lost block that caused the
516 indirect leak is fixed, Memcheck won't report such blocks individually
philippe2193a7c2012-12-08 17:54:16 +0000517 by default.</para>
njn8225cc02009-03-09 22:52:24 +0000518 </listitem>
519
520 <listitem>
521 <para>"Possibly lost". This covers cases 5--8 (for the BBB blocks)
522 above. This means that a chain of one or more pointers to the block has
523 been found, but at least one of the pointers is an interior-pointer.
524 This could just be a random value in memory that happens to point into a
525 block, and so you shouldn't consider this ok unless you know you have
526 interior-pointers.</para>
527 </listitem>
528
njnab5b7142005-08-16 02:20:17 +0000529</itemizedlist>
530
philippe2193a7c2012-12-08 17:54:16 +0000531<para>(Note: This mapping of the nine possible cases onto four leak kinds is
njn8225cc02009-03-09 22:52:24 +0000532not necessarily the best way that leaks could be reported; in particular,
533interior-pointers are treated inconsistently. It is possible the
534categorisation may be improved in the future.)</para>
535
536<para>Furthermore, if suppressions exists for a block, it will be reported
philippe2193a7c2012-12-08 17:54:16 +0000537as "suppressed" no matter what which of the above four kinds it belongs
njn8225cc02009-03-09 22:52:24 +0000538to.</para>
539
540
541<para>The following is an example leak summary.</para>
542
543<programlisting><![CDATA[
544LEAK SUMMARY:
545 definitely lost: 48 bytes in 3 blocks.
546 indirectly lost: 32 bytes in 2 blocks.
547 possibly lost: 96 bytes in 6 blocks.
548 still reachable: 64 bytes in 4 blocks.
549 suppressed: 0 bytes in 0 blocks.
550]]></programlisting>
551
philippeab1fce92013-09-29 13:47:32 +0000552<para>If heuristics have been used to consider some blocks as
553reachable, the leak summary details the heuristically reachable subset
554of 'still reachable:' per heuristic. In the below example, of the 79
555bytes still reachable, 71 bytes (56+7+8) have been considered
556heuristically reachable.
557</para>
558
559<programlisting><![CDATA[
560LEAK SUMMARY:
561 definitely lost: 4 bytes in 1 blocks
562 indirectly lost: 0 bytes in 0 blocks
563 possibly lost: 0 bytes in 0 blocks
564 still reachable: 79 bytes in 5 blocks
565 of which reachable via heuristic:
566 stdstring : 56 bytes in 2 blocks
567 newarray : 7 bytes in 1 blocks
568 multipleinheritance: 8 bytes in 1 blocks
569 suppressed: 0 bytes in 0 blocks
570]]></programlisting>
571
njn7e5d4ed2009-07-30 02:57:52 +0000572<para>If <option>--leak-check=full</option> is specified,
njn8225cc02009-03-09 22:52:24 +0000573Memcheck will give details for each definitely lost or possibly lost block,
njn62dd9fa2009-03-10 21:40:46 +0000574including where it was allocated. (Actually, it merges results for all
philippe2193a7c2012-12-08 17:54:16 +0000575blocks that have the same leak kind and sufficiently similar stack traces
njn62dd9fa2009-03-10 21:40:46 +0000576into a single "loss record". The
njn7e5d4ed2009-07-30 02:57:52 +0000577<option>--leak-resolution</option> lets you control the
njn62dd9fa2009-03-10 21:40:46 +0000578meaning of "sufficiently similar".) It cannot tell you when or how or why
579the pointer to a leaked block was lost; you have to work that out for
580yourself. In general, you should attempt to ensure your programs do not
581have any definitely lost or possibly lost blocks at exit.</para>
njnab5b7142005-08-16 02:20:17 +0000582
583<para>For example:</para>
584<programlisting><![CDATA[
5858 bytes in 1 blocks are definitely lost in loss record 1 of 14
586 at 0x........: malloc (vg_replace_malloc.c:...)
587 by 0x........: mk (leak-tree.c:11)
588 by 0x........: main (leak-tree.c:39)
589
njn8225cc02009-03-09 22:52:24 +000059088 (8 direct, 80 indirect) bytes in 1 blocks are definitely lost in loss record 13 of 14
njnab5b7142005-08-16 02:20:17 +0000591 at 0x........: malloc (vg_replace_malloc.c:...)
592 by 0x........: mk (leak-tree.c:11)
593 by 0x........: main (leak-tree.c:25)
594]]></programlisting>
595
de03e0e7c2005-12-03 23:02:33 +0000596<para>The first message describes a simple case of a single 8 byte block
njn8225cc02009-03-09 22:52:24 +0000597that has been definitely lost. The second case mentions another 8 byte
598block that has been definitely lost; the difference is that a further 80
njn62dd9fa2009-03-10 21:40:46 +0000599bytes in other blocks are indirectly lost because of this lost block.
600The loss records are not presented in any notable order, so the loss record
philippe2193a7c2012-12-08 17:54:16 +0000601numbers aren't particularly meaningful. The loss record numbers can be used
602in the Valgrind gdbserver to list the addresses of the leaked blocks and/or give
603more details about how a block is still reachable.</para>
njnab5b7142005-08-16 02:20:17 +0000604
philippe2193a7c2012-12-08 17:54:16 +0000605<para>The option <option>--show-leak-kinds=&lt;set&gt;</option>
606controls the set of leak kinds to show
sewardj4c7254d2013-11-29 23:08:28 +0000607when <option>--leak-check=full</option> is specified. </para>
philippe2193a7c2012-12-08 17:54:16 +0000608
sewardj4c7254d2013-11-29 23:08:28 +0000609<para>The <option>&lt;set&gt;</option> of leak kinds is specified
610in one of the following ways:
philippe2193a7c2012-12-08 17:54:16 +0000611
612<itemizedlist>
613 <listitem>a comma separated list of one or more of
614 <option>definite indirect possible reachable</option>.
615 </listitem>
616
617 <listitem><option>all</option> to specify the complete set (all leak kinds).
618 </listitem>
619
sewardj4c7254d2013-11-29 23:08:28 +0000620 <listitem><option>none</option> for the empty set.
philippe2193a7c2012-12-08 17:54:16 +0000621 </listitem>
622</itemizedlist>
623
624</para>
625
626<para> The default value for the leak kinds to show is
627 <option>--show-leak-kinds=definite,possible</option>.
628</para>
629
sewardj4c7254d2013-11-29 23:08:28 +0000630<para>To also show the reachable and indirectly lost blocks in
631addition to the definitely and possibly lost blocks, you can
632use <option>--show-leak-kinds=all</option>. To only show the
633reachable and indirectly lost blocks, use
634<option>--show-leak-kinds=indirect,reachable</option>. The reachable
635and indirectly lost blocks will then be presented as shown in
636the following two examples.</para>
njn8225cc02009-03-09 22:52:24 +0000637
638<programlisting><![CDATA[
63964 bytes in 4 blocks are still reachable in loss record 2 of 4
640 at 0x........: malloc (vg_replace_malloc.c:177)
641 by 0x........: mk (leak-cases.c:52)
642 by 0x........: main (leak-cases.c:74)
643
64432 bytes in 2 blocks are indirectly lost in loss record 1 of 4
645 at 0x........: malloc (vg_replace_malloc.c:177)
646 by 0x........: mk (leak-cases.c:52)
647 by 0x........: main (leak-cases.c:80)
648]]></programlisting>
njnab5b7142005-08-16 02:20:17 +0000649
philippe2193a7c2012-12-08 17:54:16 +0000650<para>Because there are different kinds of leaks with different
sewardj4c7254d2013-11-29 23:08:28 +0000651severities, an interesting question is: which leaks should be
philippe2193a7c2012-12-08 17:54:16 +0000652counted as true "errors" and which should not?
653</para>
njn26670552009-08-13 00:02:30 +0000654
philippe2193a7c2012-12-08 17:54:16 +0000655<para> The answer to this question affects the numbers printed in
656the <computeroutput>ERROR SUMMARY</computeroutput> line, and also the
657effect of the <option>--error-exitcode</option> option. First, a leak
658is only counted as a true "error"
659if <option>--leak-check=full</option> is specified. Then, the
660option <option>--errors-for-leak-kinds=&lt;set&gt;</option> controls
661the set of leak kinds to consider as errors. The default value
662is <option>--errors-for-leak-kinds=definite,possible</option>
663</para>
njn26670552009-08-13 00:02:30 +0000664
njnab5b7142005-08-16 02:20:17 +0000665</sect2>
666
njn3e986b22004-11-30 10:43:45 +0000667</sect1>
668
669
670
njna3311642009-08-10 01:29:14 +0000671<sect1 id="mc-manual.options"
672 xreflabel="Memcheck Command-Line Options">
673<title>Memcheck Command-Line Options</title>
njnc1abdcb2009-08-05 05:11:02 +0000674
675<!-- start of xi:include in the manpage -->
676<variablelist id="mc.opts.list">
677
678 <varlistentry id="opt.leak-check" xreflabel="--leak-check">
679 <term>
680 <option><![CDATA[--leak-check=<no|summary|yes|full> [default: summary] ]]></option>
681 </term>
682 <listitem>
683 <para>When enabled, search for memory leaks when the client
684 program finishes. If set to <varname>summary</varname>, it says how
685 many leaks occurred. If set to <varname>full</varname> or
686 <varname>yes</varname>, it also gives details of each individual
687 leak.</para>
688 </listitem>
689 </varlistentry>
690
691 <varlistentry id="opt.leak-resolution" xreflabel="--leak-resolution">
692 <term>
693 <option><![CDATA[--leak-resolution=<low|med|high> [default: high] ]]></option>
694 </term>
695 <listitem>
696 <para>When doing leak checking, determines how willing
697 Memcheck is to consider different backtraces to
698 be the same for the purposes of merging multiple leaks into a single
699 leak report. When set to <varname>low</varname>, only the first
700 two entries need match. When <varname>med</varname>, four entries
701 have to match. When <varname>high</varname>, all entries need to
702 match.</para>
703
704 <para>For hardcore leak debugging, you probably want to use
705 <option>--leak-resolution=high</option> together with
706 <option>--num-callers=40</option> or some such large number.
707 </para>
708
709 <para>Note that the <option>--leak-resolution</option> setting
710 does not affect Memcheck's ability to find
711 leaks. It only changes how the results are presented.</para>
712 </listitem>
713 </varlistentry>
714
philippe2193a7c2012-12-08 17:54:16 +0000715 <varlistentry id="opt.show-leak-kinds" xreflabel="--show-leak-kinds">
njnc1abdcb2009-08-05 05:11:02 +0000716 <term>
philippe2193a7c2012-12-08 17:54:16 +0000717 <option><![CDATA[--show-leak-kinds=<set> [default: definite,possible] ]]></option>
njnc1abdcb2009-08-05 05:11:02 +0000718 </term>
719 <listitem>
sewardj4c7254d2013-11-29 23:08:28 +0000720 <para>Specifies the leak kinds to show in a full leak search, in
721 one of the following ways:
philippe2193a7c2012-12-08 17:54:16 +0000722
723 <itemizedlist>
724 <listitem>a comma separated list of one or more of
725 <option>definite indirect possible reachable</option>.
726 </listitem>
727
728 <listitem><option>all</option> to specify the complete set (all leak kinds).
729 It is equivalent to
730 <option>--show-leak-kinds=definite,indirect,possible,reachable</option>.
731 </listitem>
732
sewardj4c7254d2013-11-29 23:08:28 +0000733 <listitem><option>none</option> for the empty set.
philippe2193a7c2012-12-08 17:54:16 +0000734 </listitem>
735 </itemizedlist>
736 </para>
njnc1abdcb2009-08-05 05:11:02 +0000737 </listitem>
738 </varlistentry>
739
philippe2193a7c2012-12-08 17:54:16 +0000740
741 <varlistentry id="opt.errors-for-leak-kinds" xreflabel="--errors-for-leak-kinds">
742 <term>
743 <option><![CDATA[--errors-for-leak-kinds=<set> [default: definite,possible] ]]></option>
744 </term>
745 <listitem>
746 <para>Specifies the leak kinds to count as errors in a full leak search. The
747 <option><![CDATA[<set>]]></option> is specified similarly to
748 <option>--show-leak-kinds</option>
749 </para>
750 </listitem>
751 </varlistentry>
752
753
philippeab1fce92013-09-29 13:47:32 +0000754 <varlistentry id="opt.leak-check-heuristics" xreflabel="--leak-check-heuristics">
755 <term>
756 <option><![CDATA[--leak-check-heuristics=<set> [default: none] ]]></option>
757 </term>
758 <listitem>
sewardj4c7254d2013-11-29 23:08:28 +0000759 <para>Specifies the set of leak check heuristics to be used
760 during
761 leak searches. The heuristics control which interior pointers
762 to a block cause it to be considered as reachable.
763 The heuristic set is specified in one of the following ways:
philippeab1fce92013-09-29 13:47:32 +0000764
765 <itemizedlist>
766 <listitem>a comma separated list of one or more of
767 <option>stdstring newarray multipleinheritance</option>.
768 </listitem>
769
770 <listitem><option>all</option> to activate the complete set of
771 heuristics.
772 It is equivalent to
773 <option>--leak-check-heuristics=stdstring,newarray,multipleinheritance</option>.
774 </listitem>
775
sewardj4c7254d2013-11-29 23:08:28 +0000776 <listitem><option>none</option> for the empty set.
philippeab1fce92013-09-29 13:47:32 +0000777 </listitem>
778 </itemizedlist>
779 </para>
780
781 <para>Note that these heuristics are dependent on the layout of the objects
782 produced by the C++ compiler. They have been tested with some gcc versions
783 (e.g. 4.4 and 4.7). They might not work properly with other C++ compilers.
784 </para>
785 </listitem>
786 </varlistentry>
787
788
philippe2193a7c2012-12-08 17:54:16 +0000789 <varlistentry id="opt.show-reachable" xreflabel="--show-reachable">
790 <term>
791 <option><![CDATA[--show-reachable=<yes|no> ]]></option>
792 </term>
793 <term>
794 <option><![CDATA[--show-possibly-lost=<yes|no> ]]></option>
795 </term>
796 <listitem>
797 <para>These options provide an alternative way to specify the leak kinds to show:
798 <itemizedlist>
799 <listitem>
800 <option>--show-reachable=no --show-possibly-lost=yes</option> is equivalent to
801 <option>--show-leak-kinds=definite,possible</option>.
802 </listitem>
803 <listitem>
804 <option>--show-reachable=no --show-possibly-lost=no</option> is equivalent to
805 <option>--show-leak-kinds=definite</option>.
806 </listitem>
807 <listitem>
808 <option>--show-reachable=yes</option> is equivalent to
809 <option>--show-leak-kinds=all</option>.
810 Note that <option>--show-possibly-lost=no</option> has no effect
811 if <option>--show-reachable=yes</option> is specified.
812 </listitem>
813 </itemizedlist>
814 </para>
815 </listitem>
816 </varlistentry>
817
njnc1abdcb2009-08-05 05:11:02 +0000818 <varlistentry id="opt.undef-value-errors" xreflabel="--undef-value-errors">
819 <term>
820 <option><![CDATA[--undef-value-errors=<yes|no> [default: yes] ]]></option>
821 </term>
822 <listitem>
823 <para>Controls whether Memcheck reports
824 uses of undefined value errors. Set this to
825 <varname>no</varname> if you don't want to see undefined value
826 errors. It also has the side effect of speeding up
827 Memcheck somewhat.
828 </para>
829 </listitem>
830 </varlistentry>
831
832 <varlistentry id="opt.track-origins" xreflabel="--track-origins">
833 <term>
834 <option><![CDATA[--track-origins=<yes|no> [default: no] ]]></option>
835 </term>
836 <listitem>
837 <para>Controls whether Memcheck tracks
838 the origin of uninitialised values. By default, it does not,
839 which means that although it can tell you that an
840 uninitialised value is being used in a dangerous way, it
841 cannot tell you where the uninitialised value came from. This
842 often makes it difficult to track down the root problem.
843 </para>
844 <para>When set
845 to <varname>yes</varname>, Memcheck keeps
846 track of the origins of all uninitialised values. Then, when
847 an uninitialised value error is
848 reported, Memcheck will try to show the
849 origin of the value. An origin can be one of the following
850 four places: a heap block, a stack allocation, a client
851 request, or miscellaneous other sources (eg, a call
852 to <varname>brk</varname>).
853 </para>
854 <para>For uninitialised values originating from a heap
855 block, Memcheck shows where the block was
856 allocated. For uninitialised values originating from a stack
857 allocation, Memcheck can tell you which
858 function allocated the value, but no more than that -- typically
859 it shows you the source location of the opening brace of the
860 function. So you should carefully check that all of the
861 function's local variables are initialised properly.
862 </para>
863 <para>Performance overhead: origin tracking is expensive. It
864 halves Memcheck's speed and increases
865 memory use by a minimum of 100MB, and possibly more.
866 Nevertheless it can drastically reduce the effort required to
867 identify the root cause of uninitialised value errors, and so
868 is often a programmer productivity win, despite running
869 more slowly.
870 </para>
871 <para>Accuracy: Memcheck tracks origins
872 quite accurately. To avoid very large space and time
873 overheads, some approximations are made. It is possible,
874 although unlikely, that Memcheck will report an incorrect origin, or
875 not be able to identify any origin.
876 </para>
877 <para>Note that the combination
878 <option>--track-origins=yes</option>
879 and <option>--undef-value-errors=no</option> is
880 nonsensical. Memcheck checks for and
881 rejects this combination at startup.
882 </para>
883 </listitem>
884 </varlistentry>
885
886 <varlistentry id="opt.partial-loads-ok" xreflabel="--partial-loads-ok">
887 <term>
888 <option><![CDATA[--partial-loads-ok=<yes|no> [default: no] ]]></option>
889 </term>
890 <listitem>
sewardj4c7254d2013-11-29 23:08:28 +0000891 <para>Controls how Memcheck handles 32-, 64-, 128- and 256-bit
892 naturally aligned loads from addresses for which some bytes are
njnc1abdcb2009-08-05 05:11:02 +0000893 addressable and others are not. When <varname>yes</varname>, such
894 loads do not produce an address error. Instead, loaded bytes
895 originating from illegal addresses are marked as uninitialised, and
896 those corresponding to legal addresses are handled in the normal
897 way.</para>
898
899 <para>When <varname>no</varname>, loads from partially invalid
900 addresses are treated the same as loads from completely invalid
901 addresses: an illegal-address error is issued, and the resulting
902 bytes are marked as initialised.</para>
903
904 <para>Note that code that behaves in this way is in violation of
mjw2be51222013-04-05 13:19:12 +0000905 the ISO C/C++ standards, and should be considered broken. If
njna3311642009-08-10 01:29:14 +0000906 at all possible, such code should be fixed. This option should be
njnc1abdcb2009-08-05 05:11:02 +0000907 used only as a last resort.</para>
908 </listitem>
909 </varlistentry>
910
philippe8617b5b2013-01-12 19:53:08 +0000911 <varlistentry id="opt.keep-stacktraces" xreflabel="--keep-stacktraces">
912 <term>
913 <option><![CDATA[--keep-stacktraces=alloc|free|alloc-and-free|alloc-then-free|none [default: alloc-then-free] ]]></option>
914 </term>
915 <listitem>
916 <para>Controls which stack trace(s) to keep for malloc'd and/or
917 free'd blocks.
918 </para>
919
sewardj4c7254d2013-11-29 23:08:28 +0000920 <para>With <varname>alloc-then-free</varname>, a stack trace is
921 recorded at allocation time, and is associated with the block.
922 When the block is freed, a second stack trace is recorded, and
923 this replaces the allocation stack trace. As a result, any "use
924 after free" errors relating to this block can only show a stack
925 trace for where the block was freed.
philippe8617b5b2013-01-12 19:53:08 +0000926 </para>
927
sewardj4c7254d2013-11-29 23:08:28 +0000928 <para>With <varname>alloc-and-free</varname>, both allocation
929 and the deallocation stack traces for the block are stored.
930 Hence a "use after free" error will
931 show both, which may make the error easier to diagnose.
932 Compared to <varname>alloc-then-free</varname>, this setting
933 slightly increases Valgrind's memory use as the block contains two
934 references instead of one.
philippe8617b5b2013-01-12 19:53:08 +0000935 </para>
936
sewardj4c7254d2013-11-29 23:08:28 +0000937 <para>With <varname>alloc</varname>, only the allocation stack
938 trace is recorded (and reported). With <varname>free</varname>,
939 only the deallocation stack trace is recorded (and reported).
940 These values somewhat decrease Valgrind's memory and cpu usage.
941 They can be useful depending on the error types you are
942 searching for and the level of detail you need to analyse
943 them. For example, if you are only interested in memory leak
944 errors, it is sufficient to record the allocation stack traces.
philippe8617b5b2013-01-12 19:53:08 +0000945 </para>
946
947 <para>With <varname>none</varname>, no stack traces are recorded
948 for malloc and free operations. If your program allocates a lot
sewardj4c7254d2013-11-29 23:08:28 +0000949 of blocks and/or allocates/frees from many different stack
950 traces, this can significantly decrease cpu and/or memory
951 required. Of course, few details will be reported for errors
952 related to heap blocks.
philippe8617b5b2013-01-12 19:53:08 +0000953 </para>
954
sewardj4c7254d2013-11-29 23:08:28 +0000955 <para>Note that once a stack trace is recorded, Valgrind keeps
956 the stack trace in memory even if it is not referenced by any
957 block. Some programs (for example, recursive algorithms) can
958 generate a huge number of stack traces. If Valgrind uses too
philippe8617b5b2013-01-12 19:53:08 +0000959 much memory in such circumstances, you can reduce the memory
sewardj4c7254d2013-11-29 23:08:28 +0000960 required with the options <varname>--keep-stacktraces</varname>
philippe8617b5b2013-01-12 19:53:08 +0000961 and/or by using a smaller value for the
962 option <varname>--num-callers</varname>.
963 </para>
964 </listitem>
965 </varlistentry>
966
njnc1abdcb2009-08-05 05:11:02 +0000967 <varlistentry id="opt.freelist-vol" xreflabel="--freelist-vol">
968 <term>
sewardje089f012010-10-13 21:47:29 +0000969 <option><![CDATA[--freelist-vol=<number> [default: 20000000] ]]></option>
njnc1abdcb2009-08-05 05:11:02 +0000970 </term>
971 <listitem>
972 <para>When the client program releases memory using
973 <function>free</function> (in <literal>C</literal>) or
974 <computeroutput>delete</computeroutput>
975 (<literal>C++</literal>), that memory is not immediately made
976 available for re-allocation. Instead, it is marked inaccessible
977 and placed in a queue of freed blocks. The purpose is to defer as
978 long as possible the point at which freed-up memory comes back
979 into circulation. This increases the chance that
980 Memcheck will be able to detect invalid
981 accesses to blocks for some significant period of time after they
982 have been freed.</para>
983
njna3311642009-08-10 01:29:14 +0000984 <para>This option specifies the maximum total size, in bytes, of the
sewardje089f012010-10-13 21:47:29 +0000985 blocks in the queue. The default value is twenty million bytes.
njnc1abdcb2009-08-05 05:11:02 +0000986 Increasing this increases the total amount of memory used by
987 Memcheck but may detect invalid uses of freed
988 blocks which would otherwise go undetected.</para>
989 </listitem>
990 </varlistentry>
991
sewardj403d8aa2011-10-22 19:48:57 +0000992 <varlistentry id="opt.freelist-big-blocks" xreflabel="--freelist-big-blocks">
993 <term>
994 <option><![CDATA[--freelist-big-blocks=<number> [default: 1000000] ]]></option>
995 </term>
996 <listitem>
997 <para>When making blocks from the queue of freed blocks available
998 for re-allocation, Memcheck will in priority re-circulate the blocks
999 with a size greater or equal to <option>--freelist-big-blocks</option>.
1000 This ensures that freeing big blocks (in particular freeing blocks bigger than
1001 <option>--freelist-vol</option>) does not immediately lead to a re-circulation
1002 of all (or a lot of) the small blocks in the free list. In other words,
1003 this option increases the likelihood to discover dangling pointers
1004 for the "small" blocks, even when big blocks are freed.</para>
1005 <para>Setting a value of 0 means that all the blocks are re-circulated
1006 in a FIFO order. </para>
1007 </listitem>
1008 </varlistentry>
1009
njnc1abdcb2009-08-05 05:11:02 +00001010 <varlistentry id="opt.workaround-gcc296-bugs" xreflabel="--workaround-gcc296-bugs">
1011 <term>
1012 <option><![CDATA[--workaround-gcc296-bugs=<yes|no> [default: no] ]]></option>
1013 </term>
1014 <listitem>
1015 <para>When enabled, assume that reads and writes some small
1016 distance below the stack pointer are due to bugs in GCC 2.96, and
1017 does not report them. The "small distance" is 256 bytes by
1018 default. Note that GCC 2.96 is the default compiler on some ancient
1019 Linux distributions (RedHat 7.X) and so you may need to use this
njna3311642009-08-10 01:29:14 +00001020 option. Do not use it if you do not have to, as it can cause real
njnc1abdcb2009-08-05 05:11:02 +00001021 errors to be overlooked. A better alternative is to use a more
1022 recent GCC in which this bug is fixed.</para>
1023
njna3311642009-08-10 01:29:14 +00001024 <para>You may also need to use this option when working with
njnc1abdcb2009-08-05 05:11:02 +00001025 GCC 3.X or 4.X on 32-bit PowerPC Linux. This is because
1026 GCC generates code which occasionally accesses below the
1027 stack pointer, particularly for floating-point to/from integer
1028 conversions. This is in violation of the 32-bit PowerPC ELF
1029 specification, which makes no provision for locations below the
1030 stack pointer to be accessible.</para>
1031 </listitem>
1032 </varlistentry>
1033
1034 <varlistentry id="opt.ignore-ranges" xreflabel="--ignore-ranges">
1035 <term>
1036 <option><![CDATA[--ignore-ranges=0xPP-0xQQ[,0xRR-0xSS] ]]></option>
1037 </term>
1038 <listitem>
1039 <para>Any ranges listed in this option (and multiple ranges can be
1040 specified, separated by commas) will be ignored by Memcheck's
1041 addressability checking.</para>
1042 </listitem>
1043 </varlistentry>
1044
1045 <varlistentry id="opt.malloc-fill" xreflabel="--malloc-fill">
1046 <term>
1047 <option><![CDATA[--malloc-fill=<hexnumber> ]]></option>
1048 </term>
1049 <listitem>
1050 <para>Fills blocks allocated
1051 by <computeroutput>malloc</computeroutput>,
1052 <computeroutput>new</computeroutput>, etc, but not
1053 by <computeroutput>calloc</computeroutput>, with the specified
1054 byte. This can be useful when trying to shake out obscure
1055 memory corruption problems. The allocated area is still
njna3311642009-08-10 01:29:14 +00001056 regarded by Memcheck as undefined -- this option only affects its
philippea2cc0c02012-05-11 22:10:39 +00001057 contents. Note that <option>--malloc-fill</option> does not
1058 affect a block of memory when it is used as argument
1059 to client requests VALGRIND_MEMPOOL_ALLOC or
1060 VALGRIND_MALLOCLIKE_BLOCK.
njnc1abdcb2009-08-05 05:11:02 +00001061 </para>
1062 </listitem>
1063 </varlistentry>
1064
1065 <varlistentry id="opt.free-fill" xreflabel="--free-fill">
1066 <term>
1067 <option><![CDATA[--free-fill=<hexnumber> ]]></option>
1068 </term>
1069 <listitem>
1070 <para>Fills blocks freed
1071 by <computeroutput>free</computeroutput>,
1072 <computeroutput>delete</computeroutput>, etc, with the
1073 specified byte value. This can be useful when trying to shake out
1074 obscure memory corruption problems. The freed area is still
njna3311642009-08-10 01:29:14 +00001075 regarded by Memcheck as not valid for access -- this option only
philippea2cc0c02012-05-11 22:10:39 +00001076 affects its contents. Note that <option>--free-fill</option> does not
1077 affect a block of memory when it is used as argument to
1078 client requests VALGRIND_MEMPOOL_FREE or VALGRIND_FREELIKE_BLOCK.
njnc1abdcb2009-08-05 05:11:02 +00001079 </para>
1080 </listitem>
1081 </varlistentry>
1082
1083</variablelist>
1084<!-- end of xi:include in the manpage -->
1085
1086</sect1>
1087
1088
njn62ad73d2005-08-15 04:26:13 +00001089<sect1 id="mc-manual.suppfiles" xreflabel="Writing suppression files">
1090<title>Writing suppression files</title>
njn3e986b22004-11-30 10:43:45 +00001091
1092<para>The basic suppression format is described in
1093<xref linkend="manual-core.suppress"/>.</para>
1094
sewardj08e31e22007-05-23 21:58:33 +00001095<para>The suppression-type (second) line should have the form:</para>
njn3e986b22004-11-30 10:43:45 +00001096<programlisting><![CDATA[
1097Memcheck:suppression_type]]></programlisting>
1098
njn3e986b22004-11-30 10:43:45 +00001099<para>The Memcheck suppression types are as follows:</para>
1100
1101<itemizedlist>
1102 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001103 <para><varname>Value1</varname>,
1104 <varname>Value2</varname>,
1105 <varname>Value4</varname>,
1106 <varname>Value8</varname>,
1107 <varname>Value16</varname>,
njn3e986b22004-11-30 10:43:45 +00001108 meaning an uninitialised-value error when
1109 using a value of 1, 2, 4, 8 or 16 bytes.</para>
1110 </listitem>
1111
1112 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001113 <para><varname>Cond</varname> (or its old
de03e0e7c2005-12-03 23:02:33 +00001114 name, <varname>Value0</varname>), meaning use
njn3e986b22004-11-30 10:43:45 +00001115 of an uninitialised CPU condition code.</para>
1116 </listitem>
1117
1118 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001119 <para><varname>Addr1</varname>,
de03e0e7c2005-12-03 23:02:33 +00001120 <varname>Addr2</varname>,
1121 <varname>Addr4</varname>,
1122 <varname>Addr8</varname>,
1123 <varname>Addr16</varname>,
njn3e986b22004-11-30 10:43:45 +00001124 meaning an invalid address during a
1125 memory access of 1, 2, 4, 8 or 16 bytes respectively.</para>
1126 </listitem>
1127
1128 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001129 <para><varname>Jump</varname>, meaning an
njn718d3b12006-12-16 00:54:12 +00001130 jump to an unaddressable location error.</para>
1131 </listitem>
1132
1133 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001134 <para><varname>Param</varname>, meaning an
njn3e986b22004-11-30 10:43:45 +00001135 invalid system call parameter error.</para>
1136 </listitem>
1137
1138 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001139 <para><varname>Free</varname>, meaning an
njn3e986b22004-11-30 10:43:45 +00001140 invalid or mismatching free.</para>
1141 </listitem>
1142
1143 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001144 <para><varname>Overlap</varname>, meaning a
njn3e986b22004-11-30 10:43:45 +00001145 <computeroutput>src</computeroutput> /
1146 <computeroutput>dst</computeroutput> overlap in
njn2f7eebe2009-08-05 06:34:27 +00001147 <function>memcpy</function> or a similar function.</para>
njn3e986b22004-11-30 10:43:45 +00001148 </listitem>
1149
1150 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001151 <para><varname>Leak</varname>, meaning
njn62ad73d2005-08-15 04:26:13 +00001152 a memory leak.</para>
njn3e986b22004-11-30 10:43:45 +00001153 </listitem>
1154
1155</itemizedlist>
1156
philippe2193a7c2012-12-08 17:54:16 +00001157<para><computeroutput>Param</computeroutput> errors have a mandatory extra
sewardj08e31e22007-05-23 21:58:33 +00001158information line at this point, which is the name of the offending
philippe2193a7c2012-12-08 17:54:16 +00001159system call parameter. </para>
1160
sewardj4c7254d2013-11-29 23:08:28 +00001161<para><computeroutput>Leak</computeroutput> errors have an optional
1162extra information line, with the following format:</para>
philippe2193a7c2012-12-08 17:54:16 +00001163<programlisting><![CDATA[
1164match-leak-kinds:<set>]]></programlisting>
1165<para>where <computeroutput>&lt;set&gt;</computeroutput> specifies which
1166leak kinds are matched by this suppression entry.
sewardj4c7254d2013-11-29 23:08:28 +00001167<computeroutput>&lt;set&gt;</computeroutput> is specified in the
1168same way as with the option <option>--show-leak-kinds</option>, that is,
1169one of the following:</para>
1170<itemizedlist>
1171 <listitem>a comma separated list of one or more of
1172 <option>definite indirect possible reachable</option>.
1173 </listitem>
philippe2193a7c2012-12-08 17:54:16 +00001174
sewardj4c7254d2013-11-29 23:08:28 +00001175 <listitem><option>all</option> to specify the complete set (all leak kinds).
1176 </listitem>
1177
1178 <listitem><option>none</option> for the empty set.
1179 </listitem>
1180</itemizedlist>
1181<para>If this optional extra line is not present, the suppression
1182entry will match all leak kinds.</para>
1183
1184<para>Be aware that leak suppressions that are created using
1185<option>--gen-suppressions</option> will contain this optional extra
1186line, and therefore may match fewer leaks than you expect. You may
1187want to remove the line before using the generated
1188suppressions.</para>
1189
1190<para>The other Memcheck error kinds do not have extra lines.</para>
njn3e986b22004-11-30 10:43:45 +00001191
philippe4e32d672013-10-17 22:10:41 +00001192<para>
1193If you give the <option>-v</option> option, Valgrind will print
sewardj4c7254d2013-11-29 23:08:28 +00001194the list of used suppressions at the end of execution.
philippe4e32d672013-10-17 22:10:41 +00001195For a leak suppression, this output gives the number of different
sewardj4c7254d2013-11-29 23:08:28 +00001196loss records that match the suppression, and the number of bytes
1197and blocks suppressed by the suppression.
1198If the run contains multiple leak checks, the number of bytes and blocks
1199are reset to zero before each new leak check. Note that the number of different
1200loss records is not reset to zero.</para>
philippe4e32d672013-10-17 22:10:41 +00001201<para>In the example below, in the last leak search, 7 blocks and 96 bytes have
sewardj4c7254d2013-11-29 23:08:28 +00001202been suppressed by a suppression with the name
1203<option>some_leak_suppression</option>:</para>
philippe4e32d672013-10-17 22:10:41 +00001204<programlisting><![CDATA[
1205--21041-- used_suppression: 10 some_other_leak_suppression s.supp:14 suppressed: 12,400 bytes in 1 blocks
1206--21041-- used_suppression: 39 some_leak_suppression s.supp:2 suppressed: 96 bytes in 7 blocks
1207]]></programlisting>
philippe4e32d672013-10-17 22:10:41 +00001208
sewardj4c7254d2013-11-29 23:08:28 +00001209<para>For <varname>ValueN</varname> and <varname>AddrN</varname>
1210errors, the first line of the calling context is either the name of
1211the function in which the error occurred, or, failing that, the full
1212path of the <filename>.so</filename> file or executable containing the
1213error location. For <varname>Free</varname> errors, the first line is
1214the name of the function doing the freeing (eg,
1215<function>free</function>, <function>__builtin_vec_delete</function>,
1216etc). For <varname>Overlap</varname> errors, the first line is the name of the
1217function with the overlapping arguments (eg.
1218<function>memcpy</function>, <function>strcpy</function>, etc).</para>
njn3e986b22004-11-30 10:43:45 +00001219
sewardj4c7254d2013-11-29 23:08:28 +00001220<para>The last part of any suppression specifies the rest of the
1221calling context that needs to be matched.</para>
njn3e986b22004-11-30 10:43:45 +00001222
1223</sect1>
1224
1225
1226
1227<sect1 id="mc-manual.machine"
1228 xreflabel="Details of Memcheck's checking machinery">
1229<title>Details of Memcheck's checking machinery</title>
1230
1231<para>Read this section if you want to know, in detail, exactly
1232what and how Memcheck is checking.</para>
1233
1234
1235<sect2 id="mc-manual.value" xreflabel="Valid-value (V) bit">
1236<title>Valid-value (V) bits</title>
1237
de03e0e7c2005-12-03 23:02:33 +00001238<para>It is simplest to think of Memcheck implementing a synthetic CPU
1239which is identical to a real CPU, except for one crucial detail. Every
1240bit (literally) of data processed, stored and handled by the real CPU
1241has, in the synthetic CPU, an associated "valid-value" bit, which says
1242whether or not the accompanying bit has a legitimate value. In the
1243discussions which follow, this bit is referred to as the V (valid-value)
njn3e986b22004-11-30 10:43:45 +00001244bit.</para>
1245
de03e0e7c2005-12-03 23:02:33 +00001246<para>Each byte in the system therefore has a 8 V bits which follow it
1247wherever it goes. For example, when the CPU loads a word-size item (4
1248bytes) from memory, it also loads the corresponding 32 V bits from a
1249bitmap which stores the V bits for the process' entire address space.
1250If the CPU should later write the whole or some part of that value to
1251memory at a different address, the relevant V bits will be stored back
1252in the V-bit bitmap.</para>
njn3e986b22004-11-30 10:43:45 +00001253
njn2f7eebe2009-08-05 06:34:27 +00001254<para>In short, each bit in the system has (conceptually) an associated V
1255bit, which follows it around everywhere, even inside the CPU. Yes, all the
1256CPU's registers (integer, floating point, vector and condition registers)
1257have their own V bit vectors. For this to work, Memcheck uses a great deal
1258of compression to represent the V bits compactly.</para>
njn3e986b22004-11-30 10:43:45 +00001259
de03e0e7c2005-12-03 23:02:33 +00001260<para>Copying values around does not cause Memcheck to check for, or
1261report on, errors. However, when a value is used in a way which might
njn2f7eebe2009-08-05 06:34:27 +00001262conceivably affect your program's externally-visible behaviour,
1263the associated V bits are immediately checked. If any of these indicate
1264that the value is undefined (even partially), an error is reported.</para>
njn3e986b22004-11-30 10:43:45 +00001265
1266<para>Here's an (admittedly nonsensical) example:</para>
1267<programlisting><![CDATA[
1268int i, j;
1269int a[10], b[10];
1270for ( i = 0; i < 10; i++ ) {
1271 j = a[i];
1272 b[i] = j;
1273}]]></programlisting>
1274
de03e0e7c2005-12-03 23:02:33 +00001275<para>Memcheck emits no complaints about this, since it merely copies
1276uninitialised values from <varname>a[]</varname> into
sewardj08e31e22007-05-23 21:58:33 +00001277<varname>b[]</varname>, and doesn't use them in a way which could
1278affect the behaviour of the program. However, if
de03e0e7c2005-12-03 23:02:33 +00001279the loop is changed to:</para>
njn3e986b22004-11-30 10:43:45 +00001280<programlisting><![CDATA[
1281for ( i = 0; i < 10; i++ ) {
1282 j += a[i];
1283}
1284if ( j == 77 )
1285 printf("hello there\n");
1286]]></programlisting>
1287
sewardj08e31e22007-05-23 21:58:33 +00001288<para>then Memcheck will complain, at the
de03e0e7c2005-12-03 23:02:33 +00001289<computeroutput>if</computeroutput>, that the condition depends on
1290uninitialised values. Note that it <command>doesn't</command> complain
1291at the <varname>j += a[i];</varname>, since at that point the
1292undefinedness is not "observable". It's only when a decision has to be
1293made as to whether or not to do the <function>printf</function> -- an
1294observable action of your program -- that Memcheck complains.</para>
njn3e986b22004-11-30 10:43:45 +00001295
de03e0e7c2005-12-03 23:02:33 +00001296<para>Most low level operations, such as adds, cause Memcheck to use the
1297V bits for the operands to calculate the V bits for the result. Even if
1298the result is partially or wholly undefined, it does not
njn62ad73d2005-08-15 04:26:13 +00001299complain.</para>
njn3e986b22004-11-30 10:43:45 +00001300
de03e0e7c2005-12-03 23:02:33 +00001301<para>Checks on definedness only occur in three places: when a value is
1302used to generate a memory address, when control flow decision needs to
sewardj08e31e22007-05-23 21:58:33 +00001303be made, and when a system call is detected, Memcheck checks definedness
de03e0e7c2005-12-03 23:02:33 +00001304of parameters as required.</para>
njn3e986b22004-11-30 10:43:45 +00001305
1306<para>If a check should detect undefinedness, an error message is
de03e0e7c2005-12-03 23:02:33 +00001307issued. The resulting value is subsequently regarded as well-defined.
sewardj08e31e22007-05-23 21:58:33 +00001308To do otherwise would give long chains of error messages. In other
1309words, once Memcheck reports an undefined value error, it tries to
1310avoid reporting further errors derived from that same undefined
1311value.</para>
njn3e986b22004-11-30 10:43:45 +00001312
de03e0e7c2005-12-03 23:02:33 +00001313<para>This sounds overcomplicated. Why not just check all reads from
1314memory, and complain if an undefined value is loaded into a CPU
1315register? Well, that doesn't work well, because perfectly legitimate C
1316programs routinely copy uninitialised values around in memory, and we
1317don't want endless complaints about that. Here's the canonical example.
1318Consider a struct like this:</para>
njn3e986b22004-11-30 10:43:45 +00001319<programlisting><![CDATA[
1320struct S { int x; char c; };
1321struct S s1, s2;
1322s1.x = 42;
1323s1.c = 'z';
1324s2 = s1;
1325]]></programlisting>
1326
de03e0e7c2005-12-03 23:02:33 +00001327<para>The question to ask is: how large is <varname>struct S</varname>,
1328in bytes? An <varname>int</varname> is 4 bytes and a
1329<varname>char</varname> one byte, so perhaps a <varname>struct
sewardj08e31e22007-05-23 21:58:33 +00001330S</varname> occupies 5 bytes? Wrong. All non-toy compilers we know
de03e0e7c2005-12-03 23:02:33 +00001331of will round the size of <varname>struct S</varname> up to a whole
1332number of words, in this case 8 bytes. Not doing this forces compilers
sewardj08e31e22007-05-23 21:58:33 +00001333to generate truly appalling code for accessing arrays of
1334<varname>struct S</varname>'s on some architectures.</para>
njn3e986b22004-11-30 10:43:45 +00001335
de03e0e7c2005-12-03 23:02:33 +00001336<para>So <varname>s1</varname> occupies 8 bytes, yet only 5 of them will
njn7316df22009-08-04 01:16:01 +00001337be initialised. For the assignment <varname>s2 = s1</varname>, GCC
de03e0e7c2005-12-03 23:02:33 +00001338generates code to copy all 8 bytes wholesale into <varname>s2</varname>
1339without regard for their meaning. If Memcheck simply checked values as
1340they came out of memory, it would yelp every time a structure assignment
sewardj08e31e22007-05-23 21:58:33 +00001341like this happened. So the more complicated behaviour described above
njn7316df22009-08-04 01:16:01 +00001342is necessary. This allows GCC to copy
de03e0e7c2005-12-03 23:02:33 +00001343<varname>s1</varname> into <varname>s2</varname> any way it likes, and a
1344warning will only be emitted if the uninitialised values are later
1345used.</para>
njn3e986b22004-11-30 10:43:45 +00001346
njn3e986b22004-11-30 10:43:45 +00001347</sect2>
1348
1349
1350<sect2 id="mc-manual.vaddress" xreflabel=" Valid-address (A) bits">
1351<title>Valid-address (A) bits</title>
1352
de03e0e7c2005-12-03 23:02:33 +00001353<para>Notice that the previous subsection describes how the validity of
1354values is established and maintained without having to say whether the
1355program does or does not have the right to access any particular memory
sewardj08e31e22007-05-23 21:58:33 +00001356location. We now consider the latter question.</para>
njn3e986b22004-11-30 10:43:45 +00001357
de03e0e7c2005-12-03 23:02:33 +00001358<para>As described above, every bit in memory or in the CPU has an
1359associated valid-value (V) bit. In addition, all bytes in memory, but
1360not in the CPU, have an associated valid-address (A) bit. This
1361indicates whether or not the program can legitimately read or write that
sewardj49d5a282011-02-28 10:26:42 +00001362location. It does not give any indication of the validity of the data
de03e0e7c2005-12-03 23:02:33 +00001363at that location -- that's the job of the V bits -- only whether or not
1364the location may be accessed.</para>
njn3e986b22004-11-30 10:43:45 +00001365
de03e0e7c2005-12-03 23:02:33 +00001366<para>Every time your program reads or writes memory, Memcheck checks
1367the A bits associated with the address. If any of them indicate an
1368invalid address, an error is emitted. Note that the reads and writes
1369themselves do not change the A bits, only consult them.</para>
njn3e986b22004-11-30 10:43:45 +00001370
njn62ad73d2005-08-15 04:26:13 +00001371<para>So how do the A bits get set/cleared? Like this:</para>
njn3e986b22004-11-30 10:43:45 +00001372
1373<itemizedlist>
1374 <listitem>
1375 <para>When the program starts, all the global data areas are
1376 marked as accessible.</para>
1377 </listitem>
1378
1379 <listitem>
bartaf25f672009-06-26 19:03:53 +00001380 <para>When the program does
1381 <function>malloc</function>/<computeroutput>new</computeroutput>,
1382 the A bits for exactly the area allocated, and not a byte more,
1383 are marked as accessible. Upon freeing the area the A bits are
1384 changed to indicate inaccessibility.</para>
njn3e986b22004-11-30 10:43:45 +00001385 </listitem>
1386
1387 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001388 <para>When the stack pointer register (<literal>SP</literal>) moves
1389 up or down, A bits are set. The rule is that the area from
1390 <literal>SP</literal> up to the base of the stack is marked as
1391 accessible, and below <literal>SP</literal> is inaccessible. (If
1392 that sounds illogical, bear in mind that the stack grows down, not
1393 up, on almost all Unix systems, including GNU/Linux.) Tracking
1394 <literal>SP</literal> like this has the useful side-effect that the
1395 section of stack used by a function for local variables etc is
1396 automatically marked accessible on function entry and inaccessible
1397 on exit.</para>
njn3e986b22004-11-30 10:43:45 +00001398 </listitem>
1399
1400 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001401 <para>When doing system calls, A bits are changed appropriately.
sewardj08e31e22007-05-23 21:58:33 +00001402 For example, <literal>mmap</literal>
1403 magically makes files appear in the process'
1404 address space, so the A bits must be updated if <literal>mmap</literal>
de03e0e7c2005-12-03 23:02:33 +00001405 succeeds.</para>
njn3e986b22004-11-30 10:43:45 +00001406 </listitem>
1407
1408 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001409 <para>Optionally, your program can tell Memcheck about such changes
de03e0e7c2005-12-03 23:02:33 +00001410 explicitly, using the client request mechanism described
1411 above.</para>
njn3e986b22004-11-30 10:43:45 +00001412 </listitem>
1413
1414</itemizedlist>
1415
1416</sect2>
1417
1418
1419<sect2 id="mc-manual.together" xreflabel="Putting it all together">
1420<title>Putting it all together</title>
1421
1422<para>Memcheck's checking machinery can be summarised as
1423follows:</para>
1424
1425<itemizedlist>
1426 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001427 <para>Each byte in memory has 8 associated V (valid-value) bits,
1428 saying whether or not the byte has a defined value, and a single A
1429 (valid-address) bit, saying whether or not the program currently has
sewardje089f012010-10-13 21:47:29 +00001430 the right to read/write that address. As mentioned above, heavy
1431 use of compression means the overhead is typically around 25%.</para>
njn3e986b22004-11-30 10:43:45 +00001432 </listitem>
1433
1434 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001435 <para>When memory is read or written, the relevant A bits are
sewardj08e31e22007-05-23 21:58:33 +00001436 consulted. If they indicate an invalid address, Memcheck emits an
de03e0e7c2005-12-03 23:02:33 +00001437 Invalid read or Invalid write error.</para>
njn3e986b22004-11-30 10:43:45 +00001438 </listitem>
1439
1440 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001441 <para>When memory is read into the CPU's registers, the relevant V
1442 bits are fetched from memory and stored in the simulated CPU. They
1443 are not consulted.</para>
njn3e986b22004-11-30 10:43:45 +00001444 </listitem>
1445
1446 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001447 <para>When a register is written out to memory, the V bits for that
1448 register are written back to memory too.</para>
njn3e986b22004-11-30 10:43:45 +00001449 </listitem>
1450
1451 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001452 <para>When values in CPU registers are used to generate a memory
1453 address, or to determine the outcome of a conditional branch, the V
1454 bits for those values are checked, and an error emitted if any of
1455 them are undefined.</para>
njn3e986b22004-11-30 10:43:45 +00001456 </listitem>
1457
1458 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001459 <para>When values in CPU registers are used for any other purpose,
sewardj08e31e22007-05-23 21:58:33 +00001460 Memcheck computes the V bits for the result, but does not check
de03e0e7c2005-12-03 23:02:33 +00001461 them.</para>
njn3e986b22004-11-30 10:43:45 +00001462 </listitem>
1463
1464 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001465 <para>Once the V bits for a value in the CPU have been checked, they
de03e0e7c2005-12-03 23:02:33 +00001466 are then set to indicate validity. This avoids long chains of
1467 errors.</para>
njn3e986b22004-11-30 10:43:45 +00001468 </listitem>
1469
1470 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001471 <para>When values are loaded from memory, Memcheck checks the A bits
de03e0e7c2005-12-03 23:02:33 +00001472 for that location and issues an illegal-address warning if needed.
1473 In that case, the V bits loaded are forced to indicate Valid,
1474 despite the location being invalid.</para>
1475
1476 <para>This apparently strange choice reduces the amount of confusing
1477 information presented to the user. It avoids the unpleasant
1478 phenomenon in which memory is read from a place which is both
sewardj33878892007-11-17 09:43:25 +00001479 unaddressable and contains invalid values, and, as a result, you get
de03e0e7c2005-12-03 23:02:33 +00001480 not only an invalid-address (read/write) error, but also a
1481 potentially large set of uninitialised-value errors, one for every
1482 time the value is used.</para>
1483
1484 <para>There is a hazy boundary case to do with multi-byte loads from
1485 addresses which are partially valid and partially invalid. See
njna3311642009-08-10 01:29:14 +00001486 details of the option <option>--partial-loads-ok</option> for details.
de03e0e7c2005-12-03 23:02:33 +00001487 </para>
njn3e986b22004-11-30 10:43:45 +00001488 </listitem>
1489
1490</itemizedlist>
1491
1492
bartaf25f672009-06-26 19:03:53 +00001493<para>Memcheck intercepts calls to <function>malloc</function>,
1494<function>calloc</function>, <function>realloc</function>,
1495<function>valloc</function>, <function>memalign</function>,
1496<function>free</function>, <computeroutput>new</computeroutput>,
1497<computeroutput>new[]</computeroutput>,
1498<computeroutput>delete</computeroutput> and
1499<computeroutput>delete[]</computeroutput>. The behaviour you get
njn3e986b22004-11-30 10:43:45 +00001500is:</para>
1501
1502<itemizedlist>
1503
1504 <listitem>
bartaf25f672009-06-26 19:03:53 +00001505 <para><function>malloc</function>/<function>new</function>/<computeroutput>new[]</computeroutput>:
1506 the returned memory is marked as addressable but not having valid
1507 values. This means you have to write to it before you can read
1508 it.</para>
njn3e986b22004-11-30 10:43:45 +00001509 </listitem>
1510
1511 <listitem>
bartaf25f672009-06-26 19:03:53 +00001512 <para><function>calloc</function>: returned memory is marked both
1513 addressable and valid, since <function>calloc</function> clears
1514 the area to zero.</para>
njn3e986b22004-11-30 10:43:45 +00001515 </listitem>
1516
1517 <listitem>
bartaf25f672009-06-26 19:03:53 +00001518 <para><function>realloc</function>: if the new size is larger than
1519 the old, the new section is addressable but invalid, as with
njn2f7eebe2009-08-05 06:34:27 +00001520 <function>malloc</function>. If the new size is smaller, the
1521 dropped-off section is marked as unaddressable. You may only pass to
bartaf25f672009-06-26 19:03:53 +00001522 <function>realloc</function> a pointer previously issued to you by
1523 <function>malloc</function>/<function>calloc</function>/<function>realloc</function>.</para>
njn3e986b22004-11-30 10:43:45 +00001524 </listitem>
1525
1526 <listitem>
bartaf25f672009-06-26 19:03:53 +00001527 <para><function>free</function>/<computeroutput>delete</computeroutput>/<computeroutput>delete[]</computeroutput>:
1528 you may only pass to these functions a pointer previously issued
1529 to you by the corresponding allocation function. Otherwise,
1530 Memcheck complains. If the pointer is indeed valid, Memcheck
1531 marks the entire area it points at as unaddressable, and places
1532 the block in the freed-blocks-queue. The aim is to defer as long
1533 as possible reallocation of this block. Until that happens, all
1534 attempts to access it will elicit an invalid-address error, as you
1535 would hope.</para>
njn3e986b22004-11-30 10:43:45 +00001536 </listitem>
1537
1538</itemizedlist>
1539
1540</sect2>
1541</sect1>
1542
sewardj3b290482011-05-06 21:02:55 +00001543<sect1 id="mc-manual.monitor-commands" xreflabel="Memcheck Monitor Commands">
1544<title>Memcheck Monitor Commands</title>
sewardjc8bd1df2011-06-26 12:41:33 +00001545<para>The Memcheck tool provides monitor commands handled by Valgrind's
1546built-in gdbserver (see <xref linkend="manual-core-adv.gdbserver-commandhandling"/>).
sewardj3b290482011-05-06 21:02:55 +00001547</para>
njn3e986b22004-11-30 10:43:45 +00001548
sewardj3b290482011-05-06 21:02:55 +00001549<itemizedlist>
1550 <listitem>
sewardj30b3eca2011-06-28 08:20:39 +00001551 <para><varname>get_vbits &lt;addr&gt; [&lt;len&gt;]</varname>
sewardjc8bd1df2011-06-26 12:41:33 +00001552 shows the definedness (V) bits for &lt;len&gt; (default 1) bytes
1553 starting at &lt;addr&gt;. The definedness of each byte in the
1554 range is given using two hexadecimal digits. These hexadecimal
1555 digits encode the validity of each bit of the corresponding byte,
1556 using 0 if the bit is defined and 1 if the bit is undefined.
1557 If a byte is not addressable, its validity bits are replaced
1558 by <varname>__</varname> (a double underscore).
1559 </para>
1560 <para>
1561 In the following example, <varname>string10</varname> is an array
1562 of 10 characters, in which the even numbered bytes are
1563 undefined. In the below example, the byte corresponding
1564 to <varname>string10[5]</varname> is not addressable.
1565 </para>
sewardj3b290482011-05-06 21:02:55 +00001566<programlisting><![CDATA[
1567(gdb) p &string10
1568$4 = (char (*)[10]) 0x8049e28
sewardj30b3eca2011-06-28 08:20:39 +00001569(gdb) monitor get_vbits 0x8049e28 10
sewardj3b290482011-05-06 21:02:55 +00001570ff00ff00 ff__ff00 ff00
1571(gdb)
1572]]></programlisting>
sewardj30b3eca2011-06-28 08:20:39 +00001573
1574 <para> The command get_vbits cannot be used with registers. To get
1575 the validity bits of a register, you must start Valgrind with the
1576 option <option>--vgdb-shadow-registers=yes</option>. The validity
1577 bits of a register can be obtained by printing the 'shadow 1'
1578 corresponding register. In the below x86 example, the register
1579 eax has all its bits undefined, while the register ebx is fully
1580 defined.
1581 </para>
1582<programlisting><![CDATA[
1583(gdb) p /x $eaxs1
1584$9 = 0xffffffff
1585(gdb) p /x $ebxs1
1586$10 = 0x0
1587(gdb)
1588]]></programlisting>
1589
sewardj3b290482011-05-06 21:02:55 +00001590 </listitem>
1591
1592 <listitem>
sewardj30b3eca2011-06-28 08:20:39 +00001593 <para><varname>make_memory
1594 [noaccess|undefined|defined|Definedifaddressable] &lt;addr&gt;
sewardjc8bd1df2011-06-26 12:41:33 +00001595 [&lt;len&gt;]</varname> marks the range of &lt;len&gt; (default 1)
1596 bytes at &lt;addr&gt; as having the given status. Parameter
1597 <varname>noaccess</varname> marks the range as non-accessible, so
1598 Memcheck will report an error on any access to it.
1599 <varname>undefined</varname> or <varname>defined</varname> mark
1600 the area as accessible, but Memcheck regards the bytes in it
1601 respectively as having undefined or defined values.
sewardj30b3eca2011-06-28 08:20:39 +00001602 <varname>Definedifaddressable</varname> marks as defined, bytes in
sewardjc8bd1df2011-06-26 12:41:33 +00001603 the range which are already addressible, but makes no change to
sewardj30b3eca2011-06-28 08:20:39 +00001604 the status of bytes in the range which are not addressible. Note
1605 that the first letter of <varname>Definedifaddressable</varname>
1606 is an uppercase D to avoid confusion with <varname>defined</varname>.
1607 </para>
sewardjc8bd1df2011-06-26 12:41:33 +00001608
1609 <para>
1610 In the following example, the first byte of the
1611 <varname>string10</varname> is marked as defined:
sewardj3b290482011-05-06 21:02:55 +00001612 </para>
1613<programlisting><![CDATA[
sewardj30b3eca2011-06-28 08:20:39 +00001614(gdb) monitor make_memory defined 0x8049e28 1
1615(gdb) monitor get_vbits 0x8049e28 10
sewardj3b290482011-05-06 21:02:55 +000016160000ff00 ff00ff00 ff00
1617(gdb)
1618]]></programlisting>
1619 </listitem>
1620
1621 <listitem>
sewardj30b3eca2011-06-28 08:20:39 +00001622 <para><varname>check_memory [addressable|defined] &lt;addr&gt;
sewardj3b290482011-05-06 21:02:55 +00001623 [&lt;len&gt;]</varname> checks that the range of &lt;len&gt;
sewardj30b3eca2011-06-28 08:20:39 +00001624 (default 1) bytes at &lt;addr&gt; has the specified accessibility.
1625 It then outputs a description of &lt;addr&gt;. In the following
1626 example, a detailed description is available because the
philippea22f59d2012-01-26 23:13:52 +00001627 option <option>--read-var-info=yes</option> was given at Valgrind
sewardj30b3eca2011-06-28 08:20:39 +00001628 startup:
sewardj3b290482011-05-06 21:02:55 +00001629 </para>
1630<programlisting><![CDATA[
sewardj30b3eca2011-06-28 08:20:39 +00001631(gdb) monitor check_memory defined 0x8049e28 1
sewardj3b290482011-05-06 21:02:55 +00001632Address 0x8049E28 len 1 defined
1633==14698== Location 0x8049e28 is 0 bytes inside string10[0],
1634==14698== declared at prog.c:10, in frame #0 of thread 1
1635(gdb)
1636]]></programlisting>
1637 </listitem>
1638
1639 <listitem>
sewardj30b3eca2011-06-28 08:20:39 +00001640 <para><varname>leak_check [full*|summary]
philippe2193a7c2012-12-08 17:54:16 +00001641 [kinds &lt;set&gt;|reachable|possibleleak*|definiteleak]
philippe278b2a32013-10-09 20:12:39 +00001642 [heuristics heur1,heur2,...]
sewardj30b3eca2011-06-28 08:20:39 +00001643 [increased*|changed|any]
philippe84234902012-01-14 13:53:13 +00001644 [unlimited*|limited &lt;max_loss_records_output&gt;]
sewardjc8bd1df2011-06-26 12:41:33 +00001645 </varname>
1646 performs a leak check. The <varname>*</varname> in the arguments
philippe84234902012-01-14 13:53:13 +00001647 indicates the default values. </para>
sewardj3b290482011-05-06 21:02:55 +00001648
philippe278b2a32013-10-09 20:12:39 +00001649 <para> If the <varname>[full*|summary]</varname> argument is
1650 <varname>summary</varname>, only a summary of the leak search is given;
1651 otherwise a full leak report is produced. A full leak report gives
1652 detailed information for each leak: the stack trace where the leaked blocks
1653 were allocated, the number of blocks leaked and their total size. When a
1654 full report is requested, the next two arguments further specify what
sewardjc8bd1df2011-06-26 12:41:33 +00001655 kind of leaks to report. A leak's details are shown if they match
philippe84234902012-01-14 13:53:13 +00001656 both the second and third argument. A full leak report might
1657 output detailed information for many leaks. The nr of leaks for
1658 which information is output can be controlled using
1659 the <varname>limited</varname> argument followed by the maximum nr
1660 of leak records to output. If this maximum is reached, the leak
1661 search outputs the records with the biggest number of bytes.
sewardj3b290482011-05-06 21:02:55 +00001662 </para>
1663
philippe278b2a32013-10-09 20:12:39 +00001664 <para>The <varname>kinds</varname> argument controls what kind of blocks
1665 are shown for a <varname>full</varname> leak search. The set of leak kinds
1666 to show can be specified using a <varname>&lt;set&gt;</varname> similarly
philippe2193a7c2012-12-08 17:54:16 +00001667 to the command line option <option>--show-leak-kinds</option>.
1668 Alternatively, the value <varname>definiteleak</varname>
1669 is equivalent to <varname>kinds definite</varname>, the
1670 value <varname>possibleleak</varname> is equivalent to
1671 <varname>kinds definite,possible</varname> : it will also show
1672 possibly leaked blocks, .i.e those for which only an interior
1673 pointer was found. The value <varname>reachable</varname> will
1674 show all block categories (i.e. is equivalent to <varname>kinds
1675 all</varname>).
sewardj3b290482011-05-06 21:02:55 +00001676 </para>
sewardjc8bd1df2011-06-26 12:41:33 +00001677
philippe278b2a32013-10-09 20:12:39 +00001678 <para>The <varname>heuristics</varname> argument controls the heuristics
1679 used during the leak search. The set of heuristics to use can be specified
1680 using a <varname>&lt;set&gt;</varname> similarly
1681 to the command line option <option>--leak-check-heuristics</option>.
1682 The default value for the <varname>heuristics</varname> argument is
1683 <varname>heuristics none</varname>.
1684 </para>
1685
1686 <para>The <varname>[increased*|changed|any]</varname> argument controls what
1687 kinds of changes are shown for a <varname>full</varname> leak search. The
sewardjc8bd1df2011-06-26 12:41:33 +00001688 value <varname>increased</varname> specifies that only block
1689 allocation stacks with an increased number of leaked bytes or
1690 blocks since the previous leak check should be shown. The
1691 value <varname>changed</varname> specifies that allocation stacks
1692 with any change since the previous leak check should be shown.
1693 The value <varname>any</varname> specifies that all leak entries
1694 should be shown, regardless of any increase or decrease. When
1695 If <varname>increased</varname> or <varname>changed</varname> are
1696 specified, the leak report entries will show the delta relative to
1697 the previous leak report.
1698 </para>
1699
1700 <para>The following example shows usage of the
philippe84234902012-01-14 13:53:13 +00001701 <varname>leak_check</varname> monitor command on
sewardjc8bd1df2011-06-26 12:41:33 +00001702 the <varname>memcheck/tests/leak-cases.c</varname> regression
1703 test. The first command outputs one entry having an increase in
1704 the leaked bytes. The second command is the same as the first
1705 command, but uses the abbreviated forms accepted by GDB and the
1706 Valgrind gdbserver. It only outputs the summary information, as
1707 there was no increase since the previous leak search.</para>
sewardj3b290482011-05-06 21:02:55 +00001708<programlisting><![CDATA[
sewardj30b3eca2011-06-28 08:20:39 +00001709(gdb) monitor leak_check full possibleleak increased
philippea22f59d2012-01-26 23:13:52 +00001710==19520== 16 (+16) bytes in 1 (+1) blocks are possibly lost in loss record 9 of 12
1711==19520== at 0x40070B4: malloc (vg_replace_malloc.c:263)
1712==19520== by 0x80484D5: mk (leak-cases.c:52)
1713==19520== by 0x804855F: f (leak-cases.c:81)
1714==19520== by 0x80488E0: main (leak-cases.c:107)
1715==19520==
1716==19520== LEAK SUMMARY:
1717==19520== definitely lost: 32 (+0) bytes in 2 (+0) blocks
1718==19520== indirectly lost: 16 (+0) bytes in 1 (+0) blocks
1719==19520== possibly lost: 32 (+16) bytes in 2 (+1) blocks
1720==19520== still reachable: 96 (+16) bytes in 6 (+1) blocks
1721==19520== suppressed: 0 (+0) bytes in 0 (+0) blocks
1722==19520== Reachable blocks (those to which a pointer was found) are not shown.
1723==19520== To see them, add 'reachable any' args to leak_check
1724==19520==
sewardj30b3eca2011-06-28 08:20:39 +00001725(gdb) mo l
philippea22f59d2012-01-26 23:13:52 +00001726==19520== LEAK SUMMARY:
1727==19520== definitely lost: 32 (+0) bytes in 2 (+0) blocks
1728==19520== indirectly lost: 16 (+0) bytes in 1 (+0) blocks
1729==19520== possibly lost: 32 (+0) bytes in 2 (+0) blocks
1730==19520== still reachable: 96 (+0) bytes in 6 (+0) blocks
1731==19520== suppressed: 0 (+0) bytes in 0 (+0) blocks
1732==19520== Reachable blocks (those to which a pointer was found) are not shown.
1733==19520== To see them, add 'reachable any' args to leak_check
1734==19520==
sewardj3b290482011-05-06 21:02:55 +00001735(gdb)
1736]]></programlisting>
sewardjc8bd1df2011-06-26 12:41:33 +00001737 <para>Note that when using Valgrind's gdbserver, it is not
1738 necessary to rerun
1739 with <option>--leak-check=full</option>
1740 <option>--show-reachable=yes</option> to see the reachable
1741 blocks. You can obtain the same information without rerunning by
sewardj30b3eca2011-06-28 08:20:39 +00001742 using the GDB command <computeroutput>monitor leak_check full
sewardjc8bd1df2011-06-26 12:41:33 +00001743 reachable any</computeroutput> (or, using
sewardj30b3eca2011-06-28 08:20:39 +00001744 abbreviation: <computeroutput>mo l f r a</computeroutput>).
sewardj3b290482011-05-06 21:02:55 +00001745 </para>
1746 </listitem>
philippe84234902012-01-14 13:53:13 +00001747
philippea22f59d2012-01-26 23:13:52 +00001748 <listitem>
1749 <para><varname>block_list &lt;loss_record_nr&gt; </varname>
1750 shows the list of blocks belonging to &lt;loss_record_nr&gt;.
1751 </para>
1752
1753 <para> A leak search merges the allocated blocks in loss records :
1754 a loss record re-groups all blocks having the same state (for
1755 example, Definitely Lost) and the same allocation backtrace.
1756 Each loss record is identified in the leak search result
1757 by a loss record number.
1758 The <varname>block_list</varname> command shows the loss record information
1759 followed by the addresses and sizes of the blocks which have been
1760 merged in the loss record.
1761 </para>
1762
1763 <para> If a directly lost block causes some other blocks to be indirectly
1764 lost, the block_list command will also show these indirectly lost blocks.
1765 The indirectly lost blocks will be indented according to the level of indirection
1766 between the directly lost block and the indirectly lost block(s).
1767 Each indirectly lost block is followed by the reference of its loss record.
1768 </para>
1769
1770 <para> The block_list command can be used on the results of a leak search as long
1771 as no block has been freed after this leak search: as soon as the program frees
1772 a block, a new leak search is needed before block_list can be used again.
1773 </para>
1774
1775 <para>
1776 In the below example, the program leaks a tree structure by losing the pointer to
1777 the block A (top of the tree).
1778 So, the block A is directly lost, causing an indirect
1779 loss of blocks B to G. The first block_list command shows the loss record of A
1780 (a definitely lost block with address 0x4028028, size 16). The addresses and sizes
1781 of the indirectly lost blocks due to block A are shown below the block A.
1782 The second command shows the details of one of the indirect loss records output
1783 by the first command.
1784 </para>
1785<programlisting><![CDATA[
1786 A
1787 / \
1788 B C
1789 / \ / \
1790 D E F G
1791]]></programlisting>
1792
1793<programlisting><![CDATA[
1794(gdb) bt
1795#0 main () at leak-tree.c:69
1796(gdb) monitor leak_check full any
1797==19552== 112 (16 direct, 96 indirect) bytes in 1 blocks are definitely lost in loss record 7 of 7
1798==19552== at 0x40070B4: malloc (vg_replace_malloc.c:263)
1799==19552== by 0x80484D5: mk (leak-tree.c:28)
1800==19552== by 0x80484FC: f (leak-tree.c:41)
1801==19552== by 0x8048856: main (leak-tree.c:63)
1802==19552==
1803==19552== LEAK SUMMARY:
1804==19552== definitely lost: 16 bytes in 1 blocks
1805==19552== indirectly lost: 96 bytes in 6 blocks
1806==19552== possibly lost: 0 bytes in 0 blocks
1807==19552== still reachable: 0 bytes in 0 blocks
1808==19552== suppressed: 0 bytes in 0 blocks
1809==19552==
1810(gdb) monitor block_list 7
1811==19552== 112 (16 direct, 96 indirect) bytes in 1 blocks are definitely lost in loss record 7 of 7
1812==19552== at 0x40070B4: malloc (vg_replace_malloc.c:263)
1813==19552== by 0x80484D5: mk (leak-tree.c:28)
1814==19552== by 0x80484FC: f (leak-tree.c:41)
1815==19552== by 0x8048856: main (leak-tree.c:63)
1816==19552== 0x4028028[16]
1817==19552== 0x4028068[16] indirect loss record 1
1818==19552== 0x40280E8[16] indirect loss record 3
1819==19552== 0x4028128[16] indirect loss record 4
1820==19552== 0x40280A8[16] indirect loss record 2
1821==19552== 0x4028168[16] indirect loss record 5
1822==19552== 0x40281A8[16] indirect loss record 6
1823(gdb) mo b 2
1824==19552== 16 bytes in 1 blocks are indirectly lost in loss record 2 of 7
1825==19552== at 0x40070B4: malloc (vg_replace_malloc.c:263)
1826==19552== by 0x80484D5: mk (leak-tree.c:28)
1827==19552== by 0x8048519: f (leak-tree.c:43)
1828==19552== by 0x8048856: main (leak-tree.c:63)
1829==19552== 0x40280A8[16]
1830==19552== 0x4028168[16] indirect loss record 5
1831==19552== 0x40281A8[16] indirect loss record 6
1832(gdb)
1833
1834]]></programlisting>
1835
1836 </listitem>
1837
1838 <listitem>
1839 <para><varname>who_points_at &lt;addr&gt; [&lt;len&gt;]</varname>
1840 shows all the locations where a pointer to addr is found.
1841 If len is equal to 1, the command only shows the locations pointing
1842 exactly at addr (i.e. the "start pointers" to addr).
1843 If len is &gt; 1, "interior pointers" pointing at the len first bytes
1844 will also be shown.
1845 </para>
1846
1847 <para>The locations searched for are the same as the locations
1848 used in the leak search. So, <varname>who_points_at</varname> can a.o.
1849 be used to show why the leak search still can reach a block, or can
1850 search for dangling pointers to a freed block.
1851 Each location pointing at addr (or pointing inside addr if interior pointers
1852 are being searched for) will be described.
1853 </para>
1854
1855 <para>In the below example, the pointers to the 'tree block A' (see example
1856 in command <varname>block_list</varname>) is shown before the tree was leaked.
1857 The descriptions are detailed as the option <option>--read-var-info=yes</option>
1858 was given at Valgrind startup. The second call shows the pointers (start and interior
1859 pointers) to block G. The block G (0x40281A8) is reachable via block C (0x40280a8)
1860 and register ECX of tid 1 (tid is the Valgrind thread id).
1861 It is "interior reachable" via the register EBX.
1862 </para>
1863
1864<programlisting><![CDATA[
1865(gdb) monitor who_points_at 0x4028028
1866==20852== Searching for pointers to 0x4028028
1867==20852== *0x8049e20 points at 0x4028028
1868==20852== Location 0x8049e20 is 0 bytes inside global var "t"
1869==20852== declared at leak-tree.c:35
1870(gdb) monitor who_points_at 0x40281A8 16
1871==20852== Searching for pointers pointing in 16 bytes from 0x40281a8
1872==20852== *0x40280ac points at 0x40281a8
1873==20852== Address 0x40280ac is 4 bytes inside a block of size 16 alloc'd
1874==20852== at 0x40070B4: malloc (vg_replace_malloc.c:263)
1875==20852== by 0x80484D5: mk (leak-tree.c:28)
1876==20852== by 0x8048519: f (leak-tree.c:43)
1877==20852== by 0x8048856: main (leak-tree.c:63)
1878==20852== tid 1 register ECX points at 0x40281a8
1879==20852== tid 1 register EBX interior points at 2 bytes inside 0x40281a8
1880(gdb)
1881]]></programlisting>
philippea22f59d2012-01-26 23:13:52 +00001882
philippeab1fce92013-09-29 13:47:32 +00001883 <para> When <varname>who_points_at</varname> finds an interior pointer,
1884 it will report the heuristic(s) with which this interior pointer
1885 will be considered as reachable. Note that this is done independently
1886 of the value of the option <option>--leak-check-heuristics</option>.
1887 In the below example, the loss record 6 indicates a possibly lost
1888 block. <varname>who_points_at</varname> reports that there is an interior
1889 pointer pointing in this block, and that the block can be considered
1890 reachable using the heuristic
1891 <computeroutput>multipleinheritance</computeroutput>.
1892 </para>
1893
1894<programlisting><![CDATA[
1895(gdb) monitor block_list 6
1896==3748== 8 bytes in 1 blocks are possibly lost in loss record 6 of 7
1897==3748== at 0x4007D77: operator new(unsigned int) (vg_replace_malloc.c:313)
1898==3748== by 0x8048954: main (leak_cpp_interior.cpp:43)
1899==3748== 0x402A0E0[8]
1900(gdb) monitor who_points_at 0x402A0E0 8
1901==3748== Searching for pointers pointing in 8 bytes from 0x402a0e0
1902==3748== *0xbe8ee078 interior points at 4 bytes inside 0x402a0e0
1903==3748== Address 0xbe8ee078 is on thread 1's stack
1904==3748== block at 0x402a0e0 considered reachable by ptr 0x402a0e4 using multipleinheritance heuristic
1905(gdb)
1906]]></programlisting>
1907
1908 </listitem>
philippea22f59d2012-01-26 23:13:52 +00001909
sewardj3b290482011-05-06 21:02:55 +00001910</itemizedlist>
1911
1912</sect1>
njn3e986b22004-11-30 10:43:45 +00001913
njn3e986b22004-11-30 10:43:45 +00001914<sect1 id="mc-manual.clientreqs" xreflabel="Client requests">
1915<title>Client Requests</title>
1916
1917<para>The following client requests are defined in
njn1d0825f2006-03-27 11:37:07 +00001918<filename>memcheck.h</filename>.
njn3e986b22004-11-30 10:43:45 +00001919See <filename>memcheck.h</filename> for exact details of their
1920arguments.</para>
1921
1922<itemizedlist>
1923
1924 <listitem>
njndbf7ca72006-03-31 11:57:59 +00001925 <para><varname>VALGRIND_MAKE_MEM_NOACCESS</varname>,
1926 <varname>VALGRIND_MAKE_MEM_UNDEFINED</varname> and
1927 <varname>VALGRIND_MAKE_MEM_DEFINED</varname>.
njn3e986b22004-11-30 10:43:45 +00001928 These mark address ranges as completely inaccessible,
1929 accessible but containing undefined data, and accessible and
sewardje7decf82011-01-22 11:21:58 +00001930 containing defined data, respectively.</para>
njn3e986b22004-11-30 10:43:45 +00001931 </listitem>
1932
1933 <listitem>
njndbf7ca72006-03-31 11:57:59 +00001934 <para><varname>VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE</varname>.
1935 This is just like <varname>VALGRIND_MAKE_MEM_DEFINED</varname> but only
1936 affects those bytes that are already addressable.</para>
1937 </listitem>
1938
1939 <listitem>
njndbf7ca72006-03-31 11:57:59 +00001940 <para><varname>VALGRIND_CHECK_MEM_IS_ADDRESSABLE</varname> and
1941 <varname>VALGRIND_CHECK_MEM_IS_DEFINED</varname>: check immediately
de03e0e7c2005-12-03 23:02:33 +00001942 whether or not the given address range has the relevant property,
1943 and if not, print an error message. Also, for the convenience of
1944 the client, returns zero if the relevant property holds; otherwise,
1945 the returned value is the address of the first byte for which the
1946 property is not true. Always returns 0 when not run on
1947 Valgrind.</para>
njn3e986b22004-11-30 10:43:45 +00001948 </listitem>
1949
1950 <listitem>
njndbf7ca72006-03-31 11:57:59 +00001951 <para><varname>VALGRIND_CHECK_VALUE_IS_DEFINED</varname>: a quick and easy
1952 way to find out whether Valgrind thinks a particular value
1953 (lvalue, to be precise) is addressable and defined. Prints an error
njn8225cc02009-03-09 22:52:24 +00001954 message if not. It has no return value.</para>
njn3e986b22004-11-30 10:43:45 +00001955 </listitem>
1956
1957 <listitem>
njn8225cc02009-03-09 22:52:24 +00001958 <para><varname>VALGRIND_DO_LEAK_CHECK</varname>: does a full memory leak
njn2f7eebe2009-08-05 06:34:27 +00001959 check (like <option>--leak-check=full</option>) right now.
njn8225cc02009-03-09 22:52:24 +00001960 This is useful for incrementally checking for leaks between arbitrary
1961 places in the program's execution. It has no return value.</para>
1962 </listitem>
1963
1964 <listitem>
sewardjc8bd1df2011-06-26 12:41:33 +00001965 <para><varname>VALGRIND_DO_ADDED_LEAK_CHECK</varname>: same as
1966 <varname> VALGRIND_DO_LEAK_CHECK</varname> but only shows the
1967 entries for which there was an increase in leaked bytes or leaked
1968 number of blocks since the previous leak search. It has no return
1969 value.</para>
1970 </listitem>
1971
1972 <listitem>
1973 <para><varname>VALGRIND_DO_CHANGED_LEAK_CHECK</varname>: same as
1974 <varname>VALGRIND_DO_LEAK_CHECK</varname> but only shows the
1975 entries for which there was an increase or decrease in leaked
1976 bytes or leaked number of blocks since the previous leak search. It
1977 has no return value.</para>
1978 </listitem>
1979
1980 <listitem>
njn8225cc02009-03-09 22:52:24 +00001981 <para><varname>VALGRIND_DO_QUICK_LEAK_CHECK</varname>: like
1982 <varname>VALGRIND_DO_LEAK_CHECK</varname>, except it produces only a leak
njn7e5d4ed2009-07-30 02:57:52 +00001983 summary (like <option>--leak-check=summary</option>).
njn8225cc02009-03-09 22:52:24 +00001984 It has no return value.</para>
njn3e986b22004-11-30 10:43:45 +00001985 </listitem>
1986
1987 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001988 <para><varname>VALGRIND_COUNT_LEAKS</varname>: fills in the four
1989 arguments with the number of bytes of memory found by the previous
njn8225cc02009-03-09 22:52:24 +00001990 leak check to be leaked (i.e. the sum of direct leaks and indirect leaks),
njn2f7eebe2009-08-05 06:34:27 +00001991 dubious, reachable and suppressed. This is useful in test harness code,
njn8225cc02009-03-09 22:52:24 +00001992 after calling <varname>VALGRIND_DO_LEAK_CHECK</varname> or
1993 <varname>VALGRIND_DO_QUICK_LEAK_CHECK</varname>.</para>
njn3e986b22004-11-30 10:43:45 +00001994 </listitem>
1995
1996 <listitem>
njn8df80b22009-03-02 05:11:06 +00001997 <para><varname>VALGRIND_COUNT_LEAK_BLOCKS</varname>: identical to
1998 <varname>VALGRIND_COUNT_LEAKS</varname> except that it returns the
1999 number of blocks rather than the number of bytes in each
2000 category.</para>
2001 </listitem>
2002
2003 <listitem>
de03e0e7c2005-12-03 23:02:33 +00002004 <para><varname>VALGRIND_GET_VBITS</varname> and
2005 <varname>VALGRIND_SET_VBITS</varname>: allow you to get and set the
2006 V (validity) bits for an address range. You should probably only
2007 set V bits that you have got with
2008 <varname>VALGRIND_GET_VBITS</varname>. Only for those who really
njn1d0825f2006-03-27 11:37:07 +00002009 know what they are doing.</para>
njn3e986b22004-11-30 10:43:45 +00002010 </listitem>
2011
sewardje7decf82011-01-22 11:21:58 +00002012 <listitem>
2013 <para><varname>VALGRIND_CREATE_BLOCK</varname> and
2014 <varname>VALGRIND_DISCARD</varname>. <varname>VALGRIND_CREATE_BLOCK</varname>
2015 takes an address, a number of bytes and a character string. The
2016 specified address range is then associated with that string. When
2017 Memcheck reports an invalid access to an address in the range, it
2018 will describe it in terms of this block rather than in terms of
2019 any other block it knows about. Note that the use of this macro
2020 does not actually change the state of memory in any way -- it
2021 merely gives a name for the range.
2022 </para>
2023
2024 <para>At some point you may want Memcheck to stop reporting errors
2025 in terms of the block named
2026 by <varname>VALGRIND_CREATE_BLOCK</varname>. To make this
2027 possible, <varname>VALGRIND_CREATE_BLOCK</varname> returns a
2028 "block handle", which is a C <varname>int</varname> value. You
2029 can pass this block handle to <varname>VALGRIND_DISCARD</varname>.
2030 After doing so, Valgrind will no longer relate addressing errors
2031 in the specified range to the block. Passing invalid handles to
2032 <varname>VALGRIND_DISCARD</varname> is harmless.
2033 </para>
2034 </listitem>
2035
njn3e986b22004-11-30 10:43:45 +00002036</itemizedlist>
2037
2038</sect1>
sewardjce10c262006-10-05 17:56:14 +00002039
2040
2041
2042
njn09f2e6c2009-08-10 04:07:54 +00002043<sect1 id="mc-manual.mempools" xreflabel="Memory Pools">
sewardjce10c262006-10-05 17:56:14 +00002044<title>Memory Pools: describing and working with custom allocators</title>
2045
2046<para>Some programs use custom memory allocators, often for performance
njna3311642009-08-10 01:29:14 +00002047reasons. Left to itself, Memcheck is unable to understand the
2048behaviour of custom allocation schemes as well as it understands the
2049standard allocators, and so may miss errors and leaks in your program. What
2050this section describes is a way to give Memcheck enough of a description of
2051your custom allocator that it can make at least some sense of what is
2052happening.</para>
sewardjae0e07b2006-10-06 11:47:01 +00002053
2054<para>There are many different sorts of custom allocator, so Memcheck
sewardjce10c262006-10-05 17:56:14 +00002055attempts to reason about them using a loose, abstract model. We
2056use the following terminology when describing custom allocation
2057systems:</para>
2058
2059<itemizedlist>
2060 <listitem>
2061 <para>Custom allocation involves a set of independent "memory pools".
2062 </para>
2063 </listitem>
2064 <listitem>
2065 <para>Memcheck's notion of a a memory pool consists of a single "anchor
2066 address" and a set of non-overlapping "chunks" associated with the
2067 anchor address.</para>
2068 </listitem>
2069 <listitem>
2070 <para>Typically a pool's anchor address is the address of a
2071 book-keeping "header" structure.</para>
2072 </listitem>
2073 <listitem>
2074 <para>Typically the pool's chunks are drawn from a contiguous
bartaf25f672009-06-26 19:03:53 +00002075 "superblock" acquired through the system
njn2f7eebe2009-08-05 06:34:27 +00002076 <function>malloc</function> or
2077 <function>mmap</function>.</para>
sewardjce10c262006-10-05 17:56:14 +00002078 </listitem>
2079
2080</itemizedlist>
2081
2082<para>Keep in mind that the last two points above say "typically": the
2083Valgrind mempool client request API is intentionally vague about the
2084exact structure of a mempool. There is no specific mention made of
2085headers or superblocks. Nevertheless, the following picture may help
2086elucidate the intention of the terms in the API:</para>
2087
2088<programlisting><![CDATA[
2089 "pool"
2090 (anchor address)
2091 |
2092 v
2093 +--------+---+
2094 | header | o |
2095 +--------+-|-+
2096 |
2097 v superblock
2098 +------+---+--------------+---+------------------+
2099 | |rzB| allocation |rzB| |
2100 +------+---+--------------+---+------------------+
2101 ^ ^
2102 | |
2103 "addr" "addr"+"size"
2104]]></programlisting>
2105
2106<para>
2107Note that the header and the superblock may be contiguous or
2108discontiguous, and there may be multiple superblocks associated with a
2109single header; such variations are opaque to Memcheck. The API
2110only requires that your allocation scheme can present sensible values
2111of "pool", "addr" and "size".</para>
2112
2113<para>
2114Typically, before making client requests related to mempools, a client
2115program will have allocated such a header and superblock for their
2116mempool, and marked the superblock NOACCESS using the
2117<varname>VALGRIND_MAKE_MEM_NOACCESS</varname> client request.</para>
2118
2119<para>
2120When dealing with mempools, the goal is to maintain a particular
2121invariant condition: that Memcheck believes the unallocated portions
2122of the pool's superblock (including redzones) are NOACCESS. To
2123maintain this invariant, the client program must ensure that the
2124superblock starts out in that state; Memcheck cannot make it so, since
2125Memcheck never explicitly learns about the superblock of a pool, only
2126the allocated chunks within the pool.</para>
2127
2128<para>
2129Once the header and superblock for a pool are established and properly
2130marked, there are a number of client requests programs can use to
2131inform Memcheck about changes to the state of a mempool:</para>
2132
2133<itemizedlist>
2134
2135 <listitem>
2136 <para>
2137 <varname>VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed)</varname>:
njna3311642009-08-10 01:29:14 +00002138 This request registers the address <varname>pool</varname> as the anchor
2139 address for a memory pool. It also provides a size
2140 <varname>rzB</varname>, specifying how large the redzones placed around
2141 chunks allocated from the pool should be. Finally, it provides an
2142 <varname>is_zeroed</varname> argument that specifies whether the pool's
2143 chunks are zeroed (more precisely: defined) when allocated.
sewardjce10c262006-10-05 17:56:14 +00002144 </para>
2145 <para>
2146 Upon completion of this request, no chunks are associated with the
2147 pool. The request simply tells Memcheck that the pool exists, so that
2148 subsequent calls can refer to it as a pool.
2149 </para>
2150 </listitem>
2151
2152 <listitem>
2153 <para><varname>VALGRIND_DESTROY_MEMPOOL(pool)</varname>:
2154 This request tells Memcheck that a pool is being torn down. Memcheck
2155 then removes all records of chunks associated with the pool, as well
2156 as its record of the pool's existence. While destroying its records of
2157 a mempool, Memcheck resets the redzones of any live chunks in the pool
2158 to NOACCESS.
2159 </para>
2160 </listitem>
2161
2162 <listitem>
2163 <para><varname>VALGRIND_MEMPOOL_ALLOC(pool, addr, size)</varname>:
njna3311642009-08-10 01:29:14 +00002164 This request informs Memcheck that a <varname>size</varname>-byte chunk
2165 has been allocated at <varname>addr</varname>, and associates the chunk with the
2166 specified
2167 <varname>pool</varname>. If the pool was created with nonzero
2168 <varname>rzB</varname> redzones, Memcheck will mark the
2169 <varname>rzB</varname> bytes before and after the chunk as NOACCESS. If
2170 the pool was created with the <varname>is_zeroed</varname> argument set,
2171 Memcheck will mark the chunk as DEFINED, otherwise Memcheck will mark
2172 the chunk as UNDEFINED.
sewardjce10c262006-10-05 17:56:14 +00002173 </para>
2174 </listitem>
2175
2176 <listitem>
2177 <para><varname>VALGRIND_MEMPOOL_FREE(pool, addr)</varname>:
njna3311642009-08-10 01:29:14 +00002178 This request informs Memcheck that the chunk at <varname>addr</varname>
2179 should no longer be considered allocated. Memcheck will mark the chunk
2180 associated with <varname>addr</varname> as NOACCESS, and delete its
2181 record of the chunk's existence.
sewardjce10c262006-10-05 17:56:14 +00002182 </para>
2183 </listitem>
2184
2185 <listitem>
2186 <para><varname>VALGRIND_MEMPOOL_TRIM(pool, addr, size)</varname>:
njna3311642009-08-10 01:29:14 +00002187 This request trims the chunks associated with <varname>pool</varname>.
2188 The request only operates on chunks associated with
2189 <varname>pool</varname>. Trimming is formally defined as:</para>
sewardjce10c262006-10-05 17:56:14 +00002190 <itemizedlist>
2191 <listitem>
njna3311642009-08-10 01:29:14 +00002192 <para> All chunks entirely inside the range
2193 <varname>addr..(addr+size-1)</varname> are preserved.</para>
sewardjce10c262006-10-05 17:56:14 +00002194 </listitem>
2195 <listitem>
njna3311642009-08-10 01:29:14 +00002196 <para>All chunks entirely outside the range
2197 <varname>addr..(addr+size-1)</varname> are discarded, as though
2198 <varname>VALGRIND_MEMPOOL_FREE</varname> was called on them. </para>
sewardjce10c262006-10-05 17:56:14 +00002199 </listitem>
2200 <listitem>
2201 <para>All other chunks must intersect with the range
njna3311642009-08-10 01:29:14 +00002202 <varname>addr..(addr+size-1)</varname>; areas outside the
2203 intersection are marked as NOACCESS, as though they had been
2204 independently freed with
sewardjce10c262006-10-05 17:56:14 +00002205 <varname>VALGRIND_MEMPOOL_FREE</varname>.</para>
2206 </listitem>
2207 </itemizedlist>
2208 <para>This is a somewhat rare request, but can be useful in
2209 implementing the type of mass-free operations common in custom
2210 LIFO allocators.</para>
2211 </listitem>
2212
2213 <listitem>
bartaf25f672009-06-26 19:03:53 +00002214 <para><varname>VALGRIND_MOVE_MEMPOOL(poolA, poolB)</varname>: This
2215 request informs Memcheck that the pool previously anchored at
njna3311642009-08-10 01:29:14 +00002216 address <varname>poolA</varname> has moved to anchor address
2217 <varname>poolB</varname>. This is a rare request, typically only needed
2218 if you <function>realloc</function> the header of a mempool.</para>
sewardjce10c262006-10-05 17:56:14 +00002219 <para>No memory-status bits are altered by this request.</para>
2220 </listitem>
2221
2222 <listitem>
2223 <para>
bartaf25f672009-06-26 19:03:53 +00002224 <varname>VALGRIND_MEMPOOL_CHANGE(pool, addrA, addrB,
2225 size)</varname>: This request informs Memcheck that the chunk
njna3311642009-08-10 01:29:14 +00002226 previously allocated at address <varname>addrA</varname> within
2227 <varname>pool</varname> has been moved and/or resized, and should be
2228 changed to cover the region <varname>addrB..(addrB+size-1)</varname>. This
2229 is a rare request, typically only needed if you
2230 <function>realloc</function> a superblock or wish to extend a chunk
2231 without changing its memory-status bits.
sewardjce10c262006-10-05 17:56:14 +00002232 </para>
2233 <para>No memory-status bits are altered by this request.
2234 </para>
2235 </listitem>
2236
2237 <listitem>
2238 <para><varname>VALGRIND_MEMPOOL_EXISTS(pool)</varname>:
2239 This request informs the caller whether or not Memcheck is currently
njna3311642009-08-10 01:29:14 +00002240 tracking a mempool at anchor address <varname>pool</varname>. It
2241 evaluates to 1 when there is a mempool associated with that address, 0
2242 otherwise. This is a rare request, only useful in circumstances when
2243 client code might have lost track of the set of active mempools.
sewardjce10c262006-10-05 17:56:14 +00002244 </para>
2245 </listitem>
2246
2247</itemizedlist>
2248
sewardj778d7832007-11-22 01:21:56 +00002249</sect1>
2250
2251
2252
2253
2254
2255
2256
2257<sect1 id="mc-manual.mpiwrap" xreflabel="MPI Wrappers">
2258<title>Debugging MPI Parallel Programs with Valgrind</title>
2259
njn2f7eebe2009-08-05 06:34:27 +00002260<para>Memcheck supports debugging of distributed-memory applications
sewardj778d7832007-11-22 01:21:56 +00002261which use the MPI message passing standard. This support consists of a
2262library of wrapper functions for the
2263<computeroutput>PMPI_*</computeroutput> interface. When incorporated
2264into the application's address space, either by direct linking or by
2265<computeroutput>LD_PRELOAD</computeroutput>, the wrappers intercept
2266calls to <computeroutput>PMPI_Send</computeroutput>,
2267<computeroutput>PMPI_Recv</computeroutput>, etc. They then
njn2f7eebe2009-08-05 06:34:27 +00002268use client requests to inform Memcheck of memory state changes caused
sewardj778d7832007-11-22 01:21:56 +00002269by the function being wrapped. This reduces the number of false
2270positives that Memcheck otherwise typically reports for MPI
2271applications.</para>
2272
2273<para>The wrappers also take the opportunity to carefully check
2274size and definedness of buffers passed as arguments to MPI functions, hence
2275detecting errors such as passing undefined data to
2276<computeroutput>PMPI_Send</computeroutput>, or receiving data into a
2277buffer which is too small.</para>
2278
2279<para>Unlike most of the rest of Valgrind, the wrapper library is subject to a
2280BSD-style license, so you can link it into any code base you like.
njna437a602009-08-04 05:24:46 +00002281See the top of <computeroutput>mpi/libmpiwrap.c</computeroutput>
sewardj778d7832007-11-22 01:21:56 +00002282for license details.</para>
2283
2284
2285<sect2 id="mc-manual.mpiwrap.build" xreflabel="Building MPI Wrappers">
2286<title>Building and installing the wrappers</title>
2287
2288<para> The wrapper library will be built automatically if possible.
2289Valgrind's configure script will look for a suitable
2290<computeroutput>mpicc</computeroutput> to build it with. This must be
2291the same <computeroutput>mpicc</computeroutput> you use to build the
2292MPI application you want to debug. By default, Valgrind tries
2293<computeroutput>mpicc</computeroutput>, but you can specify a
njna3311642009-08-10 01:29:14 +00002294different one by using the configure-time option
njn7316df22009-08-04 01:16:01 +00002295<option>--with-mpicc</option>. Currently the
sewardj778d7832007-11-22 01:21:56 +00002296wrappers are only buildable with
2297<computeroutput>mpicc</computeroutput>s which are based on GNU
njn7316df22009-08-04 01:16:01 +00002298GCC or Intel's C++ Compiler.</para>
sewardj778d7832007-11-22 01:21:56 +00002299
2300<para>Check that the configure script prints a line like this:</para>
2301
2302<programlisting><![CDATA[
2303checking for usable MPI2-compliant mpicc and mpi.h... yes, mpicc
2304]]></programlisting>
2305
2306<para>If it says <computeroutput>... no</computeroutput>, your
2307<computeroutput>mpicc</computeroutput> has failed to compile and link
2308a test MPI2 program.</para>
2309
2310<para>If the configure test succeeds, continue in the usual way with
2311<computeroutput>make</computeroutput> and <computeroutput>make
2312install</computeroutput>. The final install tree should then contain
njn2f7eebe2009-08-05 06:34:27 +00002313<computeroutput>libmpiwrap-&lt;platform&gt;.so</computeroutput>.
sewardj778d7832007-11-22 01:21:56 +00002314</para>
2315
2316<para>Compile up a test MPI program (eg, MPI hello-world) and try
2317this:</para>
2318
2319<programlisting><![CDATA[
njn6bf365c2009-02-11 00:35:45 +00002320LD_PRELOAD=$prefix/lib/valgrind/libmpiwrap-<platform>.so \
sewardj778d7832007-11-22 01:21:56 +00002321 mpirun [args] $prefix/bin/valgrind ./hello
2322]]></programlisting>
2323
2324<para>You should see something similar to the following</para>
2325
2326<programlisting><![CDATA[
2327valgrind MPI wrappers 31901: Active for pid 31901
2328valgrind MPI wrappers 31901: Try MPIWRAP_DEBUG=help for possible options
2329]]></programlisting>
2330
2331<para>repeated for every process in the group. If you do not see
2332these, there is an build/installation problem of some kind.</para>
2333
2334<para> The MPI functions to be wrapped are assumed to be in an ELF
2335shared object with soname matching
2336<computeroutput>libmpi.so*</computeroutput>. This is known to be
2337correct at least for Open MPI and Quadrics MPI, and can easily be
2338changed if required.</para>
2339</sect2>
2340
2341
2342<sect2 id="mc-manual.mpiwrap.gettingstarted"
2343 xreflabel="Getting started with MPI Wrappers">
2344<title>Getting started</title>
2345
2346<para>Compile your MPI application as usual, taking care to link it
2347using the same <computeroutput>mpicc</computeroutput> that your
2348Valgrind build was configured with.</para>
2349
2350<para>
2351Use the following basic scheme to run your application on Valgrind with
2352the wrappers engaged:</para>
2353
2354<programlisting><![CDATA[
2355MPIWRAP_DEBUG=[wrapper-args] \
njn6bf365c2009-02-11 00:35:45 +00002356 LD_PRELOAD=$prefix/lib/valgrind/libmpiwrap-<platform>.so \
sewardj778d7832007-11-22 01:21:56 +00002357 mpirun [mpirun-args] \
2358 $prefix/bin/valgrind [valgrind-args] \
2359 [application] [app-args]
2360]]></programlisting>
2361
2362<para>As an alternative to
2363<computeroutput>LD_PRELOAD</computeroutput>ing
njn6bf365c2009-02-11 00:35:45 +00002364<computeroutput>libmpiwrap-&lt;platform&gt;.so</computeroutput>, you can
2365simply link it to your application if desired. This should not disturb
2366native behaviour of your application in any way.</para>
sewardj778d7832007-11-22 01:21:56 +00002367</sect2>
2368
2369
2370<sect2 id="mc-manual.mpiwrap.controlling"
2371 xreflabel="Controlling the MPI Wrappers">
2372<title>Controlling the wrapper library</title>
2373
2374<para>Environment variable
2375<computeroutput>MPIWRAP_DEBUG</computeroutput> is consulted at
2376startup. The default behaviour is to print a starting banner</para>
2377
2378<programlisting><![CDATA[
2379valgrind MPI wrappers 16386: Active for pid 16386
2380valgrind MPI wrappers 16386: Try MPIWRAP_DEBUG=help for possible options
2381]]></programlisting>
2382
2383<para> and then be relatively quiet.</para>
2384
2385<para>You can give a list of comma-separated options in
2386<computeroutput>MPIWRAP_DEBUG</computeroutput>. These are</para>
2387
2388<itemizedlist>
2389 <listitem>
2390 <para><computeroutput>verbose</computeroutput>:
2391 show entries/exits of all wrappers. Also show extra
2392 debugging info, such as the status of outstanding
2393 <computeroutput>MPI_Request</computeroutput>s resulting
2394 from uncompleted <computeroutput>MPI_Irecv</computeroutput>s.</para>
2395 </listitem>
2396 <listitem>
2397 <para><computeroutput>quiet</computeroutput>:
2398 opposite of <computeroutput>verbose</computeroutput>, only print
2399 anything when the wrappers want
2400 to report a detected programming error, or in case of catastrophic
2401 failure of the wrappers.</para>
2402 </listitem>
2403 <listitem>
2404 <para><computeroutput>warn</computeroutput>:
2405 by default, functions which lack proper wrappers
2406 are not commented on, just silently
2407 ignored. This causes a warning to be printed for each unwrapped
2408 function used, up to a maximum of three warnings per function.</para>
2409 </listitem>
2410 <listitem>
2411 <para><computeroutput>strict</computeroutput>:
2412 print an error message and abort the program if
2413 a function lacking a wrapper is used.</para>
2414 </listitem>
2415</itemizedlist>
2416
2417<para> If you want to use Valgrind's XML output facility
njn7e5d4ed2009-07-30 02:57:52 +00002418(<option>--xml=yes</option>), you should pass
sewardj778d7832007-11-22 01:21:56 +00002419<computeroutput>quiet</computeroutput> in
2420<computeroutput>MPIWRAP_DEBUG</computeroutput> so as to get rid of any
2421extraneous printing from the wrappers.</para>
2422
2423</sect2>
2424
2425
njn2f7eebe2009-08-05 06:34:27 +00002426<sect2 id="mc-manual.mpiwrap.limitations.functions"
2427 xreflabel="Functions: Abilities and Limitations">
sewardj778d7832007-11-22 01:21:56 +00002428<title>Functions</title>
2429
2430<para>All MPI2 functions except
2431<computeroutput>MPI_Wtick</computeroutput>,
2432<computeroutput>MPI_Wtime</computeroutput> and
2433<computeroutput>MPI_Pcontrol</computeroutput> have wrappers. The
2434first two are not wrapped because they return a
njn2f7eebe2009-08-05 06:34:27 +00002435<computeroutput>double</computeroutput>, which Valgrind's
2436function-wrap mechanism cannot handle (but it could easily be
2437extended to do so). <computeroutput>MPI_Pcontrol</computeroutput> cannot be
sewardj778d7832007-11-22 01:21:56 +00002438wrapped as it has variable arity:
2439<computeroutput>int MPI_Pcontrol(const int level, ...)</computeroutput></para>
2440
2441<para>Most functions are wrapped with a default wrapper which does
2442nothing except complain or abort if it is called, depending on
2443settings in <computeroutput>MPIWRAP_DEBUG</computeroutput> listed
2444above. The following functions have "real", do-something-useful
2445wrappers:</para>
2446
2447<programlisting><![CDATA[
2448PMPI_Send PMPI_Bsend PMPI_Ssend PMPI_Rsend
2449
2450PMPI_Recv PMPI_Get_count
2451
2452PMPI_Isend PMPI_Ibsend PMPI_Issend PMPI_Irsend
2453
2454PMPI_Irecv
2455PMPI_Wait PMPI_Waitall
2456PMPI_Test PMPI_Testall
2457
2458PMPI_Iprobe PMPI_Probe
2459
2460PMPI_Cancel
2461
2462PMPI_Sendrecv
2463
2464PMPI_Type_commit PMPI_Type_free
2465
2466PMPI_Pack PMPI_Unpack
2467
2468PMPI_Bcast PMPI_Gather PMPI_Scatter PMPI_Alltoall
2469PMPI_Reduce PMPI_Allreduce PMPI_Op_create
2470
2471PMPI_Comm_create PMPI_Comm_dup PMPI_Comm_free PMPI_Comm_rank PMPI_Comm_size
2472
2473PMPI_Error_string
2474PMPI_Init PMPI_Initialized PMPI_Finalize
2475]]></programlisting>
2476
2477<para> A few functions such as
2478<computeroutput>PMPI_Address</computeroutput> are listed as
2479<computeroutput>HAS_NO_WRAPPER</computeroutput>. They have no wrapper
2480at all as there is nothing worth checking, and giving a no-op wrapper
2481would reduce performance for no reason.</para>
2482
2483<para> Note that the wrapper library itself can itself generate large
2484numbers of calls to the MPI implementation, especially when walking
2485complex types. The most common functions called are
2486<computeroutput>PMPI_Extent</computeroutput>,
2487<computeroutput>PMPI_Type_get_envelope</computeroutput>,
2488<computeroutput>PMPI_Type_get_contents</computeroutput>, and
2489<computeroutput>PMPI_Type_free</computeroutput>. </para>
njn2f7eebe2009-08-05 06:34:27 +00002490</sect2>
sewardj778d7832007-11-22 01:21:56 +00002491
njn2f7eebe2009-08-05 06:34:27 +00002492<sect2 id="mc-manual.mpiwrap.limitations.types"
2493 xreflabel="Types: Abilities and Limitations">
sewardj778d7832007-11-22 01:21:56 +00002494<title>Types</title>
2495
2496<para> MPI-1.1 structured types are supported, and walked exactly.
2497The currently supported combiners are
2498<computeroutput>MPI_COMBINER_NAMED</computeroutput>,
2499<computeroutput>MPI_COMBINER_CONTIGUOUS</computeroutput>,
2500<computeroutput>MPI_COMBINER_VECTOR</computeroutput>,
2501<computeroutput>MPI_COMBINER_HVECTOR</computeroutput>
2502<computeroutput>MPI_COMBINER_INDEXED</computeroutput>,
2503<computeroutput>MPI_COMBINER_HINDEXED</computeroutput> and
2504<computeroutput>MPI_COMBINER_STRUCT</computeroutput>. This should
2505cover all MPI-1.1 types. The mechanism (function
2506<computeroutput>walk_type</computeroutput>) should extend easily to
2507cover MPI2 combiners.</para>
2508
2509<para>MPI defines some named structured types
2510(<computeroutput>MPI_FLOAT_INT</computeroutput>,
2511<computeroutput>MPI_DOUBLE_INT</computeroutput>,
2512<computeroutput>MPI_LONG_INT</computeroutput>,
2513<computeroutput>MPI_2INT</computeroutput>,
2514<computeroutput>MPI_SHORT_INT</computeroutput>,
2515<computeroutput>MPI_LONG_DOUBLE_INT</computeroutput>) which are pairs
2516of some basic type and a C <computeroutput>int</computeroutput>.
2517Unfortunately the MPI specification makes it impossible to look inside
2518these types and see where the fields are. Therefore these wrappers
2519assume the types are laid out as <computeroutput>struct { float val;
2520int loc; }</computeroutput> (for
2521<computeroutput>MPI_FLOAT_INT</computeroutput>), etc, and act
2522accordingly. This appears to be correct at least for Open MPI 1.0.2
2523and for Quadrics MPI.</para>
2524
2525<para>If <computeroutput>strict</computeroutput> is an option specified
2526in <computeroutput>MPIWRAP_DEBUG</computeroutput>, the application
2527will abort if an unhandled type is encountered. Otherwise, the
2528application will print a warning message and continue.</para>
2529
2530<para>Some effort is made to mark/check memory ranges corresponding to
2531arrays of values in a single pass. This is important for performance
2532since asking Valgrind to mark/check any range, no matter how small,
2533carries quite a large constant cost. This optimisation is applied to
2534arrays of primitive types (<computeroutput>double</computeroutput>,
2535<computeroutput>float</computeroutput>,
2536<computeroutput>int</computeroutput>,
2537<computeroutput>long</computeroutput>, <computeroutput>long
2538long</computeroutput>, <computeroutput>short</computeroutput>,
2539<computeroutput>char</computeroutput>, and <computeroutput>long
2540double</computeroutput> on platforms where <computeroutput>sizeof(long
2541double) == 8</computeroutput>). For arrays of all other types, the
2542wrappers handle each element individually and so there can be a very
2543large performance cost.</para>
2544
sewardj778d7832007-11-22 01:21:56 +00002545</sect2>
2546
2547
2548<sect2 id="mc-manual.mpiwrap.writingwrappers"
2549 xreflabel="Writing new MPI Wrappers">
2550<title>Writing new wrappers</title>
2551
2552<para>
2553For the most part the wrappers are straightforward. The only
2554significant complexity arises with nonblocking receives.</para>
2555
2556<para>The issue is that <computeroutput>MPI_Irecv</computeroutput>
2557states the recv buffer and returns immediately, giving a handle
2558(<computeroutput>MPI_Request</computeroutput>) for the transaction.
2559Later the user will have to poll for completion with
2560<computeroutput>MPI_Wait</computeroutput> etc, and when the
2561transaction completes successfully, the wrappers have to paint the
2562recv buffer. But the recv buffer details are not presented to
2563<computeroutput>MPI_Wait</computeroutput> -- only the handle is. The
2564library therefore maintains a shadow table which associates
2565uncompleted <computeroutput>MPI_Request</computeroutput>s with the
2566corresponding buffer address/count/type. When an operation completes,
2567the table is searched for the associated address/count/type info, and
2568memory is marked accordingly.</para>
2569
2570<para>Access to the table is guarded by a (POSIX pthreads) lock, so as
2571to make the library thread-safe.</para>
2572
2573<para>The table is allocated with
2574<computeroutput>malloc</computeroutput> and never
2575<computeroutput>free</computeroutput>d, so it will show up in leak
2576checks.</para>
2577
2578<para>Writing new wrappers should be fairly easy. The source file is
njna437a602009-08-04 05:24:46 +00002579<computeroutput>mpi/libmpiwrap.c</computeroutput>. If possible,
sewardj778d7832007-11-22 01:21:56 +00002580find an existing wrapper for a function of similar behaviour to the
2581one you want to wrap, and use it as a starting point. The wrappers
2582are organised in sections in the same order as the MPI 1.1 spec, to
2583aid navigation. When adding a wrapper, remember to comment out the
2584definition of the default wrapper in the long list of defaults at the
2585bottom of the file (do not remove it, just comment it out).</para>
2586</sect2>
2587
2588<sect2 id="mc-manual.mpiwrap.whattoexpect"
2589 xreflabel="What to expect with MPI Wrappers">
2590<title>What to expect when using the wrappers</title>
2591
2592<para>The wrappers should reduce Memcheck's false-error rate on MPI
2593applications. Because the wrapping is done at the MPI interface,
2594there will still potentially be a large number of errors reported in
2595the MPI implementation below the interface. The best you can do is
2596try to suppress them.</para>
2597
2598<para>You may also find that the input-side (buffer
2599length/definedness) checks find errors in your MPI use, for example
2600passing too short a buffer to
2601<computeroutput>MPI_Recv</computeroutput>.</para>
2602
2603<para>Functions which are not wrapped may increase the false
2604error rate. A possible approach is to run with
2605<computeroutput>MPI_DEBUG</computeroutput> containing
2606<computeroutput>warn</computeroutput>. This will show you functions
2607which lack proper wrappers but which are nevertheless used. You can
2608then write wrappers for them.
2609</para>
2610
2611<para>A known source of potential false errors are the
2612<computeroutput>PMPI_Reduce</computeroutput> family of functions, when
2613using a custom (user-defined) reduction function. In a reduction
2614operation, each node notionally sends data to a "central point" which
2615uses the specified reduction function to merge the data items into a
2616single item. Hence, in general, data is passed between nodes and fed
2617to the reduction function, but the wrapper library cannot mark the
2618transferred data as initialised before it is handed to the reduction
2619function, because all that happens "inside" the
2620<computeroutput>PMPI_Reduce</computeroutput> call. As a result you
2621may see false positives reported in your reduction function.</para>
2622
2623</sect2>
sewardjce10c262006-10-05 17:56:14 +00002624
2625</sect1>
sewardj778d7832007-11-22 01:21:56 +00002626
2627
2628
2629
2630
njn3e986b22004-11-30 10:43:45 +00002631</chapter>