📄 manual-core.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>2.燯sing and understanding the Valgrind core</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="manual.html" title="Valgrind User Manual"><link rel="prev" href="manual-intro.html" title="1.營ntroduction"><link rel="next" href="mc-manual.html" title="3.燤emcheck: a heavyweight memory checker"></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="manual-intro.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="manual.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">Valgrind User Manual</th><td width="22px" align="center" valign="middle"><a accesskey="n" href="mc-manual.html"><img src="images/next.png" width="18" height="21" border="0" alt="Next"></a></td></tr></table></div><div class="chapter" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="manual-core"></a>2.燯sing and understanding the Valgrind core</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="manual-core.html#manual-core.whatdoes">2.1. What Valgrind does with your program</a></span></dt><dt><span class="sect1"><a href="manual-core.html#manual-core.started">2.2. Getting started</a></span></dt><dt><span class="sect1"><a href="manual-core.html#manual-core.comment">2.3. The Commentary</a></span></dt><dt><span class="sect1"><a href="manual-core.html#manual-core.report">2.4. Reporting of errors</a></span></dt><dt><span class="sect1"><a href="manual-core.html#manual-core.suppress">2.5. Suppressing errors</a></span></dt><dt><span class="sect1"><a href="manual-core.html#manual-core.flags">2.6. Command-line flags for the Valgrind core</a></span></dt><dd><dl><dt><span class="sect2"><a href="manual-core.html#manual-core.toolopts">2.6.1. Tool-selection option</a></span></dt><dt><span class="sect2"><a href="manual-core.html#manual-core.basicopts">2.6.2. Basic Options</a></span></dt><dt><span class="sect2"><a href="manual-core.html#manual-core.erropts">2.6.3. Error-related options</a></span></dt><dt><span class="sect2"><a href="manual-core.html#manual-core.mallocopts">2.6.4. <code class="computeroutput">malloc()</code>-related Options</a></span></dt><dt><span class="sect2"><a href="manual-core.html#manual-core.rareopts">2.6.5. Uncommon Options</a></span></dt><dt><span class="sect2"><a href="manual-core.html#manual-core.debugopts">2.6.6. Debugging Valgrind Options</a></span></dt><dt><span class="sect2"><a href="manual-core.html#manual-core.defopts">2.6.7. Setting default Options</a></span></dt></dl></dd><dt><span class="sect1"><a href="manual-core.html#manual-core.clientreq">2.7. The Client Request mechanism</a></span></dt><dt><span class="sect1"><a href="manual-core.html#manual-core.pthreads">2.8. Support for Threads</a></span></dt><dt><span class="sect1"><a href="manual-core.html#manual-core.signals">2.9. Handling of Signals</a></span></dt><dt><span class="sect1"><a href="manual-core.html#manual-core.wrapping">2.10. Function wrapping</a></span></dt><dd><dl><dt><span class="sect2"><a href="manual-core.html#manual-core.wrapping.example">2.10.1. A Simple Example</a></span></dt><dt><span class="sect2"><a href="manual-core.html#manual-core.wrapping.specs">2.10.2. Wrapping Specifications</a></span></dt><dt><span class="sect2"><a href="manual-core.html#manual-core.wrapping.semantics">2.10.3. Wrapping Semantics</a></span></dt><dt><span class="sect2"><a href="manual-core.html#manual-core.wrapping.debugging">2.10.4. Debugging</a></span></dt><dt><span class="sect2"><a href="manual-core.html#manual-core.wrapping.limitations-cf">2.10.5. Limitations - control flow</a></span></dt><dt><span class="sect2"><a href="manual-core.html#manual-core.wrapping.limitations-sigs">2.10.6. Limitations - original function signatures</a></span></dt><dt><span class="sect2"><a href="manual-core.html#manual-core.wrapping.examples">2.10.7. Examples</a></span></dt></dl></dd><dt><span class="sect1"><a href="manual-core.html#manual-core.install">2.11. Building and Installing</a></span></dt><dt><span class="sect1"><a href="manual-core.html#manual-core.problems">2.12. If You Have Problems</a></span></dt><dt><span class="sect1"><a href="manual-core.html#manual-core.limits">2.13. Limitations</a></span></dt><dt><span class="sect1"><a href="manual-core.html#manual-core.example">2.14. An Example Run</a></span></dt><dt><span class="sect1"><a href="manual-core.html#manual-core.warnings">2.15. Warning Messages You Might See</a></span></dt><dt><span class="sect1"><a href="manual-core.html#manual-core.mpiwrap">2.16. Debugging MPI Parallel Programs with Valgrind</a></span></dt><dd><dl><dt><span class="sect2"><a href="manual-core.html#manual-core.mpiwrap.build">2.16.1. Building and installing the wrappers</a></span></dt><dt><span class="sect2"><a href="manual-core.html#manual-core.mpiwrap.gettingstarted">2.16.2. Getting started</a></span></dt><dt><span class="sect2"><a href="manual-core.html#manual-core.mpiwrap.controlling">2.16.3. Controlling the wrapper library</a></span></dt><dt><span class="sect2"><a href="manual-core.html#manual-core.mpiwrap.limitations">2.16.4. Abilities and limitations</a></span></dt><dt><span class="sect2"><a href="manual-core.html#manual-core.mpiwrap.writingwrappers">2.16.5. Writing new wrappers</a></span></dt><dt><span class="sect2"><a href="manual-core.html#manual-core.mpiwrap.whattoexpect">2.16.6. What to expect when using the wrappers</a></span></dt></dl></dd></dl></div><p>This section describes the Valgrind core services, flags andbehaviours. That means it is relevant regardless of what particulartool you are using. A point of terminology: most references to"valgrind" in the rest of this section (Section 2) refer to the Valgrindcore services.</p><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="manual-core.whatdoes"></a>2.1.燱hat Valgrind does with your program</h2></div></div></div><p>Valgrind is designed to be as non-intrusive as possible. It worksdirectly with existing executables. You don't need to recompile, relink,or otherwise modify, the program to be checked.</p><p>Simply put <code class="computeroutput">valgrind --tool=tool_name</code> at the start of the command line normally used to run the program. Forexample, if want to run the command <code class="computeroutput">ls -l</code> using the heavyweightmemory-checking tool Memcheck, issue the command:</p><pre class="programlisting">valgrind --tool=memcheck ls -l</pre><p>(Memcheck is the default, so if you want to use it you canactually omit the <code class="option">--tool</code> flag.</p><p>Regardless of which tool is in use, Valgrind takes control of yourprogram before it starts. Debugging information is read from theexecutable and associated libraries, so that error messages and otheroutputs can be phrased in terms of source code locations (if that isappropriate).</p><p>Your program is then run on a synthetic CPU provided by theValgrind core. As new code is executed for the first time, the corehands the code to the selected tool. The tool adds its owninstrumentation code to this and hands the result back to the core,which coordinates the continued execution of this instrumentedcode.</p><p>The amount of instrumentation code added varies widely betweentools. At one end of the scale, Memcheck adds code to check everymemory access and every value computed, increasing the size of the codeat least 12 times, and making it run 25-50 times slower than natively.At the other end of the spectrum, the ultra-trivial "none" tool(a.k.a. Nulgrind) adds no instrumentation at all and causes in total"only" about a 4 times slowdown.</p><p>Valgrind simulates every single instruction your program executes.Because of this, the active tool checks, or profiles, not only the codein your application but also in all supporting dynamically-linked(<code class="computeroutput">.so</code>-format) libraries, including theGNU C library, the X client libraries, Qt, if you work with KDE, and soon.</p><p>If you're using one of the error-detection tools, Valgrind willoften detect errors in libraries, for example the GNU C or X11libraries, which you have to use. You might not be interested in theseerrors, since you probably have no control over that code. Therefore,Valgrind allows you to selectively suppress errors, by recording them ina suppressions file which is read when Valgrind starts up. The buildmechanism attempts to select suppressions which give reasonablebehaviour for the libc and XFree86 versions detected on your machine.To make it easier to write suppressions, you can use the<code class="option">--gen-suppressions=yes</code> option which tells Valgrind toprint out a suppression for each error that appears, which you can thencopy into a suppressions file.</p><p>Different error-checking tools report different kinds of errors.The suppression mechanism therefore allows you to say which tool ortool(s) each suppression applies to.</p></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="manual-core.started"></a>2.2.燝etting started</h2></div></div></div><p>First off, consider whether it might be beneficial to recompileyour application and supporting libraries with debugging info enabled(the <code class="option">-g</code> flag). Without debugging info, the bestValgrind tools will be able to do is guess which function a particularpiece of code belongs to, which makes both error messages and profilingoutput nearly useless. With <code class="option">-g</code>, you'll hopefully getmessages which point directly to the relevant source code lines.</p><p>Another flag you might like to consider, if you are working withC++, is <code class="option">-fno-inline</code>. That makes it easier to see thefunction-call chain, which can help reduce confusion when navigatingaround large C++ apps. For whatever it's worth, debuggingOpenOffice.org with Memcheck is a bit easier when using this flag. Youdon't have to do this, but doing so helps Valgrind produce more accurateand less confusing error reports. Chances are you're set up like thisalready, if you intended to debug your program with GNU gdb, or someother debugger.</p><p>This paragraph applies only if you plan to use Memcheck: On rareoccasions, optimisation levels at <code class="computeroutput">-O2</code>and above have been observed to generate code which fools Memcheck intowrongly reporting uninitialised value errors. We have looked in detailinto fixing this, and unfortunately the result is that doing so wouldgive a further significant slowdown in what is already a slow tool. Sothe best solution is to turn off optimisation altogether. Since thisoften makes things unmanagably slow, a plausible compromise is to use<code class="computeroutput">-O</code>. This gets you the majority of thebenefits of higher optimisation levels whilst keeping relatively smallthe chances of false complaints from Memcheck. All other tools (as faras we know) are unaffected by optimisation level.</p><p>Valgrind understands both the older "stabs" debugging format, usedby gcc versions prior to 3.1, and the newer DWARF2 format used by gcc3.1 and later. We continue to refine and debug our debug-info readers,although the majority of effort will naturally enough go into the newerDWARF2 reader.</p><p>When you're ready to roll, just run your application as youwould normally, but place <code class="computeroutput">valgrind --tool=tool_name</code> in front ofyour usual command-line invocation. Note that you should run the real(machine-code) executable here. If your application is started by, forexample, a shell or perl script, you'll need to modify it to invokeValgrind on the real executables. Running such scripts directly underValgrind will result in you getting error reports pertaining to<code class="computeroutput">/bin/sh</code>,<code class="computeroutput">/usr/bin/perl</code>, or whatever interpreteryou're using. This may not be what you want and can be confusing. Youcan force the issue by giving the flag<code class="option">--trace-children=yes</code>, but confusion is stilllikely.</p></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="manual-core.comment"></a>2.3.燭he Commentary</h2></div></div></div><p>Valgrind tools write a commentary, a stream of text, detailingerror reports and other significant events. All lines in the commentaryhave following form:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -