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