| 1 |
<html> |
| 2 |
<head> |
| 3 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| 4 |
<title>4. Memcheck: a memory error detector</title> |
| 5 |
<link rel="stylesheet" type="text/css" href="vg_basic.css"> |
| 6 |
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot"> |
| 7 |
<link rel="home" href="index.html" title="Valgrind Documentation"> |
| 8 |
<link rel="up" href="manual.html" title="Valgrind User Manual"> |
| 9 |
<link rel="prev" href="manual-core-adv.html" title="3. Using and understanding the Valgrind core: Advanced Topics"> |
| 10 |
<link rel="next" href="cg-manual.html" title="5. Cachegrind: a high-precision tracing profiler"> |
| 11 |
</head> |
| 12 |
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
| 13 |
<div><table class="nav" width="100%" cellspacing="3" cellpadding="3" border="0" summary="Navigation header"><tr> |
| 14 |
<td width="22px" align="center" valign="middle"><a accesskey="p" href="manual-core-adv.html"><img src="images/prev.png" width="18" height="21" border="0" alt="Prev"></a></td> |
| 15 |
<td width="25px" align="center" valign="middle"><a accesskey="u" href="manual.html"><img src="images/up.png" width="21" height="18" border="0" alt="Up"></a></td> |
| 16 |
<td width="31px" align="center" valign="middle"><a accesskey="h" href="index.html"><img src="images/home.png" width="27" height="20" border="0" alt="Up"></a></td> |
| 17 |
<th align="center" valign="middle">Valgrind User Manual</th> |
| 18 |
<td width="22px" align="center" valign="middle"><a accesskey="n" href="cg-manual.html"><img src="images/next.png" width="18" height="21" border="0" alt="Next"></a></td> |
| 19 |
</tr></table></div> |
| 20 |
<div class="chapter"> |
| 21 |
<div class="titlepage"><div><div><h1 class="title"> |
| 22 |
<a name="mc-manual"></a>4. Memcheck: a memory error detector</h1></div></div></div> |
| 23 |
<div class="toc"> |
| 24 |
<p><b>Table of Contents</b></p> |
| 25 |
<dl class="toc"> |
| 26 |
<dt><span class="sect1"><a href="mc-manual.html#mc-manual.overview">4.1. Overview</a></span></dt> |
| 27 |
<dt><span class="sect1"><a href="mc-manual.html#mc-manual.errormsgs">4.2. Explanation of error messages from Memcheck</a></span></dt> |
| 28 |
<dd><dl> |
| 29 |
<dt><span class="sect2"><a href="mc-manual.html#mc-manual.badrw">4.2.1. Illegal read / Illegal write errors</a></span></dt> |
| 30 |
<dt><span class="sect2"><a href="mc-manual.html#mc-manual.uninitvals">4.2.2. Use of uninitialised values</a></span></dt> |
| 31 |
<dt><span class="sect2"><a href="mc-manual.html#mc-manual.bad-syscall-args">4.2.3. Use of uninitialised or unaddressable values in system |
| 32 |
calls</a></span></dt> |
| 33 |
<dt><span class="sect2"><a href="mc-manual.html#mc-manual.badfrees">4.2.4. Illegal frees</a></span></dt> |
| 34 |
<dt><span class="sect2"><a href="mc-manual.html#mc-manual.rudefn">4.2.5. When a heap block is freed with an inappropriate deallocation |
| 35 |
function</a></span></dt> |
| 36 |
<dt><span class="sect2"><a href="mc-manual.html#mc-manual.overlap">4.2.6. Overlapping source and destination blocks</a></span></dt> |
| 37 |
<dt><span class="sect2"><a href="mc-manual.html#mc-manual.fishyvalue">4.2.7. Fishy argument values</a></span></dt> |
| 38 |
<dt><span class="sect2"><a href="mc-manual.html#mc-manual.reallocsizezero">4.2.8. Realloc size zero</a></span></dt> |
| 39 |
<dt><span class="sect2"><a href="mc-manual.html#mc-manual.alignment">4.2.9. Alignment Errors</a></span></dt> |
| 40 |
<dt><span class="sect2"><a href="mc-manual.html#mc-manual.leaks">4.2.10. Memory leak detection</a></span></dt> |
| 41 |
</dl></dd> |
| 42 |
<dt><span class="sect1"><a href="mc-manual.html#mc-manual.options">4.3. Memcheck Command-Line Options</a></span></dt> |
| 43 |
<dt><span class="sect1"><a href="mc-manual.html#mc-manual.suppfiles">4.4. Writing suppression files</a></span></dt> |
| 44 |
<dt><span class="sect1"><a href="mc-manual.html#mc-manual.machine">4.5. Details of Memcheck's checking machinery</a></span></dt> |
| 45 |
<dd><dl> |
| 46 |
<dt><span class="sect2"><a href="mc-manual.html#mc-manual.value">4.5.1. Valid-value (V) bits</a></span></dt> |
| 47 |
<dt><span class="sect2"><a href="mc-manual.html#mc-manual.vaddress">4.5.2. Valid-address (A) bits</a></span></dt> |
| 48 |
<dt><span class="sect2"><a href="mc-manual.html#mc-manual.together">4.5.3. Putting it all together</a></span></dt> |
| 49 |
</dl></dd> |
| 50 |
<dt><span class="sect1"><a href="mc-manual.html#mc-manual.monitor-commands">4.6. Memcheck Monitor Commands</a></span></dt> |
| 51 |
<dt><span class="sect1"><a href="mc-manual.html#mc-manual.clientreqs">4.7. Client Requests</a></span></dt> |
| 52 |
<dt><span class="sect1"><a href="mc-manual.html#mc-manual.mempools">4.8. Memory Pools: describing and working with custom allocators</a></span></dt> |
| 53 |
<dt><span class="sect1"><a href="mc-manual.html#mc-manual.mpiwrap">4.9. Debugging MPI Parallel Programs with Valgrind</a></span></dt> |
| 54 |
<dd><dl> |
| 55 |
<dt><span class="sect2"><a href="mc-manual.html#mc-manual.mpiwrap.build">4.9.1. Building and installing the wrappers</a></span></dt> |
| 56 |
<dt><span class="sect2"><a href="mc-manual.html#mc-manual.mpiwrap.gettingstarted">4.9.2. Getting started</a></span></dt> |
| 57 |
<dt><span class="sect2"><a href="mc-manual.html#mc-manual.mpiwrap.controlling">4.9.3. Controlling the wrapper library</a></span></dt> |
| 58 |
<dt><span class="sect2"><a href="mc-manual.html#mc-manual.mpiwrap.limitations.functions">4.9.4. Functions</a></span></dt> |
| 59 |
<dt><span class="sect2"><a href="mc-manual.html#mc-manual.mpiwrap.limitations.types">4.9.5. Types</a></span></dt> |
| 60 |
<dt><span class="sect2"><a href="mc-manual.html#mc-manual.mpiwrap.writingwrappers">4.9.6. Writing new wrappers</a></span></dt> |
| 61 |
<dt><span class="sect2"><a href="mc-manual.html#mc-manual.mpiwrap.whattoexpect">4.9.7. What to expect when using the wrappers</a></span></dt> |
| 62 |
</dl></dd> |
| 63 |
</dl> |
| 64 |
</div> |
| 65 |
<p>To use this tool, you may specify <code class="option">--tool=memcheck</code> |
| 66 |
on the Valgrind command line. You don't have to, though, since Memcheck |
| 67 |
is the default tool.</p> |
| 68 |
<div class="sect1"> |
| 69 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 70 |
<a name="mc-manual.overview"></a>4.1. Overview</h2></div></div></div> |
| 71 |
<p>Memcheck is a memory error detector. It can detect the following |
| 72 |
problems that are common in C and C++ programs.</p> |
| 73 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 74 |
<li class="listitem"><p>Accessing memory you shouldn't, e.g. overrunning and underrunning |
| 75 |
heap blocks, overrunning the top of the stack, and accessing memory after |
| 76 |
it has been freed.</p></li> |
| 77 |
<li class="listitem"><p>Using undefined values, i.e. values that have not been initialised, |
| 78 |
or that have been derived from other undefined values.</p></li> |
| 79 |
<li class="listitem"> |
| 80 |
<p>Incorrect freeing of heap memory, such as double-freeing heap |
| 81 |
blocks, or mismatched use of |
| 82 |
<code class="function">malloc</code>/<code class="computeroutput">new</code>/<code class="computeroutput">new[]</code> |
| 83 |
versus |
| 84 |
<code class="function">free</code>/<code class="computeroutput">delete</code>/<code class="computeroutput">delete[]</code></p> |
| 85 |
<p>Mismatches will also be reported for <code class="computeroutput">sized</code> and <code class="computeroutput">aligned</code> |
| 86 |
allocation and deallocation functions if the deallocation value |
| 87 |
does not match the allocation value.</p> |
| 88 |
</li> |
| 89 |
<li class="listitem"><p>Overlapping <code class="computeroutput">src</code> and |
| 90 |
<code class="computeroutput">dst</code> pointers in |
| 91 |
<code class="computeroutput">memcpy</code> and related |
| 92 |
functions.</p></li> |
| 93 |
<li class="listitem"><p>Passing a fishy (presumably negative) value to the |
| 94 |
<code class="computeroutput">size</code> parameter of a memory |
| 95 |
allocation function.</p></li> |
| 96 |
<li class="listitem"><p>Using a <code class="computeroutput">size</code> value of 0 |
| 97 |
with realloc.</p></li> |
| 98 |
<li class="listitem"><p>Using an <code class="computeroutput">alignment</code> value that |
| 99 |
is not a power of two.</p></li> |
| 100 |
<li class="listitem"><p>Memory leaks.</p></li> |
| 101 |
</ul></div> |
| 102 |
<p>Problems like these can be difficult to find by other means, |
| 103 |
often remaining undetected for long periods, then causing occasional, |
| 104 |
difficult-to-diagnose crashes.</p> |
| 105 |
<p>Memcheck also provides <a class="xref" href="manual-core.html#manual-core.xtree" title="2.10. Execution Trees">Execution Trees</a> memory |
| 106 |
profiling using the command line |
| 107 |
option <code class="computeroutput">--xtree-memory</code> and the monitor command |
| 108 |
<code class="computeroutput">xtmemory</code>.</p> |
| 109 |
</div> |
| 110 |
<div class="sect1"> |
| 111 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 112 |
<a name="mc-manual.errormsgs"></a>4.2. Explanation of error messages from Memcheck</h2></div></div></div> |
| 113 |
<p>Memcheck issues a range of error messages. This section presents a |
| 114 |
quick summary of what error messages mean. The precise behaviour of the |
| 115 |
error-checking machinery is described in <a class="xref" href="mc-manual.html#mc-manual.machine" title="4.5. Details of Memcheck's checking machinery">Details of Memcheck's checking machinery</a>.</p> |
| 116 |
<div class="sect2"> |
| 117 |
<div class="titlepage"><div><div><h3 class="title"> |
| 118 |
<a name="mc-manual.badrw"></a>4.2.1. Illegal read / Illegal write errors</h3></div></div></div> |
| 119 |
<p>For example:</p> |
| 120 |
<pre class="programlisting"> |
| 121 |
Invalid read of size 4 |
| 122 |
at 0x40F6BBCC: (within /usr/lib/libpng.so.2.1.0.9) |
| 123 |
by 0x40F6B804: (within /usr/lib/libpng.so.2.1.0.9) |
| 124 |
by 0x40B07FF4: read_png_image(QImageIO *) (kernel/qpngio.cpp:326) |
| 125 |
by 0x40AC751B: QImageIO::read() (kernel/qimage.cpp:3621) |
| 126 |
Address 0xBFFFF0E0 is not stack'd, malloc'd or free'd |
| 127 |
</pre> |
| 128 |
<p>This happens when your program reads or writes memory at a place |
| 129 |
which Memcheck reckons it shouldn't. In this example, the program did a |
| 130 |
4-byte read at address 0xBFFFF0E0, somewhere within the system-supplied |
| 131 |
library libpng.so.2.1.0.9, which was called from somewhere else in the |
| 132 |
same library, called from line 326 of <code class="filename">qpngio.cpp</code>, |
| 133 |
and so on.</p> |
| 134 |
<p>Memcheck tries to establish what the illegal address might relate |
| 135 |
to, since that's often useful. So, if it points into a block of memory |
| 136 |
which has already been freed, you'll be informed of this, and also where |
| 137 |
the block was freed. Likewise, if it should turn out to be just off |
| 138 |
the end of a heap block, a common result of off-by-one-errors in |
| 139 |
array subscripting, you'll be informed of this fact, and also where the |
| 140 |
block was allocated. If you use the <code class="option"><a class="link" href="manual-core.html#opt.read-var-info">--read-var-info</a></code> option |
| 141 |
Memcheck will run more slowly |
| 142 |
but may give a more detailed description of any illegal address.</p> |
| 143 |
<p>In this example, Memcheck can't identify the address. Actually |
| 144 |
the address is on the stack, but, for some reason, this is not a valid |
| 145 |
stack address -- it is below the stack pointer and that isn't allowed. |
| 146 |
In this particular case it's probably caused by GCC generating invalid |
| 147 |
code, a known bug in some ancient versions of GCC.</p> |
| 148 |
<p>Note that Memcheck only tells you that your program is about to |
| 149 |
access memory at an illegal address. It can't stop the access from |
| 150 |
happening. So, if your program makes an access which normally would |
| 151 |
result in a segmentation fault, you program will still suffer the same |
| 152 |
fate -- but you will get a message from Memcheck immediately prior to |
| 153 |
this. In this particular example, reading junk on the stack is |
| 154 |
non-fatal, and the program stays alive.</p> |
| 155 |
</div> |
| 156 |
<div class="sect2"> |
| 157 |
<div class="titlepage"><div><div><h3 class="title"> |
| 158 |
<a name="mc-manual.uninitvals"></a>4.2.2. Use of uninitialised values</h3></div></div></div> |
| 159 |
<p>For example:</p> |
| 160 |
<pre class="programlisting"> |
| 161 |
Conditional jump or move depends on uninitialised value(s) |
| 162 |
at 0x402DFA94: _IO_vfprintf (_itoa.h:49) |
| 163 |
by 0x402E8476: _IO_printf (printf.c:36) |
| 164 |
by 0x8048472: main (tests/manuel1.c:8) |
| 165 |
</pre> |
| 166 |
<p>An uninitialised-value use error is reported when your program |
| 167 |
uses a value which hasn't been initialised -- in other words, is |
| 168 |
undefined. Here, the undefined value is used somewhere inside the |
| 169 |
<code class="function">printf</code> machinery of the C library. This error was |
| 170 |
reported when running the following small program:</p> |
| 171 |
<pre class="programlisting"> |
| 172 |
int main() |
| 173 |
{ |
| 174 |
int x; |
| 175 |
printf ("x = %d\n", x); |
| 176 |
}</pre> |
| 177 |
<p>It is important to understand that your program can copy around |
| 178 |
junk (uninitialised) data as much as it likes. Memcheck observes this |
| 179 |
and keeps track of the data, but does not complain. A complaint is |
| 180 |
issued only when your program attempts to make use of uninitialised |
| 181 |
data in a way that might affect your program's externally-visible behaviour. |
| 182 |
In this example, <code class="varname">x</code> is uninitialised. Memcheck observes |
| 183 |
the value being passed to <code class="function">_IO_printf</code> and thence to |
| 184 |
<code class="function">_IO_vfprintf</code>, but makes no comment. However, |
| 185 |
<code class="function">_IO_vfprintf</code> has to examine the value of |
| 186 |
<code class="varname">x</code> so it can turn it into the corresponding ASCII string, |
| 187 |
and it is at this point that Memcheck complains.</p> |
| 188 |
<p>Sources of uninitialised data tend to be:</p> |
| 189 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 190 |
<li class="listitem"><p>Local variables in procedures which have not been initialised, |
| 191 |
as in the example above.</p></li> |
| 192 |
<li class="listitem"><p>The contents of heap blocks (allocated with |
| 193 |
<code class="function">malloc</code>, <code class="function">new</code>, or a similar |
| 194 |
function) before you (or a constructor) write something there. |
| 195 |
</p></li> |
| 196 |
</ul></div> |
| 197 |
<p>To see information on the sources of uninitialised data in your |
| 198 |
program, use the <code class="option">--track-origins=yes</code> option. This |
| 199 |
makes Memcheck run more slowly, but can make it much easier to track down |
| 200 |
the root causes of uninitialised value errors.</p> |
| 201 |
</div> |
| 202 |
<div class="sect2"> |
| 203 |
<div class="titlepage"><div><div><h3 class="title"> |
| 204 |
<a name="mc-manual.bad-syscall-args"></a>4.2.3. Use of uninitialised or unaddressable values in system |
| 205 |
calls</h3></div></div></div> |
| 206 |
<p>Memcheck checks all parameters to system calls: |
| 207 |
</p> |
| 208 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 209 |
<li class="listitem"><p>It checks all the direct parameters themselves, whether they are |
| 210 |
initialised.</p></li> |
| 211 |
<li class="listitem"><p>Also, if a system call needs to read from a buffer provided by |
| 212 |
your program, Memcheck checks that the entire buffer is addressable |
| 213 |
and its contents are initialised.</p></li> |
| 214 |
<li class="listitem"><p>Also, if the system call needs to write to a user-supplied |
| 215 |
buffer, Memcheck checks that the buffer is addressable.</p></li> |
| 216 |
</ul></div> |
| 217 |
<p> |
| 218 |
</p> |
| 219 |
<p>After the system call, Memcheck updates its tracked information to |
| 220 |
precisely reflect any changes in memory state caused by the system |
| 221 |
call.</p> |
| 222 |
<p>Here's an example of two system calls with invalid parameters:</p> |
| 223 |
<pre class="programlisting"> |
| 224 |
#include <stdlib.h> |
| 225 |
#include <unistd.h> |
| 226 |
int main( void ) |
| 227 |
{ |
| 228 |
char* arr = malloc(10); |
| 229 |
int* arr2 = malloc(sizeof(int)); |
| 230 |
write( 1 /* stdout */, arr, 10 ); |
| 231 |
exit(arr2[0]); |
| 232 |
} |
| 233 |
</pre> |
| 234 |
<p>You get these complaints ...</p> |
| 235 |
<pre class="programlisting"> |
| 236 |
Syscall param write(buf) points to uninitialised byte(s) |
| 237 |
at 0x25A48723: __write_nocancel (in /lib/tls/libc-2.3.3.so) |
| 238 |
by 0x259AFAD3: __libc_start_main (in /lib/tls/libc-2.3.3.so) |
| 239 |
by 0x8048348: (within /auto/homes/njn25/grind/head4/a.out) |
| 240 |
Address 0x25AB8028 is 0 bytes inside a block of size 10 alloc'd |
| 241 |
at 0x259852B0: malloc (vg_replace_malloc.c:130) |
| 242 |
by 0x80483F1: main (a.c:5) |
| 243 |
|
| 244 |
Syscall param exit(error_code) contains uninitialised byte(s) |
| 245 |
at 0x25A21B44: __GI__exit (in /lib/tls/libc-2.3.3.so) |
| 246 |
by 0x8048426: main (a.c:8) |
| 247 |
</pre> |
| 248 |
<p>... because the program has (a) written uninitialised junk |
| 249 |
from the heap block to the standard output, and (b) passed an |
| 250 |
uninitialised value to <code class="function">exit</code>. Note that the first |
| 251 |
error refers to the memory pointed to by |
| 252 |
<code class="computeroutput">buf</code> (not |
| 253 |
<code class="computeroutput">buf</code> itself), but the second error |
| 254 |
refers directly to <code class="computeroutput">exit</code>'s argument |
| 255 |
<code class="computeroutput">arr2[0]</code>.</p> |
| 256 |
</div> |
| 257 |
<div class="sect2"> |
| 258 |
<div class="titlepage"><div><div><h3 class="title"> |
| 259 |
<a name="mc-manual.badfrees"></a>4.2.4. Illegal frees</h3></div></div></div> |
| 260 |
<p>For example:</p> |
| 261 |
<pre class="programlisting"> |
| 262 |
Invalid free() |
| 263 |
at 0x4004FFDF: free (vg_clientmalloc.c:577) |
| 264 |
by 0x80484C7: main (tests/doublefree.c:10) |
| 265 |
Address 0x3807F7B4 is 0 bytes inside a block of size 177 free'd |
| 266 |
at 0x4004FFDF: free (vg_clientmalloc.c:577) |
| 267 |
by 0x80484C7: main (tests/doublefree.c:10) |
| 268 |
</pre> |
| 269 |
<p>Memcheck keeps track of the blocks allocated by your program |
| 270 |
with <code class="function">malloc</code>/<code class="computeroutput">new</code>, |
| 271 |
so it can know exactly whether or not the argument to |
| 272 |
<code class="function">free</code>/<code class="computeroutput">delete</code> is |
| 273 |
legitimate or not. Here, this test program has freed the same block |
| 274 |
twice. As with the illegal read/write errors, Memcheck attempts to |
| 275 |
make sense of the address freed. If, as here, the address is one |
| 276 |
which has previously been freed, you wil be told that -- making |
| 277 |
duplicate frees of the same block easy to spot. You will also get this |
| 278 |
message if you try to free a pointer that doesn't point to the start of a |
| 279 |
heap block.</p> |
| 280 |
</div> |
| 281 |
<div class="sect2"> |
| 282 |
<div class="titlepage"><div><div><h3 class="title"> |
| 283 |
<a name="mc-manual.rudefn"></a>4.2.5. When a heap block is freed with an inappropriate deallocation |
| 284 |
function</h3></div></div></div> |
| 285 |
<p>In the following example, a block allocated with |
| 286 |
<code class="function">new[]</code> has wrongly been deallocated with |
| 287 |
<code class="function">free</code>:</p> |
| 288 |
<pre class="programlisting"> |
| 289 |
Mismatched free() / delete / delete [] |
| 290 |
at 0x40043249: free (vg_clientfuncs.c:171) |
| 291 |
by 0x4102BB4E: QGArray::~QGArray(void) (tools/qgarray.cpp:149) |
| 292 |
by 0x4C261C41: PptDoc::~PptDoc(void) (include/qmemarray.h:60) |
| 293 |
by 0x4C261F0E: PptXml::~PptXml(void) (pptxml.cc:44) |
| 294 |
Address 0x4BB292A8 is 0 bytes inside a block of size 64 alloc'd |
| 295 |
at 0x4004318C: operator new[](unsigned int) (vg_clientfuncs.c:152) |
| 296 |
by 0x4C21BC15: KLaola::readSBStream(int) const (klaola.cc:314) |
| 297 |
by 0x4C21C155: KLaola::stream(KLaola::OLENode const *) (klaola.cc:416) |
| 298 |
by 0x4C21788F: OLEFilter::convert(QCString const &) (olefilter.cc:272) |
| 299 |
</pre> |
| 300 |
<p>In <code class="literal">C++</code> it's important to deallocate memory in a |
| 301 |
way compatible with how it was allocated.</p> |
| 302 |
<p>Most of the time in C++ you will write code that |
| 303 |
uses <code class="function">new expression</code> and <code class="function">delete |
| 304 |
expression</code> |
| 305 |
(see <a class="ulink" href="https://en.cppreference.com/w/cpp/language/new" target="_top">cppreference |
| 306 |
new expression</a> |
| 307 |
and <a class="ulink" href="https://en.cppreference.com/w/cpp/language/delete" target="_top">cppreference |
| 308 |
delete expression</a>). A new expression will |
| 309 |
call <code class="function">operator new</code> to perform the allocation and |
| 310 |
then call the constructor (if one exists) on the object. Similarly a |
| 311 |
delete expression will call the destructor on the object (if one |
| 312 |
exists) and then call <code class="function">operator delete</code>. The array |
| 313 |
overloads call constructors/destructors for each object in the |
| 314 |
array.</p> |
| 315 |
<p>The deal is:</p> |
| 316 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 317 |
<li class="listitem"><p>If allocated with |
| 318 |
<code class="function">malloc</code>, |
| 319 |
<code class="function">calloc</code>, |
| 320 |
<code class="function">realloc</code>, |
| 321 |
<code class="function">valloc</code> or |
| 322 |
<code class="function">memalign</code>, you must |
| 323 |
deallocate with <code class="function">free</code>.</p></li> |
| 324 |
<li class="listitem"><p>If allocated with <code class="function">new</code>, you must deallocate |
| 325 |
with <code class="function">delete</code>.</p></li> |
| 326 |
<li class="listitem"><p>If allocated with <code class="function">new[]</code>, you must |
| 327 |
deallocate with <code class="function">delete[]</code>.</p></li> |
| 328 |
</ul></div> |
| 329 |
<p>Mixing types of allocators and deallocators is undefined |
| 330 |
behaviour. That means that on some platforms you might not have any |
| 331 |
problems, but the same program may then crash on a different platform, |
| 332 |
Solaris for example. So it's best to fix it properly. According to |
| 333 |
the KDE folks "it's amazing how many C++ programmers don't know |
| 334 |
this".</p> |
| 335 |
<p>The reason behind the requirement is as follows. In some C++ |
| 336 |
implementations, <code class="function">delete[]</code> must be used for |
| 337 |
objects allocated by <code class="function">new[]</code> because the compiler |
| 338 |
stores the size of the array and the pointer-to-member to the |
| 339 |
destructor of the array's content just before the pointer actually |
| 340 |
returned. <code class="function">delete</code> doesn't account for this and |
| 341 |
will get confused, possibly corrupting the heap. Even if there is no |
| 342 |
corruption there are likely to be resource leaks since using the wrong |
| 343 |
delete may result in the wrong number of destructors being |
| 344 |
called.</p> |
| 345 |
<p>C++ aligned allocations need to be freed using aligned delete |
| 346 |
with the same alignment.</p> |
| 347 |
</div> |
| 348 |
<div class="sect2"> |
| 349 |
<div class="titlepage"><div><div><h3 class="title"> |
| 350 |
<a name="mc-manual.overlap"></a>4.2.6. Overlapping source and destination blocks</h3></div></div></div> |
| 351 |
<p>The following C library functions copy some data from one |
| 352 |
memory block to another (or something similar): |
| 353 |
<code class="function">memcpy</code>, |
| 354 |
<code class="function">strcpy</code>, |
| 355 |
<code class="function">strncpy</code>, |
| 356 |
<code class="function">strcat</code>, |
| 357 |
<code class="function">strncat</code>. |
| 358 |
The blocks pointed to by their <code class="computeroutput">src</code> and |
| 359 |
<code class="computeroutput">dst</code> pointers aren't allowed to overlap. |
| 360 |
The POSIX standards have wording along the lines "If copying takes place |
| 361 |
between objects that overlap, the behavior is undefined." Therefore, |
| 362 |
Memcheck checks for this. |
| 363 |
</p> |
| 364 |
<p>For example:</p> |
| 365 |
<pre class="programlisting"> |
| 366 |
==27492== Source and destination overlap in memcpy(0xbffff294, 0xbffff280, 21) |
| 367 |
==27492== at 0x40026CDC: memcpy (mc_replace_strmem.c:71) |
| 368 |
==27492== by 0x804865A: main (overlap.c:40) |
| 369 |
</pre> |
| 370 |
<p>You don't want the two blocks to overlap because one of them could |
| 371 |
get partially overwritten by the copying.</p> |
| 372 |
<p>You might think that Memcheck is being overly pedantic reporting |
| 373 |
this in the case where <code class="computeroutput">dst</code> is less than |
| 374 |
<code class="computeroutput">src</code>. For example, the obvious way to |
| 375 |
implement <code class="function">memcpy</code> is by copying from the first |
| 376 |
byte to the last. However, the optimisation guides of some |
| 377 |
architectures recommend copying from the last byte down to the first. |
| 378 |
Also, some implementations of <code class="function">memcpy</code> zero |
| 379 |
<code class="computeroutput">dst</code> before copying, because zeroing the |
| 380 |
destination's cache line(s) can improve performance.</p> |
| 381 |
<p>The moral of the story is: if you want to write truly portable |
| 382 |
code, don't make any assumptions about the language |
| 383 |
implementation.</p> |
| 384 |
</div> |
| 385 |
<div class="sect2"> |
| 386 |
<div class="titlepage"><div><div><h3 class="title"> |
| 387 |
<a name="mc-manual.fishyvalue"></a>4.2.7. Fishy argument values</h3></div></div></div> |
| 388 |
<p>All memory allocation functions take an argument specifying the |
| 389 |
size of the memory block that should be allocated. Clearly, the requested |
| 390 |
size should be a non-negative value and is typically not excessively large. |
| 391 |
For instance, it is extremely unlikly that the size of an allocation |
| 392 |
request exceeds 2**63 bytes on a 64-bit machine. It is much more likely that |
| 393 |
such a value is the result of an erroneous size calculation and is in effect |
| 394 |
a negative value (that just happens to appear excessively large because |
| 395 |
the bit pattern is interpreted as an unsigned integer). |
| 396 |
Such a value is called a "fishy value". |
| 397 |
|
| 398 |
The <code class="varname">size</code> argument of the following allocation functions |
| 399 |
is checked for being fishy: |
| 400 |
<code class="function">malloc</code>, |
| 401 |
<code class="function">calloc</code>, |
| 402 |
<code class="function">realloc</code>, |
| 403 |
<code class="function">memalign</code>, |
| 404 |
<code class="function">posix_memalign</code>, |
| 405 |
<code class="function">aligned_alloc</code>, |
| 406 |
<code class="function">new</code>, |
| 407 |
<code class="function">new []</code>. |
| 408 |
<code class="function">__builtin_new</code>, |
| 409 |
<code class="function">__builtin_vec_new</code>, |
| 410 |
For <code class="function">calloc</code> both arguments are checked. |
| 411 |
</p> |
| 412 |
<p>For example:</p> |
| 413 |
<pre class="programlisting"> |
| 414 |
==32233== Argument 'size' of function malloc has a fishy (possibly negative) value: -3 |
| 415 |
==32233== at 0x4C2CFA7: malloc (vg_replace_malloc.c:298) |
| 416 |
==32233== by 0x400555: foo (fishy.c:15) |
| 417 |
==32233== by 0x400583: main (fishy.c:23) |
| 418 |
</pre> |
| 419 |
<p>In earlier Valgrind versions those values were being referred to |
| 420 |
as "silly arguments" and no back-trace was included. |
| 421 |
</p> |
| 422 |
</div> |
| 423 |
<div class="sect2"> |
| 424 |
<div class="titlepage"><div><div><h3 class="title"> |
| 425 |
<a name="mc-manual.reallocsizezero"></a>4.2.8. Realloc size zero</h3></div></div></div> |
| 426 |
<p>The (ab)use or realloc to also do the job of <code class="function">free</code> |
| 427 |
has been poorly understood for a long time. In the C17 standard |
| 428 |
ISO/IEC 9899:2017] the behaviour of realloc when the size argument |
| 429 |
is zero is specified as implementation defined. Memcheck warns about |
| 430 |
the non-portable use or realloc.</p> |
| 431 |
<p>For example:</p> |
| 432 |
<pre class="programlisting"> |
| 433 |
==77609== realloc() with size 0 |
| 434 |
==77609== at 0x48502B8: realloc (vg_replace_malloc.c:1450) |
| 435 |
==77609== by 0x201989: main (realloczero.c:8) |
| 436 |
==77609== Address 0x5464040 is 0 bytes inside a block of size 4 alloc'd |
| 437 |
==77609== at 0x484CBB4: malloc (vg_replace_malloc.c:397) |
| 438 |
==77609== by 0x201978: main (realloczero.c:7) |
| 439 |
</pre> |
| 440 |
</div> |
| 441 |
<div class="sect2"> |
| 442 |
<div class="titlepage"><div><div><h3 class="title"> |
| 443 |
<a name="mc-manual.alignment"></a>4.2.9. Alignment Errors</h3></div></div></div> |
| 444 |
<p>C and C++ have several functions that allow the user to obtain aligned memory. |
| 445 |
Typically this is done for performance reasons so that the memory will be cache line |
| 446 |
or memory page aligned. C has the functions <code class="computeroutput">memalign</code>, |
| 447 |
<code class="computeroutput">posix_memalign</code> and <code class="computeroutput">aligned_alloc</code>. |
| 448 |
C++ has numerous overloads of <code class="computeroutput">operator new</code> and <code class="computeroutput"> |
| 449 |
operator delete</code>. Of these, posix_memalign is quite clearly |
| 450 |
specified, the others vary quite widely between implementations. Valgrind will generate |
| 451 |
errors for values of alignment that are invalid on any platform.</p> |
| 452 |
<p><code class="computeroutput">memalign</code> will produce errors if the alignment |
| 453 |
is zero or not a multiple of two.</p> |
| 454 |
<p><code class="computeroutput">posix_memalign</code> will produce errors if the alignment |
| 455 |
is less than sizeof(size_t), not a multiple of two or if the size is zero.</p> |
| 456 |
<p><code class="computeroutput">aligned_alloc</code> will produce errors if the alignment |
| 457 |
is not a multiple of two , if the size is zero or if the size is not an integral |
| 458 |
multiple of the alignment.</p> |
| 459 |
<p><code class="computeroutput">aligned new</code> will produce errors if the alignment |
| 460 |
is zero or not a multiple of two. The <code class="computeroutput">nothrow</code> overloads |
| 461 |
will return a NULL pointer. The non-nothrow overloads will abort Valgrind.</p> |
| 462 |
<p><code class="computeroutput">aligned delete</code> will produce errors if the alignment |
| 463 |
is zero or not a multiple of two or if the alignment is not the same as that used by |
| 464 |
<code class="computeroutput">aligned new</code>.</p> |
| 465 |
<p><code class="computeroutput">sized delete</code> will produce errors if the size |
| 466 |
is not the same as that used by <code class="computeroutput">new</code>.</p> |
| 467 |
<p><code class="computeroutput">sized aligned delete</code> combines the error conditions |
| 468 |
of the individual sized and aligned delete operators.</p> |
| 469 |
<p>Example output:</p> |
| 470 |
<pre class="programlisting"> |
| 471 |
==65825== Invalid alignment value: 3 (should be power of 2) |
| 472 |
==65825== at 0x485197E: memalign (vg_replace_malloc.c:1740) |
| 473 |
==65825== by 0x201CD2: main (memalign.c:39) |
| 474 |
</pre> |
| 475 |
</div> |
| 476 |
<div class="sect2"> |
| 477 |
<div class="titlepage"><div><div><h3 class="title"> |
| 478 |
<a name="mc-manual.leaks"></a>4.2.10. Memory leak detection</h3></div></div></div> |
| 479 |
<p>Memcheck keeps track of all heap blocks issued in response to |
| 480 |
calls to |
| 481 |
<code class="function">malloc</code>/<code class="function">new</code> et al. |
| 482 |
So when the program exits, it knows which blocks have not been freed. |
| 483 |
</p> |
| 484 |
<p>If <code class="option">--leak-check</code> is set appropriately, for each |
| 485 |
remaining block, Memcheck determines if the block is reachable from pointers |
| 486 |
within the root-set. The root-set consists of (a) general purpose registers |
| 487 |
of all threads, and (b) initialised, aligned, pointer-sized data words in |
| 488 |
accessible client memory, including stacks.</p> |
| 489 |
<p>There are two ways a block can be reached. The first is with a |
| 490 |
"start-pointer", i.e. a pointer to the start of the block. The second is with |
| 491 |
an "interior-pointer", i.e. a pointer to the middle of the block. There are |
| 492 |
several ways we know of that an interior-pointer can occur:</p> |
| 493 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 494 |
<li class="listitem"><p>The pointer might have originally been a start-pointer and have been |
| 495 |
moved along deliberately (or not deliberately) by the program. In |
| 496 |
particular, this can happen if your program uses tagged pointers, i.e. |
| 497 |
if it uses the bottom one, two or three bits of a pointer, which are |
| 498 |
normally always zero due to alignment, in order to store extra |
| 499 |
information.</p></li> |
| 500 |
<li class="listitem"><p>It might be a random junk value in memory, entirely unrelated, just |
| 501 |
a coincidence.</p></li> |
| 502 |
<li class="listitem"><p>It might be a pointer to the inner char array of a C++ |
| 503 |
<code class="computeroutput">std::string</code>. For example, some |
| 504 |
compilers add 3 words at the beginning of the std::string to |
| 505 |
store the length, the capacity and a reference count before the |
| 506 |
memory containing the array of characters. They return a pointer |
| 507 |
just after these 3 words, pointing at the char array.</p></li> |
| 508 |
<li class="listitem"><p>Some code might allocate a block of memory, and use the first 8 |
| 509 |
bytes to store (block size - 8) as a 64bit number. |
| 510 |
<code class="computeroutput">sqlite3MemMalloc</code> does this.</p></li> |
| 511 |
<li class="listitem"><p>It might be a pointer to an array of C++ objects (which possess |
| 512 |
destructors) allocated with <code class="computeroutput">new[]</code>. In |
| 513 |
this case, some compilers store a "magic cookie" containing the array |
| 514 |
length at the start of the allocated block, and return a pointer to just |
| 515 |
past that magic cookie, i.e. an interior-pointer. |
| 516 |
|
| 517 |
See <a class="ulink" href="https://www.math.utah.edu/docs/info/gxxint_1.html#SEC17" target="_top">this |
| 518 |
page</a> for more information.</p></li> |
| 519 |
<li class="listitem"><p>It might be a pointer to an inner part of a C++ object using |
| 520 |
multiple inheritance. </p></li> |
| 521 |
</ul></div> |
| 522 |
<p>You can optionally activate heuristics to use during the leak |
| 523 |
search to detect the interior pointers corresponding to |
| 524 |
the <code class="computeroutput">stdstring</code>, |
| 525 |
<code class="computeroutput">length64</code>, |
| 526 |
<code class="computeroutput">newarray</code> |
| 527 |
and <code class="computeroutput">multipleinheritance</code> cases. If the |
| 528 |
heuristic detects that an interior pointer corresponds to such a case, |
| 529 |
the block will be considered as reachable by the interior |
| 530 |
pointer. In other words, the interior pointer will be treated |
| 531 |
as if it were a start pointer.</p> |
| 532 |
<p>With that in mind, consider the nine possible cases described by the |
| 533 |
following figure.</p> |
| 534 |
<pre class="programlisting"> |
| 535 |
Pointer chain AAA Leak Case BBB Leak Case |
| 536 |
------------- ------------- ------------- |
| 537 |
(1) RRR ------------> BBB DR |
| 538 |
(2) RRR ---> AAA ---> BBB DR IR |
| 539 |
(3) RRR BBB DL |
| 540 |
(4) RRR AAA ---> BBB DL IL |
| 541 |
(5) RRR ------?-----> BBB (y)DR, (n)DL |
| 542 |
(6) RRR ---> AAA -?-> BBB DR (y)IR, (n)DL |
| 543 |
(7) RRR -?-> AAA ---> BBB (y)DR, (n)DL (y)IR, (n)IL |
| 544 |
(8) RRR -?-> AAA -?-> BBB (y)DR, (n)DL (y,y)IR, (n,y)IL, (_,n)DL |
| 545 |
(9) RRR AAA -?-> BBB DL (y)IL, (n)DL |
| 546 |
|
| 547 |
Pointer chain legend: |
| 548 |
- RRR: a root set node or DR block |
| 549 |
- AAA, BBB: heap blocks |
| 550 |
- --->: a start-pointer |
| 551 |
- -?->: an interior-pointer |
| 552 |
|
| 553 |
Leak Case legend: |
| 554 |
- DR: Directly reachable |
| 555 |
- IR: Indirectly reachable |
| 556 |
- DL: Directly lost |
| 557 |
- IL: Indirectly lost |
| 558 |
- (y)XY: it's XY if the interior-pointer is a real pointer |
| 559 |
- (n)XY: it's XY if the interior-pointer is not a real pointer |
| 560 |
- (_)XY: it's XY in either case |
| 561 |
</pre> |
| 562 |
<p>Every possible case can be reduced to one of the above nine. Memcheck |
| 563 |
merges some of these cases in its output, resulting in the following four |
| 564 |
leak kinds.</p> |
| 565 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 566 |
<li class="listitem"><p>"Still reachable". This covers cases 1 and 2 (for the BBB blocks) |
| 567 |
above. A start-pointer or chain of start-pointers to the block is |
| 568 |
found. Since the block is still pointed at, the programmer could, at |
| 569 |
least in principle, have freed it before program exit. "Still reachable" |
| 570 |
blocks are very common and arguably not a problem. So, by default, |
| 571 |
Memcheck won't report such blocks individually.</p></li> |
| 572 |
<li class="listitem"><p>"Definitely lost". This covers case 3 (for the BBB blocks) above. |
| 573 |
This means that no pointer to the block can be found. The block is |
| 574 |
classified as "lost", because the programmer could not possibly have |
| 575 |
freed it at program exit, since no pointer to it exists. This is likely |
| 576 |
a symptom of having lost the pointer at some earlier point in the |
| 577 |
program. Such cases should be fixed by the programmer.</p></li> |
| 578 |
<li class="listitem"><p>"Indirectly lost". This covers cases 4 and 9 (for the BBB blocks) |
| 579 |
above. This means that the block is lost, not because there are no |
| 580 |
pointers to it, but rather because all the blocks that point to it are |
| 581 |
themselves lost. For example, if you have a binary tree and the root |
| 582 |
node is lost, all its children nodes will be indirectly lost. Because |
| 583 |
the problem will disappear if the definitely lost block that caused the |
| 584 |
indirect leak is fixed, Memcheck won't report such blocks individually |
| 585 |
by default.</p></li> |
| 586 |
<li class="listitem"><p>"Possibly lost". This covers cases 5--8 (for the BBB blocks) |
| 587 |
above. This means that a chain of one or more pointers to the block has |
| 588 |
been found, but at least one of the pointers is an interior-pointer. |
| 589 |
This could just be a random value in memory that happens to point into a |
| 590 |
block, and so you shouldn't consider this ok unless you know you have |
| 591 |
interior-pointers.</p></li> |
| 592 |
</ul></div> |
| 593 |
<p>(Note: This mapping of the nine possible cases onto four leak kinds is |
| 594 |
not necessarily the best way that leaks could be reported; in particular, |
| 595 |
interior-pointers are treated inconsistently. It is possible the |
| 596 |
categorisation may be improved in the future.)</p> |
| 597 |
<p>Furthermore, if suppressions exists for a block, it will be reported |
| 598 |
as "suppressed" no matter what which of the above four kinds it belongs |
| 599 |
to.</p> |
| 600 |
<p>The following is an example leak summary.</p> |
| 601 |
<pre class="programlisting"> |
| 602 |
LEAK SUMMARY: |
| 603 |
definitely lost: 48 bytes in 3 blocks. |
| 604 |
indirectly lost: 32 bytes in 2 blocks. |
| 605 |
possibly lost: 96 bytes in 6 blocks. |
| 606 |
still reachable: 64 bytes in 4 blocks. |
| 607 |
suppressed: 0 bytes in 0 blocks. |
| 608 |
</pre> |
| 609 |
<p>If heuristics have been used to consider some blocks as |
| 610 |
reachable, the leak summary details the heuristically reachable subset |
| 611 |
of 'still reachable:' per heuristic. In the below example, of the 95 |
| 612 |
bytes still reachable, 87 bytes (56+7+8+16) have been considered |
| 613 |
heuristically reachable. |
| 614 |
</p> |
| 615 |
<pre class="programlisting"> |
| 616 |
LEAK SUMMARY: |
| 617 |
definitely lost: 4 bytes in 1 blocks |
| 618 |
indirectly lost: 0 bytes in 0 blocks |
| 619 |
possibly lost: 0 bytes in 0 blocks |
| 620 |
still reachable: 95 bytes in 6 blocks |
| 621 |
of which reachable via heuristic: |
| 622 |
stdstring : 56 bytes in 2 blocks |
| 623 |
length64 : 16 bytes in 1 blocks |
| 624 |
newarray : 7 bytes in 1 blocks |
| 625 |
multipleinheritance: 8 bytes in 1 blocks |
| 626 |
suppressed: 0 bytes in 0 blocks |
| 627 |
</pre> |
| 628 |
<p>If <code class="option">--leak-check=full</code> is specified, |
| 629 |
Memcheck will give details for each definitely lost or possibly lost block, |
| 630 |
including where it was allocated. (Actually, it merges results for all |
| 631 |
blocks that have the same leak kind and sufficiently similar stack traces |
| 632 |
into a single "loss record". The |
| 633 |
<code class="option">--leak-resolution</code> lets you control the |
| 634 |
meaning of "sufficiently similar".) It cannot tell you when or how or why |
| 635 |
the pointer to a leaked block was lost; you have to work that out for |
| 636 |
yourself. In general, you should attempt to ensure your programs do not |
| 637 |
have any definitely lost or possibly lost blocks at exit.</p> |
| 638 |
<p>For example:</p> |
| 639 |
<pre class="programlisting"> |
| 640 |
8 bytes in 1 blocks are definitely lost in loss record 1 of 14 |
| 641 |
at 0x........: malloc (vg_replace_malloc.c:...) |
| 642 |
by 0x........: mk (leak-tree.c:11) |
| 643 |
by 0x........: main (leak-tree.c:39) |
| 644 |
|
| 645 |
88 (8 direct, 80 indirect) bytes in 1 blocks are definitely lost in loss record 13 of 14 |
| 646 |
at 0x........: malloc (vg_replace_malloc.c:...) |
| 647 |
by 0x........: mk (leak-tree.c:11) |
| 648 |
by 0x........: main (leak-tree.c:25) |
| 649 |
</pre> |
| 650 |
<p>The first message describes a simple case of a single 8 byte block |
| 651 |
that has been definitely lost. The second case mentions another 8 byte |
| 652 |
block that has been definitely lost; the difference is that a further 80 |
| 653 |
bytes in other blocks are indirectly lost because of this lost block. |
| 654 |
The loss records are not presented in any notable order, so the loss record |
| 655 |
numbers aren't particularly meaningful. The loss record numbers can be used |
| 656 |
in the Valgrind gdbserver to list the addresses of the leaked blocks and/or give |
| 657 |
more details about how a block is still reachable.</p> |
| 658 |
<p>The option <code class="option">--show-leak-kinds=<set></code> |
| 659 |
controls the set of leak kinds to show |
| 660 |
when <code class="option">--leak-check=full</code> is specified. </p> |
| 661 |
<p>The <code class="option"><set></code> of leak kinds is specified |
| 662 |
in one of the following ways: |
| 663 |
|
| 664 |
</p> |
| 665 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 666 |
<li class="listitem"><p>a comma separated list of one or more of |
| 667 |
<code class="option">definite indirect possible reachable</code>.</p></li> |
| 668 |
<li class="listitem"><p><code class="option">all</code> to specify the complete set (all leak kinds).</p></li> |
| 669 |
<li class="listitem"><p><code class="option">none</code> for the empty set.</p></li> |
| 670 |
</ul></div> |
| 671 |
<p> |
| 672 |
|
| 673 |
</p> |
| 674 |
<p> The default value for the leak kinds to show is |
| 675 |
<code class="option">--show-leak-kinds=definite,possible</code>. |
| 676 |
</p> |
| 677 |
<p>To also show the reachable and indirectly lost blocks in |
| 678 |
addition to the definitely and possibly lost blocks, you can |
| 679 |
use <code class="option">--show-leak-kinds=all</code>. To only show the |
| 680 |
reachable and indirectly lost blocks, use |
| 681 |
<code class="option">--show-leak-kinds=indirect,reachable</code>. The reachable |
| 682 |
and indirectly lost blocks will then be presented as shown in |
| 683 |
the following two examples.</p> |
| 684 |
<pre class="programlisting"> |
| 685 |
64 bytes in 4 blocks are still reachable in loss record 2 of 4 |
| 686 |
at 0x........: malloc (vg_replace_malloc.c:177) |
| 687 |
by 0x........: mk (leak-cases.c:52) |
| 688 |
by 0x........: main (leak-cases.c:74) |
| 689 |
|
| 690 |
32 bytes in 2 blocks are indirectly lost in loss record 1 of 4 |
| 691 |
at 0x........: malloc (vg_replace_malloc.c:177) |
| 692 |
by 0x........: mk (leak-cases.c:52) |
| 693 |
by 0x........: main (leak-cases.c:80) |
| 694 |
</pre> |
| 695 |
<p>Because there are different kinds of leaks with different |
| 696 |
severities, an interesting question is: which leaks should be |
| 697 |
counted as true "errors" and which should not? |
| 698 |
</p> |
| 699 |
<p> The answer to this question affects the numbers printed in |
| 700 |
the <code class="computeroutput">ERROR SUMMARY</code> line, and also the |
| 701 |
effect of the <code class="option">--error-exitcode</code> option. First, a leak |
| 702 |
is only counted as a true "error" |
| 703 |
if <code class="option">--leak-check=full</code> is specified. Then, the |
| 704 |
option <code class="option">--errors-for-leak-kinds=<set></code> controls |
| 705 |
the set of leak kinds to consider as errors. The default value |
| 706 |
is <code class="option">--errors-for-leak-kinds=definite,possible</code> |
| 707 |
</p> |
| 708 |
</div> |
| 709 |
</div> |
| 710 |
<div class="sect1"> |
| 711 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 712 |
<a name="mc-manual.options"></a>4.3. Memcheck Command-Line Options</h2></div></div></div> |
| 713 |
<div class="variablelist"> |
| 714 |
<a name="mc.opts.list"></a><dl class="variablelist"> |
| 715 |
<dt> |
| 716 |
<a name="opt.leak-check"></a><span class="term"> |
| 717 |
<code class="option">--leak-check=<no|summary|yes|full> [default: summary] </code> |
| 718 |
</span> |
| 719 |
</dt> |
| 720 |
<dd> |
| 721 |
<p>When enabled, search for memory leaks when the client |
| 722 |
program finishes. If set to <code class="varname">summary</code>, it says how |
| 723 |
many leaks occurred. If set to <code class="varname">full</code> or |
| 724 |
<code class="varname">yes</code>, each individual leak will be shown |
| 725 |
in detail and/or counted as an error, as specified by the options |
| 726 |
<code class="option">--show-leak-kinds</code> and |
| 727 |
<code class="option">--errors-for-leak-kinds</code>. </p> |
| 728 |
<p>If <code class="varname">--xml=yes</code> is given, memcheck will |
| 729 |
automatically use the value <code class="varname">--leak-check=full</code>. |
| 730 |
You can use <code class="option">--show-leak-kinds=none</code> to reduce |
| 731 |
the size of the xml output if you are not interested in the leak |
| 732 |
results.</p> |
| 733 |
</dd> |
| 734 |
<dt> |
| 735 |
<a name="opt.leak-resolution"></a><span class="term"> |
| 736 |
<code class="option">--leak-resolution=<low|med|high> [default: high] </code> |
| 737 |
</span> |
| 738 |
</dt> |
| 739 |
<dd> |
| 740 |
<p>When doing leak checking, determines how willing |
| 741 |
Memcheck is to consider different backtraces to |
| 742 |
be the same for the purposes of merging multiple leaks into a single |
| 743 |
leak report. When set to <code class="varname">low</code>, only the first |
| 744 |
two entries need match. When <code class="varname">med</code>, four entries |
| 745 |
have to match. When <code class="varname">high</code>, all entries need to |
| 746 |
match.</p> |
| 747 |
<p>For hardcore leak debugging, you probably want to use |
| 748 |
<code class="option">--leak-resolution=high</code> together with |
| 749 |
<code class="option">--num-callers=40</code> or some such large number. |
| 750 |
</p> |
| 751 |
<p>Note that the <code class="option">--leak-resolution</code> setting |
| 752 |
does not affect Memcheck's ability to find |
| 753 |
leaks. It only changes how the results are presented.</p> |
| 754 |
</dd> |
| 755 |
<dt> |
| 756 |
<a name="opt.show-leak-kinds"></a><span class="term"> |
| 757 |
<code class="option">--show-leak-kinds=<set> [default: definite,possible] </code> |
| 758 |
</span> |
| 759 |
</dt> |
| 760 |
<dd> |
| 761 |
<p>Specifies the leak kinds to show in a <code class="varname">full</code> |
| 762 |
leak search, in one of the following ways: </p> |
| 763 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 764 |
<li class="listitem"><p>a comma separated list of one or more of |
| 765 |
<code class="option">definite indirect possible reachable</code>.</p></li> |
| 766 |
<li class="listitem"><p><code class="option">all</code> to specify the complete set (all leak kinds). |
| 767 |
It is equivalent to |
| 768 |
<code class="option">--show-leak-kinds=definite,indirect,possible,reachable</code>.</p></li> |
| 769 |
<li class="listitem"><p><code class="option">none</code> for the empty set.</p></li> |
| 770 |
</ul></div> |
| 771 |
</dd> |
| 772 |
<dt> |
| 773 |
<a name="opt.errors-for-leak-kinds"></a><span class="term"> |
| 774 |
<code class="option">--errors-for-leak-kinds=<set> [default: definite,possible] </code> |
| 775 |
</span> |
| 776 |
</dt> |
| 777 |
<dd><p>Specifies the leak kinds to count as errors in a |
| 778 |
<code class="varname">full</code> leak search. The |
| 779 |
<code class="option"><set></code> is specified similarly to |
| 780 |
<code class="option">--show-leak-kinds</code> |
| 781 |
</p></dd> |
| 782 |
<dt> |
| 783 |
<a name="opt.leak-check-heuristics"></a><span class="term"> |
| 784 |
<code class="option">--leak-check-heuristics=<set> [default: all] </code> |
| 785 |
</span> |
| 786 |
</dt> |
| 787 |
<dd> |
| 788 |
<p>Specifies the set of leak check heuristics to be used |
| 789 |
during leak searches. The heuristics control which interior pointers |
| 790 |
to a block cause it to be considered as reachable. |
| 791 |
The heuristic set is specified in one of the following ways:</p> |
| 792 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 793 |
<li class="listitem"><p>a comma separated list of one or more of |
| 794 |
<code class="option">stdstring length64 newarray multipleinheritance</code>.</p></li> |
| 795 |
<li class="listitem"><p><code class="option">all</code> to activate the complete set of |
| 796 |
heuristics. |
| 797 |
It is equivalent to |
| 798 |
<code class="option">--leak-check-heuristics=stdstring,length64,newarray,multipleinheritance</code>.</p></li> |
| 799 |
<li class="listitem"><p><code class="option">none</code> for the empty set.</p></li> |
| 800 |
</ul></div> |
| 801 |
<p>Note that these heuristics are dependent on the layout of |
| 802 |
the objects produced by the C++ compiler. They have been |
| 803 |
tested with some gcc versions (e.g. 4.4 and 4.7). They might |
| 804 |
not work properly with other C++ compilers. |
| 805 |
</p> |
| 806 |
</dd> |
| 807 |
<dt> |
| 808 |
<a name="opt.show-reachable"></a><span class="term"> |
| 809 |
<code class="option">--show-reachable=<yes|no> </code> |
| 810 |
, </span><span class="term"> |
| 811 |
<code class="option">--show-possibly-lost=<yes|no> </code> |
| 812 |
</span> |
| 813 |
</dt> |
| 814 |
<dd> |
| 815 |
<p>These options provide an alternative way to specify the leak kinds to show: |
| 816 |
</p> |
| 817 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 818 |
<li class="listitem"><p> |
| 819 |
<code class="option">--show-reachable=no --show-possibly-lost=yes</code> is equivalent to |
| 820 |
<code class="option">--show-leak-kinds=definite,possible</code>. |
| 821 |
</p></li> |
| 822 |
<li class="listitem"><p> |
| 823 |
<code class="option">--show-reachable=no --show-possibly-lost=no</code> is equivalent to |
| 824 |
<code class="option">--show-leak-kinds=definite</code>. |
| 825 |
</p></li> |
| 826 |
<li class="listitem"><p> |
| 827 |
<code class="option">--show-reachable=yes</code> is equivalent to |
| 828 |
<code class="option">--show-leak-kinds=all</code>. |
| 829 |
</p></li> |
| 830 |
</ul></div> |
| 831 |
<p> Note that <code class="option">--show-possibly-lost=no</code> has no |
| 832 |
effect if <code class="option">--show-reachable=yes</code> is |
| 833 |
specified.</p> |
| 834 |
</dd> |
| 835 |
<dt> |
| 836 |
<a name="opt.xtree-leak"></a><span class="term"> |
| 837 |
<code class="option">--xtree-leak=<no|yes> [no] </code> |
| 838 |
</span> |
| 839 |
</dt> |
| 840 |
<dd> |
| 841 |
<p>If set to yes, the results for the leak search done at exit will be |
| 842 |
output in a 'Callgrind Format' execution tree file. Note that this |
| 843 |
automatically sets the options <code class="option">--leak-check=full</code> |
| 844 |
and <code class="option">--show-leak-kinds=all</code>, to allow |
| 845 |
xtree visualisation tools such as kcachegrind to select what kind |
| 846 |
to leak to visualize. |
| 847 |
The produced file will contain the following events:</p> |
| 848 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 849 |
<li class="listitem"><p><code class="option">RB</code> : Reachable Bytes</p></li> |
| 850 |
<li class="listitem"><p><code class="option">PB</code> : Possibly lost Bytes</p></li> |
| 851 |
<li class="listitem"><p><code class="option">IB</code> : Indirectly lost Bytes</p></li> |
| 852 |
<li class="listitem"><p><code class="option">DB</code> : Definitely lost Bytes (direct plus indirect)</p></li> |
| 853 |
<li class="listitem"><p><code class="option">DIB</code> : Definitely Indirectly lost Bytes (subset of DB)</p></li> |
| 854 |
<li class="listitem"><p><code class="option">RBk</code> : reachable Blocks</p></li> |
| 855 |
<li class="listitem"><p><code class="option">PBk</code> : Possibly lost Blocks</p></li> |
| 856 |
<li class="listitem"><p><code class="option">IBk</code> : Indirectly lost Blocks</p></li> |
| 857 |
<li class="listitem"><p><code class="option">DBk</code> : Definitely lost Blocks</p></li> |
| 858 |
</ul></div> |
| 859 |
<p>The increase or decrease for all events above will also be output in |
| 860 |
the file to provide the delta (increase or decrease) between 2 |
| 861 |
successive leak searches. For example, <code class="option">iRB</code> is the |
| 862 |
increase of the <code class="option">RB</code> event, <code class="option">dPBk</code> is the |
| 863 |
decrease of <code class="option">PBk</code> event. The values for the increase and |
| 864 |
decrease events will be zero for the first leak search done.</p> |
| 865 |
<p>See <a class="xref" href="manual-core.html#manual-core.xtree" title="2.10. Execution Trees">Execution Trees</a> for a detailed explanation |
| 866 |
about execution trees.</p> |
| 867 |
</dd> |
| 868 |
<dt> |
| 869 |
<a name="opt.xtree-leak-file"></a><span class="term"> |
| 870 |
<code class="option">--xtree-leak-file=<filename> [default: |
| 871 |
xtleak.kcg.%p] </code> |
| 872 |
</span> |
| 873 |
</dt> |
| 874 |
<dd> |
| 875 |
<p>Specifies that Valgrind should produce the xtree leak |
| 876 |
report in the specified file. Any <code class="option">%p</code>, |
| 877 |
<code class="option">%q</code> or <code class="option">%n</code> sequences appearing in |
| 878 |
the filename are expanded |
| 879 |
in exactly the same way as they are for <code class="option">--log-file</code>. |
| 880 |
See the description of <a class="xref" href="manual-core.html#opt.log-file">--log-file</a> |
| 881 |
for details. </p> |
| 882 |
<p>See <a class="xref" href="manual-core.html#manual-core.xtree" title="2.10. Execution Trees">Execution Trees</a> |
| 883 |
for a detailed explanation about execution trees formats. </p> |
| 884 |
</dd> |
| 885 |
<dt> |
| 886 |
<a name="opt.undef-value-errors"></a><span class="term"> |
| 887 |
<code class="option">--undef-value-errors=<yes|no> [default: yes] </code> |
| 888 |
</span> |
| 889 |
</dt> |
| 890 |
<dd><p>Controls whether Memcheck reports |
| 891 |
uses of undefined value errors. Set this to |
| 892 |
<code class="varname">no</code> if you don't want to see undefined value |
| 893 |
errors. It also has the side effect of speeding up Memcheck somewhat. |
| 894 |
AddrCheck (removed in Valgrind 3.1.0) functioned like Memcheck with |
| 895 |
<code class="option">--undef-value-errors=no</code>. |
| 896 |
</p></dd> |
| 897 |
<dt> |
| 898 |
<a name="opt.track-origins"></a><span class="term"> |
| 899 |
<code class="option">--track-origins=<yes|no> [default: no] </code> |
| 900 |
</span> |
| 901 |
</dt> |
| 902 |
<dd> |
| 903 |
<p>Controls whether Memcheck tracks |
| 904 |
the origin of uninitialised values. By default, it does not, |
| 905 |
which means that although it can tell you that an |
| 906 |
uninitialised value is being used in a dangerous way, it |
| 907 |
cannot tell you where the uninitialised value came from. This |
| 908 |
often makes it difficult to track down the root problem. |
| 909 |
</p> |
| 910 |
<p>When set |
| 911 |
to <code class="varname">yes</code>, Memcheck keeps |
| 912 |
track of the origins of all uninitialised values. Then, when |
| 913 |
an uninitialised value error is |
| 914 |
reported, Memcheck will try to show the |
| 915 |
origin of the value. An origin can be one of the following |
| 916 |
four places: a heap block, a stack allocation, a client |
| 917 |
request, or miscellaneous other sources (eg, a call |
| 918 |
to <code class="varname">brk</code>). |
| 919 |
</p> |
| 920 |
<p>For uninitialised values originating from a heap |
| 921 |
block, Memcheck shows where the block was |
| 922 |
allocated. For uninitialised values originating from a stack |
| 923 |
allocation, Memcheck can tell you which |
| 924 |
function allocated the value, but no more than that -- typically |
| 925 |
it shows you the source location of the opening brace of the |
| 926 |
function. So you should carefully check that all of the |
| 927 |
function's local variables are initialised properly. |
| 928 |
</p> |
| 929 |
<p>Performance overhead: origin tracking is expensive. It |
| 930 |
halves Memcheck's speed and increases |
| 931 |
memory use by a minimum of 100MB, and possibly more. |
| 932 |
Nevertheless it can drastically reduce the effort required to |
| 933 |
identify the root cause of uninitialised value errors, and so |
| 934 |
is often a programmer productivity win, despite running |
| 935 |
more slowly. |
| 936 |
</p> |
| 937 |
<p>Accuracy: Memcheck tracks origins |
| 938 |
quite accurately. To avoid very large space and time |
| 939 |
overheads, some approximations are made. It is possible, |
| 940 |
although unlikely, that Memcheck will report an incorrect origin, or |
| 941 |
not be able to identify any origin. |
| 942 |
</p> |
| 943 |
<p>Note that the combination |
| 944 |
<code class="option">--track-origins=yes</code> |
| 945 |
and <code class="option">--undef-value-errors=no</code> is |
| 946 |
nonsensical. Memcheck checks for and |
| 947 |
rejects this combination at startup. |
| 948 |
</p> |
| 949 |
</dd> |
| 950 |
<dt> |
| 951 |
<a name="opt.partial-loads-ok"></a><span class="term"> |
| 952 |
<code class="option">--partial-loads-ok=<yes|no> [default: yes] </code> |
| 953 |
</span> |
| 954 |
</dt> |
| 955 |
<dd> |
| 956 |
<p>Controls how Memcheck handles 32-, 64-, 128- and 256-bit |
| 957 |
naturally aligned loads from addresses for which some bytes are |
| 958 |
addressable and others are not. When <code class="varname">yes</code>, such |
| 959 |
loads do not produce an address error. Instead, loaded bytes |
| 960 |
originating from illegal addresses are marked as uninitialised, and |
| 961 |
those corresponding to legal addresses are handled in the normal |
| 962 |
way.</p> |
| 963 |
<p>When <code class="varname">no</code>, loads from partially invalid |
| 964 |
addresses are treated the same as loads from completely invalid |
| 965 |
addresses: an illegal-address error is issued, and the resulting |
| 966 |
bytes are marked as initialised.</p> |
| 967 |
<p>Note that code that behaves in this way is in violation of |
| 968 |
the ISO C/C++ standards, and should be considered broken. If |
| 969 |
at all possible, such code should be fixed.</p> |
| 970 |
</dd> |
| 971 |
<dt> |
| 972 |
<a name="opt.expensive-definedness-checks"></a><span class="term"> |
| 973 |
<code class="option">--expensive-definedness-checks=<no|auto|yes> [default: auto] </code> |
| 974 |
</span> |
| 975 |
</dt> |
| 976 |
<dd> |
| 977 |
<p>Controls whether Memcheck should employ more precise but also |
| 978 |
more expensive (time consuming) instrumentation when checking the |
| 979 |
definedness of certain values. In particular, this affects the |
| 980 |
instrumentation of integer adds, subtracts and equality |
| 981 |
comparisons.</p> |
| 982 |
<p>Selecting <code class="option">--expensive-definedness-checks=yes</code> |
| 983 |
causes Memcheck to use the most accurate analysis possible. This |
| 984 |
minimises false error rates but can cause up to 30% performance |
| 985 |
degradation.</p> |
| 986 |
<p>Selecting <code class="option">--expensive-definedness-checks=no</code> |
| 987 |
causes Memcheck to use the cheapest instrumentation possible. This |
| 988 |
maximises performance but will normally give an unusably high false |
| 989 |
error rate.</p> |
| 990 |
<p>The default |
| 991 |
setting, <code class="option">--expensive-definedness-checks=auto</code>, is |
| 992 |
strongly recommended. This causes Memcheck to use the minimum of |
| 993 |
expensive instrumentation needed to achieve the same false error |
| 994 |
rate as <code class="option">--expensive-definedness-checks=yes</code>. It |
| 995 |
also enables an instrumentation-time analysis pass which aims to |
| 996 |
further reduce the costs of accurate instrumentation. Overall, the |
| 997 |
performance loss is generally around 5% relative to |
| 998 |
<code class="option">--expensive-definedness-checks=no</code>, although this is |
| 999 |
strongly workload dependent. Note that the exact instrumentation |
| 1000 |
settings in this mode are architecture dependent.</p> |
| 1001 |
</dd> |
| 1002 |
<dt> |
| 1003 |
<a name="opt.keep-stacktraces"></a><span class="term"> |
| 1004 |
<code class="option">--keep-stacktraces=alloc|free|alloc-and-free|alloc-then-free|none [default: alloc-and-free] </code> |
| 1005 |
</span> |
| 1006 |
</dt> |
| 1007 |
<dd> |
| 1008 |
<p>Controls which stack trace(s) to keep for malloc'd and/or |
| 1009 |
free'd blocks. |
| 1010 |
</p> |
| 1011 |
<p>With <code class="varname">alloc-then-free</code>, a stack trace is |
| 1012 |
recorded at allocation time, and is associated with the block. |
| 1013 |
When the block is freed, a second stack trace is recorded, and |
| 1014 |
this replaces the allocation stack trace. As a result, any "use |
| 1015 |
after free" errors relating to this block can only show a stack |
| 1016 |
trace for where the block was freed. |
| 1017 |
</p> |
| 1018 |
<p>With <code class="varname">alloc-and-free</code>, both allocation |
| 1019 |
and the deallocation stack traces for the block are stored. |
| 1020 |
Hence a "use after free" error will |
| 1021 |
show both, which may make the error easier to diagnose. |
| 1022 |
Compared to <code class="varname">alloc-then-free</code>, this setting |
| 1023 |
slightly increases Valgrind's memory use as the block contains two |
| 1024 |
references instead of one. |
| 1025 |
</p> |
| 1026 |
<p>With <code class="varname">alloc</code>, only the allocation stack |
| 1027 |
trace is recorded (and reported). With <code class="varname">free</code>, |
| 1028 |
only the deallocation stack trace is recorded (and reported). |
| 1029 |
These values somewhat decrease Valgrind's memory and cpu usage. |
| 1030 |
They can be useful depending on the error types you are |
| 1031 |
searching for and the level of detail you need to analyse |
| 1032 |
them. For example, if you are only interested in memory leak |
| 1033 |
errors, it is sufficient to record the allocation stack traces. |
| 1034 |
</p> |
| 1035 |
<p>With <code class="varname">none</code>, no stack traces are recorded |
| 1036 |
for malloc and free operations. If your program allocates a lot |
| 1037 |
of blocks and/or allocates/frees from many different stack |
| 1038 |
traces, this can significantly decrease cpu and/or memory |
| 1039 |
required. Of course, few details will be reported for errors |
| 1040 |
related to heap blocks. |
| 1041 |
</p> |
| 1042 |
<p>Note that once a stack trace is recorded, Valgrind keeps |
| 1043 |
the stack trace in memory even if it is not referenced by any |
| 1044 |
block. Some programs (for example, recursive algorithms) can |
| 1045 |
generate a huge number of stack traces. If Valgrind uses too |
| 1046 |
much memory in such circumstances, you can reduce the memory |
| 1047 |
required with the options <code class="varname">--keep-stacktraces</code> |
| 1048 |
and/or by using a smaller value for the |
| 1049 |
option <code class="varname">--num-callers</code>. |
| 1050 |
</p> |
| 1051 |
<p>If you want to use |
| 1052 |
<code class="computeroutput">--xtree-memory=full</code> memory profiling |
| 1053 |
(see <a class="xref" href="manual-core.html#manual-core.xtree" title="2.10. Execution Trees">Execution Trees</a>), then you cannot |
| 1054 |
specify <code class="varname">--keep-stacktraces=free</code> |
| 1055 |
or <code class="varname">--keep-stacktraces=none</code>.</p> |
| 1056 |
</dd> |
| 1057 |
<dt> |
| 1058 |
<a name="opt.freelist-vol"></a><span class="term"> |
| 1059 |
<code class="option">--freelist-vol=<number> [default: 20000000] </code> |
| 1060 |
</span> |
| 1061 |
</dt> |
| 1062 |
<dd> |
| 1063 |
<p>When the client program releases memory using |
| 1064 |
<code class="function">free</code> (in <code class="literal">C</code>) or |
| 1065 |
<code class="computeroutput">delete</code> |
| 1066 |
(<code class="literal">C++</code>), that memory is not immediately made |
| 1067 |
available for re-allocation. Instead, it is marked inaccessible |
| 1068 |
and placed in a queue of freed blocks. The purpose is to defer as |
| 1069 |
long as possible the point at which freed-up memory comes back |
| 1070 |
into circulation. This increases the chance that |
| 1071 |
Memcheck will be able to detect invalid |
| 1072 |
accesses to blocks for some significant period of time after they |
| 1073 |
have been freed.</p> |
| 1074 |
<p>This option specifies the maximum total size, in bytes, of the |
| 1075 |
blocks in the queue. The default value is twenty million bytes. |
| 1076 |
Increasing this increases the total amount of memory used by |
| 1077 |
Memcheck but may detect invalid uses of freed |
| 1078 |
blocks which would otherwise go undetected.</p> |
| 1079 |
</dd> |
| 1080 |
<dt> |
| 1081 |
<a name="opt.freelist-big-blocks"></a><span class="term"> |
| 1082 |
<code class="option">--freelist-big-blocks=<number> [default: 1000000] </code> |
| 1083 |
</span> |
| 1084 |
</dt> |
| 1085 |
<dd> |
| 1086 |
<p>When making blocks from the queue of freed blocks available |
| 1087 |
for re-allocation, Memcheck will in priority re-circulate the blocks |
| 1088 |
with a size greater or equal to <code class="option">--freelist-big-blocks</code>. |
| 1089 |
This ensures that freeing big blocks (in particular freeing blocks bigger than |
| 1090 |
<code class="option">--freelist-vol</code>) does not immediately lead to a re-circulation |
| 1091 |
of all (or a lot of) the small blocks in the free list. In other words, |
| 1092 |
this option increases the likelihood to discover dangling pointers |
| 1093 |
for the "small" blocks, even when big blocks are freed.</p> |
| 1094 |
<p>Setting a value of 0 means that all the blocks are re-circulated |
| 1095 |
in a FIFO order. </p> |
| 1096 |
</dd> |
| 1097 |
<dt> |
| 1098 |
<a name="opt.workaround-gcc296-bugs"></a><span class="term"> |
| 1099 |
<code class="option">--workaround-gcc296-bugs=<yes|no> [default: no] </code> |
| 1100 |
</span> |
| 1101 |
</dt> |
| 1102 |
<dd> |
| 1103 |
<p>When enabled, assume that reads and writes some small |
| 1104 |
distance below the stack pointer are due to bugs in GCC 2.96, and |
| 1105 |
does not report them. The "small distance" is 256 bytes by |
| 1106 |
default. Note that GCC 2.96 is the default compiler on some ancient |
| 1107 |
Linux distributions (RedHat 7.X) and so you may need to use this |
| 1108 |
option. Do not use it if you do not have to, as it can cause real |
| 1109 |
errors to be overlooked. A better alternative is to use a more |
| 1110 |
recent GCC in which this bug is fixed.</p> |
| 1111 |
<p>You may also need to use this option when working with |
| 1112 |
GCC 3.X or 4.X on 32-bit PowerPC Linux. This is because |
| 1113 |
GCC generates code which occasionally accesses below the |
| 1114 |
stack pointer, particularly for floating-point to/from integer |
| 1115 |
conversions. This is in violation of the 32-bit PowerPC ELF |
| 1116 |
specification, which makes no provision for locations below the |
| 1117 |
stack pointer to be accessible.</p> |
| 1118 |
<p>This option is deprecated as of version 3.12 and may be |
| 1119 |
removed from future versions. You should instead use |
| 1120 |
<code class="option">--ignore-range-below-sp</code> to specify the exact |
| 1121 |
range of offsets below the stack pointer that should be ignored. |
| 1122 |
A suitable equivalent |
| 1123 |
is <code class="option">--ignore-range-below-sp=1024-1</code>. |
| 1124 |
</p> |
| 1125 |
</dd> |
| 1126 |
<dt> |
| 1127 |
<a name="opt.ignore-range-below-sp"></a><span class="term"> |
| 1128 |
<code class="option">--ignore-range-below-sp=<number>-<number> </code> |
| 1129 |
</span> |
| 1130 |
</dt> |
| 1131 |
<dd><p>This is a more general replacement for the deprecated |
| 1132 |
<code class="option">--workaround-gcc296-bugs</code> option. When |
| 1133 |
specified, it causes Memcheck not to report errors for accesses |
| 1134 |
at the specified offsets below the stack pointer. The two |
| 1135 |
offsets must be positive decimal numbers and -- somewhat |
| 1136 |
counterintuitively -- the first one must be larger, in order to |
| 1137 |
imply a non-wraparound address range to ignore. For example, |
| 1138 |
to ignore 4 byte accesses at 8192 bytes below the stack |
| 1139 |
pointer, |
| 1140 |
use <code class="option">--ignore-range-below-sp=8192-8189</code>. Only |
| 1141 |
one range may be specified. |
| 1142 |
</p></dd> |
| 1143 |
<dt> |
| 1144 |
<a name="opt.show-mismatched-frees"></a><span class="term"> |
| 1145 |
<code class="option">--show-mismatched-frees=<yes|no> [default: yes] </code> |
| 1146 |
</span> |
| 1147 |
</dt> |
| 1148 |
<dd> |
| 1149 |
<p>When enabled, Memcheck checks that heap blocks are |
| 1150 |
deallocated using a function that matches the allocating |
| 1151 |
function. That is, it expects <code class="varname">free</code> to be |
| 1152 |
used to deallocate blocks allocated |
| 1153 |
by <code class="varname">malloc</code>, <code class="varname">delete</code> for |
| 1154 |
blocks allocated by <code class="varname">new</code>, |
| 1155 |
and <code class="varname">delete[]</code> for blocks allocated |
| 1156 |
by <code class="varname">new[]</code>. If a mismatch is detected, an |
| 1157 |
error is reported. This is in general important because in some |
| 1158 |
environments, freeing with a non-matching function can cause |
| 1159 |
crashes.</p> |
| 1160 |
<p>There is however a scenario where such mismatches cannot |
| 1161 |
be avoided. That is when the user provides implementations of |
| 1162 |
<code class="varname">new</code>/<code class="varname">new[]</code> that |
| 1163 |
call <code class="varname">malloc</code> and |
| 1164 |
of <code class="varname">delete</code>/<code class="varname">delete[]</code> that |
| 1165 |
call <code class="varname">free</code>, and these functions are |
| 1166 |
asymmetrically inlined. For example, imagine |
| 1167 |
that <code class="varname">delete[]</code> is inlined |
| 1168 |
but <code class="varname">new[]</code> is not. The result is that |
| 1169 |
Memcheck "sees" all <code class="varname">delete[]</code> calls as direct |
| 1170 |
calls to <code class="varname">free</code>, even when the program source |
| 1171 |
contains no mismatched calls.</p> |
| 1172 |
<p>This causes a lot of confusing and irrelevant error |
| 1173 |
reports. <code class="varname">--show-mismatched-frees=no</code> disables |
| 1174 |
these checks. It is not generally advisable to disable them, |
| 1175 |
though, because you may miss real errors as a result.</p> |
| 1176 |
</dd> |
| 1177 |
<dt> |
| 1178 |
<a name="opt.show-realloc-size-zero"></a><span class="term"> |
| 1179 |
<code class="option">--show-realloc-size-zero=<yes|no> [default: yes] </code> |
| 1180 |
</span> |
| 1181 |
</dt> |
| 1182 |
<dd><p>When enabled, Memcheck checks for uses of <code class="varname">realloc</code> with a size of zero. |
| 1183 |
This usage of <code class="varname">realloc</code> is unsafe since it is not portable. On some systems it |
| 1184 |
will behave like <code class="varname">free</code>. On other systems it will either do nothing or else |
| 1185 |
behave like a call to <code class="varname">free</code> followed by a call to <code class="varname">malloc</code> |
| 1186 |
with a size of zero.</p></dd> |
| 1187 |
<dt> |
| 1188 |
<a name="opt.ignore-ranges"></a><span class="term"> |
| 1189 |
<code class="option">--ignore-ranges=0xPP-0xQQ[,0xRR-0xSS] </code> |
| 1190 |
</span> |
| 1191 |
</dt> |
| 1192 |
<dd><p>Any ranges listed in this option (and multiple ranges can be |
| 1193 |
specified, separated by commas) will be ignored by Memcheck's |
| 1194 |
addressability checking.</p></dd> |
| 1195 |
<dt> |
| 1196 |
<a name="opt.malloc-fill"></a><span class="term"> |
| 1197 |
<code class="option">--malloc-fill=<hexnumber> </code> |
| 1198 |
</span> |
| 1199 |
</dt> |
| 1200 |
<dd><p>Fills blocks allocated |
| 1201 |
by <code class="computeroutput">malloc</code>, |
| 1202 |
<code class="computeroutput">new</code>, etc, but not |
| 1203 |
by <code class="computeroutput">calloc</code>, with the specified |
| 1204 |
byte. This can be useful when trying to shake out obscure |
| 1205 |
memory corruption problems. The allocated area is still |
| 1206 |
regarded by Memcheck as undefined -- this option only affects its |
| 1207 |
contents. Note that <code class="option">--malloc-fill</code> does not |
| 1208 |
affect a block of memory when it is used as argument |
| 1209 |
to client requests VALGRIND_MEMPOOL_ALLOC or |
| 1210 |
VALGRIND_MALLOCLIKE_BLOCK. |
| 1211 |
</p></dd> |
| 1212 |
<dt> |
| 1213 |
<a name="opt.free-fill"></a><span class="term"> |
| 1214 |
<code class="option">--free-fill=<hexnumber> </code> |
| 1215 |
</span> |
| 1216 |
</dt> |
| 1217 |
<dd><p>Fills blocks freed |
| 1218 |
by <code class="computeroutput">free</code>, |
| 1219 |
<code class="computeroutput">delete</code>, etc, with the |
| 1220 |
specified byte value. This can be useful when trying to shake out |
| 1221 |
obscure memory corruption problems. The freed area is still |
| 1222 |
regarded by Memcheck as not valid for access -- this option only |
| 1223 |
affects its contents. Note that <code class="option">--free-fill</code> does not |
| 1224 |
affect a block of memory when it is used as argument to |
| 1225 |
client requests VALGRIND_MEMPOOL_FREE or VALGRIND_FREELIKE_BLOCK. |
| 1226 |
</p></dd> |
| 1227 |
</dl> |
| 1228 |
</div> |
| 1229 |
</div> |
| 1230 |
<div class="sect1"> |
| 1231 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 1232 |
<a name="mc-manual.suppfiles"></a>4.4. Writing suppression files</h2></div></div></div> |
| 1233 |
<p>The basic suppression format is described in |
| 1234 |
<a class="xref" href="manual-core.html#manual-core.suppress" title="2.5. Suppressing errors">Suppressing errors</a>.</p> |
| 1235 |
<p>The suppression-type (second) line should have the form:</p> |
| 1236 |
<pre class="programlisting"> |
| 1237 |
Memcheck:suppression_type</pre> |
| 1238 |
<p>The Memcheck suppression types are as follows:</p> |
| 1239 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 1240 |
<li class="listitem"><p><code class="varname">Value1</code>, |
| 1241 |
<code class="varname">Value2</code>, |
| 1242 |
<code class="varname">Value4</code>, |
| 1243 |
<code class="varname">Value8</code>, |
| 1244 |
<code class="varname">Value16</code>, |
| 1245 |
meaning an uninitialised-value error when |
| 1246 |
using a value of 1, 2, 4, 8 or 16 bytes.</p></li> |
| 1247 |
<li class="listitem"><p><code class="varname">Cond</code> (or its old |
| 1248 |
name, <code class="varname">Value0</code>), meaning use |
| 1249 |
of an uninitialised CPU condition code.</p></li> |
| 1250 |
<li class="listitem"><p><code class="varname">Addr1</code>, |
| 1251 |
<code class="varname">Addr2</code>, |
| 1252 |
<code class="varname">Addr4</code>, |
| 1253 |
<code class="varname">Addr8</code>, |
| 1254 |
<code class="varname">Addr16</code>, |
| 1255 |
meaning an invalid address during a |
| 1256 |
memory access of 1, 2, 4, 8 or 16 bytes respectively.</p></li> |
| 1257 |
<li class="listitem"><p><code class="varname">Jump</code>, meaning an |
| 1258 |
jump to an unaddressable location error.</p></li> |
| 1259 |
<li class="listitem"><p><code class="varname">Param</code>, meaning an |
| 1260 |
invalid system call parameter error.</p></li> |
| 1261 |
<li class="listitem"><p><code class="varname">Free</code>, meaning an |
| 1262 |
invalid or mismatching free.</p></li> |
| 1263 |
<li class="listitem"><p><code class="varname">Overlap</code>, meaning a |
| 1264 |
<code class="computeroutput">src</code> / |
| 1265 |
<code class="computeroutput">dst</code> overlap in |
| 1266 |
<code class="function">memcpy</code> or a similar function.</p></li> |
| 1267 |
<li class="listitem"><p><code class="varname">Leak</code>, meaning |
| 1268 |
a memory leak.</p></li> |
| 1269 |
</ul></div> |
| 1270 |
<p><code class="computeroutput">Param</code> errors have a mandatory extra |
| 1271 |
information line at this point, which is the name of the offending |
| 1272 |
system call parameter. </p> |
| 1273 |
<p><code class="computeroutput">Leak</code> errors have an optional |
| 1274 |
extra information line, with the following format:</p> |
| 1275 |
<pre class="programlisting"> |
| 1276 |
match-leak-kinds:<set></pre> |
| 1277 |
<p>where <code class="computeroutput"><set></code> specifies which |
| 1278 |
leak kinds are matched by this suppression entry. |
| 1279 |
<code class="computeroutput"><set></code> is specified in the |
| 1280 |
same way as with the option <code class="option">--show-leak-kinds</code>, that is, |
| 1281 |
one of the following:</p> |
| 1282 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 1283 |
<li class="listitem"><p>a comma separated list of one or more of |
| 1284 |
<code class="option">definite indirect possible reachable</code>.</p></li> |
| 1285 |
<li class="listitem"><p><code class="option">all</code> to specify the complete set |
| 1286 |
(all leak kinds).</p></li> |
| 1287 |
<li class="listitem"><p><code class="option">none</code> for the empty set.</p></li> |
| 1288 |
</ul></div> |
| 1289 |
<p>If this optional extra line is not present, the suppression |
| 1290 |
entry will match all leak kinds.</p> |
| 1291 |
<p>Be aware that leak suppressions that are created using |
| 1292 |
<code class="option">--gen-suppressions</code> will contain this optional extra |
| 1293 |
line, and therefore may match fewer leaks than you expect. You may |
| 1294 |
want to remove the line before using the generated |
| 1295 |
suppressions.</p> |
| 1296 |
<p>The other Memcheck error kinds do not have extra lines.</p> |
| 1297 |
<p> |
| 1298 |
If you give the <code class="option">-v</code> option, Valgrind will print |
| 1299 |
the list of used suppressions at the end of execution. |
| 1300 |
For a leak suppression, this output gives the number of different |
| 1301 |
loss records that match the suppression, and the number of bytes |
| 1302 |
and blocks suppressed by the suppression. |
| 1303 |
If the run contains multiple leak checks, the number of bytes and blocks |
| 1304 |
are reset to zero before each new leak check. Note that the number of different |
| 1305 |
loss records is not reset to zero.</p> |
| 1306 |
<p>In the example below, in the last leak search, 7 blocks and 96 bytes have |
| 1307 |
been suppressed by a suppression with the name |
| 1308 |
<code class="option">some_leak_suppression</code>:</p> |
| 1309 |
<pre class="programlisting"> |
| 1310 |
--21041-- used_suppression: 10 some_other_leak_suppression s.supp:14 suppressed: 12,400 bytes in 1 blocks |
| 1311 |
--21041-- used_suppression: 39 some_leak_suppression s.supp:2 suppressed: 96 bytes in 7 blocks |
| 1312 |
</pre> |
| 1313 |
<p>For <code class="varname">ValueN</code> and <code class="varname">AddrN</code> |
| 1314 |
errors, the first line of the calling context is either the name of |
| 1315 |
the function in which the error occurred, or, failing that, the full |
| 1316 |
path of the <code class="filename">.so</code> file or executable containing the |
| 1317 |
error location. For <code class="varname">Free</code> errors, the first line is |
| 1318 |
the name of the function doing the freeing (eg, |
| 1319 |
<code class="function">free</code>, <code class="function">__builtin_vec_delete</code>, |
| 1320 |
etc). For <code class="varname">Overlap</code> errors, the first line is the name of the |
| 1321 |
function with the overlapping arguments (eg. |
| 1322 |
<code class="function">memcpy</code>, <code class="function">strcpy</code>, etc).</p> |
| 1323 |
<p>The last part of any suppression specifies the rest of the |
| 1324 |
calling context that needs to be matched.</p> |
| 1325 |
</div> |
| 1326 |
<div class="sect1"> |
| 1327 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 1328 |
<a name="mc-manual.machine"></a>4.5. Details of Memcheck's checking machinery</h2></div></div></div> |
| 1329 |
<p>Read this section if you want to know, in detail, exactly |
| 1330 |
what and how Memcheck is checking.</p> |
| 1331 |
<div class="sect2"> |
| 1332 |
<div class="titlepage"><div><div><h3 class="title"> |
| 1333 |
<a name="mc-manual.value"></a>4.5.1. Valid-value (V) bits</h3></div></div></div> |
| 1334 |
<p>It is simplest to think of Memcheck implementing a synthetic CPU |
| 1335 |
which is identical to a real CPU, except for one crucial detail. Every |
| 1336 |
bit (literally) of data processed, stored and handled by the real CPU |
| 1337 |
has, in the synthetic CPU, an associated "valid-value" bit, which says |
| 1338 |
whether or not the accompanying bit has a legitimate value. In the |
| 1339 |
discussions which follow, this bit is referred to as the V (valid-value) |
| 1340 |
bit.</p> |
| 1341 |
<p>Each byte in the system therefore has a 8 V bits which follow it |
| 1342 |
wherever it goes. For example, when the CPU loads a word-size item (4 |
| 1343 |
bytes) from memory, it also loads the corresponding 32 V bits from a |
| 1344 |
bitmap which stores the V bits for the process' entire address space. |
| 1345 |
If the CPU should later write the whole or some part of that value to |
| 1346 |
memory at a different address, the relevant V bits will be stored back |
| 1347 |
in the V-bit bitmap.</p> |
| 1348 |
<p>In short, each bit in the system has (conceptually) an associated V |
| 1349 |
bit, which follows it around everywhere, even inside the CPU. Yes, all the |
| 1350 |
CPU's registers (integer, floating point, vector and condition registers) |
| 1351 |
have their own V bit vectors. For this to work, Memcheck uses a great deal |
| 1352 |
of compression to represent the V bits compactly.</p> |
| 1353 |
<p>Copying values around does not cause Memcheck to check for, or |
| 1354 |
report on, errors. However, when a value is used in a way which might |
| 1355 |
conceivably affect your program's externally-visible behaviour, |
| 1356 |
the associated V bits are immediately checked. If any of these indicate |
| 1357 |
that the value is undefined (even partially), an error is reported.</p> |
| 1358 |
<p>Here's an (admittedly nonsensical) example:</p> |
| 1359 |
<pre class="programlisting"> |
| 1360 |
int i, j; |
| 1361 |
int a[10], b[10]; |
| 1362 |
for ( i = 0; i < 10; i++ ) { |
| 1363 |
j = a[i]; |
| 1364 |
b[i] = j; |
| 1365 |
}</pre> |
| 1366 |
<p>Memcheck emits no complaints about this, since it merely copies |
| 1367 |
uninitialised values from <code class="varname">a[]</code> into |
| 1368 |
<code class="varname">b[]</code>, and doesn't use them in a way which could |
| 1369 |
affect the behaviour of the program. However, if |
| 1370 |
the loop is changed to:</p> |
| 1371 |
<pre class="programlisting"> |
| 1372 |
for ( i = 0; i < 10; i++ ) { |
| 1373 |
j += a[i]; |
| 1374 |
} |
| 1375 |
if ( j == 77 ) |
| 1376 |
printf("hello there\n"); |
| 1377 |
</pre> |
| 1378 |
<p>then Memcheck will complain, at the |
| 1379 |
<code class="computeroutput">if</code>, that the condition depends on |
| 1380 |
uninitialised values. Note that it <span class="command"><strong>doesn't</strong></span> complain |
| 1381 |
at the <code class="varname">j += a[i];</code>, since at that point the |
| 1382 |
undefinedness is not "observable". It's only when a decision has to be |
| 1383 |
made as to whether or not to do the <code class="function">printf</code> -- an |
| 1384 |
observable action of your program -- that Memcheck complains.</p> |
| 1385 |
<p>Most low level operations, such as adds, cause Memcheck to use the |
| 1386 |
V bits for the operands to calculate the V bits for the result. Even if |
| 1387 |
the result is partially or wholly undefined, it does not |
| 1388 |
complain.</p> |
| 1389 |
<p>Checks on definedness only occur in three places: when a value is |
| 1390 |
used to generate a memory address, when control flow decision needs to |
| 1391 |
be made, and when a system call is detected, Memcheck checks definedness |
| 1392 |
of parameters as required.</p> |
| 1393 |
<p>If a check should detect undefinedness, an error message is |
| 1394 |
issued. The resulting value is subsequently regarded as well-defined. |
| 1395 |
To do otherwise would give long chains of error messages. In other |
| 1396 |
words, once Memcheck reports an undefined value error, it tries to |
| 1397 |
avoid reporting further errors derived from that same undefined |
| 1398 |
value.</p> |
| 1399 |
<p>This sounds overcomplicated. Why not just check all reads from |
| 1400 |
memory, and complain if an undefined value is loaded into a CPU |
| 1401 |
register? Well, that doesn't work well, because perfectly legitimate C |
| 1402 |
programs routinely copy uninitialised values around in memory, and we |
| 1403 |
don't want endless complaints about that. Here's the canonical example. |
| 1404 |
Consider a struct like this:</p> |
| 1405 |
<pre class="programlisting"> |
| 1406 |
struct S { int x; char c; }; |
| 1407 |
struct S s1, s2; |
| 1408 |
s1.x = 42; |
| 1409 |
s1.c = 'z'; |
| 1410 |
s2 = s1; |
| 1411 |
</pre> |
| 1412 |
<p>The question to ask is: how large is <code class="varname">struct S</code>, |
| 1413 |
in bytes? An <code class="varname">int</code> is 4 bytes and a |
| 1414 |
<code class="varname">char</code> one byte, so perhaps a <code class="varname">struct |
| 1415 |
S</code> occupies 5 bytes? Wrong. All non-toy compilers we know |
| 1416 |
of will round the size of <code class="varname">struct S</code> up to a whole |
| 1417 |
number of words, in this case 8 bytes. Not doing this forces compilers |
| 1418 |
to generate truly appalling code for accessing arrays of |
| 1419 |
<code class="varname">struct S</code>'s on some architectures.</p> |
| 1420 |
<p>So <code class="varname">s1</code> occupies 8 bytes, yet only 5 of them will |
| 1421 |
be initialised. For the assignment <code class="varname">s2 = s1</code>, GCC |
| 1422 |
generates code to copy all 8 bytes wholesale into <code class="varname">s2</code> |
| 1423 |
without regard for their meaning. If Memcheck simply checked values as |
| 1424 |
they came out of memory, it would yelp every time a structure assignment |
| 1425 |
like this happened. So the more complicated behaviour described above |
| 1426 |
is necessary. This allows GCC to copy |
| 1427 |
<code class="varname">s1</code> into <code class="varname">s2</code> any way it likes, and a |
| 1428 |
warning will only be emitted if the uninitialised values are later |
| 1429 |
used.</p> |
| 1430 |
<p>As explained above, Memcheck maintains 8 V bits for each byte in your |
| 1431 |
process, including for bytes that are in shared memory. However, the same piece |
| 1432 |
of shared memory can be mapped multiple times, by several processes or even by |
| 1433 |
the same process (for example, if the process wants a read-only and a read-write |
| 1434 |
mapping of the same page). For such multiple mappings, Memcheck tracks the V |
| 1435 |
bits for each mapping independently. This can lead to false positive errors, as |
| 1436 |
the shared memory can be initialised via a first mapping, and accessed via |
| 1437 |
another mapping. The access via this other mapping will have its own V bits, |
| 1438 |
which have not been changed when the memory was initialised via the first |
| 1439 |
mapping. The bypass for these false positives is to use Memcheck's client |
| 1440 |
requests <code class="varname">VALGRIND_MAKE_MEM_DEFINED</code> and |
| 1441 |
<code class="varname">VALGRIND_MAKE_MEM_UNDEFINED</code> to inform |
| 1442 |
Memcheck about what your program does (or what another process does) |
| 1443 |
to these shared memory mappings. |
| 1444 |
</p> |
| 1445 |
</div> |
| 1446 |
<div class="sect2"> |
| 1447 |
<div class="titlepage"><div><div><h3 class="title"> |
| 1448 |
<a name="mc-manual.vaddress"></a>4.5.2. Valid-address (A) bits</h3></div></div></div> |
| 1449 |
<p>Notice that the previous subsection describes how the validity of |
| 1450 |
values is established and maintained without having to say whether the |
| 1451 |
program does or does not have the right to access any particular memory |
| 1452 |
location. We now consider the latter question.</p> |
| 1453 |
<p>As described above, every bit in memory or in the CPU has an |
| 1454 |
associated valid-value (V) bit. In addition, all bytes in memory, but |
| 1455 |
not in the CPU, have an associated valid-address (A) bit. This |
| 1456 |
indicates whether or not the program can legitimately read or write that |
| 1457 |
location. It does not give any indication of the validity of the data |
| 1458 |
at that location -- that's the job of the V bits -- only whether or not |
| 1459 |
the location may be accessed.</p> |
| 1460 |
<p>Every time your program reads or writes memory, Memcheck checks |
| 1461 |
the A bits associated with the address. If any of them indicate an |
| 1462 |
invalid address, an error is emitted. Note that the reads and writes |
| 1463 |
themselves do not change the A bits, only consult them.</p> |
| 1464 |
<p>So how do the A bits get set/cleared? Like this:</p> |
| 1465 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 1466 |
<li class="listitem"><p>When the program starts, all the global data areas are |
| 1467 |
marked as accessible.</p></li> |
| 1468 |
<li class="listitem"><p>When the program does |
| 1469 |
<code class="function">malloc</code>/<code class="computeroutput">new</code>, |
| 1470 |
the A bits for exactly the area allocated, and not a byte more, |
| 1471 |
are marked as accessible. Upon freeing the area the A bits are |
| 1472 |
changed to indicate inaccessibility.</p></li> |
| 1473 |
<li class="listitem"><p>When the stack pointer register (<code class="literal">SP</code>) moves |
| 1474 |
up or down, A bits are set. The rule is that the area from |
| 1475 |
<code class="literal">SP</code> up to the base of the stack is marked as |
| 1476 |
accessible, and below <code class="literal">SP</code> is inaccessible. (If |
| 1477 |
that sounds illogical, bear in mind that the stack grows down, not |
| 1478 |
up, on almost all Unix systems, including GNU/Linux.) Tracking |
| 1479 |
<code class="literal">SP</code> like this has the useful side-effect that the |
| 1480 |
section of stack used by a function for local variables etc is |
| 1481 |
automatically marked accessible on function entry and inaccessible |
| 1482 |
on exit.</p></li> |
| 1483 |
<li class="listitem"><p>When doing system calls, A bits are changed appropriately. |
| 1484 |
For example, <code class="literal">mmap</code> |
| 1485 |
magically makes files appear in the process' |
| 1486 |
address space, so the A bits must be updated if <code class="literal">mmap</code> |
| 1487 |
succeeds.</p></li> |
| 1488 |
<li class="listitem"><p>Optionally, your program can tell Memcheck about such changes |
| 1489 |
explicitly, using the client request mechanism described |
| 1490 |
above.</p></li> |
| 1491 |
</ul></div> |
| 1492 |
</div> |
| 1493 |
<div class="sect2"> |
| 1494 |
<div class="titlepage"><div><div><h3 class="title"> |
| 1495 |
<a name="mc-manual.together"></a>4.5.3. Putting it all together</h3></div></div></div> |
| 1496 |
<p>Memcheck's checking machinery can be summarised as |
| 1497 |
follows:</p> |
| 1498 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 1499 |
<li class="listitem"><p>Each byte in memory has 8 associated V (valid-value) bits, |
| 1500 |
saying whether or not the byte has a defined value, and a single A |
| 1501 |
(valid-address) bit, saying whether or not the program currently has |
| 1502 |
the right to read/write that address. As mentioned above, heavy |
| 1503 |
use of compression means the overhead is typically around 25%.</p></li> |
| 1504 |
<li class="listitem"><p>When memory is read or written, the relevant A bits are |
| 1505 |
consulted. If they indicate an invalid address, Memcheck emits an |
| 1506 |
Invalid read or Invalid write error.</p></li> |
| 1507 |
<li class="listitem"><p>When memory is read into the CPU's registers, the relevant V |
| 1508 |
bits are fetched from memory and stored in the simulated CPU. They |
| 1509 |
are not consulted.</p></li> |
| 1510 |
<li class="listitem"><p>When a register is written out to memory, the V bits for that |
| 1511 |
register are written back to memory too.</p></li> |
| 1512 |
<li class="listitem"><p>When values in CPU registers are used to generate a memory |
| 1513 |
address, or to determine the outcome of a conditional branch, the V |
| 1514 |
bits for those values are checked, and an error emitted if any of |
| 1515 |
them are undefined.</p></li> |
| 1516 |
<li class="listitem"><p>When values in CPU registers are used for any other purpose, |
| 1517 |
Memcheck computes the V bits for the result, but does not check |
| 1518 |
them.</p></li> |
| 1519 |
<li class="listitem"><p>Once the V bits for a value in the CPU have been checked, they |
| 1520 |
are then set to indicate validity. This avoids long chains of |
| 1521 |
errors.</p></li> |
| 1522 |
<li class="listitem"> |
| 1523 |
<p>When values are loaded from memory, Memcheck checks the A bits |
| 1524 |
for that location and issues an illegal-address warning if needed. |
| 1525 |
In that case, the V bits loaded are forced to indicate Valid, |
| 1526 |
despite the location being invalid.</p> |
| 1527 |
<p>This apparently strange choice reduces the amount of confusing |
| 1528 |
information presented to the user. It avoids the unpleasant |
| 1529 |
phenomenon in which memory is read from a place which is both |
| 1530 |
unaddressable and contains invalid values, and, as a result, you get |
| 1531 |
not only an invalid-address (read/write) error, but also a |
| 1532 |
potentially large set of uninitialised-value errors, one for every |
| 1533 |
time the value is used.</p> |
| 1534 |
<p>There is a hazy boundary case to do with multi-byte loads from |
| 1535 |
addresses which are partially valid and partially invalid. See |
| 1536 |
details of the option <code class="option">--partial-loads-ok</code> for details. |
| 1537 |
</p> |
| 1538 |
</li> |
| 1539 |
</ul></div> |
| 1540 |
<p>Memcheck intercepts calls to <code class="function">malloc</code>, |
| 1541 |
<code class="function">calloc</code>, <code class="function">realloc</code>, |
| 1542 |
<code class="function">valloc</code>, <code class="function">memalign</code>, |
| 1543 |
<code class="function">free</code>, <code class="computeroutput">new</code>, |
| 1544 |
<code class="computeroutput">new[]</code>, |
| 1545 |
<code class="computeroutput">delete</code> and |
| 1546 |
<code class="computeroutput">delete[]</code>. The behaviour you get |
| 1547 |
is:</p> |
| 1548 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 1549 |
<li class="listitem"><p><code class="function">malloc</code>/<code class="function">new</code>/<code class="computeroutput">new[]</code>: |
| 1550 |
the returned memory is marked as addressable but not having valid |
| 1551 |
values. This means you have to write to it before you can read |
| 1552 |
it.</p></li> |
| 1553 |
<li class="listitem"><p><code class="function">calloc</code>: returned memory is marked both |
| 1554 |
addressable and valid, since <code class="function">calloc</code> clears |
| 1555 |
the area to zero.</p></li> |
| 1556 |
<li class="listitem"><p><code class="function">realloc</code>: if the new size is larger than |
| 1557 |
the old, the new section is addressable but invalid, as with |
| 1558 |
<code class="function">malloc</code>. If the new size is smaller, the |
| 1559 |
dropped-off section is marked as unaddressable. You may only pass to |
| 1560 |
<code class="function">realloc</code> a pointer previously issued to you by |
| 1561 |
<code class="function">malloc</code>/<code class="function">calloc</code>/<code class="function">realloc</code>.</p></li> |
| 1562 |
<li class="listitem"><p><code class="function">free</code>/<code class="computeroutput">delete</code>/<code class="computeroutput">delete[]</code>: |
| 1563 |
you may only pass to these functions a pointer previously issued |
| 1564 |
to you by the corresponding allocation function. Otherwise, |
| 1565 |
Memcheck complains. If the pointer is indeed valid, Memcheck |
| 1566 |
marks the entire area it points at as unaddressable, and places |
| 1567 |
the block in the freed-blocks-queue. The aim is to defer as long |
| 1568 |
as possible reallocation of this block. Until that happens, all |
| 1569 |
attempts to access it will elicit an invalid-address error, as you |
| 1570 |
would hope.</p></li> |
| 1571 |
</ul></div> |
| 1572 |
</div> |
| 1573 |
</div> |
| 1574 |
<div class="sect1"> |
| 1575 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 1576 |
<a name="mc-manual.monitor-commands"></a>4.6. Memcheck Monitor Commands</h2></div></div></div> |
| 1577 |
<p>The Memcheck tool provides monitor commands handled by Valgrind's built-in |
| 1578 |
gdbserver (see <a class="xref" href="manual-core-adv.html#manual-core-adv.gdbserver-commandhandling" title="3.2.5. Monitor command handling by the Valgrind gdbserver">Monitor command handling by the Valgrind gdbserver</a>). |
| 1579 |
Valgrind python code provides GDB front end commands giving an easier usage of |
| 1580 |
the memcheck monitor commands (see |
| 1581 |
<a class="xref" href="manual-core-adv.html#manual-core-adv.gdbserver-gdbmonitorfrontend" title="3.2.6. GDB front end commands for Valgrind gdbserver monitor commands">GDB front end commands for Valgrind gdbserver monitor commands</a>). To launch a |
| 1582 |
memcheck monitor command via its GDB front end command, instead of prefixing |
| 1583 |
the command with "monitor", you must use the GDB <code class="varname">memcheck</code> |
| 1584 |
command (or the shorter aliases <code class="varname">mc</code>). Using the memcheck |
| 1585 |
GDB front end command provide a more flexible usage, such as evaluation of |
| 1586 |
address and length arguments by GDB. In GDB, you can use <code class="varname">help |
| 1587 |
memcheck</code> to get help about the memcheck front end monitor commands |
| 1588 |
and you can use <code class="varname">apropos memcheck</code> to get all the commands |
| 1589 |
mentionning the word "memcheck" in their name or on-line help. |
| 1590 |
</p> |
| 1591 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 1592 |
<li class="listitem"> |
| 1593 |
<p><code class="varname">xb <addr> [<len>]</code> |
| 1594 |
shows the definedness (V) bits and values for <len> (default 1) |
| 1595 |
bytes starting at <addr>. |
| 1596 |
For each 8 bytes, two lines are output. |
| 1597 |
</p> |
| 1598 |
<p> |
| 1599 |
The first line shows the validity bits for 8 bytes. |
| 1600 |
The definedness of each byte in the range is given using two hexadecimal |
| 1601 |
digits. These hexadecimal digits encode the validity of each bit of the |
| 1602 |
corresponding byte, |
| 1603 |
using 0 if the bit is defined and 1 if the bit is undefined. |
| 1604 |
If a byte is not addressable, its validity bits are replaced |
| 1605 |
by <code class="varname">__</code> (a double underscore). |
| 1606 |
</p> |
| 1607 |
<p> |
| 1608 |
The second line shows the values of the bytes below the corresponding |
| 1609 |
validity bits. The format used to show the bytes data is similar to the |
| 1610 |
GDB command 'x /<len>xb <addr>'. The value for a non |
| 1611 |
addressable bytes is shown as ?? (two question marks). |
| 1612 |
</p> |
| 1613 |
<p> |
| 1614 |
In the following example, <code class="varname">string10</code> is an array |
| 1615 |
of 10 characters, in which the even numbered bytes are |
| 1616 |
undefined. In the below example, the byte corresponding |
| 1617 |
to <code class="varname">string10[5]</code> is not addressable. |
| 1618 |
</p> |
| 1619 |
<pre class="programlisting"> |
| 1620 |
(gdb) p &string10 |
| 1621 |
$4 = (char (*)[10]) 0x804a2f0 |
| 1622 |
(gdb) mo xb 0x804a2f0 10 |
| 1623 |
ff 00 ff 00 ff __ ff 00 |
| 1624 |
0x804A2F0: 0x3f 0x6e 0x3f 0x65 0x3f 0x?? 0x3f 0x65 |
| 1625 |
ff 00 |
| 1626 |
0x804A2F8: 0x3f 0x00 |
| 1627 |
Address 0x804A2F0 len 10 has 1 bytes unaddressable |
| 1628 |
(gdb) |
| 1629 |
</pre> |
| 1630 |
<p>The GDB memcheck front end command <code class="varname">memcheck xb ADDR |
| 1631 |
[LEN]</code> accepts any address expression for its first ADDR |
| 1632 |
argument. The second optional argument is any integer expression. Note |
| 1633 |
that these 2 arguments must be separated by a space. |
| 1634 |
The following example shows how to get the definedness of |
| 1635 |
<code class="varname">string10</code> using the memcheck xb front end command. |
| 1636 |
</p> |
| 1637 |
<pre class="programlisting"> |
| 1638 |
(gdb) mc xb &string10 sizeof(string10) |
| 1639 |
ff 00 ff 00 ff __ ff 00 |
| 1640 |
0x804A2F0: 0x3f 0x6e 0x3f 0x65 0x3f 0x?? 0x3f 0x65 |
| 1641 |
ff 00 |
| 1642 |
0x804A2F8: 0x3f 0x00 |
| 1643 |
Address 0x804A2F0 len 10 has 1 bytes unaddressable |
| 1644 |
(gdb) |
| 1645 |
</pre> |
| 1646 |
<p> The command xb cannot be used with registers. To get |
| 1647 |
the validity bits of a register, you must start Valgrind with the |
| 1648 |
option <code class="option">--vgdb-shadow-registers=yes</code>. The validity |
| 1649 |
bits of a register can then be obtained by printing the 'shadow 1' |
| 1650 |
corresponding register. In the below x86 example, the register |
| 1651 |
eax has all its bits undefined, while the register ebx is fully |
| 1652 |
defined. |
| 1653 |
</p> |
| 1654 |
<pre class="programlisting"> |
| 1655 |
(gdb) p /x $eaxs1 |
| 1656 |
$9 = 0xffffffff |
| 1657 |
(gdb) p /x $ebxs1 |
| 1658 |
$10 = 0x0 |
| 1659 |
(gdb) |
| 1660 |
</pre> |
| 1661 |
</li> |
| 1662 |
<li class="listitem"> |
| 1663 |
<p><code class="varname">get_vbits <addr> [<len>]</code> |
| 1664 |
shows the definedness (V) bits for <len> (default 1) bytes |
| 1665 |
starting at <addr> using the same convention as the |
| 1666 |
<code class="varname">xb</code> command. <code class="varname">get_vbits</code> only |
| 1667 |
shows the V bits (grouped by 4 bytes). It does not show the values. |
| 1668 |
If you want to associate V bits with the corresponding byte values, the |
| 1669 |
<code class="varname">xb</code> command will be easier to use, in particular |
| 1670 |
on little endian computers when associating undefined parts of an integer |
| 1671 |
with their V bits values. |
| 1672 |
</p> |
| 1673 |
<p> |
| 1674 |
The following example shows the result of <code class="varname">get_vbits</code> on |
| 1675 |
the <code class="varname">string10</code> used in the <code class="varname">xb</code> command |
| 1676 |
explanation. The GDB memcheck equivalent front end command <code class="varname">memcheck |
| 1677 |
get_vbits ADDR [LEN]</code>accepts any ADDR expression and any LEN |
| 1678 |
expression (separated by a space). |
| 1679 |
</p> |
| 1680 |
<pre class="programlisting"> |
| 1681 |
(gdb) monitor get_vbits 0x804a2f0 10 |
| 1682 |
ff00ff00 ff__ff00 ff00 |
| 1683 |
Address 0x804A2F0 len 10 has 1 bytes unaddressable |
| 1684 |
(gdb) memcheck get_vbits &string10 sizeof(string10) |
| 1685 |
ff00ff00 ff__ff00 ff00 |
| 1686 |
Address 0x804A2F0 len 10 has 1 bytes unaddressable |
| 1687 |
</pre> |
| 1688 |
</li> |
| 1689 |
<li class="listitem"> |
| 1690 |
<p><code class="varname">make_memory |
| 1691 |
[noaccess|undefined|defined|Definedifaddressable] <addr> |
| 1692 |
[<len>]</code> marks the range of <len> (default 1) |
| 1693 |
bytes at <addr> as having the given status. Parameter |
| 1694 |
<code class="varname">noaccess</code> marks the range as non-accessible, so |
| 1695 |
Memcheck will report an error on any access to it. |
| 1696 |
<code class="varname">undefined</code> or <code class="varname">defined</code> mark |
| 1697 |
the area as accessible, but Memcheck regards the bytes in it |
| 1698 |
respectively as having undefined or defined values. |
| 1699 |
<code class="varname">Definedifaddressable</code> marks as defined, bytes in |
| 1700 |
the range which are already addressible, but makes no change to |
| 1701 |
the status of bytes in the range which are not addressible. Note |
| 1702 |
that the first letter of <code class="varname">Definedifaddressable</code> |
| 1703 |
is an uppercase D to avoid confusion with <code class="varname">defined</code>. |
| 1704 |
</p> |
| 1705 |
<p>The GDB equivalent memcheck front end commands <code class="varname">memcheck |
| 1706 |
make_memory [noaccess|undefined|defined|Definedifaddressable] ADDR |
| 1707 |
[LEN]</code> accept any address expression for their first ADDR |
| 1708 |
argument. The second optional argument is any integer expression. Note |
| 1709 |
that these 2 arguments must be separated by a space. |
| 1710 |
</p> |
| 1711 |
<p> |
| 1712 |
In the following example, the first byte of the |
| 1713 |
<code class="varname">string10</code> is marked as defined and then is marked |
| 1714 |
noaccess: |
| 1715 |
</p> |
| 1716 |
<pre class="programlisting"> |
| 1717 |
(gdb) monitor make_memory defined 0x8049e28 1 |
| 1718 |
(gdb) monitor get_vbits 0x8049e28 10 |
| 1719 |
0000ff00 ff00ff00 ff00 |
| 1720 |
(gdb) memcheck make_memory noaccess &string10[0] |
| 1721 |
(gdb) memcheck get_vbits &string10 sizeof(string10) |
| 1722 |
__00ff00 ff00ff00 ff00 |
| 1723 |
Address 0x8049E28 len 10 has 1 bytes unaddressable |
| 1724 |
(gdb) |
| 1725 |
</pre> |
| 1726 |
</li> |
| 1727 |
<li class="listitem"> |
| 1728 |
<p><code class="varname">check_memory [addressable|defined] <addr> |
| 1729 |
[<len>]</code> checks that the range of <len> |
| 1730 |
(default 1) bytes at <addr> has the specified accessibility. |
| 1731 |
It then outputs a description of <addr>. In the following |
| 1732 |
example, a detailed description is available because the |
| 1733 |
option <code class="option">--read-var-info=yes</code> was given at Valgrind |
| 1734 |
startup: |
| 1735 |
</p> |
| 1736 |
<pre class="programlisting"> |
| 1737 |
(gdb) monitor check_memory defined 0x8049e28 1 |
| 1738 |
Address 0x8049E28 len 1 defined |
| 1739 |
==14698== Location 0x8049e28 is 0 bytes inside string10[0], |
| 1740 |
==14698== declared at prog.c:10, in frame #0 of thread 1 |
| 1741 |
(gdb) |
| 1742 |
</pre> |
| 1743 |
<p>The GDB equivalent memcheck front end commands <code class="varname">memcheck |
| 1744 |
check_memory [addressable|defined] ADDR [LEN]</code> accept any address |
| 1745 |
expression for their first ADDR argument. The second optional argument is |
| 1746 |
any integer expression. Note that these 2 arguments must be separated by a |
| 1747 |
space. |
| 1748 |
</p> |
| 1749 |
</li> |
| 1750 |
<li class="listitem"> |
| 1751 |
<p><code class="varname">leak_check [full*|summary|xtleak] |
| 1752 |
[kinds <set>|reachable|possibleleak*|definiteleak] |
| 1753 |
[heuristics heur1,heur2,...] |
| 1754 |
[new|increased*|changed|any] |
| 1755 |
[unlimited*|limited <max_loss_records_output>] |
| 1756 |
</code> |
| 1757 |
performs a leak check. The <code class="varname">*</code> in the arguments |
| 1758 |
indicates the default values. </p> |
| 1759 |
<p> If the <code class="varname">[full*|summary|xtleak]</code> argument is |
| 1760 |
<code class="varname">summary</code>, only a summary of the leak search is given; |
| 1761 |
otherwise a full leak report is produced. A full leak report gives |
| 1762 |
detailed information for each leak: the stack trace where the leaked blocks |
| 1763 |
were allocated, the number of blocks leaked and their total size. When a |
| 1764 |
full report is requested, the next two arguments further specify what |
| 1765 |
kind of leaks to report. A leak's details are shown if they match |
| 1766 |
both the second and third argument. A full leak report might |
| 1767 |
output detailed information for many leaks. The nr of leaks for |
| 1768 |
which information is output can be controlled using |
| 1769 |
the <code class="varname">limited</code> argument followed by the maximum nr |
| 1770 |
of leak records to output. If this maximum is reached, the leak |
| 1771 |
search outputs the records with the biggest number of bytes. |
| 1772 |
</p> |
| 1773 |
<p>The value <code class="varname">xtleak</code> also produces a full leak report, |
| 1774 |
but output it as an xtree in a file xtleak.kcg.%p.%n (see <a class="xref" href="manual-core.html#opt.log-file">--log-file</a>). |
| 1775 |
See <a class="xref" href="manual-core.html#manual-core.xtree" title="2.10. Execution Trees">Execution Trees</a> |
| 1776 |
for a detailed explanation about execution trees formats. |
| 1777 |
See <a class="xref" href="mc-manual.html#opt.xtree-leak">--xtree-leak</a> for the description of the events |
| 1778 |
in a xtree leak file. |
| 1779 |
</p> |
| 1780 |
<p>The <code class="varname">kinds</code> argument controls what kind of blocks |
| 1781 |
are shown for a <code class="varname">full</code> leak search. The set of leak kinds |
| 1782 |
to show can be specified using a <code class="varname"><set></code> similarly |
| 1783 |
to the command line option <code class="option">--show-leak-kinds</code>. |
| 1784 |
Alternatively, the value <code class="varname">definiteleak</code> |
| 1785 |
is equivalent to <code class="varname">kinds definite</code>, the |
| 1786 |
value <code class="varname">possibleleak</code> is equivalent to |
| 1787 |
<code class="varname">kinds definite,possible</code> : it will also show |
| 1788 |
possibly leaked blocks, .i.e those for which only an interior |
| 1789 |
pointer was found. The value <code class="varname">reachable</code> will |
| 1790 |
show all block categories (i.e. is equivalent to <code class="varname">kinds |
| 1791 |
all</code>). |
| 1792 |
</p> |
| 1793 |
<p>The <code class="varname">heuristics</code> argument controls the heuristics |
| 1794 |
used during the leak search. The set of heuristics to use can be specified |
| 1795 |
using a <code class="varname"><set></code> similarly |
| 1796 |
to the command line option <code class="option">--leak-check-heuristics</code>. |
| 1797 |
The default value for the <code class="varname">heuristics</code> argument is |
| 1798 |
<code class="varname">heuristics none</code>. |
| 1799 |
</p> |
| 1800 |
<p>The <code class="varname">[new|increased*|changed|any]</code> argument controls |
| 1801 |
what kinds of changes are shown for a <code class="varname">full</code> leak search. |
| 1802 |
The value <code class="varname">increased</code> specifies that only block |
| 1803 |
allocation stacks with an increased number of leaked bytes or |
| 1804 |
blocks since the previous leak check should be shown. The |
| 1805 |
value <code class="varname">changed</code> specifies that allocation stacks |
| 1806 |
with any change since the previous leak check should be shown. |
| 1807 |
The value <code class="varname">new</code> specifies to show only the block |
| 1808 |
allocation stacks that are new since the previous leak search. |
| 1809 |
The value <code class="varname">any</code> specifies that all leak entries |
| 1810 |
should be shown, regardless of any increase or decrease. |
| 1811 |
If <code class="varname">new</code> or <code class="varname">increased</code> or |
| 1812 |
<code class="varname">changed</code> are specified, the leak report entries will show |
| 1813 |
the delta relative to the previous leak report and the new loss records |
| 1814 |
will have a "new" marker (even when <code class="varname">increased</code> or |
| 1815 |
<code class="varname">changed</code> were specified). |
| 1816 |
</p> |
| 1817 |
<p>The following example shows usage of the |
| 1818 |
<code class="varname">leak_check</code> monitor command on |
| 1819 |
the <code class="varname">memcheck/tests/leak-cases.c</code> regression |
| 1820 |
test. The first command outputs one entry having an increase in |
| 1821 |
the leaked bytes. The second command is the same as the first |
| 1822 |
command, but uses the abbreviated forms accepted by GDB and the |
| 1823 |
Valgrind gdbserver. It only outputs the summary information, as |
| 1824 |
there was no increase since the previous leak search.</p> |
| 1825 |
<pre class="programlisting"> |
| 1826 |
(gdb) monitor leak_check full possibleleak increased |
| 1827 |
==19520== 16 (+16) bytes in 1 (+1) blocks are possibly lost in new loss record 9 of 12 |
| 1828 |
==19520== at 0x40070B4: malloc (vg_replace_malloc.c:263) |
| 1829 |
==19520== by 0x80484D5: mk (leak-cases.c:52) |
| 1830 |
==19520== by 0x804855F: f (leak-cases.c:81) |
| 1831 |
==19520== by 0x80488E0: main (leak-cases.c:107) |
| 1832 |
==19520== |
| 1833 |
==19520== LEAK SUMMARY: |
| 1834 |
==19520== definitely lost: 32 (+0) bytes in 2 (+0) blocks |
| 1835 |
==19520== indirectly lost: 16 (+0) bytes in 1 (+0) blocks |
| 1836 |
==19520== possibly lost: 32 (+16) bytes in 2 (+1) blocks |
| 1837 |
==19520== still reachable: 96 (+16) bytes in 6 (+1) blocks |
| 1838 |
==19520== suppressed: 0 (+0) bytes in 0 (+0) blocks |
| 1839 |
==19520== Reachable blocks (those to which a pointer was found) are not shown. |
| 1840 |
==19520== To see them, add 'reachable any' args to leak_check |
| 1841 |
==19520== |
| 1842 |
(gdb) mo l |
| 1843 |
==19520== LEAK SUMMARY: |
| 1844 |
==19520== definitely lost: 32 (+0) bytes in 2 (+0) blocks |
| 1845 |
==19520== indirectly lost: 16 (+0) bytes in 1 (+0) blocks |
| 1846 |
==19520== possibly lost: 32 (+0) bytes in 2 (+0) blocks |
| 1847 |
==19520== still reachable: 96 (+0) bytes in 6 (+0) blocks |
| 1848 |
==19520== suppressed: 0 (+0) bytes in 0 (+0) blocks |
| 1849 |
==19520== Reachable blocks (those to which a pointer was found) are not shown. |
| 1850 |
==19520== To see them, add 'reachable any' args to leak_check |
| 1851 |
==19520== |
| 1852 |
(gdb) |
| 1853 |
</pre> |
| 1854 |
<p>Note that when using Valgrind's gdbserver, it is not |
| 1855 |
necessary to rerun |
| 1856 |
with <code class="option">--leak-check=full</code> |
| 1857 |
<code class="option">--show-reachable=yes</code> to see the reachable |
| 1858 |
blocks. You can obtain the same information without rerunning by |
| 1859 |
using the GDB command <code class="computeroutput">monitor leak_check full |
| 1860 |
reachable any</code> (or, using |
| 1861 |
abbreviation: <code class="computeroutput">mo l f r a</code>). |
| 1862 |
</p> |
| 1863 |
<p>The GDB equivalent memcheck front end command <code class="varname">memcheck |
| 1864 |
leak_check</code> auto-completes the user input by providing the full |
| 1865 |
list of keywords still relevant according to what is already typed. For |
| 1866 |
example, if the "summary" keyword has been provided, the following TABs to |
| 1867 |
auto-complete other items will not propose anymore "full" and "xtleak". |
| 1868 |
Note that KIND and HEUR values are not part of auto-completed elements. |
| 1869 |
</p> |
| 1870 |
</li> |
| 1871 |
<li class="listitem"> |
| 1872 |
<p><code class="varname">block_list <loss_record_nr>|<loss_record_nr_from>..<loss_record_nr_to> |
| 1873 |
[unlimited*|limited <max_blocks>] |
| 1874 |
[heuristics heur1,heur2,...] |
| 1875 |
</code> |
| 1876 |
shows the list of blocks belonging to |
| 1877 |
<code class="varname"><loss_record_nr></code> (or to the loss records range |
| 1878 |
<code class="varname"><loss_record_nr_from>..<loss_record_nr_to></code>). |
| 1879 |
The nr of blocks to print can be controlled using the |
| 1880 |
<code class="varname">limited</code> argument followed by the maximum nr |
| 1881 |
of blocks to output. |
| 1882 |
If one or more heuristics are given, only prints the loss records |
| 1883 |
and blocks found via one of the given <code class="varname">heur1,heur2,...</code> |
| 1884 |
heuristics. |
| 1885 |
</p> |
| 1886 |
<p> A leak search merges the allocated blocks in loss records : |
| 1887 |
a loss record re-groups all blocks having the same state (for |
| 1888 |
example, Definitely Lost) and the same allocation backtrace. |
| 1889 |
Each loss record is identified in the leak search result |
| 1890 |
by a loss record number. |
| 1891 |
The <code class="varname">block_list</code> command shows the loss record information |
| 1892 |
followed by the addresses and sizes of the blocks which have been |
| 1893 |
merged in the loss record. If a block was found using an heuristic, the block size |
| 1894 |
is followed by the heuristic. |
| 1895 |
</p> |
| 1896 |
<p> If a directly lost block causes some other blocks to be indirectly |
| 1897 |
lost, the block_list command will also show these indirectly lost blocks. |
| 1898 |
The indirectly lost blocks will be indented according to the level of indirection |
| 1899 |
between the directly lost block and the indirectly lost block(s). |
| 1900 |
Each indirectly lost block is followed by the reference of its loss record. |
| 1901 |
</p> |
| 1902 |
<p> The block_list command can be used on the results of a leak search as long |
| 1903 |
as no block has been freed after this leak search: as soon as the program frees |
| 1904 |
a block, a new leak search is needed before block_list can be used again. |
| 1905 |
</p> |
| 1906 |
<p> |
| 1907 |
In the below example, the program leaks a tree structure by losing the pointer to |
| 1908 |
the block A (top of the tree). |
| 1909 |
So, the block A is directly lost, causing an indirect |
| 1910 |
loss of blocks B to G. The first block_list command shows the loss record of A |
| 1911 |
(a definitely lost block with address 0x4028028, size 16). The addresses and sizes |
| 1912 |
of the indirectly lost blocks due to block A are shown below the block A. |
| 1913 |
The second command shows the details of one of the indirect loss records output |
| 1914 |
by the first command. |
| 1915 |
</p> |
| 1916 |
<pre class="programlisting"> |
| 1917 |
A |
| 1918 |
/ \ |
| 1919 |
B C |
| 1920 |
/ \ / \ |
| 1921 |
D E F G |
| 1922 |
</pre> |
| 1923 |
<pre class="programlisting"> |
| 1924 |
(gdb) bt |
| 1925 |
#0 main () at leak-tree.c:69 |
| 1926 |
(gdb) monitor leak_check full any |
| 1927 |
==19552== 112 (16 direct, 96 indirect) bytes in 1 blocks are definitely lost in loss record 7 of 7 |
| 1928 |
==19552== at 0x40070B4: malloc (vg_replace_malloc.c:263) |
| 1929 |
==19552== by 0x80484D5: mk (leak-tree.c:28) |
| 1930 |
==19552== by 0x80484FC: f (leak-tree.c:41) |
| 1931 |
==19552== by 0x8048856: main (leak-tree.c:63) |
| 1932 |
==19552== |
| 1933 |
==19552== LEAK SUMMARY: |
| 1934 |
==19552== definitely lost: 16 bytes in 1 blocks |
| 1935 |
==19552== indirectly lost: 96 bytes in 6 blocks |
| 1936 |
==19552== possibly lost: 0 bytes in 0 blocks |
| 1937 |
==19552== still reachable: 0 bytes in 0 blocks |
| 1938 |
==19552== suppressed: 0 bytes in 0 blocks |
| 1939 |
==19552== |
| 1940 |
(gdb) monitor block_list 7 |
| 1941 |
==19552== 112 (16 direct, 96 indirect) bytes in 1 blocks are definitely lost in loss record 7 of 7 |
| 1942 |
==19552== at 0x40070B4: malloc (vg_replace_malloc.c:263) |
| 1943 |
==19552== by 0x80484D5: mk (leak-tree.c:28) |
| 1944 |
==19552== by 0x80484FC: f (leak-tree.c:41) |
| 1945 |
==19552== by 0x8048856: main (leak-tree.c:63) |
| 1946 |
==19552== 0x4028028[16] |
| 1947 |
==19552== 0x4028068[16] indirect loss record 1 |
| 1948 |
==19552== 0x40280E8[16] indirect loss record 3 |
| 1949 |
==19552== 0x4028128[16] indirect loss record 4 |
| 1950 |
==19552== 0x40280A8[16] indirect loss record 2 |
| 1951 |
==19552== 0x4028168[16] indirect loss record 5 |
| 1952 |
==19552== 0x40281A8[16] indirect loss record 6 |
| 1953 |
(gdb) mo b 2 |
| 1954 |
==19552== 16 bytes in 1 blocks are indirectly lost in loss record 2 of 7 |
| 1955 |
==19552== at 0x40070B4: malloc (vg_replace_malloc.c:263) |
| 1956 |
==19552== by 0x80484D5: mk (leak-tree.c:28) |
| 1957 |
==19552== by 0x8048519: f (leak-tree.c:43) |
| 1958 |
==19552== by 0x8048856: main (leak-tree.c:63) |
| 1959 |
==19552== 0x40280A8[16] |
| 1960 |
==19552== 0x4028168[16] indirect loss record 5 |
| 1961 |
==19552== 0x40281A8[16] indirect loss record 6 |
| 1962 |
(gdb) |
| 1963 |
|
| 1964 |
</pre> |
| 1965 |
</li> |
| 1966 |
<li class="listitem"> |
| 1967 |
<p><code class="varname">who_points_at <addr> [<len>]</code> |
| 1968 |
shows all the locations where a pointer to addr is found. |
| 1969 |
If len is equal to 1, the command only shows the locations pointing |
| 1970 |
exactly at addr (i.e. the "start pointers" to addr). |
| 1971 |
If len is > 1, "interior pointers" pointing at the len first bytes |
| 1972 |
will also be shown. |
| 1973 |
</p> |
| 1974 |
<p>The locations searched for are the same as the locations |
| 1975 |
used in the leak search. So, <code class="varname">who_points_at</code> can a.o. |
| 1976 |
be used to show why the leak search still can reach a block, or can |
| 1977 |
search for dangling pointers to a freed block. |
| 1978 |
Each location pointing at addr (or pointing inside addr if interior pointers |
| 1979 |
are being searched for) will be described. |
| 1980 |
</p> |
| 1981 |
<p>The GDB equivalent memcheck front end command <code class="varname">memcheck |
| 1982 |
who_points_at ADDR [LEN]</code> accept any address expression for its |
| 1983 |
first ADDR argument. The second optional argument is any integer |
| 1984 |
expression. Note that these 2 arguments must be separated by a space. |
| 1985 |
</p> |
| 1986 |
<p>In the below example, the pointers to the 'tree block A' (see example |
| 1987 |
in command <code class="varname">block_list</code>) is shown before the tree was leaked. |
| 1988 |
The descriptions are detailed as the option <code class="option">--read-var-info=yes</code> |
| 1989 |
was given at Valgrind startup. The second call shows the pointers (start and interior |
| 1990 |
pointers) to block G. The block G (0x40281A8) is reachable via block C (0x40280a8) |
| 1991 |
and register ECX of tid 1 (tid is the Valgrind thread id). |
| 1992 |
It is "interior reachable" via the register EBX. |
| 1993 |
</p> |
| 1994 |
<pre class="programlisting"> |
| 1995 |
(gdb) monitor who_points_at 0x4028028 |
| 1996 |
==20852== Searching for pointers to 0x4028028 |
| 1997 |
==20852== *0x8049e20 points at 0x4028028 |
| 1998 |
==20852== Location 0x8049e20 is 0 bytes inside global var "t" |
| 1999 |
==20852== declared at leak-tree.c:35 |
| 2000 |
(gdb) monitor who_points_at 0x40281A8 16 |
| 2001 |
==20852== Searching for pointers pointing in 16 bytes from 0x40281a8 |
| 2002 |
==20852== *0x40280ac points at 0x40281a8 |
| 2003 |
==20852== Address 0x40280ac is 4 bytes inside a block of size 16 alloc'd |
| 2004 |
==20852== at 0x40070B4: malloc (vg_replace_malloc.c:263) |
| 2005 |
==20852== by 0x80484D5: mk (leak-tree.c:28) |
| 2006 |
==20852== by 0x8048519: f (leak-tree.c:43) |
| 2007 |
==20852== by 0x8048856: main (leak-tree.c:63) |
| 2008 |
==20852== tid 1 register ECX points at 0x40281a8 |
| 2009 |
==20852== tid 1 register EBX interior points at 2 bytes inside 0x40281a8 |
| 2010 |
(gdb) |
| 2011 |
</pre> |
| 2012 |
<p> When <code class="varname">who_points_at</code> finds an interior pointer, |
| 2013 |
it will report the heuristic(s) with which this interior pointer |
| 2014 |
will be considered as reachable. Note that this is done independently |
| 2015 |
of the value of the option <code class="option">--leak-check-heuristics</code>. |
| 2016 |
In the below example, the loss record 6 indicates a possibly lost |
| 2017 |
block. <code class="varname">who_points_at</code> reports that there is an interior |
| 2018 |
pointer pointing in this block, and that the block can be considered |
| 2019 |
reachable using the heuristic |
| 2020 |
<code class="computeroutput">multipleinheritance</code>. |
| 2021 |
</p> |
| 2022 |
<pre class="programlisting"> |
| 2023 |
(gdb) monitor block_list 6 |
| 2024 |
==3748== 8 bytes in 1 blocks are possibly lost in loss record 6 of 7 |
| 2025 |
==3748== at 0x4007D77: operator new(unsigned int) (vg_replace_malloc.c:313) |
| 2026 |
==3748== by 0x8048954: main (leak_cpp_interior.cpp:43) |
| 2027 |
==3748== 0x402A0E0[8] |
| 2028 |
(gdb) monitor who_points_at 0x402A0E0 8 |
| 2029 |
==3748== Searching for pointers pointing in 8 bytes from 0x402a0e0 |
| 2030 |
==3748== *0xbe8ee078 interior points at 4 bytes inside 0x402a0e0 |
| 2031 |
==3748== Address 0xbe8ee078 is on thread 1's stack |
| 2032 |
==3748== block at 0x402a0e0 considered reachable by ptr 0x402a0e4 using multipleinheritance heuristic |
| 2033 |
(gdb) |
| 2034 |
</pre> |
| 2035 |
</li> |
| 2036 |
<li class="listitem"><p><code class="varname">xtmemory [<filename> default xtmemory.kcg.%p.%n]</code> |
| 2037 |
requests Memcheck tool to produce an xtree heap memory report. |
| 2038 |
See <a class="xref" href="manual-core.html#manual-core.xtree" title="2.10. Execution Trees">Execution Trees</a> for |
| 2039 |
a detailed explanation about execution trees. </p></li> |
| 2040 |
</ul></div> |
| 2041 |
</div> |
| 2042 |
<div class="sect1"> |
| 2043 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 2044 |
<a name="mc-manual.clientreqs"></a>4.7. Client Requests</h2></div></div></div> |
| 2045 |
<p>The following client requests are defined in |
| 2046 |
<code class="filename">memcheck.h</code>. |
| 2047 |
See <code class="filename">memcheck.h</code> for exact details of their |
| 2048 |
arguments.</p> |
| 2049 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 2050 |
<li class="listitem"><p><code class="varname">VALGRIND_MAKE_MEM_NOACCESS</code>, |
| 2051 |
<code class="varname">VALGRIND_MAKE_MEM_UNDEFINED</code> and |
| 2052 |
<code class="varname">VALGRIND_MAKE_MEM_DEFINED</code>. |
| 2053 |
These mark address ranges as completely inaccessible, |
| 2054 |
accessible but containing undefined data, and accessible and |
| 2055 |
containing defined data, respectively. They return -1, when |
| 2056 |
run on Valgrind and 0 otherwise.</p></li> |
| 2057 |
<li class="listitem"><p><code class="varname">VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE</code>. |
| 2058 |
This is just like <code class="varname">VALGRIND_MAKE_MEM_DEFINED</code> but only |
| 2059 |
affects those bytes that are already addressable.</p></li> |
| 2060 |
<li class="listitem"><p><code class="varname">VALGRIND_CHECK_MEM_IS_ADDRESSABLE</code> and |
| 2061 |
<code class="varname">VALGRIND_CHECK_MEM_IS_DEFINED</code>: check immediately |
| 2062 |
whether or not the given address range has the relevant property, |
| 2063 |
and if not, print an error message. Also, for the convenience of |
| 2064 |
the client, returns zero if the relevant property holds; otherwise, |
| 2065 |
the returned value is the address of the first byte for which the |
| 2066 |
property is not true. Always returns 0 when not run on |
| 2067 |
Valgrind.</p></li> |
| 2068 |
<li class="listitem"><p><code class="varname">VALGRIND_CHECK_VALUE_IS_DEFINED</code>: a quick and easy |
| 2069 |
way to find out whether Valgrind thinks a particular value |
| 2070 |
(lvalue, to be precise) is addressable and defined. Prints an error |
| 2071 |
message if not. It has no return value.</p></li> |
| 2072 |
<li class="listitem"><p><code class="varname">VALGRIND_DO_LEAK_CHECK</code>: does a full memory leak |
| 2073 |
check (like <code class="option">--leak-check=full</code>) right now. |
| 2074 |
This is useful for incrementally checking for leaks between arbitrary |
| 2075 |
places in the program's execution. It has no return value.</p></li> |
| 2076 |
<li class="listitem"><p><code class="varname">VALGRIND_DO_ADDED_LEAK_CHECK</code>: same as |
| 2077 |
<code class="varname"> VALGRIND_DO_LEAK_CHECK</code> but only shows the |
| 2078 |
entries for which there was an increase in leaked bytes or leaked |
| 2079 |
number of blocks since the previous leak search. It has no return |
| 2080 |
value.</p></li> |
| 2081 |
<li class="listitem"><p><code class="varname">VALGRIND_DO_CHANGED_LEAK_CHECK</code>: same as |
| 2082 |
<code class="varname">VALGRIND_DO_LEAK_CHECK</code> but only shows the |
| 2083 |
entries for which there was an increase or decrease in leaked |
| 2084 |
bytes or leaked number of blocks since the previous leak search. It |
| 2085 |
has no return value.</p></li> |
| 2086 |
<li class="listitem"><p><code class="varname">VALGRIND_DO_NEW_LEAK_CHECK</code>: same as |
| 2087 |
<code class="varname"> VALGRIND_DO_LEAK_CHECK</code> but only shows the new |
| 2088 |
entries since the previous leak search. It has no return value.</p></li> |
| 2089 |
<li class="listitem"><p><code class="varname">VALGRIND_DO_QUICK_LEAK_CHECK</code>: like |
| 2090 |
<code class="varname">VALGRIND_DO_LEAK_CHECK</code>, except it produces only a leak |
| 2091 |
summary (like <code class="option">--leak-check=summary</code>). |
| 2092 |
It has no return value.</p></li> |
| 2093 |
<li class="listitem"><p><code class="varname">VALGRIND_COUNT_LEAKS</code>: fills in the four |
| 2094 |
arguments with the number of bytes of memory found by the previous |
| 2095 |
leak check to be leaked (i.e. the sum of direct leaks and indirect leaks), |
| 2096 |
dubious, reachable and suppressed. This is useful in test harness code, |
| 2097 |
after calling <code class="varname">VALGRIND_DO_LEAK_CHECK</code> or |
| 2098 |
<code class="varname">VALGRIND_DO_QUICK_LEAK_CHECK</code>.</p></li> |
| 2099 |
<li class="listitem"><p><code class="varname">VALGRIND_COUNT_LEAK_BLOCKS</code>: identical to |
| 2100 |
<code class="varname">VALGRIND_COUNT_LEAKS</code> except that it returns the |
| 2101 |
number of blocks rather than the number of bytes in each |
| 2102 |
category.</p></li> |
| 2103 |
<li class="listitem"><p><code class="varname">VALGRIND_GET_VBITS</code> and |
| 2104 |
<code class="varname">VALGRIND_SET_VBITS</code>: allow you to get and set the |
| 2105 |
V (validity) bits for an address range. You should probably only |
| 2106 |
set V bits that you have got with |
| 2107 |
<code class="varname">VALGRIND_GET_VBITS</code>. Only for those who really |
| 2108 |
know what they are doing.</p></li> |
| 2109 |
<li class="listitem"> |
| 2110 |
<p><code class="varname">VALGRIND_CREATE_BLOCK</code> and |
| 2111 |
<code class="varname">VALGRIND_DISCARD</code>. <code class="varname">VALGRIND_CREATE_BLOCK</code> |
| 2112 |
takes an address, a number of bytes and a character string. The |
| 2113 |
specified address range is then associated with that string. When |
| 2114 |
Memcheck reports an invalid access to an address in the range, it |
| 2115 |
will describe it in terms of this block rather than in terms of |
| 2116 |
any other block it knows about. Note that the use of this macro |
| 2117 |
does not actually change the state of memory in any way -- it |
| 2118 |
merely gives a name for the range. |
| 2119 |
</p> |
| 2120 |
<p>At some point you may want Memcheck to stop reporting errors |
| 2121 |
in terms of the block named |
| 2122 |
by <code class="varname">VALGRIND_CREATE_BLOCK</code>. To make this |
| 2123 |
possible, <code class="varname">VALGRIND_CREATE_BLOCK</code> returns a |
| 2124 |
"block handle", which is a C <code class="varname">int</code> value. You |
| 2125 |
can pass this block handle to <code class="varname">VALGRIND_DISCARD</code>. |
| 2126 |
After doing so, Valgrind will no longer relate addressing errors |
| 2127 |
in the specified range to the block. Passing invalid handles to |
| 2128 |
<code class="varname">VALGRIND_DISCARD</code> is harmless. |
| 2129 |
</p> |
| 2130 |
</li> |
| 2131 |
</ul></div> |
| 2132 |
</div> |
| 2133 |
<div class="sect1"> |
| 2134 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 2135 |
<a name="mc-manual.mempools"></a>4.8. Memory Pools: describing and working with custom allocators</h2></div></div></div> |
| 2136 |
<p>Some programs use custom memory allocators, often for performance |
| 2137 |
reasons. Left to itself, Memcheck is unable to understand the |
| 2138 |
behaviour of custom allocation schemes as well as it understands the |
| 2139 |
standard allocators, and so may miss errors and leaks in your program. What |
| 2140 |
this section describes is a way to give Memcheck enough of a description of |
| 2141 |
your custom allocator that it can make at least some sense of what is |
| 2142 |
happening.</p> |
| 2143 |
<p>There are many different sorts of custom allocator, so Memcheck |
| 2144 |
attempts to reason about them using a loose, abstract model. We |
| 2145 |
use the following terminology when describing custom allocation |
| 2146 |
systems:</p> |
| 2147 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 2148 |
<li class="listitem"><p>Custom allocation involves a set of independent "memory pools". |
| 2149 |
</p></li> |
| 2150 |
<li class="listitem"><p>Memcheck's notion of a a memory pool consists of a single "anchor |
| 2151 |
address" and a set of non-overlapping "chunks" associated with the |
| 2152 |
anchor address.</p></li> |
| 2153 |
<li class="listitem"><p>Typically a pool's anchor address is the address of a |
| 2154 |
book-keeping "header" structure.</p></li> |
| 2155 |
<li class="listitem"><p>Typically the pool's chunks are drawn from a contiguous |
| 2156 |
"superblock" acquired through the system |
| 2157 |
<code class="function">malloc</code> or |
| 2158 |
<code class="function">mmap</code>.</p></li> |
| 2159 |
</ul></div> |
| 2160 |
<p>Keep in mind that the last two points above say "typically": the |
| 2161 |
Valgrind mempool client request API is intentionally vague about the |
| 2162 |
exact structure of a mempool. There is no specific mention made of |
| 2163 |
headers or superblocks. Nevertheless, the following picture may help |
| 2164 |
elucidate the intention of the terms in the API:</p> |
| 2165 |
<pre class="programlisting"> |
| 2166 |
"pool" |
| 2167 |
(anchor address) |
| 2168 |
| |
| 2169 |
v |
| 2170 |
+--------+---+ |
| 2171 |
| header | o | |
| 2172 |
+--------+-|-+ |
| 2173 |
| |
| 2174 |
v superblock |
| 2175 |
+------+---+--------------+---+------------------+ |
| 2176 |
| |rzB| allocation |rzB| | |
| 2177 |
+------+---+--------------+---+------------------+ |
| 2178 |
^ ^ |
| 2179 |
| | |
| 2180 |
"addr" "addr"+"size" |
| 2181 |
</pre> |
| 2182 |
<p> |
| 2183 |
Note that the header and the superblock may be contiguous or |
| 2184 |
discontiguous, and there may be multiple superblocks associated with a |
| 2185 |
single header; such variations are opaque to Memcheck. The API |
| 2186 |
only requires that your allocation scheme can present sensible values |
| 2187 |
of "pool", "addr" and "size".</p> |
| 2188 |
<p> |
| 2189 |
Typically, before making client requests related to mempools, a client |
| 2190 |
program will have allocated such a header and superblock for their |
| 2191 |
mempool, and marked the superblock NOACCESS using the |
| 2192 |
<code class="varname">VALGRIND_MAKE_MEM_NOACCESS</code> client request.</p> |
| 2193 |
<p> |
| 2194 |
When dealing with mempools, the goal is to maintain a particular |
| 2195 |
invariant condition: that Memcheck believes the unallocated portions |
| 2196 |
of the pool's superblock (including redzones) are NOACCESS. To |
| 2197 |
maintain this invariant, the client program must ensure that the |
| 2198 |
superblock starts out in that state; Memcheck cannot make it so, since |
| 2199 |
Memcheck never explicitly learns about the superblock of a pool, only |
| 2200 |
the allocated chunks within the pool.</p> |
| 2201 |
<p> |
| 2202 |
Once the header and superblock for a pool are established and properly |
| 2203 |
marked, there are a number of client requests programs can use to |
| 2204 |
inform Memcheck about changes to the state of a mempool:</p> |
| 2205 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 2206 |
<li class="listitem"> |
| 2207 |
<p> |
| 2208 |
<code class="varname">VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed)</code>: |
| 2209 |
This request registers the address <code class="varname">pool</code> as the anchor |
| 2210 |
address for a memory pool. It also provides a size |
| 2211 |
<code class="varname">rzB</code>, specifying how large the redzones placed around |
| 2212 |
chunks allocated from the pool should be. Finally, it provides an |
| 2213 |
<code class="varname">is_zeroed</code> argument that specifies whether the pool's |
| 2214 |
chunks are zeroed (more precisely: defined) when allocated. |
| 2215 |
</p> |
| 2216 |
<p> |
| 2217 |
Upon completion of this request, no chunks are associated with the |
| 2218 |
pool. The request simply tells Memcheck that the pool exists, so that |
| 2219 |
subsequent calls can refer to it as a pool. |
| 2220 |
</p> |
| 2221 |
</li> |
| 2222 |
<li class="listitem"> |
| 2223 |
<p> |
| 2224 |
<code class="varname">VALGRIND_CREATE_MEMPOOL_EXT(pool, rzB, is_zeroed, flags)</code>: |
| 2225 |
Create a memory pool with some flags (that can |
| 2226 |
be OR-ed together) specifying extended behaviour. When flags is |
| 2227 |
zero, the behaviour is identical to |
| 2228 |
<code class="varname">VALGRIND_CREATE_MEMPOOL</code>.</p> |
| 2229 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "> |
| 2230 |
<li class="listitem"><p> The flag <code class="varname">VALGRIND_MEMPOOL_METAPOOL</code> |
| 2231 |
specifies that the pieces of memory associated with the pool |
| 2232 |
using <code class="varname">VALGRIND_MEMPOOL_ALLOC</code> will be used |
| 2233 |
by the application as superblocks to dole out MALLOC_LIKE |
| 2234 |
blocks using <code class="varname">VALGRIND_MALLOCLIKE_BLOCK</code>. |
| 2235 |
In other words, a meta pool is a "2 levels" pool : first |
| 2236 |
level is the blocks described |
| 2237 |
by <code class="varname">VALGRIND_MEMPOOL_ALLOC</code>. The second |
| 2238 |
level blocks are described |
| 2239 |
using <code class="varname">VALGRIND_MALLOCLIKE_BLOCK</code>. Note |
| 2240 |
that the association between the pool and the second level |
| 2241 |
blocks is implicit : second level blocks will be located |
| 2242 |
inside first level blocks. It is necessary to use |
| 2243 |
the <code class="varname">VALGRIND_MEMPOOL_METAPOOL</code> flag for |
| 2244 |
such 2 levels pools, as otherwise valgrind will detect |
| 2245 |
overlapping memory blocks, and will abort execution |
| 2246 |
(e.g. during leak search). |
| 2247 |
</p></li> |
| 2248 |
<li class="listitem"><p> |
| 2249 |
<code class="varname">VALGRIND_MEMPOOL_AUTO_FREE</code>. Such a meta |
| 2250 |
pool can also be marked as an 'auto free' pool using the |
| 2251 |
flag <code class="varname">VALGRIND_MEMPOOL_AUTO_FREE</code>, which |
| 2252 |
must be OR-ed together with |
| 2253 |
the <code class="varname">VALGRIND_MEMPOOL_METAPOOL</code>. For an |
| 2254 |
'auto free' pool, <code class="varname">VALGRIND_MEMPOOL_FREE</code> |
| 2255 |
will automatically free the second level blocks that are |
| 2256 |
contained inside the first level block freed |
| 2257 |
with <code class="varname">VALGRIND_MEMPOOL_FREE</code>. In other |
| 2258 |
words, calling <code class="varname">VALGRIND_MEMPOOL_FREE</code> will |
| 2259 |
cause implicit calls |
| 2260 |
to <code class="varname">VALGRIND_FREELIKE_BLOCK</code> for all the |
| 2261 |
second level blocks included in the first level block. |
| 2262 |
Note: it is an error to use |
| 2263 |
the <code class="varname">VALGRIND_MEMPOOL_AUTO_FREE</code> flag |
| 2264 |
without the |
| 2265 |
<code class="varname">VALGRIND_MEMPOOL_METAPOOL</code> flag. |
| 2266 |
</p></li> |
| 2267 |
</ul></div> |
| 2268 |
</li> |
| 2269 |
<li class="listitem"><p><code class="varname">VALGRIND_DESTROY_MEMPOOL(pool)</code>: |
| 2270 |
This request tells Memcheck that a pool is being torn down. Memcheck |
| 2271 |
then removes all records of chunks associated with the pool, as well |
| 2272 |
as its record of the pool's existence. While destroying its records of |
| 2273 |
a mempool, Memcheck resets the redzones of any live chunks in the pool |
| 2274 |
to NOACCESS. |
| 2275 |
</p></li> |
| 2276 |
<li class="listitem"><p><code class="varname">VALGRIND_MEMPOOL_ALLOC(pool, addr, size)</code>: |
| 2277 |
This request informs Memcheck that a <code class="varname">size</code>-byte chunk |
| 2278 |
has been allocated at <code class="varname">addr</code>, and associates the chunk with the |
| 2279 |
specified |
| 2280 |
<code class="varname">pool</code>. If the pool was created with nonzero |
| 2281 |
<code class="varname">rzB</code> redzones, Memcheck will mark the |
| 2282 |
<code class="varname">rzB</code> bytes before and after the chunk as NOACCESS. If |
| 2283 |
the pool was created with the <code class="varname">is_zeroed</code> argument set, |
| 2284 |
Memcheck will mark the chunk as DEFINED, otherwise Memcheck will mark |
| 2285 |
the chunk as UNDEFINED. |
| 2286 |
</p></li> |
| 2287 |
<li class="listitem"><p><code class="varname">VALGRIND_MEMPOOL_FREE(pool, addr)</code>: |
| 2288 |
This request informs Memcheck that the chunk at <code class="varname">addr</code> |
| 2289 |
should no longer be considered allocated. Memcheck will mark the chunk |
| 2290 |
associated with <code class="varname">addr</code> as NOACCESS, and delete its |
| 2291 |
record of the chunk's existence. |
| 2292 |
</p></li> |
| 2293 |
<li class="listitem"> |
| 2294 |
<p><code class="varname">VALGRIND_MEMPOOL_TRIM(pool, addr, size)</code>: |
| 2295 |
This request trims the chunks associated with <code class="varname">pool</code>. |
| 2296 |
The request only operates on chunks associated with |
| 2297 |
<code class="varname">pool</code>. Trimming is formally defined as:</p> |
| 2298 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "> |
| 2299 |
<li class="listitem"><p> All chunks entirely inside the range |
| 2300 |
<code class="varname">addr..(addr+size-1)</code> are preserved.</p></li> |
| 2301 |
<li class="listitem"><p>All chunks entirely outside the range |
| 2302 |
<code class="varname">addr..(addr+size-1)</code> are discarded, as though |
| 2303 |
<code class="varname">VALGRIND_MEMPOOL_FREE</code> was called on them. </p></li> |
| 2304 |
<li class="listitem"><p>All other chunks must intersect with the range |
| 2305 |
<code class="varname">addr..(addr+size-1)</code>; areas outside the |
| 2306 |
intersection are marked as NOACCESS, as though they had been |
| 2307 |
independently freed with |
| 2308 |
<code class="varname">VALGRIND_MEMPOOL_FREE</code>.</p></li> |
| 2309 |
</ul></div> |
| 2310 |
<p>This is a somewhat rare request, but can be useful in |
| 2311 |
implementing the type of mass-free operations common in custom |
| 2312 |
LIFO allocators.</p> |
| 2313 |
</li> |
| 2314 |
<li class="listitem"> |
| 2315 |
<p><code class="varname">VALGRIND_MOVE_MEMPOOL(poolA, poolB)</code>: This |
| 2316 |
request informs Memcheck that the pool previously anchored at |
| 2317 |
address <code class="varname">poolA</code> has moved to anchor address |
| 2318 |
<code class="varname">poolB</code>. This is a rare request, typically only needed |
| 2319 |
if you <code class="function">realloc</code> the header of a mempool.</p> |
| 2320 |
<p>No memory-status bits are altered by this request.</p> |
| 2321 |
</li> |
| 2322 |
<li class="listitem"> |
| 2323 |
<p> |
| 2324 |
<code class="varname">VALGRIND_MEMPOOL_CHANGE(pool, addrA, addrB, |
| 2325 |
size)</code>: This request informs Memcheck that the chunk |
| 2326 |
previously allocated at address <code class="varname">addrA</code> within |
| 2327 |
<code class="varname">pool</code> has been moved and/or resized, and should be |
| 2328 |
changed to cover the region <code class="varname">addrB..(addrB+size-1)</code>. This |
| 2329 |
is a rare request, typically only needed if you |
| 2330 |
<code class="function">realloc</code> a superblock or wish to extend a chunk |
| 2331 |
without changing its memory-status bits. |
| 2332 |
</p> |
| 2333 |
<p>No memory-status bits are altered by this request. |
| 2334 |
</p> |
| 2335 |
</li> |
| 2336 |
<li class="listitem"><p><code class="varname">VALGRIND_MEMPOOL_EXISTS(pool)</code>: |
| 2337 |
This request informs the caller whether or not Memcheck is currently |
| 2338 |
tracking a mempool at anchor address <code class="varname">pool</code>. It |
| 2339 |
evaluates to 1 when there is a mempool associated with that address, 0 |
| 2340 |
otherwise. This is a rare request, only useful in circumstances when |
| 2341 |
client code might have lost track of the set of active mempools. |
| 2342 |
</p></li> |
| 2343 |
</ul></div> |
| 2344 |
</div> |
| 2345 |
<div class="sect1"> |
| 2346 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 2347 |
<a name="mc-manual.mpiwrap"></a>4.9. Debugging MPI Parallel Programs with Valgrind</h2></div></div></div> |
| 2348 |
<p>Memcheck supports debugging of distributed-memory applications |
| 2349 |
which use the MPI message passing standard. This support consists of a |
| 2350 |
library of wrapper functions for the |
| 2351 |
<code class="computeroutput">PMPI_*</code> interface. When incorporated |
| 2352 |
into the application's address space, either by direct linking or by |
| 2353 |
<code class="computeroutput">LD_PRELOAD</code>, the wrappers intercept |
| 2354 |
calls to <code class="computeroutput">PMPI_Send</code>, |
| 2355 |
<code class="computeroutput">PMPI_Recv</code>, etc. They then |
| 2356 |
use client requests to inform Memcheck of memory state changes caused |
| 2357 |
by the function being wrapped. This reduces the number of false |
| 2358 |
positives that Memcheck otherwise typically reports for MPI |
| 2359 |
applications.</p> |
| 2360 |
<p>The wrappers also take the opportunity to carefully check |
| 2361 |
size and definedness of buffers passed as arguments to MPI functions, hence |
| 2362 |
detecting errors such as passing undefined data to |
| 2363 |
<code class="computeroutput">PMPI_Send</code>, or receiving data into a |
| 2364 |
buffer which is too small.</p> |
| 2365 |
<p>Unlike most of the rest of Valgrind, the wrapper library is subject to a |
| 2366 |
BSD-style license, so you can link it into any code base you like. |
| 2367 |
See the top of <code class="computeroutput">mpi/libmpiwrap.c</code> |
| 2368 |
for license details.</p> |
| 2369 |
<div class="sect2"> |
| 2370 |
<div class="titlepage"><div><div><h3 class="title"> |
| 2371 |
<a name="mc-manual.mpiwrap.build"></a>4.9.1. Building and installing the wrappers</h3></div></div></div> |
| 2372 |
<p> The wrapper library will be built automatically if possible. |
| 2373 |
Valgrind's configure script will look for a suitable |
| 2374 |
<code class="computeroutput">mpicc</code> to build it with. This must be |
| 2375 |
the same <code class="computeroutput">mpicc</code> you use to build the |
| 2376 |
MPI application you want to debug. By default, Valgrind tries |
| 2377 |
<code class="computeroutput">mpicc</code>, but you can specify a |
| 2378 |
different one by using the configure-time option |
| 2379 |
<code class="option">--with-mpicc</code>. Currently the |
| 2380 |
wrappers are only buildable with |
| 2381 |
<code class="computeroutput">mpicc</code>s which are based on GNU |
| 2382 |
GCC or Intel's C++ Compiler.</p> |
| 2383 |
<p>Check that the configure script prints a line like this:</p> |
| 2384 |
<pre class="programlisting"> |
| 2385 |
checking for usable MPI2-compliant mpicc and mpi.h... yes, mpicc |
| 2386 |
</pre> |
| 2387 |
<p>If it says <code class="computeroutput">... no</code>, your |
| 2388 |
<code class="computeroutput">mpicc</code> has failed to compile and link |
| 2389 |
a test MPI2 program.</p> |
| 2390 |
<p>If the configure test succeeds, continue in the usual way with |
| 2391 |
<code class="computeroutput">make</code> and <code class="computeroutput">make |
| 2392 |
install</code>. The final install tree should then contain |
| 2393 |
<code class="computeroutput">libmpiwrap-<platform>.so</code>. |
| 2394 |
</p> |
| 2395 |
<p>Compile up a test MPI program (eg, MPI hello-world) and try |
| 2396 |
this:</p> |
| 2397 |
<pre class="programlisting"> |
| 2398 |
LD_PRELOAD=$prefix/lib/valgrind/libmpiwrap-<platform>.so \ |
| 2399 |
mpirun [args] $prefix/bin/valgrind ./hello |
| 2400 |
</pre> |
| 2401 |
<p>You should see something similar to the following</p> |
| 2402 |
<pre class="programlisting"> |
| 2403 |
valgrind MPI wrappers 31901: Active for pid 31901 |
| 2404 |
valgrind MPI wrappers 31901: Try MPIWRAP_DEBUG=help for possible options |
| 2405 |
</pre> |
| 2406 |
<p>repeated for every process in the group. If you do not see |
| 2407 |
these, there is an build/installation problem of some kind.</p> |
| 2408 |
<p> The MPI functions to be wrapped are assumed to be in an ELF |
| 2409 |
shared object with soname matching |
| 2410 |
<code class="computeroutput">libmpi.so*</code>. This is known to be |
| 2411 |
correct at least for Open MPI and Quadrics MPI, and can easily be |
| 2412 |
changed if required.</p> |
| 2413 |
</div> |
| 2414 |
<div class="sect2"> |
| 2415 |
<div class="titlepage"><div><div><h3 class="title"> |
| 2416 |
<a name="mc-manual.mpiwrap.gettingstarted"></a>4.9.2. Getting started</h3></div></div></div> |
| 2417 |
<p>Compile your MPI application as usual, taking care to link it |
| 2418 |
using the same <code class="computeroutput">mpicc</code> that your |
| 2419 |
Valgrind build was configured with.</p> |
| 2420 |
<p> |
| 2421 |
Use the following basic scheme to run your application on Valgrind with |
| 2422 |
the wrappers engaged:</p> |
| 2423 |
<pre class="programlisting"> |
| 2424 |
MPIWRAP_DEBUG=[wrapper-args] \ |
| 2425 |
LD_PRELOAD=$prefix/lib/valgrind/libmpiwrap-<platform>.so \ |
| 2426 |
mpirun [mpirun-args] \ |
| 2427 |
$prefix/bin/valgrind [valgrind-args] \ |
| 2428 |
[application] [app-args] |
| 2429 |
</pre> |
| 2430 |
<p>As an alternative to |
| 2431 |
<code class="computeroutput">LD_PRELOAD</code>ing |
| 2432 |
<code class="computeroutput">libmpiwrap-<platform>.so</code>, you can |
| 2433 |
simply link it to your application if desired. This should not disturb |
| 2434 |
native behaviour of your application in any way.</p> |
| 2435 |
</div> |
| 2436 |
<div class="sect2"> |
| 2437 |
<div class="titlepage"><div><div><h3 class="title"> |
| 2438 |
<a name="mc-manual.mpiwrap.controlling"></a>4.9.3. Controlling the wrapper library</h3></div></div></div> |
| 2439 |
<p>Environment variable |
| 2440 |
<code class="computeroutput">MPIWRAP_DEBUG</code> is consulted at |
| 2441 |
startup. The default behaviour is to print a starting banner</p> |
| 2442 |
<pre class="programlisting"> |
| 2443 |
valgrind MPI wrappers 16386: Active for pid 16386 |
| 2444 |
valgrind MPI wrappers 16386: Try MPIWRAP_DEBUG=help for possible options |
| 2445 |
</pre> |
| 2446 |
<p> and then be relatively quiet.</p> |
| 2447 |
<p>You can give a list of comma-separated options in |
| 2448 |
<code class="computeroutput">MPIWRAP_DEBUG</code>. These are</p> |
| 2449 |
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> |
| 2450 |
<li class="listitem"><p><code class="computeroutput">verbose</code>: |
| 2451 |
show entries/exits of all wrappers. Also show extra |
| 2452 |
debugging info, such as the status of outstanding |
| 2453 |
<code class="computeroutput">MPI_Request</code>s resulting |
| 2454 |
from uncompleted <code class="computeroutput">MPI_Irecv</code>s.</p></li> |
| 2455 |
<li class="listitem"><p><code class="computeroutput">quiet</code>: |
| 2456 |
opposite of <code class="computeroutput">verbose</code>, only print |
| 2457 |
anything when the wrappers want |
| 2458 |
to report a detected programming error, or in case of catastrophic |
| 2459 |
failure of the wrappers.</p></li> |
| 2460 |
<li class="listitem"><p><code class="computeroutput">warn</code>: |
| 2461 |
by default, functions which lack proper wrappers |
| 2462 |
are not commented on, just silently |
| 2463 |
ignored. This causes a warning to be printed for each unwrapped |
| 2464 |
function used, up to a maximum of three warnings per function.</p></li> |
| 2465 |
<li class="listitem"><p><code class="computeroutput">strict</code>: |
| 2466 |
print an error message and abort the program if |
| 2467 |
a function lacking a wrapper is used.</p></li> |
| 2468 |
</ul></div> |
| 2469 |
<p> If you want to use Valgrind's XML output facility |
| 2470 |
(<code class="option">--xml=yes</code>), you should pass |
| 2471 |
<code class="computeroutput">quiet</code> in |
| 2472 |
<code class="computeroutput">MPIWRAP_DEBUG</code> so as to get rid of any |
| 2473 |
extraneous printing from the wrappers.</p> |
| 2474 |
</div> |
| 2475 |
<div class="sect2"> |
| 2476 |
<div class="titlepage"><div><div><h3 class="title"> |
| 2477 |
<a name="mc-manual.mpiwrap.limitations.functions"></a>4.9.4. Functions</h3></div></div></div> |
| 2478 |
<p>All MPI2 functions except |
| 2479 |
<code class="computeroutput">MPI_Wtick</code>, |
| 2480 |
<code class="computeroutput">MPI_Wtime</code> and |
| 2481 |
<code class="computeroutput">MPI_Pcontrol</code> have wrappers. The |
| 2482 |
first two are not wrapped because they return a |
| 2483 |
<code class="computeroutput">double</code>, which Valgrind's |
| 2484 |
function-wrap mechanism cannot handle (but it could easily be |
| 2485 |
extended to do so). <code class="computeroutput">MPI_Pcontrol</code> cannot be |
| 2486 |
wrapped as it has variable arity: |
| 2487 |
<code class="computeroutput">int MPI_Pcontrol(const int level, ...)</code></p> |
| 2488 |
<p>Most functions are wrapped with a default wrapper which does |
| 2489 |
nothing except complain or abort if it is called, depending on |
| 2490 |
settings in <code class="computeroutput">MPIWRAP_DEBUG</code> listed |
| 2491 |
above. The following functions have "real", do-something-useful |
| 2492 |
wrappers:</p> |
| 2493 |
<pre class="programlisting"> |
| 2494 |
PMPI_Send PMPI_Bsend PMPI_Ssend PMPI_Rsend |
| 2495 |
|
| 2496 |
PMPI_Recv PMPI_Get_count |
| 2497 |
|
| 2498 |
PMPI_Isend PMPI_Ibsend PMPI_Issend PMPI_Irsend |
| 2499 |
|
| 2500 |
PMPI_Irecv |
| 2501 |
PMPI_Wait PMPI_Waitall |
| 2502 |
PMPI_Test PMPI_Testall |
| 2503 |
|
| 2504 |
PMPI_Iprobe PMPI_Probe |
| 2505 |
|
| 2506 |
PMPI_Cancel |
| 2507 |
|
| 2508 |
PMPI_Sendrecv |
| 2509 |
|
| 2510 |
PMPI_Type_commit PMPI_Type_free |
| 2511 |
|
| 2512 |
PMPI_Pack PMPI_Unpack |
| 2513 |
|
| 2514 |
PMPI_Bcast PMPI_Gather PMPI_Scatter PMPI_Alltoall |
| 2515 |
PMPI_Reduce PMPI_Allreduce PMPI_Op_create |
| 2516 |
|
| 2517 |
PMPI_Comm_create PMPI_Comm_dup PMPI_Comm_free PMPI_Comm_rank PMPI_Comm_size |
| 2518 |
|
| 2519 |
PMPI_Error_string |
| 2520 |
PMPI_Init PMPI_Initialized PMPI_Finalize |
| 2521 |
</pre> |
| 2522 |
<p> A few functions such as |
| 2523 |
<code class="computeroutput">PMPI_Address</code> are listed as |
| 2524 |
<code class="computeroutput">HAS_NO_WRAPPER</code>. They have no wrapper |
| 2525 |
at all as there is nothing worth checking, and giving a no-op wrapper |
| 2526 |
would reduce performance for no reason.</p> |
| 2527 |
<p> Note that the wrapper library itself can itself generate large |
| 2528 |
numbers of calls to the MPI implementation, especially when walking |
| 2529 |
complex types. The most common functions called are |
| 2530 |
<code class="computeroutput">PMPI_Extent</code>, |
| 2531 |
<code class="computeroutput">PMPI_Type_get_envelope</code>, |
| 2532 |
<code class="computeroutput">PMPI_Type_get_contents</code>, and |
| 2533 |
<code class="computeroutput">PMPI_Type_free</code>. </p> |
| 2534 |
</div> |
| 2535 |
<div class="sect2"> |
| 2536 |
<div class="titlepage"><div><div><h3 class="title"> |
| 2537 |
<a name="mc-manual.mpiwrap.limitations.types"></a>4.9.5. Types</h3></div></div></div> |
| 2538 |
<p> MPI-1.1 structured types are supported, and walked exactly. |
| 2539 |
The currently supported combiners are |
| 2540 |
<code class="computeroutput">MPI_COMBINER_NAMED</code>, |
| 2541 |
<code class="computeroutput">MPI_COMBINER_CONTIGUOUS</code>, |
| 2542 |
<code class="computeroutput">MPI_COMBINER_VECTOR</code>, |
| 2543 |
<code class="computeroutput">MPI_COMBINER_HVECTOR</code> |
| 2544 |
<code class="computeroutput">MPI_COMBINER_INDEXED</code>, |
| 2545 |
<code class="computeroutput">MPI_COMBINER_HINDEXED</code> and |
| 2546 |
<code class="computeroutput">MPI_COMBINER_STRUCT</code>. This should |
| 2547 |
cover all MPI-1.1 types. The mechanism (function |
| 2548 |
<code class="computeroutput">walk_type</code>) should extend easily to |
| 2549 |
cover MPI2 combiners.</p> |
| 2550 |
<p>MPI defines some named structured types |
| 2551 |
(<code class="computeroutput">MPI_FLOAT_INT</code>, |
| 2552 |
<code class="computeroutput">MPI_DOUBLE_INT</code>, |
| 2553 |
<code class="computeroutput">MPI_LONG_INT</code>, |
| 2554 |
<code class="computeroutput">MPI_2INT</code>, |
| 2555 |
<code class="computeroutput">MPI_SHORT_INT</code>, |
| 2556 |
<code class="computeroutput">MPI_LONG_DOUBLE_INT</code>) which are pairs |
| 2557 |
of some basic type and a C <code class="computeroutput">int</code>. |
| 2558 |
Unfortunately the MPI specification makes it impossible to look inside |
| 2559 |
these types and see where the fields are. Therefore these wrappers |
| 2560 |
assume the types are laid out as <code class="computeroutput">struct { float val; |
| 2561 |
int loc; }</code> (for |
| 2562 |
<code class="computeroutput">MPI_FLOAT_INT</code>), etc, and act |
| 2563 |
accordingly. This appears to be correct at least for Open MPI 1.0.2 |
| 2564 |
and for Quadrics MPI.</p> |
| 2565 |
<p>If <code class="computeroutput">strict</code> is an option specified |
| 2566 |
in <code class="computeroutput">MPIWRAP_DEBUG</code>, the application |
| 2567 |
will abort if an unhandled type is encountered. Otherwise, the |
| 2568 |
application will print a warning message and continue.</p> |
| 2569 |
<p>Some effort is made to mark/check memory ranges corresponding to |
| 2570 |
arrays of values in a single pass. This is important for performance |
| 2571 |
since asking Valgrind to mark/check any range, no matter how small, |
| 2572 |
carries quite a large constant cost. This optimisation is applied to |
| 2573 |
arrays of primitive types (<code class="computeroutput">double</code>, |
| 2574 |
<code class="computeroutput">float</code>, |
| 2575 |
<code class="computeroutput">int</code>, |
| 2576 |
<code class="computeroutput">long</code>, <code class="computeroutput">long |
| 2577 |
long</code>, <code class="computeroutput">short</code>, |
| 2578 |
<code class="computeroutput">char</code>, and <code class="computeroutput">long |
| 2579 |
double</code> on platforms where <code class="computeroutput">sizeof(long |
| 2580 |
double) == 8</code>). For arrays of all other types, the |
| 2581 |
wrappers handle each element individually and so there can be a very |
| 2582 |
large performance cost.</p> |
| 2583 |
</div> |
| 2584 |
<div class="sect2"> |
| 2585 |
<div class="titlepage"><div><div><h3 class="title"> |
| 2586 |
<a name="mc-manual.mpiwrap.writingwrappers"></a>4.9.6. Writing new wrappers</h3></div></div></div> |
| 2587 |
<p> |
| 2588 |
For the most part the wrappers are straightforward. The only |
| 2589 |
significant complexity arises with nonblocking receives.</p> |
| 2590 |
<p>The issue is that <code class="computeroutput">MPI_Irecv</code> |
| 2591 |
states the recv buffer and returns immediately, giving a handle |
| 2592 |
(<code class="computeroutput">MPI_Request</code>) for the transaction. |
| 2593 |
Later the user will have to poll for completion with |
| 2594 |
<code class="computeroutput">MPI_Wait</code> etc, and when the |
| 2595 |
transaction completes successfully, the wrappers have to paint the |
| 2596 |
recv buffer. But the recv buffer details are not presented to |
| 2597 |
<code class="computeroutput">MPI_Wait</code> -- only the handle is. The |
| 2598 |
library therefore maintains a shadow table which associates |
| 2599 |
uncompleted <code class="computeroutput">MPI_Request</code>s with the |
| 2600 |
corresponding buffer address/count/type. When an operation completes, |
| 2601 |
the table is searched for the associated address/count/type info, and |
| 2602 |
memory is marked accordingly.</p> |
| 2603 |
<p>Access to the table is guarded by a (POSIX pthreads) lock, so as |
| 2604 |
to make the library thread-safe.</p> |
| 2605 |
<p>The table is allocated with |
| 2606 |
<code class="computeroutput">malloc</code> and never |
| 2607 |
<code class="computeroutput">free</code>d, so it will show up in leak |
| 2608 |
checks.</p> |
| 2609 |
<p>Writing new wrappers should be fairly easy. The source file is |
| 2610 |
<code class="computeroutput">mpi/libmpiwrap.c</code>. If possible, |
| 2611 |
find an existing wrapper for a function of similar behaviour to the |
| 2612 |
one you want to wrap, and use it as a starting point. The wrappers |
| 2613 |
are organised in sections in the same order as the MPI 1.1 spec, to |
| 2614 |
aid navigation. When adding a wrapper, remember to comment out the |
| 2615 |
definition of the default wrapper in the long list of defaults at the |
| 2616 |
bottom of the file (do not remove it, just comment it out).</p> |
| 2617 |
</div> |
| 2618 |
<div class="sect2"> |
| 2619 |
<div class="titlepage"><div><div><h3 class="title"> |
| 2620 |
<a name="mc-manual.mpiwrap.whattoexpect"></a>4.9.7. What to expect when using the wrappers</h3></div></div></div> |
| 2621 |
<p>The wrappers should reduce Memcheck's false-error rate on MPI |
| 2622 |
applications. Because the wrapping is done at the MPI interface, |
| 2623 |
there will still potentially be a large number of errors reported in |
| 2624 |
the MPI implementation below the interface. The best you can do is |
| 2625 |
try to suppress them.</p> |
| 2626 |
<p>You may also find that the input-side (buffer |
| 2627 |
length/definedness) checks find errors in your MPI use, for example |
| 2628 |
passing too short a buffer to |
| 2629 |
<code class="computeroutput">MPI_Recv</code>.</p> |
| 2630 |
<p>Functions which are not wrapped may increase the false |
| 2631 |
error rate. A possible approach is to run with |
| 2632 |
<code class="computeroutput">MPI_DEBUG</code> containing |
| 2633 |
<code class="computeroutput">warn</code>. This will show you functions |
| 2634 |
which lack proper wrappers but which are nevertheless used. You can |
| 2635 |
then write wrappers for them. |
| 2636 |
</p> |
| 2637 |
<p>A known source of potential false errors are the |
| 2638 |
<code class="computeroutput">PMPI_Reduce</code> family of functions, when |
| 2639 |
using a custom (user-defined) reduction function. In a reduction |
| 2640 |
operation, each node notionally sends data to a "central point" which |
| 2641 |
uses the specified reduction function to merge the data items into a |
| 2642 |
single item. Hence, in general, data is passed between nodes and fed |
| 2643 |
to the reduction function, but the wrapper library cannot mark the |
| 2644 |
transferred data as initialised before it is handed to the reduction |
| 2645 |
function, because all that happens "inside" the |
| 2646 |
<code class="computeroutput">PMPI_Reduce</code> call. As a result you |
| 2647 |
may see false positives reported in your reduction function.</p> |
| 2648 |
</div> |
| 2649 |
</div> |
| 2650 |
</div> |
| 2651 |
<div> |
| 2652 |
<br><table class="nav" width="100%" cellspacing="3" cellpadding="2" border="0" summary="Navigation footer"> |
| 2653 |
<tr> |
| 2654 |
<td rowspan="2" width="40%" align="left"> |
| 2655 |
<a accesskey="p" href="manual-core-adv.html"><< 3. Using and understanding the Valgrind core: Advanced Topics</a> </td> |
| 2656 |
<td width="20%" align="center"><a accesskey="u" href="manual.html">Up</a></td> |
| 2657 |
<td rowspan="2" width="40%" align="right"> <a accesskey="n" href="cg-manual.html">5. Cachegrind: a high-precision tracing profiler >></a> |
| 2658 |
</td> |
| 2659 |
</tr> |
| 2660 |
<tr><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td></tr> |
| 2661 |
</table> |
| 2662 |
</div> |
| 2663 |
</body> |
| 2664 |
</html> |