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