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