1 <?xml version="1.0"?> <!-- -*- sgml -*- -->
2 <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"
4 [ <!ENTITY % vg-entities SYSTEM "../../docs/xml/vg-entities.xml"> %vg-entities; ]>
7 <chapter id="mc-manual" xreflabel="Memcheck: a memory error detector">
8 <title>Memcheck: a memory error detector</title>
10 <para>To use this tool, you may specify <option>--tool=memcheck</option>
11 on the Valgrind command line. You don't have to, though, since Memcheck
12 is the default tool.</para>
15 <sect1 id="mc-manual.overview" xreflabel="Overview">
16 <title>Overview</title>
18 <para>Memcheck is a memory error detector. It can detect the following
19 problems that are common in C and C++ programs.</para>
23 <para>Accessing memory you shouldn't, e.g. overrunning and underrunning
24 heap blocks, overrunning the top of the stack, and accessing memory after
25 it has been freed.</para>
29 <para>Using undefined values, i.e. values that have not been initialised,
30 or that have been derived from other undefined values.</para>
34 <para>Incorrect freeing of heap memory, such as double-freeing heap
35 blocks, or mismatched use of
36 <function>malloc</function>/<computeroutput>new</computeroutput>/<computeroutput>new[]</computeroutput>
38 <function>free</function>/<computeroutput>delete</computeroutput>/<computeroutput>delete[]</computeroutput></para>
39 <para>Mismatches will also be reported for <computeroutput>sized</computeroutput> and <computeroutput>aligned</computeroutput>
40 allocation and deallocation functions if the deallocation value
41 does not match the allocation value.</para>
45 <para>Overlapping <computeroutput>src</computeroutput> and
46 <computeroutput>dst</computeroutput> pointers in
47 <computeroutput>memcpy</computeroutput> and related
52 <para>Passing a fishy (presumably negative) value to the
53 <computeroutput>size</computeroutput> parameter of a memory
54 allocation function.</para>
58 <para>Using a <computeroutput>size</computeroutput> value of 0
63 <para>Using an <computeroutput>alignment</computeroutput> value that
64 is not a power of two.</para>
68 <para>Memory leaks.</para>
72 <para>Problems like these can be difficult to find by other means,
73 often remaining undetected for long periods, then causing occasional,
74 difficult-to-diagnose crashes.</para>
76 <para>Memcheck also provides <xref linkend="&vg-xtree-id;"/> memory
77 profiling using the command line
78 option <computeroutput>--xtree-memory</computeroutput> and the monitor command
79 <computeroutput>xtmemory</computeroutput>.</para>
84 <sect1 id="mc-manual.errormsgs"
85 xreflabel="Explanation of error messages from Memcheck">
86 <title>Explanation of error messages from Memcheck</title>
88 <para>Memcheck issues a range of error messages. This section presents a
89 quick summary of what error messages mean. The precise behaviour of the
90 error-checking machinery is described in <xref
91 linkend="mc-manual.machine"/>.</para>
94 <sect2 id="mc-manual.badrw"
95 xreflabel="Illegal read / Illegal write errors">
96 <title>Illegal read / Illegal write errors</title>
98 <para>For example:</para>
99 <programlisting><![CDATA[
100 Invalid read of size 4
101 at 0x40F6BBCC: (within /usr/lib/libpng.so.2.1.0.9)
102 by 0x40F6B804: (within /usr/lib/libpng.so.2.1.0.9)
103 by 0x40B07FF4: read_png_image(QImageIO *) (kernel/qpngio.cpp:326)
104 by 0x40AC751B: QImageIO::read() (kernel/qimage.cpp:3621)
105 Address 0xBFFFF0E0 is not stack'd, malloc'd or free'd
108 <para>This happens when your program reads or writes memory at a place
109 which Memcheck reckons it shouldn't. In this example, the program did a
110 4-byte read at address 0xBFFFF0E0, somewhere within the system-supplied
111 library libpng.so.2.1.0.9, which was called from somewhere else in the
112 same library, called from line 326 of <filename>qpngio.cpp</filename>,
115 <para>Memcheck tries to establish what the illegal address might relate
116 to, since that's often useful. So, if it points into a block of memory
117 which has already been freed, you'll be informed of this, and also where
118 the block was freed. Likewise, if it should turn out to be just off
119 the end of a heap block, a common result of off-by-one-errors in
120 array subscripting, you'll be informed of this fact, and also where the
121 block was allocated. If you use the <option><link
122 linkend="opt.read-var-info">--read-var-info</link></option> option
123 Memcheck will run more slowly
124 but may give a more detailed description of any illegal address.</para>
126 <para>In this example, Memcheck can't identify the address. Actually
127 the address is on the stack, but, for some reason, this is not a valid
128 stack address -- it is below the stack pointer and that isn't allowed.
129 In this particular case it's probably caused by GCC generating invalid
130 code, a known bug in some ancient versions of GCC.</para>
132 <para>Note that Memcheck only tells you that your program is about to
133 access memory at an illegal address. It can't stop the access from
134 happening. So, if your program makes an access which normally would
135 result in a segmentation fault, you program will still suffer the same
136 fate -- but you will get a message from Memcheck immediately prior to
137 this. In this particular example, reading junk on the stack is
138 non-fatal, and the program stays alive.</para>
144 <sect2 id="mc-manual.uninitvals"
145 xreflabel="Use of uninitialised values">
146 <title>Use of uninitialised values</title>
148 <para>For example:</para>
149 <programlisting><![CDATA[
150 Conditional jump or move depends on uninitialised value(s)
151 at 0x402DFA94: _IO_vfprintf (_itoa.h:49)
152 by 0x402E8476: _IO_printf (printf.c:36)
153 by 0x8048472: main (tests/manuel1.c:8)
156 <para>An uninitialised-value use error is reported when your program
157 uses a value which hasn't been initialised -- in other words, is
158 undefined. Here, the undefined value is used somewhere inside the
159 <function>printf</function> machinery of the C library. This error was
160 reported when running the following small program:</para>
161 <programlisting><![CDATA[
165 printf ("x = %d\n", x);
166 }]]></programlisting>
168 <para>It is important to understand that your program can copy around
169 junk (uninitialised) data as much as it likes. Memcheck observes this
170 and keeps track of the data, but does not complain. A complaint is
171 issued only when your program attempts to make use of uninitialised
172 data in a way that might affect your program's externally-visible behaviour.
173 In this example, <varname>x</varname> is uninitialised. Memcheck observes
174 the value being passed to <function>_IO_printf</function> and thence to
175 <function>_IO_vfprintf</function>, but makes no comment. However,
176 <function>_IO_vfprintf</function> has to examine the value of
177 <varname>x</varname> so it can turn it into the corresponding ASCII string,
178 and it is at this point that Memcheck complains.</para>
180 <para>Sources of uninitialised data tend to be:</para>
183 <para>Local variables in procedures which have not been initialised,
184 as in the example above.</para>
187 <para>The contents of heap blocks (allocated with
188 <function>malloc</function>, <function>new</function>, or a similar
189 function) before you (or a constructor) write something there.
194 <para>To see information on the sources of uninitialised data in your
195 program, use the <option>--track-origins=yes</option> option. This
196 makes Memcheck run more slowly, but can make it much easier to track down
197 the root causes of uninitialised value errors.</para>
203 <sect2 id="mc-manual.bad-syscall-args"
204 xreflabel="Use of uninitialised or unaddressable values in system
206 <title>Use of uninitialised or unaddressable values in system
209 <para>Memcheck checks all parameters to system calls:
212 <para>It checks all the direct parameters themselves, whether they are
216 <para>Also, if a system call needs to read from a buffer provided by
217 your program, Memcheck checks that the entire buffer is addressable
218 and its contents are initialised.</para>
221 <para>Also, if the system call needs to write to a user-supplied
222 buffer, Memcheck checks that the buffer is addressable.</para>
227 <para>After the system call, Memcheck updates its tracked information to
228 precisely reflect any changes in memory state caused by the system
231 <para>Here's an example of two system calls with invalid parameters:</para>
232 <programlisting><![CDATA[
237 char* arr = malloc(10);
238 int* arr2 = malloc(sizeof(int));
239 write( 1 /* stdout */, arr, 10 );
244 <para>You get these complaints ...</para>
245 <programlisting><![CDATA[
246 Syscall param write(buf) points to uninitialised byte(s)
247 at 0x25A48723: __write_nocancel (in /lib/tls/libc-2.3.3.so)
248 by 0x259AFAD3: __libc_start_main (in /lib/tls/libc-2.3.3.so)
249 by 0x8048348: (within /auto/homes/njn25/grind/head4/a.out)
250 Address 0x25AB8028 is 0 bytes inside a block of size 10 alloc'd
251 at 0x259852B0: malloc (vg_replace_malloc.c:130)
252 by 0x80483F1: main (a.c:5)
254 Syscall param exit(error_code) contains uninitialised byte(s)
255 at 0x25A21B44: __GI__exit (in /lib/tls/libc-2.3.3.so)
256 by 0x8048426: main (a.c:8)
259 <para>... because the program has (a) written uninitialised junk
260 from the heap block to the standard output, and (b) passed an
261 uninitialised value to <function>exit</function>. Note that the first
262 error refers to the memory pointed to by
263 <computeroutput>buf</computeroutput> (not
264 <computeroutput>buf</computeroutput> itself), but the second error
265 refers directly to <computeroutput>exit</computeroutput>'s argument
266 <computeroutput>arr2[0]</computeroutput>.</para>
271 <sect2 id="mc-manual.badfrees" xreflabel="Illegal frees">
272 <title>Illegal frees</title>
274 <para>For example:</para>
275 <programlisting><![CDATA[
277 at 0x4004FFDF: free (vg_clientmalloc.c:577)
278 by 0x80484C7: main (tests/doublefree.c:10)
279 Address 0x3807F7B4 is 0 bytes inside a block of size 177 free'd
280 at 0x4004FFDF: free (vg_clientmalloc.c:577)
281 by 0x80484C7: main (tests/doublefree.c:10)
284 <para>Memcheck keeps track of the blocks allocated by your program
285 with <function>malloc</function>/<computeroutput>new</computeroutput>,
286 so it can know exactly whether or not the argument to
287 <function>free</function>/<computeroutput>delete</computeroutput> is
288 legitimate or not. Here, this test program has freed the same block
289 twice. As with the illegal read/write errors, Memcheck attempts to
290 make sense of the address freed. If, as here, the address is one
291 which has previously been freed, you wil be told that -- making
292 duplicate frees of the same block easy to spot. You will also get this
293 message if you try to free a pointer that doesn't point to the start of a
299 <sect2 id="mc-manual.rudefn"
300 xreflabel="When a heap block is freed with an inappropriate deallocation
302 <title>When a heap block is freed with an inappropriate deallocation
305 <para>In the following example, a block allocated with
306 <function>new[]</function> has wrongly been deallocated with
307 <function>free</function>:</para>
308 <programlisting><![CDATA[
309 Mismatched free() / delete / delete []
310 at 0x40043249: free (vg_clientfuncs.c:171)
311 by 0x4102BB4E: QGArray::~QGArray(void) (tools/qgarray.cpp:149)
312 by 0x4C261C41: PptDoc::~PptDoc(void) (include/qmemarray.h:60)
313 by 0x4C261F0E: PptXml::~PptXml(void) (pptxml.cc:44)
314 Address 0x4BB292A8 is 0 bytes inside a block of size 64 alloc'd
315 at 0x4004318C: operator new[](unsigned int) (vg_clientfuncs.c:152)
316 by 0x4C21BC15: KLaola::readSBStream(int) const (klaola.cc:314)
317 by 0x4C21C155: KLaola::stream(KLaola::OLENode const *) (klaola.cc:416)
318 by 0x4C21788F: OLEFilter::convert(QCString const &) (olefilter.cc:272)
321 <para>In <literal>C++</literal> it's important to deallocate memory in a
322 way compatible with how it was allocated. The deal is:</para>
325 <para>If allocated with
326 <function>malloc</function>,
327 <function>calloc</function>,
328 <function>realloc</function>,
329 <function>valloc</function> or
330 <function>memalign</function>, you must
331 deallocate with <function>free</function>.</para>
334 <para>If allocated with <function>new</function>, you must deallocate
335 with <function>delete</function>.</para>
338 <para>If allocated with <function>new[]</function>, you must
339 deallocate with <function>delete[]</function>.</para>
343 <para>The worst thing is that on Linux apparently it doesn't matter if
344 you do mix these up, but the same program may then crash on a
345 different platform, Solaris for example. So it's best to fix it
346 properly. According to the KDE folks "it's amazing how many C++
347 programmers don't know this".</para>
349 <para>The reason behind the requirement is as follows. In some C++
350 implementations, <function>delete[]</function> must be used for
351 objects allocated by <function>new[]</function> because the compiler
352 stores the size of the array and the pointer-to-member to the
353 destructor of the array's content just before the pointer actually
354 returned. <function>delete</function> doesn't account for this and will get
355 confused, possibly corrupting the heap.</para>
361 <sect2 id="mc-manual.overlap"
362 xreflabel="Overlapping source and destination blocks">
363 <title>Overlapping source and destination blocks</title>
365 <para>The following C library functions copy some data from one
366 memory block to another (or something similar):
367 <function>memcpy</function>,
368 <function>strcpy</function>,
369 <function>strncpy</function>,
370 <function>strcat</function>,
371 <function>strncat</function>.
372 The blocks pointed to by their <computeroutput>src</computeroutput> and
373 <computeroutput>dst</computeroutput> pointers aren't allowed to overlap.
374 The POSIX standards have wording along the lines "If copying takes place
375 between objects that overlap, the behavior is undefined." Therefore,
376 Memcheck checks for this.
379 <para>For example:</para>
380 <programlisting><![CDATA[
381 ==27492== Source and destination overlap in memcpy(0xbffff294, 0xbffff280, 21)
382 ==27492== at 0x40026CDC: memcpy (mc_replace_strmem.c:71)
383 ==27492== by 0x804865A: main (overlap.c:40)
386 <para>You don't want the two blocks to overlap because one of them could
387 get partially overwritten by the copying.</para>
389 <para>You might think that Memcheck is being overly pedantic reporting
390 this in the case where <computeroutput>dst</computeroutput> is less than
391 <computeroutput>src</computeroutput>. For example, the obvious way to
392 implement <function>memcpy</function> is by copying from the first
393 byte to the last. However, the optimisation guides of some
394 architectures recommend copying from the last byte down to the first.
395 Also, some implementations of <function>memcpy</function> zero
396 <computeroutput>dst</computeroutput> before copying, because zeroing the
397 destination's cache line(s) can improve performance.</para>
399 <para>The moral of the story is: if you want to write truly portable
400 code, don't make any assumptions about the language
401 implementation.</para>
406 <sect2 id="mc-manual.fishyvalue"
407 xreflabel="Fishy argument values">
408 <title>Fishy argument values</title>
410 <para>All memory allocation functions take an argument specifying the
411 size of the memory block that should be allocated. Clearly, the requested
412 size should be a non-negative value and is typically not excessively large.
413 For instance, it is extremely unlikly that the size of an allocation
414 request exceeds 2**63 bytes on a 64-bit machine. It is much more likely that
415 such a value is the result of an erroneous size calculation and is in effect
416 a negative value (that just happens to appear excessively large because
417 the bit pattern is interpreted as an unsigned integer).
418 Such a value is called a "fishy value".
420 The <varname>size</varname> argument of the following allocation functions
421 is checked for being fishy:
422 <function>malloc</function>,
423 <function>calloc</function>,
424 <function>realloc</function>,
425 <function>memalign</function>,
426 <function>posix_memalign</function>,
427 <function>aligned_alloc</function>,
428 <function>new</function>,
429 <function>new []</function>.
430 <function>__builtin_new</function>,
431 <function>__builtin_vec_new</function>,
432 For <function>calloc</function> both arguments are checked.
435 <para>For example:</para>
436 <programlisting><![CDATA[
437 ==32233== Argument 'size' of function malloc has a fishy (possibly negative) value: -3
438 ==32233== at 0x4C2CFA7: malloc (vg_replace_malloc.c:298)
439 ==32233== by 0x400555: foo (fishy.c:15)
440 ==32233== by 0x400583: main (fishy.c:23)
443 <para>In earlier Valgrind versions those values were being referred to
444 as "silly arguments" and no back-trace was included.
449 <sect2 id="mc-manual.realocsizezero"
450 xreflabel="Realloc size zero">
451 <title>Realloc size zero</title>
453 <para>The (ab)use or realloc to also do the job of <function>free</function>
454 has been poorly understood for a long time. In the C17 standard
455 ISO/IEC 9899:2017] the behaviour of realloc when the size argument
456 is zero is specified as implementation defined. Memcheck warns about
457 the non-portable use or realloc.</para>
459 <para>For example:</para>
460 <programlisting><![CDATA[
461 ==77609== realloc() with size 0
462 ==77609== at 0x48502B8: realloc (vg_replace_malloc.c:1450)
463 ==77609== by 0x201989: main (realloczero.c:8)
464 ==77609== Address 0x5464040 is 0 bytes inside a block of size 4 alloc'd
465 ==77609== at 0x484CBB4: malloc (vg_replace_malloc.c:397)
466 ==77609== by 0x201978: main (realloczero.c:7)
471 <sect2 id="mc-manual.alignment"
472 xreflabel="Alignment (and Size) Errors">
473 <title>Alignment Errors</title>
475 <para>C and C++ have several functions that allow the user to obtain aligned memory.
476 Typically this is done for performance reasons so that the memory will be cache line
477 or memory page aligned. C has the functions <computeroutput>memalign</computeroutput>,
478 <computeroutput>posix_memalign</computeroutput> and <computeroutput>aligned_alloc</computeroutput>.
479 C++ has numerous overloads of <computeroutput>operator new</computeroutput> and <computeroutput>
480 operator delete</computeroutput>. Of these, posix_memalign is quite clearly
481 specified, the others vary quite widely between implementations. Valgrind will generate
482 errors for values of alignment that are invalid on any platform.</para>
484 <!-- would like to have a table here -->
485 <para><computeroutput>memalign</computeroutput> will produce errors if the alignment
486 is zero or not a multiple of two.</para>
488 <para><computeroutput>posix_memalign</computeroutput> will produce errors if the alignment
489 is less than sizeof(size_t), not a multiple of two or if the size is zero.</para>
491 <para><computeroutput>aligned_alloc</computeroutput> will produce errors if the alignment
492 is not a multiple of two , if the size is zero or if the size is not an integral
493 multiple of the alignment.</para>
495 <para><computeroutput>aligned new</computeroutput> will produce errors if the alignment
496 is zero or not a multiple of two. The <computeroutput>nothrow</computeroutput> overloads
497 will return a NULL pointer. The non-nothrow overloads will abort Valgrind.</para>
499 <para><computeroutput>aligned delete</computeroutput> will produce errors if the alignment
500 is zero or not a multiple of two or if the alignment is not the same as that used by
501 <computeroutput>aligned new</computeroutput>.</para>
503 <para><computeroutput>sized delete</computeroutput> will produce errors if the size
504 is not the same as that used by <computeroutput>new</computeroutput>.</para>
506 <para><computeroutput>sized aligned delete</computeroutput> combines the error conditions
507 of the individual sized and aligned delete operators.</para>
509 <para>Example output:</para>
510 <programlisting><![CDATA[
511 ==65825== Invalid alignment value: 3 (should be power of 2)
512 ==65825== at 0x485197E: memalign (vg_replace_malloc.c:1740)
513 ==65825== by 0x201CD2: main (memalign.c:39)
518 <sect2 id="mc-manual.leaks" xreflabel="Memory leak detection">
519 <title>Memory leak detection</title>
521 <para>Memcheck keeps track of all heap blocks issued in response to
523 <function>malloc</function>/<function>new</function> et al.
524 So when the program exits, it knows which blocks have not been freed.
527 <para>If <option>--leak-check</option> is set appropriately, for each
528 remaining block, Memcheck determines if the block is reachable from pointers
529 within the root-set. The root-set consists of (a) general purpose registers
530 of all threads, and (b) initialised, aligned, pointer-sized data words in
531 accessible client memory, including stacks.</para>
533 <para>There are two ways a block can be reached. The first is with a
534 "start-pointer", i.e. a pointer to the start of the block. The second is with
535 an "interior-pointer", i.e. a pointer to the middle of the block. There are
536 several ways we know of that an interior-pointer can occur:</para>
540 <para>The pointer might have originally been a start-pointer and have been
541 moved along deliberately (or not deliberately) by the program. In
542 particular, this can happen if your program uses tagged pointers, i.e.
543 if it uses the bottom one, two or three bits of a pointer, which are
544 normally always zero due to alignment, in order to store extra
549 <para>It might be a random junk value in memory, entirely unrelated, just
550 a coincidence.</para>
554 <para>It might be a pointer to the inner char array of a C++
555 <computeroutput>std::string</computeroutput>. For example, some
556 compilers add 3 words at the beginning of the std::string to
557 store the length, the capacity and a reference count before the
558 memory containing the array of characters. They return a pointer
559 just after these 3 words, pointing at the char array.</para>
563 <para>Some code might allocate a block of memory, and use the first 8
564 bytes to store (block size - 8) as a 64bit number.
565 <computeroutput>sqlite3MemMalloc</computeroutput> does this.</para>
569 <para>It might be a pointer to an array of C++ objects (which possess
570 destructors) allocated with <computeroutput>new[]</computeroutput>. In
571 this case, some compilers store a "magic cookie" containing the array
572 length at the start of the allocated block, and return a pointer to just
573 past that magic cookie, i.e. an interior-pointer.
574 See <ulink url="https://docs.freebsd.org/info/gxxint/gxxint.info.Free_Store.html">this
575 page</ulink> for more information.</para>
579 <para>It might be a pointer to an inner part of a C++ object using
580 multiple inheritance. </para>
584 <para>You can optionally activate heuristics to use during the leak
585 search to detect the interior pointers corresponding to
586 the <computeroutput>stdstring</computeroutput>,
587 <computeroutput>length64</computeroutput>,
588 <computeroutput>newarray</computeroutput>
589 and <computeroutput>multipleinheritance</computeroutput> cases. If the
590 heuristic detects that an interior pointer corresponds to such a case,
591 the block will be considered as reachable by the interior
592 pointer. In other words, the interior pointer will be treated
593 as if it were a start pointer.</para>
596 <para>With that in mind, consider the nine possible cases described by the
597 following figure.</para>
599 <programlisting><![CDATA[
600 Pointer chain AAA Leak Case BBB Leak Case
601 ------------- ------------- -------------
602 (1) RRR ------------> BBB DR
603 (2) RRR ---> AAA ---> BBB DR IR
605 (4) RRR AAA ---> BBB DL IL
606 (5) RRR ------?-----> BBB (y)DR, (n)DL
607 (6) RRR ---> AAA -?-> BBB DR (y)IR, (n)DL
608 (7) RRR -?-> AAA ---> BBB (y)DR, (n)DL (y)IR, (n)IL
609 (8) RRR -?-> AAA -?-> BBB (y)DR, (n)DL (y,y)IR, (n,y)IL, (_,n)DL
610 (9) RRR AAA -?-> BBB DL (y)IL, (n)DL
612 Pointer chain legend:
613 - RRR: a root set node or DR block
614 - AAA, BBB: heap blocks
615 - --->: a start-pointer
616 - -?->: an interior-pointer
619 - DR: Directly reachable
620 - IR: Indirectly reachable
622 - IL: Indirectly lost
623 - (y)XY: it's XY if the interior-pointer is a real pointer
624 - (n)XY: it's XY if the interior-pointer is not a real pointer
625 - (_)XY: it's XY in either case
628 <para>Every possible case can be reduced to one of the above nine. Memcheck
629 merges some of these cases in its output, resulting in the following four
636 <para>"Still reachable". This covers cases 1 and 2 (for the BBB blocks)
637 above. A start-pointer or chain of start-pointers to the block is
638 found. Since the block is still pointed at, the programmer could, at
639 least in principle, have freed it before program exit. "Still reachable"
640 blocks are very common and arguably not a problem. So, by default,
641 Memcheck won't report such blocks individually.</para>
645 <para>"Definitely lost". This covers case 3 (for the BBB blocks) above.
646 This means that no pointer to the block can be found. The block is
647 classified as "lost", because the programmer could not possibly have
648 freed it at program exit, since no pointer to it exists. This is likely
649 a symptom of having lost the pointer at some earlier point in the
650 program. Such cases should be fixed by the programmer.</para>
654 <para>"Indirectly lost". This covers cases 4 and 9 (for the BBB blocks)
655 above. This means that the block is lost, not because there are no
656 pointers to it, but rather because all the blocks that point to it are
657 themselves lost. For example, if you have a binary tree and the root
658 node is lost, all its children nodes will be indirectly lost. Because
659 the problem will disappear if the definitely lost block that caused the
660 indirect leak is fixed, Memcheck won't report such blocks individually
665 <para>"Possibly lost". This covers cases 5--8 (for the BBB blocks)
666 above. This means that a chain of one or more pointers to the block has
667 been found, but at least one of the pointers is an interior-pointer.
668 This could just be a random value in memory that happens to point into a
669 block, and so you shouldn't consider this ok unless you know you have
670 interior-pointers.</para>
675 <para>(Note: This mapping of the nine possible cases onto four leak kinds is
676 not necessarily the best way that leaks could be reported; in particular,
677 interior-pointers are treated inconsistently. It is possible the
678 categorisation may be improved in the future.)</para>
680 <para>Furthermore, if suppressions exists for a block, it will be reported
681 as "suppressed" no matter what which of the above four kinds it belongs
685 <para>The following is an example leak summary.</para>
687 <programlisting><![CDATA[
689 definitely lost: 48 bytes in 3 blocks.
690 indirectly lost: 32 bytes in 2 blocks.
691 possibly lost: 96 bytes in 6 blocks.
692 still reachable: 64 bytes in 4 blocks.
693 suppressed: 0 bytes in 0 blocks.
696 <para>If heuristics have been used to consider some blocks as
697 reachable, the leak summary details the heuristically reachable subset
698 of 'still reachable:' per heuristic. In the below example, of the 95
699 bytes still reachable, 87 bytes (56+7+8+16) have been considered
700 heuristically reachable.
703 <programlisting><![CDATA[
705 definitely lost: 4 bytes in 1 blocks
706 indirectly lost: 0 bytes in 0 blocks
707 possibly lost: 0 bytes in 0 blocks
708 still reachable: 95 bytes in 6 blocks
709 of which reachable via heuristic:
710 stdstring : 56 bytes in 2 blocks
711 length64 : 16 bytes in 1 blocks
712 newarray : 7 bytes in 1 blocks
713 multipleinheritance: 8 bytes in 1 blocks
714 suppressed: 0 bytes in 0 blocks
717 <para>If <option>--leak-check=full</option> is specified,
718 Memcheck will give details for each definitely lost or possibly lost block,
719 including where it was allocated. (Actually, it merges results for all
720 blocks that have the same leak kind and sufficiently similar stack traces
721 into a single "loss record". The
722 <option>--leak-resolution</option> lets you control the
723 meaning of "sufficiently similar".) It cannot tell you when or how or why
724 the pointer to a leaked block was lost; you have to work that out for
725 yourself. In general, you should attempt to ensure your programs do not
726 have any definitely lost or possibly lost blocks at exit.</para>
728 <para>For example:</para>
729 <programlisting><![CDATA[
730 8 bytes in 1 blocks are definitely lost in loss record 1 of 14
731 at 0x........: malloc (vg_replace_malloc.c:...)
732 by 0x........: mk (leak-tree.c:11)
733 by 0x........: main (leak-tree.c:39)
735 88 (8 direct, 80 indirect) bytes in 1 blocks are definitely lost in loss record 13 of 14
736 at 0x........: malloc (vg_replace_malloc.c:...)
737 by 0x........: mk (leak-tree.c:11)
738 by 0x........: main (leak-tree.c:25)
741 <para>The first message describes a simple case of a single 8 byte block
742 that has been definitely lost. The second case mentions another 8 byte
743 block that has been definitely lost; the difference is that a further 80
744 bytes in other blocks are indirectly lost because of this lost block.
745 The loss records are not presented in any notable order, so the loss record
746 numbers aren't particularly meaningful. The loss record numbers can be used
747 in the Valgrind gdbserver to list the addresses of the leaked blocks and/or give
748 more details about how a block is still reachable.</para>
750 <para>The option <option>--show-leak-kinds=<set></option>
751 controls the set of leak kinds to show
752 when <option>--leak-check=full</option> is specified. </para>
754 <para>The <option><set></option> of leak kinds is specified
755 in one of the following ways:
758 <listitem><para>a comma separated list of one or more of
759 <option>definite indirect possible reachable</option>.</para>
762 <listitem><para><option>all</option> to specify the complete set (all leak kinds).</para>
765 <listitem><para><option>none</option> for the empty set.</para>
771 <para> The default value for the leak kinds to show is
772 <option>--show-leak-kinds=definite,possible</option>.
775 <para>To also show the reachable and indirectly lost blocks in
776 addition to the definitely and possibly lost blocks, you can
777 use <option>--show-leak-kinds=all</option>. To only show the
778 reachable and indirectly lost blocks, use
779 <option>--show-leak-kinds=indirect,reachable</option>. The reachable
780 and indirectly lost blocks will then be presented as shown in
781 the following two examples.</para>
783 <programlisting><![CDATA[
784 64 bytes in 4 blocks are still reachable in loss record 2 of 4
785 at 0x........: malloc (vg_replace_malloc.c:177)
786 by 0x........: mk (leak-cases.c:52)
787 by 0x........: main (leak-cases.c:74)
789 32 bytes in 2 blocks are indirectly lost in loss record 1 of 4
790 at 0x........: malloc (vg_replace_malloc.c:177)
791 by 0x........: mk (leak-cases.c:52)
792 by 0x........: main (leak-cases.c:80)
795 <para>Because there are different kinds of leaks with different
796 severities, an interesting question is: which leaks should be
797 counted as true "errors" and which should not?
800 <para> The answer to this question affects the numbers printed in
801 the <computeroutput>ERROR SUMMARY</computeroutput> line, and also the
802 effect of the <option>--error-exitcode</option> option. First, a leak
803 is only counted as a true "error"
804 if <option>--leak-check=full</option> is specified. Then, the
805 option <option>--errors-for-leak-kinds=<set></option> controls
806 the set of leak kinds to consider as errors. The default value
807 is <option>--errors-for-leak-kinds=definite,possible</option>
816 <sect1 id="mc-manual.options"
817 xreflabel="Memcheck Command-Line Options">
818 <title>Memcheck Command-Line Options</title>
820 <!-- start of xi:include in the manpage -->
821 <variablelist id="mc.opts.list">
823 <varlistentry id="opt.leak-check" xreflabel="--leak-check">
825 <option><![CDATA[--leak-check=<no|summary|yes|full> [default: summary] ]]></option>
828 <para>When enabled, search for memory leaks when the client
829 program finishes. If set to <varname>summary</varname>, it says how
830 many leaks occurred. If set to <varname>full</varname> or
831 <varname>yes</varname>, each individual leak will be shown
832 in detail and/or counted as an error, as specified by the options
833 <option>--show-leak-kinds</option> and
834 <option>--errors-for-leak-kinds</option>. </para>
835 <para>If <varname>--xml=yes</varname> is given, memcheck will
836 automatically use the value <varname>--leak-check=full</varname>.
837 You can use <option>--show-leak-kinds=none</option> to reduce
838 the size of the xml output if you are not interested in the leak
843 <varlistentry id="opt.leak-resolution" xreflabel="--leak-resolution">
845 <option><![CDATA[--leak-resolution=<low|med|high> [default: high] ]]></option>
848 <para>When doing leak checking, determines how willing
849 Memcheck is to consider different backtraces to
850 be the same for the purposes of merging multiple leaks into a single
851 leak report. When set to <varname>low</varname>, only the first
852 two entries need match. When <varname>med</varname>, four entries
853 have to match. When <varname>high</varname>, all entries need to
856 <para>For hardcore leak debugging, you probably want to use
857 <option>--leak-resolution=high</option> together with
858 <option>--num-callers=40</option> or some such large number.
861 <para>Note that the <option>--leak-resolution</option> setting
862 does not affect Memcheck's ability to find
863 leaks. It only changes how the results are presented.</para>
867 <varlistentry id="opt.show-leak-kinds" xreflabel="--show-leak-kinds">
869 <option><![CDATA[--show-leak-kinds=<set> [default: definite,possible] ]]></option>
872 <para>Specifies the leak kinds to show in a <varname>full</varname>
873 leak search, in one of the following ways: </para>
876 <listitem><para>a comma separated list of one or more of
877 <option>definite indirect possible reachable</option>.</para>
880 <listitem><para><option>all</option> to specify the complete set (all leak kinds).
882 <option>--show-leak-kinds=definite,indirect,possible,reachable</option>.</para>
885 <listitem><para><option>none</option> for the empty set.</para>
892 <varlistentry id="opt.errors-for-leak-kinds" xreflabel="--errors-for-leak-kinds">
894 <option><![CDATA[--errors-for-leak-kinds=<set> [default: definite,possible] ]]></option>
897 <para>Specifies the leak kinds to count as errors in a
898 <varname>full</varname> leak search. The
899 <option><![CDATA[<set>]]></option> is specified similarly to
900 <option>--show-leak-kinds</option>
906 <varlistentry id="opt.leak-check-heuristics" xreflabel="--leak-check-heuristics">
908 <option><![CDATA[--leak-check-heuristics=<set> [default: all] ]]></option>
911 <para>Specifies the set of leak check heuristics to be used
912 during leak searches. The heuristics control which interior pointers
913 to a block cause it to be considered as reachable.
914 The heuristic set is specified in one of the following ways:</para>
917 <listitem><para>a comma separated list of one or more of
918 <option>stdstring length64 newarray multipleinheritance</option>.</para>
921 <listitem><para><option>all</option> to activate the complete set of
924 <option>--leak-check-heuristics=stdstring,length64,newarray,multipleinheritance</option>.</para>
927 <listitem><para><option>none</option> for the empty set.</para>
930 <para>Note that these heuristics are dependent on the layout of
931 the objects produced by the C++ compiler. They have been
932 tested with some gcc versions (e.g. 4.4 and 4.7). They might
933 not work properly with other C++ compilers.
938 <varlistentry id="opt.show-reachable" xreflabel="--show-reachable">
940 <option><![CDATA[--show-reachable=<yes|no> ]]></option>
943 <option><![CDATA[--show-possibly-lost=<yes|no> ]]></option>
946 <para>These options provide an alternative way to specify the leak kinds to show:
951 <option>--show-reachable=no --show-possibly-lost=yes</option> is equivalent to
952 <option>--show-leak-kinds=definite,possible</option>.
957 <option>--show-reachable=no --show-possibly-lost=no</option> is equivalent to
958 <option>--show-leak-kinds=definite</option>.
963 <option>--show-reachable=yes</option> is equivalent to
964 <option>--show-leak-kinds=all</option>.
968 <para> Note that <option>--show-possibly-lost=no</option> has no
969 effect if <option>--show-reachable=yes</option> is
974 <varlistentry id="opt.xtree-leak" xreflabel="--xtree-leak">
976 <option><![CDATA[--xtree-leak=<no|yes> [no] ]]></option>
979 <para>If set to yes, the results for the leak search done at exit will be
980 output in a 'Callgrind Format' execution tree file. Note that this
981 automatically sets the options <option>--leak-check=full</option>
982 and <option>--show-leak-kinds=all</option>, to allow
983 xtree visualisation tools such as kcachegrind to select what kind
984 to leak to visualize.
985 The produced file will contain the following events:</para>
987 <listitem><para><option>RB</option> : Reachable Bytes</para></listitem>
988 <listitem><para><option>PB</option> : Possibly lost Bytes</para></listitem>
989 <listitem><para><option>IB</option> : Indirectly lost Bytes</para></listitem>
990 <listitem><para><option>DB</option> : Definitely lost Bytes (direct plus indirect)</para></listitem>
991 <listitem><para><option>DIB</option> : Definitely Indirectly lost Bytes (subset of DB)</para></listitem>
992 <listitem><para><option>RBk</option> : reachable Blocks</para></listitem>
993 <listitem><para><option>PBk</option> : Possibly lost Blocks</para></listitem>
994 <listitem><para><option>IBk</option> : Indirectly lost Blocks</para></listitem>
995 <listitem><para><option>DBk</option> : Definitely lost Blocks</para></listitem>
998 <para>The increase or decrease for all events above will also be output in
999 the file to provide the delta (increase or decrease) between 2
1000 successive leak searches. For example, <option>iRB</option> is the
1001 increase of the <option>RB</option> event, <option>dPBk</option> is the
1002 decrease of <option>PBk</option> event. The values for the increase and
1003 decrease events will be zero for the first leak search done.</para>
1005 <para>See <xref linkend="&vg-xtree-id;"/> for a detailed explanation
1006 about execution trees.</para>
1010 <varlistentry id="opt.xtree-leak-file" xreflabel="--xtree-leak-file">
1012 <option><![CDATA[--xtree-leak-file=<filename> [default:
1013 xtleak.kcg.%p] ]]></option>
1016 <para>Specifies that Valgrind should produce the xtree leak
1017 report in the specified file. Any <option>%p</option>,
1018 <option>%q</option> or <option>%n</option> sequences appearing in
1019 the filename are expanded
1020 in exactly the same way as they are for <option>--log-file</option>.
1021 See the description of <xref linkend="opt.log-file"/>
1022 for details. </para>
1023 <para>See <xref linkend="&vg-xtree-id;"/>
1024 for a detailed explanation about execution trees formats. </para>
1028 <varlistentry id="opt.undef-value-errors" xreflabel="--undef-value-errors">
1030 <option><![CDATA[--undef-value-errors=<yes|no> [default: yes] ]]></option>
1033 <para>Controls whether Memcheck reports
1034 uses of undefined value errors. Set this to
1035 <varname>no</varname> if you don't want to see undefined value
1036 errors. It also has the side effect of speeding up Memcheck somewhat.
1037 AddrCheck (removed in Valgrind 3.1.0) functioned like Memcheck with
1038 <option>--undef-value-errors=no</option>.
1043 <varlistentry id="opt.track-origins" xreflabel="--track-origins">
1045 <option><![CDATA[--track-origins=<yes|no> [default: no] ]]></option>
1048 <para>Controls whether Memcheck tracks
1049 the origin of uninitialised values. By default, it does not,
1050 which means that although it can tell you that an
1051 uninitialised value is being used in a dangerous way, it
1052 cannot tell you where the uninitialised value came from. This
1053 often makes it difficult to track down the root problem.
1056 to <varname>yes</varname>, Memcheck keeps
1057 track of the origins of all uninitialised values. Then, when
1058 an uninitialised value error is
1059 reported, Memcheck will try to show the
1060 origin of the value. An origin can be one of the following
1061 four places: a heap block, a stack allocation, a client
1062 request, or miscellaneous other sources (eg, a call
1063 to <varname>brk</varname>).
1065 <para>For uninitialised values originating from a heap
1066 block, Memcheck shows where the block was
1067 allocated. For uninitialised values originating from a stack
1068 allocation, Memcheck can tell you which
1069 function allocated the value, but no more than that -- typically
1070 it shows you the source location of the opening brace of the
1071 function. So you should carefully check that all of the
1072 function's local variables are initialised properly.
1074 <para>Performance overhead: origin tracking is expensive. It
1075 halves Memcheck's speed and increases
1076 memory use by a minimum of 100MB, and possibly more.
1077 Nevertheless it can drastically reduce the effort required to
1078 identify the root cause of uninitialised value errors, and so
1079 is often a programmer productivity win, despite running
1082 <para>Accuracy: Memcheck tracks origins
1083 quite accurately. To avoid very large space and time
1084 overheads, some approximations are made. It is possible,
1085 although unlikely, that Memcheck will report an incorrect origin, or
1086 not be able to identify any origin.
1088 <para>Note that the combination
1089 <option>--track-origins=yes</option>
1090 and <option>--undef-value-errors=no</option> is
1091 nonsensical. Memcheck checks for and
1092 rejects this combination at startup.
1097 <varlistentry id="opt.partial-loads-ok" xreflabel="--partial-loads-ok">
1099 <option><![CDATA[--partial-loads-ok=<yes|no> [default: yes] ]]></option>
1102 <para>Controls how Memcheck handles 32-, 64-, 128- and 256-bit
1103 naturally aligned loads from addresses for which some bytes are
1104 addressable and others are not. When <varname>yes</varname>, such
1105 loads do not produce an address error. Instead, loaded bytes
1106 originating from illegal addresses are marked as uninitialised, and
1107 those corresponding to legal addresses are handled in the normal
1110 <para>When <varname>no</varname>, loads from partially invalid
1111 addresses are treated the same as loads from completely invalid
1112 addresses: an illegal-address error is issued, and the resulting
1113 bytes are marked as initialised.</para>
1115 <para>Note that code that behaves in this way is in violation of
1116 the ISO C/C++ standards, and should be considered broken. If
1117 at all possible, such code should be fixed.</para>
1121 <varlistentry id="opt.expensive-definedness-checks" xreflabel="--expensive-definedness-checks">
1123 <option><![CDATA[--expensive-definedness-checks=<no|auto|yes> [default: auto] ]]></option>
1126 <para>Controls whether Memcheck should employ more precise but also
1127 more expensive (time consuming) instrumentation when checking the
1128 definedness of certain values. In particular, this affects the
1129 instrumentation of integer adds, subtracts and equality
1131 <para>Selecting <option>--expensive-definedness-checks=yes</option>
1132 causes Memcheck to use the most accurate analysis possible. This
1133 minimises false error rates but can cause up to 30% performance
1135 <para>Selecting <option>--expensive-definedness-checks=no</option>
1136 causes Memcheck to use the cheapest instrumentation possible. This
1137 maximises performance but will normally give an unusably high false
1140 setting, <option>--expensive-definedness-checks=auto</option>, is
1141 strongly recommended. This causes Memcheck to use the minimum of
1142 expensive instrumentation needed to achieve the same false error
1143 rate as <option>--expensive-definedness-checks=yes</option>. It
1144 also enables an instrumentation-time analysis pass which aims to
1145 further reduce the costs of accurate instrumentation. Overall, the
1146 performance loss is generally around 5% relative to
1147 <option>--expensive-definedness-checks=no</option>, although this is
1148 strongly workload dependent. Note that the exact instrumentation
1149 settings in this mode are architecture dependent.</para>
1153 <varlistentry id="opt.keep-stacktraces" xreflabel="--keep-stacktraces">
1155 <option><![CDATA[--keep-stacktraces=alloc|free|alloc-and-free|alloc-then-free|none [default: alloc-and-free] ]]></option>
1158 <para>Controls which stack trace(s) to keep for malloc'd and/or
1162 <para>With <varname>alloc-then-free</varname>, a stack trace is
1163 recorded at allocation time, and is associated with the block.
1164 When the block is freed, a second stack trace is recorded, and
1165 this replaces the allocation stack trace. As a result, any "use
1166 after free" errors relating to this block can only show a stack
1167 trace for where the block was freed.
1170 <para>With <varname>alloc-and-free</varname>, both allocation
1171 and the deallocation stack traces for the block are stored.
1172 Hence a "use after free" error will
1173 show both, which may make the error easier to diagnose.
1174 Compared to <varname>alloc-then-free</varname>, this setting
1175 slightly increases Valgrind's memory use as the block contains two
1176 references instead of one.
1179 <para>With <varname>alloc</varname>, only the allocation stack
1180 trace is recorded (and reported). With <varname>free</varname>,
1181 only the deallocation stack trace is recorded (and reported).
1182 These values somewhat decrease Valgrind's memory and cpu usage.
1183 They can be useful depending on the error types you are
1184 searching for and the level of detail you need to analyse
1185 them. For example, if you are only interested in memory leak
1186 errors, it is sufficient to record the allocation stack traces.
1189 <para>With <varname>none</varname>, no stack traces are recorded
1190 for malloc and free operations. If your program allocates a lot
1191 of blocks and/or allocates/frees from many different stack
1192 traces, this can significantly decrease cpu and/or memory
1193 required. Of course, few details will be reported for errors
1194 related to heap blocks.
1197 <para>Note that once a stack trace is recorded, Valgrind keeps
1198 the stack trace in memory even if it is not referenced by any
1199 block. Some programs (for example, recursive algorithms) can
1200 generate a huge number of stack traces. If Valgrind uses too
1201 much memory in such circumstances, you can reduce the memory
1202 required with the options <varname>--keep-stacktraces</varname>
1203 and/or by using a smaller value for the
1204 option <varname>--num-callers</varname>.
1207 <para>If you want to use
1208 <computeroutput>--xtree-memory=full</computeroutput> memory profiling
1209 (see <xref linkend="&vg-xtree-id;"/>), then you cannot
1210 specify <varname>--keep-stacktraces=free</varname>
1211 or <varname>--keep-stacktraces=none</varname>.</para>
1216 <varlistentry id="opt.freelist-vol" xreflabel="--freelist-vol">
1218 <option><![CDATA[--freelist-vol=<number> [default: 20000000] ]]></option>
1221 <para>When the client program releases memory using
1222 <function>free</function> (in <literal>C</literal>) or
1223 <computeroutput>delete</computeroutput>
1224 (<literal>C++</literal>), that memory is not immediately made
1225 available for re-allocation. Instead, it is marked inaccessible
1226 and placed in a queue of freed blocks. The purpose is to defer as
1227 long as possible the point at which freed-up memory comes back
1228 into circulation. This increases the chance that
1229 Memcheck will be able to detect invalid
1230 accesses to blocks for some significant period of time after they
1231 have been freed.</para>
1233 <para>This option specifies the maximum total size, in bytes, of the
1234 blocks in the queue. The default value is twenty million bytes.
1235 Increasing this increases the total amount of memory used by
1236 Memcheck but may detect invalid uses of freed
1237 blocks which would otherwise go undetected.</para>
1241 <varlistentry id="opt.freelist-big-blocks" xreflabel="--freelist-big-blocks">
1243 <option><![CDATA[--freelist-big-blocks=<number> [default: 1000000] ]]></option>
1246 <para>When making blocks from the queue of freed blocks available
1247 for re-allocation, Memcheck will in priority re-circulate the blocks
1248 with a size greater or equal to <option>--freelist-big-blocks</option>.
1249 This ensures that freeing big blocks (in particular freeing blocks bigger than
1250 <option>--freelist-vol</option>) does not immediately lead to a re-circulation
1251 of all (or a lot of) the small blocks in the free list. In other words,
1252 this option increases the likelihood to discover dangling pointers
1253 for the "small" blocks, even when big blocks are freed.</para>
1254 <para>Setting a value of 0 means that all the blocks are re-circulated
1255 in a FIFO order. </para>
1259 <varlistentry id="opt.workaround-gcc296-bugs" xreflabel="--workaround-gcc296-bugs">
1261 <option><![CDATA[--workaround-gcc296-bugs=<yes|no> [default: no] ]]></option>
1264 <para>When enabled, assume that reads and writes some small
1265 distance below the stack pointer are due to bugs in GCC 2.96, and
1266 does not report them. The "small distance" is 256 bytes by
1267 default. Note that GCC 2.96 is the default compiler on some ancient
1268 Linux distributions (RedHat 7.X) and so you may need to use this
1269 option. Do not use it if you do not have to, as it can cause real
1270 errors to be overlooked. A better alternative is to use a more
1271 recent GCC in which this bug is fixed.</para>
1273 <para>You may also need to use this option when working with
1274 GCC 3.X or 4.X on 32-bit PowerPC Linux. This is because
1275 GCC generates code which occasionally accesses below the
1276 stack pointer, particularly for floating-point to/from integer
1277 conversions. This is in violation of the 32-bit PowerPC ELF
1278 specification, which makes no provision for locations below the
1279 stack pointer to be accessible.</para>
1281 <para>This option is deprecated as of version 3.12 and may be
1282 removed from future versions. You should instead use
1283 <option>--ignore-range-below-sp</option> to specify the exact
1284 range of offsets below the stack pointer that should be ignored.
1285 A suitable equivalent
1286 is <option>--ignore-range-below-sp=1024-1</option>.
1291 <varlistentry id="opt.ignore-range-below-sp"
1292 xreflabel="--ignore-range-below-sp">
1294 <option><![CDATA[--ignore-range-below-sp=<number>-<number> ]]></option>
1297 <para>This is a more general replacement for the deprecated
1298 <option>--workaround-gcc296-bugs</option> option. When
1299 specified, it causes Memcheck not to report errors for accesses
1300 at the specified offsets below the stack pointer. The two
1301 offsets must be positive decimal numbers and -- somewhat
1302 counterintuitively -- the first one must be larger, in order to
1303 imply a non-wraparound address range to ignore. For example,
1304 to ignore 4 byte accesses at 8192 bytes below the stack
1306 use <option>--ignore-range-below-sp=8192-8189</option>. Only
1307 one range may be specified.
1312 <varlistentry id="opt.show-mismatched-frees"
1313 xreflabel="--show-mismatched-frees">
1315 <option><![CDATA[--show-mismatched-frees=<yes|no> [default: yes] ]]></option>
1318 <para>When enabled, Memcheck checks that heap blocks are
1319 deallocated using a function that matches the allocating
1320 function. That is, it expects <varname>free</varname> to be
1321 used to deallocate blocks allocated
1322 by <varname>malloc</varname>, <varname>delete</varname> for
1323 blocks allocated by <varname>new</varname>,
1324 and <varname>delete[]</varname> for blocks allocated
1325 by <varname>new[]</varname>. If a mismatch is detected, an
1326 error is reported. This is in general important because in some
1327 environments, freeing with a non-matching function can cause
1330 <para>There is however a scenario where such mismatches cannot
1331 be avoided. That is when the user provides implementations of
1332 <varname>new</varname>/<varname>new[]</varname> that
1333 call <varname>malloc</varname> and
1334 of <varname>delete</varname>/<varname>delete[]</varname> that
1335 call <varname>free</varname>, and these functions are
1336 asymmetrically inlined. For example, imagine
1337 that <varname>delete[]</varname> is inlined
1338 but <varname>new[]</varname> is not. The result is that
1339 Memcheck "sees" all <varname>delete[]</varname> calls as direct
1340 calls to <varname>free</varname>, even when the program source
1341 contains no mismatched calls.</para>
1343 <para>This causes a lot of confusing and irrelevant error
1344 reports. <varname>--show-mismatched-frees=no</varname> disables
1345 these checks. It is not generally advisable to disable them,
1346 though, because you may miss real errors as a result.</para>
1350 <varlistentry id="opt.show-realloc-size-zero"
1351 xreflabel="--show-realloc-size-zero">
1353 <option><![CDATA[--show-realloc-size-zero=<yes|no> [default: yes] ]]></option>
1356 <para>When enabled, Memcheck checks for uses of <varname>realloc</varname> with a size of zero.
1357 This usage of <varname>realloc</varname> is unsafe since it is not portable. On some systems it
1358 will behave like <varname>free</varname>. On other systems it will either do nothing or else
1359 behave like a call to <varname>free</varname> followed by a call to <varname>malloc</varname>
1360 with a size of zero.</para>
1364 <varlistentry id="opt.ignore-ranges" xreflabel="--ignore-ranges">
1366 <option><![CDATA[--ignore-ranges=0xPP-0xQQ[,0xRR-0xSS] ]]></option>
1369 <para>Any ranges listed in this option (and multiple ranges can be
1370 specified, separated by commas) will be ignored by Memcheck's
1371 addressability checking.</para>
1375 <varlistentry id="opt.malloc-fill" xreflabel="--malloc-fill">
1377 <option><![CDATA[--malloc-fill=<hexnumber> ]]></option>
1380 <para>Fills blocks allocated
1381 by <computeroutput>malloc</computeroutput>,
1382 <computeroutput>new</computeroutput>, etc, but not
1383 by <computeroutput>calloc</computeroutput>, with the specified
1384 byte. This can be useful when trying to shake out obscure
1385 memory corruption problems. The allocated area is still
1386 regarded by Memcheck as undefined -- this option only affects its
1387 contents. Note that <option>--malloc-fill</option> does not
1388 affect a block of memory when it is used as argument
1389 to client requests VALGRIND_MEMPOOL_ALLOC or
1390 VALGRIND_MALLOCLIKE_BLOCK.
1395 <varlistentry id="opt.free-fill" xreflabel="--free-fill">
1397 <option><![CDATA[--free-fill=<hexnumber> ]]></option>
1400 <para>Fills blocks freed
1401 by <computeroutput>free</computeroutput>,
1402 <computeroutput>delete</computeroutput>, etc, with the
1403 specified byte value. This can be useful when trying to shake out
1404 obscure memory corruption problems. The freed area is still
1405 regarded by Memcheck as not valid for access -- this option only
1406 affects its contents. Note that <option>--free-fill</option> does not
1407 affect a block of memory when it is used as argument to
1408 client requests VALGRIND_MEMPOOL_FREE or VALGRIND_FREELIKE_BLOCK.
1414 <!-- end of xi:include in the manpage -->
1419 <sect1 id="mc-manual.suppfiles" xreflabel="Writing suppression files">
1420 <title>Writing suppression files</title>
1422 <para>The basic suppression format is described in
1423 <xref linkend="manual-core.suppress"/>.</para>
1425 <para>The suppression-type (second) line should have the form:</para>
1426 <programlisting><![CDATA[
1427 Memcheck:suppression_type]]></programlisting>
1429 <para>The Memcheck suppression types are as follows:</para>
1433 <para><varname>Value1</varname>,
1434 <varname>Value2</varname>,
1435 <varname>Value4</varname>,
1436 <varname>Value8</varname>,
1437 <varname>Value16</varname>,
1438 meaning an uninitialised-value error when
1439 using a value of 1, 2, 4, 8 or 16 bytes.</para>
1443 <para><varname>Cond</varname> (or its old
1444 name, <varname>Value0</varname>), meaning use
1445 of an uninitialised CPU condition code.</para>
1449 <para><varname>Addr1</varname>,
1450 <varname>Addr2</varname>,
1451 <varname>Addr4</varname>,
1452 <varname>Addr8</varname>,
1453 <varname>Addr16</varname>,
1454 meaning an invalid address during a
1455 memory access of 1, 2, 4, 8 or 16 bytes respectively.</para>
1459 <para><varname>Jump</varname>, meaning an
1460 jump to an unaddressable location error.</para>
1464 <para><varname>Param</varname>, meaning an
1465 invalid system call parameter error.</para>
1469 <para><varname>Free</varname>, meaning an
1470 invalid or mismatching free.</para>
1474 <para><varname>Overlap</varname>, meaning a
1475 <computeroutput>src</computeroutput> /
1476 <computeroutput>dst</computeroutput> overlap in
1477 <function>memcpy</function> or a similar function.</para>
1481 <para><varname>Leak</varname>, meaning
1482 a memory leak.</para>
1487 <para><computeroutput>Param</computeroutput> errors have a mandatory extra
1488 information line at this point, which is the name of the offending
1489 system call parameter. </para>
1491 <para><computeroutput>Leak</computeroutput> errors have an optional
1492 extra information line, with the following format:</para>
1493 <programlisting><![CDATA[
1494 match-leak-kinds:<set>]]></programlisting>
1495 <para>where <computeroutput><set></computeroutput> specifies which
1496 leak kinds are matched by this suppression entry.
1497 <computeroutput><set></computeroutput> is specified in the
1498 same way as with the option <option>--show-leak-kinds</option>, that is,
1499 one of the following:</para>
1501 <listitem><para>a comma separated list of one or more of
1502 <option>definite indirect possible reachable</option>.</para>
1505 <listitem><para><option>all</option> to specify the complete set
1506 (all leak kinds).</para>
1509 <listitem><para><option>none</option> for the empty set.</para>
1512 <para>If this optional extra line is not present, the suppression
1513 entry will match all leak kinds.</para>
1515 <para>Be aware that leak suppressions that are created using
1516 <option>--gen-suppressions</option> will contain this optional extra
1517 line, and therefore may match fewer leaks than you expect. You may
1518 want to remove the line before using the generated
1519 suppressions.</para>
1521 <para>The other Memcheck error kinds do not have extra lines.</para>
1524 If you give the <option>-v</option> option, Valgrind will print
1525 the list of used suppressions at the end of execution.
1526 For a leak suppression, this output gives the number of different
1527 loss records that match the suppression, and the number of bytes
1528 and blocks suppressed by the suppression.
1529 If the run contains multiple leak checks, the number of bytes and blocks
1530 are reset to zero before each new leak check. Note that the number of different
1531 loss records is not reset to zero.</para>
1532 <para>In the example below, in the last leak search, 7 blocks and 96 bytes have
1533 been suppressed by a suppression with the name
1534 <option>some_leak_suppression</option>:</para>
1535 <programlisting><![CDATA[
1536 --21041-- used_suppression: 10 some_other_leak_suppression s.supp:14 suppressed: 12,400 bytes in 1 blocks
1537 --21041-- used_suppression: 39 some_leak_suppression s.supp:2 suppressed: 96 bytes in 7 blocks
1538 ]]></programlisting>
1540 <para>For <varname>ValueN</varname> and <varname>AddrN</varname>
1541 errors, the first line of the calling context is either the name of
1542 the function in which the error occurred, or, failing that, the full
1543 path of the <filename>.so</filename> file or executable containing the
1544 error location. For <varname>Free</varname> errors, the first line is
1545 the name of the function doing the freeing (eg,
1546 <function>free</function>, <function>__builtin_vec_delete</function>,
1547 etc). For <varname>Overlap</varname> errors, the first line is the name of the
1548 function with the overlapping arguments (eg.
1549 <function>memcpy</function>, <function>strcpy</function>, etc).</para>
1551 <para>The last part of any suppression specifies the rest of the
1552 calling context that needs to be matched.</para>
1558 <sect1 id="mc-manual.machine"
1559 xreflabel="Details of Memcheck's checking machinery">
1560 <title>Details of Memcheck's checking machinery</title>
1562 <para>Read this section if you want to know, in detail, exactly
1563 what and how Memcheck is checking.</para>
1566 <sect2 id="mc-manual.value" xreflabel="Valid-value (V) bit">
1567 <title>Valid-value (V) bits</title>
1569 <para>It is simplest to think of Memcheck implementing a synthetic CPU
1570 which is identical to a real CPU, except for one crucial detail. Every
1571 bit (literally) of data processed, stored and handled by the real CPU
1572 has, in the synthetic CPU, an associated "valid-value" bit, which says
1573 whether or not the accompanying bit has a legitimate value. In the
1574 discussions which follow, this bit is referred to as the V (valid-value)
1577 <para>Each byte in the system therefore has a 8 V bits which follow it
1578 wherever it goes. For example, when the CPU loads a word-size item (4
1579 bytes) from memory, it also loads the corresponding 32 V bits from a
1580 bitmap which stores the V bits for the process' entire address space.
1581 If the CPU should later write the whole or some part of that value to
1582 memory at a different address, the relevant V bits will be stored back
1583 in the V-bit bitmap.</para>
1585 <para>In short, each bit in the system has (conceptually) an associated V
1586 bit, which follows it around everywhere, even inside the CPU. Yes, all the
1587 CPU's registers (integer, floating point, vector and condition registers)
1588 have their own V bit vectors. For this to work, Memcheck uses a great deal
1589 of compression to represent the V bits compactly.</para>
1591 <para>Copying values around does not cause Memcheck to check for, or
1592 report on, errors. However, when a value is used in a way which might
1593 conceivably affect your program's externally-visible behaviour,
1594 the associated V bits are immediately checked. If any of these indicate
1595 that the value is undefined (even partially), an error is reported.</para>
1597 <para>Here's an (admittedly nonsensical) example:</para>
1598 <programlisting><![CDATA[
1601 for ( i = 0; i < 10; i++ ) {
1604 }]]></programlisting>
1606 <para>Memcheck emits no complaints about this, since it merely copies
1607 uninitialised values from <varname>a[]</varname> into
1608 <varname>b[]</varname>, and doesn't use them in a way which could
1609 affect the behaviour of the program. However, if
1610 the loop is changed to:</para>
1611 <programlisting><![CDATA[
1612 for ( i = 0; i < 10; i++ ) {
1616 printf("hello there\n");
1617 ]]></programlisting>
1619 <para>then Memcheck will complain, at the
1620 <computeroutput>if</computeroutput>, that the condition depends on
1621 uninitialised values. Note that it <command>doesn't</command> complain
1622 at the <varname>j += a[i];</varname>, since at that point the
1623 undefinedness is not "observable". It's only when a decision has to be
1624 made as to whether or not to do the <function>printf</function> -- an
1625 observable action of your program -- that Memcheck complains.</para>
1627 <para>Most low level operations, such as adds, cause Memcheck to use the
1628 V bits for the operands to calculate the V bits for the result. Even if
1629 the result is partially or wholly undefined, it does not
1632 <para>Checks on definedness only occur in three places: when a value is
1633 used to generate a memory address, when control flow decision needs to
1634 be made, and when a system call is detected, Memcheck checks definedness
1635 of parameters as required.</para>
1637 <para>If a check should detect undefinedness, an error message is
1638 issued. The resulting value is subsequently regarded as well-defined.
1639 To do otherwise would give long chains of error messages. In other
1640 words, once Memcheck reports an undefined value error, it tries to
1641 avoid reporting further errors derived from that same undefined
1644 <para>This sounds overcomplicated. Why not just check all reads from
1645 memory, and complain if an undefined value is loaded into a CPU
1646 register? Well, that doesn't work well, because perfectly legitimate C
1647 programs routinely copy uninitialised values around in memory, and we
1648 don't want endless complaints about that. Here's the canonical example.
1649 Consider a struct like this:</para>
1650 <programlisting><![CDATA[
1651 struct S { int x; char c; };
1656 ]]></programlisting>
1658 <para>The question to ask is: how large is <varname>struct S</varname>,
1659 in bytes? An <varname>int</varname> is 4 bytes and a
1660 <varname>char</varname> one byte, so perhaps a <varname>struct
1661 S</varname> occupies 5 bytes? Wrong. All non-toy compilers we know
1662 of will round the size of <varname>struct S</varname> up to a whole
1663 number of words, in this case 8 bytes. Not doing this forces compilers
1664 to generate truly appalling code for accessing arrays of
1665 <varname>struct S</varname>'s on some architectures.</para>
1667 <para>So <varname>s1</varname> occupies 8 bytes, yet only 5 of them will
1668 be initialised. For the assignment <varname>s2 = s1</varname>, GCC
1669 generates code to copy all 8 bytes wholesale into <varname>s2</varname>
1670 without regard for their meaning. If Memcheck simply checked values as
1671 they came out of memory, it would yelp every time a structure assignment
1672 like this happened. So the more complicated behaviour described above
1673 is necessary. This allows GCC to copy
1674 <varname>s1</varname> into <varname>s2</varname> any way it likes, and a
1675 warning will only be emitted if the uninitialised values are later
1678 <para>As explained above, Memcheck maintains 8 V bits for each byte in your
1679 process, including for bytes that are in shared memory. However, the same piece
1680 of shared memory can be mapped multiple times, by several processes or even by
1681 the same process (for example, if the process wants a read-only and a read-write
1682 mapping of the same page). For such multiple mappings, Memcheck tracks the V
1683 bits for each mapping independently. This can lead to false positive errors, as
1684 the shared memory can be initialised via a first mapping, and accessed via
1685 another mapping. The access via this other mapping will have its own V bits,
1686 which have not been changed when the memory was initialised via the first
1687 mapping. The bypass for these false positives is to use Memcheck's client
1688 requests <varname>VALGRIND_MAKE_MEM_DEFINED</varname> and
1689 <varname>VALGRIND_MAKE_MEM_UNDEFINED</varname> to inform
1690 Memcheck about what your program does (or what another process does)
1691 to these shared memory mappings.
1697 <sect2 id="mc-manual.vaddress" xreflabel=" Valid-address (A) bits">
1698 <title>Valid-address (A) bits</title>
1700 <para>Notice that the previous subsection describes how the validity of
1701 values is established and maintained without having to say whether the
1702 program does or does not have the right to access any particular memory
1703 location. We now consider the latter question.</para>
1705 <para>As described above, every bit in memory or in the CPU has an
1706 associated valid-value (V) bit. In addition, all bytes in memory, but
1707 not in the CPU, have an associated valid-address (A) bit. This
1708 indicates whether or not the program can legitimately read or write that
1709 location. It does not give any indication of the validity of the data
1710 at that location -- that's the job of the V bits -- only whether or not
1711 the location may be accessed.</para>
1713 <para>Every time your program reads or writes memory, Memcheck checks
1714 the A bits associated with the address. If any of them indicate an
1715 invalid address, an error is emitted. Note that the reads and writes
1716 themselves do not change the A bits, only consult them.</para>
1718 <para>So how do the A bits get set/cleared? Like this:</para>
1722 <para>When the program starts, all the global data areas are
1723 marked as accessible.</para>
1727 <para>When the program does
1728 <function>malloc</function>/<computeroutput>new</computeroutput>,
1729 the A bits for exactly the area allocated, and not a byte more,
1730 are marked as accessible. Upon freeing the area the A bits are
1731 changed to indicate inaccessibility.</para>
1735 <para>When the stack pointer register (<literal>SP</literal>) moves
1736 up or down, A bits are set. The rule is that the area from
1737 <literal>SP</literal> up to the base of the stack is marked as
1738 accessible, and below <literal>SP</literal> is inaccessible. (If
1739 that sounds illogical, bear in mind that the stack grows down, not
1740 up, on almost all Unix systems, including GNU/Linux.) Tracking
1741 <literal>SP</literal> like this has the useful side-effect that the
1742 section of stack used by a function for local variables etc is
1743 automatically marked accessible on function entry and inaccessible
1748 <para>When doing system calls, A bits are changed appropriately.
1749 For example, <literal>mmap</literal>
1750 magically makes files appear in the process'
1751 address space, so the A bits must be updated if <literal>mmap</literal>
1756 <para>Optionally, your program can tell Memcheck about such changes
1757 explicitly, using the client request mechanism described
1766 <sect2 id="mc-manual.together" xreflabel="Putting it all together">
1767 <title>Putting it all together</title>
1769 <para>Memcheck's checking machinery can be summarised as
1774 <para>Each byte in memory has 8 associated V (valid-value) bits,
1775 saying whether or not the byte has a defined value, and a single A
1776 (valid-address) bit, saying whether or not the program currently has
1777 the right to read/write that address. As mentioned above, heavy
1778 use of compression means the overhead is typically around 25%.</para>
1782 <para>When memory is read or written, the relevant A bits are
1783 consulted. If they indicate an invalid address, Memcheck emits an
1784 Invalid read or Invalid write error.</para>
1788 <para>When memory is read into the CPU's registers, the relevant V
1789 bits are fetched from memory and stored in the simulated CPU. They
1790 are not consulted.</para>
1794 <para>When a register is written out to memory, the V bits for that
1795 register are written back to memory too.</para>
1799 <para>When values in CPU registers are used to generate a memory
1800 address, or to determine the outcome of a conditional branch, the V
1801 bits for those values are checked, and an error emitted if any of
1802 them are undefined.</para>
1806 <para>When values in CPU registers are used for any other purpose,
1807 Memcheck computes the V bits for the result, but does not check
1812 <para>Once the V bits for a value in the CPU have been checked, they
1813 are then set to indicate validity. This avoids long chains of
1818 <para>When values are loaded from memory, Memcheck checks the A bits
1819 for that location and issues an illegal-address warning if needed.
1820 In that case, the V bits loaded are forced to indicate Valid,
1821 despite the location being invalid.</para>
1823 <para>This apparently strange choice reduces the amount of confusing
1824 information presented to the user. It avoids the unpleasant
1825 phenomenon in which memory is read from a place which is both
1826 unaddressable and contains invalid values, and, as a result, you get
1827 not only an invalid-address (read/write) error, but also a
1828 potentially large set of uninitialised-value errors, one for every
1829 time the value is used.</para>
1831 <para>There is a hazy boundary case to do with multi-byte loads from
1832 addresses which are partially valid and partially invalid. See
1833 details of the option <option>--partial-loads-ok</option> for details.
1840 <para>Memcheck intercepts calls to <function>malloc</function>,
1841 <function>calloc</function>, <function>realloc</function>,
1842 <function>valloc</function>, <function>memalign</function>,
1843 <function>free</function>, <computeroutput>new</computeroutput>,
1844 <computeroutput>new[]</computeroutput>,
1845 <computeroutput>delete</computeroutput> and
1846 <computeroutput>delete[]</computeroutput>. The behaviour you get
1852 <para><function>malloc</function>/<function>new</function>/<computeroutput>new[]</computeroutput>:
1853 the returned memory is marked as addressable but not having valid
1854 values. This means you have to write to it before you can read
1859 <para><function>calloc</function>: returned memory is marked both
1860 addressable and valid, since <function>calloc</function> clears
1861 the area to zero.</para>
1865 <para><function>realloc</function>: if the new size is larger than
1866 the old, the new section is addressable but invalid, as with
1867 <function>malloc</function>. If the new size is smaller, the
1868 dropped-off section is marked as unaddressable. You may only pass to
1869 <function>realloc</function> a pointer previously issued to you by
1870 <function>malloc</function>/<function>calloc</function>/<function>realloc</function>.</para>
1874 <para><function>free</function>/<computeroutput>delete</computeroutput>/<computeroutput>delete[]</computeroutput>:
1875 you may only pass to these functions a pointer previously issued
1876 to you by the corresponding allocation function. Otherwise,
1877 Memcheck complains. If the pointer is indeed valid, Memcheck
1878 marks the entire area it points at as unaddressable, and places
1879 the block in the freed-blocks-queue. The aim is to defer as long
1880 as possible reallocation of this block. Until that happens, all
1881 attempts to access it will elicit an invalid-address error, as you
1890 <sect1 id="mc-manual.monitor-commands" xreflabel="Memcheck Monitor Commands">
1891 <title>Memcheck Monitor Commands</title>
1892 <para>The Memcheck tool provides monitor commands handled by Valgrind's built-in
1893 gdbserver (see <xref linkend="manual-core-adv.gdbserver-commandhandling"/>).
1894 Valgrind python code provides GDB front end commands giving an easier usage of
1895 the memcheck monitor commands (see
1896 <xref linkend="manual-core-adv.gdbserver-gdbmonitorfrontend"/>). To launch a
1897 memcheck monitor command via its GDB front end command, instead of prefixing
1898 the command with "monitor", you must use the GDB <varname>memcheck</varname>
1899 command (or the shorter aliases <varname>mc</varname>). Using the memcheck
1900 GDB front end command provide a more flexible usage, such as evaluation of
1901 address and length arguments by GDB. In GDB, you can use <varname>help
1902 memcheck</varname> to get help about the memcheck front end monitor commands
1903 and you can use <varname>apropos memcheck</varname> to get all the commands
1904 mentionning the word "memcheck" in their name or on-line help.
1909 <para><varname>xb <addr> [<len>]</varname>
1910 shows the definedness (V) bits and values for <len> (default 1)
1911 bytes starting at <addr>.
1912 For each 8 bytes, two lines are output.
1915 The first line shows the validity bits for 8 bytes.
1916 The definedness of each byte in the range is given using two hexadecimal
1917 digits. These hexadecimal digits encode the validity of each bit of the
1919 using 0 if the bit is defined and 1 if the bit is undefined.
1920 If a byte is not addressable, its validity bits are replaced
1921 by <varname>__</varname> (a double underscore).
1924 The second line shows the values of the bytes below the corresponding
1925 validity bits. The format used to show the bytes data is similar to the
1926 GDB command 'x /<len>xb <addr>'. The value for a non
1927 addressable bytes is shown as ?? (two question marks).
1930 In the following example, <varname>string10</varname> is an array
1931 of 10 characters, in which the even numbered bytes are
1932 undefined. In the below example, the byte corresponding
1933 to <varname>string10[5]</varname> is not addressable.
1935 <programlisting><![CDATA[
1937 $4 = (char (*)[10]) 0x804a2f0
1938 (gdb) mo xb 0x804a2f0 10
1939 ff 00 ff 00 ff __ ff 00
1940 0x804A2F0: 0x3f 0x6e 0x3f 0x65 0x3f 0x?? 0x3f 0x65
1942 0x804A2F8: 0x3f 0x00
1943 Address 0x804A2F0 len 10 has 1 bytes unaddressable
1945 ]]></programlisting>
1947 <para>The GDB memcheck front end command <varname>memcheck xb ADDR
1948 [LEN]</varname> accepts any address expression for its first ADDR
1949 argument. The second optional argument is any integer expression. Note
1950 that these 2 arguments must be separated by a space.
1951 The following example shows how to get the definedness of
1952 <varname>string10</varname> using the memcheck xb front end command.
1954 <programlisting><![CDATA[
1955 (gdb) mc xb &string10 sizeof(string10)
1956 ff 00 ff 00 ff __ ff 00
1957 0x804A2F0: 0x3f 0x6e 0x3f 0x65 0x3f 0x?? 0x3f 0x65
1959 0x804A2F8: 0x3f 0x00
1960 Address 0x804A2F0 len 10 has 1 bytes unaddressable
1962 ]]></programlisting>
1964 <para> The command xb cannot be used with registers. To get
1965 the validity bits of a register, you must start Valgrind with the
1966 option <option>--vgdb-shadow-registers=yes</option>. The validity
1967 bits of a register can then be obtained by printing the 'shadow 1'
1968 corresponding register. In the below x86 example, the register
1969 eax has all its bits undefined, while the register ebx is fully
1972 <programlisting><![CDATA[
1978 ]]></programlisting>
1983 <para><varname>get_vbits <addr> [<len>]</varname>
1984 shows the definedness (V) bits for <len> (default 1) bytes
1985 starting at <addr> using the same convention as the
1986 <varname>xb</varname> command. <varname>get_vbits</varname> only
1987 shows the V bits (grouped by 4 bytes). It does not show the values.
1988 If you want to associate V bits with the corresponding byte values, the
1989 <varname>xb</varname> command will be easier to use, in particular
1990 on little endian computers when associating undefined parts of an integer
1991 with their V bits values.
1994 The following example shows the result of <varname>get_vbits</varname> on
1995 the <varname>string10</varname> used in the <varname>xb</varname> command
1996 explanation. The GDB memcheck equivalent front end command <varname>memcheck
1997 get_vbits ADDR [LEN]</varname>accepts any ADDR expression and any LEN
1998 expression (separated by a space).
2000 <programlisting><![CDATA[
2001 (gdb) monitor get_vbits 0x804a2f0 10
2002 ff00ff00 ff__ff00 ff00
2003 Address 0x804A2F0 len 10 has 1 bytes unaddressable
2004 (gdb) memcheck get_vbits &string10 sizeof(string10)
2005 ff00ff00 ff__ff00 ff00
2006 Address 0x804A2F0 len 10 has 1 bytes unaddressable
2007 ]]></programlisting>
2012 <para><varname>make_memory
2013 [noaccess|undefined|defined|Definedifaddressable] <addr>
2014 [<len>]</varname> marks the range of <len> (default 1)
2015 bytes at <addr> as having the given status. Parameter
2016 <varname>noaccess</varname> marks the range as non-accessible, so
2017 Memcheck will report an error on any access to it.
2018 <varname>undefined</varname> or <varname>defined</varname> mark
2019 the area as accessible, but Memcheck regards the bytes in it
2020 respectively as having undefined or defined values.
2021 <varname>Definedifaddressable</varname> marks as defined, bytes in
2022 the range which are already addressible, but makes no change to
2023 the status of bytes in the range which are not addressible. Note
2024 that the first letter of <varname>Definedifaddressable</varname>
2025 is an uppercase D to avoid confusion with <varname>defined</varname>.
2028 <para>The GDB equivalent memcheck front end commands <varname>memcheck
2029 make_memory [noaccess|undefined|defined|Definedifaddressable] ADDR
2030 [LEN]</varname> accept any address expression for their first ADDR
2031 argument. The second optional argument is any integer expression. Note
2032 that these 2 arguments must be separated by a space.
2036 In the following example, the first byte of the
2037 <varname>string10</varname> is marked as defined and then is marked
2040 <programlisting><![CDATA[
2041 (gdb) monitor make_memory defined 0x8049e28 1
2042 (gdb) monitor get_vbits 0x8049e28 10
2043 0000ff00 ff00ff00 ff00
2044 (gdb) memcheck make_memory noaccess &string10[0]
2045 (gdb) memcheck get_vbits &string10 sizeof(string10)
2046 __00ff00 ff00ff00 ff00
2047 Address 0x8049E28 len 10 has 1 bytes unaddressable
2049 ]]></programlisting>
2053 <para><varname>check_memory [addressable|defined] <addr>
2054 [<len>]</varname> checks that the range of <len>
2055 (default 1) bytes at <addr> has the specified accessibility.
2056 It then outputs a description of <addr>. In the following
2057 example, a detailed description is available because the
2058 option <option>--read-var-info=yes</option> was given at Valgrind
2061 <programlisting><![CDATA[
2062 (gdb) monitor check_memory defined 0x8049e28 1
2063 Address 0x8049E28 len 1 defined
2064 ==14698== Location 0x8049e28 is 0 bytes inside string10[0],
2065 ==14698== declared at prog.c:10, in frame #0 of thread 1
2067 ]]></programlisting>
2068 <para>The GDB equivalent memcheck front end commands <varname>memcheck
2069 check_memory [addressable|defined] ADDR [LEN]</varname> accept any address
2070 expression for their first ADDR argument. The second optional argument is
2071 any integer expression. Note that these 2 arguments must be separated by a
2078 <para><varname>leak_check [full*|summary|xtleak]
2079 [kinds <set>|reachable|possibleleak*|definiteleak]
2080 [heuristics heur1,heur2,...]
2081 [new|increased*|changed|any]
2082 [unlimited*|limited <max_loss_records_output>]
2084 performs a leak check. The <varname>*</varname> in the arguments
2085 indicates the default values. </para>
2087 <para> If the <varname>[full*|summary|xtleak]</varname> argument is
2088 <varname>summary</varname>, only a summary of the leak search is given;
2089 otherwise a full leak report is produced. A full leak report gives
2090 detailed information for each leak: the stack trace where the leaked blocks
2091 were allocated, the number of blocks leaked and their total size. When a
2092 full report is requested, the next two arguments further specify what
2093 kind of leaks to report. A leak's details are shown if they match
2094 both the second and third argument. A full leak report might
2095 output detailed information for many leaks. The nr of leaks for
2096 which information is output can be controlled using
2097 the <varname>limited</varname> argument followed by the maximum nr
2098 of leak records to output. If this maximum is reached, the leak
2099 search outputs the records with the biggest number of bytes.
2101 <para>The value <varname>xtleak</varname> also produces a full leak report,
2102 but output it as an xtree in a file xtleak.kcg.%p.%n (see <xref linkend="opt.log-file"/>).
2103 See <xref linkend="&vg-xtree-id;"/>
2104 for a detailed explanation about execution trees formats.
2105 See <xref linkend="opt.xtree-leak"/> for the description of the events
2106 in a xtree leak file.
2109 <para>The <varname>kinds</varname> argument controls what kind of blocks
2110 are shown for a <varname>full</varname> leak search. The set of leak kinds
2111 to show can be specified using a <varname><set></varname> similarly
2112 to the command line option <option>--show-leak-kinds</option>.
2113 Alternatively, the value <varname>definiteleak</varname>
2114 is equivalent to <varname>kinds definite</varname>, the
2115 value <varname>possibleleak</varname> is equivalent to
2116 <varname>kinds definite,possible</varname> : it will also show
2117 possibly leaked blocks, .i.e those for which only an interior
2118 pointer was found. The value <varname>reachable</varname> will
2119 show all block categories (i.e. is equivalent to <varname>kinds
2123 <para>The <varname>heuristics</varname> argument controls the heuristics
2124 used during the leak search. The set of heuristics to use can be specified
2125 using a <varname><set></varname> similarly
2126 to the command line option <option>--leak-check-heuristics</option>.
2127 The default value for the <varname>heuristics</varname> argument is
2128 <varname>heuristics none</varname>.
2131 <para>The <varname>[new|increased*|changed|any]</varname> argument controls
2132 what kinds of changes are shown for a <varname>full</varname> leak search.
2133 The value <varname>increased</varname> specifies that only block
2134 allocation stacks with an increased number of leaked bytes or
2135 blocks since the previous leak check should be shown. The
2136 value <varname>changed</varname> specifies that allocation stacks
2137 with any change since the previous leak check should be shown.
2138 The value <varname>new</varname> specifies to show only the block
2139 allocation stacks that are new since the previous leak search.
2140 The value <varname>any</varname> specifies that all leak entries
2141 should be shown, regardless of any increase or decrease.
2142 If <varname>new</varname> or <varname>increased</varname> or
2143 <varname>changed</varname> are specified, the leak report entries will show
2144 the delta relative to the previous leak report and the new loss records
2145 will have a "new" marker (even when <varname>increased</varname> or
2146 <varname>changed</varname> were specified).
2149 <para>The following example shows usage of the
2150 <varname>leak_check</varname> monitor command on
2151 the <varname>memcheck/tests/leak-cases.c</varname> regression
2152 test. The first command outputs one entry having an increase in
2153 the leaked bytes. The second command is the same as the first
2154 command, but uses the abbreviated forms accepted by GDB and the
2155 Valgrind gdbserver. It only outputs the summary information, as
2156 there was no increase since the previous leak search.</para>
2157 <programlisting><![CDATA[
2158 (gdb) monitor leak_check full possibleleak increased
2159 ==19520== 16 (+16) bytes in 1 (+1) blocks are possibly lost in new loss record 9 of 12
2160 ==19520== at 0x40070B4: malloc (vg_replace_malloc.c:263)
2161 ==19520== by 0x80484D5: mk (leak-cases.c:52)
2162 ==19520== by 0x804855F: f (leak-cases.c:81)
2163 ==19520== by 0x80488E0: main (leak-cases.c:107)
2165 ==19520== LEAK SUMMARY:
2166 ==19520== definitely lost: 32 (+0) bytes in 2 (+0) blocks
2167 ==19520== indirectly lost: 16 (+0) bytes in 1 (+0) blocks
2168 ==19520== possibly lost: 32 (+16) bytes in 2 (+1) blocks
2169 ==19520== still reachable: 96 (+16) bytes in 6 (+1) blocks
2170 ==19520== suppressed: 0 (+0) bytes in 0 (+0) blocks
2171 ==19520== Reachable blocks (those to which a pointer was found) are not shown.
2172 ==19520== To see them, add 'reachable any' args to leak_check
2175 ==19520== LEAK SUMMARY:
2176 ==19520== definitely lost: 32 (+0) bytes in 2 (+0) blocks
2177 ==19520== indirectly lost: 16 (+0) bytes in 1 (+0) blocks
2178 ==19520== possibly lost: 32 (+0) bytes in 2 (+0) blocks
2179 ==19520== still reachable: 96 (+0) bytes in 6 (+0) blocks
2180 ==19520== suppressed: 0 (+0) bytes in 0 (+0) blocks
2181 ==19520== Reachable blocks (those to which a pointer was found) are not shown.
2182 ==19520== To see them, add 'reachable any' args to leak_check
2185 ]]></programlisting>
2186 <para>Note that when using Valgrind's gdbserver, it is not
2188 with <option>--leak-check=full</option>
2189 <option>--show-reachable=yes</option> to see the reachable
2190 blocks. You can obtain the same information without rerunning by
2191 using the GDB command <computeroutput>monitor leak_check full
2192 reachable any</computeroutput> (or, using
2193 abbreviation: <computeroutput>mo l f r a</computeroutput>).
2196 <para>The GDB equivalent memcheck front end command <varname>memcheck
2197 leak_check</varname> auto-completes the user input by providing the full
2198 list of keywords still relevant according to what is already typed. For
2199 example, if the "summary" keyword has been provided, the following TABs to
2200 auto-complete other items will not propose anymore "full" and "xtleak".
2201 Note that KIND and HEUR values are not part of auto-completed elements.
2207 <para><varname>block_list <loss_record_nr>|<loss_record_nr_from>..<loss_record_nr_to>
2208 [unlimited*|limited <max_blocks>]
2209 [heuristics heur1,heur2,...]
2211 shows the list of blocks belonging to
2212 <varname><loss_record_nr></varname> (or to the loss records range
2213 <varname><loss_record_nr_from>..<loss_record_nr_to></varname>).
2214 The nr of blocks to print can be controlled using the
2215 <varname>limited</varname> argument followed by the maximum nr
2216 of blocks to output.
2217 If one or more heuristics are given, only prints the loss records
2218 and blocks found via one of the given <varname>heur1,heur2,...</varname>
2222 <para> A leak search merges the allocated blocks in loss records :
2223 a loss record re-groups all blocks having the same state (for
2224 example, Definitely Lost) and the same allocation backtrace.
2225 Each loss record is identified in the leak search result
2226 by a loss record number.
2227 The <varname>block_list</varname> command shows the loss record information
2228 followed by the addresses and sizes of the blocks which have been
2229 merged in the loss record. If a block was found using an heuristic, the block size
2230 is followed by the heuristic.
2233 <para> If a directly lost block causes some other blocks to be indirectly
2234 lost, the block_list command will also show these indirectly lost blocks.
2235 The indirectly lost blocks will be indented according to the level of indirection
2236 between the directly lost block and the indirectly lost block(s).
2237 Each indirectly lost block is followed by the reference of its loss record.
2240 <para> The block_list command can be used on the results of a leak search as long
2241 as no block has been freed after this leak search: as soon as the program frees
2242 a block, a new leak search is needed before block_list can be used again.
2246 In the below example, the program leaks a tree structure by losing the pointer to
2247 the block A (top of the tree).
2248 So, the block A is directly lost, causing an indirect
2249 loss of blocks B to G. The first block_list command shows the loss record of A
2250 (a definitely lost block with address 0x4028028, size 16). The addresses and sizes
2251 of the indirectly lost blocks due to block A are shown below the block A.
2252 The second command shows the details of one of the indirect loss records output
2253 by the first command.
2255 <programlisting><![CDATA[
2261 ]]></programlisting>
2263 <programlisting><![CDATA[
2265 #0 main () at leak-tree.c:69
2266 (gdb) monitor leak_check full any
2267 ==19552== 112 (16 direct, 96 indirect) bytes in 1 blocks are definitely lost in loss record 7 of 7
2268 ==19552== at 0x40070B4: malloc (vg_replace_malloc.c:263)
2269 ==19552== by 0x80484D5: mk (leak-tree.c:28)
2270 ==19552== by 0x80484FC: f (leak-tree.c:41)
2271 ==19552== by 0x8048856: main (leak-tree.c:63)
2273 ==19552== LEAK SUMMARY:
2274 ==19552== definitely lost: 16 bytes in 1 blocks
2275 ==19552== indirectly lost: 96 bytes in 6 blocks
2276 ==19552== possibly lost: 0 bytes in 0 blocks
2277 ==19552== still reachable: 0 bytes in 0 blocks
2278 ==19552== suppressed: 0 bytes in 0 blocks
2280 (gdb) monitor block_list 7
2281 ==19552== 112 (16 direct, 96 indirect) bytes in 1 blocks are definitely lost in loss record 7 of 7
2282 ==19552== at 0x40070B4: malloc (vg_replace_malloc.c:263)
2283 ==19552== by 0x80484D5: mk (leak-tree.c:28)
2284 ==19552== by 0x80484FC: f (leak-tree.c:41)
2285 ==19552== by 0x8048856: main (leak-tree.c:63)
2286 ==19552== 0x4028028[16]
2287 ==19552== 0x4028068[16] indirect loss record 1
2288 ==19552== 0x40280E8[16] indirect loss record 3
2289 ==19552== 0x4028128[16] indirect loss record 4
2290 ==19552== 0x40280A8[16] indirect loss record 2
2291 ==19552== 0x4028168[16] indirect loss record 5
2292 ==19552== 0x40281A8[16] indirect loss record 6
2294 ==19552== 16 bytes in 1 blocks are indirectly lost in loss record 2 of 7
2295 ==19552== at 0x40070B4: malloc (vg_replace_malloc.c:263)
2296 ==19552== by 0x80484D5: mk (leak-tree.c:28)
2297 ==19552== by 0x8048519: f (leak-tree.c:43)
2298 ==19552== by 0x8048856: main (leak-tree.c:63)
2299 ==19552== 0x40280A8[16]
2300 ==19552== 0x4028168[16] indirect loss record 5
2301 ==19552== 0x40281A8[16] indirect loss record 6
2304 ]]></programlisting>
2309 <para><varname>who_points_at <addr> [<len>]</varname>
2310 shows all the locations where a pointer to addr is found.
2311 If len is equal to 1, the command only shows the locations pointing
2312 exactly at addr (i.e. the "start pointers" to addr).
2313 If len is > 1, "interior pointers" pointing at the len first bytes
2317 <para>The locations searched for are the same as the locations
2318 used in the leak search. So, <varname>who_points_at</varname> can a.o.
2319 be used to show why the leak search still can reach a block, or can
2320 search for dangling pointers to a freed block.
2321 Each location pointing at addr (or pointing inside addr if interior pointers
2322 are being searched for) will be described.
2325 <para>The GDB equivalent memcheck front end command <varname>memcheck
2326 who_points_at ADDR [LEN]</varname> accept any address expression for its
2327 first ADDR argument. The second optional argument is any integer
2328 expression. Note that these 2 arguments must be separated by a space.
2331 <para>In the below example, the pointers to the 'tree block A' (see example
2332 in command <varname>block_list</varname>) is shown before the tree was leaked.
2333 The descriptions are detailed as the option <option>--read-var-info=yes</option>
2334 was given at Valgrind startup. The second call shows the pointers (start and interior
2335 pointers) to block G. The block G (0x40281A8) is reachable via block C (0x40280a8)
2336 and register ECX of tid 1 (tid is the Valgrind thread id).
2337 It is "interior reachable" via the register EBX.
2340 <programlisting><![CDATA[
2341 (gdb) monitor who_points_at 0x4028028
2342 ==20852== Searching for pointers to 0x4028028
2343 ==20852== *0x8049e20 points at 0x4028028
2344 ==20852== Location 0x8049e20 is 0 bytes inside global var "t"
2345 ==20852== declared at leak-tree.c:35
2346 (gdb) monitor who_points_at 0x40281A8 16
2347 ==20852== Searching for pointers pointing in 16 bytes from 0x40281a8
2348 ==20852== *0x40280ac points at 0x40281a8
2349 ==20852== Address 0x40280ac is 4 bytes inside a block of size 16 alloc'd
2350 ==20852== at 0x40070B4: malloc (vg_replace_malloc.c:263)
2351 ==20852== by 0x80484D5: mk (leak-tree.c:28)
2352 ==20852== by 0x8048519: f (leak-tree.c:43)
2353 ==20852== by 0x8048856: main (leak-tree.c:63)
2354 ==20852== tid 1 register ECX points at 0x40281a8
2355 ==20852== tid 1 register EBX interior points at 2 bytes inside 0x40281a8
2357 ]]></programlisting>
2359 <para> When <varname>who_points_at</varname> finds an interior pointer,
2360 it will report the heuristic(s) with which this interior pointer
2361 will be considered as reachable. Note that this is done independently
2362 of the value of the option <option>--leak-check-heuristics</option>.
2363 In the below example, the loss record 6 indicates a possibly lost
2364 block. <varname>who_points_at</varname> reports that there is an interior
2365 pointer pointing in this block, and that the block can be considered
2366 reachable using the heuristic
2367 <computeroutput>multipleinheritance</computeroutput>.
2370 <programlisting><![CDATA[
2371 (gdb) monitor block_list 6
2372 ==3748== 8 bytes in 1 blocks are possibly lost in loss record 6 of 7
2373 ==3748== at 0x4007D77: operator new(unsigned int) (vg_replace_malloc.c:313)
2374 ==3748== by 0x8048954: main (leak_cpp_interior.cpp:43)
2375 ==3748== 0x402A0E0[8]
2376 (gdb) monitor who_points_at 0x402A0E0 8
2377 ==3748== Searching for pointers pointing in 8 bytes from 0x402a0e0
2378 ==3748== *0xbe8ee078 interior points at 4 bytes inside 0x402a0e0
2379 ==3748== Address 0xbe8ee078 is on thread 1's stack
2380 ==3748== block at 0x402a0e0 considered reachable by ptr 0x402a0e4 using multipleinheritance heuristic
2382 ]]></programlisting>
2387 <para><varname>xtmemory [<filename> default xtmemory.kcg.%p.%n]</varname>
2388 requests Memcheck tool to produce an xtree heap memory report.
2389 See <xref linkend="&vg-xtree-id;"/> for
2390 a detailed explanation about execution trees. </para>
2397 <sect1 id="mc-manual.clientreqs" xreflabel="Client requests">
2398 <title>Client Requests</title>
2400 <para>The following client requests are defined in
2401 <filename>memcheck.h</filename>.
2402 See <filename>memcheck.h</filename> for exact details of their
2408 <para><varname>VALGRIND_MAKE_MEM_NOACCESS</varname>,
2409 <varname>VALGRIND_MAKE_MEM_UNDEFINED</varname> and
2410 <varname>VALGRIND_MAKE_MEM_DEFINED</varname>.
2411 These mark address ranges as completely inaccessible,
2412 accessible but containing undefined data, and accessible and
2413 containing defined data, respectively. They return -1, when
2414 run on Valgrind and 0 otherwise.</para>
2418 <para><varname>VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE</varname>.
2419 This is just like <varname>VALGRIND_MAKE_MEM_DEFINED</varname> but only
2420 affects those bytes that are already addressable.</para>
2424 <para><varname>VALGRIND_CHECK_MEM_IS_ADDRESSABLE</varname> and
2425 <varname>VALGRIND_CHECK_MEM_IS_DEFINED</varname>: check immediately
2426 whether or not the given address range has the relevant property,
2427 and if not, print an error message. Also, for the convenience of
2428 the client, returns zero if the relevant property holds; otherwise,
2429 the returned value is the address of the first byte for which the
2430 property is not true. Always returns 0 when not run on
2435 <para><varname>VALGRIND_CHECK_VALUE_IS_DEFINED</varname>: a quick and easy
2436 way to find out whether Valgrind thinks a particular value
2437 (lvalue, to be precise) is addressable and defined. Prints an error
2438 message if not. It has no return value.</para>
2442 <para><varname>VALGRIND_DO_LEAK_CHECK</varname>: does a full memory leak
2443 check (like <option>--leak-check=full</option>) right now.
2444 This is useful for incrementally checking for leaks between arbitrary
2445 places in the program's execution. It has no return value.</para>
2449 <para><varname>VALGRIND_DO_ADDED_LEAK_CHECK</varname>: same as
2450 <varname> VALGRIND_DO_LEAK_CHECK</varname> but only shows the
2451 entries for which there was an increase in leaked bytes or leaked
2452 number of blocks since the previous leak search. It has no return
2457 <para><varname>VALGRIND_DO_CHANGED_LEAK_CHECK</varname>: same as
2458 <varname>VALGRIND_DO_LEAK_CHECK</varname> but only shows the
2459 entries for which there was an increase or decrease in leaked
2460 bytes or leaked number of blocks since the previous leak search. It
2461 has no return value.</para>
2465 <para><varname>VALGRIND_DO_NEW_LEAK_CHECK</varname>: same as
2466 <varname> VALGRIND_DO_LEAK_CHECK</varname> but only shows the new
2467 entries since the previous leak search. It has no return value.</para>
2471 <para><varname>VALGRIND_DO_QUICK_LEAK_CHECK</varname>: like
2472 <varname>VALGRIND_DO_LEAK_CHECK</varname>, except it produces only a leak
2473 summary (like <option>--leak-check=summary</option>).
2474 It has no return value.</para>
2478 <para><varname>VALGRIND_COUNT_LEAKS</varname>: fills in the four
2479 arguments with the number of bytes of memory found by the previous
2480 leak check to be leaked (i.e. the sum of direct leaks and indirect leaks),
2481 dubious, reachable and suppressed. This is useful in test harness code,
2482 after calling <varname>VALGRIND_DO_LEAK_CHECK</varname> or
2483 <varname>VALGRIND_DO_QUICK_LEAK_CHECK</varname>.</para>
2487 <para><varname>VALGRIND_COUNT_LEAK_BLOCKS</varname>: identical to
2488 <varname>VALGRIND_COUNT_LEAKS</varname> except that it returns the
2489 number of blocks rather than the number of bytes in each
2494 <para><varname>VALGRIND_GET_VBITS</varname> and
2495 <varname>VALGRIND_SET_VBITS</varname>: allow you to get and set the
2496 V (validity) bits for an address range. You should probably only
2497 set V bits that you have got with
2498 <varname>VALGRIND_GET_VBITS</varname>. Only for those who really
2499 know what they are doing.</para>
2503 <para><varname>VALGRIND_CREATE_BLOCK</varname> and
2504 <varname>VALGRIND_DISCARD</varname>. <varname>VALGRIND_CREATE_BLOCK</varname>
2505 takes an address, a number of bytes and a character string. The
2506 specified address range is then associated with that string. When
2507 Memcheck reports an invalid access to an address in the range, it
2508 will describe it in terms of this block rather than in terms of
2509 any other block it knows about. Note that the use of this macro
2510 does not actually change the state of memory in any way -- it
2511 merely gives a name for the range.
2514 <para>At some point you may want Memcheck to stop reporting errors
2515 in terms of the block named
2516 by <varname>VALGRIND_CREATE_BLOCK</varname>. To make this
2517 possible, <varname>VALGRIND_CREATE_BLOCK</varname> returns a
2518 "block handle", which is a C <varname>int</varname> value. You
2519 can pass this block handle to <varname>VALGRIND_DISCARD</varname>.
2520 After doing so, Valgrind will no longer relate addressing errors
2521 in the specified range to the block. Passing invalid handles to
2522 <varname>VALGRIND_DISCARD</varname> is harmless.
2533 <sect1 id="mc-manual.mempools" xreflabel="Memory Pools">
2534 <title>Memory Pools: describing and working with custom allocators</title>
2536 <para>Some programs use custom memory allocators, often for performance
2537 reasons. Left to itself, Memcheck is unable to understand the
2538 behaviour of custom allocation schemes as well as it understands the
2539 standard allocators, and so may miss errors and leaks in your program. What
2540 this section describes is a way to give Memcheck enough of a description of
2541 your custom allocator that it can make at least some sense of what is
2544 <para>There are many different sorts of custom allocator, so Memcheck
2545 attempts to reason about them using a loose, abstract model. We
2546 use the following terminology when describing custom allocation
2551 <para>Custom allocation involves a set of independent "memory pools".
2555 <para>Memcheck's notion of a a memory pool consists of a single "anchor
2556 address" and a set of non-overlapping "chunks" associated with the
2557 anchor address.</para>
2560 <para>Typically a pool's anchor address is the address of a
2561 book-keeping "header" structure.</para>
2564 <para>Typically the pool's chunks are drawn from a contiguous
2565 "superblock" acquired through the system
2566 <function>malloc</function> or
2567 <function>mmap</function>.</para>
2572 <para>Keep in mind that the last two points above say "typically": the
2573 Valgrind mempool client request API is intentionally vague about the
2574 exact structure of a mempool. There is no specific mention made of
2575 headers or superblocks. Nevertheless, the following picture may help
2576 elucidate the intention of the terms in the API:</para>
2578 <programlisting><![CDATA[
2588 +------+---+--------------+---+------------------+
2589 | |rzB| allocation |rzB| |
2590 +------+---+--------------+---+------------------+
2593 "addr" "addr"+"size"
2594 ]]></programlisting>
2597 Note that the header and the superblock may be contiguous or
2598 discontiguous, and there may be multiple superblocks associated with a
2599 single header; such variations are opaque to Memcheck. The API
2600 only requires that your allocation scheme can present sensible values
2601 of "pool", "addr" and "size".</para>
2604 Typically, before making client requests related to mempools, a client
2605 program will have allocated such a header and superblock for their
2606 mempool, and marked the superblock NOACCESS using the
2607 <varname>VALGRIND_MAKE_MEM_NOACCESS</varname> client request.</para>
2610 When dealing with mempools, the goal is to maintain a particular
2611 invariant condition: that Memcheck believes the unallocated portions
2612 of the pool's superblock (including redzones) are NOACCESS. To
2613 maintain this invariant, the client program must ensure that the
2614 superblock starts out in that state; Memcheck cannot make it so, since
2615 Memcheck never explicitly learns about the superblock of a pool, only
2616 the allocated chunks within the pool.</para>
2619 Once the header and superblock for a pool are established and properly
2620 marked, there are a number of client requests programs can use to
2621 inform Memcheck about changes to the state of a mempool:</para>
2627 <varname>VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed)</varname>:
2628 This request registers the address <varname>pool</varname> as the anchor
2629 address for a memory pool. It also provides a size
2630 <varname>rzB</varname>, specifying how large the redzones placed around
2631 chunks allocated from the pool should be. Finally, it provides an
2632 <varname>is_zeroed</varname> argument that specifies whether the pool's
2633 chunks are zeroed (more precisely: defined) when allocated.
2636 Upon completion of this request, no chunks are associated with the
2637 pool. The request simply tells Memcheck that the pool exists, so that
2638 subsequent calls can refer to it as a pool.
2643 <!-- Note: the below is mostly a copy of valgrind.h. Keep in sync! -->
2645 <varname>VALGRIND_CREATE_MEMPOOL_EXT(pool, rzB, is_zeroed, flags)</varname>:
2646 Create a memory pool with some flags (that can
2647 be OR-ed together) specifying extended behaviour. When flags is
2648 zero, the behaviour is identical to
2649 <varname>VALGRIND_CREATE_MEMPOOL</varname>.</para>
2652 <para> The flag <varname>VALGRIND_MEMPOOL_METAPOOL</varname>
2653 specifies that the pieces of memory associated with the pool
2654 using <varname>VALGRIND_MEMPOOL_ALLOC</varname> will be used
2655 by the application as superblocks to dole out MALLOC_LIKE
2656 blocks using <varname>VALGRIND_MALLOCLIKE_BLOCK</varname>.
2657 In other words, a meta pool is a "2 levels" pool : first
2658 level is the blocks described
2659 by <varname>VALGRIND_MEMPOOL_ALLOC</varname>. The second
2660 level blocks are described
2661 using <varname>VALGRIND_MALLOCLIKE_BLOCK</varname>. Note
2662 that the association between the pool and the second level
2663 blocks is implicit : second level blocks will be located
2664 inside first level blocks. It is necessary to use
2665 the <varname>VALGRIND_MEMPOOL_METAPOOL</varname> flag for
2666 such 2 levels pools, as otherwise valgrind will detect
2667 overlapping memory blocks, and will abort execution
2668 (e.g. during leak search).
2673 <varname>VALGRIND_MEMPOOL_AUTO_FREE</varname>. Such a meta
2674 pool can also be marked as an 'auto free' pool using the
2675 flag <varname>VALGRIND_MEMPOOL_AUTO_FREE</varname>, which
2676 must be OR-ed together with
2677 the <varname>VALGRIND_MEMPOOL_METAPOOL</varname>. For an
2678 'auto free' pool, <varname>VALGRIND_MEMPOOL_FREE</varname>
2679 will automatically free the second level blocks that are
2680 contained inside the first level block freed
2681 with <varname>VALGRIND_MEMPOOL_FREE</varname>. In other
2682 words, calling <varname>VALGRIND_MEMPOOL_FREE</varname> will
2683 cause implicit calls
2684 to <varname>VALGRIND_FREELIKE_BLOCK</varname> for all the
2685 second level blocks included in the first level block.
2686 Note: it is an error to use
2687 the <varname>VALGRIND_MEMPOOL_AUTO_FREE</varname> flag
2689 <varname>VALGRIND_MEMPOOL_METAPOOL</varname> flag.
2696 <para><varname>VALGRIND_DESTROY_MEMPOOL(pool)</varname>:
2697 This request tells Memcheck that a pool is being torn down. Memcheck
2698 then removes all records of chunks associated with the pool, as well
2699 as its record of the pool's existence. While destroying its records of
2700 a mempool, Memcheck resets the redzones of any live chunks in the pool
2706 <para><varname>VALGRIND_MEMPOOL_ALLOC(pool, addr, size)</varname>:
2707 This request informs Memcheck that a <varname>size</varname>-byte chunk
2708 has been allocated at <varname>addr</varname>, and associates the chunk with the
2710 <varname>pool</varname>. If the pool was created with nonzero
2711 <varname>rzB</varname> redzones, Memcheck will mark the
2712 <varname>rzB</varname> bytes before and after the chunk as NOACCESS. If
2713 the pool was created with the <varname>is_zeroed</varname> argument set,
2714 Memcheck will mark the chunk as DEFINED, otherwise Memcheck will mark
2715 the chunk as UNDEFINED.
2720 <para><varname>VALGRIND_MEMPOOL_FREE(pool, addr)</varname>:
2721 This request informs Memcheck that the chunk at <varname>addr</varname>
2722 should no longer be considered allocated. Memcheck will mark the chunk
2723 associated with <varname>addr</varname> as NOACCESS, and delete its
2724 record of the chunk's existence.
2729 <para><varname>VALGRIND_MEMPOOL_TRIM(pool, addr, size)</varname>:
2730 This request trims the chunks associated with <varname>pool</varname>.
2731 The request only operates on chunks associated with
2732 <varname>pool</varname>. Trimming is formally defined as:</para>
2735 <para> All chunks entirely inside the range
2736 <varname>addr..(addr+size-1)</varname> are preserved.</para>
2739 <para>All chunks entirely outside the range
2740 <varname>addr..(addr+size-1)</varname> are discarded, as though
2741 <varname>VALGRIND_MEMPOOL_FREE</varname> was called on them. </para>
2744 <para>All other chunks must intersect with the range
2745 <varname>addr..(addr+size-1)</varname>; areas outside the
2746 intersection are marked as NOACCESS, as though they had been
2747 independently freed with
2748 <varname>VALGRIND_MEMPOOL_FREE</varname>.</para>
2751 <para>This is a somewhat rare request, but can be useful in
2752 implementing the type of mass-free operations common in custom
2753 LIFO allocators.</para>
2757 <para><varname>VALGRIND_MOVE_MEMPOOL(poolA, poolB)</varname>: This
2758 request informs Memcheck that the pool previously anchored at
2759 address <varname>poolA</varname> has moved to anchor address
2760 <varname>poolB</varname>. This is a rare request, typically only needed
2761 if you <function>realloc</function> the header of a mempool.</para>
2762 <para>No memory-status bits are altered by this request.</para>
2767 <varname>VALGRIND_MEMPOOL_CHANGE(pool, addrA, addrB,
2768 size)</varname>: This request informs Memcheck that the chunk
2769 previously allocated at address <varname>addrA</varname> within
2770 <varname>pool</varname> has been moved and/or resized, and should be
2771 changed to cover the region <varname>addrB..(addrB+size-1)</varname>. This
2772 is a rare request, typically only needed if you
2773 <function>realloc</function> a superblock or wish to extend a chunk
2774 without changing its memory-status bits.
2776 <para>No memory-status bits are altered by this request.
2781 <para><varname>VALGRIND_MEMPOOL_EXISTS(pool)</varname>:
2782 This request informs the caller whether or not Memcheck is currently
2783 tracking a mempool at anchor address <varname>pool</varname>. It
2784 evaluates to 1 when there is a mempool associated with that address, 0
2785 otherwise. This is a rare request, only useful in circumstances when
2786 client code might have lost track of the set of active mempools.
2800 <sect1 id="mc-manual.mpiwrap" xreflabel="MPI Wrappers">
2801 <title>Debugging MPI Parallel Programs with Valgrind</title>
2803 <para>Memcheck supports debugging of distributed-memory applications
2804 which use the MPI message passing standard. This support consists of a
2805 library of wrapper functions for the
2806 <computeroutput>PMPI_*</computeroutput> interface. When incorporated
2807 into the application's address space, either by direct linking or by
2808 <computeroutput>LD_PRELOAD</computeroutput>, the wrappers intercept
2809 calls to <computeroutput>PMPI_Send</computeroutput>,
2810 <computeroutput>PMPI_Recv</computeroutput>, etc. They then
2811 use client requests to inform Memcheck of memory state changes caused
2812 by the function being wrapped. This reduces the number of false
2813 positives that Memcheck otherwise typically reports for MPI
2814 applications.</para>
2816 <para>The wrappers also take the opportunity to carefully check
2817 size and definedness of buffers passed as arguments to MPI functions, hence
2818 detecting errors such as passing undefined data to
2819 <computeroutput>PMPI_Send</computeroutput>, or receiving data into a
2820 buffer which is too small.</para>
2822 <para>Unlike most of the rest of Valgrind, the wrapper library is subject to a
2823 BSD-style license, so you can link it into any code base you like.
2824 See the top of <computeroutput>mpi/libmpiwrap.c</computeroutput>
2825 for license details.</para>
2828 <sect2 id="mc-manual.mpiwrap.build" xreflabel="Building MPI Wrappers">
2829 <title>Building and installing the wrappers</title>
2831 <para> The wrapper library will be built automatically if possible.
2832 Valgrind's configure script will look for a suitable
2833 <computeroutput>mpicc</computeroutput> to build it with. This must be
2834 the same <computeroutput>mpicc</computeroutput> you use to build the
2835 MPI application you want to debug. By default, Valgrind tries
2836 <computeroutput>mpicc</computeroutput>, but you can specify a
2837 different one by using the configure-time option
2838 <option>--with-mpicc</option>. Currently the
2839 wrappers are only buildable with
2840 <computeroutput>mpicc</computeroutput>s which are based on GNU
2841 GCC or Intel's C++ Compiler.</para>
2843 <para>Check that the configure script prints a line like this:</para>
2845 <programlisting><![CDATA[
2846 checking for usable MPI2-compliant mpicc and mpi.h... yes, mpicc
2847 ]]></programlisting>
2849 <para>If it says <computeroutput>... no</computeroutput>, your
2850 <computeroutput>mpicc</computeroutput> has failed to compile and link
2851 a test MPI2 program.</para>
2853 <para>If the configure test succeeds, continue in the usual way with
2854 <computeroutput>make</computeroutput> and <computeroutput>make
2855 install</computeroutput>. The final install tree should then contain
2856 <computeroutput>libmpiwrap-<platform>.so</computeroutput>.
2859 <para>Compile up a test MPI program (eg, MPI hello-world) and try
2862 <programlisting><![CDATA[
2863 LD_PRELOAD=$prefix/lib/valgrind/libmpiwrap-<platform>.so \
2864 mpirun [args] $prefix/bin/valgrind ./hello
2865 ]]></programlisting>
2867 <para>You should see something similar to the following</para>
2869 <programlisting><![CDATA[
2870 valgrind MPI wrappers 31901: Active for pid 31901
2871 valgrind MPI wrappers 31901: Try MPIWRAP_DEBUG=help for possible options
2872 ]]></programlisting>
2874 <para>repeated for every process in the group. If you do not see
2875 these, there is an build/installation problem of some kind.</para>
2877 <para> The MPI functions to be wrapped are assumed to be in an ELF
2878 shared object with soname matching
2879 <computeroutput>libmpi.so*</computeroutput>. This is known to be
2880 correct at least for Open MPI and Quadrics MPI, and can easily be
2881 changed if required.</para>
2885 <sect2 id="mc-manual.mpiwrap.gettingstarted"
2886 xreflabel="Getting started with MPI Wrappers">
2887 <title>Getting started</title>
2889 <para>Compile your MPI application as usual, taking care to link it
2890 using the same <computeroutput>mpicc</computeroutput> that your
2891 Valgrind build was configured with.</para>
2894 Use the following basic scheme to run your application on Valgrind with
2895 the wrappers engaged:</para>
2897 <programlisting><![CDATA[
2898 MPIWRAP_DEBUG=[wrapper-args] \
2899 LD_PRELOAD=$prefix/lib/valgrind/libmpiwrap-<platform>.so \
2900 mpirun [mpirun-args] \
2901 $prefix/bin/valgrind [valgrind-args] \
2902 [application] [app-args]
2903 ]]></programlisting>
2905 <para>As an alternative to
2906 <computeroutput>LD_PRELOAD</computeroutput>ing
2907 <computeroutput>libmpiwrap-<platform>.so</computeroutput>, you can
2908 simply link it to your application if desired. This should not disturb
2909 native behaviour of your application in any way.</para>
2913 <sect2 id="mc-manual.mpiwrap.controlling"
2914 xreflabel="Controlling the MPI Wrappers">
2915 <title>Controlling the wrapper library</title>
2917 <para>Environment variable
2918 <computeroutput>MPIWRAP_DEBUG</computeroutput> is consulted at
2919 startup. The default behaviour is to print a starting banner</para>
2921 <programlisting><![CDATA[
2922 valgrind MPI wrappers 16386: Active for pid 16386
2923 valgrind MPI wrappers 16386: Try MPIWRAP_DEBUG=help for possible options
2924 ]]></programlisting>
2926 <para> and then be relatively quiet.</para>
2928 <para>You can give a list of comma-separated options in
2929 <computeroutput>MPIWRAP_DEBUG</computeroutput>. These are</para>
2933 <para><computeroutput>verbose</computeroutput>:
2934 show entries/exits of all wrappers. Also show extra
2935 debugging info, such as the status of outstanding
2936 <computeroutput>MPI_Request</computeroutput>s resulting
2937 from uncompleted <computeroutput>MPI_Irecv</computeroutput>s.</para>
2940 <para><computeroutput>quiet</computeroutput>:
2941 opposite of <computeroutput>verbose</computeroutput>, only print
2942 anything when the wrappers want
2943 to report a detected programming error, or in case of catastrophic
2944 failure of the wrappers.</para>
2947 <para><computeroutput>warn</computeroutput>:
2948 by default, functions which lack proper wrappers
2949 are not commented on, just silently
2950 ignored. This causes a warning to be printed for each unwrapped
2951 function used, up to a maximum of three warnings per function.</para>
2954 <para><computeroutput>strict</computeroutput>:
2955 print an error message and abort the program if
2956 a function lacking a wrapper is used.</para>
2960 <para> If you want to use Valgrind's XML output facility
2961 (<option>--xml=yes</option>), you should pass
2962 <computeroutput>quiet</computeroutput> in
2963 <computeroutput>MPIWRAP_DEBUG</computeroutput> so as to get rid of any
2964 extraneous printing from the wrappers.</para>
2969 <sect2 id="mc-manual.mpiwrap.limitations.functions"
2970 xreflabel="Functions: Abilities and Limitations">
2971 <title>Functions</title>
2973 <para>All MPI2 functions except
2974 <computeroutput>MPI_Wtick</computeroutput>,
2975 <computeroutput>MPI_Wtime</computeroutput> and
2976 <computeroutput>MPI_Pcontrol</computeroutput> have wrappers. The
2977 first two are not wrapped because they return a
2978 <computeroutput>double</computeroutput>, which Valgrind's
2979 function-wrap mechanism cannot handle (but it could easily be
2980 extended to do so). <computeroutput>MPI_Pcontrol</computeroutput> cannot be
2981 wrapped as it has variable arity:
2982 <computeroutput>int MPI_Pcontrol(const int level, ...)</computeroutput></para>
2984 <para>Most functions are wrapped with a default wrapper which does
2985 nothing except complain or abort if it is called, depending on
2986 settings in <computeroutput>MPIWRAP_DEBUG</computeroutput> listed
2987 above. The following functions have "real", do-something-useful
2990 <programlisting><![CDATA[
2991 PMPI_Send PMPI_Bsend PMPI_Ssend PMPI_Rsend
2993 PMPI_Recv PMPI_Get_count
2995 PMPI_Isend PMPI_Ibsend PMPI_Issend PMPI_Irsend
2998 PMPI_Wait PMPI_Waitall
2999 PMPI_Test PMPI_Testall
3001 PMPI_Iprobe PMPI_Probe
3007 PMPI_Type_commit PMPI_Type_free
3009 PMPI_Pack PMPI_Unpack
3011 PMPI_Bcast PMPI_Gather PMPI_Scatter PMPI_Alltoall
3012 PMPI_Reduce PMPI_Allreduce PMPI_Op_create
3014 PMPI_Comm_create PMPI_Comm_dup PMPI_Comm_free PMPI_Comm_rank PMPI_Comm_size
3017 PMPI_Init PMPI_Initialized PMPI_Finalize
3018 ]]></programlisting>
3020 <para> A few functions such as
3021 <computeroutput>PMPI_Address</computeroutput> are listed as
3022 <computeroutput>HAS_NO_WRAPPER</computeroutput>. They have no wrapper
3023 at all as there is nothing worth checking, and giving a no-op wrapper
3024 would reduce performance for no reason.</para>
3026 <para> Note that the wrapper library itself can itself generate large
3027 numbers of calls to the MPI implementation, especially when walking
3028 complex types. The most common functions called are
3029 <computeroutput>PMPI_Extent</computeroutput>,
3030 <computeroutput>PMPI_Type_get_envelope</computeroutput>,
3031 <computeroutput>PMPI_Type_get_contents</computeroutput>, and
3032 <computeroutput>PMPI_Type_free</computeroutput>. </para>
3035 <sect2 id="mc-manual.mpiwrap.limitations.types"
3036 xreflabel="Types: Abilities and Limitations">
3037 <title>Types</title>
3039 <para> MPI-1.1 structured types are supported, and walked exactly.
3040 The currently supported combiners are
3041 <computeroutput>MPI_COMBINER_NAMED</computeroutput>,
3042 <computeroutput>MPI_COMBINER_CONTIGUOUS</computeroutput>,
3043 <computeroutput>MPI_COMBINER_VECTOR</computeroutput>,
3044 <computeroutput>MPI_COMBINER_HVECTOR</computeroutput>
3045 <computeroutput>MPI_COMBINER_INDEXED</computeroutput>,
3046 <computeroutput>MPI_COMBINER_HINDEXED</computeroutput> and
3047 <computeroutput>MPI_COMBINER_STRUCT</computeroutput>. This should
3048 cover all MPI-1.1 types. The mechanism (function
3049 <computeroutput>walk_type</computeroutput>) should extend easily to
3050 cover MPI2 combiners.</para>
3052 <para>MPI defines some named structured types
3053 (<computeroutput>MPI_FLOAT_INT</computeroutput>,
3054 <computeroutput>MPI_DOUBLE_INT</computeroutput>,
3055 <computeroutput>MPI_LONG_INT</computeroutput>,
3056 <computeroutput>MPI_2INT</computeroutput>,
3057 <computeroutput>MPI_SHORT_INT</computeroutput>,
3058 <computeroutput>MPI_LONG_DOUBLE_INT</computeroutput>) which are pairs
3059 of some basic type and a C <computeroutput>int</computeroutput>.
3060 Unfortunately the MPI specification makes it impossible to look inside
3061 these types and see where the fields are. Therefore these wrappers
3062 assume the types are laid out as <computeroutput>struct { float val;
3063 int loc; }</computeroutput> (for
3064 <computeroutput>MPI_FLOAT_INT</computeroutput>), etc, and act
3065 accordingly. This appears to be correct at least for Open MPI 1.0.2
3066 and for Quadrics MPI.</para>
3068 <para>If <computeroutput>strict</computeroutput> is an option specified
3069 in <computeroutput>MPIWRAP_DEBUG</computeroutput>, the application
3070 will abort if an unhandled type is encountered. Otherwise, the
3071 application will print a warning message and continue.</para>
3073 <para>Some effort is made to mark/check memory ranges corresponding to
3074 arrays of values in a single pass. This is important for performance
3075 since asking Valgrind to mark/check any range, no matter how small,
3076 carries quite a large constant cost. This optimisation is applied to
3077 arrays of primitive types (<computeroutput>double</computeroutput>,
3078 <computeroutput>float</computeroutput>,
3079 <computeroutput>int</computeroutput>,
3080 <computeroutput>long</computeroutput>, <computeroutput>long
3081 long</computeroutput>, <computeroutput>short</computeroutput>,
3082 <computeroutput>char</computeroutput>, and <computeroutput>long
3083 double</computeroutput> on platforms where <computeroutput>sizeof(long
3084 double) == 8</computeroutput>). For arrays of all other types, the
3085 wrappers handle each element individually and so there can be a very
3086 large performance cost.</para>
3091 <sect2 id="mc-manual.mpiwrap.writingwrappers"
3092 xreflabel="Writing new MPI Wrappers">
3093 <title>Writing new wrappers</title>
3096 For the most part the wrappers are straightforward. The only
3097 significant complexity arises with nonblocking receives.</para>
3099 <para>The issue is that <computeroutput>MPI_Irecv</computeroutput>
3100 states the recv buffer and returns immediately, giving a handle
3101 (<computeroutput>MPI_Request</computeroutput>) for the transaction.
3102 Later the user will have to poll for completion with
3103 <computeroutput>MPI_Wait</computeroutput> etc, and when the
3104 transaction completes successfully, the wrappers have to paint the
3105 recv buffer. But the recv buffer details are not presented to
3106 <computeroutput>MPI_Wait</computeroutput> -- only the handle is. The
3107 library therefore maintains a shadow table which associates
3108 uncompleted <computeroutput>MPI_Request</computeroutput>s with the
3109 corresponding buffer address/count/type. When an operation completes,
3110 the table is searched for the associated address/count/type info, and
3111 memory is marked accordingly.</para>
3113 <para>Access to the table is guarded by a (POSIX pthreads) lock, so as
3114 to make the library thread-safe.</para>
3116 <para>The table is allocated with
3117 <computeroutput>malloc</computeroutput> and never
3118 <computeroutput>free</computeroutput>d, so it will show up in leak
3121 <para>Writing new wrappers should be fairly easy. The source file is
3122 <computeroutput>mpi/libmpiwrap.c</computeroutput>. If possible,
3123 find an existing wrapper for a function of similar behaviour to the
3124 one you want to wrap, and use it as a starting point. The wrappers
3125 are organised in sections in the same order as the MPI 1.1 spec, to
3126 aid navigation. When adding a wrapper, remember to comment out the
3127 definition of the default wrapper in the long list of defaults at the
3128 bottom of the file (do not remove it, just comment it out).</para>
3131 <sect2 id="mc-manual.mpiwrap.whattoexpect"
3132 xreflabel="What to expect with MPI Wrappers">
3133 <title>What to expect when using the wrappers</title>
3135 <para>The wrappers should reduce Memcheck's false-error rate on MPI
3136 applications. Because the wrapping is done at the MPI interface,
3137 there will still potentially be a large number of errors reported in
3138 the MPI implementation below the interface. The best you can do is
3139 try to suppress them.</para>
3141 <para>You may also find that the input-side (buffer
3142 length/definedness) checks find errors in your MPI use, for example
3143 passing too short a buffer to
3144 <computeroutput>MPI_Recv</computeroutput>.</para>
3146 <para>Functions which are not wrapped may increase the false
3147 error rate. A possible approach is to run with
3148 <computeroutput>MPI_DEBUG</computeroutput> containing
3149 <computeroutput>warn</computeroutput>. This will show you functions
3150 which lack proper wrappers but which are nevertheless used. You can
3151 then write wrappers for them.
3154 <para>A known source of potential false errors are the
3155 <computeroutput>PMPI_Reduce</computeroutput> family of functions, when
3156 using a custom (user-defined) reduction function. In a reduction
3157 operation, each node notionally sends data to a "central point" which
3158 uses the specified reduction function to merge the data items into a
3159 single item. Hence, in general, data is passed between nodes and fed
3160 to the reduction function, but the wrapper library cannot mark the
3161 transferred data as initialised before it is handed to the reduction
3162 function, because all that happens "inside" the
3163 <computeroutput>PMPI_Reduce</computeroutput> call. As a result you
3164 may see false positives reported in your reduction function.</para>