?? quick-start.html
字號:
<html xmlns:cf="http://docbook.sourceforge.net/xmlns/chunkfast/1.0"><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>The Valgrind Quick Start Guide</title><link rel="stylesheet" href="vg_basic.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.69.0"><link rel="start" href="index.html" title="Valgrind Documentation"><link rel="up" href="QuickStart.html" title="The Valgrind Quick Start Guide"><link rel="prev" href="QuickStart.html" title="The Valgrind Quick Start Guide"><link rel="next" href="manual.html" title="Valgrind User Manual"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div><table class="nav" width="100%" cellspacing="3" cellpadding="3" border="0" summary="Navigation header"><tr><td width="22px" align="center" valign="middle"><a accesskey="p" href="QuickStart.html"><img src="images/prev.png" width="18" height="21" border="0" alt="Prev"></a></td><td width="25px" align="center" valign="middle"><a accesskey="u" href="QuickStart.html"><img src="images/up.png" width="21" height="18" border="0" alt="Up"></a></td><td width="31px" align="center" valign="middle"><a accesskey="h" href="index.html"><img src="images/home.png" width="27" height="20" border="0" alt="Up"></a></td><th align="center" valign="middle">The Valgrind Quick Start Guide</th><td width="22px" align="center" valign="middle"><a accesskey="n" href="manual.html"><img src="images/next.png" width="18" height="21" border="0" alt="Next"></a></td></tr></table></div><div class="article" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="quick-start"></a>The Valgrind Quick Start Guide</h2></div></div><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="quick-start.intro"></a>1.營ntroduction</h2></div></div></div><p>The Valgrind distribution has multiple tools. The most popular isthe memory checking tool (called Memcheck) which can detect many commonmemory errors such as:</p><div class="itemizedlist"><ul type="disc"><li><p>touching memory you shouldn't (eg. overrunning heap block boundaries);</p></li><li><p>using values before they have been initialized;</p></li><li><p>incorrect freeing of memory, such as double-freeing heap blocks;</p></li><li><p>memory leaks.</p></li></ul></div><p>What follows is the minimum information you need to startdetecting memory errors in your program with Memcheck. Note that thisguide applies to Valgrind version 2.4.0 and later; some of theinformation is not quite right for earlier versions.</p></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="quick-start.prepare"></a>2.燩reparing your program</h2></div></div></div><p>Compile your program with <code class="option">-g</code> to include debugginginformation so that Memcheck's error messages include exact linenumbers. Using <code class="computeroutput">-O0</code> is also a goodidea, if you can tolerate the slowdown. With<code class="computeroutput">-O1</code> line numbers in error messages canbe inaccurate, although generally speaking Memchecking code compiled at<code class="computeroutput">-O1</code> works fairly well. Use of<code class="computeroutput">-O2</code> and above is not recommended asMemcheck occasionally reports uninitialised-value errors which don'treally exist.</p></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="quick-start.mcrun"></a>3.燫unning your program under Memcheck</h2></div></div></div><p>If you normally run your program like this:</p><pre class="programlisting"> myprog arg1 arg2</pre><p>Use this command line:</p><pre class="programlisting"> valgrind --leak-check=yes myprog arg1 arg2</pre><p>Memcheck is the default tool. The <code class="option">--leak-check</code> optionturns on the detailed memory leak detector.</p><p>Your program will run much slower (eg. 20 to 30 times) thannormal, and use a lot more memory. Memcheck will issue messages aboutmemory errors and leaks that it detects.</p></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="quick-start.interpret"></a>4.營nterpreting Memcheck's output</h2></div></div></div><p>Here's an example C program with a memory error and a memory leak.</p><pre class="programlisting"> #include <stdlib.h> void f(void) { int* x = malloc(10 * sizeof(int)); x[10] = 0; // problem 1: heap block overrun } // problem 2: memory leak -- x not freed int main(void) { f(); return 0; }</pre><p>Most error messages look like the following, which describes problem 1,the heap block overrun:</p><pre class="programlisting"> ==19182== Invalid write of size 4 ==19182== at 0x804838F: f (example.c:6) ==19182== by 0x80483AB: main (example.c:11) ==19182== Address 0x1BA45050 is 0 bytes after a block of size 40 alloc'd ==19182== at 0x1B8FF5CD: malloc (vg_replace_malloc.c:130) ==19182== by 0x8048385: f (example.c:5) ==19182== by 0x80483AB: main (example.c:11)</pre><p>Things to notice:</p><div class="itemizedlist"><ul type="disc"><li><p>There is a lot of information in each error message; read it carefully.</p></li><li><p>The 19182 is the process ID; it's usually unimportant.</p></li><li><p>The first line ("Invalid write...") tells you what kind of error it is. Here, the program wrote to some memory it should not have due to a heap block overrun.</p></li><li><p>Below the first line is a stack trace telling you where the problem occurred. Stack traces can get quite large, and be confusing, especially if you are using the C++ STL. Reading them from the bottom up can help. If the stack trace is not big enough, use the <code class="option">--num-callers</code> option to make it bigger.</p></li><li><p>The code addresses (eg. 0x804838F) are usually unimportant, but occasionally crucial for tracking down weirder bugs.</p></li><li><p>Some error messages have a second component which describes the memory address involved. This one shows that the written memory is just past the end of a block allocated with malloc() on line 5 of example.c.</p></li></ul></div><p>It's worth fixing errors in the order they are reported, as later errorscan be caused by earlier errors.</p><p>Memory leak messages look like this:</p><pre class="programlisting"> ==19182== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==19182== at 0x1B8FF5CD: malloc (vg_replace_malloc.c:130) ==19182== by 0x8048385: f (a.c:5) ==19182== by 0x80483AB: main (a.c:11)</pre><p>The stack trace tells you where the leaked memory was allocated.Memcheck cannot tell you why the memory leaked, unfortunately. (Ignorethe "vg_replace_malloc.c", that's an implementation detail.)</p><p>There are several kinds of leaks; the two most importantcategories are:</p><div class="itemizedlist"><ul type="disc"><li><p>"definitely lost": your program is leaking memory -- fix it!</p></li><li><p>"probably lost": your program is leaking memory, unless you're doing funny things with pointers (such as moving them to point to the middle of a heap block).</p></li></ul></div><p>If you don't understand an error message, please consult <a href="mc-manual.html#mc-manual.errormsgs">Explanation of error messages from Memcheck</a> in the <a href="manual.html">Valgrind User Manual</a>which has examples of all the error messages Memcheck produces.</p></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="quick-start.caveats"></a>5.燙aveats</h2></div></div></div><p>Memcheck is not perfect; it occasionally produces false positives,and there are mechanisms for suppressing these (see <a href="manual-core.html#manual-core.suppress">Suppressing errors</a> in the <a href="manual.html">Valgrind User Manual</a>).However, it is typically right 99% of the time, so you should be wary ofignoring its error messages. After all, you wouldn't ignore warningmessages produced by a compiler, right? The suppression mechanism isalso useful if Memcheck is reporting errors in library code that youcannot change; the default suppression set hides a lot of these, but youmay come across more.</p><p>Memcheck also cannot detect every memory error your program has.For example, it can't detect if you overrun the bounds of an array thatis allocated statically or on the stack. But it should detect everyerror that could crash your program (eg. cause a segmentationfault).</p></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="quick-start.info"></a>6.燤ore information</h2></div></div></div><p>Please consult the <a href="FAQ.html">Valgrind FAQ</a> and the <a href="manual.html">Valgrind User Manual</a>, which have much more information. Note thatthe other tools in the Valgrind distribution can be invoked with the<code class="option">--tool</code> option.</p></div></div><div><br><table class="nav" width="100%" cellspacing="3" cellpadding="2" border="0" summary="Navigation footer"><tr><td rowspan="2" width="40%" align="left"><a accesskey="p" href="QuickStart.html"><<燭he Valgrind Quick Start Guide</a>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -