njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 1 | <?xml version="1.0"?> <!-- -*- sgml -*- --> |
| 2 | <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" |
| 3 | "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" |
| 4 | [ <!ENTITY % vg-entities SYSTEM "vg-entities.xml"> %vg-entities; ]> |
| 5 | |
de | 252c614 | 2005-11-27 04:10:00 +0000 | [diff] [blame] | 6 | <book id="QuickStart" xreflabel="Valgrind Quick Start Guide"> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 7 | |
de | 53ad684 | 2005-11-19 03:28:10 +0000 | [diff] [blame] | 8 | <bookinfo> |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 9 | <title>The Valgrind Quick Start Guide</title> |
| 10 | <releaseinfo>&rel-type; &rel-version; &rel-date;</releaseinfo> |
| 11 | <copyright> |
| 12 | <year>&vg-lifespan;</year> |
| 13 | <holder><ulink url="&vg-developers;">Valgrind Developers</ulink></holder> |
| 14 | </copyright> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 15 | <legalnotice> |
| 16 | <para>Email: <ulink url="mailto:&vg-vemail;">&vg-vemail;</ulink></para> |
| 17 | </legalnotice> |
de | 53ad684 | 2005-11-19 03:28:10 +0000 | [diff] [blame] | 18 | </bookinfo> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 19 | |
| 20 | |
de | 252c614 | 2005-11-27 04:10:00 +0000 | [diff] [blame] | 21 | <article id="quick-start"> |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 22 | <title>The Valgrind Quick Start Guide</title> |
| 23 | |
| 24 | |
| 25 | <sect1 id="quick-start.intro" xreflabel="Introduction"> |
| 26 | <title>Introduction</title> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 27 | |
sewardj | 08e31e2 | 2007-05-23 21:58:33 +0000 | [diff] [blame] | 28 | <para>The Valgrind tool suite provides a number of debugging and |
| 29 | profiling tools. The most popular is |
| 30 | Memcheck, a memory checking tool which can detect many common |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 31 | memory errors such as:</para> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 32 | |
| 33 | <itemizedlist> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 34 | <listitem> |
sewardj | 778d783 | 2007-11-22 01:21:56 +0000 | [diff] [blame] | 35 | <para>Touching memory you shouldn't (eg. overrunning heap block |
| 36 | boundaries, or reading/writing freed memory).</para> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 37 | </listitem> |
| 38 | <listitem> |
sewardj | 778d783 | 2007-11-22 01:21:56 +0000 | [diff] [blame] | 39 | <para>Using values before they have been initialized.</para> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 40 | </listitem> |
| 41 | <listitem> |
sewardj | 778d783 | 2007-11-22 01:21:56 +0000 | [diff] [blame] | 42 | <para>Incorrect freeing of memory, such as double-freeing heap |
| 43 | blocks.</para> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 44 | </listitem> |
| 45 | <listitem> |
sewardj | 778d783 | 2007-11-22 01:21:56 +0000 | [diff] [blame] | 46 | <para>Memory leaks.</para> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 47 | </listitem> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 48 | </itemizedlist> |
| 49 | |
sewardj | 778d783 | 2007-11-22 01:21:56 +0000 | [diff] [blame] | 50 | <para>Memcheck is only one of the tools in the Valgrind suite. |
| 51 | Other tools you may find useful are:</para> |
| 52 | |
| 53 | <itemizedlist> |
| 54 | <listitem> |
| 55 | <para>Cachegrind: a profiling tool which produces detailed data on |
| 56 | cache (miss) and branch (misprediction) events. Statistics are |
sewardj | e01c505 | 2007-11-25 00:56:51 +0000 | [diff] [blame] | 57 | gathered for the entire program, for each function, and for each |
| 58 | line of code, if you need that level of detail.</para> |
sewardj | 778d783 | 2007-11-22 01:21:56 +0000 | [diff] [blame] | 59 | </listitem> |
| 60 | <listitem> |
sewardj | e01c505 | 2007-11-25 00:56:51 +0000 | [diff] [blame] | 61 | <para>Callgrind: a profiling tool that shows cost relationships |
| 62 | across function calls, optionally with cache simulation similar to |
| 63 | Cachegrind. Information gathered by Callgrind can be viewed |
| 64 | either with an included command line tool, or by using the |
| 65 | KCachegrind GUI. KCachegrind is not part of the Valgrind suite |
| 66 | -- it is part of the KDE Desktop Environment.</para> |
sewardj | 778d783 | 2007-11-22 01:21:56 +0000 | [diff] [blame] | 67 | </listitem> |
| 68 | <listitem> |
| 69 | <para>Massif: a space profiling tool. It allows you to explore |
| 70 | in detail which parts of your program allocate memory.</para> |
| 71 | </listitem> |
| 72 | <listitem> |
| 73 | <para>Helgrind: a debugging tool for threaded programs. Helgrind |
| 74 | looks for various kinds of synchronisation errors in code that uses |
| 75 | the POSIX PThreads API.</para> |
| 76 | </listitem> |
| 77 | <listitem> |
| 78 | <para>In addition, there are a number of "experimental" tools in |
| 79 | the codebase. They can be distinguished by the "exp-" prefix on |
| 80 | their names. Experimental tools are not subject to the same |
| 81 | quality control standards that apply to our production-grade tools |
sewardj | af57d5b | 2008-12-22 01:00:15 +0000 | [diff] [blame] | 82 | (Memcheck, Cachegrind, Callgrind, Massif, Helgrind and DRD).</para> |
sewardj | 778d783 | 2007-11-22 01:21:56 +0000 | [diff] [blame] | 83 | </listitem> |
| 84 | </itemizedlist> |
| 85 | |
| 86 | <para>The rest of this guide discusses only the Memcheck tool. For |
sewardj | e01c505 | 2007-11-25 00:56:51 +0000 | [diff] [blame] | 87 | full documentation on the other tools, and for Memcheck, see the |
| 88 | Valgrind User Manual.</para> |
sewardj | 778d783 | 2007-11-22 01:21:56 +0000 | [diff] [blame] | 89 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 90 | <para>What follows is the minimum information you need to start |
| 91 | detecting memory errors in your program with Memcheck. Note that this |
sewardj | af57d5b | 2008-12-22 01:00:15 +0000 | [diff] [blame] | 92 | guide applies to Valgrind version 3.4.0 and later. Some of the |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 93 | information is not quite right for earlier versions.</para> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 94 | |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 95 | </sect1> |
| 96 | |
| 97 | |
| 98 | <sect1 id="quick-start.prepare" xreflabel="Preparing your program"> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 99 | <title>Preparing your program</title> |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 100 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 101 | <para>Compile your program with <option>-g</option> to include debugging |
| 102 | information so that Memcheck's error messages include exact line |
| 103 | numbers. Using <computeroutput>-O0</computeroutput> is also a good |
| 104 | idea, if you can tolerate the slowdown. With |
| 105 | <computeroutput>-O1</computeroutput> line numbers in error messages can |
| 106 | be inaccurate, although generally speaking Memchecking code compiled at |
sewardj | af57d5b | 2008-12-22 01:00:15 +0000 | [diff] [blame] | 107 | <computeroutput>-O1</computeroutput> works fairly well and is |
| 108 | recommended. Use of |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 109 | <computeroutput>-O2</computeroutput> and above is not recommended as |
| 110 | Memcheck occasionally reports uninitialised-value errors which don't |
| 111 | really exist.</para> |
| 112 | |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 113 | </sect1> |
| 114 | |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 115 | |
| 116 | <sect1 id="quick-start.mcrun" xreflabel="Running your program under Memcheck"> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 117 | <title>Running your program under Memcheck</title> |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 118 | |
sewardj | af57d5b | 2008-12-22 01:00:15 +0000 | [diff] [blame] | 119 | <para>If you normally run your program like this:</para> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 120 | <programlisting> myprog arg1 arg2 |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 121 | </programlisting> |
| 122 | |
sewardj | af57d5b | 2008-12-22 01:00:15 +0000 | [diff] [blame] | 123 | <para>Use this command line:</para> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 124 | <programlisting> valgrind --leak-check=yes myprog arg1 arg2 |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 125 | </programlisting> |
| 126 | |
sewardj | af57d5b | 2008-12-22 01:00:15 +0000 | [diff] [blame] | 127 | <para>Memcheck is the default tool. The <option>--leak-check</option> |
| 128 | option turns on the detailed memory leak detector.</para> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 129 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 130 | <para>Your program will run much slower (eg. 20 to 30 times) than |
| 131 | normal, and use a lot more memory. Memcheck will issue messages about |
| 132 | memory errors and leaks that it detects.</para> |
| 133 | |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 134 | </sect1> |
| 135 | |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 136 | |
de | ccde45e | 2005-06-12 10:23:23 +0000 | [diff] [blame] | 137 | <sect1 id="quick-start.interpret" |
| 138 | xreflabel="Interpreting Memcheck's output"> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 139 | <title>Interpreting Memcheck's output</title> |
sewardj | af57d5b | 2008-12-22 01:00:15 +0000 | [diff] [blame] | 140 | <para>Here's an example C program with a memory error and a memory |
| 141 | leak.</para> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 142 | |
| 143 | <programlisting> |
| 144 | #include <stdlib.h> |
| 145 | |
| 146 | void f(void) |
| 147 | { |
| 148 | int* x = malloc(10 * sizeof(int)); |
| 149 | x[10] = 0; // problem 1: heap block overrun |
| 150 | } // problem 2: memory leak -- x not freed |
| 151 | |
| 152 | int main(void) |
| 153 | { |
| 154 | f(); |
| 155 | return 0; |
| 156 | } |
| 157 | </programlisting> |
| 158 | |
sewardj | af57d5b | 2008-12-22 01:00:15 +0000 | [diff] [blame] | 159 | <para>Most error messages look like the following, which describes |
| 160 | problem 1, the heap block overrun:</para> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 161 | |
| 162 | <programlisting> |
| 163 | ==19182== Invalid write of size 4 |
| 164 | ==19182== at 0x804838F: f (example.c:6) |
| 165 | ==19182== by 0x80483AB: main (example.c:11) |
| 166 | ==19182== Address 0x1BA45050 is 0 bytes after a block of size 40 alloc'd |
| 167 | ==19182== at 0x1B8FF5CD: malloc (vg_replace_malloc.c:130) |
| 168 | ==19182== by 0x8048385: f (example.c:5) |
| 169 | ==19182== by 0x80483AB: main (example.c:11) |
| 170 | </programlisting> |
| 171 | |
sewardj | af57d5b | 2008-12-22 01:00:15 +0000 | [diff] [blame] | 172 | <para>Things to notice:</para> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 173 | |
| 174 | <itemizedlist> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 175 | <listitem> |
| 176 | <para>There is a lot of information in each error message; read it |
| 177 | carefully.</para> |
| 178 | </listitem> |
| 179 | <listitem> |
| 180 | <para>The 19182 is the process ID; it's usually unimportant.</para> |
| 181 | </listitem> |
| 182 | <listitem> |
| 183 | <para>The first line ("Invalid write...") tells you what kind of |
| 184 | error it is. Here, the program wrote to some memory it should not |
| 185 | have due to a heap block overrun.</para> |
| 186 | </listitem> |
| 187 | <listitem> |
| 188 | <para>Below the first line is a stack trace telling you where the |
| 189 | problem occurred. Stack traces can get quite large, and be |
| 190 | confusing, especially if you are using the C++ STL. Reading them |
| 191 | from the bottom up can help. If the stack trace is not big enough, |
| 192 | use the <option>--num-callers</option> option to make it |
| 193 | bigger.</para> |
| 194 | </listitem> |
| 195 | <listitem> |
| 196 | <para>The code addresses (eg. 0x804838F) are usually unimportant, but |
| 197 | occasionally crucial for tracking down weirder bugs.</para> |
| 198 | </listitem> |
| 199 | <listitem> |
| 200 | <para>Some error messages have a second component which describes |
| 201 | the memory address involved. This one shows that the written memory |
| 202 | is just past the end of a block allocated with malloc() on line 5 of |
| 203 | example.c.</para> |
| 204 | </listitem> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 205 | </itemizedlist> |
| 206 | |
sewardj | af57d5b | 2008-12-22 01:00:15 +0000 | [diff] [blame] | 207 | <para>It's worth fixing errors in the order they are reported, as |
| 208 | later errors can be caused by earlier errors. Failing to do this is a |
sewardj | 778d783 | 2007-11-22 01:21:56 +0000 | [diff] [blame] | 209 | common cause of difficulty with Memcheck.</para> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 210 | |
sewardj | af57d5b | 2008-12-22 01:00:15 +0000 | [diff] [blame] | 211 | <para>Memory leak messages look like this:</para> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 212 | |
| 213 | <programlisting> |
| 214 | ==19182== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1 |
| 215 | ==19182== at 0x1B8FF5CD: malloc (vg_replace_malloc.c:130) |
njn | bb9700d | 2005-08-15 04:40:57 +0000 | [diff] [blame] | 216 | ==19182== by 0x8048385: f (a.c:5) |
| 217 | ==19182== by 0x80483AB: main (a.c:11) |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 218 | </programlisting> |
| 219 | |
sewardj | af57d5b | 2008-12-22 01:00:15 +0000 | [diff] [blame] | 220 | <para>The stack trace tells you where the leaked memory was allocated. |
| 221 | Memcheck cannot tell you why the memory leaked, unfortunately. |
| 222 | (Ignore the "vg_replace_malloc.c", that's an implementation |
| 223 | detail.)</para> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 224 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 225 | <para>There are several kinds of leaks; the two most important |
sewardj | af57d5b | 2008-12-22 01:00:15 +0000 | [diff] [blame] | 226 | categories are:</para> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 227 | |
| 228 | <itemizedlist> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 229 | <listitem> |
| 230 | <para>"definitely lost": your program is leaking memory -- fix |
| 231 | it!</para> |
| 232 | </listitem> |
| 233 | <listitem> |
| 234 | <para>"probably lost": your program is leaking memory, unless you're |
| 235 | doing funny things with pointers (such as moving them to point to |
| 236 | the middle of a heap block).</para> |
| 237 | </listitem> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 238 | </itemizedlist> |
| 239 | |
sewardj | af57d5b | 2008-12-22 01:00:15 +0000 | [diff] [blame] | 240 | <para>It can be difficult to track down the root causes of |
| 241 | uninitialised-value errors reported by Memcheck. Try using |
| 242 | the <option>--track-origins=yes</option> to get extra information. |
| 243 | This makes Memcheck run slower, but the extra information you get |
| 244 | often saves a lot of time figuring out where the uninitialised values |
| 245 | are coming from.</para> |
| 246 | |
| 247 | <para>If you don't understand an error message, please consult |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 248 | <xref linkend="mc-manual.errormsgs"/> in the <xref linkend="manual"/> |
| 249 | which has examples of all the error messages Memcheck produces.</para> |
| 250 | |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 251 | </sect1> |
| 252 | |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 253 | |
de | ccde45e | 2005-06-12 10:23:23 +0000 | [diff] [blame] | 254 | <sect1 id="quick-start.caveats" xreflabel="Caveats"> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 255 | <title>Caveats</title> |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 256 | |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 257 | <para>Memcheck is not perfect; it occasionally produces false positives, |
njn | 2c091b8 | 2005-11-18 22:09:47 +0000 | [diff] [blame] | 258 | and there are mechanisms for suppressing these (see |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 259 | <xref linkend="manual-core.suppress"/> in the <xref linkend="manual"/>). |
| 260 | However, it is typically right 99% of the time, so you should be wary of |
| 261 | ignoring its error messages. After all, you wouldn't ignore warning |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 262 | messages produced by a compiler, right? The suppression mechanism is |
| 263 | also useful if Memcheck is reporting errors in library code that you |
sewardj | 08e31e2 | 2007-05-23 21:58:33 +0000 | [diff] [blame] | 264 | cannot change. The default suppression set hides a lot of these, but you |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 265 | may come across more.</para> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 266 | |
sewardj | 08e31e2 | 2007-05-23 21:58:33 +0000 | [diff] [blame] | 267 | <para>Memcheck cannot detect every memory error your program has. |
| 268 | For example, it can't detect out-of-range reads or writes to arrays |
| 269 | that are allocated statically or on the stack. But it should detect many |
| 270 | errors that could crash your program (eg. cause a segmentation |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 271 | fault).</para> |
| 272 | |
sewardj | 778d783 | 2007-11-22 01:21:56 +0000 | [diff] [blame] | 273 | <para>Try to make your program so clean that Memcheck reports no |
| 274 | errors. Once you achieve this state, it is much easier to see when |
| 275 | changes to the program cause Memcheck to report new errors. |
| 276 | Experience from several years of Memcheck use shows that it is |
| 277 | possible to make even huge programs run Memcheck-clean. For example, |
| 278 | large parts of KDE 3.5.X, and recent versions of OpenOffice.org |
| 279 | (2.3.0) are Memcheck-clean, or very close to it.</para> |
| 280 | |
| 281 | |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 282 | </sect1> |
| 283 | |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 284 | |
de | ccde45e | 2005-06-12 10:23:23 +0000 | [diff] [blame] | 285 | <sect1 id="quick-start.info" xreflabel="More Information"> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 286 | <title>More information</title> |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 287 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 288 | <para>Please consult the <xref linkend="FAQ"/> and the |
| 289 | <xref linkend="manual"/>, which have much more information. Note that |
| 290 | the other tools in the Valgrind distribution can be invoked with the |
| 291 | <option>--tool</option> option.</para> |
| 292 | |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 293 | </sect1> |
| 294 | |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 295 | |
| 296 | </article> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 297 | </book> |