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