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> |
| 35 | <para>touching memory you shouldn't (eg. overrunning heap block |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 36 | boundaries);</para> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 37 | </listitem> |
| 38 | <listitem> |
| 39 | <para>using values before they have been initialized;</para> |
| 40 | </listitem> |
| 41 | <listitem> |
| 42 | <para>incorrect freeing of memory, such as double-freeing heap |
| 43 | blocks;</para> |
| 44 | </listitem> |
| 45 | <listitem> |
| 46 | <para>memory leaks.</para> |
| 47 | </listitem> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 48 | </itemizedlist> |
| 49 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 50 | <para>What follows is the minimum information you need to start |
| 51 | detecting memory errors in your program with Memcheck. Note that this |
sewardj | 08e31e2 | 2007-05-23 21:58:33 +0000 | [diff] [blame^] | 52 | guide applies to Valgrind version 2.4.0 and later. Some of the |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 53 | information is not quite right for earlier versions.</para> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 54 | |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 55 | </sect1> |
| 56 | |
| 57 | |
| 58 | <sect1 id="quick-start.prepare" xreflabel="Preparing your program"> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 59 | <title>Preparing your program</title> |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 60 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 61 | <para>Compile your program with <option>-g</option> to include debugging |
| 62 | information so that Memcheck's error messages include exact line |
| 63 | numbers. Using <computeroutput>-O0</computeroutput> is also a good |
| 64 | idea, if you can tolerate the slowdown. With |
| 65 | <computeroutput>-O1</computeroutput> line numbers in error messages can |
| 66 | be inaccurate, although generally speaking Memchecking code compiled at |
| 67 | <computeroutput>-O1</computeroutput> works fairly well. Use of |
| 68 | <computeroutput>-O2</computeroutput> and above is not recommended as |
| 69 | Memcheck occasionally reports uninitialised-value errors which don't |
| 70 | really exist.</para> |
| 71 | |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 72 | </sect1> |
| 73 | |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 74 | |
| 75 | <sect1 id="quick-start.mcrun" xreflabel="Running your program under Memcheck"> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 76 | <title>Running your program under Memcheck</title> |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 77 | |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 78 | <para>If you normally run your program like this: |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 79 | <programlisting> myprog arg1 arg2 |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 80 | </programlisting> |
| 81 | |
| 82 | Use this command line: |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 83 | <programlisting> valgrind --leak-check=yes myprog arg1 arg2 |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 84 | </programlisting> |
| 85 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 86 | Memcheck is the default tool. The <option>--leak-check</option> option |
| 87 | turns on the detailed memory leak detector.</para> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 88 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 89 | <para>Your program will run much slower (eg. 20 to 30 times) than |
| 90 | normal, and use a lot more memory. Memcheck will issue messages about |
| 91 | memory errors and leaks that it detects.</para> |
| 92 | |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 93 | </sect1> |
| 94 | |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 95 | |
de | ccde45e | 2005-06-12 10:23:23 +0000 | [diff] [blame] | 96 | <sect1 id="quick-start.interpret" |
| 97 | xreflabel="Interpreting Memcheck's output"> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 98 | <title>Interpreting Memcheck's output</title> |
| 99 | <para>Here's an example C program with a memory error and a memory leak. |
| 100 | |
| 101 | <programlisting> |
| 102 | #include <stdlib.h> |
| 103 | |
| 104 | void f(void) |
| 105 | { |
| 106 | int* x = malloc(10 * sizeof(int)); |
| 107 | x[10] = 0; // problem 1: heap block overrun |
| 108 | } // problem 2: memory leak -- x not freed |
| 109 | |
| 110 | int main(void) |
| 111 | { |
| 112 | f(); |
| 113 | return 0; |
| 114 | } |
| 115 | </programlisting> |
| 116 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 117 | Most error messages look like the following, which describes problem 1, |
| 118 | the heap block overrun: |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 119 | |
| 120 | <programlisting> |
| 121 | ==19182== Invalid write of size 4 |
| 122 | ==19182== at 0x804838F: f (example.c:6) |
| 123 | ==19182== by 0x80483AB: main (example.c:11) |
| 124 | ==19182== Address 0x1BA45050 is 0 bytes after a block of size 40 alloc'd |
| 125 | ==19182== at 0x1B8FF5CD: malloc (vg_replace_malloc.c:130) |
| 126 | ==19182== by 0x8048385: f (example.c:5) |
| 127 | ==19182== by 0x80483AB: main (example.c:11) |
| 128 | </programlisting> |
| 129 | |
| 130 | Things to notice: |
| 131 | |
| 132 | <itemizedlist> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 133 | <listitem> |
| 134 | <para>There is a lot of information in each error message; read it |
| 135 | carefully.</para> |
| 136 | </listitem> |
| 137 | <listitem> |
| 138 | <para>The 19182 is the process ID; it's usually unimportant.</para> |
| 139 | </listitem> |
| 140 | <listitem> |
| 141 | <para>The first line ("Invalid write...") tells you what kind of |
| 142 | error it is. Here, the program wrote to some memory it should not |
| 143 | have due to a heap block overrun.</para> |
| 144 | </listitem> |
| 145 | <listitem> |
| 146 | <para>Below the first line is a stack trace telling you where the |
| 147 | problem occurred. Stack traces can get quite large, and be |
| 148 | confusing, especially if you are using the C++ STL. Reading them |
| 149 | from the bottom up can help. If the stack trace is not big enough, |
| 150 | use the <option>--num-callers</option> option to make it |
| 151 | bigger.</para> |
| 152 | </listitem> |
| 153 | <listitem> |
| 154 | <para>The code addresses (eg. 0x804838F) are usually unimportant, but |
| 155 | occasionally crucial for tracking down weirder bugs.</para> |
| 156 | </listitem> |
| 157 | <listitem> |
| 158 | <para>Some error messages have a second component which describes |
| 159 | the memory address involved. This one shows that the written memory |
| 160 | is just past the end of a block allocated with malloc() on line 5 of |
| 161 | example.c.</para> |
| 162 | </listitem> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 163 | </itemizedlist> |
| 164 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 165 | It's worth fixing errors in the order they are reported, as later errors |
| 166 | can be caused by earlier errors.</para> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 167 | |
| 168 | <para>Memory leak messages look like this: |
| 169 | |
| 170 | <programlisting> |
| 171 | ==19182== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1 |
| 172 | ==19182== at 0x1B8FF5CD: malloc (vg_replace_malloc.c:130) |
njn | bb9700d | 2005-08-15 04:40:57 +0000 | [diff] [blame] | 173 | ==19182== by 0x8048385: f (a.c:5) |
| 174 | ==19182== by 0x80483AB: main (a.c:11) |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 175 | </programlisting> |
| 176 | |
| 177 | The stack trace tells you where the leaked memory was allocated. |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 178 | Memcheck cannot tell you why the memory leaked, unfortunately. (Ignore |
| 179 | the "vg_replace_malloc.c", that's an implementation detail.)</para> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 180 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 181 | <para>There are several kinds of leaks; the two most important |
| 182 | categories are: |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 183 | |
| 184 | <itemizedlist> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 185 | <listitem> |
| 186 | <para>"definitely lost": your program is leaking memory -- fix |
| 187 | it!</para> |
| 188 | </listitem> |
| 189 | <listitem> |
| 190 | <para>"probably lost": your program is leaking memory, unless you're |
| 191 | doing funny things with pointers (such as moving them to point to |
| 192 | the middle of a heap block).</para> |
| 193 | </listitem> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 194 | </itemizedlist> |
| 195 | |
njn | 2c091b8 | 2005-11-18 22:09:47 +0000 | [diff] [blame] | 196 | If you don't understand an error message, please consult |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 197 | <xref linkend="mc-manual.errormsgs"/> in the <xref linkend="manual"/> |
| 198 | which has examples of all the error messages Memcheck produces.</para> |
| 199 | |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 200 | </sect1> |
| 201 | |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 202 | |
de | ccde45e | 2005-06-12 10:23:23 +0000 | [diff] [blame] | 203 | <sect1 id="quick-start.caveats" xreflabel="Caveats"> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 204 | <title>Caveats</title> |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 205 | |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 206 | <para>Memcheck is not perfect; it occasionally produces false positives, |
njn | 2c091b8 | 2005-11-18 22:09:47 +0000 | [diff] [blame] | 207 | and there are mechanisms for suppressing these (see |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 208 | <xref linkend="manual-core.suppress"/> in the <xref linkend="manual"/>). |
| 209 | However, it is typically right 99% of the time, so you should be wary of |
| 210 | ignoring its error messages. After all, you wouldn't ignore warning |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 211 | messages produced by a compiler, right? The suppression mechanism is |
| 212 | also useful if Memcheck is reporting errors in library code that you |
sewardj | 08e31e2 | 2007-05-23 21:58:33 +0000 | [diff] [blame^] | 213 | cannot change. The default suppression set hides a lot of these, but you |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 214 | may come across more.</para> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 215 | |
sewardj | 08e31e2 | 2007-05-23 21:58:33 +0000 | [diff] [blame^] | 216 | <para>Memcheck cannot detect every memory error your program has. |
| 217 | For example, it can't detect out-of-range reads or writes to arrays |
| 218 | that are allocated statically or on the stack. But it should detect many |
| 219 | errors that could crash your program (eg. cause a segmentation |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 220 | fault).</para> |
| 221 | |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 222 | </sect1> |
| 223 | |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 224 | |
de | ccde45e | 2005-06-12 10:23:23 +0000 | [diff] [blame] | 225 | <sect1 id="quick-start.info" xreflabel="More Information"> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 226 | <title>More information</title> |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 227 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 228 | <para>Please consult the <xref linkend="FAQ"/> and the |
| 229 | <xref linkend="manual"/>, which have much more information. Note that |
| 230 | the other tools in the Valgrind distribution can be invoked with the |
| 231 | <option>--tool</option> option.</para> |
| 232 | |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 233 | </sect1> |
| 234 | |
de | 9bec93c | 2005-11-25 05:36:48 +0000 | [diff] [blame] | 235 | |
| 236 | </article> |
njn | 76c5bfa | 2005-03-11 04:33:29 +0000 | [diff] [blame] | 237 | </book> |