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.</para>
323 <para>Most of the time in C++ you will write code that
324 uses <function>new expression</function> and <function>delete
325 expression</function>
326 (see <ulink url="https://en.cppreference.com/w/cpp/language/new">cppreference
327 new expression</ulink>
328 and <ulink url="https://en.cppreference.com/w/cpp/language/delete">cppreference
329 delete expression</ulink>). A new expression will
330 call <function>operator new</function> to perform the allocation and
331 then call the constructor (if one exists) on the object. Similarly a
332 delete expression will call the destructor on the object (if one
333 exists) and then call <function>operator delete</function>. The array
334 overloads call constructors/destructors for each object in the
336 <para>The deal is:</para>
339 <para>If allocated with
340 <function>malloc</function>,
341 <function>calloc</function>,
342 <function>realloc</function>,
343 <function>valloc</function> or
344 <function>memalign</function>, you must
345 deallocate with <function>free</function>.</para>
348 <para>If allocated with <function>new</function>, you must deallocate
349 with <function>delete</function>.</para>
352 <para>If allocated with <function>new[]</function>, you must
353 deallocate with <function>delete[]</function>.</para>
357 <para>Mixing types of allocators and deallocators is undefined
358 behaviour. That means that on some platforms you might not have any
359 problems, but the same program may then crash on a different platform,
360 Solaris for example. So it's best to fix it properly. According to
361 the KDE folks "it's amazing how many C++ programmers don't know
364 <para>The reason behind the requirement is as follows. In some C++
365 implementations, <function>delete[]</function> must be used for
366 objects allocated by <function>new[]</function> because the compiler
367 stores the size of the array and the pointer-to-member to the
368 destructor of the array's content just before the pointer actually
369 returned. <function>delete</function> doesn't account for this and
370 will get confused, possibly corrupting the heap. Even if there is no
371 corruption there are likely to be resource leaks since using the wrong
372 delete may result in the wrong number of destructors being
375 <para>C++ aligned allocations need to be freed using aligned delete
376 with the same alignment.</para>
382 <sect2 id="mc-manual.overlap"
383 xreflabel="Overlapping source and destination blocks">
384 <title>Overlapping source and destination blocks</title>
386 <para>The following C library functions copy some data from one
387 memory block to another (or something similar):
388 <function>memcpy</function>,
389 <function>strcpy</function>,
390 <function>strncpy</function>,
391 <function>strcat</function>,
392 <function>strncat</function>.
393 The blocks pointed to by their <computeroutput>src</computeroutput> and
394 <computeroutput>dst</computeroutput> pointers aren't allowed to overlap.
395 The POSIX standards have wording along the lines "If copying takes place
396 between objects that overlap, the behavior is undefined." Therefore,
397 Memcheck checks for this.
400 <para>For example:</para>
401 <programlisting><![CDATA[
402 ==27492== Source and destination overlap in memcpy(0xbffff294, 0xbffff280, 21)
403 ==27492== at 0x40026CDC: memcpy (mc_replace_strmem.c:71)
404 ==27492== by 0x804865A: main (overlap.c:40)
407 <para>You don't want the two blocks to overlap because one of them could
408 get partially overwritten by the copying.</para>
410 <para>You might think that Memcheck is being overly pedantic reporting
411 this in the case where <computeroutput>dst</computeroutput> is less than
412 <computeroutput>src</computeroutput>. For example, the obvious way to
413 implement <function>memcpy</function> is by copying from the first
414 byte to the last. However, the optimisation guides of some
415 architectures recommend copying from the last byte down to the first.
416 Also, some implementations of <function>memcpy</function> zero
417 <computeroutput>dst</computeroutput> before copying, because zeroing the
418 destination's cache line(s) can improve performance.</para>
420 <para>The moral of the story is: if you want to write truly portable
421 code, don't make any assumptions about the language
422 implementation.</para>
427 <sect2 id="mc-manual.fishyvalue"
428 xreflabel="Fishy argument values">
429 <title>Fishy argument values</title>
431 <para>All memory allocation functions take an argument specifying the
432 size of the memory block that should be allocated. Clearly, the requested
433 size should be a non-negative value and is typically not excessively large.
434 For instance, it is extremely unlikly that the size of an allocation
435 request exceeds 2**63 bytes on a 64-bit machine. It is much more likely that
436 such a value is the result of an erroneous size calculation and is in effect
437 a negative value (that just happens to appear excessively large because
438 the bit pattern is interpreted as an unsigned integer).
439 Such a value is called a "fishy value".
441 The <varname>size</varname> argument of the following allocation functions
442 is checked for being fishy:
443 <function>malloc</function>,
444 <function>calloc</function>,
445 <function>realloc</function>,
446 <function>memalign</function>,
447 <function>posix_memalign</function>,
448 <function>aligned_alloc</function>,
449 <function>new</function>,
450 <function>new []</function>.
451 <function>__builtin_new</function>,
452 <function>__builtin_vec_new</function>,
453 For <function>calloc</function> both arguments are checked.
456 <para>For example:</para>
457 <programlisting><![CDATA[
458 ==32233== Argument 'size' of function malloc has a fishy (possibly negative) value: -3
459 ==32233== at 0x4C2CFA7: malloc (vg_replace_malloc.c:298)
460 ==32233== by 0x400555: foo (fishy.c:15)
461 ==32233== by 0x400583: main (fishy.c:23)
464 <para>In earlier Valgrind versions those values were being referred to
465 as "silly arguments" and no back-trace was included.
470 <sect2 id="mc-manual.reallocsizezero"
471 xreflabel="Realloc size zero">
472 <title>Realloc size zero</title>
474 <para>The (ab)use or realloc to also do the job of <function>free</function>
475 has been poorly understood for a long time. In the C17 standard
476 ISO/IEC 9899:2017] the behaviour of realloc when the size argument
477 is zero is specified as implementation defined. Memcheck warns about
478 the non-portable use or realloc.</para>
480 <para>For example:</para>
481 <programlisting><![CDATA[
482 ==77609== realloc() with size 0
483 ==77609== at 0x48502B8: realloc (vg_replace_malloc.c:1450)
484 ==77609== by 0x201989: main (realloczero.c:8)
485 ==77609== Address 0x5464040 is 0 bytes inside a block of size 4 alloc'd
486 ==77609== at 0x484CBB4: malloc (vg_replace_malloc.c:397)
487 ==77609== by 0x201978: main (realloczero.c:7)
492 <sect2 id="mc-manual.alignment"
493 xreflabel="Alignment (and Size) Errors">
494 <title>Alignment Errors</title>
496 <para>C and C++ have several functions that allow the user to obtain aligned memory.
497 Typically this is done for performance reasons so that the memory will be cache line
498 or memory page aligned. C has the functions <computeroutput>memalign</computeroutput>,
499 <computeroutput>posix_memalign</computeroutput> and <computeroutput>aligned_alloc</computeroutput>.
500 C++ has numerous overloads of <computeroutput>operator new</computeroutput> and <computeroutput>
501 operator delete</computeroutput>. Of these, posix_memalign is quite clearly
502 specified, the others vary quite widely between implementations. Valgrind will generate
503 errors for values of alignment that are invalid on any platform.</para>
505 <!-- would like to have a table here -->
506 <para><computeroutput>memalign</computeroutput> will produce errors if the alignment
507 is zero or not a multiple of two.</para>
509 <para><computeroutput>posix_memalign</computeroutput> will produce errors if the alignment
510 is less than sizeof(size_t), not a multiple of two or if the size is zero.</para>
512 <para><computeroutput>aligned_alloc</computeroutput> will produce errors if the alignment
513 is not a multiple of two , if the size is zero or if the size is not an integral
514 multiple of the alignment.</para>
516 <para><computeroutput>aligned new</computeroutput> will produce errors if the alignment
517 is zero or not a multiple of two. The <computeroutput>nothrow</computeroutput> overloads
518 will return a NULL pointer. The non-nothrow overloads will abort Valgrind.</para>
520 <para><computeroutput>aligned delete</computeroutput> will produce errors if the alignment
521 is zero or not a multiple of two or if the alignment is not the same as that used by
522 <computeroutput>aligned new</computeroutput>.</para>
524 <para><computeroutput>sized delete</computeroutput> will produce errors if the size
525 is not the same as that used by <computeroutput>new</computeroutput>.</para>
527 <para><computeroutput>sized aligned delete</computeroutput> combines the error conditions
528 of the individual sized and aligned delete operators.</para>
530 <para>Example output:</para>
531 <programlisting><![CDATA[
532 ==65825== Invalid alignment value: 3 (should be power of 2)
533 ==65825== at 0x485197E: memalign (vg_replace_malloc.c:1740)
534 ==65825== by 0x201CD2: main (memalign.c:39)
539 <sect2 id="mc-manual.leaks" xreflabel="Memory leak detection">
540 <title>Memory leak detection</title>
542 <para>Memcheck keeps track of all heap blocks issued in response to
544 <function>malloc</function>/<function>new</function> et al.
545 So when the program exits, it knows which blocks have not been freed.
548 <para>If <option>--leak-check</option> is set appropriately, for each
549 remaining block, Memcheck determines if the block is reachable from pointers
550 within the root-set. The root-set consists of (a) general purpose registers
551 of all threads, and (b) initialised, aligned, pointer-sized data words in
552 accessible client memory, including stacks.</para>
554 <para>There are two ways a block can be reached. The first is with a
555 "start-pointer", i.e. a pointer to the start of the block. The second is with
556 an "interior-pointer", i.e. a pointer to the middle of the block. There are
557 several ways we know of that an interior-pointer can occur:</para>
561 <para>The pointer might have originally been a start-pointer and have been
562 moved along deliberately (or not deliberately) by the program. In
563 particular, this can happen if your program uses tagged pointers, i.e.
564 if it uses the bottom one, two or three bits of a pointer, which are
565 normally always zero due to alignment, in order to store extra
570 <para>It might be a random junk value in memory, entirely unrelated, just
571 a coincidence.</para>
575 <para>It might be a pointer to the inner char array of a C++
576 <computeroutput>std::string</computeroutput>. For example, some
577 compilers add 3 words at the beginning of the std::string to
578 store the length, the capacity and a reference count before the
579 memory containing the array of characters. They return a pointer
580 just after these 3 words, pointing at the char array.</para>
584 <para>Some code might allocate a block of memory, and use the first 8
585 bytes to store (block size - 8) as a 64bit number.
586 <computeroutput>sqlite3MemMalloc</computeroutput> does this.</para>
590 <para>It might be a pointer to an array of C++ objects (which possess
591 destructors) allocated with <computeroutput>new[]</computeroutput>. In
592 this case, some compilers store a "magic cookie" containing the array
593 length at the start of the allocated block, and return a pointer to just
594 past that magic cookie, i.e. an interior-pointer.
596 The link below is to an old C++ feature and has rotted away.
597 There are various copies around the web, probably also likely to rot.
598 See <ulink url="https://docs.freebsd.org/info/gxxint/gxxint.info.Free_Store.html">this
600 See <ulink url="https://www.math.utah.edu/docs/info/gxxint_1.html#SEC17">this
601 page</ulink> for more information.</para>
605 <para>It might be a pointer to an inner part of a C++ object using
606 multiple inheritance. </para>
610 <para>You can optionally activate heuristics to use during the leak
611 search to detect the interior pointers corresponding to
612 the <computeroutput>stdstring</computeroutput>,
613 <computeroutput>length64</computeroutput>,
614 <computeroutput>newarray</computeroutput>
615 and <computeroutput>multipleinheritance</computeroutput> cases. If the
616 heuristic detects that an interior pointer corresponds to such a case,
617 the block will be considered as reachable by the interior
618 pointer. In other words, the interior pointer will be treated
619 as if it were a start pointer.</para>
622 <para>With that in mind, consider the nine possible cases described by the
623 following figure.</para>
625 <programlisting><![CDATA[
626 Pointer chain AAA Leak Case BBB Leak Case
627 ------------- ------------- -------------
628 (1) RRR ------------> BBB DR
629 (2) RRR ---> AAA ---> BBB DR IR
631 (4) RRR AAA ---> BBB DL IL
632 (5) RRR ------?-----> BBB (y)DR, (n)DL
633 (6) RRR ---> AAA -?-> BBB DR (y)IR, (n)DL
634 (7) RRR -?-> AAA ---> BBB (y)DR, (n)DL (y)IR, (n)IL
635 (8) RRR -?-> AAA -?-> BBB (y)DR, (n)DL (y,y)IR, (n,y)IL, (_,n)DL
636 (9) RRR AAA -?-> BBB DL (y)IL, (n)DL
638 Pointer chain legend:
639 - RRR: a root set node or DR block
640 - AAA, BBB: heap blocks
641 - --->: a start-pointer
642 - -?->: an interior-pointer
645 - DR: Directly reachable
646 - IR: Indirectly reachable
648 - IL: Indirectly lost
649 - (y)XY: it's XY if the interior-pointer is a real pointer
650 - (n)XY: it's XY if the interior-pointer is not a real pointer
651 - (_)XY: it's XY in either case
654 <para>Every possible case can be reduced to one of the above nine. Memcheck
655 merges some of these cases in its output, resulting in the following four
662 <para>"Still reachable". This covers cases 1 and 2 (for the BBB blocks)
663 above. A start-pointer or chain of start-pointers to the block is
664 found. Since the block is still pointed at, the programmer could, at
665 least in principle, have freed it before program exit. "Still reachable"
666 blocks are very common and arguably not a problem. So, by default,
667 Memcheck won't report such blocks individually.</para>
671 <para>"Definitely lost". This covers case 3 (for the BBB blocks) above.
672 This means that no pointer to the block can be found. The block is
673 classified as "lost", because the programmer could not possibly have
674 freed it at program exit, since no pointer to it exists. This is likely
675 a symptom of having lost the pointer at some earlier point in the
676 program. Such cases should be fixed by the programmer.</para>
680 <para>"Indirectly lost". This covers cases 4 and 9 (for the BBB blocks)
681 above. This means that the block is lost, not because there are no
682 pointers to it, but rather because all the blocks that point to it are
683 themselves lost. For example, if you have a binary tree and the root
684 node is lost, all its children nodes will be indirectly lost. Because
685 the problem will disappear if the definitely lost block that caused the
686 indirect leak is fixed, Memcheck won't report such blocks individually
691 <para>"Possibly lost". This covers cases 5--8 (for the BBB blocks)
692 above. This means that a chain of one or more pointers to the block has
693 been found, but at least one of the pointers is an interior-pointer.
694 This could just be a random value in memory that happens to point into a
695 block, and so you shouldn't consider this ok unless you know you have
696 interior-pointers.</para>
701 <para>(Note: This mapping of the nine possible cases onto four leak kinds is
702 not necessarily the best way that leaks could be reported; in particular,
703 interior-pointers are treated inconsistently. It is possible the
704 categorisation may be improved in the future.)</para>
706 <para>Furthermore, if suppressions exists for a block, it will be reported
707 as "suppressed" no matter what which of the above four kinds it belongs
711 <para>The following is an example leak summary.</para>
713 <programlisting><![CDATA[
715 definitely lost: 48 bytes in 3 blocks.
716 indirectly lost: 32 bytes in 2 blocks.
717 possibly lost: 96 bytes in 6 blocks.
718 still reachable: 64 bytes in 4 blocks.
719 suppressed: 0 bytes in 0 blocks.
722 <para>If heuristics have been used to consider some blocks as
723 reachable, the leak summary details the heuristically reachable subset
724 of 'still reachable:' per heuristic. In the below example, of the 95
725 bytes still reachable, 87 bytes (56+7+8+16) have been considered
726 heuristically reachable.
729 <programlisting><![CDATA[
731 definitely lost: 4 bytes in 1 blocks
732 indirectly lost: 0 bytes in 0 blocks
733 possibly lost: 0 bytes in 0 blocks
734 still reachable: 95 bytes in 6 blocks
735 of which reachable via heuristic:
736 stdstring : 56 bytes in 2 blocks
737 length64 : 16 bytes in 1 blocks
738 newarray : 7 bytes in 1 blocks
739 multipleinheritance: 8 bytes in 1 blocks
740 suppressed: 0 bytes in 0 blocks
743 <para>If <option>--leak-check=full</option> is specified,
744 Memcheck will give details for each definitely lost or possibly lost block,
745 including where it was allocated. (Actually, it merges results for all
746 blocks that have the same leak kind and sufficiently similar stack traces
747 into a single "loss record". The
748 <option>--leak-resolution</option> lets you control the
749 meaning of "sufficiently similar".) It cannot tell you when or how or why
750 the pointer to a leaked block was lost; you have to work that out for
751 yourself. In general, you should attempt to ensure your programs do not
752 have any definitely lost or possibly lost blocks at exit.</para>
754 <para>For example:</para>
755 <programlisting><![CDATA[
756 8 bytes in 1 blocks are definitely lost in loss record 1 of 14
757 at 0x........: malloc (vg_replace_malloc.c:...)
758 by 0x........: mk (leak-tree.c:11)
759 by 0x........: main (leak-tree.c:39)
761 88 (8 direct, 80 indirect) bytes in 1 blocks are definitely lost in loss record 13 of 14
762 at 0x........: malloc (vg_replace_malloc.c:...)
763 by 0x........: mk (leak-tree.c:11)
764 by 0x........: main (leak-tree.c:25)
767 <para>The first message describes a simple case of a single 8 byte block
768 that has been definitely lost. The second case mentions another 8 byte
769 block that has been definitely lost; the difference is that a further 80
770 bytes in other blocks are indirectly lost because of this lost block.
771 The loss records are not presented in any notable order, so the loss record
772 numbers aren't particularly meaningful. The loss record numbers can be used
773 in the Valgrind gdbserver to list the addresses of the leaked blocks and/or give
774 more details about how a block is still reachable.</para>
776 <para>The option <option>--show-leak-kinds=<set></option>
777 controls the set of leak kinds to show
778 when <option>--leak-check=full</option> is specified. </para>
780 <para>The <option><set></option> of leak kinds is specified
781 in one of the following ways:
784 <listitem><para>a comma separated list of one or more of
785 <option>definite indirect possible reachable</option>.</para>
788 <listitem><para><option>all</option> to specify the complete set (all leak kinds).</para>
791 <listitem><para><option>none</option> for the empty set.</para>
797 <para> The default value for the leak kinds to show is
798 <option>--show-leak-kinds=definite,possible</option>.
801 <para>To also show the reachable and indirectly lost blocks in
802 addition to the definitely and possibly lost blocks, you can
803 use <option>--show-leak-kinds=all</option>. To only show the
804 reachable and indirectly lost blocks, use
805 <option>--show-leak-kinds=indirect,reachable</option>. The reachable
806 and indirectly lost blocks will then be presented as shown in
807 the following two examples.</para>
809 <programlisting><![CDATA[
810 64 bytes in 4 blocks are still reachable in loss record 2 of 4
811 at 0x........: malloc (vg_replace_malloc.c:177)
812 by 0x........: mk (leak-cases.c:52)
813 by 0x........: main (leak-cases.c:74)
815 32 bytes in 2 blocks are indirectly lost in loss record 1 of 4
816 at 0x........: malloc (vg_replace_malloc.c:177)
817 by 0x........: mk (leak-cases.c:52)
818 by 0x........: main (leak-cases.c:80)
821 <para>Because there are different kinds of leaks with different
822 severities, an interesting question is: which leaks should be
823 counted as true "errors" and which should not?
826 <para> The answer to this question affects the numbers printed in
827 the <computeroutput>ERROR SUMMARY</computeroutput> line, and also the
828 effect of the <option>--error-exitcode</option> option. First, a leak
829 is only counted as a true "error"
830 if <option>--leak-check=full</option> is specified. Then, the
831 option <option>--errors-for-leak-kinds=<set></option> controls
832 the set of leak kinds to consider as errors. The default value
833 is <option>--errors-for-leak-kinds=definite,possible</option>
842 <sect1 id="mc-manual.options"
843 xreflabel="Memcheck Command-Line Options">
844 <title>Memcheck Command-Line Options</title>
846 <!-- start of xi:include in the manpage -->
847 <variablelist id="mc.opts.list">
849 <varlistentry id="opt.leak-check" xreflabel="--leak-check">
851 <option><![CDATA[--leak-check=<no|summary|yes|full> [default: summary] ]]></option>
854 <para>When enabled, search for memory leaks when the client
855 program finishes. If set to <varname>summary</varname>, it says how
856 many leaks occurred. If set to <varname>full</varname> or
857 <varname>yes</varname>, each individual leak will be shown
858 in detail and/or counted as an error, as specified by the options
859 <option>--show-leak-kinds</option> and
860 <option>--errors-for-leak-kinds</option>. </para>
861 <para>If <varname>--xml=yes</varname> is given, memcheck will
862 automatically use the value <varname>--leak-check=full</varname>.
863 You can use <option>--show-leak-kinds=none</option> to reduce
864 the size of the xml output if you are not interested in the leak
869 <varlistentry id="opt.leak-resolution" xreflabel="--leak-resolution">
871 <option><![CDATA[--leak-resolution=<low|med|high> [default: high] ]]></option>
874 <para>When doing leak checking, determines how willing
875 Memcheck is to consider different backtraces to
876 be the same for the purposes of merging multiple leaks into a single
877 leak report. When set to <varname>low</varname>, only the first
878 two entries need match. When <varname>med</varname>, four entries
879 have to match. When <varname>high</varname>, all entries need to
882 <para>For hardcore leak debugging, you probably want to use
883 <option>--leak-resolution=high</option> together with
884 <option>--num-callers=40</option> or some such large number.
887 <para>Note that the <option>--leak-resolution</option> setting
888 does not affect Memcheck's ability to find
889 leaks. It only changes how the results are presented.</para>
893 <varlistentry id="opt.show-leak-kinds" xreflabel="--show-leak-kinds">
895 <option><![CDATA[--show-leak-kinds=<set> [default: definite,possible] ]]></option>
898 <para>Specifies the leak kinds to show in a <varname>full</varname>
899 leak search, in one of the following ways: </para>
902 <listitem><para>a comma separated list of one or more of
903 <option>definite indirect possible reachable</option>.</para>
906 <listitem><para><option>all</option> to specify the complete set (all leak kinds).
908 <option>--show-leak-kinds=definite,indirect,possible,reachable</option>.</para>
911 <listitem><para><option>none</option> for the empty set.</para>
918 <varlistentry id="opt.errors-for-leak-kinds" xreflabel="--errors-for-leak-kinds">
920 <option><![CDATA[--errors-for-leak-kinds=<set> [default: definite,possible] ]]></option>
923 <para>Specifies the leak kinds to count as errors in a
924 <varname>full</varname> leak search. The
925 <option><![CDATA[<set>]]></option> is specified similarly to
926 <option>--show-leak-kinds</option>
932 <varlistentry id="opt.leak-check-heuristics" xreflabel="--leak-check-heuristics">
934 <option><![CDATA[--leak-check-heuristics=<set> [default: all] ]]></option>
937 <para>Specifies the set of leak check heuristics to be used
938 during leak searches. The heuristics control which interior pointers
939 to a block cause it to be considered as reachable.
940 The heuristic set is specified in one of the following ways:</para>
943 <listitem><para>a comma separated list of one or more of
944 <option>stdstring length64 newarray multipleinheritance</option>.</para>
947 <listitem><para><option>all</option> to activate the complete set of
950 <option>--leak-check-heuristics=stdstring,length64,newarray,multipleinheritance</option>.</para>
953 <listitem><para><option>none</option> for the empty set.</para>
956 <para>Note that these heuristics are dependent on the layout of
957 the objects produced by the C++ compiler. They have been
958 tested with some gcc versions (e.g. 4.4 and 4.7). They might
959 not work properly with other C++ compilers.
964 <varlistentry id="opt.show-reachable" xreflabel="--show-reachable">
966 <option><![CDATA[--show-reachable=<yes|no> ]]></option>
969 <option><![CDATA[--show-possibly-lost=<yes|no> ]]></option>
972 <para>These options provide an alternative way to specify the leak kinds to show:
977 <option>--show-reachable=no --show-possibly-lost=yes</option> is equivalent to
978 <option>--show-leak-kinds=definite,possible</option>.
983 <option>--show-reachable=no --show-possibly-lost=no</option> is equivalent to
984 <option>--show-leak-kinds=definite</option>.
989 <option>--show-reachable=yes</option> is equivalent to
990 <option>--show-leak-kinds=all</option>.
994 <para> Note that <option>--show-possibly-lost=no</option> has no
995 effect if <option>--show-reachable=yes</option> is
1000 <varlistentry id="opt.xtree-leak" xreflabel="--xtree-leak">
1002 <option><![CDATA[--xtree-leak=<no|yes> [no] ]]></option>
1005 <para>If set to yes, the results for the leak search done at exit will be
1006 output in a 'Callgrind Format' execution tree file. Note that this
1007 automatically sets the options <option>--leak-check=full</option>
1008 and <option>--show-leak-kinds=all</option>, to allow
1009 xtree visualisation tools such as kcachegrind to select what kind
1010 to leak to visualize.
1011 The produced file will contain the following events:</para>
1013 <listitem><para><option>RB</option> : Reachable Bytes</para></listitem>
1014 <listitem><para><option>PB</option> : Possibly lost Bytes</para></listitem>
1015 <listitem><para><option>IB</option> : Indirectly lost Bytes</para></listitem>
1016 <listitem><para><option>DB</option> : Definitely lost Bytes (direct plus indirect)</para></listitem>
1017 <listitem><para><option>DIB</option> : Definitely Indirectly lost Bytes (subset of DB)</para></listitem>
1018 <listitem><para><option>RBk</option> : reachable Blocks</para></listitem>
1019 <listitem><para><option>PBk</option> : Possibly lost Blocks</para></listitem>
1020 <listitem><para><option>IBk</option> : Indirectly lost Blocks</para></listitem>
1021 <listitem><para><option>DBk</option> : Definitely lost Blocks</para></listitem>
1024 <para>The increase or decrease for all events above will also be output in
1025 the file to provide the delta (increase or decrease) between 2
1026 successive leak searches. For example, <option>iRB</option> is the
1027 increase of the <option>RB</option> event, <option>dPBk</option> is the
1028 decrease of <option>PBk</option> event. The values for the increase and
1029 decrease events will be zero for the first leak search done.</para>
1031 <para>See <xref linkend="&vg-xtree-id;"/> for a detailed explanation
1032 about execution trees.</para>
1036 <varlistentry id="opt.xtree-leak-file" xreflabel="--xtree-leak-file">
1038 <option><![CDATA[--xtree-leak-file=<filename> [default:
1039 xtleak.kcg.%p] ]]></option>
1042 <para>Specifies that Valgrind should produce the xtree leak
1043 report in the specified file. Any <option>%p</option>,
1044 <option>%q</option> or <option>%n</option> sequences appearing in
1045 the filename are expanded
1046 in exactly the same way as they are for <option>--log-file</option>.
1047 See the description of <xref linkend="opt.log-file"/>
1048 for details. </para>
1049 <para>See <xref linkend="&vg-xtree-id;"/>
1050 for a detailed explanation about execution trees formats. </para>
1054 <varlistentry id="opt.undef-value-errors" xreflabel="--undef-value-errors">
1056 <option><![CDATA[--undef-value-errors=<yes|no> [default: yes] ]]></option>
1059 <para>Controls whether Memcheck reports
1060 uses of undefined value errors. Set this to
1061 <varname>no</varname> if you don't want to see undefined value
1062 errors. It also has the side effect of speeding up Memcheck somewhat.
1063 AddrCheck (removed in Valgrind 3.1.0) functioned like Memcheck with
1064 <option>--undef-value-errors=no</option>.
1069 <varlistentry id="opt.track-origins" xreflabel="--track-origins">
1071 <option><![CDATA[--track-origins=<yes|no> [default: no] ]]></option>
1074 <para>Controls whether Memcheck tracks
1075 the origin of uninitialised values. By default, it does not,
1076 which means that although it can tell you that an
1077 uninitialised value is being used in a dangerous way, it
1078 cannot tell you where the uninitialised value came from. This
1079 often makes it difficult to track down the root problem.
1082 to <varname>yes</varname>, Memcheck keeps
1083 track of the origins of all uninitialised values. Then, when
1084 an uninitialised value error is
1085 reported, Memcheck will try to show the
1086 origin of the value. An origin can be one of the following
1087 four places: a heap block, a stack allocation, a client
1088 request, or miscellaneous other sources (eg, a call
1089 to <varname>brk</varname>).
1091 <para>For uninitialised values originating from a heap
1092 block, Memcheck shows where the block was
1093 allocated. For uninitialised values originating from a stack
1094 allocation, Memcheck can tell you which
1095 function allocated the value, but no more than that -- typically
1096 it shows you the source location of the opening brace of the
1097 function. So you should carefully check that all of the
1098 function's local variables are initialised properly.
1100 <para>Performance overhead: origin tracking is expensive. It
1101 halves Memcheck's speed and increases
1102 memory use by a minimum of 100MB, and possibly more.
1103 Nevertheless it can drastically reduce the effort required to
1104 identify the root cause of uninitialised value errors, and so
1105 is often a programmer productivity win, despite running
1108 <para>Accuracy: Memcheck tracks origins
1109 quite accurately. To avoid very large space and time
1110 overheads, some approximations are made. It is possible,
1111 although unlikely, that Memcheck will report an incorrect origin, or
1112 not be able to identify any origin.
1114 <para>Note that the combination
1115 <option>--track-origins=yes</option>
1116 and <option>--undef-value-errors=no</option> is
1117 nonsensical. Memcheck checks for and
1118 rejects this combination at startup.
1123 <varlistentry id="opt.partial-loads-ok" xreflabel="--partial-loads-ok">
1125 <option><![CDATA[--partial-loads-ok=<yes|no> [default: yes] ]]></option>
1128 <para>Controls how Memcheck handles 32-, 64-, 128- and 256-bit
1129 naturally aligned loads from addresses for which some bytes are
1130 addressable and others are not. When <varname>yes</varname>, such
1131 loads do not produce an address error. Instead, loaded bytes
1132 originating from illegal addresses are marked as uninitialised, and
1133 those corresponding to legal addresses are handled in the normal
1136 <para>When <varname>no</varname>, loads from partially invalid
1137 addresses are treated the same as loads from completely invalid
1138 addresses: an illegal-address error is issued, and the resulting
1139 bytes are marked as initialised.</para>
1141 <para>Note that code that behaves in this way is in violation of
1142 the ISO C/C++ standards, and should be considered broken. If
1143 at all possible, such code should be fixed.</para>
1147 <varlistentry id="opt.expensive-definedness-checks" xreflabel="--expensive-definedness-checks">
1149 <option><![CDATA[--expensive-definedness-checks=<no|auto|yes> [default: auto] ]]></option>
1152 <para>Controls whether Memcheck should employ more precise but also
1153 more expensive (time consuming) instrumentation when checking the
1154 definedness of certain values. In particular, this affects the
1155 instrumentation of integer adds, subtracts and equality
1157 <para>Selecting <option>--expensive-definedness-checks=yes</option>
1158 causes Memcheck to use the most accurate analysis possible. This
1159 minimises false error rates but can cause up to 30% performance
1161 <para>Selecting <option>--expensive-definedness-checks=no</option>
1162 causes Memcheck to use the cheapest instrumentation possible. This
1163 maximises performance but will normally give an unusably high false
1166 setting, <option>--expensive-definedness-checks=auto</option>, is
1167 strongly recommended. This causes Memcheck to use the minimum of
1168 expensive instrumentation needed to achieve the same false error
1169 rate as <option>--expensive-definedness-checks=yes</option>. It
1170 also enables an instrumentation-time analysis pass which aims to
1171 further reduce the costs of accurate instrumentation. Overall, the
1172 performance loss is generally around 5% relative to
1173 <option>--expensive-definedness-checks=no</option>, although this is
1174 strongly workload dependent. Note that the exact instrumentation
1175 settings in this mode are architecture dependent.</para>
1179 <varlistentry id="opt.keep-stacktraces" xreflabel="--keep-stacktraces">
1181 <option><![CDATA[--keep-stacktraces=alloc|free|alloc-and-free|alloc-then-free|none [default: alloc-and-free] ]]></option>
1184 <para>Controls which stack trace(s) to keep for malloc'd and/or
1188 <para>With <varname>alloc-then-free</varname>, a stack trace is
1189 recorded at allocation time, and is associated with the block.
1190 When the block is freed, a second stack trace is recorded, and
1191 this replaces the allocation stack trace. As a result, any "use
1192 after free" errors relating to this block can only show a stack
1193 trace for where the block was freed.
1196 <para>With <varname>alloc-and-free</varname>, both allocation
1197 and the deallocation stack traces for the block are stored.
1198 Hence a "use after free" error will
1199 show both, which may make the error easier to diagnose.
1200 Compared to <varname>alloc-then-free</varname>, this setting
1201 slightly increases Valgrind's memory use as the block contains two
1202 references instead of one.
1205 <para>With <varname>alloc</varname>, only the allocation stack
1206 trace is recorded (and reported). With <varname>free</varname>,
1207 only the deallocation stack trace is recorded (and reported).
1208 These values somewhat decrease Valgrind's memory and cpu usage.
1209 They can be useful depending on the error types you are
1210 searching for and the level of detail you need to analyse
1211 them. For example, if you are only interested in memory leak
1212 errors, it is sufficient to record the allocation stack traces.
1215 <para>With <varname>none</varname>, no stack traces are recorded
1216 for malloc and free operations. If your program allocates a lot
1217 of blocks and/or allocates/frees from many different stack
1218 traces, this can significantly decrease cpu and/or memory
1219 required. Of course, few details will be reported for errors
1220 related to heap blocks.
1223 <para>Note that once a stack trace is recorded, Valgrind keeps
1224 the stack trace in memory even if it is not referenced by any
1225 block. Some programs (for example, recursive algorithms) can
1226 generate a huge number of stack traces. If Valgrind uses too
1227 much memory in such circumstances, you can reduce the memory
1228 required with the options <varname>--keep-stacktraces</varname>
1229 and/or by using a smaller value for the
1230 option <varname>--num-callers</varname>.
1233 <para>If you want to use
1234 <computeroutput>--xtree-memory=full</computeroutput> memory profiling
1235 (see <xref linkend="&vg-xtree-id;"/>), then you cannot
1236 specify <varname>--keep-stacktraces=free</varname>
1237 or <varname>--keep-stacktraces=none</varname>.</para>
1242 <varlistentry id="opt.freelist-vol" xreflabel="--freelist-vol">
1244 <option><![CDATA[--freelist-vol=<number> [default: 20000000] ]]></option>
1247 <para>When the client program releases memory using
1248 <function>free</function> (in <literal>C</literal>) or
1249 <computeroutput>delete</computeroutput>
1250 (<literal>C++</literal>), that memory is not immediately made
1251 available for re-allocation. Instead, it is marked inaccessible
1252 and placed in a queue of freed blocks. The purpose is to defer as
1253 long as possible the point at which freed-up memory comes back
1254 into circulation. This increases the chance that
1255 Memcheck will be able to detect invalid
1256 accesses to blocks for some significant period of time after they
1257 have been freed.</para>
1259 <para>This option specifies the maximum total size, in bytes, of the
1260 blocks in the queue. The default value is twenty million bytes.
1261 Increasing this increases the total amount of memory used by
1262 Memcheck but may detect invalid uses of freed
1263 blocks which would otherwise go undetected.</para>
1267 <varlistentry id="opt.freelist-big-blocks" xreflabel="--freelist-big-blocks">
1269 <option><![CDATA[--freelist-big-blocks=<number> [default: 1000000] ]]></option>
1272 <para>When making blocks from the queue of freed blocks available
1273 for re-allocation, Memcheck will in priority re-circulate the blocks
1274 with a size greater or equal to <option>--freelist-big-blocks</option>.
1275 This ensures that freeing big blocks (in particular freeing blocks bigger than
1276 <option>--freelist-vol</option>) does not immediately lead to a re-circulation
1277 of all (or a lot of) the small blocks in the free list. In other words,
1278 this option increases the likelihood to discover dangling pointers
1279 for the "small" blocks, even when big blocks are freed.</para>
1280 <para>Setting a value of 0 means that all the blocks are re-circulated
1281 in a FIFO order. </para>
1285 <varlistentry id="opt.workaround-gcc296-bugs" xreflabel="--workaround-gcc296-bugs">
1287 <option><![CDATA[--workaround-gcc296-bugs=<yes|no> [default: no] ]]></option>
1290 <para>When enabled, assume that reads and writes some small
1291 distance below the stack pointer are due to bugs in GCC 2.96, and
1292 does not report them. The "small distance" is 256 bytes by
1293 default. Note that GCC 2.96 is the default compiler on some ancient
1294 Linux distributions (RedHat 7.X) and so you may need to use this
1295 option. Do not use it if you do not have to, as it can cause real
1296 errors to be overlooked. A better alternative is to use a more
1297 recent GCC in which this bug is fixed.</para>
1299 <para>You may also need to use this option when working with
1300 GCC 3.X or 4.X on 32-bit PowerPC Linux. This is because
1301 GCC generates code which occasionally accesses below the
1302 stack pointer, particularly for floating-point to/from integer
1303 conversions. This is in violation of the 32-bit PowerPC ELF
1304 specification, which makes no provision for locations below the
1305 stack pointer to be accessible.</para>
1307 <para>This option is deprecated as of version 3.12 and may be
1308 removed from future versions. You should instead use
1309 <option>--ignore-range-below-sp</option> to specify the exact
1310 range of offsets below the stack pointer that should be ignored.
1311 A suitable equivalent
1312 is <option>--ignore-range-below-sp=1024-1</option>.
1317 <varlistentry id="opt.ignore-range-below-sp"
1318 xreflabel="--ignore-range-below-sp">
1320 <option><![CDATA[--ignore-range-below-sp=<number>-<number> ]]></option>
1323 <para>This is a more general replacement for the deprecated
1324 <option>--workaround-gcc296-bugs</option> option. When
1325 specified, it causes Memcheck not to report errors for accesses
1326 at the specified offsets below the stack pointer. The two
1327 offsets must be positive decimal numbers and -- somewhat
1328 counterintuitively -- the first one must be larger, in order to
1329 imply a non-wraparound address range to ignore. For example,
1330 to ignore 4 byte accesses at 8192 bytes below the stack
1332 use <option>--ignore-range-below-sp=8192-8189</option>. Only
1333 one range may be specified.
1338 <varlistentry id="opt.show-mismatched-frees"
1339 xreflabel="--show-mismatched-frees">
1341 <option><![CDATA[--show-mismatched-frees=<yes|no> [default: yes] ]]></option>
1344 <para>When enabled, Memcheck checks that heap blocks are
1345 deallocated using a function that matches the allocating
1346 function. That is, it expects <varname>free</varname> to be
1347 used to deallocate blocks allocated
1348 by <varname>malloc</varname>, <varname>delete</varname> for
1349 blocks allocated by <varname>new</varname>,
1350 and <varname>delete[]</varname> for blocks allocated
1351 by <varname>new[]</varname>. If a mismatch is detected, an
1352 error is reported. This is in general important because in some
1353 environments, freeing with a non-matching function can cause
1356 <para>There is however a scenario where such mismatches cannot
1357 be avoided. That is when the user provides implementations of
1358 <varname>new</varname>/<varname>new[]</varname> that
1359 call <varname>malloc</varname> and
1360 of <varname>delete</varname>/<varname>delete[]</varname> that
1361 call <varname>free</varname>, and these functions are
1362 asymmetrically inlined. For example, imagine
1363 that <varname>delete[]</varname> is inlined
1364 but <varname>new[]</varname> is not. The result is that
1365 Memcheck "sees" all <varname>delete[]</varname> calls as direct
1366 calls to <varname>free</varname>, even when the program source
1367 contains no mismatched calls.</para>
1369 <para>This causes a lot of confusing and irrelevant error
1370 reports. <varname>--show-mismatched-frees=no</varname> disables
1371 these checks. It is not generally advisable to disable them,
1372 though, because you may miss real errors as a result.</para>
1376 <varlistentry id="opt.show-realloc-size-zero"
1377 xreflabel="--show-realloc-size-zero">
1379 <option><![CDATA[--show-realloc-size-zero=<yes|no> [default: yes] ]]></option>
1382 <para>When enabled, Memcheck checks for uses of <varname>realloc</varname> with a size of zero.
1383 This usage of <varname>realloc</varname> is unsafe since it is not portable. On some systems it
1384 will behave like <varname>free</varname>. On other systems it will either do nothing or else
1385 behave like a call to <varname>free</varname> followed by a call to <varname>malloc</varname>
1386 with a size of zero.</para>
1390 <varlistentry id="opt.ignore-ranges" xreflabel="--ignore-ranges">
1392 <option><![CDATA[--ignore-ranges=0xPP-0xQQ[,0xRR-0xSS] ]]></option>
1395 <para>Any ranges listed in this option (and multiple ranges can be
1396 specified, separated by commas) will be ignored by Memcheck's
1397 addressability checking.</para>
1401 <varlistentry id="opt.malloc-fill" xreflabel="--malloc-fill">
1403 <option><![CDATA[--malloc-fill=<hexnumber> ]]></option>
1406 <para>Fills blocks allocated
1407 by <computeroutput>malloc</computeroutput>,
1408 <computeroutput>new</computeroutput>, etc, but not
1409 by <computeroutput>calloc</computeroutput>, with the specified
1410 byte. This can be useful when trying to shake out obscure
1411 memory corruption problems. The allocated area is still
1412 regarded by Memcheck as undefined -- this option only affects its
1413 contents. Note that <option>--malloc-fill</option> does not
1414 affect a block of memory when it is used as argument
1415 to client requests VALGRIND_MEMPOOL_ALLOC or
1416 VALGRIND_MALLOCLIKE_BLOCK.
1421 <varlistentry id="opt.free-fill" xreflabel="--free-fill">
1423 <option><![CDATA[--free-fill=<hexnumber> ]]></option>
1426 <para>Fills blocks freed
1427 by <computeroutput>free</computeroutput>,
1428 <computeroutput>delete</computeroutput>, etc, with the
1429 specified byte value. This can be useful when trying to shake out
1430 obscure memory corruption problems. The freed area is still
1431 regarded by Memcheck as not valid for access -- this option only
1432 affects its contents. Note that <option>--free-fill</option> does not
1433 affect a block of memory when it is used as argument to
1434 client requests VALGRIND_MEMPOOL_FREE or VALGRIND_FREELIKE_BLOCK.
1440 <!-- end of xi:include in the manpage -->
1445 <sect1 id="mc-manual.suppfiles" xreflabel="Writing suppression files">
1446 <title>Writing suppression files</title>
1448 <para>The basic suppression format is described in
1449 <xref linkend="manual-core.suppress"/>.</para>
1451 <para>The suppression-type (second) line should have the form:</para>
1452 <programlisting><![CDATA[
1453 Memcheck:suppression_type]]></programlisting>
1455 <para>The Memcheck suppression types are as follows:</para>
1459 <para><varname>Value1</varname>,
1460 <varname>Value2</varname>,
1461 <varname>Value4</varname>,
1462 <varname>Value8</varname>,
1463 <varname>Value16</varname>,
1464 meaning an uninitialised-value error when
1465 using a value of 1, 2, 4, 8 or 16 bytes.</para>
1469 <para><varname>Cond</varname> (or its old
1470 name, <varname>Value0</varname>), meaning use
1471 of an uninitialised CPU condition code.</para>
1475 <para><varname>Addr1</varname>,
1476 <varname>Addr2</varname>,
1477 <varname>Addr4</varname>,
1478 <varname>Addr8</varname>,
1479 <varname>Addr16</varname>,
1480 meaning an invalid address during a
1481 memory access of 1, 2, 4, 8 or 16 bytes respectively.</para>
1485 <para><varname>Jump</varname>, meaning an
1486 jump to an unaddressable location error.</para>
1490 <para><varname>Param</varname>, meaning an
1491 invalid system call parameter error.</para>
1495 <para><varname>Free</varname>, meaning an
1496 invalid or mismatching free.</para>
1500 <para><varname>Overlap</varname>, meaning a
1501 <computeroutput>src</computeroutput> /
1502 <computeroutput>dst</computeroutput> overlap in
1503 <function>memcpy</function> or a similar function.</para>
1507 <para><varname>Leak</varname>, meaning
1508 a memory leak.</para>
1513 <para><computeroutput>Param</computeroutput> errors have a mandatory extra
1514 information line at this point, which is the name of the offending
1515 system call parameter. </para>
1517 <para><computeroutput>Leak</computeroutput> errors have an optional
1518 extra information line, with the following format:</para>
1519 <programlisting><![CDATA[
1520 match-leak-kinds:<set>]]></programlisting>
1521 <para>where <computeroutput><set></computeroutput> specifies which
1522 leak kinds are matched by this suppression entry.
1523 <computeroutput><set></computeroutput> is specified in the
1524 same way as with the option <option>--show-leak-kinds</option>, that is,
1525 one of the following:</para>
1527 <listitem><para>a comma separated list of one or more of
1528 <option>definite indirect possible reachable</option>.</para>
1531 <listitem><para><option>all</option> to specify the complete set
1532 (all leak kinds).</para>
1535 <listitem><para><option>none</option> for the empty set.</para>
1538 <para>If this optional extra line is not present, the suppression
1539 entry will match all leak kinds.</para>
1541 <para>Be aware that leak suppressions that are created using
1542 <option>--gen-suppressions</option> will contain this optional extra
1543 line, and therefore may match fewer leaks than you expect. You may
1544 want to remove the line before using the generated
1545 suppressions.</para>
1547 <para>The other Memcheck error kinds do not have extra lines.</para>
1550 If you give the <option>-v</option> option, Valgrind will print
1551 the list of used suppressions at the end of execution.
1552 For a leak suppression, this output gives the number of different
1553 loss records that match the suppression, and the number of bytes
1554 and blocks suppressed by the suppression.
1555 If the run contains multiple leak checks, the number of bytes and blocks
1556 are reset to zero before each new leak check. Note that the number of different
1557 loss records is not reset to zero.</para>
1558 <para>In the example below, in the last leak search, 7 blocks and 96 bytes have
1559 been suppressed by a suppression with the name
1560 <option>some_leak_suppression</option>:</para>
1561 <programlisting><![CDATA[
1562 --21041-- used_suppression: 10 some_other_leak_suppression s.supp:14 suppressed: 12,400 bytes in 1 blocks
1563 --21041-- used_suppression: 39 some_leak_suppression s.supp:2 suppressed: 96 bytes in 7 blocks
1564 ]]></programlisting>
1566 <para>For <varname>ValueN</varname> and <varname>AddrN</varname>
1567 errors, the first line of the calling context is either the name of
1568 the function in which the error occurred, or, failing that, the full
1569 path of the <filename>.so</filename> file or executable containing the
1570 error location. For <varname>Free</varname> errors, the first line is
1571 the name of the function doing the freeing (eg,
1572 <function>free</function>, <function>__builtin_vec_delete</function>,
1573 etc). For <varname>Overlap</varname> errors, the first line is the name of the
1574 function with the overlapping arguments (eg.
1575 <function>memcpy</function>, <function>strcpy</function>, etc).</para>
1577 <para>The last part of any suppression specifies the rest of the
1578 calling context that needs to be matched.</para>
1584 <sect1 id="mc-manual.machine"
1585 xreflabel="Details of Memcheck's checking machinery">
1586 <title>Details of Memcheck's checking machinery</title>
1588 <para>Read this section if you want to know, in detail, exactly
1589 what and how Memcheck is checking.</para>
1592 <sect2 id="mc-manual.value" xreflabel="Valid-value (V) bit">
1593 <title>Valid-value (V) bits</title>
1595 <para>It is simplest to think of Memcheck implementing a synthetic CPU
1596 which is identical to a real CPU, except for one crucial detail. Every
1597 bit (literally) of data processed, stored and handled by the real CPU
1598 has, in the synthetic CPU, an associated "valid-value" bit, which says
1599 whether or not the accompanying bit has a legitimate value. In the
1600 discussions which follow, this bit is referred to as the V (valid-value)
1603 <para>Each byte in the system therefore has a 8 V bits which follow it
1604 wherever it goes. For example, when the CPU loads a word-size item (4
1605 bytes) from memory, it also loads the corresponding 32 V bits from a
1606 bitmap which stores the V bits for the process' entire address space.
1607 If the CPU should later write the whole or some part of that value to
1608 memory at a different address, the relevant V bits will be stored back
1609 in the V-bit bitmap.</para>
1611 <para>In short, each bit in the system has (conceptually) an associated V
1612 bit, which follows it around everywhere, even inside the CPU. Yes, all the
1613 CPU's registers (integer, floating point, vector and condition registers)
1614 have their own V bit vectors. For this to work, Memcheck uses a great deal
1615 of compression to represent the V bits compactly.</para>
1617 <para>Copying values around does not cause Memcheck to check for, or
1618 report on, errors. However, when a value is used in a way which might
1619 conceivably affect your program's externally-visible behaviour,
1620 the associated V bits are immediately checked. If any of these indicate
1621 that the value is undefined (even partially), an error is reported.</para>
1623 <para>Here's an (admittedly nonsensical) example:</para>
1624 <programlisting><![CDATA[
1627 for ( i = 0; i < 10; i++ ) {
1630 }]]></programlisting>
1632 <para>Memcheck emits no complaints about this, since it merely copies
1633 uninitialised values from <varname>a[]</varname> into
1634 <varname>b[]</varname>, and doesn't use them in a way which could
1635 affect the behaviour of the program. However, if
1636 the loop is changed to:</para>
1637 <programlisting><![CDATA[
1638 for ( i = 0; i < 10; i++ ) {
1642 printf("hello there\n");
1643 ]]></programlisting>
1645 <para>then Memcheck will complain, at the
1646 <computeroutput>if</computeroutput>, that the condition depends on
1647 uninitialised values. Note that it <command>doesn't</command> complain
1648 at the <varname>j += a[i];</varname>, since at that point the
1649 undefinedness is not "observable". It's only when a decision has to be
1650 made as to whether or not to do the <function>printf</function> -- an
1651 observable action of your program -- that Memcheck complains.</para>
1653 <para>Most low level operations, such as adds, cause Memcheck to use the
1654 V bits for the operands to calculate the V bits for the result. Even if
1655 the result is partially or wholly undefined, it does not
1658 <para>Checks on definedness only occur in three places: when a value is
1659 used to generate a memory address, when control flow decision needs to
1660 be made, and when a system call is detected, Memcheck checks definedness
1661 of parameters as required.</para>
1663 <para>If a check should detect undefinedness, an error message is
1664 issued. The resulting value is subsequently regarded as well-defined.
1665 To do otherwise would give long chains of error messages. In other
1666 words, once Memcheck reports an undefined value error, it tries to
1667 avoid reporting further errors derived from that same undefined
1670 <para>This sounds overcomplicated. Why not just check all reads from
1671 memory, and complain if an undefined value is loaded into a CPU
1672 register? Well, that doesn't work well, because perfectly legitimate C
1673 programs routinely copy uninitialised values around in memory, and we
1674 don't want endless complaints about that. Here's the canonical example.
1675 Consider a struct like this:</para>
1676 <programlisting><![CDATA[
1677 struct S { int x; char c; };
1682 ]]></programlisting>
1684 <para>The question to ask is: how large is <varname>struct S</varname>,
1685 in bytes? An <varname>int</varname> is 4 bytes and a
1686 <varname>char</varname> one byte, so perhaps a <varname>struct
1687 S</varname> occupies 5 bytes? Wrong. All non-toy compilers we know
1688 of will round the size of <varname>struct S</varname> up to a whole
1689 number of words, in this case 8 bytes. Not doing this forces compilers
1690 to generate truly appalling code for accessing arrays of
1691 <varname>struct S</varname>'s on some architectures.</para>
1693 <para>So <varname>s1</varname> occupies 8 bytes, yet only 5 of them will
1694 be initialised. For the assignment <varname>s2 = s1</varname>, GCC
1695 generates code to copy all 8 bytes wholesale into <varname>s2</varname>
1696 without regard for their meaning. If Memcheck simply checked values as
1697 they came out of memory, it would yelp every time a structure assignment
1698 like this happened. So the more complicated behaviour described above
1699 is necessary. This allows GCC to copy
1700 <varname>s1</varname> into <varname>s2</varname> any way it likes, and a
1701 warning will only be emitted if the uninitialised values are later
1704 <para>As explained above, Memcheck maintains 8 V bits for each byte in your
1705 process, including for bytes that are in shared memory. However, the same piece
1706 of shared memory can be mapped multiple times, by several processes or even by
1707 the same process (for example, if the process wants a read-only and a read-write
1708 mapping of the same page). For such multiple mappings, Memcheck tracks the V
1709 bits for each mapping independently. This can lead to false positive errors, as
1710 the shared memory can be initialised via a first mapping, and accessed via
1711 another mapping. The access via this other mapping will have its own V bits,
1712 which have not been changed when the memory was initialised via the first
1713 mapping. The bypass for these false positives is to use Memcheck's client
1714 requests <varname>VALGRIND_MAKE_MEM_DEFINED</varname> and
1715 <varname>VALGRIND_MAKE_MEM_UNDEFINED</varname> to inform
1716 Memcheck about what your program does (or what another process does)
1717 to these shared memory mappings.
1723 <sect2 id="mc-manual.vaddress" xreflabel=" Valid-address (A) bits">
1724 <title>Valid-address (A) bits</title>
1726 <para>Notice that the previous subsection describes how the validity of
1727 values is established and maintained without having to say whether the
1728 program does or does not have the right to access any particular memory
1729 location. We now consider the latter question.</para>
1731 <para>As described above, every bit in memory or in the CPU has an
1732 associated valid-value (V) bit. In addition, all bytes in memory, but
1733 not in the CPU, have an associated valid-address (A) bit. This
1734 indicates whether or not the program can legitimately read or write that
1735 location. It does not give any indication of the validity of the data
1736 at that location -- that's the job of the V bits -- only whether or not
1737 the location may be accessed.</para>
1739 <para>Every time your program reads or writes memory, Memcheck checks
1740 the A bits associated with the address. If any of them indicate an
1741 invalid address, an error is emitted. Note that the reads and writes
1742 themselves do not change the A bits, only consult them.</para>
1744 <para>So how do the A bits get set/cleared? Like this:</para>
1748 <para>When the program starts, all the global data areas are
1749 marked as accessible.</para>
1753 <para>When the program does
1754 <function>malloc</function>/<computeroutput>new</computeroutput>,
1755 the A bits for exactly the area allocated, and not a byte more,
1756 are marked as accessible. Upon freeing the area the A bits are
1757 changed to indicate inaccessibility.</para>
1761 <para>When the stack pointer register (<literal>SP</literal>) moves
1762 up or down, A bits are set. The rule is that the area from
1763 <literal>SP</literal> up to the base of the stack is marked as
1764 accessible, and below <literal>SP</literal> is inaccessible. (If
1765 that sounds illogical, bear in mind that the stack grows down, not
1766 up, on almost all Unix systems, including GNU/Linux.) Tracking
1767 <literal>SP</literal> like this has the useful side-effect that the
1768 section of stack used by a function for local variables etc is
1769 automatically marked accessible on function entry and inaccessible
1774 <para>When doing system calls, A bits are changed appropriately.
1775 For example, <literal>mmap</literal>
1776 magically makes files appear in the process'
1777 address space, so the A bits must be updated if <literal>mmap</literal>
1782 <para>Optionally, your program can tell Memcheck about such changes
1783 explicitly, using the client request mechanism described
1792 <sect2 id="mc-manual.together" xreflabel="Putting it all together">
1793 <title>Putting it all together</title>
1795 <para>Memcheck's checking machinery can be summarised as
1800 <para>Each byte in memory has 8 associated V (valid-value) bits,
1801 saying whether or not the byte has a defined value, and a single A
1802 (valid-address) bit, saying whether or not the program currently has
1803 the right to read/write that address. As mentioned above, heavy
1804 use of compression means the overhead is typically around 25%.</para>
1808 <para>When memory is read or written, the relevant A bits are
1809 consulted. If they indicate an invalid address, Memcheck emits an
1810 Invalid read or Invalid write error.</para>
1814 <para>When memory is read into the CPU's registers, the relevant V
1815 bits are fetched from memory and stored in the simulated CPU. They
1816 are not consulted.</para>
1820 <para>When a register is written out to memory, the V bits for that
1821 register are written back to memory too.</para>
1825 <para>When values in CPU registers are used to generate a memory
1826 address, or to determine the outcome of a conditional branch, the V
1827 bits for those values are checked, and an error emitted if any of
1828 them are undefined.</para>
1832 <para>When values in CPU registers are used for any other purpose,
1833 Memcheck computes the V bits for the result, but does not check
1838 <para>Once the V bits for a value in the CPU have been checked, they
1839 are then set to indicate validity. This avoids long chains of
1844 <para>When values are loaded from memory, Memcheck checks the A bits
1845 for that location and issues an illegal-address warning if needed.
1846 In that case, the V bits loaded are forced to indicate Valid,
1847 despite the location being invalid.</para>
1849 <para>This apparently strange choice reduces the amount of confusing
1850 information presented to the user. It avoids the unpleasant
1851 phenomenon in which memory is read from a place which is both
1852 unaddressable and contains invalid values, and, as a result, you get
1853 not only an invalid-address (read/write) error, but also a
1854 potentially large set of uninitialised-value errors, one for every
1855 time the value is used.</para>
1857 <para>There is a hazy boundary case to do with multi-byte loads from
1858 addresses which are partially valid and partially invalid. See
1859 details of the option <option>--partial-loads-ok</option> for details.
1866 <para>Memcheck intercepts calls to <function>malloc</function>,
1867 <function>calloc</function>, <function>realloc</function>,
1868 <function>valloc</function>, <function>memalign</function>,
1869 <function>free</function>, <computeroutput>new</computeroutput>,
1870 <computeroutput>new[]</computeroutput>,
1871 <computeroutput>delete</computeroutput> and
1872 <computeroutput>delete[]</computeroutput>. The behaviour you get
1878 <para><function>malloc</function>/<function>new</function>/<computeroutput>new[]</computeroutput>:
1879 the returned memory is marked as addressable but not having valid
1880 values. This means you have to write to it before you can read
1885 <para><function>calloc</function>: returned memory is marked both
1886 addressable and valid, since <function>calloc</function> clears
1887 the area to zero.</para>
1891 <para><function>realloc</function>: if the new size is larger than
1892 the old, the new section is addressable but invalid, as with
1893 <function>malloc</function>. If the new size is smaller, the
1894 dropped-off section is marked as unaddressable. You may only pass to
1895 <function>realloc</function> a pointer previously issued to you by
1896 <function>malloc</function>/<function>calloc</function>/<function>realloc</function>.</para>
1900 <para><function>free</function>/<computeroutput>delete</computeroutput>/<computeroutput>delete[]</computeroutput>:
1901 you may only pass to these functions a pointer previously issued
1902 to you by the corresponding allocation function. Otherwise,
1903 Memcheck complains. If the pointer is indeed valid, Memcheck
1904 marks the entire area it points at as unaddressable, and places
1905 the block in the freed-blocks-queue. The aim is to defer as long
1906 as possible reallocation of this block. Until that happens, all
1907 attempts to access it will elicit an invalid-address error, as you
1916 <sect1 id="mc-manual.monitor-commands" xreflabel="Memcheck Monitor Commands">
1917 <title>Memcheck Monitor Commands</title>
1918 <para>The Memcheck tool provides monitor commands handled by Valgrind's built-in
1919 gdbserver (see <xref linkend="manual-core-adv.gdbserver-commandhandling"/>).
1920 Valgrind python code provides GDB front end commands giving an easier usage of
1921 the memcheck monitor commands (see
1922 <xref linkend="manual-core-adv.gdbserver-gdbmonitorfrontend"/>). To launch a
1923 memcheck monitor command via its GDB front end command, instead of prefixing
1924 the command with "monitor", you must use the GDB <varname>memcheck</varname>
1925 command (or the shorter aliases <varname>mc</varname>). Using the memcheck
1926 GDB front end command provide a more flexible usage, such as evaluation of
1927 address and length arguments by GDB. In GDB, you can use <varname>help
1928 memcheck</varname> to get help about the memcheck front end monitor commands
1929 and you can use <varname>apropos memcheck</varname> to get all the commands
1930 mentionning the word "memcheck" in their name or on-line help.
1935 <para><varname>xb <addr> [<len>]</varname>
1936 shows the definedness (V) bits and values for <len> (default 1)
1937 bytes starting at <addr>.
1938 For each 8 bytes, two lines are output.
1941 The first line shows the validity bits for 8 bytes.
1942 The definedness of each byte in the range is given using two hexadecimal
1943 digits. These hexadecimal digits encode the validity of each bit of the
1945 using 0 if the bit is defined and 1 if the bit is undefined.
1946 If a byte is not addressable, its validity bits are replaced
1947 by <varname>__</varname> (a double underscore).
1950 The second line shows the values of the bytes below the corresponding
1951 validity bits. The format used to show the bytes data is similar to the
1952 GDB command 'x /<len>xb <addr>'. The value for a non
1953 addressable bytes is shown as ?? (two question marks).
1956 In the following example, <varname>string10</varname> is an array
1957 of 10 characters, in which the even numbered bytes are
1958 undefined. In the below example, the byte corresponding
1959 to <varname>string10[5]</varname> is not addressable.
1961 <programlisting><![CDATA[
1963 $4 = (char (*)[10]) 0x804a2f0
1964 (gdb) mo xb 0x804a2f0 10
1965 ff 00 ff 00 ff __ ff 00
1966 0x804A2F0: 0x3f 0x6e 0x3f 0x65 0x3f 0x?? 0x3f 0x65
1968 0x804A2F8: 0x3f 0x00
1969 Address 0x804A2F0 len 10 has 1 bytes unaddressable
1971 ]]></programlisting>
1973 <para>The GDB memcheck front end command <varname>memcheck xb ADDR
1974 [LEN]</varname> accepts any address expression for its first ADDR
1975 argument. The second optional argument is any integer expression. Note
1976 that these 2 arguments must be separated by a space.
1977 The following example shows how to get the definedness of
1978 <varname>string10</varname> using the memcheck xb front end command.
1980 <programlisting><![CDATA[
1981 (gdb) mc xb &string10 sizeof(string10)
1982 ff 00 ff 00 ff __ ff 00
1983 0x804A2F0: 0x3f 0x6e 0x3f 0x65 0x3f 0x?? 0x3f 0x65
1985 0x804A2F8: 0x3f 0x00
1986 Address 0x804A2F0 len 10 has 1 bytes unaddressable
1988 ]]></programlisting>
1990 <para> The command xb cannot be used with registers. To get
1991 the validity bits of a register, you must start Valgrind with the
1992 option <option>--vgdb-shadow-registers=yes</option>. The validity
1993 bits of a register can then be obtained by printing the 'shadow 1'
1994 corresponding register. In the below x86 example, the register
1995 eax has all its bits undefined, while the register ebx is fully
1998 <programlisting><![CDATA[
2004 ]]></programlisting>
2009 <para><varname>get_vbits <addr> [<len>]</varname>
2010 shows the definedness (V) bits for <len> (default 1) bytes
2011 starting at <addr> using the same convention as the
2012 <varname>xb</varname> command. <varname>get_vbits</varname> only
2013 shows the V bits (grouped by 4 bytes). It does not show the values.
2014 If you want to associate V bits with the corresponding byte values, the
2015 <varname>xb</varname> command will be easier to use, in particular
2016 on little endian computers when associating undefined parts of an integer
2017 with their V bits values.
2020 The following example shows the result of <varname>get_vbits</varname> on
2021 the <varname>string10</varname> used in the <varname>xb</varname> command
2022 explanation. The GDB memcheck equivalent front end command <varname>memcheck
2023 get_vbits ADDR [LEN]</varname>accepts any ADDR expression and any LEN
2024 expression (separated by a space).
2026 <programlisting><![CDATA[
2027 (gdb) monitor get_vbits 0x804a2f0 10
2028 ff00ff00 ff__ff00 ff00
2029 Address 0x804A2F0 len 10 has 1 bytes unaddressable
2030 (gdb) memcheck get_vbits &string10 sizeof(string10)
2031 ff00ff00 ff__ff00 ff00
2032 Address 0x804A2F0 len 10 has 1 bytes unaddressable
2033 ]]></programlisting>
2038 <para><varname>make_memory
2039 [noaccess|undefined|defined|Definedifaddressable] <addr>
2040 [<len>]</varname> marks the range of <len> (default 1)
2041 bytes at <addr> as having the given status. Parameter
2042 <varname>noaccess</varname> marks the range as non-accessible, so
2043 Memcheck will report an error on any access to it.
2044 <varname>undefined</varname> or <varname>defined</varname> mark
2045 the area as accessible, but Memcheck regards the bytes in it
2046 respectively as having undefined or defined values.
2047 <varname>Definedifaddressable</varname> marks as defined, bytes in
2048 the range which are already addressible, but makes no change to
2049 the status of bytes in the range which are not addressible. Note
2050 that the first letter of <varname>Definedifaddressable</varname>
2051 is an uppercase D to avoid confusion with <varname>defined</varname>.
2054 <para>The GDB equivalent memcheck front end commands <varname>memcheck
2055 make_memory [noaccess|undefined|defined|Definedifaddressable] ADDR
2056 [LEN]</varname> accept any address expression for their first ADDR
2057 argument. The second optional argument is any integer expression. Note
2058 that these 2 arguments must be separated by a space.
2062 In the following example, the first byte of the
2063 <varname>string10</varname> is marked as defined and then is marked
2066 <programlisting><![CDATA[
2067 (gdb) monitor make_memory defined 0x8049e28 1
2068 (gdb) monitor get_vbits 0x8049e28 10
2069 0000ff00 ff00ff00 ff00
2070 (gdb) memcheck make_memory noaccess &string10[0]
2071 (gdb) memcheck get_vbits &string10 sizeof(string10)
2072 __00ff00 ff00ff00 ff00
2073 Address 0x8049E28 len 10 has 1 bytes unaddressable
2075 ]]></programlisting>
2079 <para><varname>check_memory [addressable|defined] <addr>
2080 [<len>]</varname> checks that the range of <len>
2081 (default 1) bytes at <addr> has the specified accessibility.
2082 It then outputs a description of <addr>. In the following
2083 example, a detailed description is available because the
2084 option <option>--read-var-info=yes</option> was given at Valgrind
2087 <programlisting><![CDATA[
2088 (gdb) monitor check_memory defined 0x8049e28 1
2089 Address 0x8049E28 len 1 defined
2090 ==14698== Location 0x8049e28 is 0 bytes inside string10[0],
2091 ==14698== declared at prog.c:10, in frame #0 of thread 1
2093 ]]></programlisting>
2094 <para>The GDB equivalent memcheck front end commands <varname>memcheck
2095 check_memory [addressable|defined] ADDR [LEN]</varname> accept any address
2096 expression for their first ADDR argument. The second optional argument is
2097 any integer expression. Note that these 2 arguments must be separated by a
2104 <para><varname>leak_check [full*|summary|xtleak]
2105 [kinds <set>|reachable|possibleleak*|definiteleak]
2106 [heuristics heur1,heur2,...]
2107 [new|increased*|changed|any]
2108 [unlimited*|limited <max_loss_records_output>]
2110 performs a leak check. The <varname>*</varname> in the arguments
2111 indicates the default values. </para>
2113 <para> If the <varname>[full*|summary|xtleak]</varname> argument is
2114 <varname>summary</varname>, only a summary of the leak search is given;
2115 otherwise a full leak report is produced. A full leak report gives
2116 detailed information for each leak: the stack trace where the leaked blocks
2117 were allocated, the number of blocks leaked and their total size. When a
2118 full report is requested, the next two arguments further specify what
2119 kind of leaks to report. A leak's details are shown if they match
2120 both the second and third argument. A full leak report might
2121 output detailed information for many leaks. The nr of leaks for
2122 which information is output can be controlled using
2123 the <varname>limited</varname> argument followed by the maximum nr
2124 of leak records to output. If this maximum is reached, the leak
2125 search outputs the records with the biggest number of bytes.
2127 <para>The value <varname>xtleak</varname> also produces a full leak report,
2128 but output it as an xtree in a file xtleak.kcg.%p.%n (see <xref linkend="opt.log-file"/>).
2129 See <xref linkend="&vg-xtree-id;"/>
2130 for a detailed explanation about execution trees formats.
2131 See <xref linkend="opt.xtree-leak"/> for the description of the events
2132 in a xtree leak file.
2135 <para>The <varname>kinds</varname> argument controls what kind of blocks
2136 are shown for a <varname>full</varname> leak search. The set of leak kinds
2137 to show can be specified using a <varname><set></varname> similarly
2138 to the command line option <option>--show-leak-kinds</option>.
2139 Alternatively, the value <varname>definiteleak</varname>
2140 is equivalent to <varname>kinds definite</varname>, the
2141 value <varname>possibleleak</varname> is equivalent to
2142 <varname>kinds definite,possible</varname> : it will also show
2143 possibly leaked blocks, .i.e those for which only an interior
2144 pointer was found. The value <varname>reachable</varname> will
2145 show all block categories (i.e. is equivalent to <varname>kinds
2149 <para>The <varname>heuristics</varname> argument controls the heuristics
2150 used during the leak search. The set of heuristics to use can be specified
2151 using a <varname><set></varname> similarly
2152 to the command line option <option>--leak-check-heuristics</option>.
2153 The default value for the <varname>heuristics</varname> argument is
2154 <varname>heuristics none</varname>.
2157 <para>The <varname>[new|increased*|changed|any]</varname> argument controls
2158 what kinds of changes are shown for a <varname>full</varname> leak search.
2159 The value <varname>increased</varname> specifies that only block
2160 allocation stacks with an increased number of leaked bytes or
2161 blocks since the previous leak check should be shown. The
2162 value <varname>changed</varname> specifies that allocation stacks
2163 with any change since the previous leak check should be shown.
2164 The value <varname>new</varname> specifies to show only the block
2165 allocation stacks that are new since the previous leak search.
2166 The value <varname>any</varname> specifies that all leak entries
2167 should be shown, regardless of any increase or decrease.
2168 If <varname>new</varname> or <varname>increased</varname> or
2169 <varname>changed</varname> are specified, the leak report entries will show
2170 the delta relative to the previous leak report and the new loss records
2171 will have a "new" marker (even when <varname>increased</varname> or
2172 <varname>changed</varname> were specified).
2175 <para>The following example shows usage of the
2176 <varname>leak_check</varname> monitor command on
2177 the <varname>memcheck/tests/leak-cases.c</varname> regression
2178 test. The first command outputs one entry having an increase in
2179 the leaked bytes. The second command is the same as the first
2180 command, but uses the abbreviated forms accepted by GDB and the
2181 Valgrind gdbserver. It only outputs the summary information, as
2182 there was no increase since the previous leak search.</para>
2183 <programlisting><![CDATA[
2184 (gdb) monitor leak_check full possibleleak increased
2185 ==19520== 16 (+16) bytes in 1 (+1) blocks are possibly lost in new loss record 9 of 12
2186 ==19520== at 0x40070B4: malloc (vg_replace_malloc.c:263)
2187 ==19520== by 0x80484D5: mk (leak-cases.c:52)
2188 ==19520== by 0x804855F: f (leak-cases.c:81)
2189 ==19520== by 0x80488E0: main (leak-cases.c:107)
2191 ==19520== LEAK SUMMARY:
2192 ==19520== definitely lost: 32 (+0) bytes in 2 (+0) blocks
2193 ==19520== indirectly lost: 16 (+0) bytes in 1 (+0) blocks
2194 ==19520== possibly lost: 32 (+16) bytes in 2 (+1) blocks
2195 ==19520== still reachable: 96 (+16) bytes in 6 (+1) blocks
2196 ==19520== suppressed: 0 (+0) bytes in 0 (+0) blocks
2197 ==19520== Reachable blocks (those to which a pointer was found) are not shown.
2198 ==19520== To see them, add 'reachable any' args to leak_check
2201 ==19520== LEAK SUMMARY:
2202 ==19520== definitely lost: 32 (+0) bytes in 2 (+0) blocks
2203 ==19520== indirectly lost: 16 (+0) bytes in 1 (+0) blocks
2204 ==19520== possibly lost: 32 (+0) bytes in 2 (+0) blocks
2205 ==19520== still reachable: 96 (+0) bytes in 6 (+0) blocks
2206 ==19520== suppressed: 0 (+0) bytes in 0 (+0) blocks
2207 ==19520== Reachable blocks (those to which a pointer was found) are not shown.
2208 ==19520== To see them, add 'reachable any' args to leak_check
2211 ]]></programlisting>
2212 <para>Note that when using Valgrind's gdbserver, it is not
2214 with <option>--leak-check=full</option>
2215 <option>--show-reachable=yes</option> to see the reachable
2216 blocks. You can obtain the same information without rerunning by
2217 using the GDB command <computeroutput>monitor leak_check full
2218 reachable any</computeroutput> (or, using
2219 abbreviation: <computeroutput>mo l f r a</computeroutput>).
2222 <para>The GDB equivalent memcheck front end command <varname>memcheck
2223 leak_check</varname> auto-completes the user input by providing the full
2224 list of keywords still relevant according to what is already typed. For
2225 example, if the "summary" keyword has been provided, the following TABs to
2226 auto-complete other items will not propose anymore "full" and "xtleak".
2227 Note that KIND and HEUR values are not part of auto-completed elements.
2233 <para><varname>block_list <loss_record_nr>|<loss_record_nr_from>..<loss_record_nr_to>
2234 [unlimited*|limited <max_blocks>]
2235 [heuristics heur1,heur2,...]
2237 shows the list of blocks belonging to
2238 <varname><loss_record_nr></varname> (or to the loss records range
2239 <varname><loss_record_nr_from>..<loss_record_nr_to></varname>).
2240 The nr of blocks to print can be controlled using the
2241 <varname>limited</varname> argument followed by the maximum nr
2242 of blocks to output.
2243 If one or more heuristics are given, only prints the loss records
2244 and blocks found via one of the given <varname>heur1,heur2,...</varname>
2248 <para> A leak search merges the allocated blocks in loss records :
2249 a loss record re-groups all blocks having the same state (for
2250 example, Definitely Lost) and the same allocation backtrace.
2251 Each loss record is identified in the leak search result
2252 by a loss record number.
2253 The <varname>block_list</varname> command shows the loss record information
2254 followed by the addresses and sizes of the blocks which have been
2255 merged in the loss record. If a block was found using an heuristic, the block size
2256 is followed by the heuristic.
2259 <para> If a directly lost block causes some other blocks to be indirectly
2260 lost, the block_list command will also show these indirectly lost blocks.
2261 The indirectly lost blocks will be indented according to the level of indirection
2262 between the directly lost block and the indirectly lost block(s).
2263 Each indirectly lost block is followed by the reference of its loss record.
2266 <para> The block_list command can be used on the results of a leak search as long
2267 as no block has been freed after this leak search: as soon as the program frees
2268 a block, a new leak search is needed before block_list can be used again.
2272 In the below example, the program leaks a tree structure by losing the pointer to
2273 the block A (top of the tree).
2274 So, the block A is directly lost, causing an indirect
2275 loss of blocks B to G. The first block_list command shows the loss record of A
2276 (a definitely lost block with address 0x4028028, size 16). The addresses and sizes
2277 of the indirectly lost blocks due to block A are shown below the block A.
2278 The second command shows the details of one of the indirect loss records output
2279 by the first command.
2281 <programlisting><![CDATA[
2287 ]]></programlisting>
2289 <programlisting><![CDATA[
2291 #0 main () at leak-tree.c:69
2292 (gdb) monitor leak_check full any
2293 ==19552== 112 (16 direct, 96 indirect) bytes in 1 blocks are definitely lost in loss record 7 of 7
2294 ==19552== at 0x40070B4: malloc (vg_replace_malloc.c:263)
2295 ==19552== by 0x80484D5: mk (leak-tree.c:28)
2296 ==19552== by 0x80484FC: f (leak-tree.c:41)
2297 ==19552== by 0x8048856: main (leak-tree.c:63)
2299 ==19552== LEAK SUMMARY:
2300 ==19552== definitely lost: 16 bytes in 1 blocks
2301 ==19552== indirectly lost: 96 bytes in 6 blocks
2302 ==19552== possibly lost: 0 bytes in 0 blocks
2303 ==19552== still reachable: 0 bytes in 0 blocks
2304 ==19552== suppressed: 0 bytes in 0 blocks
2306 (gdb) monitor block_list 7
2307 ==19552== 112 (16 direct, 96 indirect) bytes in 1 blocks are definitely lost in loss record 7 of 7
2308 ==19552== at 0x40070B4: malloc (vg_replace_malloc.c:263)
2309 ==19552== by 0x80484D5: mk (leak-tree.c:28)
2310 ==19552== by 0x80484FC: f (leak-tree.c:41)
2311 ==19552== by 0x8048856: main (leak-tree.c:63)
2312 ==19552== 0x4028028[16]
2313 ==19552== 0x4028068[16] indirect loss record 1
2314 ==19552== 0x40280E8[16] indirect loss record 3
2315 ==19552== 0x4028128[16] indirect loss record 4
2316 ==19552== 0x40280A8[16] indirect loss record 2
2317 ==19552== 0x4028168[16] indirect loss record 5
2318 ==19552== 0x40281A8[16] indirect loss record 6
2320 ==19552== 16 bytes in 1 blocks are indirectly lost in loss record 2 of 7
2321 ==19552== at 0x40070B4: malloc (vg_replace_malloc.c:263)
2322 ==19552== by 0x80484D5: mk (leak-tree.c:28)
2323 ==19552== by 0x8048519: f (leak-tree.c:43)
2324 ==19552== by 0x8048856: main (leak-tree.c:63)
2325 ==19552== 0x40280A8[16]
2326 ==19552== 0x4028168[16] indirect loss record 5
2327 ==19552== 0x40281A8[16] indirect loss record 6
2330 ]]></programlisting>
2335 <para><varname>who_points_at <addr> [<len>]</varname>
2336 shows all the locations where a pointer to addr is found.
2337 If len is equal to 1, the command only shows the locations pointing
2338 exactly at addr (i.e. the "start pointers" to addr).
2339 If len is > 1, "interior pointers" pointing at the len first bytes
2343 <para>The locations searched for are the same as the locations
2344 used in the leak search. So, <varname>who_points_at</varname> can a.o.
2345 be used to show why the leak search still can reach a block, or can
2346 search for dangling pointers to a freed block.
2347 Each location pointing at addr (or pointing inside addr if interior pointers
2348 are being searched for) will be described.
2351 <para>The GDB equivalent memcheck front end command <varname>memcheck
2352 who_points_at ADDR [LEN]</varname> accept any address expression for its
2353 first ADDR argument. The second optional argument is any integer
2354 expression. Note that these 2 arguments must be separated by a space.
2357 <para>In the below example, the pointers to the 'tree block A' (see example
2358 in command <varname>block_list</varname>) is shown before the tree was leaked.
2359 The descriptions are detailed as the option <option>--read-var-info=yes</option>
2360 was given at Valgrind startup. The second call shows the pointers (start and interior
2361 pointers) to block G. The block G (0x40281A8) is reachable via block C (0x40280a8)
2362 and register ECX of tid 1 (tid is the Valgrind thread id).
2363 It is "interior reachable" via the register EBX.
2366 <programlisting><![CDATA[
2367 (gdb) monitor who_points_at 0x4028028
2368 ==20852== Searching for pointers to 0x4028028
2369 ==20852== *0x8049e20 points at 0x4028028
2370 ==20852== Location 0x8049e20 is 0 bytes inside global var "t"
2371 ==20852== declared at leak-tree.c:35
2372 (gdb) monitor who_points_at 0x40281A8 16
2373 ==20852== Searching for pointers pointing in 16 bytes from 0x40281a8
2374 ==20852== *0x40280ac points at 0x40281a8
2375 ==20852== Address 0x40280ac is 4 bytes inside a block of size 16 alloc'd
2376 ==20852== at 0x40070B4: malloc (vg_replace_malloc.c:263)
2377 ==20852== by 0x80484D5: mk (leak-tree.c:28)
2378 ==20852== by 0x8048519: f (leak-tree.c:43)
2379 ==20852== by 0x8048856: main (leak-tree.c:63)
2380 ==20852== tid 1 register ECX points at 0x40281a8
2381 ==20852== tid 1 register EBX interior points at 2 bytes inside 0x40281a8
2383 ]]></programlisting>
2385 <para> When <varname>who_points_at</varname> finds an interior pointer,
2386 it will report the heuristic(s) with which this interior pointer
2387 will be considered as reachable. Note that this is done independently
2388 of the value of the option <option>--leak-check-heuristics</option>.
2389 In the below example, the loss record 6 indicates a possibly lost
2390 block. <varname>who_points_at</varname> reports that there is an interior
2391 pointer pointing in this block, and that the block can be considered
2392 reachable using the heuristic
2393 <computeroutput>multipleinheritance</computeroutput>.
2396 <programlisting><![CDATA[
2397 (gdb) monitor block_list 6
2398 ==3748== 8 bytes in 1 blocks are possibly lost in loss record 6 of 7
2399 ==3748== at 0x4007D77: operator new(unsigned int) (vg_replace_malloc.c:313)
2400 ==3748== by 0x8048954: main (leak_cpp_interior.cpp:43)
2401 ==3748== 0x402A0E0[8]
2402 (gdb) monitor who_points_at 0x402A0E0 8
2403 ==3748== Searching for pointers pointing in 8 bytes from 0x402a0e0
2404 ==3748== *0xbe8ee078 interior points at 4 bytes inside 0x402a0e0
2405 ==3748== Address 0xbe8ee078 is on thread 1's stack
2406 ==3748== block at 0x402a0e0 considered reachable by ptr 0x402a0e4 using multipleinheritance heuristic
2408 ]]></programlisting>
2413 <para><varname>xtmemory [<filename> default xtmemory.kcg.%p.%n]</varname>
2414 requests Memcheck tool to produce an xtree heap memory report.
2415 See <xref linkend="&vg-xtree-id;"/> for
2416 a detailed explanation about execution trees. </para>
2423 <sect1 id="mc-manual.clientreqs" xreflabel="Client requests">
2424 <title>Client Requests</title>
2426 <para>The following client requests are defined in
2427 <filename>memcheck.h</filename>.
2428 See <filename>memcheck.h</filename> for exact details of their
2434 <para><varname>VALGRIND_MAKE_MEM_NOACCESS</varname>,
2435 <varname>VALGRIND_MAKE_MEM_UNDEFINED</varname> and
2436 <varname>VALGRIND_MAKE_MEM_DEFINED</varname>.
2437 These mark address ranges as completely inaccessible,
2438 accessible but containing undefined data, and accessible and
2439 containing defined data, respectively. They return -1, when
2440 run on Valgrind and 0 otherwise.</para>
2444 <para><varname>VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE</varname>.
2445 This is just like <varname>VALGRIND_MAKE_MEM_DEFINED</varname> but only
2446 affects those bytes that are already addressable.</para>
2450 <para><varname>VALGRIND_CHECK_MEM_IS_ADDRESSABLE</varname> and
2451 <varname>VALGRIND_CHECK_MEM_IS_DEFINED</varname>: check immediately
2452 whether or not the given address range has the relevant property,
2453 and if not, print an error message. Also, for the convenience of
2454 the client, returns zero if the relevant property holds; otherwise,
2455 the returned value is the address of the first byte for which the
2456 property is not true. Always returns 0 when not run on
2461 <para><varname>VALGRIND_CHECK_VALUE_IS_DEFINED</varname>: a quick and easy
2462 way to find out whether Valgrind thinks a particular value
2463 (lvalue, to be precise) is addressable and defined. Prints an error
2464 message if not. It has no return value.</para>
2468 <para><varname>VALGRIND_DO_LEAK_CHECK</varname>: does a full memory leak
2469 check (like <option>--leak-check=full</option>) right now.
2470 This is useful for incrementally checking for leaks between arbitrary
2471 places in the program's execution. It has no return value.</para>
2475 <para><varname>VALGRIND_DO_ADDED_LEAK_CHECK</varname>: same as
2476 <varname> VALGRIND_DO_LEAK_CHECK</varname> but only shows the
2477 entries for which there was an increase in leaked bytes or leaked
2478 number of blocks since the previous leak search. It has no return
2483 <para><varname>VALGRIND_DO_CHANGED_LEAK_CHECK</varname>: same as
2484 <varname>VALGRIND_DO_LEAK_CHECK</varname> but only shows the
2485 entries for which there was an increase or decrease in leaked
2486 bytes or leaked number of blocks since the previous leak search. It
2487 has no return value.</para>
2491 <para><varname>VALGRIND_DO_NEW_LEAK_CHECK</varname>: same as
2492 <varname> VALGRIND_DO_LEAK_CHECK</varname> but only shows the new
2493 entries since the previous leak search. It has no return value.</para>
2497 <para><varname>VALGRIND_DO_QUICK_LEAK_CHECK</varname>: like
2498 <varname>VALGRIND_DO_LEAK_CHECK</varname>, except it produces only a leak
2499 summary (like <option>--leak-check=summary</option>).
2500 It has no return value.</para>
2504 <para><varname>VALGRIND_COUNT_LEAKS</varname>: fills in the four
2505 arguments with the number of bytes of memory found by the previous
2506 leak check to be leaked (i.e. the sum of direct leaks and indirect leaks),
2507 dubious, reachable and suppressed. This is useful in test harness code,
2508 after calling <varname>VALGRIND_DO_LEAK_CHECK</varname> or
2509 <varname>VALGRIND_DO_QUICK_LEAK_CHECK</varname>.</para>
2513 <para><varname>VALGRIND_COUNT_LEAK_BLOCKS</varname>: identical to
2514 <varname>VALGRIND_COUNT_LEAKS</varname> except that it returns the
2515 number of blocks rather than the number of bytes in each
2520 <para><varname>VALGRIND_GET_VBITS</varname> and
2521 <varname>VALGRIND_SET_VBITS</varname>: allow you to get and set the
2522 V (validity) bits for an address range. You should probably only
2523 set V bits that you have got with
2524 <varname>VALGRIND_GET_VBITS</varname>. Only for those who really
2525 know what they are doing.</para>
2529 <para><varname>VALGRIND_CREATE_BLOCK</varname> and
2530 <varname>VALGRIND_DISCARD</varname>. <varname>VALGRIND_CREATE_BLOCK</varname>
2531 takes an address, a number of bytes and a character string. The
2532 specified address range is then associated with that string. When
2533 Memcheck reports an invalid access to an address in the range, it
2534 will describe it in terms of this block rather than in terms of
2535 any other block it knows about. Note that the use of this macro
2536 does not actually change the state of memory in any way -- it
2537 merely gives a name for the range.
2540 <para>At some point you may want Memcheck to stop reporting errors
2541 in terms of the block named
2542 by <varname>VALGRIND_CREATE_BLOCK</varname>. To make this
2543 possible, <varname>VALGRIND_CREATE_BLOCK</varname> returns a
2544 "block handle", which is a C <varname>int</varname> value. You
2545 can pass this block handle to <varname>VALGRIND_DISCARD</varname>.
2546 After doing so, Valgrind will no longer relate addressing errors
2547 in the specified range to the block. Passing invalid handles to
2548 <varname>VALGRIND_DISCARD</varname> is harmless.
2559 <sect1 id="mc-manual.mempools" xreflabel="Memory Pools">
2560 <title>Memory Pools: describing and working with custom allocators</title>
2562 <para>Some programs use custom memory allocators, often for performance
2563 reasons. Left to itself, Memcheck is unable to understand the
2564 behaviour of custom allocation schemes as well as it understands the
2565 standard allocators, and so may miss errors and leaks in your program. What
2566 this section describes is a way to give Memcheck enough of a description of
2567 your custom allocator that it can make at least some sense of what is
2570 <para>There are many different sorts of custom allocator, so Memcheck
2571 attempts to reason about them using a loose, abstract model. We
2572 use the following terminology when describing custom allocation
2577 <para>Custom allocation involves a set of independent "memory pools".
2581 <para>Memcheck's notion of a a memory pool consists of a single "anchor
2582 address" and a set of non-overlapping "chunks" associated with the
2583 anchor address.</para>
2586 <para>Typically a pool's anchor address is the address of a
2587 book-keeping "header" structure.</para>
2590 <para>Typically the pool's chunks are drawn from a contiguous
2591 "superblock" acquired through the system
2592 <function>malloc</function> or
2593 <function>mmap</function>.</para>
2598 <para>Keep in mind that the last two points above say "typically": the
2599 Valgrind mempool client request API is intentionally vague about the
2600 exact structure of a mempool. There is no specific mention made of
2601 headers or superblocks. Nevertheless, the following picture may help
2602 elucidate the intention of the terms in the API:</para>
2604 <programlisting><![CDATA[
2614 +------+---+--------------+---+------------------+
2615 | |rzB| allocation |rzB| |
2616 +------+---+--------------+---+------------------+
2619 "addr" "addr"+"size"
2620 ]]></programlisting>
2623 Note that the header and the superblock may be contiguous or
2624 discontiguous, and there may be multiple superblocks associated with a
2625 single header; such variations are opaque to Memcheck. The API
2626 only requires that your allocation scheme can present sensible values
2627 of "pool", "addr" and "size".</para>
2630 Typically, before making client requests related to mempools, a client
2631 program will have allocated such a header and superblock for their
2632 mempool, and marked the superblock NOACCESS using the
2633 <varname>VALGRIND_MAKE_MEM_NOACCESS</varname> client request.</para>
2636 When dealing with mempools, the goal is to maintain a particular
2637 invariant condition: that Memcheck believes the unallocated portions
2638 of the pool's superblock (including redzones) are NOACCESS. To
2639 maintain this invariant, the client program must ensure that the
2640 superblock starts out in that state; Memcheck cannot make it so, since
2641 Memcheck never explicitly learns about the superblock of a pool, only
2642 the allocated chunks within the pool.</para>
2645 Once the header and superblock for a pool are established and properly
2646 marked, there are a number of client requests programs can use to
2647 inform Memcheck about changes to the state of a mempool:</para>
2653 <varname>VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed)</varname>:
2654 This request registers the address <varname>pool</varname> as the anchor
2655 address for a memory pool. It also provides a size
2656 <varname>rzB</varname>, specifying how large the redzones placed around
2657 chunks allocated from the pool should be. Finally, it provides an
2658 <varname>is_zeroed</varname> argument that specifies whether the pool's
2659 chunks are zeroed (more precisely: defined) when allocated.
2662 Upon completion of this request, no chunks are associated with the
2663 pool. The request simply tells Memcheck that the pool exists, so that
2664 subsequent calls can refer to it as a pool.
2669 <!-- Note: the below is mostly a copy of valgrind.h. Keep in sync! -->
2671 <varname>VALGRIND_CREATE_MEMPOOL_EXT(pool, rzB, is_zeroed, flags)</varname>:
2672 Create a memory pool with some flags (that can
2673 be OR-ed together) specifying extended behaviour. When flags is
2674 zero, the behaviour is identical to
2675 <varname>VALGRIND_CREATE_MEMPOOL</varname>.</para>
2678 <para> The flag <varname>VALGRIND_MEMPOOL_METAPOOL</varname>
2679 specifies that the pieces of memory associated with the pool
2680 using <varname>VALGRIND_MEMPOOL_ALLOC</varname> will be used
2681 by the application as superblocks to dole out MALLOC_LIKE
2682 blocks using <varname>VALGRIND_MALLOCLIKE_BLOCK</varname>.
2683 In other words, a meta pool is a "2 levels" pool : first
2684 level is the blocks described
2685 by <varname>VALGRIND_MEMPOOL_ALLOC</varname>. The second
2686 level blocks are described
2687 using <varname>VALGRIND_MALLOCLIKE_BLOCK</varname>. Note
2688 that the association between the pool and the second level
2689 blocks is implicit : second level blocks will be located
2690 inside first level blocks. It is necessary to use
2691 the <varname>VALGRIND_MEMPOOL_METAPOOL</varname> flag for
2692 such 2 levels pools, as otherwise valgrind will detect
2693 overlapping memory blocks, and will abort execution
2694 (e.g. during leak search).
2699 <varname>VALGRIND_MEMPOOL_AUTO_FREE</varname>. Such a meta
2700 pool can also be marked as an 'auto free' pool using the
2701 flag <varname>VALGRIND_MEMPOOL_AUTO_FREE</varname>, which
2702 must be OR-ed together with
2703 the <varname>VALGRIND_MEMPOOL_METAPOOL</varname>. For an
2704 'auto free' pool, <varname>VALGRIND_MEMPOOL_FREE</varname>
2705 will automatically free the second level blocks that are
2706 contained inside the first level block freed
2707 with <varname>VALGRIND_MEMPOOL_FREE</varname>. In other
2708 words, calling <varname>VALGRIND_MEMPOOL_FREE</varname> will
2709 cause implicit calls
2710 to <varname>VALGRIND_FREELIKE_BLOCK</varname> for all the
2711 second level blocks included in the first level block.
2712 Note: it is an error to use
2713 the <varname>VALGRIND_MEMPOOL_AUTO_FREE</varname> flag
2715 <varname>VALGRIND_MEMPOOL_METAPOOL</varname> flag.
2722 <para><varname>VALGRIND_DESTROY_MEMPOOL(pool)</varname>:
2723 This request tells Memcheck that a pool is being torn down. Memcheck
2724 then removes all records of chunks associated with the pool, as well
2725 as its record of the pool's existence. While destroying its records of
2726 a mempool, Memcheck resets the redzones of any live chunks in the pool
2732 <para><varname>VALGRIND_MEMPOOL_ALLOC(pool, addr, size)</varname>:
2733 This request informs Memcheck that a <varname>size</varname>-byte chunk
2734 has been allocated at <varname>addr</varname>, and associates the chunk with the
2736 <varname>pool</varname>. If the pool was created with nonzero
2737 <varname>rzB</varname> redzones, Memcheck will mark the
2738 <varname>rzB</varname> bytes before and after the chunk as NOACCESS. If
2739 the pool was created with the <varname>is_zeroed</varname> argument set,
2740 Memcheck will mark the chunk as DEFINED, otherwise Memcheck will mark
2741 the chunk as UNDEFINED.
2746 <para><varname>VALGRIND_MEMPOOL_FREE(pool, addr)</varname>:
2747 This request informs Memcheck that the chunk at <varname>addr</varname>
2748 should no longer be considered allocated. Memcheck will mark the chunk
2749 associated with <varname>addr</varname> as NOACCESS, and delete its
2750 record of the chunk's existence.
2755 <para><varname>VALGRIND_MEMPOOL_TRIM(pool, addr, size)</varname>:
2756 This request trims the chunks associated with <varname>pool</varname>.
2757 The request only operates on chunks associated with
2758 <varname>pool</varname>. Trimming is formally defined as:</para>
2761 <para> All chunks entirely inside the range
2762 <varname>addr..(addr+size-1)</varname> are preserved.</para>
2765 <para>All chunks entirely outside the range
2766 <varname>addr..(addr+size-1)</varname> are discarded, as though
2767 <varname>VALGRIND_MEMPOOL_FREE</varname> was called on them. </para>
2770 <para>All other chunks must intersect with the range
2771 <varname>addr..(addr+size-1)</varname>; areas outside the
2772 intersection are marked as NOACCESS, as though they had been
2773 independently freed with
2774 <varname>VALGRIND_MEMPOOL_FREE</varname>.</para>
2777 <para>This is a somewhat rare request, but can be useful in
2778 implementing the type of mass-free operations common in custom
2779 LIFO allocators.</para>
2783 <para><varname>VALGRIND_MOVE_MEMPOOL(poolA, poolB)</varname>: This
2784 request informs Memcheck that the pool previously anchored at
2785 address <varname>poolA</varname> has moved to anchor address
2786 <varname>poolB</varname>. This is a rare request, typically only needed
2787 if you <function>realloc</function> the header of a mempool.</para>
2788 <para>No memory-status bits are altered by this request.</para>
2793 <varname>VALGRIND_MEMPOOL_CHANGE(pool, addrA, addrB,
2794 size)</varname>: This request informs Memcheck that the chunk
2795 previously allocated at address <varname>addrA</varname> within
2796 <varname>pool</varname> has been moved and/or resized, and should be
2797 changed to cover the region <varname>addrB..(addrB+size-1)</varname>. This
2798 is a rare request, typically only needed if you
2799 <function>realloc</function> a superblock or wish to extend a chunk
2800 without changing its memory-status bits.
2802 <para>No memory-status bits are altered by this request.
2807 <para><varname>VALGRIND_MEMPOOL_EXISTS(pool)</varname>:
2808 This request informs the caller whether or not Memcheck is currently
2809 tracking a mempool at anchor address <varname>pool</varname>. It
2810 evaluates to 1 when there is a mempool associated with that address, 0
2811 otherwise. This is a rare request, only useful in circumstances when
2812 client code might have lost track of the set of active mempools.
2826 <sect1 id="mc-manual.mpiwrap" xreflabel="MPI Wrappers">
2827 <title>Debugging MPI Parallel Programs with Valgrind</title>
2829 <para>Memcheck supports debugging of distributed-memory applications
2830 which use the MPI message passing standard. This support consists of a
2831 library of wrapper functions for the
2832 <computeroutput>PMPI_*</computeroutput> interface. When incorporated
2833 into the application's address space, either by direct linking or by
2834 <computeroutput>LD_PRELOAD</computeroutput>, the wrappers intercept
2835 calls to <computeroutput>PMPI_Send</computeroutput>,
2836 <computeroutput>PMPI_Recv</computeroutput>, etc. They then
2837 use client requests to inform Memcheck of memory state changes caused
2838 by the function being wrapped. This reduces the number of false
2839 positives that Memcheck otherwise typically reports for MPI
2840 applications.</para>
2842 <para>The wrappers also take the opportunity to carefully check
2843 size and definedness of buffers passed as arguments to MPI functions, hence
2844 detecting errors such as passing undefined data to
2845 <computeroutput>PMPI_Send</computeroutput>, or receiving data into a
2846 buffer which is too small.</para>
2848 <para>Unlike most of the rest of Valgrind, the wrapper library is subject to a
2849 BSD-style license, so you can link it into any code base you like.
2850 See the top of <computeroutput>mpi/libmpiwrap.c</computeroutput>
2851 for license details.</para>
2854 <sect2 id="mc-manual.mpiwrap.build" xreflabel="Building MPI Wrappers">
2855 <title>Building and installing the wrappers</title>
2857 <para> The wrapper library will be built automatically if possible.
2858 Valgrind's configure script will look for a suitable
2859 <computeroutput>mpicc</computeroutput> to build it with. This must be
2860 the same <computeroutput>mpicc</computeroutput> you use to build the
2861 MPI application you want to debug. By default, Valgrind tries
2862 <computeroutput>mpicc</computeroutput>, but you can specify a
2863 different one by using the configure-time option
2864 <option>--with-mpicc</option>. Currently the
2865 wrappers are only buildable with
2866 <computeroutput>mpicc</computeroutput>s which are based on GNU
2867 GCC or Intel's C++ Compiler.</para>
2869 <para>Check that the configure script prints a line like this:</para>
2871 <programlisting><![CDATA[
2872 checking for usable MPI2-compliant mpicc and mpi.h... yes, mpicc
2873 ]]></programlisting>
2875 <para>If it says <computeroutput>... no</computeroutput>, your
2876 <computeroutput>mpicc</computeroutput> has failed to compile and link
2877 a test MPI2 program.</para>
2879 <para>If the configure test succeeds, continue in the usual way with
2880 <computeroutput>make</computeroutput> and <computeroutput>make
2881 install</computeroutput>. The final install tree should then contain
2882 <computeroutput>libmpiwrap-<platform>.so</computeroutput>.
2885 <para>Compile up a test MPI program (eg, MPI hello-world) and try
2888 <programlisting><![CDATA[
2889 LD_PRELOAD=$prefix/lib/valgrind/libmpiwrap-<platform>.so \
2890 mpirun [args] $prefix/bin/valgrind ./hello
2891 ]]></programlisting>
2893 <para>You should see something similar to the following</para>
2895 <programlisting><![CDATA[
2896 valgrind MPI wrappers 31901: Active for pid 31901
2897 valgrind MPI wrappers 31901: Try MPIWRAP_DEBUG=help for possible options
2898 ]]></programlisting>
2900 <para>repeated for every process in the group. If you do not see
2901 these, there is an build/installation problem of some kind.</para>
2903 <para> The MPI functions to be wrapped are assumed to be in an ELF
2904 shared object with soname matching
2905 <computeroutput>libmpi.so*</computeroutput>. This is known to be
2906 correct at least for Open MPI and Quadrics MPI, and can easily be
2907 changed if required.</para>
2911 <sect2 id="mc-manual.mpiwrap.gettingstarted"
2912 xreflabel="Getting started with MPI Wrappers">
2913 <title>Getting started</title>
2915 <para>Compile your MPI application as usual, taking care to link it
2916 using the same <computeroutput>mpicc</computeroutput> that your
2917 Valgrind build was configured with.</para>
2920 Use the following basic scheme to run your application on Valgrind with
2921 the wrappers engaged:</para>
2923 <programlisting><![CDATA[
2924 MPIWRAP_DEBUG=[wrapper-args] \
2925 LD_PRELOAD=$prefix/lib/valgrind/libmpiwrap-<platform>.so \
2926 mpirun [mpirun-args] \
2927 $prefix/bin/valgrind [valgrind-args] \
2928 [application] [app-args]
2929 ]]></programlisting>
2931 <para>As an alternative to
2932 <computeroutput>LD_PRELOAD</computeroutput>ing
2933 <computeroutput>libmpiwrap-<platform>.so</computeroutput>, you can
2934 simply link it to your application if desired. This should not disturb
2935 native behaviour of your application in any way.</para>
2939 <sect2 id="mc-manual.mpiwrap.controlling"
2940 xreflabel="Controlling the MPI Wrappers">
2941 <title>Controlling the wrapper library</title>
2943 <para>Environment variable
2944 <computeroutput>MPIWRAP_DEBUG</computeroutput> is consulted at
2945 startup. The default behaviour is to print a starting banner</para>
2947 <programlisting><![CDATA[
2948 valgrind MPI wrappers 16386: Active for pid 16386
2949 valgrind MPI wrappers 16386: Try MPIWRAP_DEBUG=help for possible options
2950 ]]></programlisting>
2952 <para> and then be relatively quiet.</para>
2954 <para>You can give a list of comma-separated options in
2955 <computeroutput>MPIWRAP_DEBUG</computeroutput>. These are</para>
2959 <para><computeroutput>verbose</computeroutput>:
2960 show entries/exits of all wrappers. Also show extra
2961 debugging info, such as the status of outstanding
2962 <computeroutput>MPI_Request</computeroutput>s resulting
2963 from uncompleted <computeroutput>MPI_Irecv</computeroutput>s.</para>
2966 <para><computeroutput>quiet</computeroutput>:
2967 opposite of <computeroutput>verbose</computeroutput>, only print
2968 anything when the wrappers want
2969 to report a detected programming error, or in case of catastrophic
2970 failure of the wrappers.</para>
2973 <para><computeroutput>warn</computeroutput>:
2974 by default, functions which lack proper wrappers
2975 are not commented on, just silently
2976 ignored. This causes a warning to be printed for each unwrapped
2977 function used, up to a maximum of three warnings per function.</para>
2980 <para><computeroutput>strict</computeroutput>:
2981 print an error message and abort the program if
2982 a function lacking a wrapper is used.</para>
2986 <para> If you want to use Valgrind's XML output facility
2987 (<option>--xml=yes</option>), you should pass
2988 <computeroutput>quiet</computeroutput> in
2989 <computeroutput>MPIWRAP_DEBUG</computeroutput> so as to get rid of any
2990 extraneous printing from the wrappers.</para>
2995 <sect2 id="mc-manual.mpiwrap.limitations.functions"
2996 xreflabel="Functions: Abilities and Limitations">
2997 <title>Functions</title>
2999 <para>All MPI2 functions except
3000 <computeroutput>MPI_Wtick</computeroutput>,
3001 <computeroutput>MPI_Wtime</computeroutput> and
3002 <computeroutput>MPI_Pcontrol</computeroutput> have wrappers. The
3003 first two are not wrapped because they return a
3004 <computeroutput>double</computeroutput>, which Valgrind's
3005 function-wrap mechanism cannot handle (but it could easily be
3006 extended to do so). <computeroutput>MPI_Pcontrol</computeroutput> cannot be
3007 wrapped as it has variable arity:
3008 <computeroutput>int MPI_Pcontrol(const int level, ...)</computeroutput></para>
3010 <para>Most functions are wrapped with a default wrapper which does
3011 nothing except complain or abort if it is called, depending on
3012 settings in <computeroutput>MPIWRAP_DEBUG</computeroutput> listed
3013 above. The following functions have "real", do-something-useful
3016 <programlisting><![CDATA[
3017 PMPI_Send PMPI_Bsend PMPI_Ssend PMPI_Rsend
3019 PMPI_Recv PMPI_Get_count
3021 PMPI_Isend PMPI_Ibsend PMPI_Issend PMPI_Irsend
3024 PMPI_Wait PMPI_Waitall
3025 PMPI_Test PMPI_Testall
3027 PMPI_Iprobe PMPI_Probe
3033 PMPI_Type_commit PMPI_Type_free
3035 PMPI_Pack PMPI_Unpack
3037 PMPI_Bcast PMPI_Gather PMPI_Scatter PMPI_Alltoall
3038 PMPI_Reduce PMPI_Allreduce PMPI_Op_create
3040 PMPI_Comm_create PMPI_Comm_dup PMPI_Comm_free PMPI_Comm_rank PMPI_Comm_size
3043 PMPI_Init PMPI_Initialized PMPI_Finalize
3044 ]]></programlisting>
3046 <para> A few functions such as
3047 <computeroutput>PMPI_Address</computeroutput> are listed as
3048 <computeroutput>HAS_NO_WRAPPER</computeroutput>. They have no wrapper
3049 at all as there is nothing worth checking, and giving a no-op wrapper
3050 would reduce performance for no reason.</para>
3052 <para> Note that the wrapper library itself can itself generate large
3053 numbers of calls to the MPI implementation, especially when walking
3054 complex types. The most common functions called are
3055 <computeroutput>PMPI_Extent</computeroutput>,
3056 <computeroutput>PMPI_Type_get_envelope</computeroutput>,
3057 <computeroutput>PMPI_Type_get_contents</computeroutput>, and
3058 <computeroutput>PMPI_Type_free</computeroutput>. </para>
3061 <sect2 id="mc-manual.mpiwrap.limitations.types"
3062 xreflabel="Types: Abilities and Limitations">
3063 <title>Types</title>
3065 <para> MPI-1.1 structured types are supported, and walked exactly.
3066 The currently supported combiners are
3067 <computeroutput>MPI_COMBINER_NAMED</computeroutput>,
3068 <computeroutput>MPI_COMBINER_CONTIGUOUS</computeroutput>,
3069 <computeroutput>MPI_COMBINER_VECTOR</computeroutput>,
3070 <computeroutput>MPI_COMBINER_HVECTOR</computeroutput>
3071 <computeroutput>MPI_COMBINER_INDEXED</computeroutput>,
3072 <computeroutput>MPI_COMBINER_HINDEXED</computeroutput> and
3073 <computeroutput>MPI_COMBINER_STRUCT</computeroutput>. This should
3074 cover all MPI-1.1 types. The mechanism (function
3075 <computeroutput>walk_type</computeroutput>) should extend easily to
3076 cover MPI2 combiners.</para>
3078 <para>MPI defines some named structured types
3079 (<computeroutput>MPI_FLOAT_INT</computeroutput>,
3080 <computeroutput>MPI_DOUBLE_INT</computeroutput>,
3081 <computeroutput>MPI_LONG_INT</computeroutput>,
3082 <computeroutput>MPI_2INT</computeroutput>,
3083 <computeroutput>MPI_SHORT_INT</computeroutput>,
3084 <computeroutput>MPI_LONG_DOUBLE_INT</computeroutput>) which are pairs
3085 of some basic type and a C <computeroutput>int</computeroutput>.
3086 Unfortunately the MPI specification makes it impossible to look inside
3087 these types and see where the fields are. Therefore these wrappers
3088 assume the types are laid out as <computeroutput>struct { float val;
3089 int loc; }</computeroutput> (for
3090 <computeroutput>MPI_FLOAT_INT</computeroutput>), etc, and act
3091 accordingly. This appears to be correct at least for Open MPI 1.0.2
3092 and for Quadrics MPI.</para>
3094 <para>If <computeroutput>strict</computeroutput> is an option specified
3095 in <computeroutput>MPIWRAP_DEBUG</computeroutput>, the application
3096 will abort if an unhandled type is encountered. Otherwise, the
3097 application will print a warning message and continue.</para>
3099 <para>Some effort is made to mark/check memory ranges corresponding to
3100 arrays of values in a single pass. This is important for performance
3101 since asking Valgrind to mark/check any range, no matter how small,
3102 carries quite a large constant cost. This optimisation is applied to
3103 arrays of primitive types (<computeroutput>double</computeroutput>,
3104 <computeroutput>float</computeroutput>,
3105 <computeroutput>int</computeroutput>,
3106 <computeroutput>long</computeroutput>, <computeroutput>long
3107 long</computeroutput>, <computeroutput>short</computeroutput>,
3108 <computeroutput>char</computeroutput>, and <computeroutput>long
3109 double</computeroutput> on platforms where <computeroutput>sizeof(long
3110 double) == 8</computeroutput>). For arrays of all other types, the
3111 wrappers handle each element individually and so there can be a very
3112 large performance cost.</para>
3117 <sect2 id="mc-manual.mpiwrap.writingwrappers"
3118 xreflabel="Writing new MPI Wrappers">
3119 <title>Writing new wrappers</title>
3122 For the most part the wrappers are straightforward. The only
3123 significant complexity arises with nonblocking receives.</para>
3125 <para>The issue is that <computeroutput>MPI_Irecv</computeroutput>
3126 states the recv buffer and returns immediately, giving a handle
3127 (<computeroutput>MPI_Request</computeroutput>) for the transaction.
3128 Later the user will have to poll for completion with
3129 <computeroutput>MPI_Wait</computeroutput> etc, and when the
3130 transaction completes successfully, the wrappers have to paint the
3131 recv buffer. But the recv buffer details are not presented to
3132 <computeroutput>MPI_Wait</computeroutput> -- only the handle is. The
3133 library therefore maintains a shadow table which associates
3134 uncompleted <computeroutput>MPI_Request</computeroutput>s with the
3135 corresponding buffer address/count/type. When an operation completes,
3136 the table is searched for the associated address/count/type info, and
3137 memory is marked accordingly.</para>
3139 <para>Access to the table is guarded by a (POSIX pthreads) lock, so as
3140 to make the library thread-safe.</para>
3142 <para>The table is allocated with
3143 <computeroutput>malloc</computeroutput> and never
3144 <computeroutput>free</computeroutput>d, so it will show up in leak
3147 <para>Writing new wrappers should be fairly easy. The source file is
3148 <computeroutput>mpi/libmpiwrap.c</computeroutput>. If possible,
3149 find an existing wrapper for a function of similar behaviour to the
3150 one you want to wrap, and use it as a starting point. The wrappers
3151 are organised in sections in the same order as the MPI 1.1 spec, to
3152 aid navigation. When adding a wrapper, remember to comment out the
3153 definition of the default wrapper in the long list of defaults at the
3154 bottom of the file (do not remove it, just comment it out).</para>
3157 <sect2 id="mc-manual.mpiwrap.whattoexpect"
3158 xreflabel="What to expect with MPI Wrappers">
3159 <title>What to expect when using the wrappers</title>
3161 <para>The wrappers should reduce Memcheck's false-error rate on MPI
3162 applications. Because the wrapping is done at the MPI interface,
3163 there will still potentially be a large number of errors reported in
3164 the MPI implementation below the interface. The best you can do is
3165 try to suppress them.</para>
3167 <para>You may also find that the input-side (buffer
3168 length/definedness) checks find errors in your MPI use, for example
3169 passing too short a buffer to
3170 <computeroutput>MPI_Recv</computeroutput>.</para>
3172 <para>Functions which are not wrapped may increase the false
3173 error rate. A possible approach is to run with
3174 <computeroutput>MPI_DEBUG</computeroutput> containing
3175 <computeroutput>warn</computeroutput>. This will show you functions
3176 which lack proper wrappers but which are nevertheless used. You can
3177 then write wrappers for them.
3180 <para>A known source of potential false errors are the
3181 <computeroutput>PMPI_Reduce</computeroutput> family of functions, when
3182 using a custom (user-defined) reduction function. In a reduction
3183 operation, each node notionally sends data to a "central point" which
3184 uses the specified reduction function to merge the data items into a
3185 single item. Hence, in general, data is passed between nodes and fed
3186 to the reduction function, but the wrapper library cannot mark the
3187 transferred data as initialised before it is handed to the reduction
3188 function, because all that happens "inside" the
3189 <computeroutput>PMPI_Reduce</computeroutput> call. As a result you
3190 may see false positives reported in your reduction function.</para>