blob: 69cf90d765da7ababf227885c13ffc990c816777 [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>
florian7b6899d2014-07-13 14:41:55 +000048 <para>Passing a fishy (presumably negative) value to the
49 <computeroutput>size</computeroutput> parameter of a memory
50 allocation function.</para>
51 </listitem>
52
53 <listitem>
njn05a89172009-07-29 02:36:21 +000054 <para>Memory leaks.</para>
njn3e986b22004-11-30 10:43:45 +000055 </listitem>
njn3e986b22004-11-30 10:43:45 +000056</itemizedlist>
57
njn05a89172009-07-29 02:36:21 +000058<para>Problems like these can be difficult to find by other means,
59often remaining undetected for long periods, then causing occasional,
60difficult-to-diagnose crashes.</para>
61
njn3e986b22004-11-30 10:43:45 +000062</sect1>
63
64
65
njn3e986b22004-11-30 10:43:45 +000066<sect1 id="mc-manual.errormsgs"
67 xreflabel="Explanation of error messages from Memcheck">
68<title>Explanation of error messages from Memcheck</title>
69
njnc1abdcb2009-08-05 05:11:02 +000070<para>Memcheck issues a range of error messages. This section presents a
71quick summary of what error messages mean. The precise behaviour of the
72error-checking machinery is described in <xref
73linkend="mc-manual.machine"/>.</para>
njn3e986b22004-11-30 10:43:45 +000074
75
76<sect2 id="mc-manual.badrw"
77 xreflabel="Illegal read / Illegal write errors">
78<title>Illegal read / Illegal write errors</title>
79
80<para>For example:</para>
81<programlisting><![CDATA[
82Invalid read of size 4
83 at 0x40F6BBCC: (within /usr/lib/libpng.so.2.1.0.9)
84 by 0x40F6B804: (within /usr/lib/libpng.so.2.1.0.9)
sewardj08e31e22007-05-23 21:58:33 +000085 by 0x40B07FF4: read_png_image(QImageIO *) (kernel/qpngio.cpp:326)
njn3e986b22004-11-30 10:43:45 +000086 by 0x40AC751B: QImageIO::read() (kernel/qimage.cpp:3621)
njn21f91952005-03-12 22:14:42 +000087 Address 0xBFFFF0E0 is not stack'd, malloc'd or free'd
njn3e986b22004-11-30 10:43:45 +000088]]></programlisting>
89
de03e0e7c2005-12-03 23:02:33 +000090<para>This happens when your program reads or writes memory at a place
91which Memcheck reckons it shouldn't. In this example, the program did a
924-byte read at address 0xBFFFF0E0, somewhere within the system-supplied
93library libpng.so.2.1.0.9, which was called from somewhere else in the
94same library, called from line 326 of <filename>qpngio.cpp</filename>,
95and so on.</para>
njn3e986b22004-11-30 10:43:45 +000096
de03e0e7c2005-12-03 23:02:33 +000097<para>Memcheck tries to establish what the illegal address might relate
98to, since that's often useful. So, if it points into a block of memory
99which has already been freed, you'll be informed of this, and also where
njn7316df22009-08-04 01:16:01 +0000100the block was freed. Likewise, if it should turn out to be just off
101the end of a heap block, a common result of off-by-one-errors in
de03e0e7c2005-12-03 23:02:33 +0000102array subscripting, you'll be informed of this fact, and also where the
njn2f7eebe2009-08-05 06:34:27 +0000103block was allocated. If you use the <option><xref
104linkend="opt.read-var-info"/></option> option Memcheck will run more slowly
105but may give a more detailed description of any illegal address.</para>
njn3e986b22004-11-30 10:43:45 +0000106
de03e0e7c2005-12-03 23:02:33 +0000107<para>In this example, Memcheck can't identify the address. Actually
108the address is on the stack, but, for some reason, this is not a valid
109stack address -- it is below the stack pointer and that isn't allowed.
njn7316df22009-08-04 01:16:01 +0000110In this particular case it's probably caused by GCC generating invalid
111code, a known bug in some ancient versions of GCC.</para>
njn3e986b22004-11-30 10:43:45 +0000112
de03e0e7c2005-12-03 23:02:33 +0000113<para>Note that Memcheck only tells you that your program is about to
114access memory at an illegal address. It can't stop the access from
115happening. So, if your program makes an access which normally would
116result in a segmentation fault, you program will still suffer the same
117fate -- but you will get a message from Memcheck immediately prior to
118this. In this particular example, reading junk on the stack is
119non-fatal, and the program stays alive.</para>
njn3e986b22004-11-30 10:43:45 +0000120
121</sect2>
122
123
124
125<sect2 id="mc-manual.uninitvals"
126 xreflabel="Use of uninitialised values">
127<title>Use of uninitialised values</title>
128
129<para>For example:</para>
130<programlisting><![CDATA[
131Conditional jump or move depends on uninitialised value(s)
132 at 0x402DFA94: _IO_vfprintf (_itoa.h:49)
133 by 0x402E8476: _IO_printf (printf.c:36)
134 by 0x8048472: main (tests/manuel1.c:8)
njn3e986b22004-11-30 10:43:45 +0000135]]></programlisting>
136
de03e0e7c2005-12-03 23:02:33 +0000137<para>An uninitialised-value use error is reported when your program
138uses a value which hasn't been initialised -- in other words, is
139undefined. Here, the undefined value is used somewhere inside the
njn2f7eebe2009-08-05 06:34:27 +0000140<function>printf</function> machinery of the C library. This error was
141reported when running the following small program:</para>
njn3e986b22004-11-30 10:43:45 +0000142<programlisting><![CDATA[
143int main()
144{
145 int x;
146 printf ("x = %d\n", x);
147}]]></programlisting>
148
de03e0e7c2005-12-03 23:02:33 +0000149<para>It is important to understand that your program can copy around
150junk (uninitialised) data as much as it likes. Memcheck observes this
151and keeps track of the data, but does not complain. A complaint is
152issued only when your program attempts to make use of uninitialised
njn2f7eebe2009-08-05 06:34:27 +0000153data in a way that might affect your program's externally-visible behaviour.
154In this example, <varname>x</varname> is uninitialised. Memcheck observes
155the value being passed to <function>_IO_printf</function> and thence to
156<function>_IO_vfprintf</function>, but makes no comment. However,
157<function>_IO_vfprintf</function> has to examine the value of
158<varname>x</varname> so it can turn it into the corresponding ASCII string,
159and it is at this point that Memcheck complains.</para>
njn3e986b22004-11-30 10:43:45 +0000160
161<para>Sources of uninitialised data tend to be:</para>
162<itemizedlist>
163 <listitem>
de03e0e7c2005-12-03 23:02:33 +0000164 <para>Local variables in procedures which have not been initialised,
165 as in the example above.</para>
njn3e986b22004-11-30 10:43:45 +0000166 </listitem>
167 <listitem>
njn7316df22009-08-04 01:16:01 +0000168 <para>The contents of heap blocks (allocated with
169 <function>malloc</function>, <function>new</function>, or a similar
170 function) before you (or a constructor) write something there.
171 </para>
njn3e986b22004-11-30 10:43:45 +0000172 </listitem>
173</itemizedlist>
174
sewardjcd0f2bd2008-05-04 23:06:28 +0000175<para>To see information on the sources of uninitialised data in your
njna3311642009-08-10 01:29:14 +0000176program, use the <option>--track-origins=yes</option> option. This
sewardjcd0f2bd2008-05-04 23:06:28 +0000177makes Memcheck run more slowly, but can make it much easier to track down
178the root causes of uninitialised value errors.</para>
179
njn3e986b22004-11-30 10:43:45 +0000180</sect2>
181
182
183
njn2f7eebe2009-08-05 06:34:27 +0000184<sect2 id="mc-manual.bad-syscall-args"
185 xreflabel="Use of uninitialised or unaddressable values in system
186 calls">
187<title>Use of uninitialised or unaddressable values in system
188 calls</title>
189
190<para>Memcheck checks all parameters to system calls:
191<itemizedlist>
192 <listitem>
193 <para>It checks all the direct parameters themselves, whether they are
194 initialised.</para>
195 </listitem>
196 <listitem>
197 <para>Also, if a system call needs to read from a buffer provided by
198 your program, Memcheck checks that the entire buffer is addressable
199 and its contents are initialised.</para>
200 </listitem>
201 <listitem>
202 <para>Also, if the system call needs to write to a user-supplied
203 buffer, Memcheck checks that the buffer is addressable.</para>
204 </listitem>
205</itemizedlist>
206</para>
207
208<para>After the system call, Memcheck updates its tracked information to
209precisely reflect any changes in memory state caused by the system
210call.</para>
211
212<para>Here's an example of two system calls with invalid parameters:</para>
213<programlisting><![CDATA[
214 #include <stdlib.h>
215 #include <unistd.h>
216 int main( void )
217 {
218 char* arr = malloc(10);
219 int* arr2 = malloc(sizeof(int));
220 write( 1 /* stdout */, arr, 10 );
221 exit(arr2[0]);
222 }
223]]></programlisting>
224
225<para>You get these complaints ...</para>
226<programlisting><![CDATA[
227 Syscall param write(buf) points to uninitialised byte(s)
228 at 0x25A48723: __write_nocancel (in /lib/tls/libc-2.3.3.so)
229 by 0x259AFAD3: __libc_start_main (in /lib/tls/libc-2.3.3.so)
230 by 0x8048348: (within /auto/homes/njn25/grind/head4/a.out)
231 Address 0x25AB8028 is 0 bytes inside a block of size 10 alloc'd
232 at 0x259852B0: malloc (vg_replace_malloc.c:130)
233 by 0x80483F1: main (a.c:5)
234
235 Syscall param exit(error_code) contains uninitialised byte(s)
236 at 0x25A21B44: __GI__exit (in /lib/tls/libc-2.3.3.so)
237 by 0x8048426: main (a.c:8)
238]]></programlisting>
239
240<para>... because the program has (a) written uninitialised junk
241from the heap block to the standard output, and (b) passed an
242uninitialised value to <function>exit</function>. Note that the first
243error refers to the memory pointed to by
244<computeroutput>buf</computeroutput> (not
245<computeroutput>buf</computeroutput> itself), but the second error
246refers directly to <computeroutput>exit</computeroutput>'s argument
247<computeroutput>arr2[0]</computeroutput>.</para>
248
249</sect2>
250
251
njn3e986b22004-11-30 10:43:45 +0000252<sect2 id="mc-manual.badfrees" xreflabel="Illegal frees">
253<title>Illegal frees</title>
254
255<para>For example:</para>
256<programlisting><![CDATA[
257Invalid free()
258 at 0x4004FFDF: free (vg_clientmalloc.c:577)
259 by 0x80484C7: main (tests/doublefree.c:10)
njn21f91952005-03-12 22:14:42 +0000260 Address 0x3807F7B4 is 0 bytes inside a block of size 177 free'd
njn3e986b22004-11-30 10:43:45 +0000261 at 0x4004FFDF: free (vg_clientmalloc.c:577)
262 by 0x80484C7: main (tests/doublefree.c:10)
njn3e986b22004-11-30 10:43:45 +0000263]]></programlisting>
264
bartaf25f672009-06-26 19:03:53 +0000265<para>Memcheck keeps track of the blocks allocated by your program
266with <function>malloc</function>/<computeroutput>new</computeroutput>,
267so it can know exactly whether or not the argument to
268<function>free</function>/<computeroutput>delete</computeroutput> is
269legitimate or not. Here, this test program has freed the same block
270twice. As with the illegal read/write errors, Memcheck attempts to
njn7316df22009-08-04 01:16:01 +0000271make sense of the address freed. If, as here, the address is one
bartaf25f672009-06-26 19:03:53 +0000272which has previously been freed, you wil be told that -- making
njn2f7eebe2009-08-05 06:34:27 +0000273duplicate frees of the same block easy to spot. You will also get this
274message if you try to free a pointer that doesn't point to the start of a
275heap block.</para>
njn3e986b22004-11-30 10:43:45 +0000276
277</sect2>
278
279
280<sect2 id="mc-manual.rudefn"
njn2f7eebe2009-08-05 06:34:27 +0000281 xreflabel="When a heap block is freed with an inappropriate deallocation
njn3e986b22004-11-30 10:43:45 +0000282function">
njn2f7eebe2009-08-05 06:34:27 +0000283<title>When a heap block is freed with an inappropriate deallocation
njn3e986b22004-11-30 10:43:45 +0000284function</title>
285
286<para>In the following example, a block allocated with
de03e0e7c2005-12-03 23:02:33 +0000287<function>new[]</function> has wrongly been deallocated with
288<function>free</function>:</para>
njn3e986b22004-11-30 10:43:45 +0000289<programlisting><![CDATA[
290Mismatched free() / delete / delete []
291 at 0x40043249: free (vg_clientfuncs.c:171)
292 by 0x4102BB4E: QGArray::~QGArray(void) (tools/qgarray.cpp:149)
293 by 0x4C261C41: PptDoc::~PptDoc(void) (include/qmemarray.h:60)
294 by 0x4C261F0E: PptXml::~PptXml(void) (pptxml.cc:44)
njn21f91952005-03-12 22:14:42 +0000295 Address 0x4BB292A8 is 0 bytes inside a block of size 64 alloc'd
sewardj08e31e22007-05-23 21:58:33 +0000296 at 0x4004318C: operator new[](unsigned int) (vg_clientfuncs.c:152)
njn3e986b22004-11-30 10:43:45 +0000297 by 0x4C21BC15: KLaola::readSBStream(int) const (klaola.cc:314)
298 by 0x4C21C155: KLaola::stream(KLaola::OLENode const *) (klaola.cc:416)
299 by 0x4C21788F: OLEFilter::convert(QCString const &) (olefilter.cc:272)
300]]></programlisting>
301
de03e0e7c2005-12-03 23:02:33 +0000302<para>In <literal>C++</literal> it's important to deallocate memory in a
303way compatible with how it was allocated. The deal is:</para>
njn3e986b22004-11-30 10:43:45 +0000304<itemizedlist>
305 <listitem>
306 <para>If allocated with
de03e0e7c2005-12-03 23:02:33 +0000307 <function>malloc</function>,
308 <function>calloc</function>,
309 <function>realloc</function>,
310 <function>valloc</function> or
311 <function>memalign</function>, you must
312 deallocate with <function>free</function>.</para>
njn3e986b22004-11-30 10:43:45 +0000313 </listitem>
314 <listitem>
de03e0e7c2005-12-03 23:02:33 +0000315 <para>If allocated with <function>new</function>, you must deallocate
316 with <function>delete</function>.</para>
njn3e986b22004-11-30 10:43:45 +0000317 </listitem>
njn2f7eebe2009-08-05 06:34:27 +0000318 <listitem>
319 <para>If allocated with <function>new[]</function>, you must
320 deallocate with <function>delete[]</function>.</para>
321 </listitem>
njn3e986b22004-11-30 10:43:45 +0000322</itemizedlist>
323
de03e0e7c2005-12-03 23:02:33 +0000324<para>The worst thing is that on Linux apparently it doesn't matter if
sewardj08e31e22007-05-23 21:58:33 +0000325you do mix these up, but the same program may then crash on a
326different platform, Solaris for example. So it's best to fix it
327properly. According to the KDE folks "it's amazing how many C++
328programmers don't know this".</para>
njn3e986b22004-11-30 10:43:45 +0000329
sewardj08e31e22007-05-23 21:58:33 +0000330<para>The reason behind the requirement is as follows. In some C++
331implementations, <function>delete[]</function> must be used for
332objects allocated by <function>new[]</function> because the compiler
333stores the size of the array and the pointer-to-member to the
334destructor of the array's content just before the pointer actually
njn2f7eebe2009-08-05 06:34:27 +0000335returned. <function>delete</function> doesn't account for this and will get
336confused, possibly corrupting the heap.</para>
de03e0e7c2005-12-03 23:02:33 +0000337
njn3e986b22004-11-30 10:43:45 +0000338</sect2>
339
340
341
njn3e986b22004-11-30 10:43:45 +0000342<sect2 id="mc-manual.overlap"
343 xreflabel="Overlapping source and destination blocks">
344<title>Overlapping source and destination blocks</title>
345
346<para>The following C library functions copy some data from one
347memory block to another (or something similar):
njn2f7eebe2009-08-05 06:34:27 +0000348<function>memcpy</function>,
349<function>strcpy</function>,
350<function>strncpy</function>,
351<function>strcat</function>,
352<function>strncat</function>.
de03e0e7c2005-12-03 23:02:33 +0000353The blocks pointed to by their <computeroutput>src</computeroutput> and
354<computeroutput>dst</computeroutput> pointers aren't allowed to overlap.
njn2f7eebe2009-08-05 06:34:27 +0000355The POSIX standards have wording along the lines "If copying takes place
356between objects that overlap, the behavior is undefined." Therefore,
357Memcheck checks for this.
358</para>
njn3e986b22004-11-30 10:43:45 +0000359
360<para>For example:</para>
361<programlisting><![CDATA[
362==27492== Source and destination overlap in memcpy(0xbffff294, 0xbffff280, 21)
363==27492== at 0x40026CDC: memcpy (mc_replace_strmem.c:71)
364==27492== by 0x804865A: main (overlap.c:40)
njn3e986b22004-11-30 10:43:45 +0000365]]></programlisting>
366
de03e0e7c2005-12-03 23:02:33 +0000367<para>You don't want the two blocks to overlap because one of them could
sewardj08e31e22007-05-23 21:58:33 +0000368get partially overwritten by the copying.</para>
njn3e986b22004-11-30 10:43:45 +0000369
njnccad0b82005-07-19 00:48:55 +0000370<para>You might think that Memcheck is being overly pedantic reporting
de03e0e7c2005-12-03 23:02:33 +0000371this in the case where <computeroutput>dst</computeroutput> is less than
372<computeroutput>src</computeroutput>. For example, the obvious way to
njn2f7eebe2009-08-05 06:34:27 +0000373implement <function>memcpy</function> is by copying from the first
de03e0e7c2005-12-03 23:02:33 +0000374byte to the last. However, the optimisation guides of some
375architectures recommend copying from the last byte down to the first.
njn2f7eebe2009-08-05 06:34:27 +0000376Also, some implementations of <function>memcpy</function> zero
de03e0e7c2005-12-03 23:02:33 +0000377<computeroutput>dst</computeroutput> before copying, because zeroing the
378destination's cache line(s) can improve performance.</para>
njnccad0b82005-07-19 00:48:55 +0000379
de03e0e7c2005-12-03 23:02:33 +0000380<para>The moral of the story is: if you want to write truly portable
381code, don't make any assumptions about the language
382implementation.</para>
njnccad0b82005-07-19 00:48:55 +0000383
njn3e986b22004-11-30 10:43:45 +0000384</sect2>
385
386
florian7b6899d2014-07-13 14:41:55 +0000387<sect2 id="mc-manual.fishyvalue"
388 xreflabel="Fishy argument values">
389<title>Fishy argument values</title>
390
391<para>All memory allocation functions take an argument specifying the
392size of the memory block that should be allocated. Clearly, the requested
393size should be a non-negative value and is typically not excessively large.
394For instance, it is extremely unlikly that the size of an allocation
395request exceeds 2**63 bytes on a 64-bit machine. It is much more likely that
396such a value is the result of an erroneous size calculation and is in effect
397a negative value (that just happens to appear excessively large because
398the bit pattern is interpreted as an unsigned integer).
399Such a value is called a "fishy value".
400
401The <varname>size</varname> argument of the following allocation functions
402is checked for being fishy:
403<function>malloc</function>,
404<function>calloc</function>,
405<function>realloc</function>,
406<function>memalign</function>,
407<function>new</function>,
408<function>new []</function>.
409<function>__builtin_new</function>,
410<function>__builtin_vec_new</function>,
411For <function>calloc</function> both arguments are being checked.
412</para>
413
414<para>For example:</para>
415<programlisting><![CDATA[
416==32233== Argument 'size' of function malloc has a fishy (possibly negative) value: -3
417==32233== at 0x4C2CFA7: malloc (vg_replace_malloc.c:298)
418==32233== by 0x400555: foo (fishy.c:15)
419==32233== by 0x400583: main (fishy.c:23)
420]]></programlisting>
421
422<para>In earlier Valgrind versions those values were being referred to
423as "silly arguments" and no back-trace was included.
424</para>
425
426</sect2>
427
428
njnab5b7142005-08-16 02:20:17 +0000429<sect2 id="mc-manual.leaks" xreflabel="Memory leak detection">
430<title>Memory leak detection</title>
431
njn2f7eebe2009-08-05 06:34:27 +0000432<para>Memcheck keeps track of all heap blocks issued in response to
bartaf25f672009-06-26 19:03:53 +0000433calls to
njn2f7eebe2009-08-05 06:34:27 +0000434<function>malloc</function>/<function>new</function> et al.
bartaf25f672009-06-26 19:03:53 +0000435So when the program exits, it knows which blocks have not been freed.
njnab5b7142005-08-16 02:20:17 +0000436</para>
437
de03e0e7c2005-12-03 23:02:33 +0000438<para>If <option>--leak-check</option> is set appropriately, for each
njn8225cc02009-03-09 22:52:24 +0000439remaining block, Memcheck determines if the block is reachable from pointers
440within the root-set. The root-set consists of (a) general purpose registers
441of all threads, and (b) initialised, aligned, pointer-sized data words in
442accessible client memory, including stacks.</para>
443
444<para>There are two ways a block can be reached. The first is with a
njn389f5702009-07-15 07:18:16 +0000445"start-pointer", i.e. a pointer to the start of the block. The second is with
446an "interior-pointer", i.e. a pointer to the middle of the block. There are
philippeab1fce92013-09-29 13:47:32 +0000447several ways we know of that an interior-pointer can occur:</para>
njn389f5702009-07-15 07:18:16 +0000448
449<itemizedlist>
450 <listitem>
451 <para>The pointer might have originally been a start-pointer and have been
njn7c02ba72011-01-04 23:46:07 +0000452 moved along deliberately (or not deliberately) by the program. In
453 particular, this can happen if your program uses tagged pointers, i.e.
454 if it uses the bottom one, two or three bits of a pointer, which are
455 normally always zero due to alignment, in order to store extra
456 information.</para>
njn389f5702009-07-15 07:18:16 +0000457 </listitem>
458
459 <listitem>
460 <para>It might be a random junk value in memory, entirely unrelated, just
461 a coincidence.</para>
462 </listitem>
463
464 <listitem>
philippe7c69a3e2014-07-21 19:55:11 +0000465 <para>It might be a pointer to the inner char array of a C++
466 <computeroutput>std::string</computeroutput>. For example, some
467 compilers add 3 words at the beginning of the std::string to
468 store the length, the capacity and a reference count before the
469 memory containing the array of characters. They return a pointer
470 just after these 3 words, pointing at the char array.</para>
471 </listitem>
472
473 <listitem>
474 <para>Some code might allocate a block of memory, and use the first 8
475 bytes to store (block size - 8) as a 64bit number.
476 <computeroutput>sqlite3MemMalloc</computeroutput> does this.</para>
477 </listitem>
478
479 <listitem>
njn389f5702009-07-15 07:18:16 +0000480 <para>It might be a pointer to an array of C++ objects (which possess
481 destructors) allocated with <computeroutput>new[]</computeroutput>. In
482 this case, some compilers store a "magic cookie" containing the array
483 length at the start of the allocated block, and return a pointer to just
484 past that magic cookie, i.e. an interior-pointer.
485 See <ulink url="http://theory.uwinnipeg.ca/gnu/gcc/gxxint_14.html">this
486 page</ulink> for more information.</para>
487 </listitem>
philippeab1fce92013-09-29 13:47:32 +0000488
489 <listitem>
philippeab1fce92013-09-29 13:47:32 +0000490 <para>It might be a pointer to an inner part of a C++ object using
491 multiple inheritance. </para>
492 </listitem>
bart9d6d2a92009-07-19 09:19:58 +0000493</itemizedlist>
njn8225cc02009-03-09 22:52:24 +0000494
philippeab1fce92013-09-29 13:47:32 +0000495<para>You can optionally activate heuristics to use during the leak
496search to detect the interior pointers corresponding to
philippe7c69a3e2014-07-21 19:55:11 +0000497the <computeroutput>stdstring</computeroutput>,
498<computeroutput>length64</computeroutput>,
499<computeroutput>newarray</computeroutput>
500and <computeroutput>multipleinheritance</computeroutput> cases. If the
philippeab1fce92013-09-29 13:47:32 +0000501heuristic detects that an interior pointer corresponds to such a case,
502the block will be considered as reachable by the interior
503pointer. In other words, the interior pointer will be treated
504as if it were a start pointer.</para>
505
506
njn8225cc02009-03-09 22:52:24 +0000507<para>With that in mind, consider the nine possible cases described by the
508following figure.</para>
509
510<programlisting><![CDATA[
philippe2193a7c2012-12-08 17:54:16 +0000511 Pointer chain AAA Leak Case BBB Leak Case
512 ------------- ------------- -------------
njn8225cc02009-03-09 22:52:24 +0000513(1) RRR ------------> BBB DR
514(2) RRR ---> AAA ---> BBB DR IR
515(3) RRR BBB DL
516(4) RRR AAA ---> BBB DL IL
517(5) RRR ------?-----> BBB (y)DR, (n)DL
518(6) RRR ---> AAA -?-> BBB DR (y)IR, (n)DL
519(7) RRR -?-> AAA ---> BBB (y)DR, (n)DL (y)IR, (n)IL
520(8) RRR -?-> AAA -?-> BBB (y)DR, (n)DL (y,y)IR, (n,y)IL, (_,n)DL
521(9) RRR AAA -?-> BBB DL (y)IL, (n)DL
522
523Pointer chain legend:
524- RRR: a root set node or DR block
525- AAA, BBB: heap blocks
526- --->: a start-pointer
527- -?->: an interior-pointer
528
philippe2193a7c2012-12-08 17:54:16 +0000529Leak Case legend:
njn8225cc02009-03-09 22:52:24 +0000530- DR: Directly reachable
531- IR: Indirectly reachable
532- DL: Directly lost
533- IL: Indirectly lost
534- (y)XY: it's XY if the interior-pointer is a real pointer
535- (n)XY: it's XY if the interior-pointer is not a real pointer
536- (_)XY: it's XY in either case
537]]></programlisting>
538
539<para>Every possible case can be reduced to one of the above nine. Memcheck
540merges some of these cases in its output, resulting in the following four
philippe2193a7c2012-12-08 17:54:16 +0000541leak kinds.</para>
njn8225cc02009-03-09 22:52:24 +0000542
njnab5b7142005-08-16 02:20:17 +0000543
544<itemizedlist>
545
546 <listitem>
njn8225cc02009-03-09 22:52:24 +0000547 <para>"Still reachable". This covers cases 1 and 2 (for the BBB blocks)
548 above. A start-pointer or chain of start-pointers to the block is
549 found. Since the block is still pointed at, the programmer could, at
philippe2193a7c2012-12-08 17:54:16 +0000550 least in principle, have freed it before program exit. "Still reachable"
551 blocks are very common and arguably not a problem. So, by default,
552 Memcheck won't report such blocks individually.</para>
njnab5b7142005-08-16 02:20:17 +0000553 </listitem>
554
555 <listitem>
njn8225cc02009-03-09 22:52:24 +0000556 <para>"Definitely lost". This covers case 3 (for the BBB blocks) above.
557 This means that no pointer to the block can be found. The block is
558 classified as "lost", because the programmer could not possibly have
559 freed it at program exit, since no pointer to it exists. This is likely
560 a symptom of having lost the pointer at some earlier point in the
561 program. Such cases should be fixed by the programmer.</para>
njnab5b7142005-08-16 02:20:17 +0000562 </listitem>
563
njn8225cc02009-03-09 22:52:24 +0000564 <listitem>
565 <para>"Indirectly lost". This covers cases 4 and 9 (for the BBB blocks)
566 above. This means that the block is lost, not because there are no
567 pointers to it, but rather because all the blocks that point to it are
568 themselves lost. For example, if you have a binary tree and the root
569 node is lost, all its children nodes will be indirectly lost. Because
570 the problem will disappear if the definitely lost block that caused the
571 indirect leak is fixed, Memcheck won't report such blocks individually
philippe2193a7c2012-12-08 17:54:16 +0000572 by default.</para>
njn8225cc02009-03-09 22:52:24 +0000573 </listitem>
574
575 <listitem>
576 <para>"Possibly lost". This covers cases 5--8 (for the BBB blocks)
577 above. This means that a chain of one or more pointers to the block has
578 been found, but at least one of the pointers is an interior-pointer.
579 This could just be a random value in memory that happens to point into a
580 block, and so you shouldn't consider this ok unless you know you have
581 interior-pointers.</para>
582 </listitem>
583
njnab5b7142005-08-16 02:20:17 +0000584</itemizedlist>
585
philippe2193a7c2012-12-08 17:54:16 +0000586<para>(Note: This mapping of the nine possible cases onto four leak kinds is
njn8225cc02009-03-09 22:52:24 +0000587not necessarily the best way that leaks could be reported; in particular,
588interior-pointers are treated inconsistently. It is possible the
589categorisation may be improved in the future.)</para>
590
591<para>Furthermore, if suppressions exists for a block, it will be reported
philippe2193a7c2012-12-08 17:54:16 +0000592as "suppressed" no matter what which of the above four kinds it belongs
njn8225cc02009-03-09 22:52:24 +0000593to.</para>
594
595
596<para>The following is an example leak summary.</para>
597
598<programlisting><![CDATA[
599LEAK SUMMARY:
600 definitely lost: 48 bytes in 3 blocks.
601 indirectly lost: 32 bytes in 2 blocks.
602 possibly lost: 96 bytes in 6 blocks.
603 still reachable: 64 bytes in 4 blocks.
604 suppressed: 0 bytes in 0 blocks.
605]]></programlisting>
606
philippeab1fce92013-09-29 13:47:32 +0000607<para>If heuristics have been used to consider some blocks as
608reachable, the leak summary details the heuristically reachable subset
philippe7c69a3e2014-07-21 19:55:11 +0000609of 'still reachable:' per heuristic. In the below example, of the 95
610bytes still reachable, 87 bytes (56+7+8+16) have been considered
philippeab1fce92013-09-29 13:47:32 +0000611heuristically reachable.
612</para>
613
614<programlisting><![CDATA[
615LEAK SUMMARY:
616 definitely lost: 4 bytes in 1 blocks
617 indirectly lost: 0 bytes in 0 blocks
618 possibly lost: 0 bytes in 0 blocks
philippe7c69a3e2014-07-21 19:55:11 +0000619 still reachable: 95 bytes in 6 blocks
philippeab1fce92013-09-29 13:47:32 +0000620 of which reachable via heuristic:
621 stdstring : 56 bytes in 2 blocks
philippe7c69a3e2014-07-21 19:55:11 +0000622 length64 : 16 bytes in 1 blocks
philippeab1fce92013-09-29 13:47:32 +0000623 newarray : 7 bytes in 1 blocks
624 multipleinheritance: 8 bytes in 1 blocks
625 suppressed: 0 bytes in 0 blocks
626]]></programlisting>
627
njn7e5d4ed2009-07-30 02:57:52 +0000628<para>If <option>--leak-check=full</option> is specified,
njn8225cc02009-03-09 22:52:24 +0000629Memcheck will give details for each definitely lost or possibly lost block,
njn62dd9fa2009-03-10 21:40:46 +0000630including where it was allocated. (Actually, it merges results for all
philippe2193a7c2012-12-08 17:54:16 +0000631blocks that have the same leak kind and sufficiently similar stack traces
njn62dd9fa2009-03-10 21:40:46 +0000632into a single "loss record". The
njn7e5d4ed2009-07-30 02:57:52 +0000633<option>--leak-resolution</option> lets you control the
njn62dd9fa2009-03-10 21:40:46 +0000634meaning of "sufficiently similar".) It cannot tell you when or how or why
635the pointer to a leaked block was lost; you have to work that out for
636yourself. In general, you should attempt to ensure your programs do not
637have any definitely lost or possibly lost blocks at exit.</para>
njnab5b7142005-08-16 02:20:17 +0000638
639<para>For example:</para>
640<programlisting><![CDATA[
6418 bytes in 1 blocks are definitely lost in loss record 1 of 14
642 at 0x........: malloc (vg_replace_malloc.c:...)
643 by 0x........: mk (leak-tree.c:11)
644 by 0x........: main (leak-tree.c:39)
645
njn8225cc02009-03-09 22:52:24 +000064688 (8 direct, 80 indirect) bytes in 1 blocks are definitely lost in loss record 13 of 14
njnab5b7142005-08-16 02:20:17 +0000647 at 0x........: malloc (vg_replace_malloc.c:...)
648 by 0x........: mk (leak-tree.c:11)
649 by 0x........: main (leak-tree.c:25)
650]]></programlisting>
651
de03e0e7c2005-12-03 23:02:33 +0000652<para>The first message describes a simple case of a single 8 byte block
njn8225cc02009-03-09 22:52:24 +0000653that has been definitely lost. The second case mentions another 8 byte
654block that has been definitely lost; the difference is that a further 80
njn62dd9fa2009-03-10 21:40:46 +0000655bytes in other blocks are indirectly lost because of this lost block.
656The loss records are not presented in any notable order, so the loss record
philippe2193a7c2012-12-08 17:54:16 +0000657numbers aren't particularly meaningful. The loss record numbers can be used
658in the Valgrind gdbserver to list the addresses of the leaked blocks and/or give
659more details about how a block is still reachable.</para>
njnab5b7142005-08-16 02:20:17 +0000660
philippe2193a7c2012-12-08 17:54:16 +0000661<para>The option <option>--show-leak-kinds=&lt;set&gt;</option>
662controls the set of leak kinds to show
sewardj4c7254d2013-11-29 23:08:28 +0000663when <option>--leak-check=full</option> is specified. </para>
philippe2193a7c2012-12-08 17:54:16 +0000664
sewardj4c7254d2013-11-29 23:08:28 +0000665<para>The <option>&lt;set&gt;</option> of leak kinds is specified
666in one of the following ways:
philippe2193a7c2012-12-08 17:54:16 +0000667
668<itemizedlist>
mjw4229cbd2013-12-12 21:20:48 +0000669 <listitem><para>a comma separated list of one or more of
670 <option>definite indirect possible reachable</option>.</para>
philippe2193a7c2012-12-08 17:54:16 +0000671 </listitem>
672
mjw4229cbd2013-12-12 21:20:48 +0000673 <listitem><para><option>all</option> to specify the complete set (all leak kinds).</para>
philippe2193a7c2012-12-08 17:54:16 +0000674 </listitem>
675
mjw4229cbd2013-12-12 21:20:48 +0000676 <listitem><para><option>none</option> for the empty set.</para>
philippe2193a7c2012-12-08 17:54:16 +0000677 </listitem>
678</itemizedlist>
679
680</para>
681
682<para> The default value for the leak kinds to show is
683 <option>--show-leak-kinds=definite,possible</option>.
684</para>
685
sewardj4c7254d2013-11-29 23:08:28 +0000686<para>To also show the reachable and indirectly lost blocks in
687addition to the definitely and possibly lost blocks, you can
688use <option>--show-leak-kinds=all</option>. To only show the
689reachable and indirectly lost blocks, use
690<option>--show-leak-kinds=indirect,reachable</option>. The reachable
691and indirectly lost blocks will then be presented as shown in
692the following two examples.</para>
njn8225cc02009-03-09 22:52:24 +0000693
694<programlisting><![CDATA[
69564 bytes in 4 blocks are still reachable in loss record 2 of 4
696 at 0x........: malloc (vg_replace_malloc.c:177)
697 by 0x........: mk (leak-cases.c:52)
698 by 0x........: main (leak-cases.c:74)
699
70032 bytes in 2 blocks are indirectly lost in loss record 1 of 4
701 at 0x........: malloc (vg_replace_malloc.c:177)
702 by 0x........: mk (leak-cases.c:52)
703 by 0x........: main (leak-cases.c:80)
704]]></programlisting>
njnab5b7142005-08-16 02:20:17 +0000705
philippe2193a7c2012-12-08 17:54:16 +0000706<para>Because there are different kinds of leaks with different
sewardj4c7254d2013-11-29 23:08:28 +0000707severities, an interesting question is: which leaks should be
philippe2193a7c2012-12-08 17:54:16 +0000708counted as true "errors" and which should not?
709</para>
njn26670552009-08-13 00:02:30 +0000710
philippe2193a7c2012-12-08 17:54:16 +0000711<para> The answer to this question affects the numbers printed in
712the <computeroutput>ERROR SUMMARY</computeroutput> line, and also the
713effect of the <option>--error-exitcode</option> option. First, a leak
714is only counted as a true "error"
715if <option>--leak-check=full</option> is specified. Then, the
716option <option>--errors-for-leak-kinds=&lt;set&gt;</option> controls
717the set of leak kinds to consider as errors. The default value
718is <option>--errors-for-leak-kinds=definite,possible</option>
719</para>
njn26670552009-08-13 00:02:30 +0000720
njnab5b7142005-08-16 02:20:17 +0000721</sect2>
722
njn3e986b22004-11-30 10:43:45 +0000723</sect1>
724
725
726
njna3311642009-08-10 01:29:14 +0000727<sect1 id="mc-manual.options"
728 xreflabel="Memcheck Command-Line Options">
729<title>Memcheck Command-Line Options</title>
njnc1abdcb2009-08-05 05:11:02 +0000730
731<!-- start of xi:include in the manpage -->
732<variablelist id="mc.opts.list">
733
734 <varlistentry id="opt.leak-check" xreflabel="--leak-check">
735 <term>
736 <option><![CDATA[--leak-check=<no|summary|yes|full> [default: summary] ]]></option>
737 </term>
738 <listitem>
739 <para>When enabled, search for memory leaks when the client
740 program finishes. If set to <varname>summary</varname>, it says how
741 many leaks occurred. If set to <varname>full</varname> or
philippe2a97c632015-01-05 17:00:54 +0000742 <varname>yes</varname>, each individual leak will be shown
743 in detail and/or counted as an error, as specified by the options
744 <option>--show-leak-kinds</option> and
745 <option>--errors-for-leak-kinds</option>. </para>
njnc1abdcb2009-08-05 05:11:02 +0000746 </listitem>
747 </varlistentry>
748
749 <varlistentry id="opt.leak-resolution" xreflabel="--leak-resolution">
750 <term>
751 <option><![CDATA[--leak-resolution=<low|med|high> [default: high] ]]></option>
752 </term>
753 <listitem>
754 <para>When doing leak checking, determines how willing
755 Memcheck is to consider different backtraces to
756 be the same for the purposes of merging multiple leaks into a single
757 leak report. When set to <varname>low</varname>, only the first
758 two entries need match. When <varname>med</varname>, four entries
759 have to match. When <varname>high</varname>, all entries need to
760 match.</para>
761
762 <para>For hardcore leak debugging, you probably want to use
763 <option>--leak-resolution=high</option> together with
764 <option>--num-callers=40</option> or some such large number.
765 </para>
766
767 <para>Note that the <option>--leak-resolution</option> setting
768 does not affect Memcheck's ability to find
769 leaks. It only changes how the results are presented.</para>
770 </listitem>
771 </varlistentry>
772
philippe2193a7c2012-12-08 17:54:16 +0000773 <varlistentry id="opt.show-leak-kinds" xreflabel="--show-leak-kinds">
njnc1abdcb2009-08-05 05:11:02 +0000774 <term>
philippe2193a7c2012-12-08 17:54:16 +0000775 <option><![CDATA[--show-leak-kinds=<set> [default: definite,possible] ]]></option>
njnc1abdcb2009-08-05 05:11:02 +0000776 </term>
777 <listitem>
philippe2a97c632015-01-05 17:00:54 +0000778 <para>Specifies the leak kinds to show in a <varname>full</varname>
779 leak search, in one of the following ways: </para>
philippe2193a7c2012-12-08 17:54:16 +0000780
philippee52e4452013-12-12 23:19:13 +0000781 <itemizedlist>
782 <listitem><para>a comma separated list of one or more of
mjw4229cbd2013-12-12 21:20:48 +0000783 <option>definite indirect possible reachable</option>.</para>
philippee52e4452013-12-12 23:19:13 +0000784 </listitem>
785
786 <listitem><para><option>all</option> to specify the complete set (all leak kinds).
philippe2193a7c2012-12-08 17:54:16 +0000787 It is equivalent to
mjw4229cbd2013-12-12 21:20:48 +0000788 <option>--show-leak-kinds=definite,indirect,possible,reachable</option>.</para>
philippee52e4452013-12-12 23:19:13 +0000789 </listitem>
790
791 <listitem><para><option>none</option> for the empty set.</para>
792 </listitem>
793 </itemizedlist>
njnc1abdcb2009-08-05 05:11:02 +0000794 </listitem>
795 </varlistentry>
796
philippe2193a7c2012-12-08 17:54:16 +0000797
798 <varlistentry id="opt.errors-for-leak-kinds" xreflabel="--errors-for-leak-kinds">
799 <term>
800 <option><![CDATA[--errors-for-leak-kinds=<set> [default: definite,possible] ]]></option>
801 </term>
802 <listitem>
philippe2a97c632015-01-05 17:00:54 +0000803 <para>Specifies the leak kinds to count as errors in a
804 <varname>full</varname> leak search. The
philippe2193a7c2012-12-08 17:54:16 +0000805 <option><![CDATA[<set>]]></option> is specified similarly to
806 <option>--show-leak-kinds</option>
807 </para>
808 </listitem>
809 </varlistentry>
810
811
philippeab1fce92013-09-29 13:47:32 +0000812 <varlistentry id="opt.leak-check-heuristics" xreflabel="--leak-check-heuristics">
813 <term>
814 <option><![CDATA[--leak-check-heuristics=<set> [default: none] ]]></option>
815 </term>
816 <listitem>
sewardj4c7254d2013-11-29 23:08:28 +0000817 <para>Specifies the set of leak check heuristics to be used
philippee52e4452013-12-12 23:19:13 +0000818 during leak searches. The heuristics control which interior pointers
sewardj4c7254d2013-11-29 23:08:28 +0000819 to a block cause it to be considered as reachable.
philippee52e4452013-12-12 23:19:13 +0000820 The heuristic set is specified in one of the following ways:</para>
philippeab1fce92013-09-29 13:47:32 +0000821
philippee52e4452013-12-12 23:19:13 +0000822 <itemizedlist>
823 <listitem><para>a comma separated list of one or more of
philippe7c69a3e2014-07-21 19:55:11 +0000824 <option>stdstring length64 newarray multipleinheritance</option>.</para>
philippee52e4452013-12-12 23:19:13 +0000825 </listitem>
philippeab1fce92013-09-29 13:47:32 +0000826
philippee52e4452013-12-12 23:19:13 +0000827 <listitem><para><option>all</option> to activate the complete set of
philippeab1fce92013-09-29 13:47:32 +0000828 heuristics.
829 It is equivalent to
philippe7c69a3e2014-07-21 19:55:11 +0000830 <option>--leak-check-heuristics=stdstring,length64,newarray,multipleinheritance</option>.</para>
philippee52e4452013-12-12 23:19:13 +0000831 </listitem>
832
833 <listitem><para><option>none</option> for the empty set.</para>
834 </listitem>
835 </itemizedlist>
836 </listitem>
philippeab1fce92013-09-29 13:47:32 +0000837
philippee52e4452013-12-12 23:19:13 +0000838 <para>Note that these heuristics are dependent on the layout of the objects
philippeab1fce92013-09-29 13:47:32 +0000839 produced by the C++ compiler. They have been tested with some gcc versions
840 (e.g. 4.4 and 4.7). They might not work properly with other C++ compilers.
philippee52e4452013-12-12 23:19:13 +0000841 </para>
philippeab1fce92013-09-29 13:47:32 +0000842 </varlistentry>
843
844
philippe2193a7c2012-12-08 17:54:16 +0000845 <varlistentry id="opt.show-reachable" xreflabel="--show-reachable">
846 <term>
847 <option><![CDATA[--show-reachable=<yes|no> ]]></option>
848 </term>
849 <term>
850 <option><![CDATA[--show-possibly-lost=<yes|no> ]]></option>
851 </term>
852 <listitem>
853 <para>These options provide an alternative way to specify the leak kinds to show:
philippee52e4452013-12-12 23:19:13 +0000854 </para>
855 <itemizedlist>
856 <listitem>
857 <para>
philippe2193a7c2012-12-08 17:54:16 +0000858 <option>--show-reachable=no --show-possibly-lost=yes</option> is equivalent to
859 <option>--show-leak-kinds=definite,possible</option>.
philippee52e4452013-12-12 23:19:13 +0000860 </para>
861 </listitem>
862 <listitem>
863 <para>
philippe2193a7c2012-12-08 17:54:16 +0000864 <option>--show-reachable=no --show-possibly-lost=no</option> is equivalent to
865 <option>--show-leak-kinds=definite</option>.
philippee52e4452013-12-12 23:19:13 +0000866 </para>
867 </listitem>
868 <listitem>
869 <para>
philippe2193a7c2012-12-08 17:54:16 +0000870 <option>--show-reachable=yes</option> is equivalent to
871 <option>--show-leak-kinds=all</option>.
philippee52e4452013-12-12 23:19:13 +0000872 </para>
873 </listitem>
874 </itemizedlist>
philippe2193a7c2012-12-08 17:54:16 +0000875 </listitem>
philippee52e4452013-12-12 23:19:13 +0000876 <para> Note that <option>--show-possibly-lost=no</option> has no effect
877 if <option>--show-reachable=yes</option> is specified.</para>
philippe2193a7c2012-12-08 17:54:16 +0000878 </varlistentry>
879
njnc1abdcb2009-08-05 05:11:02 +0000880 <varlistentry id="opt.undef-value-errors" xreflabel="--undef-value-errors">
881 <term>
882 <option><![CDATA[--undef-value-errors=<yes|no> [default: yes] ]]></option>
883 </term>
884 <listitem>
885 <para>Controls whether Memcheck reports
886 uses of undefined value errors. Set this to
887 <varname>no</varname> if you don't want to see undefined value
888 errors. It also has the side effect of speeding up
889 Memcheck somewhat.
890 </para>
891 </listitem>
892 </varlistentry>
893
894 <varlistentry id="opt.track-origins" xreflabel="--track-origins">
895 <term>
896 <option><![CDATA[--track-origins=<yes|no> [default: no] ]]></option>
897 </term>
898 <listitem>
899 <para>Controls whether Memcheck tracks
900 the origin of uninitialised values. By default, it does not,
901 which means that although it can tell you that an
902 uninitialised value is being used in a dangerous way, it
903 cannot tell you where the uninitialised value came from. This
904 often makes it difficult to track down the root problem.
905 </para>
906 <para>When set
907 to <varname>yes</varname>, Memcheck keeps
908 track of the origins of all uninitialised values. Then, when
909 an uninitialised value error is
910 reported, Memcheck will try to show the
911 origin of the value. An origin can be one of the following
912 four places: a heap block, a stack allocation, a client
913 request, or miscellaneous other sources (eg, a call
914 to <varname>brk</varname>).
915 </para>
916 <para>For uninitialised values originating from a heap
917 block, Memcheck shows where the block was
918 allocated. For uninitialised values originating from a stack
919 allocation, Memcheck can tell you which
920 function allocated the value, but no more than that -- typically
921 it shows you the source location of the opening brace of the
922 function. So you should carefully check that all of the
923 function's local variables are initialised properly.
924 </para>
925 <para>Performance overhead: origin tracking is expensive. It
926 halves Memcheck's speed and increases
927 memory use by a minimum of 100MB, and possibly more.
928 Nevertheless it can drastically reduce the effort required to
929 identify the root cause of uninitialised value errors, and so
930 is often a programmer productivity win, despite running
931 more slowly.
932 </para>
933 <para>Accuracy: Memcheck tracks origins
934 quite accurately. To avoid very large space and time
935 overheads, some approximations are made. It is possible,
936 although unlikely, that Memcheck will report an incorrect origin, or
937 not be able to identify any origin.
938 </para>
939 <para>Note that the combination
940 <option>--track-origins=yes</option>
941 and <option>--undef-value-errors=no</option> is
942 nonsensical. Memcheck checks for and
943 rejects this combination at startup.
944 </para>
945 </listitem>
946 </varlistentry>
947
948 <varlistentry id="opt.partial-loads-ok" xreflabel="--partial-loads-ok">
949 <term>
sewardjadd95c82015-08-31 10:36:02 +0000950 <option><![CDATA[--partial-loads-ok=<yes|no> [default: yes] ]]></option>
njnc1abdcb2009-08-05 05:11:02 +0000951 </term>
952 <listitem>
sewardj4c7254d2013-11-29 23:08:28 +0000953 <para>Controls how Memcheck handles 32-, 64-, 128- and 256-bit
954 naturally aligned loads from addresses for which some bytes are
njnc1abdcb2009-08-05 05:11:02 +0000955 addressable and others are not. When <varname>yes</varname>, such
956 loads do not produce an address error. Instead, loaded bytes
957 originating from illegal addresses are marked as uninitialised, and
958 those corresponding to legal addresses are handled in the normal
959 way.</para>
960
961 <para>When <varname>no</varname>, loads from partially invalid
962 addresses are treated the same as loads from completely invalid
963 addresses: an illegal-address error is issued, and the resulting
964 bytes are marked as initialised.</para>
965
966 <para>Note that code that behaves in this way is in violation of
mjw2be51222013-04-05 13:19:12 +0000967 the ISO C/C++ standards, and should be considered broken. If
sewardjadd95c82015-08-31 10:36:02 +0000968 at all possible, such code should be fixed.</para>
njnc1abdcb2009-08-05 05:11:02 +0000969 </listitem>
970 </varlistentry>
971
sewardj2672fae2015-09-01 08:48:04 +0000972 <varlistentry id="opt.expensive-definedness-checks" xreflabel="--expensive-definedness-checks">
florian9ee20eb2015-08-27 17:50:47 +0000973 <term>
sewardj2672fae2015-09-01 08:48:04 +0000974 <option><![CDATA[--expensive-definedness-checks=<yes|no> [default: no] ]]></option>
florian9ee20eb2015-08-27 17:50:47 +0000975 </term>
976 <listitem>
977 <para>Controls whether Memcheck should employ more precise but also more
978 expensive (time consuming) algorithms when checking the definedness of a
979 value. The default setting is not to do that and it is usually
980 sufficient. However, for highly optimised code valgrind may sometimes
981 incorrectly complain.
sewardj2672fae2015-09-01 08:48:04 +0000982 Invoking valgrind with <option>--expensive-definedness-checks=yes</option>
florian9ee20eb2015-08-27 17:50:47 +0000983 helps but comes at a performance cost. Runtime degradation of
984 25% have been observed but the extra cost depends a lot on the
985 application at hand.
986 </para>
987 </listitem>
988 </varlistentry>
989
philippe8617b5b2013-01-12 19:53:08 +0000990 <varlistentry id="opt.keep-stacktraces" xreflabel="--keep-stacktraces">
991 <term>
philippe92cd8382015-08-29 10:45:26 +0000992 <option><![CDATA[--keep-stacktraces=alloc|free|alloc-and-free|alloc-then-free|none [default: alloc-and-free] ]]></option>
philippe8617b5b2013-01-12 19:53:08 +0000993 </term>
994 <listitem>
995 <para>Controls which stack trace(s) to keep for malloc'd and/or
996 free'd blocks.
997 </para>
998
sewardj4c7254d2013-11-29 23:08:28 +0000999 <para>With <varname>alloc-then-free</varname>, a stack trace is
1000 recorded at allocation time, and is associated with the block.
1001 When the block is freed, a second stack trace is recorded, and
1002 this replaces the allocation stack trace. As a result, any "use
1003 after free" errors relating to this block can only show a stack
1004 trace for where the block was freed.
philippe8617b5b2013-01-12 19:53:08 +00001005 </para>
1006
sewardj4c7254d2013-11-29 23:08:28 +00001007 <para>With <varname>alloc-and-free</varname>, both allocation
1008 and the deallocation stack traces for the block are stored.
1009 Hence a "use after free" error will
1010 show both, which may make the error easier to diagnose.
1011 Compared to <varname>alloc-then-free</varname>, this setting
1012 slightly increases Valgrind's memory use as the block contains two
1013 references instead of one.
philippe8617b5b2013-01-12 19:53:08 +00001014 </para>
1015
sewardj4c7254d2013-11-29 23:08:28 +00001016 <para>With <varname>alloc</varname>, only the allocation stack
1017 trace is recorded (and reported). With <varname>free</varname>,
1018 only the deallocation stack trace is recorded (and reported).
1019 These values somewhat decrease Valgrind's memory and cpu usage.
1020 They can be useful depending on the error types you are
1021 searching for and the level of detail you need to analyse
1022 them. For example, if you are only interested in memory leak
1023 errors, it is sufficient to record the allocation stack traces.
philippe8617b5b2013-01-12 19:53:08 +00001024 </para>
1025
1026 <para>With <varname>none</varname>, no stack traces are recorded
1027 for malloc and free operations. If your program allocates a lot
sewardj4c7254d2013-11-29 23:08:28 +00001028 of blocks and/or allocates/frees from many different stack
1029 traces, this can significantly decrease cpu and/or memory
1030 required. Of course, few details will be reported for errors
1031 related to heap blocks.
philippe8617b5b2013-01-12 19:53:08 +00001032 </para>
1033
sewardj4c7254d2013-11-29 23:08:28 +00001034 <para>Note that once a stack trace is recorded, Valgrind keeps
1035 the stack trace in memory even if it is not referenced by any
1036 block. Some programs (for example, recursive algorithms) can
1037 generate a huge number of stack traces. If Valgrind uses too
philippe8617b5b2013-01-12 19:53:08 +00001038 much memory in such circumstances, you can reduce the memory
sewardj4c7254d2013-11-29 23:08:28 +00001039 required with the options <varname>--keep-stacktraces</varname>
philippe8617b5b2013-01-12 19:53:08 +00001040 and/or by using a smaller value for the
1041 option <varname>--num-callers</varname>.
1042 </para>
1043 </listitem>
1044 </varlistentry>
1045
njnc1abdcb2009-08-05 05:11:02 +00001046 <varlistentry id="opt.freelist-vol" xreflabel="--freelist-vol">
1047 <term>
sewardje089f012010-10-13 21:47:29 +00001048 <option><![CDATA[--freelist-vol=<number> [default: 20000000] ]]></option>
njnc1abdcb2009-08-05 05:11:02 +00001049 </term>
1050 <listitem>
1051 <para>When the client program releases memory using
1052 <function>free</function> (in <literal>C</literal>) or
1053 <computeroutput>delete</computeroutput>
1054 (<literal>C++</literal>), that memory is not immediately made
1055 available for re-allocation. Instead, it is marked inaccessible
1056 and placed in a queue of freed blocks. The purpose is to defer as
1057 long as possible the point at which freed-up memory comes back
1058 into circulation. This increases the chance that
1059 Memcheck will be able to detect invalid
1060 accesses to blocks for some significant period of time after they
1061 have been freed.</para>
1062
njna3311642009-08-10 01:29:14 +00001063 <para>This option specifies the maximum total size, in bytes, of the
sewardje089f012010-10-13 21:47:29 +00001064 blocks in the queue. The default value is twenty million bytes.
njnc1abdcb2009-08-05 05:11:02 +00001065 Increasing this increases the total amount of memory used by
1066 Memcheck but may detect invalid uses of freed
1067 blocks which would otherwise go undetected.</para>
1068 </listitem>
1069 </varlistentry>
1070
sewardj403d8aa2011-10-22 19:48:57 +00001071 <varlistentry id="opt.freelist-big-blocks" xreflabel="--freelist-big-blocks">
1072 <term>
1073 <option><![CDATA[--freelist-big-blocks=<number> [default: 1000000] ]]></option>
1074 </term>
1075 <listitem>
1076 <para>When making blocks from the queue of freed blocks available
1077 for re-allocation, Memcheck will in priority re-circulate the blocks
1078 with a size greater or equal to <option>--freelist-big-blocks</option>.
1079 This ensures that freeing big blocks (in particular freeing blocks bigger than
1080 <option>--freelist-vol</option>) does not immediately lead to a re-circulation
1081 of all (or a lot of) the small blocks in the free list. In other words,
1082 this option increases the likelihood to discover dangling pointers
1083 for the "small" blocks, even when big blocks are freed.</para>
1084 <para>Setting a value of 0 means that all the blocks are re-circulated
1085 in a FIFO order. </para>
1086 </listitem>
1087 </varlistentry>
1088
njnc1abdcb2009-08-05 05:11:02 +00001089 <varlistentry id="opt.workaround-gcc296-bugs" xreflabel="--workaround-gcc296-bugs">
1090 <term>
1091 <option><![CDATA[--workaround-gcc296-bugs=<yes|no> [default: no] ]]></option>
1092 </term>
1093 <listitem>
1094 <para>When enabled, assume that reads and writes some small
1095 distance below the stack pointer are due to bugs in GCC 2.96, and
1096 does not report them. The "small distance" is 256 bytes by
1097 default. Note that GCC 2.96 is the default compiler on some ancient
1098 Linux distributions (RedHat 7.X) and so you may need to use this
njna3311642009-08-10 01:29:14 +00001099 option. Do not use it if you do not have to, as it can cause real
njnc1abdcb2009-08-05 05:11:02 +00001100 errors to be overlooked. A better alternative is to use a more
1101 recent GCC in which this bug is fixed.</para>
1102
njna3311642009-08-10 01:29:14 +00001103 <para>You may also need to use this option when working with
njnc1abdcb2009-08-05 05:11:02 +00001104 GCC 3.X or 4.X on 32-bit PowerPC Linux. This is because
1105 GCC generates code which occasionally accesses below the
1106 stack pointer, particularly for floating-point to/from integer
1107 conversions. This is in violation of the 32-bit PowerPC ELF
1108 specification, which makes no provision for locations below the
1109 stack pointer to be accessible.</para>
1110 </listitem>
1111 </varlistentry>
1112
sewardj021e6b62014-08-22 19:26:23 +00001113 <varlistentry id="opt.show-mismatched-frees"
1114 xreflabel="--show-mismatched-frees">
1115 <term>
1116 <option><![CDATA[--show-mismatched-frees=<yes|no> [default: yes] ]]></option>
1117 </term>
1118 <listitem>
1119 <para>When enabled, Memcheck checks that heap blocks are
1120 deallocated using a function that matches the allocating
1121 function. That is, it expects <varname>free</varname> to be
1122 used to deallocate blocks allocated
1123 by <varname>malloc</varname>, <varname>delete</varname> for
1124 blocks allocated by <varname>new</varname>,
1125 and <varname>delete[]</varname> for blocks allocated
1126 by <varname>new[]</varname>. If a mismatch is detected, an
1127 error is reported. This is in general important because in some
1128 environments, freeing with a non-matching function can cause
1129 crashes.</para>
1130
1131 <para>There is however a scenario where such mismatches cannot
1132 be avoided. That is when the user provides implementations of
1133 <varname>new</varname>/<varname>new[]</varname> that
1134 call <varname>malloc</varname> and
1135 of <varname>delete</varname>/<varname>delete[]</varname> that
1136 call <varname>free</varname>, and these functions are
1137 asymmetrically inlined. For example, imagine
1138 that <varname>delete[]</varname> is inlined
1139 but <varname>new[]</varname> is not. The result is that
1140 Memcheck "sees" all <varname>delete[]</varname> calls as direct
1141 calls to <varname>free</varname>, even when the program source
1142 contains no mismatched calls.</para>
1143
1144 <para>This causes a lot of confusing and irrelevant error
1145 reports. <varname>--show-mismatched-frees=no</varname> disables
1146 these checks. It is not generally advisable to disable them,
1147 though, because you may miss real errors as a result.</para>
1148 </listitem>
1149 </varlistentry>
1150
njnc1abdcb2009-08-05 05:11:02 +00001151 <varlistentry id="opt.ignore-ranges" xreflabel="--ignore-ranges">
1152 <term>
1153 <option><![CDATA[--ignore-ranges=0xPP-0xQQ[,0xRR-0xSS] ]]></option>
1154 </term>
1155 <listitem>
1156 <para>Any ranges listed in this option (and multiple ranges can be
1157 specified, separated by commas) will be ignored by Memcheck's
1158 addressability checking.</para>
1159 </listitem>
1160 </varlistentry>
1161
1162 <varlistentry id="opt.malloc-fill" xreflabel="--malloc-fill">
1163 <term>
1164 <option><![CDATA[--malloc-fill=<hexnumber> ]]></option>
1165 </term>
1166 <listitem>
1167 <para>Fills blocks allocated
1168 by <computeroutput>malloc</computeroutput>,
1169 <computeroutput>new</computeroutput>, etc, but not
1170 by <computeroutput>calloc</computeroutput>, with the specified
1171 byte. This can be useful when trying to shake out obscure
1172 memory corruption problems. The allocated area is still
njna3311642009-08-10 01:29:14 +00001173 regarded by Memcheck as undefined -- this option only affects its
philippea2cc0c02012-05-11 22:10:39 +00001174 contents. Note that <option>--malloc-fill</option> does not
1175 affect a block of memory when it is used as argument
1176 to client requests VALGRIND_MEMPOOL_ALLOC or
1177 VALGRIND_MALLOCLIKE_BLOCK.
njnc1abdcb2009-08-05 05:11:02 +00001178 </para>
1179 </listitem>
1180 </varlistentry>
1181
1182 <varlistentry id="opt.free-fill" xreflabel="--free-fill">
1183 <term>
1184 <option><![CDATA[--free-fill=<hexnumber> ]]></option>
1185 </term>
1186 <listitem>
1187 <para>Fills blocks freed
1188 by <computeroutput>free</computeroutput>,
1189 <computeroutput>delete</computeroutput>, etc, with the
1190 specified byte value. This can be useful when trying to shake out
1191 obscure memory corruption problems. The freed area is still
njna3311642009-08-10 01:29:14 +00001192 regarded by Memcheck as not valid for access -- this option only
philippea2cc0c02012-05-11 22:10:39 +00001193 affects its contents. Note that <option>--free-fill</option> does not
1194 affect a block of memory when it is used as argument to
1195 client requests VALGRIND_MEMPOOL_FREE or VALGRIND_FREELIKE_BLOCK.
njnc1abdcb2009-08-05 05:11:02 +00001196 </para>
1197 </listitem>
1198 </varlistentry>
1199
1200</variablelist>
1201<!-- end of xi:include in the manpage -->
1202
1203</sect1>
1204
1205
njn62ad73d2005-08-15 04:26:13 +00001206<sect1 id="mc-manual.suppfiles" xreflabel="Writing suppression files">
1207<title>Writing suppression files</title>
njn3e986b22004-11-30 10:43:45 +00001208
1209<para>The basic suppression format is described in
1210<xref linkend="manual-core.suppress"/>.</para>
1211
sewardj08e31e22007-05-23 21:58:33 +00001212<para>The suppression-type (second) line should have the form:</para>
njn3e986b22004-11-30 10:43:45 +00001213<programlisting><![CDATA[
1214Memcheck:suppression_type]]></programlisting>
1215
njn3e986b22004-11-30 10:43:45 +00001216<para>The Memcheck suppression types are as follows:</para>
1217
1218<itemizedlist>
1219 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001220 <para><varname>Value1</varname>,
1221 <varname>Value2</varname>,
1222 <varname>Value4</varname>,
1223 <varname>Value8</varname>,
1224 <varname>Value16</varname>,
njn3e986b22004-11-30 10:43:45 +00001225 meaning an uninitialised-value error when
1226 using a value of 1, 2, 4, 8 or 16 bytes.</para>
1227 </listitem>
1228
1229 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001230 <para><varname>Cond</varname> (or its old
de03e0e7c2005-12-03 23:02:33 +00001231 name, <varname>Value0</varname>), meaning use
njn3e986b22004-11-30 10:43:45 +00001232 of an uninitialised CPU condition code.</para>
1233 </listitem>
1234
1235 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001236 <para><varname>Addr1</varname>,
de03e0e7c2005-12-03 23:02:33 +00001237 <varname>Addr2</varname>,
1238 <varname>Addr4</varname>,
1239 <varname>Addr8</varname>,
1240 <varname>Addr16</varname>,
njn3e986b22004-11-30 10:43:45 +00001241 meaning an invalid address during a
1242 memory access of 1, 2, 4, 8 or 16 bytes respectively.</para>
1243 </listitem>
1244
1245 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001246 <para><varname>Jump</varname>, meaning an
njn718d3b12006-12-16 00:54:12 +00001247 jump to an unaddressable location error.</para>
1248 </listitem>
1249
1250 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001251 <para><varname>Param</varname>, meaning an
njn3e986b22004-11-30 10:43:45 +00001252 invalid system call parameter error.</para>
1253 </listitem>
1254
1255 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001256 <para><varname>Free</varname>, meaning an
njn3e986b22004-11-30 10:43:45 +00001257 invalid or mismatching free.</para>
1258 </listitem>
1259
1260 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001261 <para><varname>Overlap</varname>, meaning a
njn3e986b22004-11-30 10:43:45 +00001262 <computeroutput>src</computeroutput> /
1263 <computeroutput>dst</computeroutput> overlap in
njn2f7eebe2009-08-05 06:34:27 +00001264 <function>memcpy</function> or a similar function.</para>
njn3e986b22004-11-30 10:43:45 +00001265 </listitem>
1266
1267 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001268 <para><varname>Leak</varname>, meaning
njn62ad73d2005-08-15 04:26:13 +00001269 a memory leak.</para>
njn3e986b22004-11-30 10:43:45 +00001270 </listitem>
1271
1272</itemizedlist>
1273
philippe2193a7c2012-12-08 17:54:16 +00001274<para><computeroutput>Param</computeroutput> errors have a mandatory extra
sewardj08e31e22007-05-23 21:58:33 +00001275information line at this point, which is the name of the offending
philippe2193a7c2012-12-08 17:54:16 +00001276system call parameter. </para>
1277
sewardj4c7254d2013-11-29 23:08:28 +00001278<para><computeroutput>Leak</computeroutput> errors have an optional
1279extra information line, with the following format:</para>
philippe2193a7c2012-12-08 17:54:16 +00001280<programlisting><![CDATA[
1281match-leak-kinds:<set>]]></programlisting>
1282<para>where <computeroutput>&lt;set&gt;</computeroutput> specifies which
1283leak kinds are matched by this suppression entry.
sewardj4c7254d2013-11-29 23:08:28 +00001284<computeroutput>&lt;set&gt;</computeroutput> is specified in the
1285same way as with the option <option>--show-leak-kinds</option>, that is,
1286one of the following:</para>
1287<itemizedlist>
1288 <listitem>a comma separated list of one or more of
1289 <option>definite indirect possible reachable</option>.
1290 </listitem>
philippe2193a7c2012-12-08 17:54:16 +00001291
sewardj4c7254d2013-11-29 23:08:28 +00001292 <listitem><option>all</option> to specify the complete set (all leak kinds).
1293 </listitem>
1294
1295 <listitem><option>none</option> for the empty set.
1296 </listitem>
1297</itemizedlist>
1298<para>If this optional extra line is not present, the suppression
1299entry will match all leak kinds.</para>
1300
1301<para>Be aware that leak suppressions that are created using
1302<option>--gen-suppressions</option> will contain this optional extra
1303line, and therefore may match fewer leaks than you expect. You may
1304want to remove the line before using the generated
1305suppressions.</para>
1306
1307<para>The other Memcheck error kinds do not have extra lines.</para>
njn3e986b22004-11-30 10:43:45 +00001308
philippe4e32d672013-10-17 22:10:41 +00001309<para>
1310If you give the <option>-v</option> option, Valgrind will print
sewardj4c7254d2013-11-29 23:08:28 +00001311the list of used suppressions at the end of execution.
philippe4e32d672013-10-17 22:10:41 +00001312For a leak suppression, this output gives the number of different
sewardj4c7254d2013-11-29 23:08:28 +00001313loss records that match the suppression, and the number of bytes
1314and blocks suppressed by the suppression.
1315If the run contains multiple leak checks, the number of bytes and blocks
1316are reset to zero before each new leak check. Note that the number of different
1317loss records is not reset to zero.</para>
philippe4e32d672013-10-17 22:10:41 +00001318<para>In the example below, in the last leak search, 7 blocks and 96 bytes have
sewardj4c7254d2013-11-29 23:08:28 +00001319been suppressed by a suppression with the name
1320<option>some_leak_suppression</option>:</para>
philippe4e32d672013-10-17 22:10:41 +00001321<programlisting><![CDATA[
1322--21041-- used_suppression: 10 some_other_leak_suppression s.supp:14 suppressed: 12,400 bytes in 1 blocks
1323--21041-- used_suppression: 39 some_leak_suppression s.supp:2 suppressed: 96 bytes in 7 blocks
1324]]></programlisting>
philippe4e32d672013-10-17 22:10:41 +00001325
sewardj4c7254d2013-11-29 23:08:28 +00001326<para>For <varname>ValueN</varname> and <varname>AddrN</varname>
1327errors, the first line of the calling context is either the name of
1328the function in which the error occurred, or, failing that, the full
1329path of the <filename>.so</filename> file or executable containing the
1330error location. For <varname>Free</varname> errors, the first line is
1331the name of the function doing the freeing (eg,
1332<function>free</function>, <function>__builtin_vec_delete</function>,
1333etc). For <varname>Overlap</varname> errors, the first line is the name of the
1334function with the overlapping arguments (eg.
1335<function>memcpy</function>, <function>strcpy</function>, etc).</para>
njn3e986b22004-11-30 10:43:45 +00001336
sewardj4c7254d2013-11-29 23:08:28 +00001337<para>The last part of any suppression specifies the rest of the
1338calling context that needs to be matched.</para>
njn3e986b22004-11-30 10:43:45 +00001339
1340</sect1>
1341
1342
1343
1344<sect1 id="mc-manual.machine"
1345 xreflabel="Details of Memcheck's checking machinery">
1346<title>Details of Memcheck's checking machinery</title>
1347
1348<para>Read this section if you want to know, in detail, exactly
1349what and how Memcheck is checking.</para>
1350
1351
1352<sect2 id="mc-manual.value" xreflabel="Valid-value (V) bit">
1353<title>Valid-value (V) bits</title>
1354
de03e0e7c2005-12-03 23:02:33 +00001355<para>It is simplest to think of Memcheck implementing a synthetic CPU
1356which is identical to a real CPU, except for one crucial detail. Every
1357bit (literally) of data processed, stored and handled by the real CPU
1358has, in the synthetic CPU, an associated "valid-value" bit, which says
1359whether or not the accompanying bit has a legitimate value. In the
1360discussions which follow, this bit is referred to as the V (valid-value)
njn3e986b22004-11-30 10:43:45 +00001361bit.</para>
1362
de03e0e7c2005-12-03 23:02:33 +00001363<para>Each byte in the system therefore has a 8 V bits which follow it
1364wherever it goes. For example, when the CPU loads a word-size item (4
1365bytes) from memory, it also loads the corresponding 32 V bits from a
1366bitmap which stores the V bits for the process' entire address space.
1367If the CPU should later write the whole or some part of that value to
1368memory at a different address, the relevant V bits will be stored back
1369in the V-bit bitmap.</para>
njn3e986b22004-11-30 10:43:45 +00001370
njn2f7eebe2009-08-05 06:34:27 +00001371<para>In short, each bit in the system has (conceptually) an associated V
1372bit, which follows it around everywhere, even inside the CPU. Yes, all the
1373CPU's registers (integer, floating point, vector and condition registers)
1374have their own V bit vectors. For this to work, Memcheck uses a great deal
1375of compression to represent the V bits compactly.</para>
njn3e986b22004-11-30 10:43:45 +00001376
de03e0e7c2005-12-03 23:02:33 +00001377<para>Copying values around does not cause Memcheck to check for, or
1378report on, errors. However, when a value is used in a way which might
njn2f7eebe2009-08-05 06:34:27 +00001379conceivably affect your program's externally-visible behaviour,
1380the associated V bits are immediately checked. If any of these indicate
1381that the value is undefined (even partially), an error is reported.</para>
njn3e986b22004-11-30 10:43:45 +00001382
1383<para>Here's an (admittedly nonsensical) example:</para>
1384<programlisting><![CDATA[
1385int i, j;
1386int a[10], b[10];
1387for ( i = 0; i < 10; i++ ) {
1388 j = a[i];
1389 b[i] = j;
1390}]]></programlisting>
1391
de03e0e7c2005-12-03 23:02:33 +00001392<para>Memcheck emits no complaints about this, since it merely copies
1393uninitialised values from <varname>a[]</varname> into
sewardj08e31e22007-05-23 21:58:33 +00001394<varname>b[]</varname>, and doesn't use them in a way which could
1395affect the behaviour of the program. However, if
de03e0e7c2005-12-03 23:02:33 +00001396the loop is changed to:</para>
njn3e986b22004-11-30 10:43:45 +00001397<programlisting><![CDATA[
1398for ( i = 0; i < 10; i++ ) {
1399 j += a[i];
1400}
1401if ( j == 77 )
1402 printf("hello there\n");
1403]]></programlisting>
1404
sewardj08e31e22007-05-23 21:58:33 +00001405<para>then Memcheck will complain, at the
de03e0e7c2005-12-03 23:02:33 +00001406<computeroutput>if</computeroutput>, that the condition depends on
1407uninitialised values. Note that it <command>doesn't</command> complain
1408at the <varname>j += a[i];</varname>, since at that point the
1409undefinedness is not "observable". It's only when a decision has to be
1410made as to whether or not to do the <function>printf</function> -- an
1411observable action of your program -- that Memcheck complains.</para>
njn3e986b22004-11-30 10:43:45 +00001412
de03e0e7c2005-12-03 23:02:33 +00001413<para>Most low level operations, such as adds, cause Memcheck to use the
1414V bits for the operands to calculate the V bits for the result. Even if
1415the result is partially or wholly undefined, it does not
njn62ad73d2005-08-15 04:26:13 +00001416complain.</para>
njn3e986b22004-11-30 10:43:45 +00001417
de03e0e7c2005-12-03 23:02:33 +00001418<para>Checks on definedness only occur in three places: when a value is
1419used to generate a memory address, when control flow decision needs to
sewardj08e31e22007-05-23 21:58:33 +00001420be made, and when a system call is detected, Memcheck checks definedness
de03e0e7c2005-12-03 23:02:33 +00001421of parameters as required.</para>
njn3e986b22004-11-30 10:43:45 +00001422
1423<para>If a check should detect undefinedness, an error message is
de03e0e7c2005-12-03 23:02:33 +00001424issued. The resulting value is subsequently regarded as well-defined.
sewardj08e31e22007-05-23 21:58:33 +00001425To do otherwise would give long chains of error messages. In other
1426words, once Memcheck reports an undefined value error, it tries to
1427avoid reporting further errors derived from that same undefined
1428value.</para>
njn3e986b22004-11-30 10:43:45 +00001429
de03e0e7c2005-12-03 23:02:33 +00001430<para>This sounds overcomplicated. Why not just check all reads from
1431memory, and complain if an undefined value is loaded into a CPU
1432register? Well, that doesn't work well, because perfectly legitimate C
1433programs routinely copy uninitialised values around in memory, and we
1434don't want endless complaints about that. Here's the canonical example.
1435Consider a struct like this:</para>
njn3e986b22004-11-30 10:43:45 +00001436<programlisting><![CDATA[
1437struct S { int x; char c; };
1438struct S s1, s2;
1439s1.x = 42;
1440s1.c = 'z';
1441s2 = s1;
1442]]></programlisting>
1443
de03e0e7c2005-12-03 23:02:33 +00001444<para>The question to ask is: how large is <varname>struct S</varname>,
1445in bytes? An <varname>int</varname> is 4 bytes and a
1446<varname>char</varname> one byte, so perhaps a <varname>struct
sewardj08e31e22007-05-23 21:58:33 +00001447S</varname> occupies 5 bytes? Wrong. All non-toy compilers we know
de03e0e7c2005-12-03 23:02:33 +00001448of will round the size of <varname>struct S</varname> up to a whole
1449number of words, in this case 8 bytes. Not doing this forces compilers
sewardj08e31e22007-05-23 21:58:33 +00001450to generate truly appalling code for accessing arrays of
1451<varname>struct S</varname>'s on some architectures.</para>
njn3e986b22004-11-30 10:43:45 +00001452
de03e0e7c2005-12-03 23:02:33 +00001453<para>So <varname>s1</varname> occupies 8 bytes, yet only 5 of them will
njn7316df22009-08-04 01:16:01 +00001454be initialised. For the assignment <varname>s2 = s1</varname>, GCC
de03e0e7c2005-12-03 23:02:33 +00001455generates code to copy all 8 bytes wholesale into <varname>s2</varname>
1456without regard for their meaning. If Memcheck simply checked values as
1457they came out of memory, it would yelp every time a structure assignment
sewardj08e31e22007-05-23 21:58:33 +00001458like this happened. So the more complicated behaviour described above
njn7316df22009-08-04 01:16:01 +00001459is necessary. This allows GCC to copy
de03e0e7c2005-12-03 23:02:33 +00001460<varname>s1</varname> into <varname>s2</varname> any way it likes, and a
1461warning will only be emitted if the uninitialised values are later
1462used.</para>
njn3e986b22004-11-30 10:43:45 +00001463
njn3e986b22004-11-30 10:43:45 +00001464</sect2>
1465
1466
1467<sect2 id="mc-manual.vaddress" xreflabel=" Valid-address (A) bits">
1468<title>Valid-address (A) bits</title>
1469
de03e0e7c2005-12-03 23:02:33 +00001470<para>Notice that the previous subsection describes how the validity of
1471values is established and maintained without having to say whether the
1472program does or does not have the right to access any particular memory
sewardj08e31e22007-05-23 21:58:33 +00001473location. We now consider the latter question.</para>
njn3e986b22004-11-30 10:43:45 +00001474
de03e0e7c2005-12-03 23:02:33 +00001475<para>As described above, every bit in memory or in the CPU has an
1476associated valid-value (V) bit. In addition, all bytes in memory, but
1477not in the CPU, have an associated valid-address (A) bit. This
1478indicates whether or not the program can legitimately read or write that
sewardj49d5a282011-02-28 10:26:42 +00001479location. It does not give any indication of the validity of the data
de03e0e7c2005-12-03 23:02:33 +00001480at that location -- that's the job of the V bits -- only whether or not
1481the location may be accessed.</para>
njn3e986b22004-11-30 10:43:45 +00001482
de03e0e7c2005-12-03 23:02:33 +00001483<para>Every time your program reads or writes memory, Memcheck checks
1484the A bits associated with the address. If any of them indicate an
1485invalid address, an error is emitted. Note that the reads and writes
1486themselves do not change the A bits, only consult them.</para>
njn3e986b22004-11-30 10:43:45 +00001487
njn62ad73d2005-08-15 04:26:13 +00001488<para>So how do the A bits get set/cleared? Like this:</para>
njn3e986b22004-11-30 10:43:45 +00001489
1490<itemizedlist>
1491 <listitem>
1492 <para>When the program starts, all the global data areas are
1493 marked as accessible.</para>
1494 </listitem>
1495
1496 <listitem>
bartaf25f672009-06-26 19:03:53 +00001497 <para>When the program does
1498 <function>malloc</function>/<computeroutput>new</computeroutput>,
1499 the A bits for exactly the area allocated, and not a byte more,
1500 are marked as accessible. Upon freeing the area the A bits are
1501 changed to indicate inaccessibility.</para>
njn3e986b22004-11-30 10:43:45 +00001502 </listitem>
1503
1504 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001505 <para>When the stack pointer register (<literal>SP</literal>) moves
1506 up or down, A bits are set. The rule is that the area from
1507 <literal>SP</literal> up to the base of the stack is marked as
1508 accessible, and below <literal>SP</literal> is inaccessible. (If
1509 that sounds illogical, bear in mind that the stack grows down, not
1510 up, on almost all Unix systems, including GNU/Linux.) Tracking
1511 <literal>SP</literal> like this has the useful side-effect that the
1512 section of stack used by a function for local variables etc is
1513 automatically marked accessible on function entry and inaccessible
1514 on exit.</para>
njn3e986b22004-11-30 10:43:45 +00001515 </listitem>
1516
1517 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001518 <para>When doing system calls, A bits are changed appropriately.
sewardj08e31e22007-05-23 21:58:33 +00001519 For example, <literal>mmap</literal>
1520 magically makes files appear in the process'
1521 address space, so the A bits must be updated if <literal>mmap</literal>
de03e0e7c2005-12-03 23:02:33 +00001522 succeeds.</para>
njn3e986b22004-11-30 10:43:45 +00001523 </listitem>
1524
1525 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001526 <para>Optionally, your program can tell Memcheck about such changes
de03e0e7c2005-12-03 23:02:33 +00001527 explicitly, using the client request mechanism described
1528 above.</para>
njn3e986b22004-11-30 10:43:45 +00001529 </listitem>
1530
1531</itemizedlist>
1532
1533</sect2>
1534
1535
1536<sect2 id="mc-manual.together" xreflabel="Putting it all together">
1537<title>Putting it all together</title>
1538
1539<para>Memcheck's checking machinery can be summarised as
1540follows:</para>
1541
1542<itemizedlist>
1543 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001544 <para>Each byte in memory has 8 associated V (valid-value) bits,
1545 saying whether or not the byte has a defined value, and a single A
1546 (valid-address) bit, saying whether or not the program currently has
sewardje089f012010-10-13 21:47:29 +00001547 the right to read/write that address. As mentioned above, heavy
1548 use of compression means the overhead is typically around 25%.</para>
njn3e986b22004-11-30 10:43:45 +00001549 </listitem>
1550
1551 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001552 <para>When memory is read or written, the relevant A bits are
sewardj08e31e22007-05-23 21:58:33 +00001553 consulted. If they indicate an invalid address, Memcheck emits an
de03e0e7c2005-12-03 23:02:33 +00001554 Invalid read or Invalid write error.</para>
njn3e986b22004-11-30 10:43:45 +00001555 </listitem>
1556
1557 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001558 <para>When memory is read into the CPU's registers, the relevant V
1559 bits are fetched from memory and stored in the simulated CPU. They
1560 are not consulted.</para>
njn3e986b22004-11-30 10:43:45 +00001561 </listitem>
1562
1563 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001564 <para>When a register is written out to memory, the V bits for that
1565 register are written back to memory too.</para>
njn3e986b22004-11-30 10:43:45 +00001566 </listitem>
1567
1568 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001569 <para>When values in CPU registers are used to generate a memory
1570 address, or to determine the outcome of a conditional branch, the V
1571 bits for those values are checked, and an error emitted if any of
1572 them are undefined.</para>
njn3e986b22004-11-30 10:43:45 +00001573 </listitem>
1574
1575 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001576 <para>When values in CPU registers are used for any other purpose,
sewardj08e31e22007-05-23 21:58:33 +00001577 Memcheck computes the V bits for the result, but does not check
de03e0e7c2005-12-03 23:02:33 +00001578 them.</para>
njn3e986b22004-11-30 10:43:45 +00001579 </listitem>
1580
1581 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001582 <para>Once the V bits for a value in the CPU have been checked, they
de03e0e7c2005-12-03 23:02:33 +00001583 are then set to indicate validity. This avoids long chains of
1584 errors.</para>
njn3e986b22004-11-30 10:43:45 +00001585 </listitem>
1586
1587 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001588 <para>When values are loaded from memory, Memcheck checks the A bits
de03e0e7c2005-12-03 23:02:33 +00001589 for that location and issues an illegal-address warning if needed.
1590 In that case, the V bits loaded are forced to indicate Valid,
1591 despite the location being invalid.</para>
1592
1593 <para>This apparently strange choice reduces the amount of confusing
1594 information presented to the user. It avoids the unpleasant
1595 phenomenon in which memory is read from a place which is both
sewardj33878892007-11-17 09:43:25 +00001596 unaddressable and contains invalid values, and, as a result, you get
de03e0e7c2005-12-03 23:02:33 +00001597 not only an invalid-address (read/write) error, but also a
1598 potentially large set of uninitialised-value errors, one for every
1599 time the value is used.</para>
1600
1601 <para>There is a hazy boundary case to do with multi-byte loads from
1602 addresses which are partially valid and partially invalid. See
njna3311642009-08-10 01:29:14 +00001603 details of the option <option>--partial-loads-ok</option> for details.
de03e0e7c2005-12-03 23:02:33 +00001604 </para>
njn3e986b22004-11-30 10:43:45 +00001605 </listitem>
1606
1607</itemizedlist>
1608
1609
bartaf25f672009-06-26 19:03:53 +00001610<para>Memcheck intercepts calls to <function>malloc</function>,
1611<function>calloc</function>, <function>realloc</function>,
1612<function>valloc</function>, <function>memalign</function>,
1613<function>free</function>, <computeroutput>new</computeroutput>,
1614<computeroutput>new[]</computeroutput>,
1615<computeroutput>delete</computeroutput> and
1616<computeroutput>delete[]</computeroutput>. The behaviour you get
njn3e986b22004-11-30 10:43:45 +00001617is:</para>
1618
1619<itemizedlist>
1620
1621 <listitem>
bartaf25f672009-06-26 19:03:53 +00001622 <para><function>malloc</function>/<function>new</function>/<computeroutput>new[]</computeroutput>:
1623 the returned memory is marked as addressable but not having valid
1624 values. This means you have to write to it before you can read
1625 it.</para>
njn3e986b22004-11-30 10:43:45 +00001626 </listitem>
1627
1628 <listitem>
bartaf25f672009-06-26 19:03:53 +00001629 <para><function>calloc</function>: returned memory is marked both
1630 addressable and valid, since <function>calloc</function> clears
1631 the area to zero.</para>
njn3e986b22004-11-30 10:43:45 +00001632 </listitem>
1633
1634 <listitem>
bartaf25f672009-06-26 19:03:53 +00001635 <para><function>realloc</function>: if the new size is larger than
1636 the old, the new section is addressable but invalid, as with
njn2f7eebe2009-08-05 06:34:27 +00001637 <function>malloc</function>. If the new size is smaller, the
1638 dropped-off section is marked as unaddressable. You may only pass to
bartaf25f672009-06-26 19:03:53 +00001639 <function>realloc</function> a pointer previously issued to you by
1640 <function>malloc</function>/<function>calloc</function>/<function>realloc</function>.</para>
njn3e986b22004-11-30 10:43:45 +00001641 </listitem>
1642
1643 <listitem>
bartaf25f672009-06-26 19:03:53 +00001644 <para><function>free</function>/<computeroutput>delete</computeroutput>/<computeroutput>delete[]</computeroutput>:
1645 you may only pass to these functions a pointer previously issued
1646 to you by the corresponding allocation function. Otherwise,
1647 Memcheck complains. If the pointer is indeed valid, Memcheck
1648 marks the entire area it points at as unaddressable, and places
1649 the block in the freed-blocks-queue. The aim is to defer as long
1650 as possible reallocation of this block. Until that happens, all
1651 attempts to access it will elicit an invalid-address error, as you
1652 would hope.</para>
njn3e986b22004-11-30 10:43:45 +00001653 </listitem>
1654
1655</itemizedlist>
1656
1657</sect2>
1658</sect1>
1659
sewardj3b290482011-05-06 21:02:55 +00001660<sect1 id="mc-manual.monitor-commands" xreflabel="Memcheck Monitor Commands">
1661<title>Memcheck Monitor Commands</title>
sewardjc8bd1df2011-06-26 12:41:33 +00001662<para>The Memcheck tool provides monitor commands handled by Valgrind's
1663built-in gdbserver (see <xref linkend="manual-core-adv.gdbserver-commandhandling"/>).
sewardj3b290482011-05-06 21:02:55 +00001664</para>
njn3e986b22004-11-30 10:43:45 +00001665
sewardj3b290482011-05-06 21:02:55 +00001666<itemizedlist>
1667 <listitem>
philippeb92e6322015-08-04 19:11:03 +00001668 <para><varname>xb &lt;addr&gt; [&lt;len&gt;]</varname>
1669 shows the definedness (V) bits and values for &lt;len&gt; (default 1)
1670 bytes starting at &lt;addr&gt;.
1671 For each 8 bytes, two lines are output.
sewardjc8bd1df2011-06-26 12:41:33 +00001672 </para>
1673 <para>
philippeb92e6322015-08-04 19:11:03 +00001674 The first line shows the validity bits for 8 bytes.
1675 The definedness of each byte in the range is given using two hexadecimal
1676 digits. These hexadecimal digits encode the validity of each bit of the
1677 corresponding byte,
1678 using 0 if the bit is defined and 1 if the bit is undefined.
1679 If a byte is not addressable, its validity bits are replaced
1680 by <varname>__</varname> (a double underscore).
1681 </para>
1682 <para>
1683 The second line shows the values of the bytes below the corresponding
1684 validity bits. The format used to show the bytes data is similar to the
1685 GDB command 'x /&lt;len&gt;xb &lt;addr&gt;'. The value for a non
1686 addressable bytes is shown as ?? (two question marks).
1687 </para>
1688 <para>
1689 In the following example, <varname>string10</varname> is an array
1690 of 10 characters, in which the even numbered bytes are
1691 undefined. In the below example, the byte corresponding
1692 to <varname>string10[5]</varname> is not addressable.
sewardjc8bd1df2011-06-26 12:41:33 +00001693 </para>
sewardj3b290482011-05-06 21:02:55 +00001694<programlisting><![CDATA[
1695(gdb) p &string10
philippeb92e6322015-08-04 19:11:03 +00001696$4 = (char (*)[10]) 0x804a2f0
1697(gdb) mo xb 0x804a2f0 10
1698 ff 00 ff 00 ff __ ff 00
16990x804A2F0: 0x3f 0x6e 0x3f 0x65 0x3f 0x?? 0x3f 0x65
1700 ff 00
17010x804A2F8: 0x3f 0x00
1702Address 0x804A2F0 len 10 has 1 bytes unaddressable
1703(gdb)
sewardj3b290482011-05-06 21:02:55 +00001704]]></programlisting>
sewardj30b3eca2011-06-28 08:20:39 +00001705
philippeb92e6322015-08-04 19:11:03 +00001706 <para> The command xb cannot be used with registers. To get
1707 the validity bits of a register, you must start Valgrind with the
1708 option <option>--vgdb-shadow-registers=yes</option>. The validity
1709 bits of a register can then be obtained by printing the 'shadow 1'
1710 corresponding register. In the below x86 example, the register
1711 eax has all its bits undefined, while the register ebx is fully
1712 defined.
sewardj30b3eca2011-06-28 08:20:39 +00001713 </para>
1714<programlisting><![CDATA[
1715(gdb) p /x $eaxs1
1716$9 = 0xffffffff
1717(gdb) p /x $ebxs1
1718$10 = 0x0
1719(gdb)
1720]]></programlisting>
1721
sewardj3b290482011-05-06 21:02:55 +00001722 </listitem>
1723
1724 <listitem>
philippeb92e6322015-08-04 19:11:03 +00001725 <para><varname>get_vbits &lt;addr&gt; [&lt;len&gt;]</varname>
1726 shows the definedness (V) bits for &lt;len&gt; (default 1) bytes
1727 starting at &lt;addr&gt; using the same convention as the
1728 <varname>xb</varname> command. <varname>get_vbits</varname> only
1729 shows the V bits (grouped by 4 bytes). It does not show the values.
1730 If you want to associate V bits with the corresponding byte values, the
1731 <varname>xb</varname> command will be easier to use, in particular
1732 on little endian computers when associating undefined parts of an integer
1733 with their V bits values.
1734 </para>
1735 <para>
1736 The following example shows the result of <varname>get_vibts</varname>
1737 on the <varname>string10</varname> used in the <varname>xb</varname>
1738 command explanation.
1739 </para>
1740<programlisting><![CDATA[
1741(gdb) monitor get_vbits 0x804a2f0 10
1742ff00ff00 ff__ff00 ff00
1743Address 0x804A2F0 len 10 has 1 bytes unaddressable
1744(gdb)
1745]]></programlisting>
1746
1747 </listitem>
1748
1749 <listitem>
sewardj30b3eca2011-06-28 08:20:39 +00001750 <para><varname>make_memory
1751 [noaccess|undefined|defined|Definedifaddressable] &lt;addr&gt;
sewardjc8bd1df2011-06-26 12:41:33 +00001752 [&lt;len&gt;]</varname> marks the range of &lt;len&gt; (default 1)
1753 bytes at &lt;addr&gt; as having the given status. Parameter
1754 <varname>noaccess</varname> marks the range as non-accessible, so
1755 Memcheck will report an error on any access to it.
1756 <varname>undefined</varname> or <varname>defined</varname> mark
1757 the area as accessible, but Memcheck regards the bytes in it
1758 respectively as having undefined or defined values.
sewardj30b3eca2011-06-28 08:20:39 +00001759 <varname>Definedifaddressable</varname> marks as defined, bytes in
sewardjc8bd1df2011-06-26 12:41:33 +00001760 the range which are already addressible, but makes no change to
sewardj30b3eca2011-06-28 08:20:39 +00001761 the status of bytes in the range which are not addressible. Note
1762 that the first letter of <varname>Definedifaddressable</varname>
1763 is an uppercase D to avoid confusion with <varname>defined</varname>.
1764 </para>
sewardjc8bd1df2011-06-26 12:41:33 +00001765
1766 <para>
1767 In the following example, the first byte of the
1768 <varname>string10</varname> is marked as defined:
sewardj3b290482011-05-06 21:02:55 +00001769 </para>
1770<programlisting><![CDATA[
sewardj30b3eca2011-06-28 08:20:39 +00001771(gdb) monitor make_memory defined 0x8049e28 1
1772(gdb) monitor get_vbits 0x8049e28 10
sewardj3b290482011-05-06 21:02:55 +000017730000ff00 ff00ff00 ff00
1774(gdb)
1775]]></programlisting>
1776 </listitem>
1777
1778 <listitem>
sewardj30b3eca2011-06-28 08:20:39 +00001779 <para><varname>check_memory [addressable|defined] &lt;addr&gt;
sewardj3b290482011-05-06 21:02:55 +00001780 [&lt;len&gt;]</varname> checks that the range of &lt;len&gt;
sewardj30b3eca2011-06-28 08:20:39 +00001781 (default 1) bytes at &lt;addr&gt; has the specified accessibility.
1782 It then outputs a description of &lt;addr&gt;. In the following
1783 example, a detailed description is available because the
philippea22f59d2012-01-26 23:13:52 +00001784 option <option>--read-var-info=yes</option> was given at Valgrind
sewardj30b3eca2011-06-28 08:20:39 +00001785 startup:
sewardj3b290482011-05-06 21:02:55 +00001786 </para>
1787<programlisting><![CDATA[
sewardj30b3eca2011-06-28 08:20:39 +00001788(gdb) monitor check_memory defined 0x8049e28 1
sewardj3b290482011-05-06 21:02:55 +00001789Address 0x8049E28 len 1 defined
1790==14698== Location 0x8049e28 is 0 bytes inside string10[0],
1791==14698== declared at prog.c:10, in frame #0 of thread 1
1792(gdb)
1793]]></programlisting>
1794 </listitem>
1795
1796 <listitem>
sewardj30b3eca2011-06-28 08:20:39 +00001797 <para><varname>leak_check [full*|summary]
philippe2193a7c2012-12-08 17:54:16 +00001798 [kinds &lt;set&gt;|reachable|possibleleak*|definiteleak]
philippe278b2a32013-10-09 20:12:39 +00001799 [heuristics heur1,heur2,...]
sewardj30b3eca2011-06-28 08:20:39 +00001800 [increased*|changed|any]
philippe84234902012-01-14 13:53:13 +00001801 [unlimited*|limited &lt;max_loss_records_output&gt;]
sewardjc8bd1df2011-06-26 12:41:33 +00001802 </varname>
1803 performs a leak check. The <varname>*</varname> in the arguments
philippe84234902012-01-14 13:53:13 +00001804 indicates the default values. </para>
sewardj3b290482011-05-06 21:02:55 +00001805
philippe278b2a32013-10-09 20:12:39 +00001806 <para> If the <varname>[full*|summary]</varname> argument is
1807 <varname>summary</varname>, only a summary of the leak search is given;
1808 otherwise a full leak report is produced. A full leak report gives
1809 detailed information for each leak: the stack trace where the leaked blocks
1810 were allocated, the number of blocks leaked and their total size. When a
1811 full report is requested, the next two arguments further specify what
sewardjc8bd1df2011-06-26 12:41:33 +00001812 kind of leaks to report. A leak's details are shown if they match
philippe84234902012-01-14 13:53:13 +00001813 both the second and third argument. A full leak report might
1814 output detailed information for many leaks. The nr of leaks for
1815 which information is output can be controlled using
1816 the <varname>limited</varname> argument followed by the maximum nr
1817 of leak records to output. If this maximum is reached, the leak
1818 search outputs the records with the biggest number of bytes.
sewardj3b290482011-05-06 21:02:55 +00001819 </para>
1820
philippe278b2a32013-10-09 20:12:39 +00001821 <para>The <varname>kinds</varname> argument controls what kind of blocks
1822 are shown for a <varname>full</varname> leak search. The set of leak kinds
1823 to show can be specified using a <varname>&lt;set&gt;</varname> similarly
philippe2193a7c2012-12-08 17:54:16 +00001824 to the command line option <option>--show-leak-kinds</option>.
1825 Alternatively, the value <varname>definiteleak</varname>
1826 is equivalent to <varname>kinds definite</varname>, the
1827 value <varname>possibleleak</varname> is equivalent to
1828 <varname>kinds definite,possible</varname> : it will also show
1829 possibly leaked blocks, .i.e those for which only an interior
1830 pointer was found. The value <varname>reachable</varname> will
1831 show all block categories (i.e. is equivalent to <varname>kinds
1832 all</varname>).
sewardj3b290482011-05-06 21:02:55 +00001833 </para>
sewardjc8bd1df2011-06-26 12:41:33 +00001834
philippe278b2a32013-10-09 20:12:39 +00001835 <para>The <varname>heuristics</varname> argument controls the heuristics
1836 used during the leak search. The set of heuristics to use can be specified
1837 using a <varname>&lt;set&gt;</varname> similarly
1838 to the command line option <option>--leak-check-heuristics</option>.
1839 The default value for the <varname>heuristics</varname> argument is
1840 <varname>heuristics none</varname>.
1841 </para>
1842
1843 <para>The <varname>[increased*|changed|any]</varname> argument controls what
1844 kinds of changes are shown for a <varname>full</varname> leak search. The
sewardjc8bd1df2011-06-26 12:41:33 +00001845 value <varname>increased</varname> specifies that only block
1846 allocation stacks with an increased number of leaked bytes or
1847 blocks since the previous leak check should be shown. The
1848 value <varname>changed</varname> specifies that allocation stacks
1849 with any change since the previous leak check should be shown.
1850 The value <varname>any</varname> specifies that all leak entries
1851 should be shown, regardless of any increase or decrease. When
1852 If <varname>increased</varname> or <varname>changed</varname> are
1853 specified, the leak report entries will show the delta relative to
1854 the previous leak report.
1855 </para>
1856
1857 <para>The following example shows usage of the
philippe84234902012-01-14 13:53:13 +00001858 <varname>leak_check</varname> monitor command on
sewardjc8bd1df2011-06-26 12:41:33 +00001859 the <varname>memcheck/tests/leak-cases.c</varname> regression
1860 test. The first command outputs one entry having an increase in
1861 the leaked bytes. The second command is the same as the first
1862 command, but uses the abbreviated forms accepted by GDB and the
1863 Valgrind gdbserver. It only outputs the summary information, as
1864 there was no increase since the previous leak search.</para>
sewardj3b290482011-05-06 21:02:55 +00001865<programlisting><![CDATA[
sewardj30b3eca2011-06-28 08:20:39 +00001866(gdb) monitor leak_check full possibleleak increased
philippea22f59d2012-01-26 23:13:52 +00001867==19520== 16 (+16) bytes in 1 (+1) blocks are possibly lost in loss record 9 of 12
1868==19520== at 0x40070B4: malloc (vg_replace_malloc.c:263)
1869==19520== by 0x80484D5: mk (leak-cases.c:52)
1870==19520== by 0x804855F: f (leak-cases.c:81)
1871==19520== by 0x80488E0: main (leak-cases.c:107)
1872==19520==
1873==19520== LEAK SUMMARY:
1874==19520== definitely lost: 32 (+0) bytes in 2 (+0) blocks
1875==19520== indirectly lost: 16 (+0) bytes in 1 (+0) blocks
1876==19520== possibly lost: 32 (+16) bytes in 2 (+1) blocks
1877==19520== still reachable: 96 (+16) bytes in 6 (+1) blocks
1878==19520== suppressed: 0 (+0) bytes in 0 (+0) blocks
1879==19520== Reachable blocks (those to which a pointer was found) are not shown.
1880==19520== To see them, add 'reachable any' args to leak_check
1881==19520==
sewardj30b3eca2011-06-28 08:20:39 +00001882(gdb) mo l
philippea22f59d2012-01-26 23:13:52 +00001883==19520== LEAK SUMMARY:
1884==19520== definitely lost: 32 (+0) bytes in 2 (+0) blocks
1885==19520== indirectly lost: 16 (+0) bytes in 1 (+0) blocks
1886==19520== possibly lost: 32 (+0) bytes in 2 (+0) blocks
1887==19520== still reachable: 96 (+0) bytes in 6 (+0) blocks
1888==19520== suppressed: 0 (+0) bytes in 0 (+0) blocks
1889==19520== Reachable blocks (those to which a pointer was found) are not shown.
1890==19520== To see them, add 'reachable any' args to leak_check
1891==19520==
sewardj3b290482011-05-06 21:02:55 +00001892(gdb)
1893]]></programlisting>
sewardjc8bd1df2011-06-26 12:41:33 +00001894 <para>Note that when using Valgrind's gdbserver, it is not
1895 necessary to rerun
1896 with <option>--leak-check=full</option>
1897 <option>--show-reachable=yes</option> to see the reachable
1898 blocks. You can obtain the same information without rerunning by
sewardj30b3eca2011-06-28 08:20:39 +00001899 using the GDB command <computeroutput>monitor leak_check full
sewardjc8bd1df2011-06-26 12:41:33 +00001900 reachable any</computeroutput> (or, using
sewardj30b3eca2011-06-28 08:20:39 +00001901 abbreviation: <computeroutput>mo l f r a</computeroutput>).
sewardj3b290482011-05-06 21:02:55 +00001902 </para>
1903 </listitem>
philippe84234902012-01-14 13:53:13 +00001904
philippea22f59d2012-01-26 23:13:52 +00001905 <listitem>
philippe6d3cb492015-08-13 22:49:32 +00001906 <para><varname>block_list &lt;loss_record_nr&gt;
1907 [unlimited*|limited &lt;max_blocks&gt;]</varname>
1908 shows the list of blocks belonging to &lt;loss_record_nr&gt;.
1909 The nr of blocks to print can be controlled using the
1910 <varname>limited</varname> argument followed by the maximum nr
1911 of blocks to output.
philippea22f59d2012-01-26 23:13:52 +00001912 </para>
1913
1914 <para> A leak search merges the allocated blocks in loss records :
1915 a loss record re-groups all blocks having the same state (for
1916 example, Definitely Lost) and the same allocation backtrace.
1917 Each loss record is identified in the leak search result
1918 by a loss record number.
1919 The <varname>block_list</varname> command shows the loss record information
1920 followed by the addresses and sizes of the blocks which have been
philippe9b7b0882015-08-31 22:08:47 +00001921 merged in the loss record. If a block was found using an heuristic, the block size
1922 is followed by the heuristic.
philippea22f59d2012-01-26 23:13:52 +00001923 </para>
1924
1925 <para> If a directly lost block causes some other blocks to be indirectly
1926 lost, the block_list command will also show these indirectly lost blocks.
1927 The indirectly lost blocks will be indented according to the level of indirection
1928 between the directly lost block and the indirectly lost block(s).
1929 Each indirectly lost block is followed by the reference of its loss record.
1930 </para>
1931
1932 <para> The block_list command can be used on the results of a leak search as long
1933 as no block has been freed after this leak search: as soon as the program frees
1934 a block, a new leak search is needed before block_list can be used again.
1935 </para>
1936
1937 <para>
1938 In the below example, the program leaks a tree structure by losing the pointer to
1939 the block A (top of the tree).
1940 So, the block A is directly lost, causing an indirect
1941 loss of blocks B to G. The first block_list command shows the loss record of A
1942 (a definitely lost block with address 0x4028028, size 16). The addresses and sizes
1943 of the indirectly lost blocks due to block A are shown below the block A.
1944 The second command shows the details of one of the indirect loss records output
1945 by the first command.
1946 </para>
1947<programlisting><![CDATA[
1948 A
1949 / \
1950 B C
1951 / \ / \
1952 D E F G
1953]]></programlisting>
1954
1955<programlisting><![CDATA[
1956(gdb) bt
1957#0 main () at leak-tree.c:69
1958(gdb) monitor leak_check full any
1959==19552== 112 (16 direct, 96 indirect) bytes in 1 blocks are definitely lost in loss record 7 of 7
1960==19552== at 0x40070B4: malloc (vg_replace_malloc.c:263)
1961==19552== by 0x80484D5: mk (leak-tree.c:28)
1962==19552== by 0x80484FC: f (leak-tree.c:41)
1963==19552== by 0x8048856: main (leak-tree.c:63)
1964==19552==
1965==19552== LEAK SUMMARY:
1966==19552== definitely lost: 16 bytes in 1 blocks
1967==19552== indirectly lost: 96 bytes in 6 blocks
1968==19552== possibly lost: 0 bytes in 0 blocks
1969==19552== still reachable: 0 bytes in 0 blocks
1970==19552== suppressed: 0 bytes in 0 blocks
1971==19552==
1972(gdb) monitor block_list 7
1973==19552== 112 (16 direct, 96 indirect) bytes in 1 blocks are definitely lost in loss record 7 of 7
1974==19552== at 0x40070B4: malloc (vg_replace_malloc.c:263)
1975==19552== by 0x80484D5: mk (leak-tree.c:28)
1976==19552== by 0x80484FC: f (leak-tree.c:41)
1977==19552== by 0x8048856: main (leak-tree.c:63)
1978==19552== 0x4028028[16]
1979==19552== 0x4028068[16] indirect loss record 1
1980==19552== 0x40280E8[16] indirect loss record 3
1981==19552== 0x4028128[16] indirect loss record 4
1982==19552== 0x40280A8[16] indirect loss record 2
1983==19552== 0x4028168[16] indirect loss record 5
1984==19552== 0x40281A8[16] indirect loss record 6
1985(gdb) mo b 2
1986==19552== 16 bytes in 1 blocks are indirectly lost in loss record 2 of 7
1987==19552== at 0x40070B4: malloc (vg_replace_malloc.c:263)
1988==19552== by 0x80484D5: mk (leak-tree.c:28)
1989==19552== by 0x8048519: f (leak-tree.c:43)
1990==19552== by 0x8048856: main (leak-tree.c:63)
1991==19552== 0x40280A8[16]
1992==19552== 0x4028168[16] indirect loss record 5
1993==19552== 0x40281A8[16] indirect loss record 6
1994(gdb)
1995
1996]]></programlisting>
1997
1998 </listitem>
1999
2000 <listitem>
2001 <para><varname>who_points_at &lt;addr&gt; [&lt;len&gt;]</varname>
2002 shows all the locations where a pointer to addr is found.
2003 If len is equal to 1, the command only shows the locations pointing
2004 exactly at addr (i.e. the "start pointers" to addr).
2005 If len is &gt; 1, "interior pointers" pointing at the len first bytes
2006 will also be shown.
2007 </para>
2008
2009 <para>The locations searched for are the same as the locations
2010 used in the leak search. So, <varname>who_points_at</varname> can a.o.
2011 be used to show why the leak search still can reach a block, or can
2012 search for dangling pointers to a freed block.
2013 Each location pointing at addr (or pointing inside addr if interior pointers
2014 are being searched for) will be described.
2015 </para>
2016
2017 <para>In the below example, the pointers to the 'tree block A' (see example
2018 in command <varname>block_list</varname>) is shown before the tree was leaked.
2019 The descriptions are detailed as the option <option>--read-var-info=yes</option>
2020 was given at Valgrind startup. The second call shows the pointers (start and interior
2021 pointers) to block G. The block G (0x40281A8) is reachable via block C (0x40280a8)
2022 and register ECX of tid 1 (tid is the Valgrind thread id).
2023 It is "interior reachable" via the register EBX.
2024 </para>
2025
2026<programlisting><![CDATA[
2027(gdb) monitor who_points_at 0x4028028
2028==20852== Searching for pointers to 0x4028028
2029==20852== *0x8049e20 points at 0x4028028
2030==20852== Location 0x8049e20 is 0 bytes inside global var "t"
2031==20852== declared at leak-tree.c:35
2032(gdb) monitor who_points_at 0x40281A8 16
2033==20852== Searching for pointers pointing in 16 bytes from 0x40281a8
2034==20852== *0x40280ac points at 0x40281a8
2035==20852== Address 0x40280ac is 4 bytes inside a block of size 16 alloc'd
2036==20852== at 0x40070B4: malloc (vg_replace_malloc.c:263)
2037==20852== by 0x80484D5: mk (leak-tree.c:28)
2038==20852== by 0x8048519: f (leak-tree.c:43)
2039==20852== by 0x8048856: main (leak-tree.c:63)
2040==20852== tid 1 register ECX points at 0x40281a8
2041==20852== tid 1 register EBX interior points at 2 bytes inside 0x40281a8
2042(gdb)
2043]]></programlisting>
philippea22f59d2012-01-26 23:13:52 +00002044
philippeab1fce92013-09-29 13:47:32 +00002045 <para> When <varname>who_points_at</varname> finds an interior pointer,
2046 it will report the heuristic(s) with which this interior pointer
2047 will be considered as reachable. Note that this is done independently
2048 of the value of the option <option>--leak-check-heuristics</option>.
2049 In the below example, the loss record 6 indicates a possibly lost
2050 block. <varname>who_points_at</varname> reports that there is an interior
2051 pointer pointing in this block, and that the block can be considered
2052 reachable using the heuristic
2053 <computeroutput>multipleinheritance</computeroutput>.
2054 </para>
2055
2056<programlisting><![CDATA[
2057(gdb) monitor block_list 6
2058==3748== 8 bytes in 1 blocks are possibly lost in loss record 6 of 7
2059==3748== at 0x4007D77: operator new(unsigned int) (vg_replace_malloc.c:313)
2060==3748== by 0x8048954: main (leak_cpp_interior.cpp:43)
2061==3748== 0x402A0E0[8]
2062(gdb) monitor who_points_at 0x402A0E0 8
2063==3748== Searching for pointers pointing in 8 bytes from 0x402a0e0
2064==3748== *0xbe8ee078 interior points at 4 bytes inside 0x402a0e0
2065==3748== Address 0xbe8ee078 is on thread 1's stack
2066==3748== block at 0x402a0e0 considered reachable by ptr 0x402a0e4 using multipleinheritance heuristic
2067(gdb)
2068]]></programlisting>
2069
2070 </listitem>
philippea22f59d2012-01-26 23:13:52 +00002071
sewardj3b290482011-05-06 21:02:55 +00002072</itemizedlist>
2073
2074</sect1>
njn3e986b22004-11-30 10:43:45 +00002075
njn3e986b22004-11-30 10:43:45 +00002076<sect1 id="mc-manual.clientreqs" xreflabel="Client requests">
2077<title>Client Requests</title>
2078
2079<para>The following client requests are defined in
njn1d0825f2006-03-27 11:37:07 +00002080<filename>memcheck.h</filename>.
njn3e986b22004-11-30 10:43:45 +00002081See <filename>memcheck.h</filename> for exact details of their
2082arguments.</para>
2083
2084<itemizedlist>
2085
2086 <listitem>
njndbf7ca72006-03-31 11:57:59 +00002087 <para><varname>VALGRIND_MAKE_MEM_NOACCESS</varname>,
2088 <varname>VALGRIND_MAKE_MEM_UNDEFINED</varname> and
2089 <varname>VALGRIND_MAKE_MEM_DEFINED</varname>.
njn3e986b22004-11-30 10:43:45 +00002090 These mark address ranges as completely inaccessible,
2091 accessible but containing undefined data, and accessible and
florian99d91782014-06-23 21:23:44 +00002092 containing defined data, respectively. They return -1, when
2093 run on Valgrind and 0 otherwise.</para>
njn3e986b22004-11-30 10:43:45 +00002094 </listitem>
2095
2096 <listitem>
njndbf7ca72006-03-31 11:57:59 +00002097 <para><varname>VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE</varname>.
2098 This is just like <varname>VALGRIND_MAKE_MEM_DEFINED</varname> but only
2099 affects those bytes that are already addressable.</para>
2100 </listitem>
2101
2102 <listitem>
njndbf7ca72006-03-31 11:57:59 +00002103 <para><varname>VALGRIND_CHECK_MEM_IS_ADDRESSABLE</varname> and
2104 <varname>VALGRIND_CHECK_MEM_IS_DEFINED</varname>: check immediately
de03e0e7c2005-12-03 23:02:33 +00002105 whether or not the given address range has the relevant property,
2106 and if not, print an error message. Also, for the convenience of
2107 the client, returns zero if the relevant property holds; otherwise,
2108 the returned value is the address of the first byte for which the
2109 property is not true. Always returns 0 when not run on
2110 Valgrind.</para>
njn3e986b22004-11-30 10:43:45 +00002111 </listitem>
2112
2113 <listitem>
njndbf7ca72006-03-31 11:57:59 +00002114 <para><varname>VALGRIND_CHECK_VALUE_IS_DEFINED</varname>: a quick and easy
2115 way to find out whether Valgrind thinks a particular value
2116 (lvalue, to be precise) is addressable and defined. Prints an error
njn8225cc02009-03-09 22:52:24 +00002117 message if not. It has no return value.</para>
njn3e986b22004-11-30 10:43:45 +00002118 </listitem>
2119
2120 <listitem>
njn8225cc02009-03-09 22:52:24 +00002121 <para><varname>VALGRIND_DO_LEAK_CHECK</varname>: does a full memory leak
njn2f7eebe2009-08-05 06:34:27 +00002122 check (like <option>--leak-check=full</option>) right now.
njn8225cc02009-03-09 22:52:24 +00002123 This is useful for incrementally checking for leaks between arbitrary
2124 places in the program's execution. It has no return value.</para>
2125 </listitem>
2126
2127 <listitem>
sewardjc8bd1df2011-06-26 12:41:33 +00002128 <para><varname>VALGRIND_DO_ADDED_LEAK_CHECK</varname>: same as
2129 <varname> VALGRIND_DO_LEAK_CHECK</varname> but only shows the
2130 entries for which there was an increase in leaked bytes or leaked
2131 number of blocks since the previous leak search. It has no return
2132 value.</para>
2133 </listitem>
2134
2135 <listitem>
2136 <para><varname>VALGRIND_DO_CHANGED_LEAK_CHECK</varname>: same as
2137 <varname>VALGRIND_DO_LEAK_CHECK</varname> but only shows the
2138 entries for which there was an increase or decrease in leaked
2139 bytes or leaked number of blocks since the previous leak search. It
2140 has no return value.</para>
2141 </listitem>
2142
2143 <listitem>
njn8225cc02009-03-09 22:52:24 +00002144 <para><varname>VALGRIND_DO_QUICK_LEAK_CHECK</varname>: like
2145 <varname>VALGRIND_DO_LEAK_CHECK</varname>, except it produces only a leak
njn7e5d4ed2009-07-30 02:57:52 +00002146 summary (like <option>--leak-check=summary</option>).
njn8225cc02009-03-09 22:52:24 +00002147 It has no return value.</para>
njn3e986b22004-11-30 10:43:45 +00002148 </listitem>
2149
2150 <listitem>
de03e0e7c2005-12-03 23:02:33 +00002151 <para><varname>VALGRIND_COUNT_LEAKS</varname>: fills in the four
2152 arguments with the number of bytes of memory found by the previous
njn8225cc02009-03-09 22:52:24 +00002153 leak check to be leaked (i.e. the sum of direct leaks and indirect leaks),
njn2f7eebe2009-08-05 06:34:27 +00002154 dubious, reachable and suppressed. This is useful in test harness code,
njn8225cc02009-03-09 22:52:24 +00002155 after calling <varname>VALGRIND_DO_LEAK_CHECK</varname> or
2156 <varname>VALGRIND_DO_QUICK_LEAK_CHECK</varname>.</para>
njn3e986b22004-11-30 10:43:45 +00002157 </listitem>
2158
2159 <listitem>
njn8df80b22009-03-02 05:11:06 +00002160 <para><varname>VALGRIND_COUNT_LEAK_BLOCKS</varname>: identical to
2161 <varname>VALGRIND_COUNT_LEAKS</varname> except that it returns the
2162 number of blocks rather than the number of bytes in each
2163 category.</para>
2164 </listitem>
2165
2166 <listitem>
de03e0e7c2005-12-03 23:02:33 +00002167 <para><varname>VALGRIND_GET_VBITS</varname> and
2168 <varname>VALGRIND_SET_VBITS</varname>: allow you to get and set the
2169 V (validity) bits for an address range. You should probably only
2170 set V bits that you have got with
2171 <varname>VALGRIND_GET_VBITS</varname>. Only for those who really
njn1d0825f2006-03-27 11:37:07 +00002172 know what they are doing.</para>
njn3e986b22004-11-30 10:43:45 +00002173 </listitem>
2174
sewardje7decf82011-01-22 11:21:58 +00002175 <listitem>
2176 <para><varname>VALGRIND_CREATE_BLOCK</varname> and
2177 <varname>VALGRIND_DISCARD</varname>. <varname>VALGRIND_CREATE_BLOCK</varname>
2178 takes an address, a number of bytes and a character string. The
2179 specified address range is then associated with that string. When
2180 Memcheck reports an invalid access to an address in the range, it
2181 will describe it in terms of this block rather than in terms of
2182 any other block it knows about. Note that the use of this macro
2183 does not actually change the state of memory in any way -- it
2184 merely gives a name for the range.
2185 </para>
2186
2187 <para>At some point you may want Memcheck to stop reporting errors
2188 in terms of the block named
2189 by <varname>VALGRIND_CREATE_BLOCK</varname>. To make this
2190 possible, <varname>VALGRIND_CREATE_BLOCK</varname> returns a
2191 "block handle", which is a C <varname>int</varname> value. You
2192 can pass this block handle to <varname>VALGRIND_DISCARD</varname>.
2193 After doing so, Valgrind will no longer relate addressing errors
2194 in the specified range to the block. Passing invalid handles to
2195 <varname>VALGRIND_DISCARD</varname> is harmless.
2196 </para>
2197 </listitem>
2198
njn3e986b22004-11-30 10:43:45 +00002199</itemizedlist>
2200
2201</sect1>
sewardjce10c262006-10-05 17:56:14 +00002202
2203
2204
2205
njn09f2e6c2009-08-10 04:07:54 +00002206<sect1 id="mc-manual.mempools" xreflabel="Memory Pools">
sewardjce10c262006-10-05 17:56:14 +00002207<title>Memory Pools: describing and working with custom allocators</title>
2208
2209<para>Some programs use custom memory allocators, often for performance
njna3311642009-08-10 01:29:14 +00002210reasons. Left to itself, Memcheck is unable to understand the
2211behaviour of custom allocation schemes as well as it understands the
2212standard allocators, and so may miss errors and leaks in your program. What
2213this section describes is a way to give Memcheck enough of a description of
2214your custom allocator that it can make at least some sense of what is
2215happening.</para>
sewardjae0e07b2006-10-06 11:47:01 +00002216
2217<para>There are many different sorts of custom allocator, so Memcheck
sewardjce10c262006-10-05 17:56:14 +00002218attempts to reason about them using a loose, abstract model. We
2219use the following terminology when describing custom allocation
2220systems:</para>
2221
2222<itemizedlist>
2223 <listitem>
2224 <para>Custom allocation involves a set of independent "memory pools".
2225 </para>
2226 </listitem>
2227 <listitem>
2228 <para>Memcheck's notion of a a memory pool consists of a single "anchor
2229 address" and a set of non-overlapping "chunks" associated with the
2230 anchor address.</para>
2231 </listitem>
2232 <listitem>
2233 <para>Typically a pool's anchor address is the address of a
2234 book-keeping "header" structure.</para>
2235 </listitem>
2236 <listitem>
2237 <para>Typically the pool's chunks are drawn from a contiguous
bartaf25f672009-06-26 19:03:53 +00002238 "superblock" acquired through the system
njn2f7eebe2009-08-05 06:34:27 +00002239 <function>malloc</function> or
2240 <function>mmap</function>.</para>
sewardjce10c262006-10-05 17:56:14 +00002241 </listitem>
2242
2243</itemizedlist>
2244
2245<para>Keep in mind that the last two points above say "typically": the
2246Valgrind mempool client request API is intentionally vague about the
2247exact structure of a mempool. There is no specific mention made of
2248headers or superblocks. Nevertheless, the following picture may help
2249elucidate the intention of the terms in the API:</para>
2250
2251<programlisting><![CDATA[
2252 "pool"
2253 (anchor address)
2254 |
2255 v
2256 +--------+---+
2257 | header | o |
2258 +--------+-|-+
2259 |
2260 v superblock
2261 +------+---+--------------+---+------------------+
2262 | |rzB| allocation |rzB| |
2263 +------+---+--------------+---+------------------+
2264 ^ ^
2265 | |
2266 "addr" "addr"+"size"
2267]]></programlisting>
2268
2269<para>
2270Note that the header and the superblock may be contiguous or
2271discontiguous, and there may be multiple superblocks associated with a
2272single header; such variations are opaque to Memcheck. The API
2273only requires that your allocation scheme can present sensible values
2274of "pool", "addr" and "size".</para>
2275
2276<para>
2277Typically, before making client requests related to mempools, a client
2278program will have allocated such a header and superblock for their
2279mempool, and marked the superblock NOACCESS using the
2280<varname>VALGRIND_MAKE_MEM_NOACCESS</varname> client request.</para>
2281
2282<para>
2283When dealing with mempools, the goal is to maintain a particular
2284invariant condition: that Memcheck believes the unallocated portions
2285of the pool's superblock (including redzones) are NOACCESS. To
2286maintain this invariant, the client program must ensure that the
2287superblock starts out in that state; Memcheck cannot make it so, since
2288Memcheck never explicitly learns about the superblock of a pool, only
2289the allocated chunks within the pool.</para>
2290
2291<para>
2292Once the header and superblock for a pool are established and properly
2293marked, there are a number of client requests programs can use to
2294inform Memcheck about changes to the state of a mempool:</para>
2295
2296<itemizedlist>
2297
2298 <listitem>
2299 <para>
2300 <varname>VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed)</varname>:
njna3311642009-08-10 01:29:14 +00002301 This request registers the address <varname>pool</varname> as the anchor
2302 address for a memory pool. It also provides a size
2303 <varname>rzB</varname>, specifying how large the redzones placed around
2304 chunks allocated from the pool should be. Finally, it provides an
2305 <varname>is_zeroed</varname> argument that specifies whether the pool's
2306 chunks are zeroed (more precisely: defined) when allocated.
sewardjce10c262006-10-05 17:56:14 +00002307 </para>
2308 <para>
2309 Upon completion of this request, no chunks are associated with the
2310 pool. The request simply tells Memcheck that the pool exists, so that
2311 subsequent calls can refer to it as a pool.
2312 </para>
2313 </listitem>
2314
2315 <listitem>
2316 <para><varname>VALGRIND_DESTROY_MEMPOOL(pool)</varname>:
2317 This request tells Memcheck that a pool is being torn down. Memcheck
2318 then removes all records of chunks associated with the pool, as well
2319 as its record of the pool's existence. While destroying its records of
2320 a mempool, Memcheck resets the redzones of any live chunks in the pool
2321 to NOACCESS.
2322 </para>
2323 </listitem>
2324
2325 <listitem>
2326 <para><varname>VALGRIND_MEMPOOL_ALLOC(pool, addr, size)</varname>:
njna3311642009-08-10 01:29:14 +00002327 This request informs Memcheck that a <varname>size</varname>-byte chunk
2328 has been allocated at <varname>addr</varname>, and associates the chunk with the
2329 specified
2330 <varname>pool</varname>. If the pool was created with nonzero
2331 <varname>rzB</varname> redzones, Memcheck will mark the
2332 <varname>rzB</varname> bytes before and after the chunk as NOACCESS. If
2333 the pool was created with the <varname>is_zeroed</varname> argument set,
2334 Memcheck will mark the chunk as DEFINED, otherwise Memcheck will mark
2335 the chunk as UNDEFINED.
sewardjce10c262006-10-05 17:56:14 +00002336 </para>
2337 </listitem>
2338
2339 <listitem>
2340 <para><varname>VALGRIND_MEMPOOL_FREE(pool, addr)</varname>:
njna3311642009-08-10 01:29:14 +00002341 This request informs Memcheck that the chunk at <varname>addr</varname>
2342 should no longer be considered allocated. Memcheck will mark the chunk
2343 associated with <varname>addr</varname> as NOACCESS, and delete its
2344 record of the chunk's existence.
sewardjce10c262006-10-05 17:56:14 +00002345 </para>
2346 </listitem>
2347
2348 <listitem>
2349 <para><varname>VALGRIND_MEMPOOL_TRIM(pool, addr, size)</varname>:
njna3311642009-08-10 01:29:14 +00002350 This request trims the chunks associated with <varname>pool</varname>.
2351 The request only operates on chunks associated with
2352 <varname>pool</varname>. Trimming is formally defined as:</para>
sewardjce10c262006-10-05 17:56:14 +00002353 <itemizedlist>
2354 <listitem>
njna3311642009-08-10 01:29:14 +00002355 <para> All chunks entirely inside the range
2356 <varname>addr..(addr+size-1)</varname> are preserved.</para>
sewardjce10c262006-10-05 17:56:14 +00002357 </listitem>
2358 <listitem>
njna3311642009-08-10 01:29:14 +00002359 <para>All chunks entirely outside the range
2360 <varname>addr..(addr+size-1)</varname> are discarded, as though
2361 <varname>VALGRIND_MEMPOOL_FREE</varname> was called on them. </para>
sewardjce10c262006-10-05 17:56:14 +00002362 </listitem>
2363 <listitem>
2364 <para>All other chunks must intersect with the range
njna3311642009-08-10 01:29:14 +00002365 <varname>addr..(addr+size-1)</varname>; areas outside the
2366 intersection are marked as NOACCESS, as though they had been
2367 independently freed with
sewardjce10c262006-10-05 17:56:14 +00002368 <varname>VALGRIND_MEMPOOL_FREE</varname>.</para>
2369 </listitem>
2370 </itemizedlist>
2371 <para>This is a somewhat rare request, but can be useful in
2372 implementing the type of mass-free operations common in custom
2373 LIFO allocators.</para>
2374 </listitem>
2375
2376 <listitem>
bartaf25f672009-06-26 19:03:53 +00002377 <para><varname>VALGRIND_MOVE_MEMPOOL(poolA, poolB)</varname>: This
2378 request informs Memcheck that the pool previously anchored at
njna3311642009-08-10 01:29:14 +00002379 address <varname>poolA</varname> has moved to anchor address
2380 <varname>poolB</varname>. This is a rare request, typically only needed
2381 if you <function>realloc</function> the header of a mempool.</para>
sewardjce10c262006-10-05 17:56:14 +00002382 <para>No memory-status bits are altered by this request.</para>
2383 </listitem>
2384
2385 <listitem>
2386 <para>
bartaf25f672009-06-26 19:03:53 +00002387 <varname>VALGRIND_MEMPOOL_CHANGE(pool, addrA, addrB,
2388 size)</varname>: This request informs Memcheck that the chunk
njna3311642009-08-10 01:29:14 +00002389 previously allocated at address <varname>addrA</varname> within
2390 <varname>pool</varname> has been moved and/or resized, and should be
2391 changed to cover the region <varname>addrB..(addrB+size-1)</varname>. This
2392 is a rare request, typically only needed if you
2393 <function>realloc</function> a superblock or wish to extend a chunk
2394 without changing its memory-status bits.
sewardjce10c262006-10-05 17:56:14 +00002395 </para>
2396 <para>No memory-status bits are altered by this request.
2397 </para>
2398 </listitem>
2399
2400 <listitem>
2401 <para><varname>VALGRIND_MEMPOOL_EXISTS(pool)</varname>:
2402 This request informs the caller whether or not Memcheck is currently
njna3311642009-08-10 01:29:14 +00002403 tracking a mempool at anchor address <varname>pool</varname>. It
2404 evaluates to 1 when there is a mempool associated with that address, 0
2405 otherwise. This is a rare request, only useful in circumstances when
2406 client code might have lost track of the set of active mempools.
sewardjce10c262006-10-05 17:56:14 +00002407 </para>
2408 </listitem>
2409
2410</itemizedlist>
2411
sewardj778d7832007-11-22 01:21:56 +00002412</sect1>
2413
2414
2415
2416
2417
2418
2419
2420<sect1 id="mc-manual.mpiwrap" xreflabel="MPI Wrappers">
2421<title>Debugging MPI Parallel Programs with Valgrind</title>
2422
njn2f7eebe2009-08-05 06:34:27 +00002423<para>Memcheck supports debugging of distributed-memory applications
sewardj778d7832007-11-22 01:21:56 +00002424which use the MPI message passing standard. This support consists of a
2425library of wrapper functions for the
2426<computeroutput>PMPI_*</computeroutput> interface. When incorporated
2427into the application's address space, either by direct linking or by
2428<computeroutput>LD_PRELOAD</computeroutput>, the wrappers intercept
2429calls to <computeroutput>PMPI_Send</computeroutput>,
2430<computeroutput>PMPI_Recv</computeroutput>, etc. They then
njn2f7eebe2009-08-05 06:34:27 +00002431use client requests to inform Memcheck of memory state changes caused
sewardj778d7832007-11-22 01:21:56 +00002432by the function being wrapped. This reduces the number of false
2433positives that Memcheck otherwise typically reports for MPI
2434applications.</para>
2435
2436<para>The wrappers also take the opportunity to carefully check
2437size and definedness of buffers passed as arguments to MPI functions, hence
2438detecting errors such as passing undefined data to
2439<computeroutput>PMPI_Send</computeroutput>, or receiving data into a
2440buffer which is too small.</para>
2441
2442<para>Unlike most of the rest of Valgrind, the wrapper library is subject to a
2443BSD-style license, so you can link it into any code base you like.
njna437a602009-08-04 05:24:46 +00002444See the top of <computeroutput>mpi/libmpiwrap.c</computeroutput>
sewardj778d7832007-11-22 01:21:56 +00002445for license details.</para>
2446
2447
2448<sect2 id="mc-manual.mpiwrap.build" xreflabel="Building MPI Wrappers">
2449<title>Building and installing the wrappers</title>
2450
2451<para> The wrapper library will be built automatically if possible.
2452Valgrind's configure script will look for a suitable
2453<computeroutput>mpicc</computeroutput> to build it with. This must be
2454the same <computeroutput>mpicc</computeroutput> you use to build the
2455MPI application you want to debug. By default, Valgrind tries
2456<computeroutput>mpicc</computeroutput>, but you can specify a
njna3311642009-08-10 01:29:14 +00002457different one by using the configure-time option
njn7316df22009-08-04 01:16:01 +00002458<option>--with-mpicc</option>. Currently the
sewardj778d7832007-11-22 01:21:56 +00002459wrappers are only buildable with
2460<computeroutput>mpicc</computeroutput>s which are based on GNU
njn7316df22009-08-04 01:16:01 +00002461GCC or Intel's C++ Compiler.</para>
sewardj778d7832007-11-22 01:21:56 +00002462
2463<para>Check that the configure script prints a line like this:</para>
2464
2465<programlisting><![CDATA[
2466checking for usable MPI2-compliant mpicc and mpi.h... yes, mpicc
2467]]></programlisting>
2468
2469<para>If it says <computeroutput>... no</computeroutput>, your
2470<computeroutput>mpicc</computeroutput> has failed to compile and link
2471a test MPI2 program.</para>
2472
2473<para>If the configure test succeeds, continue in the usual way with
2474<computeroutput>make</computeroutput> and <computeroutput>make
2475install</computeroutput>. The final install tree should then contain
njn2f7eebe2009-08-05 06:34:27 +00002476<computeroutput>libmpiwrap-&lt;platform&gt;.so</computeroutput>.
sewardj778d7832007-11-22 01:21:56 +00002477</para>
2478
2479<para>Compile up a test MPI program (eg, MPI hello-world) and try
2480this:</para>
2481
2482<programlisting><![CDATA[
njn6bf365c2009-02-11 00:35:45 +00002483LD_PRELOAD=$prefix/lib/valgrind/libmpiwrap-<platform>.so \
sewardj778d7832007-11-22 01:21:56 +00002484 mpirun [args] $prefix/bin/valgrind ./hello
2485]]></programlisting>
2486
2487<para>You should see something similar to the following</para>
2488
2489<programlisting><![CDATA[
2490valgrind MPI wrappers 31901: Active for pid 31901
2491valgrind MPI wrappers 31901: Try MPIWRAP_DEBUG=help for possible options
2492]]></programlisting>
2493
2494<para>repeated for every process in the group. If you do not see
2495these, there is an build/installation problem of some kind.</para>
2496
2497<para> The MPI functions to be wrapped are assumed to be in an ELF
2498shared object with soname matching
2499<computeroutput>libmpi.so*</computeroutput>. This is known to be
2500correct at least for Open MPI and Quadrics MPI, and can easily be
2501changed if required.</para>
2502</sect2>
2503
2504
2505<sect2 id="mc-manual.mpiwrap.gettingstarted"
2506 xreflabel="Getting started with MPI Wrappers">
2507<title>Getting started</title>
2508
2509<para>Compile your MPI application as usual, taking care to link it
2510using the same <computeroutput>mpicc</computeroutput> that your
2511Valgrind build was configured with.</para>
2512
2513<para>
2514Use the following basic scheme to run your application on Valgrind with
2515the wrappers engaged:</para>
2516
2517<programlisting><![CDATA[
2518MPIWRAP_DEBUG=[wrapper-args] \
njn6bf365c2009-02-11 00:35:45 +00002519 LD_PRELOAD=$prefix/lib/valgrind/libmpiwrap-<platform>.so \
sewardj778d7832007-11-22 01:21:56 +00002520 mpirun [mpirun-args] \
2521 $prefix/bin/valgrind [valgrind-args] \
2522 [application] [app-args]
2523]]></programlisting>
2524
2525<para>As an alternative to
2526<computeroutput>LD_PRELOAD</computeroutput>ing
njn6bf365c2009-02-11 00:35:45 +00002527<computeroutput>libmpiwrap-&lt;platform&gt;.so</computeroutput>, you can
2528simply link it to your application if desired. This should not disturb
2529native behaviour of your application in any way.</para>
sewardj778d7832007-11-22 01:21:56 +00002530</sect2>
2531
2532
2533<sect2 id="mc-manual.mpiwrap.controlling"
2534 xreflabel="Controlling the MPI Wrappers">
2535<title>Controlling the wrapper library</title>
2536
2537<para>Environment variable
2538<computeroutput>MPIWRAP_DEBUG</computeroutput> is consulted at
2539startup. The default behaviour is to print a starting banner</para>
2540
2541<programlisting><![CDATA[
2542valgrind MPI wrappers 16386: Active for pid 16386
2543valgrind MPI wrappers 16386: Try MPIWRAP_DEBUG=help for possible options
2544]]></programlisting>
2545
2546<para> and then be relatively quiet.</para>
2547
2548<para>You can give a list of comma-separated options in
2549<computeroutput>MPIWRAP_DEBUG</computeroutput>. These are</para>
2550
2551<itemizedlist>
2552 <listitem>
2553 <para><computeroutput>verbose</computeroutput>:
2554 show entries/exits of all wrappers. Also show extra
2555 debugging info, such as the status of outstanding
2556 <computeroutput>MPI_Request</computeroutput>s resulting
2557 from uncompleted <computeroutput>MPI_Irecv</computeroutput>s.</para>
2558 </listitem>
2559 <listitem>
2560 <para><computeroutput>quiet</computeroutput>:
2561 opposite of <computeroutput>verbose</computeroutput>, only print
2562 anything when the wrappers want
2563 to report a detected programming error, or in case of catastrophic
2564 failure of the wrappers.</para>
2565 </listitem>
2566 <listitem>
2567 <para><computeroutput>warn</computeroutput>:
2568 by default, functions which lack proper wrappers
2569 are not commented on, just silently
2570 ignored. This causes a warning to be printed for each unwrapped
2571 function used, up to a maximum of three warnings per function.</para>
2572 </listitem>
2573 <listitem>
2574 <para><computeroutput>strict</computeroutput>:
2575 print an error message and abort the program if
2576 a function lacking a wrapper is used.</para>
2577 </listitem>
2578</itemizedlist>
2579
2580<para> If you want to use Valgrind's XML output facility
njn7e5d4ed2009-07-30 02:57:52 +00002581(<option>--xml=yes</option>), you should pass
sewardj778d7832007-11-22 01:21:56 +00002582<computeroutput>quiet</computeroutput> in
2583<computeroutput>MPIWRAP_DEBUG</computeroutput> so as to get rid of any
2584extraneous printing from the wrappers.</para>
2585
2586</sect2>
2587
2588
njn2f7eebe2009-08-05 06:34:27 +00002589<sect2 id="mc-manual.mpiwrap.limitations.functions"
2590 xreflabel="Functions: Abilities and Limitations">
sewardj778d7832007-11-22 01:21:56 +00002591<title>Functions</title>
2592
2593<para>All MPI2 functions except
2594<computeroutput>MPI_Wtick</computeroutput>,
2595<computeroutput>MPI_Wtime</computeroutput> and
2596<computeroutput>MPI_Pcontrol</computeroutput> have wrappers. The
2597first two are not wrapped because they return a
njn2f7eebe2009-08-05 06:34:27 +00002598<computeroutput>double</computeroutput>, which Valgrind's
2599function-wrap mechanism cannot handle (but it could easily be
2600extended to do so). <computeroutput>MPI_Pcontrol</computeroutput> cannot be
sewardj778d7832007-11-22 01:21:56 +00002601wrapped as it has variable arity:
2602<computeroutput>int MPI_Pcontrol(const int level, ...)</computeroutput></para>
2603
2604<para>Most functions are wrapped with a default wrapper which does
2605nothing except complain or abort if it is called, depending on
2606settings in <computeroutput>MPIWRAP_DEBUG</computeroutput> listed
2607above. The following functions have "real", do-something-useful
2608wrappers:</para>
2609
2610<programlisting><![CDATA[
2611PMPI_Send PMPI_Bsend PMPI_Ssend PMPI_Rsend
2612
2613PMPI_Recv PMPI_Get_count
2614
2615PMPI_Isend PMPI_Ibsend PMPI_Issend PMPI_Irsend
2616
2617PMPI_Irecv
2618PMPI_Wait PMPI_Waitall
2619PMPI_Test PMPI_Testall
2620
2621PMPI_Iprobe PMPI_Probe
2622
2623PMPI_Cancel
2624
2625PMPI_Sendrecv
2626
2627PMPI_Type_commit PMPI_Type_free
2628
2629PMPI_Pack PMPI_Unpack
2630
2631PMPI_Bcast PMPI_Gather PMPI_Scatter PMPI_Alltoall
2632PMPI_Reduce PMPI_Allreduce PMPI_Op_create
2633
2634PMPI_Comm_create PMPI_Comm_dup PMPI_Comm_free PMPI_Comm_rank PMPI_Comm_size
2635
2636PMPI_Error_string
2637PMPI_Init PMPI_Initialized PMPI_Finalize
2638]]></programlisting>
2639
2640<para> A few functions such as
2641<computeroutput>PMPI_Address</computeroutput> are listed as
2642<computeroutput>HAS_NO_WRAPPER</computeroutput>. They have no wrapper
2643at all as there is nothing worth checking, and giving a no-op wrapper
2644would reduce performance for no reason.</para>
2645
2646<para> Note that the wrapper library itself can itself generate large
2647numbers of calls to the MPI implementation, especially when walking
2648complex types. The most common functions called are
2649<computeroutput>PMPI_Extent</computeroutput>,
2650<computeroutput>PMPI_Type_get_envelope</computeroutput>,
2651<computeroutput>PMPI_Type_get_contents</computeroutput>, and
2652<computeroutput>PMPI_Type_free</computeroutput>. </para>
njn2f7eebe2009-08-05 06:34:27 +00002653</sect2>
sewardj778d7832007-11-22 01:21:56 +00002654
njn2f7eebe2009-08-05 06:34:27 +00002655<sect2 id="mc-manual.mpiwrap.limitations.types"
2656 xreflabel="Types: Abilities and Limitations">
sewardj778d7832007-11-22 01:21:56 +00002657<title>Types</title>
2658
2659<para> MPI-1.1 structured types are supported, and walked exactly.
2660The currently supported combiners are
2661<computeroutput>MPI_COMBINER_NAMED</computeroutput>,
2662<computeroutput>MPI_COMBINER_CONTIGUOUS</computeroutput>,
2663<computeroutput>MPI_COMBINER_VECTOR</computeroutput>,
2664<computeroutput>MPI_COMBINER_HVECTOR</computeroutput>
2665<computeroutput>MPI_COMBINER_INDEXED</computeroutput>,
2666<computeroutput>MPI_COMBINER_HINDEXED</computeroutput> and
2667<computeroutput>MPI_COMBINER_STRUCT</computeroutput>. This should
2668cover all MPI-1.1 types. The mechanism (function
2669<computeroutput>walk_type</computeroutput>) should extend easily to
2670cover MPI2 combiners.</para>
2671
2672<para>MPI defines some named structured types
2673(<computeroutput>MPI_FLOAT_INT</computeroutput>,
2674<computeroutput>MPI_DOUBLE_INT</computeroutput>,
2675<computeroutput>MPI_LONG_INT</computeroutput>,
2676<computeroutput>MPI_2INT</computeroutput>,
2677<computeroutput>MPI_SHORT_INT</computeroutput>,
2678<computeroutput>MPI_LONG_DOUBLE_INT</computeroutput>) which are pairs
2679of some basic type and a C <computeroutput>int</computeroutput>.
2680Unfortunately the MPI specification makes it impossible to look inside
2681these types and see where the fields are. Therefore these wrappers
2682assume the types are laid out as <computeroutput>struct { float val;
2683int loc; }</computeroutput> (for
2684<computeroutput>MPI_FLOAT_INT</computeroutput>), etc, and act
2685accordingly. This appears to be correct at least for Open MPI 1.0.2
2686and for Quadrics MPI.</para>
2687
2688<para>If <computeroutput>strict</computeroutput> is an option specified
2689in <computeroutput>MPIWRAP_DEBUG</computeroutput>, the application
2690will abort if an unhandled type is encountered. Otherwise, the
2691application will print a warning message and continue.</para>
2692
2693<para>Some effort is made to mark/check memory ranges corresponding to
2694arrays of values in a single pass. This is important for performance
2695since asking Valgrind to mark/check any range, no matter how small,
2696carries quite a large constant cost. This optimisation is applied to
2697arrays of primitive types (<computeroutput>double</computeroutput>,
2698<computeroutput>float</computeroutput>,
2699<computeroutput>int</computeroutput>,
2700<computeroutput>long</computeroutput>, <computeroutput>long
2701long</computeroutput>, <computeroutput>short</computeroutput>,
2702<computeroutput>char</computeroutput>, and <computeroutput>long
2703double</computeroutput> on platforms where <computeroutput>sizeof(long
2704double) == 8</computeroutput>). For arrays of all other types, the
2705wrappers handle each element individually and so there can be a very
2706large performance cost.</para>
2707
sewardj778d7832007-11-22 01:21:56 +00002708</sect2>
2709
2710
2711<sect2 id="mc-manual.mpiwrap.writingwrappers"
2712 xreflabel="Writing new MPI Wrappers">
2713<title>Writing new wrappers</title>
2714
2715<para>
2716For the most part the wrappers are straightforward. The only
2717significant complexity arises with nonblocking receives.</para>
2718
2719<para>The issue is that <computeroutput>MPI_Irecv</computeroutput>
2720states the recv buffer and returns immediately, giving a handle
2721(<computeroutput>MPI_Request</computeroutput>) for the transaction.
2722Later the user will have to poll for completion with
2723<computeroutput>MPI_Wait</computeroutput> etc, and when the
2724transaction completes successfully, the wrappers have to paint the
2725recv buffer. But the recv buffer details are not presented to
2726<computeroutput>MPI_Wait</computeroutput> -- only the handle is. The
2727library therefore maintains a shadow table which associates
2728uncompleted <computeroutput>MPI_Request</computeroutput>s with the
2729corresponding buffer address/count/type. When an operation completes,
2730the table is searched for the associated address/count/type info, and
2731memory is marked accordingly.</para>
2732
2733<para>Access to the table is guarded by a (POSIX pthreads) lock, so as
2734to make the library thread-safe.</para>
2735
2736<para>The table is allocated with
2737<computeroutput>malloc</computeroutput> and never
2738<computeroutput>free</computeroutput>d, so it will show up in leak
2739checks.</para>
2740
2741<para>Writing new wrappers should be fairly easy. The source file is
njna437a602009-08-04 05:24:46 +00002742<computeroutput>mpi/libmpiwrap.c</computeroutput>. If possible,
sewardj778d7832007-11-22 01:21:56 +00002743find an existing wrapper for a function of similar behaviour to the
2744one you want to wrap, and use it as a starting point. The wrappers
2745are organised in sections in the same order as the MPI 1.1 spec, to
2746aid navigation. When adding a wrapper, remember to comment out the
2747definition of the default wrapper in the long list of defaults at the
2748bottom of the file (do not remove it, just comment it out).</para>
2749</sect2>
2750
2751<sect2 id="mc-manual.mpiwrap.whattoexpect"
2752 xreflabel="What to expect with MPI Wrappers">
2753<title>What to expect when using the wrappers</title>
2754
2755<para>The wrappers should reduce Memcheck's false-error rate on MPI
2756applications. Because the wrapping is done at the MPI interface,
2757there will still potentially be a large number of errors reported in
2758the MPI implementation below the interface. The best you can do is
2759try to suppress them.</para>
2760
2761<para>You may also find that the input-side (buffer
2762length/definedness) checks find errors in your MPI use, for example
2763passing too short a buffer to
2764<computeroutput>MPI_Recv</computeroutput>.</para>
2765
2766<para>Functions which are not wrapped may increase the false
2767error rate. A possible approach is to run with
2768<computeroutput>MPI_DEBUG</computeroutput> containing
2769<computeroutput>warn</computeroutput>. This will show you functions
2770which lack proper wrappers but which are nevertheless used. You can
2771then write wrappers for them.
2772</para>
2773
2774<para>A known source of potential false errors are the
2775<computeroutput>PMPI_Reduce</computeroutput> family of functions, when
2776using a custom (user-defined) reduction function. In a reduction
2777operation, each node notionally sends data to a "central point" which
2778uses the specified reduction function to merge the data items into a
2779single item. Hence, in general, data is passed between nodes and fed
2780to the reduction function, but the wrapper library cannot mark the
2781transferred data as initialised before it is handed to the reduction
2782function, because all that happens "inside" the
2783<computeroutput>PMPI_Reduce</computeroutput> call. As a result you
2784may see false positives reported in your reduction function.</para>
2785
2786</sect2>
sewardjce10c262006-10-05 17:56:14 +00002787
2788</sect1>
sewardj778d7832007-11-22 01:21:56 +00002789
2790
2791
2792
2793
njn3e986b22004-11-30 10:43:45 +00002794</chapter>