blob: 7eee0c54a01363250ba06b3b00ddaa7662f38128 [file] [log] [blame]
njn3e986b22004-11-30 10:43:45 +00001<?xml version="1.0"?> <!-- -*- sgml -*- -->
2<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
de03e0e7c2005-12-03 23:02:33 +00003 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
4
njn3e986b22004-11-30 10:43:45 +00005
njn05a89172009-07-29 02:36:21 +00006<chapter id="mc-manual" xreflabel="Memcheck: a memory error detector">
7<title>Memcheck: a memory error detector</title>
njn3e986b22004-11-30 10:43:45 +00008
de03e0e7c2005-12-03 23:02:33 +00009<para>To use this tool, you may specify <option>--tool=memcheck</option>
10on the Valgrind command line. You don't have to, though, since Memcheck
11is the default tool.</para>
njn3e986b22004-11-30 10:43:45 +000012
13
njn05a89172009-07-29 02:36:21 +000014<sect1 id="mc-manual.overview" xreflabel="Overview">
15<title>Overview</title>
njn3e986b22004-11-30 10:43:45 +000016
njn05a89172009-07-29 02:36:21 +000017<para>Memcheck is a memory error detector. It can detect the following
18problems that are common in C and C++ programs.</para>
njn3e986b22004-11-30 10:43:45 +000019
20<itemizedlist>
21 <listitem>
njn05a89172009-07-29 02:36:21 +000022 <para>Accessing memory you shouldn't, e.g. overrunning and underrunning
23 heap blocks, overrunning the top of the stack, and accessing memory after
24 it has been freed.</para>
njn3e986b22004-11-30 10:43:45 +000025 </listitem>
njn05a89172009-07-29 02:36:21 +000026
njn3e986b22004-11-30 10:43:45 +000027 <listitem>
njn05a89172009-07-29 02:36:21 +000028 <para>Using undefined values, i.e. values that have not been initialised,
29 or that have been derived from other undefined values.</para>
njn3e986b22004-11-30 10:43:45 +000030 </listitem>
njn05a89172009-07-29 02:36:21 +000031
njn3e986b22004-11-30 10:43:45 +000032 <listitem>
njn05a89172009-07-29 02:36:21 +000033 <para>Incorrect freeing of heap memory, such as double-freeing heap
34 blocks, or mismatched use of
bartaf25f672009-06-26 19:03:53 +000035 <function>malloc</function>/<computeroutput>new</computeroutput>/<computeroutput>new[]</computeroutput>
36 versus
37 <function>free</function>/<computeroutput>delete</computeroutput>/<computeroutput>delete[]</computeroutput></para>
njn3e986b22004-11-30 10:43:45 +000038 </listitem>
njn05a89172009-07-29 02:36:21 +000039
njn3e986b22004-11-30 10:43:45 +000040 <listitem>
41 <para>Overlapping <computeroutput>src</computeroutput> and
42 <computeroutput>dst</computeroutput> pointers in
njn2f7eebe2009-08-05 06:34:27 +000043 <computeroutput>memcpy</computeroutput> and related
njn05a89172009-07-29 02:36:21 +000044 functions.</para>
45 </listitem>
46
47 <listitem>
48 <para>Memory leaks.</para>
njn3e986b22004-11-30 10:43:45 +000049 </listitem>
njn3e986b22004-11-30 10:43:45 +000050</itemizedlist>
51
njn05a89172009-07-29 02:36:21 +000052<para>Problems like these can be difficult to find by other means,
53often remaining undetected for long periods, then causing occasional,
54difficult-to-diagnose crashes.</para>
55
njn3e986b22004-11-30 10:43:45 +000056</sect1>
57
58
59
njn3e986b22004-11-30 10:43:45 +000060<sect1 id="mc-manual.errormsgs"
61 xreflabel="Explanation of error messages from Memcheck">
62<title>Explanation of error messages from Memcheck</title>
63
njnc1abdcb2009-08-05 05:11:02 +000064<para>Memcheck issues a range of error messages. This section presents a
65quick summary of what error messages mean. The precise behaviour of the
66error-checking machinery is described in <xref
67linkend="mc-manual.machine"/>.</para>
njn3e986b22004-11-30 10:43:45 +000068
69
70<sect2 id="mc-manual.badrw"
71 xreflabel="Illegal read / Illegal write errors">
72<title>Illegal read / Illegal write errors</title>
73
74<para>For example:</para>
75<programlisting><![CDATA[
76Invalid read of size 4
77 at 0x40F6BBCC: (within /usr/lib/libpng.so.2.1.0.9)
78 by 0x40F6B804: (within /usr/lib/libpng.so.2.1.0.9)
sewardj08e31e22007-05-23 21:58:33 +000079 by 0x40B07FF4: read_png_image(QImageIO *) (kernel/qpngio.cpp:326)
njn3e986b22004-11-30 10:43:45 +000080 by 0x40AC751B: QImageIO::read() (kernel/qimage.cpp:3621)
njn21f91952005-03-12 22:14:42 +000081 Address 0xBFFFF0E0 is not stack'd, malloc'd or free'd
njn3e986b22004-11-30 10:43:45 +000082]]></programlisting>
83
de03e0e7c2005-12-03 23:02:33 +000084<para>This happens when your program reads or writes memory at a place
85which Memcheck reckons it shouldn't. In this example, the program did a
864-byte read at address 0xBFFFF0E0, somewhere within the system-supplied
87library libpng.so.2.1.0.9, which was called from somewhere else in the
88same library, called from line 326 of <filename>qpngio.cpp</filename>,
89and so on.</para>
njn3e986b22004-11-30 10:43:45 +000090
de03e0e7c2005-12-03 23:02:33 +000091<para>Memcheck tries to establish what the illegal address might relate
92to, since that's often useful. So, if it points into a block of memory
93which has already been freed, you'll be informed of this, and also where
njn7316df22009-08-04 01:16:01 +000094the block was freed. Likewise, if it should turn out to be just off
95the end of a heap block, a common result of off-by-one-errors in
de03e0e7c2005-12-03 23:02:33 +000096array subscripting, you'll be informed of this fact, and also where the
njn2f7eebe2009-08-05 06:34:27 +000097block was allocated. If you use the <option><xref
98linkend="opt.read-var-info"/></option> option Memcheck will run more slowly
99but may give a more detailed description of any illegal address.</para>
njn3e986b22004-11-30 10:43:45 +0000100
de03e0e7c2005-12-03 23:02:33 +0000101<para>In this example, Memcheck can't identify the address. Actually
102the address is on the stack, but, for some reason, this is not a valid
103stack address -- it is below the stack pointer and that isn't allowed.
njn7316df22009-08-04 01:16:01 +0000104In this particular case it's probably caused by GCC generating invalid
105code, a known bug in some ancient versions of GCC.</para>
njn3e986b22004-11-30 10:43:45 +0000106
de03e0e7c2005-12-03 23:02:33 +0000107<para>Note that Memcheck only tells you that your program is about to
108access memory at an illegal address. It can't stop the access from
109happening. So, if your program makes an access which normally would
110result in a segmentation fault, you program will still suffer the same
111fate -- but you will get a message from Memcheck immediately prior to
112this. In this particular example, reading junk on the stack is
113non-fatal, and the program stays alive.</para>
njn3e986b22004-11-30 10:43:45 +0000114
115</sect2>
116
117
118
119<sect2 id="mc-manual.uninitvals"
120 xreflabel="Use of uninitialised values">
121<title>Use of uninitialised values</title>
122
123<para>For example:</para>
124<programlisting><![CDATA[
125Conditional jump or move depends on uninitialised value(s)
126 at 0x402DFA94: _IO_vfprintf (_itoa.h:49)
127 by 0x402E8476: _IO_printf (printf.c:36)
128 by 0x8048472: main (tests/manuel1.c:8)
njn3e986b22004-11-30 10:43:45 +0000129]]></programlisting>
130
de03e0e7c2005-12-03 23:02:33 +0000131<para>An uninitialised-value use error is reported when your program
132uses a value which hasn't been initialised -- in other words, is
133undefined. Here, the undefined value is used somewhere inside the
njn2f7eebe2009-08-05 06:34:27 +0000134<function>printf</function> machinery of the C library. This error was
135reported when running the following small program:</para>
njn3e986b22004-11-30 10:43:45 +0000136<programlisting><![CDATA[
137int main()
138{
139 int x;
140 printf ("x = %d\n", x);
141}]]></programlisting>
142
de03e0e7c2005-12-03 23:02:33 +0000143<para>It is important to understand that your program can copy around
144junk (uninitialised) data as much as it likes. Memcheck observes this
145and keeps track of the data, but does not complain. A complaint is
146issued only when your program attempts to make use of uninitialised
njn2f7eebe2009-08-05 06:34:27 +0000147data in a way that might affect your program's externally-visible behaviour.
148In this example, <varname>x</varname> is uninitialised. Memcheck observes
149the value being passed to <function>_IO_printf</function> and thence to
150<function>_IO_vfprintf</function>, but makes no comment. However,
151<function>_IO_vfprintf</function> has to examine the value of
152<varname>x</varname> so it can turn it into the corresponding ASCII string,
153and it is at this point that Memcheck complains.</para>
njn3e986b22004-11-30 10:43:45 +0000154
155<para>Sources of uninitialised data tend to be:</para>
156<itemizedlist>
157 <listitem>
de03e0e7c2005-12-03 23:02:33 +0000158 <para>Local variables in procedures which have not been initialised,
159 as in the example above.</para>
njn3e986b22004-11-30 10:43:45 +0000160 </listitem>
161 <listitem>
njn7316df22009-08-04 01:16:01 +0000162 <para>The contents of heap blocks (allocated with
163 <function>malloc</function>, <function>new</function>, or a similar
164 function) before you (or a constructor) write something there.
165 </para>
njn3e986b22004-11-30 10:43:45 +0000166 </listitem>
167</itemizedlist>
168
sewardjcd0f2bd2008-05-04 23:06:28 +0000169<para>To see information on the sources of uninitialised data in your
njna3311642009-08-10 01:29:14 +0000170program, use the <option>--track-origins=yes</option> option. This
sewardjcd0f2bd2008-05-04 23:06:28 +0000171makes Memcheck run more slowly, but can make it much easier to track down
172the root causes of uninitialised value errors.</para>
173
njn3e986b22004-11-30 10:43:45 +0000174</sect2>
175
176
177
njn2f7eebe2009-08-05 06:34:27 +0000178<sect2 id="mc-manual.bad-syscall-args"
179 xreflabel="Use of uninitialised or unaddressable values in system
180 calls">
181<title>Use of uninitialised or unaddressable values in system
182 calls</title>
183
184<para>Memcheck checks all parameters to system calls:
185<itemizedlist>
186 <listitem>
187 <para>It checks all the direct parameters themselves, whether they are
188 initialised.</para>
189 </listitem>
190 <listitem>
191 <para>Also, if a system call needs to read from a buffer provided by
192 your program, Memcheck checks that the entire buffer is addressable
193 and its contents are initialised.</para>
194 </listitem>
195 <listitem>
196 <para>Also, if the system call needs to write to a user-supplied
197 buffer, Memcheck checks that the buffer is addressable.</para>
198 </listitem>
199</itemizedlist>
200</para>
201
202<para>After the system call, Memcheck updates its tracked information to
203precisely reflect any changes in memory state caused by the system
204call.</para>
205
206<para>Here's an example of two system calls with invalid parameters:</para>
207<programlisting><![CDATA[
208 #include <stdlib.h>
209 #include <unistd.h>
210 int main( void )
211 {
212 char* arr = malloc(10);
213 int* arr2 = malloc(sizeof(int));
214 write( 1 /* stdout */, arr, 10 );
215 exit(arr2[0]);
216 }
217]]></programlisting>
218
219<para>You get these complaints ...</para>
220<programlisting><![CDATA[
221 Syscall param write(buf) points to uninitialised byte(s)
222 at 0x25A48723: __write_nocancel (in /lib/tls/libc-2.3.3.so)
223 by 0x259AFAD3: __libc_start_main (in /lib/tls/libc-2.3.3.so)
224 by 0x8048348: (within /auto/homes/njn25/grind/head4/a.out)
225 Address 0x25AB8028 is 0 bytes inside a block of size 10 alloc'd
226 at 0x259852B0: malloc (vg_replace_malloc.c:130)
227 by 0x80483F1: main (a.c:5)
228
229 Syscall param exit(error_code) contains uninitialised byte(s)
230 at 0x25A21B44: __GI__exit (in /lib/tls/libc-2.3.3.so)
231 by 0x8048426: main (a.c:8)
232]]></programlisting>
233
234<para>... because the program has (a) written uninitialised junk
235from the heap block to the standard output, and (b) passed an
236uninitialised value to <function>exit</function>. Note that the first
237error refers to the memory pointed to by
238<computeroutput>buf</computeroutput> (not
239<computeroutput>buf</computeroutput> itself), but the second error
240refers directly to <computeroutput>exit</computeroutput>'s argument
241<computeroutput>arr2[0]</computeroutput>.</para>
242
243</sect2>
244
245
njn3e986b22004-11-30 10:43:45 +0000246<sect2 id="mc-manual.badfrees" xreflabel="Illegal frees">
247<title>Illegal frees</title>
248
249<para>For example:</para>
250<programlisting><![CDATA[
251Invalid free()
252 at 0x4004FFDF: free (vg_clientmalloc.c:577)
253 by 0x80484C7: main (tests/doublefree.c:10)
njn21f91952005-03-12 22:14:42 +0000254 Address 0x3807F7B4 is 0 bytes inside a block of size 177 free'd
njn3e986b22004-11-30 10:43:45 +0000255 at 0x4004FFDF: free (vg_clientmalloc.c:577)
256 by 0x80484C7: main (tests/doublefree.c:10)
njn3e986b22004-11-30 10:43:45 +0000257]]></programlisting>
258
bartaf25f672009-06-26 19:03:53 +0000259<para>Memcheck keeps track of the blocks allocated by your program
260with <function>malloc</function>/<computeroutput>new</computeroutput>,
261so it can know exactly whether or not the argument to
262<function>free</function>/<computeroutput>delete</computeroutput> is
263legitimate or not. Here, this test program has freed the same block
264twice. As with the illegal read/write errors, Memcheck attempts to
njn7316df22009-08-04 01:16:01 +0000265make sense of the address freed. If, as here, the address is one
bartaf25f672009-06-26 19:03:53 +0000266which has previously been freed, you wil be told that -- making
njn2f7eebe2009-08-05 06:34:27 +0000267duplicate frees of the same block easy to spot. You will also get this
268message if you try to free a pointer that doesn't point to the start of a
269heap block.</para>
njn3e986b22004-11-30 10:43:45 +0000270
271</sect2>
272
273
274<sect2 id="mc-manual.rudefn"
njn2f7eebe2009-08-05 06:34:27 +0000275 xreflabel="When a heap block is freed with an inappropriate deallocation
njn3e986b22004-11-30 10:43:45 +0000276function">
njn2f7eebe2009-08-05 06:34:27 +0000277<title>When a heap block is freed with an inappropriate deallocation
njn3e986b22004-11-30 10:43:45 +0000278function</title>
279
280<para>In the following example, a block allocated with
de03e0e7c2005-12-03 23:02:33 +0000281<function>new[]</function> has wrongly been deallocated with
282<function>free</function>:</para>
njn3e986b22004-11-30 10:43:45 +0000283<programlisting><![CDATA[
284Mismatched free() / delete / delete []
285 at 0x40043249: free (vg_clientfuncs.c:171)
286 by 0x4102BB4E: QGArray::~QGArray(void) (tools/qgarray.cpp:149)
287 by 0x4C261C41: PptDoc::~PptDoc(void) (include/qmemarray.h:60)
288 by 0x4C261F0E: PptXml::~PptXml(void) (pptxml.cc:44)
njn21f91952005-03-12 22:14:42 +0000289 Address 0x4BB292A8 is 0 bytes inside a block of size 64 alloc'd
sewardj08e31e22007-05-23 21:58:33 +0000290 at 0x4004318C: operator new[](unsigned int) (vg_clientfuncs.c:152)
njn3e986b22004-11-30 10:43:45 +0000291 by 0x4C21BC15: KLaola::readSBStream(int) const (klaola.cc:314)
292 by 0x4C21C155: KLaola::stream(KLaola::OLENode const *) (klaola.cc:416)
293 by 0x4C21788F: OLEFilter::convert(QCString const &) (olefilter.cc:272)
294]]></programlisting>
295
de03e0e7c2005-12-03 23:02:33 +0000296<para>In <literal>C++</literal> it's important to deallocate memory in a
297way compatible with how it was allocated. The deal is:</para>
njn3e986b22004-11-30 10:43:45 +0000298<itemizedlist>
299 <listitem>
300 <para>If allocated with
de03e0e7c2005-12-03 23:02:33 +0000301 <function>malloc</function>,
302 <function>calloc</function>,
303 <function>realloc</function>,
304 <function>valloc</function> or
305 <function>memalign</function>, you must
306 deallocate with <function>free</function>.</para>
njn3e986b22004-11-30 10:43:45 +0000307 </listitem>
308 <listitem>
de03e0e7c2005-12-03 23:02:33 +0000309 <para>If allocated with <function>new</function>, you must deallocate
310 with <function>delete</function>.</para>
njn3e986b22004-11-30 10:43:45 +0000311 </listitem>
njn2f7eebe2009-08-05 06:34:27 +0000312 <listitem>
313 <para>If allocated with <function>new[]</function>, you must
314 deallocate with <function>delete[]</function>.</para>
315 </listitem>
njn3e986b22004-11-30 10:43:45 +0000316</itemizedlist>
317
de03e0e7c2005-12-03 23:02:33 +0000318<para>The worst thing is that on Linux apparently it doesn't matter if
sewardj08e31e22007-05-23 21:58:33 +0000319you do mix these up, but the same program may then crash on a
320different platform, Solaris for example. So it's best to fix it
321properly. According to the KDE folks "it's amazing how many C++
322programmers don't know this".</para>
njn3e986b22004-11-30 10:43:45 +0000323
sewardj08e31e22007-05-23 21:58:33 +0000324<para>The reason behind the requirement is as follows. In some C++
325implementations, <function>delete[]</function> must be used for
326objects allocated by <function>new[]</function> because the compiler
327stores the size of the array and the pointer-to-member to the
328destructor of the array's content just before the pointer actually
njn2f7eebe2009-08-05 06:34:27 +0000329returned. <function>delete</function> doesn't account for this and will get
330confused, possibly corrupting the heap.</para>
de03e0e7c2005-12-03 23:02:33 +0000331
njn3e986b22004-11-30 10:43:45 +0000332</sect2>
333
334
335
njn3e986b22004-11-30 10:43:45 +0000336<sect2 id="mc-manual.overlap"
337 xreflabel="Overlapping source and destination blocks">
338<title>Overlapping source and destination blocks</title>
339
340<para>The following C library functions copy some data from one
341memory block to another (or something similar):
njn2f7eebe2009-08-05 06:34:27 +0000342<function>memcpy</function>,
343<function>strcpy</function>,
344<function>strncpy</function>,
345<function>strcat</function>,
346<function>strncat</function>.
de03e0e7c2005-12-03 23:02:33 +0000347The blocks pointed to by their <computeroutput>src</computeroutput> and
348<computeroutput>dst</computeroutput> pointers aren't allowed to overlap.
njn2f7eebe2009-08-05 06:34:27 +0000349The POSIX standards have wording along the lines "If copying takes place
350between objects that overlap, the behavior is undefined." Therefore,
351Memcheck checks for this.
352</para>
njn3e986b22004-11-30 10:43:45 +0000353
354<para>For example:</para>
355<programlisting><![CDATA[
356==27492== Source and destination overlap in memcpy(0xbffff294, 0xbffff280, 21)
357==27492== at 0x40026CDC: memcpy (mc_replace_strmem.c:71)
358==27492== by 0x804865A: main (overlap.c:40)
njn3e986b22004-11-30 10:43:45 +0000359]]></programlisting>
360
de03e0e7c2005-12-03 23:02:33 +0000361<para>You don't want the two blocks to overlap because one of them could
sewardj08e31e22007-05-23 21:58:33 +0000362get partially overwritten by the copying.</para>
njn3e986b22004-11-30 10:43:45 +0000363
njnccad0b82005-07-19 00:48:55 +0000364<para>You might think that Memcheck is being overly pedantic reporting
de03e0e7c2005-12-03 23:02:33 +0000365this in the case where <computeroutput>dst</computeroutput> is less than
366<computeroutput>src</computeroutput>. For example, the obvious way to
njn2f7eebe2009-08-05 06:34:27 +0000367implement <function>memcpy</function> is by copying from the first
de03e0e7c2005-12-03 23:02:33 +0000368byte to the last. However, the optimisation guides of some
369architectures recommend copying from the last byte down to the first.
njn2f7eebe2009-08-05 06:34:27 +0000370Also, some implementations of <function>memcpy</function> zero
de03e0e7c2005-12-03 23:02:33 +0000371<computeroutput>dst</computeroutput> before copying, because zeroing the
372destination's cache line(s) can improve performance.</para>
njnccad0b82005-07-19 00:48:55 +0000373
de03e0e7c2005-12-03 23:02:33 +0000374<para>The moral of the story is: if you want to write truly portable
375code, don't make any assumptions about the language
376implementation.</para>
njnccad0b82005-07-19 00:48:55 +0000377
njn3e986b22004-11-30 10:43:45 +0000378</sect2>
379
380
njnab5b7142005-08-16 02:20:17 +0000381<sect2 id="mc-manual.leaks" xreflabel="Memory leak detection">
382<title>Memory leak detection</title>
383
njn2f7eebe2009-08-05 06:34:27 +0000384<para>Memcheck keeps track of all heap blocks issued in response to
bartaf25f672009-06-26 19:03:53 +0000385calls to
njn2f7eebe2009-08-05 06:34:27 +0000386<function>malloc</function>/<function>new</function> et al.
bartaf25f672009-06-26 19:03:53 +0000387So when the program exits, it knows which blocks have not been freed.
njnab5b7142005-08-16 02:20:17 +0000388</para>
389
de03e0e7c2005-12-03 23:02:33 +0000390<para>If <option>--leak-check</option> is set appropriately, for each
njn8225cc02009-03-09 22:52:24 +0000391remaining block, Memcheck determines if the block is reachable from pointers
392within the root-set. The root-set consists of (a) general purpose registers
393of all threads, and (b) initialised, aligned, pointer-sized data words in
394accessible client memory, including stacks.</para>
395
396<para>There are two ways a block can be reached. The first is with a
njn389f5702009-07-15 07:18:16 +0000397"start-pointer", i.e. a pointer to the start of the block. The second is with
398an "interior-pointer", i.e. a pointer to the middle of the block. There are
philippeab1fce92013-09-29 13:47:32 +0000399several ways we know of that an interior-pointer can occur:</para>
njn389f5702009-07-15 07:18:16 +0000400
401<itemizedlist>
402 <listitem>
403 <para>The pointer might have originally been a start-pointer and have been
njn7c02ba72011-01-04 23:46:07 +0000404 moved along deliberately (or not deliberately) by the program. In
405 particular, this can happen if your program uses tagged pointers, i.e.
406 if it uses the bottom one, two or three bits of a pointer, which are
407 normally always zero due to alignment, in order to store extra
408 information.</para>
njn389f5702009-07-15 07:18:16 +0000409 </listitem>
410
411 <listitem>
412 <para>It might be a random junk value in memory, entirely unrelated, just
413 a coincidence.</para>
414 </listitem>
415
416 <listitem>
417 <para>It might be a pointer to an array of C++ objects (which possess
418 destructors) allocated with <computeroutput>new[]</computeroutput>. In
419 this case, some compilers store a "magic cookie" containing the array
420 length at the start of the allocated block, and return a pointer to just
421 past that magic cookie, i.e. an interior-pointer.
422 See <ulink url="http://theory.uwinnipeg.ca/gnu/gcc/gxxint_14.html">this
423 page</ulink> for more information.</para>
424 </listitem>
philippeab1fce92013-09-29 13:47:32 +0000425
426 <listitem>
427 <para>It might be a pointer to the inner char array of a C++
428 <computeroutput>std::string</computeroutput>. For example, some
429 compilers add 3 words at the beginning of the std::string to
430 store the length, the capacity and a reference count before the
431 memory containing the array of characters. They return a pointer
432 just after these 3 words, pointing at the char array.</para>
433 </listitem>
434
435 <listitem>
436 <para>It might be a pointer to an inner part of a C++ object using
437 multiple inheritance. </para>
438 </listitem>
bart9d6d2a92009-07-19 09:19:58 +0000439</itemizedlist>
njn8225cc02009-03-09 22:52:24 +0000440
philippeab1fce92013-09-29 13:47:32 +0000441<para>You can optionally activate heuristics to use during the leak
442search to detect the interior pointers corresponding to
443the <computeroutput>newarray</computeroutput>,
444<computeroutput>stdstring</computeroutput> and
445<computeroutput>multipleinheritance</computeroutput> cases. If the
446heuristic detects that an interior pointer corresponds to such a case,
447the block will be considered as reachable by the interior
448pointer. In other words, the interior pointer will be treated
449as if it were a start pointer.</para>
450
451
njn8225cc02009-03-09 22:52:24 +0000452<para>With that in mind, consider the nine possible cases described by the
453following figure.</para>
454
455<programlisting><![CDATA[
philippe2193a7c2012-12-08 17:54:16 +0000456 Pointer chain AAA Leak Case BBB Leak Case
457 ------------- ------------- -------------
njn8225cc02009-03-09 22:52:24 +0000458(1) RRR ------------> BBB DR
459(2) RRR ---> AAA ---> BBB DR IR
460(3) RRR BBB DL
461(4) RRR AAA ---> BBB DL IL
462(5) RRR ------?-----> BBB (y)DR, (n)DL
463(6) RRR ---> AAA -?-> BBB DR (y)IR, (n)DL
464(7) RRR -?-> AAA ---> BBB (y)DR, (n)DL (y)IR, (n)IL
465(8) RRR -?-> AAA -?-> BBB (y)DR, (n)DL (y,y)IR, (n,y)IL, (_,n)DL
466(9) RRR AAA -?-> BBB DL (y)IL, (n)DL
467
468Pointer chain legend:
469- RRR: a root set node or DR block
470- AAA, BBB: heap blocks
471- --->: a start-pointer
472- -?->: an interior-pointer
473
philippe2193a7c2012-12-08 17:54:16 +0000474Leak Case legend:
njn8225cc02009-03-09 22:52:24 +0000475- DR: Directly reachable
476- IR: Indirectly reachable
477- DL: Directly lost
478- IL: Indirectly lost
479- (y)XY: it's XY if the interior-pointer is a real pointer
480- (n)XY: it's XY if the interior-pointer is not a real pointer
481- (_)XY: it's XY in either case
482]]></programlisting>
483
484<para>Every possible case can be reduced to one of the above nine. Memcheck
485merges some of these cases in its output, resulting in the following four
philippe2193a7c2012-12-08 17:54:16 +0000486leak kinds.</para>
njn8225cc02009-03-09 22:52:24 +0000487
njnab5b7142005-08-16 02:20:17 +0000488
489<itemizedlist>
490
491 <listitem>
njn8225cc02009-03-09 22:52:24 +0000492 <para>"Still reachable". This covers cases 1 and 2 (for the BBB blocks)
493 above. A start-pointer or chain of start-pointers to the block is
494 found. Since the block is still pointed at, the programmer could, at
philippe2193a7c2012-12-08 17:54:16 +0000495 least in principle, have freed it before program exit. "Still reachable"
496 blocks are very common and arguably not a problem. So, by default,
497 Memcheck won't report such blocks individually.</para>
njnab5b7142005-08-16 02:20:17 +0000498 </listitem>
499
500 <listitem>
njn8225cc02009-03-09 22:52:24 +0000501 <para>"Definitely lost". This covers case 3 (for the BBB blocks) above.
502 This means that no pointer to the block can be found. The block is
503 classified as "lost", because the programmer could not possibly have
504 freed it at program exit, since no pointer to it exists. This is likely
505 a symptom of having lost the pointer at some earlier point in the
506 program. Such cases should be fixed by the programmer.</para>
njnab5b7142005-08-16 02:20:17 +0000507 </listitem>
508
njn8225cc02009-03-09 22:52:24 +0000509 <listitem>
510 <para>"Indirectly lost". This covers cases 4 and 9 (for the BBB blocks)
511 above. This means that the block is lost, not because there are no
512 pointers to it, but rather because all the blocks that point to it are
513 themselves lost. For example, if you have a binary tree and the root
514 node is lost, all its children nodes will be indirectly lost. Because
515 the problem will disappear if the definitely lost block that caused the
516 indirect leak is fixed, Memcheck won't report such blocks individually
philippe2193a7c2012-12-08 17:54:16 +0000517 by default.</para>
njn8225cc02009-03-09 22:52:24 +0000518 </listitem>
519
520 <listitem>
521 <para>"Possibly lost". This covers cases 5--8 (for the BBB blocks)
522 above. This means that a chain of one or more pointers to the block has
523 been found, but at least one of the pointers is an interior-pointer.
524 This could just be a random value in memory that happens to point into a
525 block, and so you shouldn't consider this ok unless you know you have
526 interior-pointers.</para>
527 </listitem>
528
njnab5b7142005-08-16 02:20:17 +0000529</itemizedlist>
530
philippe2193a7c2012-12-08 17:54:16 +0000531<para>(Note: This mapping of the nine possible cases onto four leak kinds is
njn8225cc02009-03-09 22:52:24 +0000532not necessarily the best way that leaks could be reported; in particular,
533interior-pointers are treated inconsistently. It is possible the
534categorisation may be improved in the future.)</para>
535
536<para>Furthermore, if suppressions exists for a block, it will be reported
philippe2193a7c2012-12-08 17:54:16 +0000537as "suppressed" no matter what which of the above four kinds it belongs
njn8225cc02009-03-09 22:52:24 +0000538to.</para>
539
540
541<para>The following is an example leak summary.</para>
542
543<programlisting><![CDATA[
544LEAK SUMMARY:
545 definitely lost: 48 bytes in 3 blocks.
546 indirectly lost: 32 bytes in 2 blocks.
547 possibly lost: 96 bytes in 6 blocks.
548 still reachable: 64 bytes in 4 blocks.
549 suppressed: 0 bytes in 0 blocks.
550]]></programlisting>
551
philippeab1fce92013-09-29 13:47:32 +0000552<para>If heuristics have been used to consider some blocks as
553reachable, the leak summary details the heuristically reachable subset
554of 'still reachable:' per heuristic. In the below example, of the 79
555bytes still reachable, 71 bytes (56+7+8) have been considered
556heuristically reachable.
557</para>
558
559<programlisting><![CDATA[
560LEAK SUMMARY:
561 definitely lost: 4 bytes in 1 blocks
562 indirectly lost: 0 bytes in 0 blocks
563 possibly lost: 0 bytes in 0 blocks
564 still reachable: 79 bytes in 5 blocks
565 of which reachable via heuristic:
566 stdstring : 56 bytes in 2 blocks
567 newarray : 7 bytes in 1 blocks
568 multipleinheritance: 8 bytes in 1 blocks
569 suppressed: 0 bytes in 0 blocks
570]]></programlisting>
571
njn7e5d4ed2009-07-30 02:57:52 +0000572<para>If <option>--leak-check=full</option> is specified,
njn8225cc02009-03-09 22:52:24 +0000573Memcheck will give details for each definitely lost or possibly lost block,
njn62dd9fa2009-03-10 21:40:46 +0000574including where it was allocated. (Actually, it merges results for all
philippe2193a7c2012-12-08 17:54:16 +0000575blocks that have the same leak kind and sufficiently similar stack traces
njn62dd9fa2009-03-10 21:40:46 +0000576into a single "loss record". The
njn7e5d4ed2009-07-30 02:57:52 +0000577<option>--leak-resolution</option> lets you control the
njn62dd9fa2009-03-10 21:40:46 +0000578meaning of "sufficiently similar".) It cannot tell you when or how or why
579the pointer to a leaked block was lost; you have to work that out for
580yourself. In general, you should attempt to ensure your programs do not
581have any definitely lost or possibly lost blocks at exit.</para>
njnab5b7142005-08-16 02:20:17 +0000582
583<para>For example:</para>
584<programlisting><![CDATA[
5858 bytes in 1 blocks are definitely lost in loss record 1 of 14
586 at 0x........: malloc (vg_replace_malloc.c:...)
587 by 0x........: mk (leak-tree.c:11)
588 by 0x........: main (leak-tree.c:39)
589
njn8225cc02009-03-09 22:52:24 +000059088 (8 direct, 80 indirect) bytes in 1 blocks are definitely lost in loss record 13 of 14
njnab5b7142005-08-16 02:20:17 +0000591 at 0x........: malloc (vg_replace_malloc.c:...)
592 by 0x........: mk (leak-tree.c:11)
593 by 0x........: main (leak-tree.c:25)
594]]></programlisting>
595
de03e0e7c2005-12-03 23:02:33 +0000596<para>The first message describes a simple case of a single 8 byte block
njn8225cc02009-03-09 22:52:24 +0000597that has been definitely lost. The second case mentions another 8 byte
598block that has been definitely lost; the difference is that a further 80
njn62dd9fa2009-03-10 21:40:46 +0000599bytes in other blocks are indirectly lost because of this lost block.
600The loss records are not presented in any notable order, so the loss record
philippe2193a7c2012-12-08 17:54:16 +0000601numbers aren't particularly meaningful. The loss record numbers can be used
602in the Valgrind gdbserver to list the addresses of the leaked blocks and/or give
603more details about how a block is still reachable.</para>
njnab5b7142005-08-16 02:20:17 +0000604
philippe2193a7c2012-12-08 17:54:16 +0000605<para>The option <option>--show-leak-kinds=&lt;set&gt;</option>
606controls the set of leak kinds to show
607if <option>--leak-check=full</option> is specified. </para>
608
609<para>The <option>&lt;set&gt;</option> of leak kinds is specified by
610using one of the following forms:
611
612<itemizedlist>
613 <listitem>a comma separated list of one or more of
614 <option>definite indirect possible reachable</option>.
615 </listitem>
616
617 <listitem><option>all</option> to specify the complete set (all leak kinds).
618 </listitem>
619
620 <listitem><option>none</option> is the empty set.
621 </listitem>
622</itemizedlist>
623
624</para>
625
626<para> The default value for the leak kinds to show is
627 <option>--show-leak-kinds=definite,possible</option>.
628</para>
629
630<para>To also show the reachable and indirectly lost blocks in addition to the definitely
631and possibly lost blocks, you can use <option>--show-leak-kinds=all</option>.
632To only show the reachable and indirectly lost blocks, use
633<option>--show-leak-kinds=indirect,reachable</option>.
634The reachable and indirectly lost blocks will then be presented as the following
njn8225cc02009-03-09 22:52:24 +0000635two examples show.</para>
636
637<programlisting><![CDATA[
63864 bytes in 4 blocks are still reachable in loss record 2 of 4
639 at 0x........: malloc (vg_replace_malloc.c:177)
640 by 0x........: mk (leak-cases.c:52)
641 by 0x........: main (leak-cases.c:74)
642
64332 bytes in 2 blocks are indirectly lost in loss record 1 of 4
644 at 0x........: malloc (vg_replace_malloc.c:177)
645 by 0x........: mk (leak-cases.c:52)
646 by 0x........: main (leak-cases.c:80)
647]]></programlisting>
njnab5b7142005-08-16 02:20:17 +0000648
philippe2193a7c2012-12-08 17:54:16 +0000649<para>Because there are different kinds of leaks with different
650severities, an interesting question is this: which leaks should be
651counted as true "errors" and which should not?
652</para>
njn26670552009-08-13 00:02:30 +0000653
philippe2193a7c2012-12-08 17:54:16 +0000654<para> The answer to this question affects the numbers printed in
655the <computeroutput>ERROR SUMMARY</computeroutput> line, and also the
656effect of the <option>--error-exitcode</option> option. First, a leak
657is only counted as a true "error"
658if <option>--leak-check=full</option> is specified. Then, the
659option <option>--errors-for-leak-kinds=&lt;set&gt;</option> controls
660the set of leak kinds to consider as errors. The default value
661is <option>--errors-for-leak-kinds=definite,possible</option>
662</para>
njn26670552009-08-13 00:02:30 +0000663
njnab5b7142005-08-16 02:20:17 +0000664</sect2>
665
njn3e986b22004-11-30 10:43:45 +0000666</sect1>
667
668
669
njna3311642009-08-10 01:29:14 +0000670<sect1 id="mc-manual.options"
671 xreflabel="Memcheck Command-Line Options">
672<title>Memcheck Command-Line Options</title>
njnc1abdcb2009-08-05 05:11:02 +0000673
674<!-- start of xi:include in the manpage -->
675<variablelist id="mc.opts.list">
676
677 <varlistentry id="opt.leak-check" xreflabel="--leak-check">
678 <term>
679 <option><![CDATA[--leak-check=<no|summary|yes|full> [default: summary] ]]></option>
680 </term>
681 <listitem>
682 <para>When enabled, search for memory leaks when the client
683 program finishes. If set to <varname>summary</varname>, it says how
684 many leaks occurred. If set to <varname>full</varname> or
685 <varname>yes</varname>, it also gives details of each individual
686 leak.</para>
687 </listitem>
688 </varlistentry>
689
690 <varlistentry id="opt.leak-resolution" xreflabel="--leak-resolution">
691 <term>
692 <option><![CDATA[--leak-resolution=<low|med|high> [default: high] ]]></option>
693 </term>
694 <listitem>
695 <para>When doing leak checking, determines how willing
696 Memcheck is to consider different backtraces to
697 be the same for the purposes of merging multiple leaks into a single
698 leak report. When set to <varname>low</varname>, only the first
699 two entries need match. When <varname>med</varname>, four entries
700 have to match. When <varname>high</varname>, all entries need to
701 match.</para>
702
703 <para>For hardcore leak debugging, you probably want to use
704 <option>--leak-resolution=high</option> together with
705 <option>--num-callers=40</option> or some such large number.
706 </para>
707
708 <para>Note that the <option>--leak-resolution</option> setting
709 does not affect Memcheck's ability to find
710 leaks. It only changes how the results are presented.</para>
711 </listitem>
712 </varlistentry>
713
philippe2193a7c2012-12-08 17:54:16 +0000714 <varlistentry id="opt.show-leak-kinds" xreflabel="--show-leak-kinds">
njnc1abdcb2009-08-05 05:11:02 +0000715 <term>
philippe2193a7c2012-12-08 17:54:16 +0000716 <option><![CDATA[--show-leak-kinds=<set> [default: definite,possible] ]]></option>
njnc1abdcb2009-08-05 05:11:02 +0000717 </term>
718 <listitem>
philippe2193a7c2012-12-08 17:54:16 +0000719 <para>Specifies the leak kinds to show in a full leak search by
720 using one of the following forms:
721
722 <itemizedlist>
723 <listitem>a comma separated list of one or more of
724 <option>definite indirect possible reachable</option>.
725 </listitem>
726
727 <listitem><option>all</option> to specify the complete set (all leak kinds).
728 It is equivalent to
729 <option>--show-leak-kinds=definite,indirect,possible,reachable</option>.
730 </listitem>
731
732 <listitem><option>none</option> is the empty set.
733 </listitem>
734 </itemizedlist>
735 </para>
njnc1abdcb2009-08-05 05:11:02 +0000736 </listitem>
737 </varlistentry>
738
philippe2193a7c2012-12-08 17:54:16 +0000739
740 <varlistentry id="opt.errors-for-leak-kinds" xreflabel="--errors-for-leak-kinds">
741 <term>
742 <option><![CDATA[--errors-for-leak-kinds=<set> [default: definite,possible] ]]></option>
743 </term>
744 <listitem>
745 <para>Specifies the leak kinds to count as errors in a full leak search. The
746 <option><![CDATA[<set>]]></option> is specified similarly to
747 <option>--show-leak-kinds</option>
748 </para>
749 </listitem>
750 </varlistentry>
751
752
philippeab1fce92013-09-29 13:47:32 +0000753 <varlistentry id="opt.leak-check-heuristics" xreflabel="--leak-check-heuristics">
754 <term>
755 <option><![CDATA[--leak-check-heuristics=<set> [default: none] ]]></option>
756 </term>
757 <listitem>
758 <para>Specifies the leak check heuristics to use during leak search
759 to discover interior pointers with which a block should be considered
760 as reachable. The heuristic set is specified by using one of the
761 following forms:
762
763 <itemizedlist>
764 <listitem>a comma separated list of one or more of
765 <option>stdstring newarray multipleinheritance</option>.
766 </listitem>
767
768 <listitem><option>all</option> to activate the complete set of
769 heuristics.
770 It is equivalent to
771 <option>--leak-check-heuristics=stdstring,newarray,multipleinheritance</option>.
772 </listitem>
773
774 <listitem><option>none</option> is the empty set.
775 </listitem>
776 </itemizedlist>
777 </para>
778
779 <para>Note that these heuristics are dependent on the layout of the objects
780 produced by the C++ compiler. They have been tested with some gcc versions
781 (e.g. 4.4 and 4.7). They might not work properly with other C++ compilers.
782 </para>
783 </listitem>
784 </varlistentry>
785
786
philippe2193a7c2012-12-08 17:54:16 +0000787 <varlistentry id="opt.show-reachable" xreflabel="--show-reachable">
788 <term>
789 <option><![CDATA[--show-reachable=<yes|no> ]]></option>
790 </term>
791 <term>
792 <option><![CDATA[--show-possibly-lost=<yes|no> ]]></option>
793 </term>
794 <listitem>
795 <para>These options provide an alternative way to specify the leak kinds to show:
796 <itemizedlist>
797 <listitem>
798 <option>--show-reachable=no --show-possibly-lost=yes</option> is equivalent to
799 <option>--show-leak-kinds=definite,possible</option>.
800 </listitem>
801 <listitem>
802 <option>--show-reachable=no --show-possibly-lost=no</option> is equivalent to
803 <option>--show-leak-kinds=definite</option>.
804 </listitem>
805 <listitem>
806 <option>--show-reachable=yes</option> is equivalent to
807 <option>--show-leak-kinds=all</option>.
808 Note that <option>--show-possibly-lost=no</option> has no effect
809 if <option>--show-reachable=yes</option> is specified.
810 </listitem>
811 </itemizedlist>
812 </para>
813 </listitem>
814 </varlistentry>
815
njnc1abdcb2009-08-05 05:11:02 +0000816 <varlistentry id="opt.undef-value-errors" xreflabel="--undef-value-errors">
817 <term>
818 <option><![CDATA[--undef-value-errors=<yes|no> [default: yes] ]]></option>
819 </term>
820 <listitem>
821 <para>Controls whether Memcheck reports
822 uses of undefined value errors. Set this to
823 <varname>no</varname> if you don't want to see undefined value
824 errors. It also has the side effect of speeding up
825 Memcheck somewhat.
826 </para>
827 </listitem>
828 </varlistentry>
829
830 <varlistentry id="opt.track-origins" xreflabel="--track-origins">
831 <term>
832 <option><![CDATA[--track-origins=<yes|no> [default: no] ]]></option>
833 </term>
834 <listitem>
835 <para>Controls whether Memcheck tracks
836 the origin of uninitialised values. By default, it does not,
837 which means that although it can tell you that an
838 uninitialised value is being used in a dangerous way, it
839 cannot tell you where the uninitialised value came from. This
840 often makes it difficult to track down the root problem.
841 </para>
842 <para>When set
843 to <varname>yes</varname>, Memcheck keeps
844 track of the origins of all uninitialised values. Then, when
845 an uninitialised value error is
846 reported, Memcheck will try to show the
847 origin of the value. An origin can be one of the following
848 four places: a heap block, a stack allocation, a client
849 request, or miscellaneous other sources (eg, a call
850 to <varname>brk</varname>).
851 </para>
852 <para>For uninitialised values originating from a heap
853 block, Memcheck shows where the block was
854 allocated. For uninitialised values originating from a stack
855 allocation, Memcheck can tell you which
856 function allocated the value, but no more than that -- typically
857 it shows you the source location of the opening brace of the
858 function. So you should carefully check that all of the
859 function's local variables are initialised properly.
860 </para>
861 <para>Performance overhead: origin tracking is expensive. It
862 halves Memcheck's speed and increases
863 memory use by a minimum of 100MB, and possibly more.
864 Nevertheless it can drastically reduce the effort required to
865 identify the root cause of uninitialised value errors, and so
866 is often a programmer productivity win, despite running
867 more slowly.
868 </para>
869 <para>Accuracy: Memcheck tracks origins
870 quite accurately. To avoid very large space and time
871 overheads, some approximations are made. It is possible,
872 although unlikely, that Memcheck will report an incorrect origin, or
873 not be able to identify any origin.
874 </para>
875 <para>Note that the combination
876 <option>--track-origins=yes</option>
877 and <option>--undef-value-errors=no</option> is
878 nonsensical. Memcheck checks for and
879 rejects this combination at startup.
880 </para>
881 </listitem>
882 </varlistentry>
883
884 <varlistentry id="opt.partial-loads-ok" xreflabel="--partial-loads-ok">
885 <term>
886 <option><![CDATA[--partial-loads-ok=<yes|no> [default: no] ]]></option>
887 </term>
888 <listitem>
889 <para>Controls how Memcheck handles word-sized,
890 word-aligned loads from addresses for which some bytes are
891 addressable and others are not. When <varname>yes</varname>, such
892 loads do not produce an address error. Instead, loaded bytes
893 originating from illegal addresses are marked as uninitialised, and
894 those corresponding to legal addresses are handled in the normal
895 way.</para>
896
897 <para>When <varname>no</varname>, loads from partially invalid
898 addresses are treated the same as loads from completely invalid
899 addresses: an illegal-address error is issued, and the resulting
900 bytes are marked as initialised.</para>
901
902 <para>Note that code that behaves in this way is in violation of
mjw2be51222013-04-05 13:19:12 +0000903 the ISO C/C++ standards, and should be considered broken. If
njna3311642009-08-10 01:29:14 +0000904 at all possible, such code should be fixed. This option should be
njnc1abdcb2009-08-05 05:11:02 +0000905 used only as a last resort.</para>
906 </listitem>
907 </varlistentry>
908
philippe8617b5b2013-01-12 19:53:08 +0000909 <varlistentry id="opt.keep-stacktraces" xreflabel="--keep-stacktraces">
910 <term>
911 <option><![CDATA[--keep-stacktraces=alloc|free|alloc-and-free|alloc-then-free|none [default: alloc-then-free] ]]></option>
912 </term>
913 <listitem>
914 <para>Controls which stack trace(s) to keep for malloc'd and/or
915 free'd blocks.
916 </para>
917
918 <para>With <varname>alloc-then-free</varname>, the malloc stack
919 trace is recorded at allocation time. The block contains a
920 reference to this allocation stack trace. When the block is
921 freed, the block will then reference the free stack trace. So,
922 a 'use after free' error will only report the free stack trace.
923 </para>
924
925 <para>With <varname>alloc-and-free</varname>, both the malloc
926 and the free stack trace (for freed block) are recorded and
927 referenced by the block. A 'use after free' error will report
928 the free stack trace, followed by the stack trace where this
929 block was allocated. Compared
930 to <varname>alloc-then-free</varname>, this value very slightly
931 increases Valgrind memory use as the block contains two references
932 instead of one.
933 </para>
934
935 <para>With <varname>alloc</varname>, only the malloc stack trace
936 is recorded (and reported). With <varname>free</varname>, only
937 the free stack trace is recorded (and reported). These values
938 somewhat decrease Valgrind memory and cpu usage. They can be
939 useful depending on the error types you are searching for and
940 the level of details you need to analyse them. For example, if
941 you are only interested in memory leak errors, it is sufficient
942 to record the allocation stack traces.
943 </para>
944
945 <para>With <varname>none</varname>, no stack traces are recorded
946 for malloc and free operations. If your program allocates a lot
947 of blocks and/or from many different stack traces, this can
948 significantly decrease cpu and/or memory. Of course, very little
949 details will be reported for errors related to heap blocks.
950 </para>
951
952 <para> Note that once a stack trace is recorded, Valgrind keeps
953 the stack trace in memory even if not referenced anymore by
954 any block. Some programs (for example, recursive algorithms)
955 can generate a huge number of stack traces. If Valgrind uses too
956 much memory in such circumstances, you can reduce the memory
957 usage with the options <varname>--keep-stacktraces</varname>
958 and/or by using a smaller value for the
959 option <varname>--num-callers</varname>.
960 </para>
961 </listitem>
962 </varlistentry>
963
njnc1abdcb2009-08-05 05:11:02 +0000964 <varlistentry id="opt.freelist-vol" xreflabel="--freelist-vol">
965 <term>
sewardje089f012010-10-13 21:47:29 +0000966 <option><![CDATA[--freelist-vol=<number> [default: 20000000] ]]></option>
njnc1abdcb2009-08-05 05:11:02 +0000967 </term>
968 <listitem>
969 <para>When the client program releases memory using
970 <function>free</function> (in <literal>C</literal>) or
971 <computeroutput>delete</computeroutput>
972 (<literal>C++</literal>), that memory is not immediately made
973 available for re-allocation. Instead, it is marked inaccessible
974 and placed in a queue of freed blocks. The purpose is to defer as
975 long as possible the point at which freed-up memory comes back
976 into circulation. This increases the chance that
977 Memcheck will be able to detect invalid
978 accesses to blocks for some significant period of time after they
979 have been freed.</para>
980
njna3311642009-08-10 01:29:14 +0000981 <para>This option specifies the maximum total size, in bytes, of the
sewardje089f012010-10-13 21:47:29 +0000982 blocks in the queue. The default value is twenty million bytes.
njnc1abdcb2009-08-05 05:11:02 +0000983 Increasing this increases the total amount of memory used by
984 Memcheck but may detect invalid uses of freed
985 blocks which would otherwise go undetected.</para>
986 </listitem>
987 </varlistentry>
988
sewardj403d8aa2011-10-22 19:48:57 +0000989 <varlistentry id="opt.freelist-big-blocks" xreflabel="--freelist-big-blocks">
990 <term>
991 <option><![CDATA[--freelist-big-blocks=<number> [default: 1000000] ]]></option>
992 </term>
993 <listitem>
994 <para>When making blocks from the queue of freed blocks available
995 for re-allocation, Memcheck will in priority re-circulate the blocks
996 with a size greater or equal to <option>--freelist-big-blocks</option>.
997 This ensures that freeing big blocks (in particular freeing blocks bigger than
998 <option>--freelist-vol</option>) does not immediately lead to a re-circulation
999 of all (or a lot of) the small blocks in the free list. In other words,
1000 this option increases the likelihood to discover dangling pointers
1001 for the "small" blocks, even when big blocks are freed.</para>
1002 <para>Setting a value of 0 means that all the blocks are re-circulated
1003 in a FIFO order. </para>
1004 </listitem>
1005 </varlistentry>
1006
njnc1abdcb2009-08-05 05:11:02 +00001007 <varlistentry id="opt.workaround-gcc296-bugs" xreflabel="--workaround-gcc296-bugs">
1008 <term>
1009 <option><![CDATA[--workaround-gcc296-bugs=<yes|no> [default: no] ]]></option>
1010 </term>
1011 <listitem>
1012 <para>When enabled, assume that reads and writes some small
1013 distance below the stack pointer are due to bugs in GCC 2.96, and
1014 does not report them. The "small distance" is 256 bytes by
1015 default. Note that GCC 2.96 is the default compiler on some ancient
1016 Linux distributions (RedHat 7.X) and so you may need to use this
njna3311642009-08-10 01:29:14 +00001017 option. Do not use it if you do not have to, as it can cause real
njnc1abdcb2009-08-05 05:11:02 +00001018 errors to be overlooked. A better alternative is to use a more
1019 recent GCC in which this bug is fixed.</para>
1020
njna3311642009-08-10 01:29:14 +00001021 <para>You may also need to use this option when working with
njnc1abdcb2009-08-05 05:11:02 +00001022 GCC 3.X or 4.X on 32-bit PowerPC Linux. This is because
1023 GCC generates code which occasionally accesses below the
1024 stack pointer, particularly for floating-point to/from integer
1025 conversions. This is in violation of the 32-bit PowerPC ELF
1026 specification, which makes no provision for locations below the
1027 stack pointer to be accessible.</para>
1028 </listitem>
1029 </varlistentry>
1030
1031 <varlistentry id="opt.ignore-ranges" xreflabel="--ignore-ranges">
1032 <term>
1033 <option><![CDATA[--ignore-ranges=0xPP-0xQQ[,0xRR-0xSS] ]]></option>
1034 </term>
1035 <listitem>
1036 <para>Any ranges listed in this option (and multiple ranges can be
1037 specified, separated by commas) will be ignored by Memcheck's
1038 addressability checking.</para>
1039 </listitem>
1040 </varlistentry>
1041
1042 <varlistentry id="opt.malloc-fill" xreflabel="--malloc-fill">
1043 <term>
1044 <option><![CDATA[--malloc-fill=<hexnumber> ]]></option>
1045 </term>
1046 <listitem>
1047 <para>Fills blocks allocated
1048 by <computeroutput>malloc</computeroutput>,
1049 <computeroutput>new</computeroutput>, etc, but not
1050 by <computeroutput>calloc</computeroutput>, with the specified
1051 byte. This can be useful when trying to shake out obscure
1052 memory corruption problems. The allocated area is still
njna3311642009-08-10 01:29:14 +00001053 regarded by Memcheck as undefined -- this option only affects its
philippea2cc0c02012-05-11 22:10:39 +00001054 contents. Note that <option>--malloc-fill</option> does not
1055 affect a block of memory when it is used as argument
1056 to client requests VALGRIND_MEMPOOL_ALLOC or
1057 VALGRIND_MALLOCLIKE_BLOCK.
njnc1abdcb2009-08-05 05:11:02 +00001058 </para>
1059 </listitem>
1060 </varlistentry>
1061
1062 <varlistentry id="opt.free-fill" xreflabel="--free-fill">
1063 <term>
1064 <option><![CDATA[--free-fill=<hexnumber> ]]></option>
1065 </term>
1066 <listitem>
1067 <para>Fills blocks freed
1068 by <computeroutput>free</computeroutput>,
1069 <computeroutput>delete</computeroutput>, etc, with the
1070 specified byte value. This can be useful when trying to shake out
1071 obscure memory corruption problems. The freed area is still
njna3311642009-08-10 01:29:14 +00001072 regarded by Memcheck as not valid for access -- this option only
philippea2cc0c02012-05-11 22:10:39 +00001073 affects its contents. Note that <option>--free-fill</option> does not
1074 affect a block of memory when it is used as argument to
1075 client requests VALGRIND_MEMPOOL_FREE or VALGRIND_FREELIKE_BLOCK.
njnc1abdcb2009-08-05 05:11:02 +00001076 </para>
1077 </listitem>
1078 </varlistentry>
1079
1080</variablelist>
1081<!-- end of xi:include in the manpage -->
1082
1083</sect1>
1084
1085
njn62ad73d2005-08-15 04:26:13 +00001086<sect1 id="mc-manual.suppfiles" xreflabel="Writing suppression files">
1087<title>Writing suppression files</title>
njn3e986b22004-11-30 10:43:45 +00001088
1089<para>The basic suppression format is described in
1090<xref linkend="manual-core.suppress"/>.</para>
1091
sewardj08e31e22007-05-23 21:58:33 +00001092<para>The suppression-type (second) line should have the form:</para>
njn3e986b22004-11-30 10:43:45 +00001093<programlisting><![CDATA[
1094Memcheck:suppression_type]]></programlisting>
1095
njn3e986b22004-11-30 10:43:45 +00001096<para>The Memcheck suppression types are as follows:</para>
1097
1098<itemizedlist>
1099 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001100 <para><varname>Value1</varname>,
1101 <varname>Value2</varname>,
1102 <varname>Value4</varname>,
1103 <varname>Value8</varname>,
1104 <varname>Value16</varname>,
njn3e986b22004-11-30 10:43:45 +00001105 meaning an uninitialised-value error when
1106 using a value of 1, 2, 4, 8 or 16 bytes.</para>
1107 </listitem>
1108
1109 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001110 <para><varname>Cond</varname> (or its old
de03e0e7c2005-12-03 23:02:33 +00001111 name, <varname>Value0</varname>), meaning use
njn3e986b22004-11-30 10:43:45 +00001112 of an uninitialised CPU condition code.</para>
1113 </listitem>
1114
1115 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001116 <para><varname>Addr1</varname>,
de03e0e7c2005-12-03 23:02:33 +00001117 <varname>Addr2</varname>,
1118 <varname>Addr4</varname>,
1119 <varname>Addr8</varname>,
1120 <varname>Addr16</varname>,
njn3e986b22004-11-30 10:43:45 +00001121 meaning an invalid address during a
1122 memory access of 1, 2, 4, 8 or 16 bytes respectively.</para>
1123 </listitem>
1124
1125 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001126 <para><varname>Jump</varname>, meaning an
njn718d3b12006-12-16 00:54:12 +00001127 jump to an unaddressable location error.</para>
1128 </listitem>
1129
1130 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001131 <para><varname>Param</varname>, meaning an
njn3e986b22004-11-30 10:43:45 +00001132 invalid system call parameter error.</para>
1133 </listitem>
1134
1135 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001136 <para><varname>Free</varname>, meaning an
njn3e986b22004-11-30 10:43:45 +00001137 invalid or mismatching free.</para>
1138 </listitem>
1139
1140 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001141 <para><varname>Overlap</varname>, meaning a
njn3e986b22004-11-30 10:43:45 +00001142 <computeroutput>src</computeroutput> /
1143 <computeroutput>dst</computeroutput> overlap in
njn2f7eebe2009-08-05 06:34:27 +00001144 <function>memcpy</function> or a similar function.</para>
njn3e986b22004-11-30 10:43:45 +00001145 </listitem>
1146
1147 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001148 <para><varname>Leak</varname>, meaning
njn62ad73d2005-08-15 04:26:13 +00001149 a memory leak.</para>
njn3e986b22004-11-30 10:43:45 +00001150 </listitem>
1151
1152</itemizedlist>
1153
philippe2193a7c2012-12-08 17:54:16 +00001154<para><computeroutput>Param</computeroutput> errors have a mandatory extra
sewardj08e31e22007-05-23 21:58:33 +00001155information line at this point, which is the name of the offending
philippe2193a7c2012-12-08 17:54:16 +00001156system call parameter. </para>
1157
1158<para>
1159<computeroutput>Leak</computeroutput> errors have an optional extra
1160information line. This optional extra information line has the
1161following format:</para>
1162<programlisting><![CDATA[
1163match-leak-kinds:<set>]]></programlisting>
1164<para>where <computeroutput>&lt;set&gt;</computeroutput> specifies which
1165leak kinds are matched by this suppression entry.
1166<computeroutput>&lt;set&gt;</computeroutput> is specified similarly
1167to the option <option>--show-leak-kinds</option>.
1168If this optional extra line is not present, the suppression entry will match
1169all leak kinds.
1170</para>
1171
1172<para>The other memcheck error kinds do not have extra lines.</para>
njn3e986b22004-11-30 10:43:45 +00001173
njn2f7eebe2009-08-05 06:34:27 +00001174<para>The first line of the calling context: for <varname>ValueN</varname>
1175and <varname>AddrN</varname> errors, it is either the name of the function
1176in which the error occurred, or, failing that, the full path of the
1177<filename>.so</filename> file
1178or executable containing the error location. For <varname>Free</varname> errors, is the name
1179of the function doing the freeing (eg, <function>free</function>,
1180<function>__builtin_vec_delete</function>, etc). For
1181<varname>Overlap</varname> errors, is the name of the function with the
1182overlapping arguments (eg. <function>memcpy</function>,
1183<function>strcpy</function>, etc).</para>
njn3e986b22004-11-30 10:43:45 +00001184
1185<para>Lastly, there's the rest of the calling context.</para>
1186
1187</sect1>
1188
1189
1190
1191<sect1 id="mc-manual.machine"
1192 xreflabel="Details of Memcheck's checking machinery">
1193<title>Details of Memcheck's checking machinery</title>
1194
1195<para>Read this section if you want to know, in detail, exactly
1196what and how Memcheck is checking.</para>
1197
1198
1199<sect2 id="mc-manual.value" xreflabel="Valid-value (V) bit">
1200<title>Valid-value (V) bits</title>
1201
de03e0e7c2005-12-03 23:02:33 +00001202<para>It is simplest to think of Memcheck implementing a synthetic CPU
1203which is identical to a real CPU, except for one crucial detail. Every
1204bit (literally) of data processed, stored and handled by the real CPU
1205has, in the synthetic CPU, an associated "valid-value" bit, which says
1206whether or not the accompanying bit has a legitimate value. In the
1207discussions which follow, this bit is referred to as the V (valid-value)
njn3e986b22004-11-30 10:43:45 +00001208bit.</para>
1209
de03e0e7c2005-12-03 23:02:33 +00001210<para>Each byte in the system therefore has a 8 V bits which follow it
1211wherever it goes. For example, when the CPU loads a word-size item (4
1212bytes) from memory, it also loads the corresponding 32 V bits from a
1213bitmap which stores the V bits for the process' entire address space.
1214If the CPU should later write the whole or some part of that value to
1215memory at a different address, the relevant V bits will be stored back
1216in the V-bit bitmap.</para>
njn3e986b22004-11-30 10:43:45 +00001217
njn2f7eebe2009-08-05 06:34:27 +00001218<para>In short, each bit in the system has (conceptually) an associated V
1219bit, which follows it around everywhere, even inside the CPU. Yes, all the
1220CPU's registers (integer, floating point, vector and condition registers)
1221have their own V bit vectors. For this to work, Memcheck uses a great deal
1222of compression to represent the V bits compactly.</para>
njn3e986b22004-11-30 10:43:45 +00001223
de03e0e7c2005-12-03 23:02:33 +00001224<para>Copying values around does not cause Memcheck to check for, or
1225report on, errors. However, when a value is used in a way which might
njn2f7eebe2009-08-05 06:34:27 +00001226conceivably affect your program's externally-visible behaviour,
1227the associated V bits are immediately checked. If any of these indicate
1228that the value is undefined (even partially), an error is reported.</para>
njn3e986b22004-11-30 10:43:45 +00001229
1230<para>Here's an (admittedly nonsensical) example:</para>
1231<programlisting><![CDATA[
1232int i, j;
1233int a[10], b[10];
1234for ( i = 0; i < 10; i++ ) {
1235 j = a[i];
1236 b[i] = j;
1237}]]></programlisting>
1238
de03e0e7c2005-12-03 23:02:33 +00001239<para>Memcheck emits no complaints about this, since it merely copies
1240uninitialised values from <varname>a[]</varname> into
sewardj08e31e22007-05-23 21:58:33 +00001241<varname>b[]</varname>, and doesn't use them in a way which could
1242affect the behaviour of the program. However, if
de03e0e7c2005-12-03 23:02:33 +00001243the loop is changed to:</para>
njn3e986b22004-11-30 10:43:45 +00001244<programlisting><![CDATA[
1245for ( i = 0; i < 10; i++ ) {
1246 j += a[i];
1247}
1248if ( j == 77 )
1249 printf("hello there\n");
1250]]></programlisting>
1251
sewardj08e31e22007-05-23 21:58:33 +00001252<para>then Memcheck will complain, at the
de03e0e7c2005-12-03 23:02:33 +00001253<computeroutput>if</computeroutput>, that the condition depends on
1254uninitialised values. Note that it <command>doesn't</command> complain
1255at the <varname>j += a[i];</varname>, since at that point the
1256undefinedness is not "observable". It's only when a decision has to be
1257made as to whether or not to do the <function>printf</function> -- an
1258observable action of your program -- that Memcheck complains.</para>
njn3e986b22004-11-30 10:43:45 +00001259
de03e0e7c2005-12-03 23:02:33 +00001260<para>Most low level operations, such as adds, cause Memcheck to use the
1261V bits for the operands to calculate the V bits for the result. Even if
1262the result is partially or wholly undefined, it does not
njn62ad73d2005-08-15 04:26:13 +00001263complain.</para>
njn3e986b22004-11-30 10:43:45 +00001264
de03e0e7c2005-12-03 23:02:33 +00001265<para>Checks on definedness only occur in three places: when a value is
1266used to generate a memory address, when control flow decision needs to
sewardj08e31e22007-05-23 21:58:33 +00001267be made, and when a system call is detected, Memcheck checks definedness
de03e0e7c2005-12-03 23:02:33 +00001268of parameters as required.</para>
njn3e986b22004-11-30 10:43:45 +00001269
1270<para>If a check should detect undefinedness, an error message is
de03e0e7c2005-12-03 23:02:33 +00001271issued. The resulting value is subsequently regarded as well-defined.
sewardj08e31e22007-05-23 21:58:33 +00001272To do otherwise would give long chains of error messages. In other
1273words, once Memcheck reports an undefined value error, it tries to
1274avoid reporting further errors derived from that same undefined
1275value.</para>
njn3e986b22004-11-30 10:43:45 +00001276
de03e0e7c2005-12-03 23:02:33 +00001277<para>This sounds overcomplicated. Why not just check all reads from
1278memory, and complain if an undefined value is loaded into a CPU
1279register? Well, that doesn't work well, because perfectly legitimate C
1280programs routinely copy uninitialised values around in memory, and we
1281don't want endless complaints about that. Here's the canonical example.
1282Consider a struct like this:</para>
njn3e986b22004-11-30 10:43:45 +00001283<programlisting><![CDATA[
1284struct S { int x; char c; };
1285struct S s1, s2;
1286s1.x = 42;
1287s1.c = 'z';
1288s2 = s1;
1289]]></programlisting>
1290
de03e0e7c2005-12-03 23:02:33 +00001291<para>The question to ask is: how large is <varname>struct S</varname>,
1292in bytes? An <varname>int</varname> is 4 bytes and a
1293<varname>char</varname> one byte, so perhaps a <varname>struct
sewardj08e31e22007-05-23 21:58:33 +00001294S</varname> occupies 5 bytes? Wrong. All non-toy compilers we know
de03e0e7c2005-12-03 23:02:33 +00001295of will round the size of <varname>struct S</varname> up to a whole
1296number of words, in this case 8 bytes. Not doing this forces compilers
sewardj08e31e22007-05-23 21:58:33 +00001297to generate truly appalling code for accessing arrays of
1298<varname>struct S</varname>'s on some architectures.</para>
njn3e986b22004-11-30 10:43:45 +00001299
de03e0e7c2005-12-03 23:02:33 +00001300<para>So <varname>s1</varname> occupies 8 bytes, yet only 5 of them will
njn7316df22009-08-04 01:16:01 +00001301be initialised. For the assignment <varname>s2 = s1</varname>, GCC
de03e0e7c2005-12-03 23:02:33 +00001302generates code to copy all 8 bytes wholesale into <varname>s2</varname>
1303without regard for their meaning. If Memcheck simply checked values as
1304they came out of memory, it would yelp every time a structure assignment
sewardj08e31e22007-05-23 21:58:33 +00001305like this happened. So the more complicated behaviour described above
njn7316df22009-08-04 01:16:01 +00001306is necessary. This allows GCC to copy
de03e0e7c2005-12-03 23:02:33 +00001307<varname>s1</varname> into <varname>s2</varname> any way it likes, and a
1308warning will only be emitted if the uninitialised values are later
1309used.</para>
njn3e986b22004-11-30 10:43:45 +00001310
njn3e986b22004-11-30 10:43:45 +00001311</sect2>
1312
1313
1314<sect2 id="mc-manual.vaddress" xreflabel=" Valid-address (A) bits">
1315<title>Valid-address (A) bits</title>
1316
de03e0e7c2005-12-03 23:02:33 +00001317<para>Notice that the previous subsection describes how the validity of
1318values is established and maintained without having to say whether the
1319program does or does not have the right to access any particular memory
sewardj08e31e22007-05-23 21:58:33 +00001320location. We now consider the latter question.</para>
njn3e986b22004-11-30 10:43:45 +00001321
de03e0e7c2005-12-03 23:02:33 +00001322<para>As described above, every bit in memory or in the CPU has an
1323associated valid-value (V) bit. In addition, all bytes in memory, but
1324not in the CPU, have an associated valid-address (A) bit. This
1325indicates whether or not the program can legitimately read or write that
sewardj49d5a282011-02-28 10:26:42 +00001326location. It does not give any indication of the validity of the data
de03e0e7c2005-12-03 23:02:33 +00001327at that location -- that's the job of the V bits -- only whether or not
1328the location may be accessed.</para>
njn3e986b22004-11-30 10:43:45 +00001329
de03e0e7c2005-12-03 23:02:33 +00001330<para>Every time your program reads or writes memory, Memcheck checks
1331the A bits associated with the address. If any of them indicate an
1332invalid address, an error is emitted. Note that the reads and writes
1333themselves do not change the A bits, only consult them.</para>
njn3e986b22004-11-30 10:43:45 +00001334
njn62ad73d2005-08-15 04:26:13 +00001335<para>So how do the A bits get set/cleared? Like this:</para>
njn3e986b22004-11-30 10:43:45 +00001336
1337<itemizedlist>
1338 <listitem>
1339 <para>When the program starts, all the global data areas are
1340 marked as accessible.</para>
1341 </listitem>
1342
1343 <listitem>
bartaf25f672009-06-26 19:03:53 +00001344 <para>When the program does
1345 <function>malloc</function>/<computeroutput>new</computeroutput>,
1346 the A bits for exactly the area allocated, and not a byte more,
1347 are marked as accessible. Upon freeing the area the A bits are
1348 changed to indicate inaccessibility.</para>
njn3e986b22004-11-30 10:43:45 +00001349 </listitem>
1350
1351 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001352 <para>When the stack pointer register (<literal>SP</literal>) moves
1353 up or down, A bits are set. The rule is that the area from
1354 <literal>SP</literal> up to the base of the stack is marked as
1355 accessible, and below <literal>SP</literal> is inaccessible. (If
1356 that sounds illogical, bear in mind that the stack grows down, not
1357 up, on almost all Unix systems, including GNU/Linux.) Tracking
1358 <literal>SP</literal> like this has the useful side-effect that the
1359 section of stack used by a function for local variables etc is
1360 automatically marked accessible on function entry and inaccessible
1361 on exit.</para>
njn3e986b22004-11-30 10:43:45 +00001362 </listitem>
1363
1364 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001365 <para>When doing system calls, A bits are changed appropriately.
sewardj08e31e22007-05-23 21:58:33 +00001366 For example, <literal>mmap</literal>
1367 magically makes files appear in the process'
1368 address space, so the A bits must be updated if <literal>mmap</literal>
de03e0e7c2005-12-03 23:02:33 +00001369 succeeds.</para>
njn3e986b22004-11-30 10:43:45 +00001370 </listitem>
1371
1372 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001373 <para>Optionally, your program can tell Memcheck about such changes
de03e0e7c2005-12-03 23:02:33 +00001374 explicitly, using the client request mechanism described
1375 above.</para>
njn3e986b22004-11-30 10:43:45 +00001376 </listitem>
1377
1378</itemizedlist>
1379
1380</sect2>
1381
1382
1383<sect2 id="mc-manual.together" xreflabel="Putting it all together">
1384<title>Putting it all together</title>
1385
1386<para>Memcheck's checking machinery can be summarised as
1387follows:</para>
1388
1389<itemizedlist>
1390 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001391 <para>Each byte in memory has 8 associated V (valid-value) bits,
1392 saying whether or not the byte has a defined value, and a single A
1393 (valid-address) bit, saying whether or not the program currently has
sewardje089f012010-10-13 21:47:29 +00001394 the right to read/write that address. As mentioned above, heavy
1395 use of compression means the overhead is typically around 25%.</para>
njn3e986b22004-11-30 10:43:45 +00001396 </listitem>
1397
1398 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001399 <para>When memory is read or written, the relevant A bits are
sewardj08e31e22007-05-23 21:58:33 +00001400 consulted. If they indicate an invalid address, Memcheck emits an
de03e0e7c2005-12-03 23:02:33 +00001401 Invalid read or Invalid write error.</para>
njn3e986b22004-11-30 10:43:45 +00001402 </listitem>
1403
1404 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001405 <para>When memory is read into the CPU's registers, the relevant V
1406 bits are fetched from memory and stored in the simulated CPU. They
1407 are not consulted.</para>
njn3e986b22004-11-30 10:43:45 +00001408 </listitem>
1409
1410 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001411 <para>When a register is written out to memory, the V bits for that
1412 register are written back to memory too.</para>
njn3e986b22004-11-30 10:43:45 +00001413 </listitem>
1414
1415 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001416 <para>When values in CPU registers are used to generate a memory
1417 address, or to determine the outcome of a conditional branch, the V
1418 bits for those values are checked, and an error emitted if any of
1419 them are undefined.</para>
njn3e986b22004-11-30 10:43:45 +00001420 </listitem>
1421
1422 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001423 <para>When values in CPU registers are used for any other purpose,
sewardj08e31e22007-05-23 21:58:33 +00001424 Memcheck computes the V bits for the result, but does not check
de03e0e7c2005-12-03 23:02:33 +00001425 them.</para>
njn3e986b22004-11-30 10:43:45 +00001426 </listitem>
1427
1428 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001429 <para>Once the V bits for a value in the CPU have been checked, they
de03e0e7c2005-12-03 23:02:33 +00001430 are then set to indicate validity. This avoids long chains of
1431 errors.</para>
njn3e986b22004-11-30 10:43:45 +00001432 </listitem>
1433
1434 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001435 <para>When values are loaded from memory, Memcheck checks the A bits
de03e0e7c2005-12-03 23:02:33 +00001436 for that location and issues an illegal-address warning if needed.
1437 In that case, the V bits loaded are forced to indicate Valid,
1438 despite the location being invalid.</para>
1439
1440 <para>This apparently strange choice reduces the amount of confusing
1441 information presented to the user. It avoids the unpleasant
1442 phenomenon in which memory is read from a place which is both
sewardj33878892007-11-17 09:43:25 +00001443 unaddressable and contains invalid values, and, as a result, you get
de03e0e7c2005-12-03 23:02:33 +00001444 not only an invalid-address (read/write) error, but also a
1445 potentially large set of uninitialised-value errors, one for every
1446 time the value is used.</para>
1447
1448 <para>There is a hazy boundary case to do with multi-byte loads from
1449 addresses which are partially valid and partially invalid. See
njna3311642009-08-10 01:29:14 +00001450 details of the option <option>--partial-loads-ok</option> for details.
de03e0e7c2005-12-03 23:02:33 +00001451 </para>
njn3e986b22004-11-30 10:43:45 +00001452 </listitem>
1453
1454</itemizedlist>
1455
1456
bartaf25f672009-06-26 19:03:53 +00001457<para>Memcheck intercepts calls to <function>malloc</function>,
1458<function>calloc</function>, <function>realloc</function>,
1459<function>valloc</function>, <function>memalign</function>,
1460<function>free</function>, <computeroutput>new</computeroutput>,
1461<computeroutput>new[]</computeroutput>,
1462<computeroutput>delete</computeroutput> and
1463<computeroutput>delete[]</computeroutput>. The behaviour you get
njn3e986b22004-11-30 10:43:45 +00001464is:</para>
1465
1466<itemizedlist>
1467
1468 <listitem>
bartaf25f672009-06-26 19:03:53 +00001469 <para><function>malloc</function>/<function>new</function>/<computeroutput>new[]</computeroutput>:
1470 the returned memory is marked as addressable but not having valid
1471 values. This means you have to write to it before you can read
1472 it.</para>
njn3e986b22004-11-30 10:43:45 +00001473 </listitem>
1474
1475 <listitem>
bartaf25f672009-06-26 19:03:53 +00001476 <para><function>calloc</function>: returned memory is marked both
1477 addressable and valid, since <function>calloc</function> clears
1478 the area to zero.</para>
njn3e986b22004-11-30 10:43:45 +00001479 </listitem>
1480
1481 <listitem>
bartaf25f672009-06-26 19:03:53 +00001482 <para><function>realloc</function>: if the new size is larger than
1483 the old, the new section is addressable but invalid, as with
njn2f7eebe2009-08-05 06:34:27 +00001484 <function>malloc</function>. If the new size is smaller, the
1485 dropped-off section is marked as unaddressable. You may only pass to
bartaf25f672009-06-26 19:03:53 +00001486 <function>realloc</function> a pointer previously issued to you by
1487 <function>malloc</function>/<function>calloc</function>/<function>realloc</function>.</para>
njn3e986b22004-11-30 10:43:45 +00001488 </listitem>
1489
1490 <listitem>
bartaf25f672009-06-26 19:03:53 +00001491 <para><function>free</function>/<computeroutput>delete</computeroutput>/<computeroutput>delete[]</computeroutput>:
1492 you may only pass to these functions a pointer previously issued
1493 to you by the corresponding allocation function. Otherwise,
1494 Memcheck complains. If the pointer is indeed valid, Memcheck
1495 marks the entire area it points at as unaddressable, and places
1496 the block in the freed-blocks-queue. The aim is to defer as long
1497 as possible reallocation of this block. Until that happens, all
1498 attempts to access it will elicit an invalid-address error, as you
1499 would hope.</para>
njn3e986b22004-11-30 10:43:45 +00001500 </listitem>
1501
1502</itemizedlist>
1503
1504</sect2>
1505</sect1>
1506
sewardj3b290482011-05-06 21:02:55 +00001507<sect1 id="mc-manual.monitor-commands" xreflabel="Memcheck Monitor Commands">
1508<title>Memcheck Monitor Commands</title>
sewardjc8bd1df2011-06-26 12:41:33 +00001509<para>The Memcheck tool provides monitor commands handled by Valgrind's
1510built-in gdbserver (see <xref linkend="manual-core-adv.gdbserver-commandhandling"/>).
sewardj3b290482011-05-06 21:02:55 +00001511</para>
njn3e986b22004-11-30 10:43:45 +00001512
sewardj3b290482011-05-06 21:02:55 +00001513<itemizedlist>
1514 <listitem>
sewardj30b3eca2011-06-28 08:20:39 +00001515 <para><varname>get_vbits &lt;addr&gt; [&lt;len&gt;]</varname>
sewardjc8bd1df2011-06-26 12:41:33 +00001516 shows the definedness (V) bits for &lt;len&gt; (default 1) bytes
1517 starting at &lt;addr&gt;. The definedness of each byte in the
1518 range is given using two hexadecimal digits. These hexadecimal
1519 digits encode the validity of each bit of the corresponding byte,
1520 using 0 if the bit is defined and 1 if the bit is undefined.
1521 If a byte is not addressable, its validity bits are replaced
1522 by <varname>__</varname> (a double underscore).
1523 </para>
1524 <para>
1525 In the following example, <varname>string10</varname> is an array
1526 of 10 characters, in which the even numbered bytes are
1527 undefined. In the below example, the byte corresponding
1528 to <varname>string10[5]</varname> is not addressable.
1529 </para>
sewardj3b290482011-05-06 21:02:55 +00001530<programlisting><![CDATA[
1531(gdb) p &string10
1532$4 = (char (*)[10]) 0x8049e28
sewardj30b3eca2011-06-28 08:20:39 +00001533(gdb) monitor get_vbits 0x8049e28 10
sewardj3b290482011-05-06 21:02:55 +00001534ff00ff00 ff__ff00 ff00
1535(gdb)
1536]]></programlisting>
sewardj30b3eca2011-06-28 08:20:39 +00001537
1538 <para> The command get_vbits cannot be used with registers. To get
1539 the validity bits of a register, you must start Valgrind with the
1540 option <option>--vgdb-shadow-registers=yes</option>. The validity
1541 bits of a register can be obtained by printing the 'shadow 1'
1542 corresponding register. In the below x86 example, the register
1543 eax has all its bits undefined, while the register ebx is fully
1544 defined.
1545 </para>
1546<programlisting><![CDATA[
1547(gdb) p /x $eaxs1
1548$9 = 0xffffffff
1549(gdb) p /x $ebxs1
1550$10 = 0x0
1551(gdb)
1552]]></programlisting>
1553
sewardj3b290482011-05-06 21:02:55 +00001554 </listitem>
1555
1556 <listitem>
sewardj30b3eca2011-06-28 08:20:39 +00001557 <para><varname>make_memory
1558 [noaccess|undefined|defined|Definedifaddressable] &lt;addr&gt;
sewardjc8bd1df2011-06-26 12:41:33 +00001559 [&lt;len&gt;]</varname> marks the range of &lt;len&gt; (default 1)
1560 bytes at &lt;addr&gt; as having the given status. Parameter
1561 <varname>noaccess</varname> marks the range as non-accessible, so
1562 Memcheck will report an error on any access to it.
1563 <varname>undefined</varname> or <varname>defined</varname> mark
1564 the area as accessible, but Memcheck regards the bytes in it
1565 respectively as having undefined or defined values.
sewardj30b3eca2011-06-28 08:20:39 +00001566 <varname>Definedifaddressable</varname> marks as defined, bytes in
sewardjc8bd1df2011-06-26 12:41:33 +00001567 the range which are already addressible, but makes no change to
sewardj30b3eca2011-06-28 08:20:39 +00001568 the status of bytes in the range which are not addressible. Note
1569 that the first letter of <varname>Definedifaddressable</varname>
1570 is an uppercase D to avoid confusion with <varname>defined</varname>.
1571 </para>
sewardjc8bd1df2011-06-26 12:41:33 +00001572
1573 <para>
1574 In the following example, the first byte of the
1575 <varname>string10</varname> is marked as defined:
sewardj3b290482011-05-06 21:02:55 +00001576 </para>
1577<programlisting><![CDATA[
sewardj30b3eca2011-06-28 08:20:39 +00001578(gdb) monitor make_memory defined 0x8049e28 1
1579(gdb) monitor get_vbits 0x8049e28 10
sewardj3b290482011-05-06 21:02:55 +000015800000ff00 ff00ff00 ff00
1581(gdb)
1582]]></programlisting>
1583 </listitem>
1584
1585 <listitem>
sewardj30b3eca2011-06-28 08:20:39 +00001586 <para><varname>check_memory [addressable|defined] &lt;addr&gt;
sewardj3b290482011-05-06 21:02:55 +00001587 [&lt;len&gt;]</varname> checks that the range of &lt;len&gt;
sewardj30b3eca2011-06-28 08:20:39 +00001588 (default 1) bytes at &lt;addr&gt; has the specified accessibility.
1589 It then outputs a description of &lt;addr&gt;. In the following
1590 example, a detailed description is available because the
philippea22f59d2012-01-26 23:13:52 +00001591 option <option>--read-var-info=yes</option> was given at Valgrind
sewardj30b3eca2011-06-28 08:20:39 +00001592 startup:
sewardj3b290482011-05-06 21:02:55 +00001593 </para>
1594<programlisting><![CDATA[
sewardj30b3eca2011-06-28 08:20:39 +00001595(gdb) monitor check_memory defined 0x8049e28 1
sewardj3b290482011-05-06 21:02:55 +00001596Address 0x8049E28 len 1 defined
1597==14698== Location 0x8049e28 is 0 bytes inside string10[0],
1598==14698== declared at prog.c:10, in frame #0 of thread 1
1599(gdb)
1600]]></programlisting>
1601 </listitem>
1602
1603 <listitem>
sewardj30b3eca2011-06-28 08:20:39 +00001604 <para><varname>leak_check [full*|summary]
philippe2193a7c2012-12-08 17:54:16 +00001605 [kinds &lt;set&gt;|reachable|possibleleak*|definiteleak]
sewardj30b3eca2011-06-28 08:20:39 +00001606 [increased*|changed|any]
philippe84234902012-01-14 13:53:13 +00001607 [unlimited*|limited &lt;max_loss_records_output&gt;]
sewardjc8bd1df2011-06-26 12:41:33 +00001608 </varname>
1609 performs a leak check. The <varname>*</varname> in the arguments
philippe84234902012-01-14 13:53:13 +00001610 indicates the default values. </para>
sewardj3b290482011-05-06 21:02:55 +00001611
sewardjc8bd1df2011-06-26 12:41:33 +00001612 <para> If the first argument is <varname>summary</varname>, only a
1613 summary of the leak search is given; otherwise a full leak report
1614 is produced. A full leak report gives detailed information for
1615 each leak: the stack trace where the leaked blocks were allocated,
1616 the number of blocks leaked and their total size. When a full
1617 report is requested, the next two arguments further specify what
1618 kind of leaks to report. A leak's details are shown if they match
philippe84234902012-01-14 13:53:13 +00001619 both the second and third argument. A full leak report might
1620 output detailed information for many leaks. The nr of leaks for
1621 which information is output can be controlled using
1622 the <varname>limited</varname> argument followed by the maximum nr
1623 of leak records to output. If this maximum is reached, the leak
1624 search outputs the records with the biggest number of bytes.
sewardj3b290482011-05-06 21:02:55 +00001625 </para>
1626
sewardjc8bd1df2011-06-26 12:41:33 +00001627 <para>The second argument controls what kind of blocks are shown for
philippe2193a7c2012-12-08 17:54:16 +00001628 a <varname>full</varname> leak search. The set of leak kinds to show
1629 can be specified using a <varname>&lt;set&gt;</varname> similarly
1630 to the command line option <option>--show-leak-kinds</option>.
1631 Alternatively, the value <varname>definiteleak</varname>
1632 is equivalent to <varname>kinds definite</varname>, the
1633 value <varname>possibleleak</varname> is equivalent to
1634 <varname>kinds definite,possible</varname> : it will also show
1635 possibly leaked blocks, .i.e those for which only an interior
1636 pointer was found. The value <varname>reachable</varname> will
1637 show all block categories (i.e. is equivalent to <varname>kinds
1638 all</varname>).
sewardj3b290482011-05-06 21:02:55 +00001639 </para>
sewardjc8bd1df2011-06-26 12:41:33 +00001640
1641 <para>The third argument controls what kinds of changes are shown
1642 for a <varname>full</varname> leak search. The
1643 value <varname>increased</varname> specifies that only block
1644 allocation stacks with an increased number of leaked bytes or
1645 blocks since the previous leak check should be shown. The
1646 value <varname>changed</varname> specifies that allocation stacks
1647 with any change since the previous leak check should be shown.
1648 The value <varname>any</varname> specifies that all leak entries
1649 should be shown, regardless of any increase or decrease. When
1650 If <varname>increased</varname> or <varname>changed</varname> are
1651 specified, the leak report entries will show the delta relative to
1652 the previous leak report.
1653 </para>
1654
1655 <para>The following example shows usage of the
philippe84234902012-01-14 13:53:13 +00001656 <varname>leak_check</varname> monitor command on
sewardjc8bd1df2011-06-26 12:41:33 +00001657 the <varname>memcheck/tests/leak-cases.c</varname> regression
1658 test. The first command outputs one entry having an increase in
1659 the leaked bytes. The second command is the same as the first
1660 command, but uses the abbreviated forms accepted by GDB and the
1661 Valgrind gdbserver. It only outputs the summary information, as
1662 there was no increase since the previous leak search.</para>
sewardj3b290482011-05-06 21:02:55 +00001663<programlisting><![CDATA[
sewardj30b3eca2011-06-28 08:20:39 +00001664(gdb) monitor leak_check full possibleleak increased
philippea22f59d2012-01-26 23:13:52 +00001665==19520== 16 (+16) bytes in 1 (+1) blocks are possibly lost in loss record 9 of 12
1666==19520== at 0x40070B4: malloc (vg_replace_malloc.c:263)
1667==19520== by 0x80484D5: mk (leak-cases.c:52)
1668==19520== by 0x804855F: f (leak-cases.c:81)
1669==19520== by 0x80488E0: main (leak-cases.c:107)
1670==19520==
1671==19520== LEAK SUMMARY:
1672==19520== definitely lost: 32 (+0) bytes in 2 (+0) blocks
1673==19520== indirectly lost: 16 (+0) bytes in 1 (+0) blocks
1674==19520== possibly lost: 32 (+16) bytes in 2 (+1) blocks
1675==19520== still reachable: 96 (+16) bytes in 6 (+1) blocks
1676==19520== suppressed: 0 (+0) bytes in 0 (+0) blocks
1677==19520== Reachable blocks (those to which a pointer was found) are not shown.
1678==19520== To see them, add 'reachable any' args to leak_check
1679==19520==
sewardj30b3eca2011-06-28 08:20:39 +00001680(gdb) mo l
philippea22f59d2012-01-26 23:13:52 +00001681==19520== LEAK SUMMARY:
1682==19520== definitely lost: 32 (+0) bytes in 2 (+0) blocks
1683==19520== indirectly lost: 16 (+0) bytes in 1 (+0) blocks
1684==19520== possibly lost: 32 (+0) bytes in 2 (+0) blocks
1685==19520== still reachable: 96 (+0) bytes in 6 (+0) blocks
1686==19520== suppressed: 0 (+0) bytes in 0 (+0) blocks
1687==19520== Reachable blocks (those to which a pointer was found) are not shown.
1688==19520== To see them, add 'reachable any' args to leak_check
1689==19520==
sewardj3b290482011-05-06 21:02:55 +00001690(gdb)
1691]]></programlisting>
sewardjc8bd1df2011-06-26 12:41:33 +00001692 <para>Note that when using Valgrind's gdbserver, it is not
1693 necessary to rerun
1694 with <option>--leak-check=full</option>
1695 <option>--show-reachable=yes</option> to see the reachable
1696 blocks. You can obtain the same information without rerunning by
sewardj30b3eca2011-06-28 08:20:39 +00001697 using the GDB command <computeroutput>monitor leak_check full
sewardjc8bd1df2011-06-26 12:41:33 +00001698 reachable any</computeroutput> (or, using
sewardj30b3eca2011-06-28 08:20:39 +00001699 abbreviation: <computeroutput>mo l f r a</computeroutput>).
sewardj3b290482011-05-06 21:02:55 +00001700 </para>
1701 </listitem>
philippe84234902012-01-14 13:53:13 +00001702
philippea22f59d2012-01-26 23:13:52 +00001703 <listitem>
1704 <para><varname>block_list &lt;loss_record_nr&gt; </varname>
1705 shows the list of blocks belonging to &lt;loss_record_nr&gt;.
1706 </para>
1707
1708 <para> A leak search merges the allocated blocks in loss records :
1709 a loss record re-groups all blocks having the same state (for
1710 example, Definitely Lost) and the same allocation backtrace.
1711 Each loss record is identified in the leak search result
1712 by a loss record number.
1713 The <varname>block_list</varname> command shows the loss record information
1714 followed by the addresses and sizes of the blocks which have been
1715 merged in the loss record.
1716 </para>
1717
1718 <para> If a directly lost block causes some other blocks to be indirectly
1719 lost, the block_list command will also show these indirectly lost blocks.
1720 The indirectly lost blocks will be indented according to the level of indirection
1721 between the directly lost block and the indirectly lost block(s).
1722 Each indirectly lost block is followed by the reference of its loss record.
1723 </para>
1724
1725 <para> The block_list command can be used on the results of a leak search as long
1726 as no block has been freed after this leak search: as soon as the program frees
1727 a block, a new leak search is needed before block_list can be used again.
1728 </para>
1729
1730 <para>
1731 In the below example, the program leaks a tree structure by losing the pointer to
1732 the block A (top of the tree).
1733 So, the block A is directly lost, causing an indirect
1734 loss of blocks B to G. The first block_list command shows the loss record of A
1735 (a definitely lost block with address 0x4028028, size 16). The addresses and sizes
1736 of the indirectly lost blocks due to block A are shown below the block A.
1737 The second command shows the details of one of the indirect loss records output
1738 by the first command.
1739 </para>
1740<programlisting><![CDATA[
1741 A
1742 / \
1743 B C
1744 / \ / \
1745 D E F G
1746]]></programlisting>
1747
1748<programlisting><![CDATA[
1749(gdb) bt
1750#0 main () at leak-tree.c:69
1751(gdb) monitor leak_check full any
1752==19552== 112 (16 direct, 96 indirect) bytes in 1 blocks are definitely lost in loss record 7 of 7
1753==19552== at 0x40070B4: malloc (vg_replace_malloc.c:263)
1754==19552== by 0x80484D5: mk (leak-tree.c:28)
1755==19552== by 0x80484FC: f (leak-tree.c:41)
1756==19552== by 0x8048856: main (leak-tree.c:63)
1757==19552==
1758==19552== LEAK SUMMARY:
1759==19552== definitely lost: 16 bytes in 1 blocks
1760==19552== indirectly lost: 96 bytes in 6 blocks
1761==19552== possibly lost: 0 bytes in 0 blocks
1762==19552== still reachable: 0 bytes in 0 blocks
1763==19552== suppressed: 0 bytes in 0 blocks
1764==19552==
1765(gdb) monitor block_list 7
1766==19552== 112 (16 direct, 96 indirect) bytes in 1 blocks are definitely lost in loss record 7 of 7
1767==19552== at 0x40070B4: malloc (vg_replace_malloc.c:263)
1768==19552== by 0x80484D5: mk (leak-tree.c:28)
1769==19552== by 0x80484FC: f (leak-tree.c:41)
1770==19552== by 0x8048856: main (leak-tree.c:63)
1771==19552== 0x4028028[16]
1772==19552== 0x4028068[16] indirect loss record 1
1773==19552== 0x40280E8[16] indirect loss record 3
1774==19552== 0x4028128[16] indirect loss record 4
1775==19552== 0x40280A8[16] indirect loss record 2
1776==19552== 0x4028168[16] indirect loss record 5
1777==19552== 0x40281A8[16] indirect loss record 6
1778(gdb) mo b 2
1779==19552== 16 bytes in 1 blocks are indirectly lost in loss record 2 of 7
1780==19552== at 0x40070B4: malloc (vg_replace_malloc.c:263)
1781==19552== by 0x80484D5: mk (leak-tree.c:28)
1782==19552== by 0x8048519: f (leak-tree.c:43)
1783==19552== by 0x8048856: main (leak-tree.c:63)
1784==19552== 0x40280A8[16]
1785==19552== 0x4028168[16] indirect loss record 5
1786==19552== 0x40281A8[16] indirect loss record 6
1787(gdb)
1788
1789]]></programlisting>
1790
1791 </listitem>
1792
1793 <listitem>
1794 <para><varname>who_points_at &lt;addr&gt; [&lt;len&gt;]</varname>
1795 shows all the locations where a pointer to addr is found.
1796 If len is equal to 1, the command only shows the locations pointing
1797 exactly at addr (i.e. the "start pointers" to addr).
1798 If len is &gt; 1, "interior pointers" pointing at the len first bytes
1799 will also be shown.
1800 </para>
1801
1802 <para>The locations searched for are the same as the locations
1803 used in the leak search. So, <varname>who_points_at</varname> can a.o.
1804 be used to show why the leak search still can reach a block, or can
1805 search for dangling pointers to a freed block.
1806 Each location pointing at addr (or pointing inside addr if interior pointers
1807 are being searched for) will be described.
1808 </para>
1809
1810 <para>In the below example, the pointers to the 'tree block A' (see example
1811 in command <varname>block_list</varname>) is shown before the tree was leaked.
1812 The descriptions are detailed as the option <option>--read-var-info=yes</option>
1813 was given at Valgrind startup. The second call shows the pointers (start and interior
1814 pointers) to block G. The block G (0x40281A8) is reachable via block C (0x40280a8)
1815 and register ECX of tid 1 (tid is the Valgrind thread id).
1816 It is "interior reachable" via the register EBX.
1817 </para>
1818
1819<programlisting><![CDATA[
1820(gdb) monitor who_points_at 0x4028028
1821==20852== Searching for pointers to 0x4028028
1822==20852== *0x8049e20 points at 0x4028028
1823==20852== Location 0x8049e20 is 0 bytes inside global var "t"
1824==20852== declared at leak-tree.c:35
1825(gdb) monitor who_points_at 0x40281A8 16
1826==20852== Searching for pointers pointing in 16 bytes from 0x40281a8
1827==20852== *0x40280ac points at 0x40281a8
1828==20852== Address 0x40280ac is 4 bytes inside a block of size 16 alloc'd
1829==20852== at 0x40070B4: malloc (vg_replace_malloc.c:263)
1830==20852== by 0x80484D5: mk (leak-tree.c:28)
1831==20852== by 0x8048519: f (leak-tree.c:43)
1832==20852== by 0x8048856: main (leak-tree.c:63)
1833==20852== tid 1 register ECX points at 0x40281a8
1834==20852== tid 1 register EBX interior points at 2 bytes inside 0x40281a8
1835(gdb)
1836]]></programlisting>
philippea22f59d2012-01-26 23:13:52 +00001837
philippeab1fce92013-09-29 13:47:32 +00001838 <para> When <varname>who_points_at</varname> finds an interior pointer,
1839 it will report the heuristic(s) with which this interior pointer
1840 will be considered as reachable. Note that this is done independently
1841 of the value of the option <option>--leak-check-heuristics</option>.
1842 In the below example, the loss record 6 indicates a possibly lost
1843 block. <varname>who_points_at</varname> reports that there is an interior
1844 pointer pointing in this block, and that the block can be considered
1845 reachable using the heuristic
1846 <computeroutput>multipleinheritance</computeroutput>.
1847 </para>
1848
1849<programlisting><![CDATA[
1850(gdb) monitor block_list 6
1851==3748== 8 bytes in 1 blocks are possibly lost in loss record 6 of 7
1852==3748== at 0x4007D77: operator new(unsigned int) (vg_replace_malloc.c:313)
1853==3748== by 0x8048954: main (leak_cpp_interior.cpp:43)
1854==3748== 0x402A0E0[8]
1855(gdb) monitor who_points_at 0x402A0E0 8
1856==3748== Searching for pointers pointing in 8 bytes from 0x402a0e0
1857==3748== *0xbe8ee078 interior points at 4 bytes inside 0x402a0e0
1858==3748== Address 0xbe8ee078 is on thread 1's stack
1859==3748== block at 0x402a0e0 considered reachable by ptr 0x402a0e4 using multipleinheritance heuristic
1860(gdb)
1861]]></programlisting>
1862
1863 </listitem>
philippea22f59d2012-01-26 23:13:52 +00001864
sewardj3b290482011-05-06 21:02:55 +00001865</itemizedlist>
1866
1867</sect1>
njn3e986b22004-11-30 10:43:45 +00001868
njn3e986b22004-11-30 10:43:45 +00001869<sect1 id="mc-manual.clientreqs" xreflabel="Client requests">
1870<title>Client Requests</title>
1871
1872<para>The following client requests are defined in
njn1d0825f2006-03-27 11:37:07 +00001873<filename>memcheck.h</filename>.
njn3e986b22004-11-30 10:43:45 +00001874See <filename>memcheck.h</filename> for exact details of their
1875arguments.</para>
1876
1877<itemizedlist>
1878
1879 <listitem>
njndbf7ca72006-03-31 11:57:59 +00001880 <para><varname>VALGRIND_MAKE_MEM_NOACCESS</varname>,
1881 <varname>VALGRIND_MAKE_MEM_UNDEFINED</varname> and
1882 <varname>VALGRIND_MAKE_MEM_DEFINED</varname>.
njn3e986b22004-11-30 10:43:45 +00001883 These mark address ranges as completely inaccessible,
1884 accessible but containing undefined data, and accessible and
sewardje7decf82011-01-22 11:21:58 +00001885 containing defined data, respectively.</para>
njn3e986b22004-11-30 10:43:45 +00001886 </listitem>
1887
1888 <listitem>
njndbf7ca72006-03-31 11:57:59 +00001889 <para><varname>VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE</varname>.
1890 This is just like <varname>VALGRIND_MAKE_MEM_DEFINED</varname> but only
1891 affects those bytes that are already addressable.</para>
1892 </listitem>
1893
1894 <listitem>
njndbf7ca72006-03-31 11:57:59 +00001895 <para><varname>VALGRIND_CHECK_MEM_IS_ADDRESSABLE</varname> and
1896 <varname>VALGRIND_CHECK_MEM_IS_DEFINED</varname>: check immediately
de03e0e7c2005-12-03 23:02:33 +00001897 whether or not the given address range has the relevant property,
1898 and if not, print an error message. Also, for the convenience of
1899 the client, returns zero if the relevant property holds; otherwise,
1900 the returned value is the address of the first byte for which the
1901 property is not true. Always returns 0 when not run on
1902 Valgrind.</para>
njn3e986b22004-11-30 10:43:45 +00001903 </listitem>
1904
1905 <listitem>
njndbf7ca72006-03-31 11:57:59 +00001906 <para><varname>VALGRIND_CHECK_VALUE_IS_DEFINED</varname>: a quick and easy
1907 way to find out whether Valgrind thinks a particular value
1908 (lvalue, to be precise) is addressable and defined. Prints an error
njn8225cc02009-03-09 22:52:24 +00001909 message if not. It has no return value.</para>
njn3e986b22004-11-30 10:43:45 +00001910 </listitem>
1911
1912 <listitem>
njn8225cc02009-03-09 22:52:24 +00001913 <para><varname>VALGRIND_DO_LEAK_CHECK</varname>: does a full memory leak
njn2f7eebe2009-08-05 06:34:27 +00001914 check (like <option>--leak-check=full</option>) right now.
njn8225cc02009-03-09 22:52:24 +00001915 This is useful for incrementally checking for leaks between arbitrary
1916 places in the program's execution. It has no return value.</para>
1917 </listitem>
1918
1919 <listitem>
sewardjc8bd1df2011-06-26 12:41:33 +00001920 <para><varname>VALGRIND_DO_ADDED_LEAK_CHECK</varname>: same as
1921 <varname> VALGRIND_DO_LEAK_CHECK</varname> but only shows the
1922 entries for which there was an increase in leaked bytes or leaked
1923 number of blocks since the previous leak search. It has no return
1924 value.</para>
1925 </listitem>
1926
1927 <listitem>
1928 <para><varname>VALGRIND_DO_CHANGED_LEAK_CHECK</varname>: same as
1929 <varname>VALGRIND_DO_LEAK_CHECK</varname> but only shows the
1930 entries for which there was an increase or decrease in leaked
1931 bytes or leaked number of blocks since the previous leak search. It
1932 has no return value.</para>
1933 </listitem>
1934
1935 <listitem>
njn8225cc02009-03-09 22:52:24 +00001936 <para><varname>VALGRIND_DO_QUICK_LEAK_CHECK</varname>: like
1937 <varname>VALGRIND_DO_LEAK_CHECK</varname>, except it produces only a leak
njn7e5d4ed2009-07-30 02:57:52 +00001938 summary (like <option>--leak-check=summary</option>).
njn8225cc02009-03-09 22:52:24 +00001939 It has no return value.</para>
njn3e986b22004-11-30 10:43:45 +00001940 </listitem>
1941
1942 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001943 <para><varname>VALGRIND_COUNT_LEAKS</varname>: fills in the four
1944 arguments with the number of bytes of memory found by the previous
njn8225cc02009-03-09 22:52:24 +00001945 leak check to be leaked (i.e. the sum of direct leaks and indirect leaks),
njn2f7eebe2009-08-05 06:34:27 +00001946 dubious, reachable and suppressed. This is useful in test harness code,
njn8225cc02009-03-09 22:52:24 +00001947 after calling <varname>VALGRIND_DO_LEAK_CHECK</varname> or
1948 <varname>VALGRIND_DO_QUICK_LEAK_CHECK</varname>.</para>
njn3e986b22004-11-30 10:43:45 +00001949 </listitem>
1950
1951 <listitem>
njn8df80b22009-03-02 05:11:06 +00001952 <para><varname>VALGRIND_COUNT_LEAK_BLOCKS</varname>: identical to
1953 <varname>VALGRIND_COUNT_LEAKS</varname> except that it returns the
1954 number of blocks rather than the number of bytes in each
1955 category.</para>
1956 </listitem>
1957
1958 <listitem>
de03e0e7c2005-12-03 23:02:33 +00001959 <para><varname>VALGRIND_GET_VBITS</varname> and
1960 <varname>VALGRIND_SET_VBITS</varname>: allow you to get and set the
1961 V (validity) bits for an address range. You should probably only
1962 set V bits that you have got with
1963 <varname>VALGRIND_GET_VBITS</varname>. Only for those who really
njn1d0825f2006-03-27 11:37:07 +00001964 know what they are doing.</para>
njn3e986b22004-11-30 10:43:45 +00001965 </listitem>
1966
sewardje7decf82011-01-22 11:21:58 +00001967 <listitem>
1968 <para><varname>VALGRIND_CREATE_BLOCK</varname> and
1969 <varname>VALGRIND_DISCARD</varname>. <varname>VALGRIND_CREATE_BLOCK</varname>
1970 takes an address, a number of bytes and a character string. The
1971 specified address range is then associated with that string. When
1972 Memcheck reports an invalid access to an address in the range, it
1973 will describe it in terms of this block rather than in terms of
1974 any other block it knows about. Note that the use of this macro
1975 does not actually change the state of memory in any way -- it
1976 merely gives a name for the range.
1977 </para>
1978
1979 <para>At some point you may want Memcheck to stop reporting errors
1980 in terms of the block named
1981 by <varname>VALGRIND_CREATE_BLOCK</varname>. To make this
1982 possible, <varname>VALGRIND_CREATE_BLOCK</varname> returns a
1983 "block handle", which is a C <varname>int</varname> value. You
1984 can pass this block handle to <varname>VALGRIND_DISCARD</varname>.
1985 After doing so, Valgrind will no longer relate addressing errors
1986 in the specified range to the block. Passing invalid handles to
1987 <varname>VALGRIND_DISCARD</varname> is harmless.
1988 </para>
1989 </listitem>
1990
njn3e986b22004-11-30 10:43:45 +00001991</itemizedlist>
1992
1993</sect1>
sewardjce10c262006-10-05 17:56:14 +00001994
1995
1996
1997
njn09f2e6c2009-08-10 04:07:54 +00001998<sect1 id="mc-manual.mempools" xreflabel="Memory Pools">
sewardjce10c262006-10-05 17:56:14 +00001999<title>Memory Pools: describing and working with custom allocators</title>
2000
2001<para>Some programs use custom memory allocators, often for performance
njna3311642009-08-10 01:29:14 +00002002reasons. Left to itself, Memcheck is unable to understand the
2003behaviour of custom allocation schemes as well as it understands the
2004standard allocators, and so may miss errors and leaks in your program. What
2005this section describes is a way to give Memcheck enough of a description of
2006your custom allocator that it can make at least some sense of what is
2007happening.</para>
sewardjae0e07b2006-10-06 11:47:01 +00002008
2009<para>There are many different sorts of custom allocator, so Memcheck
sewardjce10c262006-10-05 17:56:14 +00002010attempts to reason about them using a loose, abstract model. We
2011use the following terminology when describing custom allocation
2012systems:</para>
2013
2014<itemizedlist>
2015 <listitem>
2016 <para>Custom allocation involves a set of independent "memory pools".
2017 </para>
2018 </listitem>
2019 <listitem>
2020 <para>Memcheck's notion of a a memory pool consists of a single "anchor
2021 address" and a set of non-overlapping "chunks" associated with the
2022 anchor address.</para>
2023 </listitem>
2024 <listitem>
2025 <para>Typically a pool's anchor address is the address of a
2026 book-keeping "header" structure.</para>
2027 </listitem>
2028 <listitem>
2029 <para>Typically the pool's chunks are drawn from a contiguous
bartaf25f672009-06-26 19:03:53 +00002030 "superblock" acquired through the system
njn2f7eebe2009-08-05 06:34:27 +00002031 <function>malloc</function> or
2032 <function>mmap</function>.</para>
sewardjce10c262006-10-05 17:56:14 +00002033 </listitem>
2034
2035</itemizedlist>
2036
2037<para>Keep in mind that the last two points above say "typically": the
2038Valgrind mempool client request API is intentionally vague about the
2039exact structure of a mempool. There is no specific mention made of
2040headers or superblocks. Nevertheless, the following picture may help
2041elucidate the intention of the terms in the API:</para>
2042
2043<programlisting><![CDATA[
2044 "pool"
2045 (anchor address)
2046 |
2047 v
2048 +--------+---+
2049 | header | o |
2050 +--------+-|-+
2051 |
2052 v superblock
2053 +------+---+--------------+---+------------------+
2054 | |rzB| allocation |rzB| |
2055 +------+---+--------------+---+------------------+
2056 ^ ^
2057 | |
2058 "addr" "addr"+"size"
2059]]></programlisting>
2060
2061<para>
2062Note that the header and the superblock may be contiguous or
2063discontiguous, and there may be multiple superblocks associated with a
2064single header; such variations are opaque to Memcheck. The API
2065only requires that your allocation scheme can present sensible values
2066of "pool", "addr" and "size".</para>
2067
2068<para>
2069Typically, before making client requests related to mempools, a client
2070program will have allocated such a header and superblock for their
2071mempool, and marked the superblock NOACCESS using the
2072<varname>VALGRIND_MAKE_MEM_NOACCESS</varname> client request.</para>
2073
2074<para>
2075When dealing with mempools, the goal is to maintain a particular
2076invariant condition: that Memcheck believes the unallocated portions
2077of the pool's superblock (including redzones) are NOACCESS. To
2078maintain this invariant, the client program must ensure that the
2079superblock starts out in that state; Memcheck cannot make it so, since
2080Memcheck never explicitly learns about the superblock of a pool, only
2081the allocated chunks within the pool.</para>
2082
2083<para>
2084Once the header and superblock for a pool are established and properly
2085marked, there are a number of client requests programs can use to
2086inform Memcheck about changes to the state of a mempool:</para>
2087
2088<itemizedlist>
2089
2090 <listitem>
2091 <para>
2092 <varname>VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed)</varname>:
njna3311642009-08-10 01:29:14 +00002093 This request registers the address <varname>pool</varname> as the anchor
2094 address for a memory pool. It also provides a size
2095 <varname>rzB</varname>, specifying how large the redzones placed around
2096 chunks allocated from the pool should be. Finally, it provides an
2097 <varname>is_zeroed</varname> argument that specifies whether the pool's
2098 chunks are zeroed (more precisely: defined) when allocated.
sewardjce10c262006-10-05 17:56:14 +00002099 </para>
2100 <para>
2101 Upon completion of this request, no chunks are associated with the
2102 pool. The request simply tells Memcheck that the pool exists, so that
2103 subsequent calls can refer to it as a pool.
2104 </para>
2105 </listitem>
2106
2107 <listitem>
2108 <para><varname>VALGRIND_DESTROY_MEMPOOL(pool)</varname>:
2109 This request tells Memcheck that a pool is being torn down. Memcheck
2110 then removes all records of chunks associated with the pool, as well
2111 as its record of the pool's existence. While destroying its records of
2112 a mempool, Memcheck resets the redzones of any live chunks in the pool
2113 to NOACCESS.
2114 </para>
2115 </listitem>
2116
2117 <listitem>
2118 <para><varname>VALGRIND_MEMPOOL_ALLOC(pool, addr, size)</varname>:
njna3311642009-08-10 01:29:14 +00002119 This request informs Memcheck that a <varname>size</varname>-byte chunk
2120 has been allocated at <varname>addr</varname>, and associates the chunk with the
2121 specified
2122 <varname>pool</varname>. If the pool was created with nonzero
2123 <varname>rzB</varname> redzones, Memcheck will mark the
2124 <varname>rzB</varname> bytes before and after the chunk as NOACCESS. If
2125 the pool was created with the <varname>is_zeroed</varname> argument set,
2126 Memcheck will mark the chunk as DEFINED, otherwise Memcheck will mark
2127 the chunk as UNDEFINED.
sewardjce10c262006-10-05 17:56:14 +00002128 </para>
2129 </listitem>
2130
2131 <listitem>
2132 <para><varname>VALGRIND_MEMPOOL_FREE(pool, addr)</varname>:
njna3311642009-08-10 01:29:14 +00002133 This request informs Memcheck that the chunk at <varname>addr</varname>
2134 should no longer be considered allocated. Memcheck will mark the chunk
2135 associated with <varname>addr</varname> as NOACCESS, and delete its
2136 record of the chunk's existence.
sewardjce10c262006-10-05 17:56:14 +00002137 </para>
2138 </listitem>
2139
2140 <listitem>
2141 <para><varname>VALGRIND_MEMPOOL_TRIM(pool, addr, size)</varname>:
njna3311642009-08-10 01:29:14 +00002142 This request trims the chunks associated with <varname>pool</varname>.
2143 The request only operates on chunks associated with
2144 <varname>pool</varname>. Trimming is formally defined as:</para>
sewardjce10c262006-10-05 17:56:14 +00002145 <itemizedlist>
2146 <listitem>
njna3311642009-08-10 01:29:14 +00002147 <para> All chunks entirely inside the range
2148 <varname>addr..(addr+size-1)</varname> are preserved.</para>
sewardjce10c262006-10-05 17:56:14 +00002149 </listitem>
2150 <listitem>
njna3311642009-08-10 01:29:14 +00002151 <para>All chunks entirely outside the range
2152 <varname>addr..(addr+size-1)</varname> are discarded, as though
2153 <varname>VALGRIND_MEMPOOL_FREE</varname> was called on them. </para>
sewardjce10c262006-10-05 17:56:14 +00002154 </listitem>
2155 <listitem>
2156 <para>All other chunks must intersect with the range
njna3311642009-08-10 01:29:14 +00002157 <varname>addr..(addr+size-1)</varname>; areas outside the
2158 intersection are marked as NOACCESS, as though they had been
2159 independently freed with
sewardjce10c262006-10-05 17:56:14 +00002160 <varname>VALGRIND_MEMPOOL_FREE</varname>.</para>
2161 </listitem>
2162 </itemizedlist>
2163 <para>This is a somewhat rare request, but can be useful in
2164 implementing the type of mass-free operations common in custom
2165 LIFO allocators.</para>
2166 </listitem>
2167
2168 <listitem>
bartaf25f672009-06-26 19:03:53 +00002169 <para><varname>VALGRIND_MOVE_MEMPOOL(poolA, poolB)</varname>: This
2170 request informs Memcheck that the pool previously anchored at
njna3311642009-08-10 01:29:14 +00002171 address <varname>poolA</varname> has moved to anchor address
2172 <varname>poolB</varname>. This is a rare request, typically only needed
2173 if you <function>realloc</function> the header of a mempool.</para>
sewardjce10c262006-10-05 17:56:14 +00002174 <para>No memory-status bits are altered by this request.</para>
2175 </listitem>
2176
2177 <listitem>
2178 <para>
bartaf25f672009-06-26 19:03:53 +00002179 <varname>VALGRIND_MEMPOOL_CHANGE(pool, addrA, addrB,
2180 size)</varname>: This request informs Memcheck that the chunk
njna3311642009-08-10 01:29:14 +00002181 previously allocated at address <varname>addrA</varname> within
2182 <varname>pool</varname> has been moved and/or resized, and should be
2183 changed to cover the region <varname>addrB..(addrB+size-1)</varname>. This
2184 is a rare request, typically only needed if you
2185 <function>realloc</function> a superblock or wish to extend a chunk
2186 without changing its memory-status bits.
sewardjce10c262006-10-05 17:56:14 +00002187 </para>
2188 <para>No memory-status bits are altered by this request.
2189 </para>
2190 </listitem>
2191
2192 <listitem>
2193 <para><varname>VALGRIND_MEMPOOL_EXISTS(pool)</varname>:
2194 This request informs the caller whether or not Memcheck is currently
njna3311642009-08-10 01:29:14 +00002195 tracking a mempool at anchor address <varname>pool</varname>. It
2196 evaluates to 1 when there is a mempool associated with that address, 0
2197 otherwise. This is a rare request, only useful in circumstances when
2198 client code might have lost track of the set of active mempools.
sewardjce10c262006-10-05 17:56:14 +00002199 </para>
2200 </listitem>
2201
2202</itemizedlist>
2203
sewardj778d7832007-11-22 01:21:56 +00002204</sect1>
2205
2206
2207
2208
2209
2210
2211
2212<sect1 id="mc-manual.mpiwrap" xreflabel="MPI Wrappers">
2213<title>Debugging MPI Parallel Programs with Valgrind</title>
2214
njn2f7eebe2009-08-05 06:34:27 +00002215<para>Memcheck supports debugging of distributed-memory applications
sewardj778d7832007-11-22 01:21:56 +00002216which use the MPI message passing standard. This support consists of a
2217library of wrapper functions for the
2218<computeroutput>PMPI_*</computeroutput> interface. When incorporated
2219into the application's address space, either by direct linking or by
2220<computeroutput>LD_PRELOAD</computeroutput>, the wrappers intercept
2221calls to <computeroutput>PMPI_Send</computeroutput>,
2222<computeroutput>PMPI_Recv</computeroutput>, etc. They then
njn2f7eebe2009-08-05 06:34:27 +00002223use client requests to inform Memcheck of memory state changes caused
sewardj778d7832007-11-22 01:21:56 +00002224by the function being wrapped. This reduces the number of false
2225positives that Memcheck otherwise typically reports for MPI
2226applications.</para>
2227
2228<para>The wrappers also take the opportunity to carefully check
2229size and definedness of buffers passed as arguments to MPI functions, hence
2230detecting errors such as passing undefined data to
2231<computeroutput>PMPI_Send</computeroutput>, or receiving data into a
2232buffer which is too small.</para>
2233
2234<para>Unlike most of the rest of Valgrind, the wrapper library is subject to a
2235BSD-style license, so you can link it into any code base you like.
njna437a602009-08-04 05:24:46 +00002236See the top of <computeroutput>mpi/libmpiwrap.c</computeroutput>
sewardj778d7832007-11-22 01:21:56 +00002237for license details.</para>
2238
2239
2240<sect2 id="mc-manual.mpiwrap.build" xreflabel="Building MPI Wrappers">
2241<title>Building and installing the wrappers</title>
2242
2243<para> The wrapper library will be built automatically if possible.
2244Valgrind's configure script will look for a suitable
2245<computeroutput>mpicc</computeroutput> to build it with. This must be
2246the same <computeroutput>mpicc</computeroutput> you use to build the
2247MPI application you want to debug. By default, Valgrind tries
2248<computeroutput>mpicc</computeroutput>, but you can specify a
njna3311642009-08-10 01:29:14 +00002249different one by using the configure-time option
njn7316df22009-08-04 01:16:01 +00002250<option>--with-mpicc</option>. Currently the
sewardj778d7832007-11-22 01:21:56 +00002251wrappers are only buildable with
2252<computeroutput>mpicc</computeroutput>s which are based on GNU
njn7316df22009-08-04 01:16:01 +00002253GCC or Intel's C++ Compiler.</para>
sewardj778d7832007-11-22 01:21:56 +00002254
2255<para>Check that the configure script prints a line like this:</para>
2256
2257<programlisting><![CDATA[
2258checking for usable MPI2-compliant mpicc and mpi.h... yes, mpicc
2259]]></programlisting>
2260
2261<para>If it says <computeroutput>... no</computeroutput>, your
2262<computeroutput>mpicc</computeroutput> has failed to compile and link
2263a test MPI2 program.</para>
2264
2265<para>If the configure test succeeds, continue in the usual way with
2266<computeroutput>make</computeroutput> and <computeroutput>make
2267install</computeroutput>. The final install tree should then contain
njn2f7eebe2009-08-05 06:34:27 +00002268<computeroutput>libmpiwrap-&lt;platform&gt;.so</computeroutput>.
sewardj778d7832007-11-22 01:21:56 +00002269</para>
2270
2271<para>Compile up a test MPI program (eg, MPI hello-world) and try
2272this:</para>
2273
2274<programlisting><![CDATA[
njn6bf365c2009-02-11 00:35:45 +00002275LD_PRELOAD=$prefix/lib/valgrind/libmpiwrap-<platform>.so \
sewardj778d7832007-11-22 01:21:56 +00002276 mpirun [args] $prefix/bin/valgrind ./hello
2277]]></programlisting>
2278
2279<para>You should see something similar to the following</para>
2280
2281<programlisting><![CDATA[
2282valgrind MPI wrappers 31901: Active for pid 31901
2283valgrind MPI wrappers 31901: Try MPIWRAP_DEBUG=help for possible options
2284]]></programlisting>
2285
2286<para>repeated for every process in the group. If you do not see
2287these, there is an build/installation problem of some kind.</para>
2288
2289<para> The MPI functions to be wrapped are assumed to be in an ELF
2290shared object with soname matching
2291<computeroutput>libmpi.so*</computeroutput>. This is known to be
2292correct at least for Open MPI and Quadrics MPI, and can easily be
2293changed if required.</para>
2294</sect2>
2295
2296
2297<sect2 id="mc-manual.mpiwrap.gettingstarted"
2298 xreflabel="Getting started with MPI Wrappers">
2299<title>Getting started</title>
2300
2301<para>Compile your MPI application as usual, taking care to link it
2302using the same <computeroutput>mpicc</computeroutput> that your
2303Valgrind build was configured with.</para>
2304
2305<para>
2306Use the following basic scheme to run your application on Valgrind with
2307the wrappers engaged:</para>
2308
2309<programlisting><![CDATA[
2310MPIWRAP_DEBUG=[wrapper-args] \
njn6bf365c2009-02-11 00:35:45 +00002311 LD_PRELOAD=$prefix/lib/valgrind/libmpiwrap-<platform>.so \
sewardj778d7832007-11-22 01:21:56 +00002312 mpirun [mpirun-args] \
2313 $prefix/bin/valgrind [valgrind-args] \
2314 [application] [app-args]
2315]]></programlisting>
2316
2317<para>As an alternative to
2318<computeroutput>LD_PRELOAD</computeroutput>ing
njn6bf365c2009-02-11 00:35:45 +00002319<computeroutput>libmpiwrap-&lt;platform&gt;.so</computeroutput>, you can
2320simply link it to your application if desired. This should not disturb
2321native behaviour of your application in any way.</para>
sewardj778d7832007-11-22 01:21:56 +00002322</sect2>
2323
2324
2325<sect2 id="mc-manual.mpiwrap.controlling"
2326 xreflabel="Controlling the MPI Wrappers">
2327<title>Controlling the wrapper library</title>
2328
2329<para>Environment variable
2330<computeroutput>MPIWRAP_DEBUG</computeroutput> is consulted at
2331startup. The default behaviour is to print a starting banner</para>
2332
2333<programlisting><![CDATA[
2334valgrind MPI wrappers 16386: Active for pid 16386
2335valgrind MPI wrappers 16386: Try MPIWRAP_DEBUG=help for possible options
2336]]></programlisting>
2337
2338<para> and then be relatively quiet.</para>
2339
2340<para>You can give a list of comma-separated options in
2341<computeroutput>MPIWRAP_DEBUG</computeroutput>. These are</para>
2342
2343<itemizedlist>
2344 <listitem>
2345 <para><computeroutput>verbose</computeroutput>:
2346 show entries/exits of all wrappers. Also show extra
2347 debugging info, such as the status of outstanding
2348 <computeroutput>MPI_Request</computeroutput>s resulting
2349 from uncompleted <computeroutput>MPI_Irecv</computeroutput>s.</para>
2350 </listitem>
2351 <listitem>
2352 <para><computeroutput>quiet</computeroutput>:
2353 opposite of <computeroutput>verbose</computeroutput>, only print
2354 anything when the wrappers want
2355 to report a detected programming error, or in case of catastrophic
2356 failure of the wrappers.</para>
2357 </listitem>
2358 <listitem>
2359 <para><computeroutput>warn</computeroutput>:
2360 by default, functions which lack proper wrappers
2361 are not commented on, just silently
2362 ignored. This causes a warning to be printed for each unwrapped
2363 function used, up to a maximum of three warnings per function.</para>
2364 </listitem>
2365 <listitem>
2366 <para><computeroutput>strict</computeroutput>:
2367 print an error message and abort the program if
2368 a function lacking a wrapper is used.</para>
2369 </listitem>
2370</itemizedlist>
2371
2372<para> If you want to use Valgrind's XML output facility
njn7e5d4ed2009-07-30 02:57:52 +00002373(<option>--xml=yes</option>), you should pass
sewardj778d7832007-11-22 01:21:56 +00002374<computeroutput>quiet</computeroutput> in
2375<computeroutput>MPIWRAP_DEBUG</computeroutput> so as to get rid of any
2376extraneous printing from the wrappers.</para>
2377
2378</sect2>
2379
2380
njn2f7eebe2009-08-05 06:34:27 +00002381<sect2 id="mc-manual.mpiwrap.limitations.functions"
2382 xreflabel="Functions: Abilities and Limitations">
sewardj778d7832007-11-22 01:21:56 +00002383<title>Functions</title>
2384
2385<para>All MPI2 functions except
2386<computeroutput>MPI_Wtick</computeroutput>,
2387<computeroutput>MPI_Wtime</computeroutput> and
2388<computeroutput>MPI_Pcontrol</computeroutput> have wrappers. The
2389first two are not wrapped because they return a
njn2f7eebe2009-08-05 06:34:27 +00002390<computeroutput>double</computeroutput>, which Valgrind's
2391function-wrap mechanism cannot handle (but it could easily be
2392extended to do so). <computeroutput>MPI_Pcontrol</computeroutput> cannot be
sewardj778d7832007-11-22 01:21:56 +00002393wrapped as it has variable arity:
2394<computeroutput>int MPI_Pcontrol(const int level, ...)</computeroutput></para>
2395
2396<para>Most functions are wrapped with a default wrapper which does
2397nothing except complain or abort if it is called, depending on
2398settings in <computeroutput>MPIWRAP_DEBUG</computeroutput> listed
2399above. The following functions have "real", do-something-useful
2400wrappers:</para>
2401
2402<programlisting><![CDATA[
2403PMPI_Send PMPI_Bsend PMPI_Ssend PMPI_Rsend
2404
2405PMPI_Recv PMPI_Get_count
2406
2407PMPI_Isend PMPI_Ibsend PMPI_Issend PMPI_Irsend
2408
2409PMPI_Irecv
2410PMPI_Wait PMPI_Waitall
2411PMPI_Test PMPI_Testall
2412
2413PMPI_Iprobe PMPI_Probe
2414
2415PMPI_Cancel
2416
2417PMPI_Sendrecv
2418
2419PMPI_Type_commit PMPI_Type_free
2420
2421PMPI_Pack PMPI_Unpack
2422
2423PMPI_Bcast PMPI_Gather PMPI_Scatter PMPI_Alltoall
2424PMPI_Reduce PMPI_Allreduce PMPI_Op_create
2425
2426PMPI_Comm_create PMPI_Comm_dup PMPI_Comm_free PMPI_Comm_rank PMPI_Comm_size
2427
2428PMPI_Error_string
2429PMPI_Init PMPI_Initialized PMPI_Finalize
2430]]></programlisting>
2431
2432<para> A few functions such as
2433<computeroutput>PMPI_Address</computeroutput> are listed as
2434<computeroutput>HAS_NO_WRAPPER</computeroutput>. They have no wrapper
2435at all as there is nothing worth checking, and giving a no-op wrapper
2436would reduce performance for no reason.</para>
2437
2438<para> Note that the wrapper library itself can itself generate large
2439numbers of calls to the MPI implementation, especially when walking
2440complex types. The most common functions called are
2441<computeroutput>PMPI_Extent</computeroutput>,
2442<computeroutput>PMPI_Type_get_envelope</computeroutput>,
2443<computeroutput>PMPI_Type_get_contents</computeroutput>, and
2444<computeroutput>PMPI_Type_free</computeroutput>. </para>
njn2f7eebe2009-08-05 06:34:27 +00002445</sect2>
sewardj778d7832007-11-22 01:21:56 +00002446
njn2f7eebe2009-08-05 06:34:27 +00002447<sect2 id="mc-manual.mpiwrap.limitations.types"
2448 xreflabel="Types: Abilities and Limitations">
sewardj778d7832007-11-22 01:21:56 +00002449<title>Types</title>
2450
2451<para> MPI-1.1 structured types are supported, and walked exactly.
2452The currently supported combiners are
2453<computeroutput>MPI_COMBINER_NAMED</computeroutput>,
2454<computeroutput>MPI_COMBINER_CONTIGUOUS</computeroutput>,
2455<computeroutput>MPI_COMBINER_VECTOR</computeroutput>,
2456<computeroutput>MPI_COMBINER_HVECTOR</computeroutput>
2457<computeroutput>MPI_COMBINER_INDEXED</computeroutput>,
2458<computeroutput>MPI_COMBINER_HINDEXED</computeroutput> and
2459<computeroutput>MPI_COMBINER_STRUCT</computeroutput>. This should
2460cover all MPI-1.1 types. The mechanism (function
2461<computeroutput>walk_type</computeroutput>) should extend easily to
2462cover MPI2 combiners.</para>
2463
2464<para>MPI defines some named structured types
2465(<computeroutput>MPI_FLOAT_INT</computeroutput>,
2466<computeroutput>MPI_DOUBLE_INT</computeroutput>,
2467<computeroutput>MPI_LONG_INT</computeroutput>,
2468<computeroutput>MPI_2INT</computeroutput>,
2469<computeroutput>MPI_SHORT_INT</computeroutput>,
2470<computeroutput>MPI_LONG_DOUBLE_INT</computeroutput>) which are pairs
2471of some basic type and a C <computeroutput>int</computeroutput>.
2472Unfortunately the MPI specification makes it impossible to look inside
2473these types and see where the fields are. Therefore these wrappers
2474assume the types are laid out as <computeroutput>struct { float val;
2475int loc; }</computeroutput> (for
2476<computeroutput>MPI_FLOAT_INT</computeroutput>), etc, and act
2477accordingly. This appears to be correct at least for Open MPI 1.0.2
2478and for Quadrics MPI.</para>
2479
2480<para>If <computeroutput>strict</computeroutput> is an option specified
2481in <computeroutput>MPIWRAP_DEBUG</computeroutput>, the application
2482will abort if an unhandled type is encountered. Otherwise, the
2483application will print a warning message and continue.</para>
2484
2485<para>Some effort is made to mark/check memory ranges corresponding to
2486arrays of values in a single pass. This is important for performance
2487since asking Valgrind to mark/check any range, no matter how small,
2488carries quite a large constant cost. This optimisation is applied to
2489arrays of primitive types (<computeroutput>double</computeroutput>,
2490<computeroutput>float</computeroutput>,
2491<computeroutput>int</computeroutput>,
2492<computeroutput>long</computeroutput>, <computeroutput>long
2493long</computeroutput>, <computeroutput>short</computeroutput>,
2494<computeroutput>char</computeroutput>, and <computeroutput>long
2495double</computeroutput> on platforms where <computeroutput>sizeof(long
2496double) == 8</computeroutput>). For arrays of all other types, the
2497wrappers handle each element individually and so there can be a very
2498large performance cost.</para>
2499
sewardj778d7832007-11-22 01:21:56 +00002500</sect2>
2501
2502
2503<sect2 id="mc-manual.mpiwrap.writingwrappers"
2504 xreflabel="Writing new MPI Wrappers">
2505<title>Writing new wrappers</title>
2506
2507<para>
2508For the most part the wrappers are straightforward. The only
2509significant complexity arises with nonblocking receives.</para>
2510
2511<para>The issue is that <computeroutput>MPI_Irecv</computeroutput>
2512states the recv buffer and returns immediately, giving a handle
2513(<computeroutput>MPI_Request</computeroutput>) for the transaction.
2514Later the user will have to poll for completion with
2515<computeroutput>MPI_Wait</computeroutput> etc, and when the
2516transaction completes successfully, the wrappers have to paint the
2517recv buffer. But the recv buffer details are not presented to
2518<computeroutput>MPI_Wait</computeroutput> -- only the handle is. The
2519library therefore maintains a shadow table which associates
2520uncompleted <computeroutput>MPI_Request</computeroutput>s with the
2521corresponding buffer address/count/type. When an operation completes,
2522the table is searched for the associated address/count/type info, and
2523memory is marked accordingly.</para>
2524
2525<para>Access to the table is guarded by a (POSIX pthreads) lock, so as
2526to make the library thread-safe.</para>
2527
2528<para>The table is allocated with
2529<computeroutput>malloc</computeroutput> and never
2530<computeroutput>free</computeroutput>d, so it will show up in leak
2531checks.</para>
2532
2533<para>Writing new wrappers should be fairly easy. The source file is
njna437a602009-08-04 05:24:46 +00002534<computeroutput>mpi/libmpiwrap.c</computeroutput>. If possible,
sewardj778d7832007-11-22 01:21:56 +00002535find an existing wrapper for a function of similar behaviour to the
2536one you want to wrap, and use it as a starting point. The wrappers
2537are organised in sections in the same order as the MPI 1.1 spec, to
2538aid navigation. When adding a wrapper, remember to comment out the
2539definition of the default wrapper in the long list of defaults at the
2540bottom of the file (do not remove it, just comment it out).</para>
2541</sect2>
2542
2543<sect2 id="mc-manual.mpiwrap.whattoexpect"
2544 xreflabel="What to expect with MPI Wrappers">
2545<title>What to expect when using the wrappers</title>
2546
2547<para>The wrappers should reduce Memcheck's false-error rate on MPI
2548applications. Because the wrapping is done at the MPI interface,
2549there will still potentially be a large number of errors reported in
2550the MPI implementation below the interface. The best you can do is
2551try to suppress them.</para>
2552
2553<para>You may also find that the input-side (buffer
2554length/definedness) checks find errors in your MPI use, for example
2555passing too short a buffer to
2556<computeroutput>MPI_Recv</computeroutput>.</para>
2557
2558<para>Functions which are not wrapped may increase the false
2559error rate. A possible approach is to run with
2560<computeroutput>MPI_DEBUG</computeroutput> containing
2561<computeroutput>warn</computeroutput>. This will show you functions
2562which lack proper wrappers but which are nevertheless used. You can
2563then write wrappers for them.
2564</para>
2565
2566<para>A known source of potential false errors are the
2567<computeroutput>PMPI_Reduce</computeroutput> family of functions, when
2568using a custom (user-defined) reduction function. In a reduction
2569operation, each node notionally sends data to a "central point" which
2570uses the specified reduction function to merge the data items into a
2571single item. Hence, in general, data is passed between nodes and fed
2572to the reduction function, but the wrapper library cannot mark the
2573transferred data as initialised before it is handed to the reduction
2574function, because all that happens "inside" the
2575<computeroutput>PMPI_Reduce</computeroutput> call. As a result you
2576may see false positives reported in your reduction function.</para>
2577
2578</sect2>
sewardjce10c262006-10-05 17:56:14 +00002579
2580</sect1>
sewardj778d7832007-11-22 01:21:56 +00002581
2582
2583
2584
2585
njn3e986b22004-11-30 10:43:45 +00002586</chapter>