blob: a413c37ff23ef42fa077b17268d3da4f0d32dfec [file] [log] [blame]
njn3e986b22004-11-30 10:43:45 +00001<?xml version="1.0"?> <!-- -*- sgml -*- -->
2<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
de252c6142005-11-27 04:10:00 +00003 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"
4[ <!ENTITY % vg-entities SYSTEM "vg-entities.xml"> %vg-entities; ]>
5
njn3e986b22004-11-30 10:43:45 +00006
7<chapter id="manual-core" xreflabel="Valgrind's core">
8<title>Using and understanding the Valgrind core</title>
9
sewardja5fac792007-11-25 00:55:11 +000010<para>This chapter describes the Valgrind core services, flags and
debad57fc2005-12-03 22:33:29 +000011behaviours. That means it is relevant regardless of what particular
sewardja5fac792007-11-25 00:55:11 +000012tool you are using. The information should be sufficient for you to
13make effective day-to-day use of Valgrind. Advanced topics related to
14the Valgrind core are described in <xref linkend="manual-core-adv"/>.
15</para>
16
17<para>
18A point of terminology: most references to "Valgrind" in this chapter
19refer to the Valgrind core services. </para>
20
21
njn3e986b22004-11-30 10:43:45 +000022
23<sect1 id="manual-core.whatdoes"
24 xreflabel="What Valgrind does with your program">
25<title>What Valgrind does with your program</title>
26
debad57fc2005-12-03 22:33:29 +000027<para>Valgrind is designed to be as non-intrusive as possible. It works
28directly with existing executables. You don't need to recompile, relink,
29or otherwise modify, the program to be checked.</para>
njn3e986b22004-11-30 10:43:45 +000030
debad57fc2005-12-03 22:33:29 +000031<para>Simply put
32<computeroutput>valgrind --tool=tool_name</computeroutput>
33at the start of the command line normally used to run the program. For
34example, if want to run the command
35<computeroutput>ls -l</computeroutput> using the heavyweight
36memory-checking tool Memcheck, issue the command:</para>
njn3e986b22004-11-30 10:43:45 +000037
38<programlisting><![CDATA[
39valgrind --tool=memcheck ls -l]]></programlisting>
40
sewardj08e31e22007-05-23 21:58:33 +000041<para>Memcheck is the default, so if you want to use it you can
42omit the <option>--tool</option> flag.</para>
njn779a2d62005-07-25 00:12:19 +000043
debad57fc2005-12-03 22:33:29 +000044<para>Regardless of which tool is in use, Valgrind takes control of your
45program before it starts. Debugging information is read from the
46executable and associated libraries, so that error messages and other
sewardj08e31e22007-05-23 21:58:33 +000047outputs can be phrased in terms of source code locations, when
48appropriate.</para>
njn3e986b22004-11-30 10:43:45 +000049
debad57fc2005-12-03 22:33:29 +000050<para>Your program is then run on a synthetic CPU provided by the
51Valgrind core. As new code is executed for the first time, the core
52hands the code to the selected tool. The tool adds its own
53instrumentation code to this and hands the result back to the core,
54which coordinates the continued execution of this instrumented
55code.</para>
njn3e986b22004-11-30 10:43:45 +000056
debad57fc2005-12-03 22:33:29 +000057<para>The amount of instrumentation code added varies widely between
58tools. At one end of the scale, Memcheck adds code to check every
sewardj08e31e22007-05-23 21:58:33 +000059memory access and every value computed,
60making it run 10-50 times slower than natively.
debad57fc2005-12-03 22:33:29 +000061At the other end of the spectrum, the ultra-trivial "none" tool
sewardj08e31e22007-05-23 21:58:33 +000062(also referred to as Nulgrind) adds no instrumentation at all
63and causes in total
debad57fc2005-12-03 22:33:29 +000064"only" about a 4 times slowdown.</para>
njn3e986b22004-11-30 10:43:45 +000065
debad57fc2005-12-03 22:33:29 +000066<para>Valgrind simulates every single instruction your program executes.
67Because of this, the active tool checks, or profiles, not only the code
68in your application but also in all supporting dynamically-linked
69(<computeroutput>.so</computeroutput>-format) libraries, including the
70GNU C library, the X client libraries, Qt, if you work with KDE, and so
71on.</para>
njn3e986b22004-11-30 10:43:45 +000072
sewardj08e31e22007-05-23 21:58:33 +000073<para>If you're using an error-detection tool, Valgrind may
74detect errors in libraries, for example the GNU C or X11
debad57fc2005-12-03 22:33:29 +000075libraries, which you have to use. You might not be interested in these
76errors, since you probably have no control over that code. Therefore,
77Valgrind allows you to selectively suppress errors, by recording them in
78a suppressions file which is read when Valgrind starts up. The build
79mechanism attempts to select suppressions which give reasonable
sewardj08e31e22007-05-23 21:58:33 +000080behaviour for the C library
81and X11 client library versions detected on your machine.
debad57fc2005-12-03 22:33:29 +000082To make it easier to write suppressions, you can use the
sewardj08e31e22007-05-23 21:58:33 +000083<option>--gen-suppressions=yes</option> option. This tells Valgrind to
84print out a suppression for each reported error, which you can then
debad57fc2005-12-03 22:33:29 +000085copy into a suppressions file.</para>
njn3e986b22004-11-30 10:43:45 +000086
debad57fc2005-12-03 22:33:29 +000087<para>Different error-checking tools report different kinds of errors.
88The suppression mechanism therefore allows you to say which tool or
89tool(s) each suppression applies to.</para>
njn3e986b22004-11-30 10:43:45 +000090
91</sect1>
92
93
94<sect1 id="manual-core.started" xreflabel="Getting started">
95<title>Getting started</title>
96
debad57fc2005-12-03 22:33:29 +000097<para>First off, consider whether it might be beneficial to recompile
98your application and supporting libraries with debugging info enabled
99(the <option>-g</option> flag). Without debugging info, the best
100Valgrind tools will be able to do is guess which function a particular
101piece of code belongs to, which makes both error messages and profiling
sewardj08e31e22007-05-23 21:58:33 +0000102output nearly useless. With <option>-g</option>, you'll get
debad57fc2005-12-03 22:33:29 +0000103messages which point directly to the relevant source code lines.</para>
njn3e986b22004-11-30 10:43:45 +0000104
debad57fc2005-12-03 22:33:29 +0000105<para>Another flag you might like to consider, if you are working with
106C++, is <option>-fno-inline</option>. That makes it easier to see the
107function-call chain, which can help reduce confusion when navigating
sewardj08e31e22007-05-23 21:58:33 +0000108around large C++ apps. For example, debugging
debad57fc2005-12-03 22:33:29 +0000109OpenOffice.org with Memcheck is a bit easier when using this flag. You
110don't have to do this, but doing so helps Valgrind produce more accurate
111and less confusing error reports. Chances are you're set up like this
112already, if you intended to debug your program with GNU gdb, or some
113other debugger.</para>
njn3e986b22004-11-30 10:43:45 +0000114
njn3d92f9c2007-10-17 22:29:08 +0000115<para>If you are planning to use Memcheck: On rare
116occasions, compiler optimisations (at <computeroutput>-O2</computeroutput>
117and above, and sometimes <computeroutput>-O1</computeroutput>) have been
118observed to generate code which fools Memcheck into wrongly reporting
119uninitialised value errors, or missing uninitialised value errors. We have
120looked in detail into fixing this, and unfortunately the result is that
121doing so would give a further significant slowdown in what is already a slow
122tool. So the best solution is to turn off optimisation altogether. Since
sewardj33878892007-11-17 09:43:25 +0000123this often makes things unmanageably slow, a reasonable compromise is to use
debad57fc2005-12-03 22:33:29 +0000124<computeroutput>-O</computeroutput>. This gets you the majority of the
njn3d92f9c2007-10-17 22:29:08 +0000125benefits of higher optimisation levels whilst keeping relatively small the
njn9bd4bd42007-10-18 23:14:48 +0000126chances of false positives or false negatives from Memcheck. Also, you
127should compile your code with <computeroutput>-Wall</computeroutput> because
128it can identify some or all of the problems that Valgrind can miss at the
sewardj778d7832007-11-22 01:21:56 +0000129higher optimisation levels. (Using <computeroutput>-Wall</computeroutput>
njn9bd4bd42007-10-18 23:14:48 +0000130is also a good idea in general.) All other tools (as far as we know) are
131unaffected by optimisation level.</para>
njn3e986b22004-11-30 10:43:45 +0000132
debad57fc2005-12-03 22:33:29 +0000133<para>Valgrind understands both the older "stabs" debugging format, used
sewardj08e31e22007-05-23 21:58:33 +0000134by gcc versions prior to 3.1, and the newer DWARF2 and DWARF3 formats
135used by gcc
1363.1 and later. We continue to develop our debug-info readers,
debad57fc2005-12-03 22:33:29 +0000137although the majority of effort will naturally enough go into the newer
sewardj08e31e22007-05-23 21:58:33 +0000138DWARF2/3 reader.</para>
njn3e986b22004-11-30 10:43:45 +0000139
140<para>When you're ready to roll, just run your application as you
debad57fc2005-12-03 22:33:29 +0000141would normally, but place
142<computeroutput>valgrind --tool=tool_name</computeroutput> in front of
143your usual command-line invocation. Note that you should run the real
144(machine-code) executable here. If your application is started by, for
145example, a shell or perl script, you'll need to modify it to invoke
146Valgrind on the real executables. Running such scripts directly under
147Valgrind will result in you getting error reports pertaining to
148<computeroutput>/bin/sh</computeroutput>,
149<computeroutput>/usr/bin/perl</computeroutput>, or whatever interpreter
150you're using. This may not be what you want and can be confusing. You
151can force the issue by giving the flag
152<option>--trace-children=yes</option>, but confusion is still
153likely.</para>
njn3e986b22004-11-30 10:43:45 +0000154
155</sect1>
156
157
debad57fc2005-12-03 22:33:29 +0000158<sect1 id="manual-core.comment" xreflabel="The Commentary">
159<title>The Commentary</title>
njn3e986b22004-11-30 10:43:45 +0000160
debad57fc2005-12-03 22:33:29 +0000161<para>Valgrind tools write a commentary, a stream of text, detailing
162error reports and other significant events. All lines in the commentary
163have following form:
njn3e986b22004-11-30 10:43:45 +0000164
165<programlisting><![CDATA[
166==12345== some-message-from-Valgrind]]></programlisting>
167</para>
168
debad57fc2005-12-03 22:33:29 +0000169<para>The <computeroutput>12345</computeroutput> is the process ID.
170This scheme makes it easy to distinguish program output from Valgrind
171commentary, and also easy to differentiate commentaries from different
172processes which have become merged together, for whatever reason.</para>
njn3e986b22004-11-30 10:43:45 +0000173
debad57fc2005-12-03 22:33:29 +0000174<para>By default, Valgrind tools write only essential messages to the
175commentary, so as to avoid flooding you with information of secondary
176importance. If you want more information about what is happening,
177re-run, passing the <option>-v</option> flag to Valgrind. A second
178<option>-v</option> gives yet more detail.
sewardj053fe982005-11-15 19:51:04 +0000179</para>
njn3e986b22004-11-30 10:43:45 +0000180
debad57fc2005-12-03 22:33:29 +0000181<para>You can direct the commentary to three different places:</para>
njn3e986b22004-11-30 10:43:45 +0000182
183<orderedlist>
184
debad57fc2005-12-03 22:33:29 +0000185 <listitem id="manual-core.out2fd" xreflabel="Directing output to fd">
186 <para>The default: send it to a file descriptor, which is by default
187 2 (stderr). So, if you give the core no options, it will write
188 commentary to the standard error stream. If you want to send it to
189 some other file descriptor, for example number 9, you can specify
190 <option>--log-fd=9</option>.</para>
191
192 <para>This is the simplest and most common arrangement, but can
sewardj08e31e22007-05-23 21:58:33 +0000193 cause problems when Valgrinding entire trees of processes which
debad57fc2005-12-03 22:33:29 +0000194 expect specific file descriptors, particularly stdin/stdout/stderr,
195 to be available for their own use.</para>
njn3e986b22004-11-30 10:43:45 +0000196 </listitem>
197
198 <listitem id="manual-core.out2file"
debad57fc2005-12-03 22:33:29 +0000199 xreflabel="Directing output to file"> <para>A less intrusive
200 option is to write the commentary to a file, which you specify by
njn374a36d2007-11-23 01:41:32 +0000201 <option>--log-file=filename</option>. There are special format
202 specifiers that can be used to use a process ID or an environment
203 variable name in the log file name. These are useful/necessary if your
204 program invokes multiple processes (especially for MPI programs).
205 See the <link linkend="manual-core.basicopts">basic options section</link>
206 for more details.</para>
njn3e986b22004-11-30 10:43:45 +0000207 </listitem>
208
209 <listitem id="manual-core.out2socket"
debad57fc2005-12-03 22:33:29 +0000210 xreflabel="Directing output to network socket"> <para>The
211 least intrusive option is to send the commentary to a network
212 socket. The socket is specified as an IP address and port number
213 pair, like this: <option>--log-socket=192.168.0.1:12345</option> if
sewardj08e31e22007-05-23 21:58:33 +0000214 you want to send the output to host IP 192.168.0.1 port 12345
215 (note: we
debad57fc2005-12-03 22:33:29 +0000216 have no idea if 12345 is a port of pre-existing significance). You
217 can also omit the port number:
218 <option>--log-socket=192.168.0.1</option>, in which case a default
219 port of 1500 is used. This default is defined by the constant
220 <computeroutput>VG_CLO_DEFAULT_LOGPORT</computeroutput> in the
221 sources.</para>
njn3e986b22004-11-30 10:43:45 +0000222
debad57fc2005-12-03 22:33:29 +0000223 <para>Note, unfortunately, that you have to use an IP address here,
224 rather than a hostname.</para>
njn3e986b22004-11-30 10:43:45 +0000225
sewardj08e31e22007-05-23 21:58:33 +0000226 <para>Writing to a network socket is pointless if you don't
debad57fc2005-12-03 22:33:29 +0000227 have something listening at the other end. We provide a simple
228 listener program,
229 <computeroutput>valgrind-listener</computeroutput>, which accepts
230 connections on the specified port and copies whatever it is sent to
231 stdout. Probably someone will tell us this is a horrible security
232 risk. It seems likely that people will write more sophisticated
233 listeners in the fullness of time.</para>
njn3e986b22004-11-30 10:43:45 +0000234
debad57fc2005-12-03 22:33:29 +0000235 <para>valgrind-listener can accept simultaneous connections from up
sewardj08e31e22007-05-23 21:58:33 +0000236 to 50 Valgrinded processes. In front of each line of output it
debad57fc2005-12-03 22:33:29 +0000237 prints the current number of active connections in round
238 brackets.</para>
njn3e986b22004-11-30 10:43:45 +0000239
debad57fc2005-12-03 22:33:29 +0000240 <para>valgrind-listener accepts two command-line flags:</para>
njn3e986b22004-11-30 10:43:45 +0000241 <itemizedlist>
debad57fc2005-12-03 22:33:29 +0000242 <listitem>
243 <para><option>-e</option> or <option>--exit-at-zero</option>:
244 when the number of connected processes falls back to zero,
245 exit. Without this, it will run forever, that is, until you
246 send it Control-C.</para>
247 </listitem>
248 <listitem>
249 <para><option>portnumber</option>: changes the port it listens
250 on from the default (1500). The specified port must be in the
251 range 1024 to 65535. The same restriction applies to port
252 numbers specified by a <option>--log-socket</option> to
253 Valgrind itself.</para>
254 </listitem>
njn3e986b22004-11-30 10:43:45 +0000255 </itemizedlist>
256
sewardj08e31e22007-05-23 21:58:33 +0000257 <para>If a Valgrinded process fails to connect to a listener, for
debad57fc2005-12-03 22:33:29 +0000258 whatever reason (the listener isn't running, invalid or unreachable
259 host or port, etc), Valgrind switches back to writing the commentary
260 to stderr. The same goes for any process which loses an established
261 connection to a listener. In other words, killing the listener
262 doesn't kill the processes sending data to it.</para>
njn3e986b22004-11-30 10:43:45 +0000263 </listitem>
njn3e986b22004-11-30 10:43:45 +0000264
debad57fc2005-12-03 22:33:29 +0000265</orderedlist>
266
267<para>Here is an important point about the relationship between the
268commentary and profiling output from tools. The commentary contains a
269mix of messages from the Valgrind core and the selected tool. If the
270tool reports errors, it will report them to the commentary. However, if
271the tool does profiling, the profile data will be written to a file of
272some kind, depending on the tool, and independent of what
273<option>--log-*</option> options are in force. The commentary is
274intended to be a low-bandwidth, human-readable channel. Profiling data,
275on the other hand, is usually voluminous and not meaningful without
276further processing, which is why we have chosen this arrangement.</para>
njn3e986b22004-11-30 10:43:45 +0000277
278</sect1>
279
280
281<sect1 id="manual-core.report" xreflabel="Reporting of errors">
282<title>Reporting of errors</title>
283
sewardj08e31e22007-05-23 21:58:33 +0000284<para>When an error-checking tool
285detects something bad happening in the program, an error
286message is written to the commentary. Here's an example from Memcheck:</para>
njn3e986b22004-11-30 10:43:45 +0000287
288<programlisting><![CDATA[
289==25832== Invalid read of size 4
290==25832== at 0x8048724: BandMatrix::ReSize(int, int, int) (bogon.cpp:45)
291==25832== by 0x80487AF: main (bogon.cpp:66)
njn21f91952005-03-12 22:14:42 +0000292==25832== Address 0xBFFFF74C is not stack'd, malloc'd or free'd]]></programlisting>
njn3e986b22004-11-30 10:43:45 +0000293
debad57fc2005-12-03 22:33:29 +0000294<para>This message says that the program did an illegal 4-byte read of
295address 0xBFFFF74C, which, as far as Memcheck can tell, is not a valid
sewardj08e31e22007-05-23 21:58:33 +0000296stack address, nor corresponds to any current malloc'd or free'd
debad57fc2005-12-03 22:33:29 +0000297blocks. The read is happening at line 45 of
298<filename>bogon.cpp</filename>, called from line 66 of the same file,
299etc. For errors associated with an identified malloc'd/free'd block,
300for example reading free'd memory, Valgrind reports not only the
301location where the error happened, but also where the associated block
302was malloc'd/free'd.</para>
njn3e986b22004-11-30 10:43:45 +0000303
debad57fc2005-12-03 22:33:29 +0000304<para>Valgrind remembers all error reports. When an error is detected,
305it is compared against old reports, to see if it is a duplicate. If so,
306the error is noted, but no further commentary is emitted. This avoids
307you being swamped with bazillions of duplicate error reports.</para>
njn3e986b22004-11-30 10:43:45 +0000308
debad57fc2005-12-03 22:33:29 +0000309<para>If you want to know how many times each error occurred, run with
310the <option>-v</option> option. When execution finishes, all the
311reports are printed out, along with, and sorted by, their occurrence
312counts. This makes it easy to see which errors have occurred most
313frequently.</para>
njn3e986b22004-11-30 10:43:45 +0000314
debad57fc2005-12-03 22:33:29 +0000315<para>Errors are reported before the associated operation actually
sewardj08e31e22007-05-23 21:58:33 +0000316happens. If you're using a tool (eg. Memcheck) which does
debad57fc2005-12-03 22:33:29 +0000317address checking, and your program attempts to read from address zero,
318the tool will emit a message to this effect, and the program will then
319duly die with a segmentation fault.</para>
njn3e986b22004-11-30 10:43:45 +0000320
debad57fc2005-12-03 22:33:29 +0000321<para>In general, you should try and fix errors in the order that they
322are reported. Not doing so can be confusing. For example, a program
323which copies uninitialised values to several memory locations, and later
324uses them, will generate several error messages, when run on Memcheck.
325The first such error message may well give the most direct clue to the
326root cause of the problem.</para>
njn3e986b22004-11-30 10:43:45 +0000327
328<para>The process of detecting duplicate errors is quite an
329expensive one and can become a significant performance overhead
330if your program generates huge quantities of errors. To avoid
sewardj053fe982005-11-15 19:51:04 +0000331serious problems, Valgrind will simply stop collecting
sewardj08e31e22007-05-23 21:58:33 +0000332errors after 1,000 different errors have been seen, or 10,000,000 errors
njn3e986b22004-11-30 10:43:45 +0000333in total have been seen. In this situation you might as well
334stop your program and fix it, because Valgrind won't tell you
sewardj08e31e22007-05-23 21:58:33 +0000335anything else useful after this. Note that the 1,000/10,000,000 limits
njn3e986b22004-11-30 10:43:45 +0000336apply after suppressed errors are removed. These limits are
njnc7561b92005-06-19 01:24:32 +0000337defined in <filename>m_errormgr.c</filename> and can be increased
njn3e986b22004-11-30 10:43:45 +0000338if necessary.</para>
339
340<para>To avoid this cutoff you can use the
debad57fc2005-12-03 22:33:29 +0000341<option>--error-limit=no</option> flag. Then Valgrind will always show
342errors, regardless of how many there are. Use this flag carefully,
343since it may have a bad effect on performance.</para>
njn3e986b22004-11-30 10:43:45 +0000344
345</sect1>
346
347
348<sect1 id="manual-core.suppress" xreflabel="Suppressing errors">
349<title>Suppressing errors</title>
350
debad57fc2005-12-03 22:33:29 +0000351<para>The error-checking tools detect numerous problems in the base
sewardj08e31e22007-05-23 21:58:33 +0000352libraries, such as the GNU C library, and the X11 client libraries,
debad57fc2005-12-03 22:33:29 +0000353which come pre-installed on your GNU/Linux system. You can't easily fix
354these, but you don't want to see these errors (and yes, there are many!)
355So Valgrind reads a list of errors to suppress at startup. A default
sewardj08e31e22007-05-23 21:58:33 +0000356suppression file is created by the
debad57fc2005-12-03 22:33:29 +0000357<computeroutput>./configure</computeroutput> script when the system is
358built.</para>
njn3e986b22004-11-30 10:43:45 +0000359
debad57fc2005-12-03 22:33:29 +0000360<para>You can modify and add to the suppressions file at your leisure,
361or, better, write your own. Multiple suppression files are allowed.
362This is useful if part of your project contains errors you can't or
363don't want to fix, yet you don't want to continuously be reminded of
364them.</para>
njn3e986b22004-11-30 10:43:45 +0000365
debad57fc2005-12-03 22:33:29 +0000366<formalpara><title>Note:</title> <para>By far the easiest way to add
367suppressions is to use the <option>--gen-suppressions=yes</option> flag
njn3e986b22004-11-30 10:43:45 +0000368described in <xref linkend="manual-core.flags"/>.</para>
369</formalpara>
370
debad57fc2005-12-03 22:33:29 +0000371<para>Each error to be suppressed is described very specifically, to
372minimise the possibility that a suppression-directive inadvertantly
373suppresses a bunch of similar errors which you did want to see. The
374suppression mechanism is designed to allow precise yet flexible
375specification of errors to suppress.</para>
njn3e986b22004-11-30 10:43:45 +0000376
debad57fc2005-12-03 22:33:29 +0000377<para>If you use the <option>-v</option> flag, at the end of execution,
378Valgrind prints out one line for each used suppression, giving its name
379and the number of times it got used. Here's the suppressions used by a
sewardj08e31e22007-05-23 21:58:33 +0000380run of <computeroutput>valgrind --tool=memcheck ls -l</computeroutput>:</para>
njn3e986b22004-11-30 10:43:45 +0000381
382<programlisting><![CDATA[
383--27579-- supp: 1 socketcall.connect(serv_addr)/__libc_connect/__nscd_getgrgid_r
384--27579-- supp: 1 socketcall.connect(serv_addr)/__libc_connect/__nscd_getpwuid_r
385--27579-- supp: 6 strrchr/_dl_map_object_from_fd/_dl_map_object]]></programlisting>
386
debad57fc2005-12-03 22:33:29 +0000387<para>Multiple suppressions files are allowed. By default, Valgrind
388uses <filename>$PREFIX/lib/valgrind/default.supp</filename>. You can
389ask to add suppressions from another file, by specifying
390<option>--suppressions=/path/to/file.supp</option>.
njn3e986b22004-11-30 10:43:45 +0000391</para>
392
debad57fc2005-12-03 22:33:29 +0000393<para>If you want to understand more about suppressions, look at an
394existing suppressions file whilst reading the following documentation.
sewardj08e31e22007-05-23 21:58:33 +0000395The file <filename>glibc-2.3.supp</filename>, in the source
njn3e986b22004-11-30 10:43:45 +0000396distribution, provides some good examples.</para>
397
398<para>Each suppression has the following components:</para>
399
debad57fc2005-12-03 22:33:29 +0000400<itemizedlist>
njn3e986b22004-11-30 10:43:45 +0000401
402 <listitem>
debad57fc2005-12-03 22:33:29 +0000403 <para>First line: its name. This merely gives a handy name to the
404 suppression, by which it is referred to in the summary of used
405 suppressions printed out when a program finishes. It's not
406 important what the name is; any identifying string will do.</para>
njn3e986b22004-11-30 10:43:45 +0000407 </listitem>
408
409 <listitem>
debad57fc2005-12-03 22:33:29 +0000410 <para>Second line: name of the tool(s) that the suppression is for
411 (if more than one, comma-separated), and the name of the suppression
sewardj33878892007-11-17 09:43:25 +0000412 itself, separated by a colon (n.b.: no spaces are allowed), eg:</para>
njn3e986b22004-11-30 10:43:45 +0000413<programlisting><![CDATA[
414tool_name1,tool_name2:suppression_name]]></programlisting>
415
sewardjf5fa3bd2006-03-14 00:56:29 +0000416 <para>Recall that Valgrind is a modular system, in which
debad57fc2005-12-03 22:33:29 +0000417 different instrumentation tools can observe your program whilst it
418 is running. Since different tools detect different kinds of errors,
419 it is necessary to say which tool(s) the suppression is meaningful
420 to.</para>
njn3e986b22004-11-30 10:43:45 +0000421
debad57fc2005-12-03 22:33:29 +0000422 <para>Tools will complain, at startup, if a tool does not understand
423 any suppression directed to it. Tools ignore suppressions which are
424 not directed to them. As a result, it is quite practical to put
425 suppressions for all tools into the same suppression file.</para>
njn3e986b22004-11-30 10:43:45 +0000426 </listitem>
427
428 <listitem>
debad57fc2005-12-03 22:33:29 +0000429 <para>Next line: a small number of suppression types have extra
430 information after the second line (eg. the <varname>Param</varname>
431 suppression for Memcheck)</para>
njn3e986b22004-11-30 10:43:45 +0000432 </listitem>
433
434 <listitem>
debad57fc2005-12-03 22:33:29 +0000435 <para>Remaining lines: This is the calling context for the error --
sewardj08e31e22007-05-23 21:58:33 +0000436 the chain of function calls that led to it. There can be up to 24
437 of these lines.</para>
njn3e986b22004-11-30 10:43:45 +0000438
sewardj66293252008-11-04 01:38:02 +0000439 <para>Locations may be names of either shared objects or
440 functions. They begin
debad57fc2005-12-03 22:33:29 +0000441 <computeroutput>obj:</computeroutput> and
442 <computeroutput>fun:</computeroutput> respectively. Function and
443 object names to match against may use the wildcard characters
444 <computeroutput>*</computeroutput> and
445 <computeroutput>?</computeroutput>.</para>
njn3e986b22004-11-30 10:43:45 +0000446
debad57fc2005-12-03 22:33:29 +0000447 <para><command>Important note: </command> C++ function names must be
448 <command>mangled</command>. If you are writing suppressions by
449 hand, use the <option>--demangle=no</option> option to get the
sewardj66293252008-11-04 01:38:02 +0000450 mangled names in your error messages. An example of a mangled
451 C++ name is <computeroutput>_ZN9QListView4showEv</computeroutput>.
452 This is the form that the GNU C++ compiler uses internally, and
453 the form that must be used in suppression files. The equivalent
454 demangled name, <computeroutput>QListView::show()</computeroutput>,
455 is what you see at the C++ source code level.
456 </para>
457
458 <para>A location line may also be
459 simply "<computeroutput>...</computeroutput>" (three dots). This is
460 a frame-level wildcard, which matches zero or more frames. Frame
461 level wildcards are useful because they make it easy to ignore
462 varying numbers of uninteresting frames in between frames of
463 interest. That is often important when writing suppressions which
464 are intended to be robust against variations in the amount of
465 function inlining done by compilers.</para>
njn3e986b22004-11-30 10:43:45 +0000466 </listitem>
467
468 <listitem>
debad57fc2005-12-03 22:33:29 +0000469 <para>Finally, the entire suppression must be between curly
470 braces. Each brace must be the first character on its own
471 line.</para>
njn3e986b22004-11-30 10:43:45 +0000472 </listitem>
473
474 </itemizedlist>
475
debad57fc2005-12-03 22:33:29 +0000476<para>A suppression only suppresses an error when the error matches all
477the details in the suppression. Here's an example:</para>
njn3e986b22004-11-30 10:43:45 +0000478
479<programlisting><![CDATA[
480{
481 __gconv_transform_ascii_internal/__mbrtowc/mbtowc
482 Memcheck:Value4
483 fun:__gconv_transform_ascii_internal
484 fun:__mbr*toc
485 fun:mbtowc
486}]]></programlisting>
487
488
489<para>What it means is: for Memcheck only, suppress a
debad57fc2005-12-03 22:33:29 +0000490use-of-uninitialised-value error, when the data size is 4, when it
491occurs in the function
492<computeroutput>__gconv_transform_ascii_internal</computeroutput>, when
493that is called from any function of name matching
494<computeroutput>__mbr*toc</computeroutput>, when that is called from
495<computeroutput>mbtowc</computeroutput>. It doesn't apply under any
496other circumstances. The string by which this suppression is identified
497to the user is
njn3e986b22004-11-30 10:43:45 +0000498<computeroutput>__gconv_transform_ascii_internal/__mbrtowc/mbtowc</computeroutput>.</para>
499
500<para>(See <xref linkend="mc-manual.suppfiles"/> for more details
501on the specifics of Memcheck's suppression kinds.)</para>
502
503<para>Another example, again for the Memcheck tool:</para>
504
505<programlisting><![CDATA[
506{
507 libX11.so.6.2/libX11.so.6.2/libXaw.so.7.0
508 Memcheck:Value4
509 obj:/usr/X11R6/lib/libX11.so.6.2
510 obj:/usr/X11R6/lib/libX11.so.6.2
511 obj:/usr/X11R6/lib/libXaw.so.7.0
512}]]></programlisting>
513
sewardj66293252008-11-04 01:38:02 +0000514<para>This suppresses any size 4 uninitialised-value error which occurs
debad57fc2005-12-03 22:33:29 +0000515anywhere in <filename>libX11.so.6.2</filename>, when called from
516anywhere in the same library, when called from anywhere in
517<filename>libXaw.so.7.0</filename>. The inexact specification of
518locations is regrettable, but is about all you can hope for, given that
sewardj08e31e22007-05-23 21:58:33 +0000519the X11 libraries shipped on the Linux distro on which this example
520was made have had their symbol tables removed.</para>
njn3e986b22004-11-30 10:43:45 +0000521
sewardj08e31e22007-05-23 21:58:33 +0000522<para>Although the above two examples do not make this clear, you can
523freely mix <computeroutput>obj:</computeroutput> and
524<computeroutput>fun:</computeroutput> lines in a suppression.</para>
njn3e986b22004-11-30 10:43:45 +0000525
sewardj66293252008-11-04 01:38:02 +0000526<para>Finally, here's an example using three frame-level wildcards:</para>
527
528<programlisting><![CDATA[
529{
530 a-contrived-example
531 Memcheck:Leak
532 fun:malloc
533 ...
534 fun:ddd
535 ...
536 fun:ccc
537 ...
538 fun:main
539}
540]]></programlisting>
541This suppresses Memcheck memory-leak errors, in the case where
542the allocation was done by <computeroutput>main</computeroutput>
543calling (though any number of intermediaries, including zero)
544<computeroutput>ccc</computeroutput>,
545calling onwards via
546<computeroutput>ddd</computeroutput> and eventually
547to <computeroutput>malloc.</computeroutput>.
njn3e986b22004-11-30 10:43:45 +0000548</sect1>
549
550
551<sect1 id="manual-core.flags"
552 xreflabel="Command-line flags for the Valgrind core">
553<title>Command-line flags for the Valgrind core</title>
554
debad57fc2005-12-03 22:33:29 +0000555<para>As mentioned above, Valgrind's core accepts a common set of flags.
556The tools also accept tool-specific flags, which are documented
sewardj33878892007-11-17 09:43:25 +0000557separately for each tool.</para>
njn3e986b22004-11-30 10:43:45 +0000558
559<para>You invoke Valgrind like this:</para>
560
561<programlisting><![CDATA[
debad57fc2005-12-03 22:33:29 +0000562valgrind [valgrind-options] your-prog [your-prog-options]]]></programlisting>
njn3e986b22004-11-30 10:43:45 +0000563
debad57fc2005-12-03 22:33:29 +0000564<para>Valgrind's default settings succeed in giving reasonable behaviour
565in most cases. We group the available options by rough
njn3e986b22004-11-30 10:43:45 +0000566categories.</para>
567
568<sect2 id="manual-core.toolopts" xreflabel="Tool-selection option">
569<title>Tool-selection option</title>
570
571<para>The single most important option.</para>
debad57fc2005-12-03 22:33:29 +0000572
573<itemizedlist>
574 <listitem id="tool_name">
575 <para><option>--tool=&lt;name&gt;</option> [default=memcheck]</para>
njn3e986b22004-11-30 10:43:45 +0000576 <para>Run the Valgrind tool called <emphasis>name</emphasis>,
njn1d0825f2006-03-27 11:37:07 +0000577 e.g. Memcheck, Cachegrind, etc.</para>
debad57fc2005-12-03 22:33:29 +0000578 </listitem>
579</itemizedlist>
580
njn3e986b22004-11-30 10:43:45 +0000581</sect2>
582
debad57fc2005-12-03 22:33:29 +0000583
584
njn3e986b22004-11-30 10:43:45 +0000585<sect2 id="manual-core.basicopts" xreflabel="Basic Options">
586<title>Basic Options</title>
587
de03e0e7c2005-12-03 23:02:33 +0000588<!-- start of xi:include in the manpage -->
589<para id="basic.opts.para">These options work with all tools.</para>
njn3e986b22004-11-30 10:43:45 +0000590
de03e0e7c2005-12-03 23:02:33 +0000591<variablelist id="basic.opts.list">
njn3e986b22004-11-30 10:43:45 +0000592
debad57fc2005-12-03 22:33:29 +0000593 <varlistentry id="opt.help" xreflabel="--help">
594 <term><option>-h --help</option></term>
595 <listitem>
596 <para>Show help for all options, both for the core and for the
597 selected tool.</para>
598 </listitem>
599 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000600
debad57fc2005-12-03 22:33:29 +0000601 <varlistentry id="opt.help-debug" xreflabel="--help-debug">
602 <term><option>--help-debug</option></term>
603 <listitem>
604 <para>Same as <option>--help</option>, but also lists debugging
605 options which usually are only of use to Valgrind's
606 developers.</para>
607 </listitem>
608 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000609
debad57fc2005-12-03 22:33:29 +0000610 <varlistentry id="opt.version" xreflabel="--version">
611 <term><option>--version</option></term>
612 <listitem>
613 <para>Show the version number of the Valgrind core. Tools can have
614 their own version numbers. There is a scheme in place to ensure
615 that tools only execute when the core version is one they are
616 known to work with. This was done to minimise the chances of
617 strange problems arising from tool-vs-core version
618 incompatibilities.</para>
619 </listitem>
620 </varlistentry>
njn51272982005-07-25 23:18:44 +0000621
debad57fc2005-12-03 22:33:29 +0000622 <varlistentry id="opt.quiet" xreflabel="--quiet">
623 <term><option>-q --quiet</option></term>
624 <listitem>
625 <para>Run silently, and only print error messages. Useful if you
626 are running regression tests or have some other automated test
627 machinery.</para>
628 </listitem>
629 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000630
debad57fc2005-12-03 22:33:29 +0000631 <varlistentry id="opt.verbose" xreflabel="--verbose">
632 <term><option>-v --verbose</option></term>
633 <listitem>
634 <para>Be more verbose. Gives extra information on various aspects
635 of your program, such as: the shared objects loaded, the
636 suppressions used, the progress of the instrumentation and
637 execution engines, and warnings about unusual behaviour. Repeating
638 the flag increases the verbosity level.</para>
639 </listitem>
640 </varlistentry>
sewardj053fe982005-11-15 19:51:04 +0000641
debad57fc2005-12-03 22:33:29 +0000642 <varlistentry id="opt.d" xreflabel="-d">
643 <term><option>-d</option></term>
644 <listitem>
645 <para>Emit information for debugging Valgrind itself. This is
646 usually only of interest to the Valgrind developers. Repeating
647 the flag produces more detailed output. If you want to send us a
648 bug report, a log of the output generated by
649 <option>-v -v -d -d</option> will make your report more
650 useful.</para>
651 </listitem>
652 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000653
debad57fc2005-12-03 22:33:29 +0000654 <varlistentry id="opt.tool" xreflabel="--tool">
655 <term>
656 <option><![CDATA[--tool=<toolname> [default: memcheck] ]]></option>
657 </term>
658 <listitem>
659 <para>Run the Valgrind tool called <varname>toolname</varname>,
sewardj08e31e22007-05-23 21:58:33 +0000660 e.g. Memcheck, Cachegrind, etc.</para>
debad57fc2005-12-03 22:33:29 +0000661 </listitem>
662 </varlistentry>
njn51272982005-07-25 23:18:44 +0000663
debad57fc2005-12-03 22:33:29 +0000664 <varlistentry id="opt.trace-children" xreflabel="--trace-children">
665 <term>
666 <option><![CDATA[--trace-children=<yes|no> [default: no] ]]></option>
667 </term>
668 <listitem>
njnae44c382007-05-15 03:59:23 +0000669 <para>When enabled, Valgrind will trace into sub-processes
670 initiated via the <varname>exec</varname> system call. This can be
671 confusing and isn't usually what you want, so it is disabled by
672 default.
673 </para>
674 <para>Note that Valgrind does trace into the child of a
sewardj79c62bc2007-11-28 01:55:29 +0000675 <varname>fork</varname> (it would be difficult not to, since
njnae44c382007-05-15 03:59:23 +0000676 <varname>fork</varname> makes an identical copy of a process), so this
677 option is arguably badly named. However, most children of
678 <varname>fork</varname> calls immediately call <varname>exec</varname>
679 anyway.
680 </para>
debad57fc2005-12-03 22:33:29 +0000681 </listitem>
682 </varlistentry>
njn51272982005-07-25 23:18:44 +0000683
sewardj778d7832007-11-22 01:21:56 +0000684 <varlistentry id="opt.child-silent-after-fork"
685 xreflabel="--child-silent-after-fork">
686 <term>
687 <option><![CDATA[--child-silent-after-fork=<yes|no> [default: no] ]]></option>
688 </term>
689 <listitem>
690 <para>When enabled, Valgrind will not show any debugging or
691 logging output for the child process resulting from
692 a <varname>fork</varname> call. This can make the output less
693 confusing (although more misleading) when dealing with processes
694 that create children. It is particularly useful in conjunction
695 with <varname>--trace-children=</varname>. Use of this flag is also
696 strongly recommended if you are requesting XML output
697 (<varname>--xml=yes</varname>), since otherwise the XML from child and
698 parent may become mixed up, which usually makes it useless.
699 </para>
700 </listitem>
701 </varlistentry>
702
debad57fc2005-12-03 22:33:29 +0000703 <varlistentry id="opt.track-fds" xreflabel="--track-fds">
704 <term>
705 <option><![CDATA[--track-fds=<yes|no> [default: no] ]]></option>
706 </term>
707 <listitem>
708 <para>When enabled, Valgrind will print out a list of open file
709 descriptors on exit. Along with each file descriptor is printed a
710 stack backtrace of where the file was opened and any details
711 relating to the file descriptor such as the file name or socket
712 details.</para>
713 </listitem>
714 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000715
debad57fc2005-12-03 22:33:29 +0000716 <varlistentry id="opt.time-stamp" xreflabel="--time-stamp">
717 <term>
718 <option><![CDATA[--time-stamp=<yes|no> [default: no] ]]></option>
719 </term>
720 <listitem>
721 <para>When enabled, each message is preceded with an indication of
722 the elapsed wallclock time since startup, expressed as days,
723 hours, minutes, seconds and milliseconds.</para>
724 </listitem>
725 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000726
debad57fc2005-12-03 22:33:29 +0000727 <varlistentry id="opt.log-fd" xreflabel="--log-fd">
728 <term>
729 <option><![CDATA[--log-fd=<number> [default: 2, stderr] ]]></option>
730 </term>
731 <listitem>
732 <para>Specifies that Valgrind should send all of its messages to
733 the specified file descriptor. The default, 2, is the standard
734 error channel (stderr). Note that this may interfere with the
735 client's own use of stderr, as Valgrind's output will be
736 interleaved with any output that the client sends to
737 stderr.</para>
738 </listitem>
739 </varlistentry>
njn779a2d62005-07-25 00:12:19 +0000740
debad57fc2005-12-03 22:33:29 +0000741 <varlistentry id="opt.log-file" xreflabel="--log-file">
742 <term>
743 <option><![CDATA[--log-file=<filename> ]]></option>
744 </term>
745 <listitem>
746 <para>Specifies that Valgrind should send all of its messages to
njn374a36d2007-11-23 01:41:32 +0000747 the specified file. If the file name is empty, it causes an abort.
748 There are three special format specifiers that can be used in the file
749 name.</para>
njn779a2d62005-07-25 00:12:19 +0000750
njn374a36d2007-11-23 01:41:32 +0000751 <para><option>%p</option> is replaced with the current process ID.
752 This is very useful for program that invoke multiple processes.
753 WARNING: If you use <option>--trace-children=yes</option> and your
njn7064fb22008-05-29 23:09:52 +0000754 program invokes multiple processes OR your program forks without
755 calling exec afterwards, and you don't use this specifier
njn374a36d2007-11-23 01:41:32 +0000756 (or the <option>%q</option> specifier below), the Valgrind output from
757 all those processes will go into one file, possibly jumbled up, and
njn498685c2007-09-17 23:15:35 +0000758 possibly incomplete.</para>
njn3e986b22004-11-30 10:43:45 +0000759
njn374a36d2007-11-23 01:41:32 +0000760 <para><option>%q{FOO}</option> is replaced with the contents of the
761 environment variable <varname>FOO</varname>. If the
762 <option>{FOO}</option> part is malformed, it causes an abort. This
763 specifier is rarely needed, but very useful in certain circumstances
764 (eg. when running MPI programs). The idea is that you specify a
765 variable which will be set differently for each process in the job,
766 for example <computeroutput>BPROC_RANK</computeroutput> or whatever is
767 applicable in your MPI setup. If the named environment variable is not
768 set, it causes an abort. Note that in some shells, the
769 <option>{</option> and <option>}</option> characters may need to be
770 escaped with a backslash.</para>
771
772 <para><option>%%</option> is replaced with <option>%</option>.</para>
773
774 <para>If an <option>%</option> is followed by any other character, it
775 causes an abort.</para>
debad57fc2005-12-03 22:33:29 +0000776 </listitem>
777 </varlistentry>
778
779 <varlistentry id="opt.log-socket" xreflabel="--log-socket">
780 <term>
781 <option><![CDATA[--log-socket=<ip-address:port-number> ]]></option>
782 </term>
783 <listitem>
784 <para>Specifies that Valgrind should send all of its messages to
785 the specified port at the specified IP address. The port may be
786 omitted, in which case port 1500 is used. If a connection cannot
787 be made to the specified socket, Valgrind falls back to writing
788 output to the standard error (stderr). This option is intended to
789 be used in conjunction with the
790 <computeroutput>valgrind-listener</computeroutput> program. For
791 further details, see
sewardj33878892007-11-17 09:43:25 +0000792 <link linkend="manual-core.comment">the commentary</link>
debad57fc2005-12-03 22:33:29 +0000793 in the manual.</para>
794 </listitem>
795 </varlistentry>
796
797</variablelist>
de03e0e7c2005-12-03 23:02:33 +0000798<!-- end of xi:include in the manpage -->
debad57fc2005-12-03 22:33:29 +0000799
800</sect2>
njn3e986b22004-11-30 10:43:45 +0000801
802
803<sect2 id="manual-core.erropts" xreflabel="Error-related Options">
804<title>Error-related options</title>
805
de03e0e7c2005-12-03 23:02:33 +0000806<!-- start of xi:include in the manpage -->
807<para id="error-related.opts.para">These options are used by all tools
808that can report errors, e.g. Memcheck, but not Cachegrind.</para>
njn3e986b22004-11-30 10:43:45 +0000809
de03e0e7c2005-12-03 23:02:33 +0000810<variablelist id="error-related.opts.list">
njn3e986b22004-11-30 10:43:45 +0000811
debad57fc2005-12-03 22:33:29 +0000812 <varlistentry id="opt.xml" xreflabel="--xml">
813 <term>
814 <option><![CDATA[--xml=<yes|no> [default: no] ]]></option>
815 </term>
816 <listitem>
817 <para>When enabled, output will be in XML format. This is aimed
818 at making life easier for tools that consume Valgrind's output as
819 input, such as GUI front ends. Currently this option only works
njnca54af32006-04-16 10:25:43 +0000820 with Memcheck.</para>
debad57fc2005-12-03 22:33:29 +0000821 </listitem>
822 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000823
debad57fc2005-12-03 22:33:29 +0000824 <varlistentry id="opt.xml-user-comment" xreflabel="--xml-user-comment">
825 <term>
826 <option><![CDATA[--xml-user-comment=<string> ]]></option>
827 </term>
828 <listitem>
829 <para>Embeds an extra user comment string at the start of the XML
830 output. Only works when <option>--xml=yes</option> is specified;
831 ignored otherwise.</para>
832 </listitem>
833 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000834
debad57fc2005-12-03 22:33:29 +0000835 <varlistentry id="opt.demangle" xreflabel="--demangle">
836 <term>
837 <option><![CDATA[--demangle=<yes|no> [default: yes] ]]></option>
838 </term>
839 <listitem>
840 <para>Enable/disable automatic demangling (decoding) of C++ names.
841 Enabled by default. When enabled, Valgrind will attempt to
842 translate encoded C++ names back to something approaching the
843 original. The demangler handles symbols mangled by g++ versions
844 2.X, 3.X and 4.X.</para>
njn3e986b22004-11-30 10:43:45 +0000845
debad57fc2005-12-03 22:33:29 +0000846 <para>An important fact about demangling is that function names
847 mentioned in suppressions files should be in their mangled form.
848 Valgrind does not demangle function names when searching for
849 applicable suppressions, because to do otherwise would make
850 suppressions file contents dependent on the state of Valgrind's
851 demangling machinery, and would also be slow and pointless.</para>
852 </listitem>
853 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000854
debad57fc2005-12-03 22:33:29 +0000855 <varlistentry id="opt.num-callers" xreflabel="--num-callers">
856 <term>
857 <option><![CDATA[--num-callers=<number> [default: 12] ]]></option>
858 </term>
859 <listitem>
860 <para>By default, Valgrind shows twelve levels of function call
861 names to help you identify program locations. You can change that
862 number with this option. This can help in determining the
863 program's location in deeply-nested call chains. Note that errors
864 are commoned up using only the top four function locations (the
865 place in the current function, and that of its three immediate
866 callers). So this doesn't affect the total number of errors
867 reported.</para>
njn3e986b22004-11-30 10:43:45 +0000868
debad57fc2005-12-03 22:33:29 +0000869 <para>The maximum value for this is 50. Note that higher settings
870 will make Valgrind run a bit more slowly and take a bit more
871 memory, but can be useful when working with programs with
872 deeply-nested call chains.</para>
873 </listitem>
874 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000875
debad57fc2005-12-03 22:33:29 +0000876 <varlistentry id="opt.error-limit" xreflabel="--error-limit">
877 <term>
878 <option><![CDATA[--error-limit=<yes|no> [default: yes] ]]></option>
879 </term>
880 <listitem>
sewardj58501082006-05-12 23:35:10 +0000881 <para>When enabled, Valgrind stops reporting errors after 10,000,000
debad57fc2005-12-03 22:33:29 +0000882 in total, or 1,000 different ones, have been seen. This is to
883 stop the error tracking machinery from becoming a huge performance
884 overhead in programs with many errors.</para>
885 </listitem>
886 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000887
sewardjb9779082006-05-12 23:50:15 +0000888 <varlistentry id="opt.error-exitcode" xreflabel="--error-exitcode">
889 <term>
890 <option><![CDATA[--error-exitcode=<number> [default: 0] ]]></option>
891 </term>
892 <listitem>
893 <para>Specifies an alternative exit code to return if Valgrind
894 reported any errors in the run. When set to the default value
895 (zero), the return value from Valgrind will always be the return
896 value of the process being simulated. When set to a nonzero value,
897 that value is returned instead, if Valgrind detects any errors.
898 This is useful for using Valgrind as part of an automated test
899 suite, since it makes it easy to detect test cases for which
900 Valgrind has reported errors, just by inspecting return codes.</para>
901 </listitem>
902 </varlistentry>
903
debad57fc2005-12-03 22:33:29 +0000904 <varlistentry id="opt.stack-traces" xreflabel="--show-below-main">
905 <term>
906 <option><![CDATA[--show-below-main=<yes|no> [default: no] ]]></option>
907 </term>
908 <listitem>
909 <para>By default, stack traces for errors do not show any
910 functions that appear beneath <function>main()</function>
911 (or similar functions such as glibc's
912 <function>__libc_start_main()</function>, if
913 <function>main()</function> is not present in the stack trace);
914 most of the time it's uninteresting C library stuff. If this
915 option is enabled, those entries below <function>main()</function>
916 will be shown.</para>
917 </listitem>
918 </varlistentry>
sewardjd153fae2005-01-10 17:24:47 +0000919
debad57fc2005-12-03 22:33:29 +0000920 <varlistentry id="opt.suppressions" xreflabel="--suppressions">
921 <term>
922 <option><![CDATA[--suppressions=<filename> [default: $PREFIX/lib/valgrind/default.supp] ]]></option>
923 </term>
924 <listitem>
925 <para>Specifies an extra file from which to read descriptions of
sewardjc44b2542008-05-14 06:43:10 +0000926 errors to suppress. You may use up to 100 extra suppression
927 files.</para>
debad57fc2005-12-03 22:33:29 +0000928 </listitem>
929 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000930
sewardj33878892007-11-17 09:43:25 +0000931 <varlistentry id="opt.gen-suppressions" xreflabel="--gen-suppressions">
debad57fc2005-12-03 22:33:29 +0000932 <term>
933 <option><![CDATA[--gen-suppressions=<yes|no|all> [default: no] ]]></option>
934 </term>
935 <listitem>
936 <para>When set to <varname>yes</varname>, Valgrind will pause
937 after every error shown and print the line:
938 <literallayout> ---- Print suppression ? --- [Return/N/n/Y/y/C/c] ----</literallayout>
njn3e986b22004-11-30 10:43:45 +0000939
debad57fc2005-12-03 22:33:29 +0000940 The prompt's behaviour is the same as for the
941 <option>--db-attach</option> option (see below).</para>
njn3e986b22004-11-30 10:43:45 +0000942
debad57fc2005-12-03 22:33:29 +0000943 <para>If you choose to, Valgrind will print out a suppression for
944 this error. You can then cut and paste it into a suppression file
945 if you don't want to hear about the error in the future.</para>
sewardjd153fae2005-01-10 17:24:47 +0000946
debad57fc2005-12-03 22:33:29 +0000947 <para>When set to <varname>all</varname>, Valgrind will print a
948 suppression for every reported error, without querying the
949 user.</para>
njn3e986b22004-11-30 10:43:45 +0000950
debad57fc2005-12-03 22:33:29 +0000951 <para>This option is particularly useful with C++ programs, as it
952 prints out the suppressions with mangled names, as
953 required.</para>
njn3e986b22004-11-30 10:43:45 +0000954
debad57fc2005-12-03 22:33:29 +0000955 <para>Note that the suppressions printed are as specific as
956 possible. You may want to common up similar ones, eg. by adding
sewardj08e31e22007-05-23 21:58:33 +0000957 wildcards to function names. Sometimes two different errors
debad57fc2005-12-03 22:33:29 +0000958 are suppressed by the same suppression, in which case Valgrind
959 will output the suppression more than once, but you only need to
960 have one copy in your suppression file (but having more than one
961 won't cause problems). Also, the suppression name is given as
962 <computeroutput>&lt;insert a suppression name
963 here&gt;</computeroutput>; the name doesn't really matter, it's
964 only used with the <option>-v</option> option which prints out all
965 used suppression records.</para>
966 </listitem>
967 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000968
debad57fc2005-12-03 22:33:29 +0000969 <varlistentry id="opt.db-attach" xreflabel="--db-attach">
970 <term>
971 <option><![CDATA[--db-attach=<yes|no> [default: no] ]]></option>
972 </term>
973 <listitem>
974 <para>When enabled, Valgrind will pause after every error shown
975 and print the line:
976 <literallayout> ---- Attach to debugger ? --- [Return/N/n/Y/y/C/c] ----</literallayout>
njn3e986b22004-11-30 10:43:45 +0000977
debad57fc2005-12-03 22:33:29 +0000978 Pressing <varname>Ret</varname>, or <varname>N Ret</varname> or
979 <varname>n Ret</varname>, causes Valgrind not to start a debugger
980 for this error.</para>
njn3e986b22004-11-30 10:43:45 +0000981
debad57fc2005-12-03 22:33:29 +0000982 <para>Pressing <varname>Y Ret</varname> or
983 <varname>y Ret</varname> causes Valgrind to start a debugger for
984 the program at this point. When you have finished with the
985 debugger, quit from it, and the program will continue. Trying to
986 continue from inside the debugger doesn't work.</para>
njn3e986b22004-11-30 10:43:45 +0000987
debad57fc2005-12-03 22:33:29 +0000988 <para><varname>C Ret</varname> or <varname>c Ret</varname> causes
989 Valgrind not to start a debugger, and not to ask again.</para>
debad57fc2005-12-03 22:33:29 +0000990 </listitem>
991 </varlistentry>
njn3e986b22004-11-30 10:43:45 +0000992
debad57fc2005-12-03 22:33:29 +0000993 <varlistentry id="opt.db-command" xreflabel="--db-command">
994 <term>
995 <option><![CDATA[--db-command=<command> [default: gdb -nw %f %p] ]]></option>
996 </term>
997 <listitem>
998 <para>Specify the debugger to use with the
999 <option>--db-attach</option> command. The default debugger is
1000 gdb. This option is a template that is expanded by Valgrind at
1001 runtime. <literal>%f</literal> is replaced with the executable's
1002 file name and <literal>%p</literal> is replaced by the process ID
1003 of the executable.</para>
njn3e986b22004-11-30 10:43:45 +00001004
debad57fc2005-12-03 22:33:29 +00001005 <para>This specifies how Valgrind will invoke the debugger. By
1006 default it will use whatever GDB is detected at build time, which
1007 is usually <computeroutput>/usr/bin/gdb</computeroutput>. Using
1008 this command, you can specify some alternative command to invoke
1009 the debugger you want to use.</para>
njn51272982005-07-25 23:18:44 +00001010
debad57fc2005-12-03 22:33:29 +00001011 <para>The command string given can include one or instances of the
1012 <literal>%p</literal> and <literal>%f</literal> expansions. Each
1013 instance of <literal>%p</literal> expands to the PID of the
1014 process to be debugged and each instance of <literal>%f</literal>
1015 expands to the path to the executable for the process to be
1016 debugged.</para>
sewardj778d7832007-11-22 01:21:56 +00001017
1018 <para>Since <computeroutput>&lt;command&gt;</computeroutput> is likely
1019 to contain spaces, you will need to put this entire flag in
1020 quotes to ensure it is correctly handled by the shell.</para>
debad57fc2005-12-03 22:33:29 +00001021 </listitem>
1022 </varlistentry>
1023
1024 <varlistentry id="opt.input-fd" xreflabel="--input-fd">
1025 <term>
1026 <option><![CDATA[--input-fd=<number> [default: 0, stdin] ]]></option>
1027 </term>
1028 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001029 <para>When using <option>--db-attach=yes</option> or
debad57fc2005-12-03 22:33:29 +00001030 <option>--gen-suppressions=yes</option>, Valgrind will stop so as
sewardj08e31e22007-05-23 21:58:33 +00001031 to read keyboard input from you when each error occurs. By
debad57fc2005-12-03 22:33:29 +00001032 default it reads from the standard input (stdin), which is
1033 problematic for programs which close stdin. This option allows
1034 you to specify an alternative file descriptor from which to read
1035 input.</para>
1036 </listitem>
1037 </varlistentry>
1038
1039 <varlistentry id="opt.max-stackframe" xreflabel="--max-stackframe">
1040 <term>
1041 <option><![CDATA[--max-stackframe=<number> [default: 2000000] ]]></option>
1042 </term>
1043 <listitem>
sewardj08e31e22007-05-23 21:58:33 +00001044 <para>The maximum size of a stack frame. If the stack pointer moves by
debad57fc2005-12-03 22:33:29 +00001045 more than this amount then Valgrind will assume that
1046 the program is switching to a different stack.</para>
1047
1048 <para>You may need to use this option if your program has large
1049 stack-allocated arrays. Valgrind keeps track of your program's
1050 stack pointer. If it changes by more than the threshold amount,
1051 Valgrind assumes your program is switching to a different stack,
1052 and Memcheck behaves differently than it would for a stack pointer
1053 change smaller than the threshold. Usually this heuristic works
1054 well. However, if your program allocates large structures on the
1055 stack, this heuristic will be fooled, and Memcheck will
1056 subsequently report large numbers of invalid stack accesses. This
1057 option allows you to change the threshold to a different
1058 value.</para>
1059
1060 <para>You should only consider use of this flag if Valgrind's
1061 debug output directs you to do so. In that case it will tell you
1062 the new threshold you should specify.</para>
1063
1064 <para>In general, allocating large structures on the stack is a
sewardj08e31e22007-05-23 21:58:33 +00001065 bad idea, because you can easily run out of stack space,
debad57fc2005-12-03 22:33:29 +00001066 especially on systems with limited memory or which expect to
sewardj08e31e22007-05-23 21:58:33 +00001067 support large numbers of threads each with a small stack, and also
debad57fc2005-12-03 22:33:29 +00001068 because the error checking performed by Memcheck is more effective
1069 for heap-allocated data than for stack-allocated data. If you
1070 have to use this flag, you may wish to consider rewriting your
1071 code to allocate on the heap rather than on the stack.</para>
1072 </listitem>
1073 </varlistentry>
1074
sewardj95d86c02007-12-18 01:49:23 +00001075 <varlistentry id="opt.main-stacksize" xreflabel="--main-stacksize">
1076 <term>
1077 <option><![CDATA[--main-stacksize=<number>
1078 [default: use current 'ulimit' value] ]]></option>
1079 </term>
1080 <listitem>
1081 <para>Specifies the size of the main thread's stack.</para>
1082
1083 <para>To simplify its memory management, Valgrind reserves all
1084 required space for the main thread's stack at startup. That
1085 means it needs to know the required stack size at
1086 startup.</para>
1087
1088 <para>By default, Valgrind uses the current "ulimit" value for
1089 the stack size, or 16 MB, whichever is lower. In many cases
1090 this gives a stack size in the range 8 to 16 MB, which almost
1091 never overflows for most applications.</para>
1092
1093 <para>If you need a larger total stack size,
1094 use <option>--main-stacksize</option> to specify it. Only set
1095 it as high as you need, since reserving far more space than you
1096 need (that is, hundreds of megabytes more than you need)
1097 constrains Valgrind's memory allocators and may reduce the total
1098 amount of memory that Valgrind can use. This is only really of
1099 significance on 32-bit machines.</para>
1100
1101 <para>On Linux, you may request a stack of size up to 2GB.
1102 Valgrind will stop with a diagnostic message if the stack cannot
1103 be allocated. On AIX5 the allowed stack size is restricted to
1104 128MB.</para>
1105
1106 <para><option>--main-stacksize</option> only affects the stack
1107 size for the program's initial thread. It has no bearing on the
1108 size of thread stacks, as Valgrind does not allocate
1109 those.</para>
1110
1111 <para>You may need to use both <option>--main-stacksize</option>
1112 and <option>--max-stackframe</option> together. It is important
1113 to understand that <option>--main-stacksize</option> sets the
1114 maximum total stack size,
1115 whilst <option>--max-stackframe</option> specifies the largest
1116 size of any one stack frame. You will have to work out
1117 the <option>--main-stacksize</option> value for yourself
1118 (usually, if your applications segfaults). But Valgrind will
1119 tell you the needed <option>--max-stackframe</option> size, if
1120 necessary.</para>
1121
1122 <para>As discussed further in the description
1123 of <option>--max-stackframe</option>, a requirement for a large
1124 stack is a sign of potential portability problems. You are best
1125 advised to place all large data in heap-allocated memory.</para>
1126 </listitem>
1127 </varlistentry>
1128
debad57fc2005-12-03 22:33:29 +00001129</variablelist>
de03e0e7c2005-12-03 23:02:33 +00001130<!-- end of xi:include in the manpage -->
debad57fc2005-12-03 22:33:29 +00001131
njn3e986b22004-11-30 10:43:45 +00001132</sect2>
1133
debad57fc2005-12-03 22:33:29 +00001134
njn3e986b22004-11-30 10:43:45 +00001135<sect2 id="manual-core.mallocopts" xreflabel="malloc()-related Options">
1136<title><computeroutput>malloc()</computeroutput>-related Options</title>
1137
de03e0e7c2005-12-03 23:02:33 +00001138<!-- start of xi:include in the manpage -->
1139<para id="malloc-related.opts.para">For tools that use their own version of
njn3e986b22004-11-30 10:43:45 +00001140<computeroutput>malloc()</computeroutput> (e.g. Memcheck and
njn1d0825f2006-03-27 11:37:07 +00001141Massif), the following options apply.</para>
njn3e986b22004-11-30 10:43:45 +00001142
de03e0e7c2005-12-03 23:02:33 +00001143<variablelist id="malloc-related.opts.list">
njn3e986b22004-11-30 10:43:45 +00001144
debad57fc2005-12-03 22:33:29 +00001145 <varlistentry id="opt.alignment" xreflabel="--alignment">
1146 <term>
1147 <option><![CDATA[--alignment=<number> [default: 8] ]]></option>
1148 </term>
1149 <listitem>
1150 <para>By default Valgrind's <function>malloc()</function>,
1151 <function>realloc()</function>, etc, return 8-byte aligned
1152 addresses. This is standard for most processors. However, some
1153 programs might assume that <function>malloc()</function> et al
1154 return 16-byte or more aligned memory. The supplied value must be
1155 between 8 and 4096 inclusive, and must be a power of two.</para>
njn51272982005-07-25 23:18:44 +00001156 </listitem>
debad57fc2005-12-03 22:33:29 +00001157 </varlistentry>
njn51272982005-07-25 23:18:44 +00001158
debad57fc2005-12-03 22:33:29 +00001159</variablelist>
de03e0e7c2005-12-03 23:02:33 +00001160<!-- end of xi:include in the manpage -->
debad57fc2005-12-03 22:33:29 +00001161
1162</sect2>
1163
1164
1165<sect2 id="manual-core.rareopts" xreflabel="Uncommon Options">
1166<title>Uncommon Options</title>
1167
de03e0e7c2005-12-03 23:02:33 +00001168<!-- start of xi:include in the manpage -->
1169<para id="uncommon.opts.para">These options apply to all tools, as they
1170affect certain obscure workings of the Valgrind core. Most people won't
1171need to use these.</para>
debad57fc2005-12-03 22:33:29 +00001172
de03e0e7c2005-12-03 23:02:33 +00001173<variablelist id="uncommon.opts.list">
debad57fc2005-12-03 22:33:29 +00001174
1175 <varlistentry id="opt.run-libc-freeres" xreflabel="--run-libc-freeres">
1176 <term>
1177 <option><![CDATA[--run-libc-freeres=<yes|no> [default: yes] ]]></option>
1178 </term>
1179 <listitem>
1180 <para>The GNU C library (<function>libc.so</function>), which is
1181 used by all programs, may allocate memory for its own uses.
1182 Usually it doesn't bother to free that memory when the program
sewardj33878892007-11-17 09:43:25 +00001183 ends&mdash;there would be no point, since the Linux kernel reclaims
debad57fc2005-12-03 22:33:29 +00001184 all process resources when a process exits anyway, so it would
1185 just slow things down.</para>
1186
1187 <para>The glibc authors realised that this behaviour causes leak
1188 checkers, such as Valgrind, to falsely report leaks in glibc, when
1189 a leak check is done at exit. In order to avoid this, they
1190 provided a routine called <function>__libc_freeres</function>
1191 specifically to make glibc release all memory it has allocated.
njn1d0825f2006-03-27 11:37:07 +00001192 Memcheck therefore tries to run
debad57fc2005-12-03 22:33:29 +00001193 <function>__libc_freeres</function> at exit.</para>
1194
sewardj08e31e22007-05-23 21:58:33 +00001195 <para>Unfortunately, in some very old versions of glibc,
debad57fc2005-12-03 22:33:29 +00001196 <function>__libc_freeres</function> is sufficiently buggy to cause
sewardj08e31e22007-05-23 21:58:33 +00001197 segmentation faults. This was particularly noticeable on Red Hat
debad57fc2005-12-03 22:33:29 +00001198 7.1. So this flag is provided in order to inhibit the run of
1199 <function>__libc_freeres</function>. If your program seems to run
1200 fine on Valgrind, but segfaults at exit, you may find that
1201 <option>--run-libc-freeres=no</option> fixes that, although at the
1202 cost of possibly falsely reporting space leaks in
1203 <filename>libc.so</filename>.</para>
1204 </listitem>
1205 </varlistentry>
1206
1207 <varlistentry id="opt.sim-hints" xreflabel="--sim-hints">
1208 <term>
1209 <option><![CDATA[--sim-hints=hint1,hint2,... ]]></option>
1210 </term>
1211 <listitem>
1212 <para>Pass miscellaneous hints to Valgrind which slightly modify
1213 the simulated behaviour in nonstandard or dangerous ways, possibly
1214 to help the simulation of strange features. By default no hints
1215 are enabled. Use with caution! Currently known hints are:</para>
1216 <itemizedlist>
1217 <listitem>
1218 <para><option>lax-ioctls: </option> Be very lax about ioctl
1219 handling; the only assumption is that the size is
1220 correct. Doesn't require the full buffer to be initialized
1221 when writing. Without this, using some device drivers with a
1222 large number of strange ioctl commands becomes very
1223 tiresome.</para>
1224 </listitem>
1225 <listitem>
1226 <para><option>enable-inner: </option> Enable some special
1227 magic needed when the program being run is itself
1228 Valgrind.</para>
1229 </listitem>
1230 </itemizedlist>
1231 </listitem>
1232 </varlistentry>
1233
1234 <varlistentry id="opt.kernel-variant" xreflabel="--kernel-variant">
1235 <term>
1236 <option>--kernel-variant=variant1,variant2,...</option>
1237 </term>
1238 <listitem>
1239 <para>Handle system calls and ioctls arising from minor variants
1240 of the default kernel for this platform. This is useful for
1241 running on hacked kernels or with kernel modules which support
1242 nonstandard ioctls, for example. Use with caution. If you don't
1243 understand what this option does then you almost certainly don't
1244 need it. Currently known variants are:</para>
1245 <itemizedlist>
1246 <listitem>
1247 <para><option>bproc: </option> Support the sys_broc system
1248 call on x86. This is for running on BProc, which is a minor
1249 variant of standard Linux which is sometimes used for building
1250 clusters.</para>
1251 </listitem>
1252 </itemizedlist>
1253 </listitem>
1254 </varlistentry>
1255
1256 <varlistentry id="opt.show-emwarns" xreflabel="--show-emwarns">
1257 <term>
1258 <option><![CDATA[--show-emwarns=<yes|no> [default: no] ]]></option>
1259 </term>
1260 <listitem>
1261 <para>When enabled, Valgrind will emit warnings about its CPU
1262 emulation in certain cases. These are usually not
1263 interesting.</para>
1264 </listitem>
1265 </varlistentry>
1266
1267 <varlistentry id="opt.smc-check" xreflabel="--smc-check">
1268 <term>
1269 <option><![CDATA[--smc-check=<none|stack|all> [default: stack] ]]></option>
1270 </term>
1271 <listitem>
1272 <para>This option controls Valgrind's detection of self-modifying
1273 code. Valgrind can do no detection, detect self-modifying code on
1274 the stack, or detect self-modifying code anywhere. Note that the
1275 default option will catch the vast majority of cases, as far as we
1276 know. Running with <varname>all</varname> will slow Valgrind down
sewardj08e31e22007-05-23 21:58:33 +00001277 greatly. Running with <varname>none</varname> will rarely
debad57fc2005-12-03 22:33:29 +00001278 speed things up, since very little code gets put on the stack for
sewardj08e31e22007-05-23 21:58:33 +00001279 most programs.</para>
1280
1281 <para>Some architectures (including ppc32 and ppc64) require
1282 programs which create code at runtime to flush the instruction
1283 cache in between code generation and first use. Valgrind
1284 observes and honours such instructions. Hence, on ppc32/Linux
1285 and ppc64/Linux, Valgrind always provides complete, transparent
1286 support for self-modifying code. It is only on x86/Linux
1287 and amd64/Linux that you need to use this flag.</para>
debad57fc2005-12-03 22:33:29 +00001288 </listitem>
1289 </varlistentry>
1290
1291</variablelist>
de03e0e7c2005-12-03 23:02:33 +00001292<!-- end of xi:include in the manpage -->
debad57fc2005-12-03 22:33:29 +00001293
njn3e986b22004-11-30 10:43:45 +00001294</sect2>
1295
1296
1297<sect2 id="manual-core.debugopts" xreflabel="Debugging Valgrind Options">
1298<title>Debugging Valgrind Options</title>
1299
de03e0e7c2005-12-03 23:02:33 +00001300<!-- start of xi:include in the manpage -->
1301<para id="debug.opts.para">There are also some options for debugging
1302Valgrind itself. You shouldn't need to use them in the normal run of
1303things. If you wish to see the list, use the
1304<option>--help-debug</option> option.</para>
1305<!-- end of xi:include in the manpage -->
njn3e986b22004-11-30 10:43:45 +00001306
njn3e986b22004-11-30 10:43:45 +00001307</sect2>
1308
1309
1310<sect2 id="manual-core.defopts" xreflabel="Setting default options">
1311<title>Setting default Options</title>
1312
1313<para>Note that Valgrind also reads options from three places:</para>
1314
1315 <orderedlist>
1316 <listitem>
1317 <para>The file <computeroutput>~/.valgrindrc</computeroutput></para>
1318 </listitem>
1319
1320 <listitem>
1321 <para>The environment variable
1322 <computeroutput>$VALGRIND_OPTS</computeroutput></para>
1323 </listitem>
1324
1325 <listitem>
1326 <para>The file <computeroutput>./.valgrindrc</computeroutput></para>
1327 </listitem>
1328 </orderedlist>
1329
1330<para>These are processed in the given order, before the
1331command-line options. Options processed later override those
1332processed earlier; for example, options in
1333<computeroutput>./.valgrindrc</computeroutput> will take
1334precedence over those in
1335<computeroutput>~/.valgrindrc</computeroutput>. The first two
1336are particularly useful for setting the default tool to
1337use.</para>
1338
1339<para>Any tool-specific options put in
1340<computeroutput>$VALGRIND_OPTS</computeroutput> or the
1341<computeroutput>.valgrindrc</computeroutput> files should be
1342prefixed with the tool name and a colon. For example, if you
1343want Memcheck to always do leak checking, you can put the
1344following entry in <literal>~/.valgrindrc</literal>:</para>
1345
1346<programlisting><![CDATA[
1347--memcheck:leak-check=yes]]></programlisting>
1348
1349<para>This will be ignored if any tool other than Memcheck is
1350run. Without the <computeroutput>memcheck:</computeroutput>
1351part, this will cause problems if you select other tools that
1352don't understand
1353<computeroutput>--leak-check=yes</computeroutput>.</para>
1354
1355</sect2>
1356
1357</sect1>
1358
1359
sewardj778d7832007-11-22 01:21:56 +00001360
1361
1362<sect1 id="manual-core.pthreads" xreflabel="Support for Threads">
1363<title>Support for Threads</title>
1364
1365<para>Valgrind supports programs which use POSIX pthreads.
1366Getting this to work was technically challenging but it now works
1367well enough for significant threaded applications to run.</para>
1368
1369<para>The main thing to point out is that although Valgrind works
1370with the standard Linux threads library (eg. NPTL or LinuxThreads), it
1371serialises execution so that only one thread is running at a time. This
1372approach avoids the horrible implementation problems of implementing a
1373truly multiprocessor version of Valgrind, but it does mean that threaded
1374apps run only on one CPU, even if you have a multiprocessor
1375machine.</para>
1376
1377<para>Valgrind schedules your program's threads in a round-robin fashion,
1378with all threads having equal priority. It switches threads
1379every 100000 basic blocks (on x86, typically around 600000
1380instructions), which means you'll get a much finer interleaving
1381of thread executions than when run natively. This in itself may
1382cause your program to behave differently if you have some kind of
1383concurrency, critical race, locking, or similar, bugs. In that case
1384you might consider using Valgrind's Helgrind tool to track them down.</para>
1385
1386<para>Your program will use the native
1387<computeroutput>libpthread</computeroutput>, but not all of its facilities
1388will work. In particular, synchronisation of processes via shared-memory
1389segments will not work. This relies on special atomic instruction sequences
1390which Valgrind does not emulate in a way which works between processes.
1391Unfortunately there's no way for Valgrind to warn when this is happening,
1392and such calls will mostly work. Only when there's a race will
1393it fail.
1394</para>
1395
1396<para>Valgrind also supports direct use of the
1397<computeroutput>clone()</computeroutput> system call,
1398<computeroutput>futex()</computeroutput> and so on.
1399<computeroutput>clone()</computeroutput> is supported where either
1400everything is shared (a thread) or nothing is shared (fork-like); partial
1401sharing will fail. Again, any use of atomic instruction sequences in shared
1402memory between processes will not work reliably.
1403</para>
1404
1405
1406</sect1>
1407
1408<sect1 id="manual-core.signals" xreflabel="Handling of Signals">
1409<title>Handling of Signals</title>
1410
1411<para>Valgrind has a fairly complete signal implementation. It should be
1412able to cope with any POSIX-compliant use of signals.</para>
1413
1414<para>If you're using signals in clever ways (for example, catching
1415SIGSEGV, modifying page state and restarting the instruction), you're
1416probably relying on precise exceptions. In this case, you will need
1417to use <computeroutput>--vex-iropt-precise-memory-exns=yes</computeroutput>.
1418</para>
1419
1420<para>If your program dies as a result of a fatal core-dumping signal,
1421Valgrind will generate its own core file
1422(<computeroutput>vgcore.NNNNN</computeroutput>) containing your program's
1423state. You may use this core file for post-mortem debugging with gdb or
1424similar. (Note: it will not generate a core if your core dump size limit is
14250.) At the time of writing the core dumps do not include all the floating
1426point register information.</para>
1427
1428<para>In the unlikely event that Valgrind itself crashes, the operating system
1429will create a core dump in the usual way.</para>
1430
1431</sect1>
1432
1433
1434
1435
1436
1437
1438
1439
1440<sect1 id="manual-core.install" xreflabel="Building and Installing">
1441<title>Building and Installing Valgrind</title>
1442
1443<para>We use the standard Unix
1444<computeroutput>./configure</computeroutput>,
1445<computeroutput>make</computeroutput>, <computeroutput>make
1446install</computeroutput> mechanism, and we have attempted to
1447ensure that it works on machines with kernel 2.4 or 2.6 and glibc
14482.2.X to 2.5.X. Once you have completed
1449<computeroutput>make install</computeroutput> you may then want
1450to run the regression tests
1451with <computeroutput>make regtest</computeroutput>.
1452</para>
1453
1454<para>There are five options (in addition to the usual
1455<option>--prefix=</option> which affect how Valgrind is built:
1456<itemizedlist>
1457
1458 <listitem>
1459 <para><option>--enable-inner</option></para>
1460 <para>This builds Valgrind with some special magic hacks which make
1461 it possible to run it on a standard build of Valgrind (what the
1462 developers call "self-hosting"). Ordinarily you should not use
1463 this flag as various kinds of safety checks are disabled.
1464 </para>
1465 </listitem>
1466
1467 <listitem>
1468 <para><option>--enable-tls</option></para>
1469 <para>TLS (Thread Local Storage) is a relatively new mechanism which
1470 requires compiler, linker and kernel support. Valgrind tries to
1471 automatically test if TLS is supported and if so enables this option.
1472 Sometimes it cannot test for TLS, so this option allows you to
1473 override the automatic test.</para>
1474 </listitem>
1475
1476 <listitem>
1477 <para><option>--with-vex=</option></para>
1478 <para>Specifies the path to the underlying VEX dynamic-translation
1479 library. By default this is taken to be in the VEX directory off
1480 the root of the source tree.
1481 </para>
1482 </listitem>
1483
1484 <listitem>
1485 <para><option>--enable-only64bit</option></para>
1486 <para><option>--enable-only32bit</option></para>
1487 <para>On 64-bit
1488 platforms (amd64-linux, ppc64-linux), Valgrind is by default built
1489 in such a way that both 32-bit and 64-bit executables can be run.
1490 Sometimes this cleverness is a problem for a variety of reasons.
1491 These two flags allow for single-target builds in this situation.
1492 If you issue both, the configure script will complain. Note they
1493 are ignored on 32-bit-only platforms (x86-linux, ppc32-linux).
1494 </para>
1495 </listitem>
1496
1497</itemizedlist>
1498</para>
1499
1500<para>The <computeroutput>configure</computeroutput> script tests
1501the version of the X server currently indicated by the current
1502<computeroutput>$DISPLAY</computeroutput>. This is a known bug.
1503The intention was to detect the version of the current X
1504client libraries, so that correct suppressions could be selected
1505for them, but instead the test checks the server version. This
1506is just plain wrong.</para>
1507
1508<para>If you are building a binary package of Valgrind for
1509distribution, please read <literal>README_PACKAGERS</literal>
1510<xref linkend="dist.readme-packagers"/>. It contains some
1511important information.</para>
1512
1513<para>Apart from that, there's not much excitement here. Let us
1514know if you have build problems.</para>
1515
1516</sect1>
1517
1518
1519
1520<sect1 id="manual-core.problems" xreflabel="If You Have Problems">
1521<title>If You Have Problems</title>
1522
1523<para>Contact us at <ulink url="&vg-url;">&vg-url;</ulink>.</para>
1524
1525<para>See <xref linkend="manual-core.limits"/> for the known
1526limitations of Valgrind, and for a list of programs which are
1527known not to work on it.</para>
1528
1529<para>All parts of the system make heavy use of assertions and
1530internal self-checks. They are permanently enabled, and we have no
1531plans to disable them. If one of them breaks, please mail us!</para>
1532
1533<para>If you get an assertion failure
1534in <filename>m_mallocfree.c</filename>, this may have happened because
1535your program wrote off the end of a malloc'd block, or before its
1536beginning. Valgrind hopefully will have emitted a proper message to that
1537effect before dying in this way. This is a known problem which
1538we should fix.</para>
1539
1540<para>Read the <xref linkend="FAQ"/> for more advice about common problems,
1541crashes, etc.</para>
1542
1543</sect1>
1544
1545
1546
1547<sect1 id="manual-core.limits" xreflabel="Limitations">
1548<title>Limitations</title>
1549
1550<para>The following list of limitations seems long. However, most
1551programs actually work fine.</para>
1552
1553<para>Valgrind will run Linux ELF binaries, on a kernel 2.4.X or 2.6.X
1554system, on the x86, amd64, ppc32 and ppc64 architectures, subject to the
1555following constraints:</para>
1556
1557 <itemizedlist>
1558 <listitem>
1559 <para>On x86 and amd64, there is no support for 3DNow! instructions.
1560 If the translator encounters these, Valgrind will generate a SIGILL
1561 when the instruction is executed. Apart from that, on x86 and amd64,
1562 essentially all instructions are supported, up to and including SSE3.
1563 </para>
1564
1565 <para>On ppc32 and ppc64, almost all integer, floating point and Altivec
1566 instructions are supported. Specifically: integer and FP insns that are
1567 mandatory for PowerPC, the "General-purpose optional" group (fsqrt, fsqrts,
1568 stfiwx), the "Graphics optional" group (fre, fres, frsqrte, frsqrtes), and
1569 the Altivec (also known as VMX) SIMD instruction set, are supported.</para>
1570 </listitem>
1571
1572 <listitem>
1573 <para>Atomic instruction sequences are not properly supported, in the
1574 sense that their atomicity is not preserved. This will affect any
1575 use of synchronization via memory shared between processes. They
1576 will appear to work, but fail sporadically.</para>
1577 </listitem>
1578
1579 <listitem>
1580 <para>If your program does its own memory management, rather than
1581 using malloc/new/free/delete, it should still work, but Memcheck's
1582 error checking won't be so effective. If you describe your program's
1583 memory management scheme using "client requests"
sewardj4c3270b2007-11-25 14:09:26 +00001584 (see <xref linkend="manual-core-adv.clientreq"/>), Memcheck can do
sewardj778d7832007-11-22 01:21:56 +00001585 better. Nevertheless, using malloc/new and free/delete is still the
1586 best approach.</para>
1587 </listitem>
1588
1589 <listitem>
1590 <para>Valgrind's signal simulation is not as robust as it could be.
1591 Basic POSIX-compliant sigaction and sigprocmask functionality is
1592 supplied, but it's conceivable that things could go badly awry if you
1593 do weird things with signals. Workaround: don't. Programs that do
1594 non-POSIX signal tricks are in any case inherently unportable, so
1595 should be avoided if possible.</para>
1596 </listitem>
1597
1598 <listitem>
1599 <para>Machine instructions, and system calls, have been implemented
1600 on demand. So it's possible, although unlikely, that a program will
1601 fall over with a message to that effect. If this happens, please
1602 report all the details printed out, so we can try and implement the
1603 missing feature.</para>
1604 </listitem>
1605
1606 <listitem>
1607 <para>Memory consumption of your program is majorly increased whilst
1608 running under Valgrind. This is due to the large amount of
1609 administrative information maintained behind the scenes. Another
1610 cause is that Valgrind dynamically translates the original
1611 executable. Translated, instrumented code is 12-18 times larger than
1612 the original so you can easily end up with 50+ MB of translations
1613 when running (eg) a web browser.</para>
1614 </listitem>
1615
1616 <listitem>
1617 <para>Valgrind can handle dynamically-generated code just fine. If
1618 you regenerate code over the top of old code (ie. at the same memory
1619 addresses), if the code is on the stack Valgrind will realise the
1620 code has changed, and work correctly. This is necessary to handle
1621 the trampolines GCC uses to implemented nested functions. If you
1622 regenerate code somewhere other than the stack, you will need to use
1623 the <option>--smc-check=all</option> flag, and Valgrind will run more
1624 slowly than normal.</para>
1625 </listitem>
1626
1627 <listitem>
1628 <para>As of version 3.0.0, Valgrind has the following limitations
1629 in its implementation of x86/AMD64 floating point relative to
1630 IEEE754.</para>
1631
1632 <para>Precision: There is no support for 80 bit arithmetic.
1633 Internally, Valgrind represents all such "long double" numbers in 64
1634 bits, and so there may be some differences in results. Whether or
1635 not this is critical remains to be seen. Note, the x86/amd64
1636 fldt/fstpt instructions (read/write 80-bit numbers) are correctly
1637 simulated, using conversions to/from 64 bits, so that in-memory
1638 images of 80-bit numbers look correct if anyone wants to see.</para>
1639
1640 <para>The impression observed from many FP regression tests is that
1641 the accuracy differences aren't significant. Generally speaking, if
1642 a program relies on 80-bit precision, there may be difficulties
1643 porting it to non x86/amd64 platforms which only support 64-bit FP
1644 precision. Even on x86/amd64, the program may get different results
1645 depending on whether it is compiled to use SSE2 instructions (64-bits
1646 only), or x87 instructions (80-bit). The net effect is to make FP
1647 programs behave as if they had been run on a machine with 64-bit IEEE
1648 floats, for example PowerPC. On amd64 FP arithmetic is done by
1649 default on SSE2, so amd64 looks more like PowerPC than x86 from an FP
1650 perspective, and there are far fewer noticeable accuracy differences
1651 than with x86.</para>
1652
1653 <para>Rounding: Valgrind does observe the 4 IEEE-mandated rounding
1654 modes (to nearest, to +infinity, to -infinity, to zero) for the
1655 following conversions: float to integer, integer to float where
1656 there is a possibility of loss of precision, and float-to-float
1657 rounding. For all other FP operations, only the IEEE default mode
1658 (round to nearest) is supported.</para>
1659
1660 <para>Numeric exceptions in FP code: IEEE754 defines five types of
1661 numeric exception that can happen: invalid operation (sqrt of
1662 negative number, etc), division by zero, overflow, underflow,
1663 inexact (loss of precision).</para>
1664
1665 <para>For each exception, two courses of action are defined by IEEE754:
1666 either (1) a user-defined exception handler may be called, or (2) a
1667 default action is defined, which "fixes things up" and allows the
1668 computation to proceed without throwing an exception.</para>
1669
1670 <para>Currently Valgrind only supports the default fixup actions.
1671 Again, feedback on the importance of exception support would be
1672 appreciated.</para>
1673
1674 <para>When Valgrind detects that the program is trying to exceed any
1675 of these limitations (setting exception handlers, rounding mode, or
1676 precision control), it can print a message giving a traceback of
1677 where this has happened, and continue execution. This behaviour used
1678 to be the default, but the messages are annoying and so showing them
1679 is now disabled by default. Use <option>--show-emwarns=yes</option> to see
1680 them.</para>
1681
1682 <para>The above limitations define precisely the IEEE754 'default'
1683 behaviour: default fixup on all exceptions, round-to-nearest
1684 operations, and 64-bit precision.</para>
1685 </listitem>
1686
1687 <listitem>
1688 <para>As of version 3.0.0, Valgrind has the following limitations in
1689 its implementation of x86/AMD64 SSE2 FP arithmetic, relative to
1690 IEEE754.</para>
1691
1692 <para>Essentially the same: no exceptions, and limited observance of
1693 rounding mode. Also, SSE2 has control bits which make it treat
1694 denormalised numbers as zero (DAZ) and a related action, flush
1695 denormals to zero (FTZ). Both of these cause SSE2 arithmetic to be
1696 less accurate than IEEE requires. Valgrind detects, ignores, and can
1697 warn about, attempts to enable either mode.</para>
1698 </listitem>
1699
1700 <listitem>
1701 <para>As of version 3.2.0, Valgrind has the following limitations
1702 in its implementation of PPC32 and PPC64 floating point
1703 arithmetic, relative to IEEE754.</para>
1704
1705 <para>Scalar (non-Altivec): Valgrind provides a bit-exact emulation of
1706 all floating point instructions, except for "fre" and "fres", which are
1707 done more precisely than required by the PowerPC architecture specification.
1708 All floating point operations observe the current rounding mode.
1709 </para>
1710
1711 <para>However, fpscr[FPRF] is not set after each operation. That could
1712 be done but would give measurable performance overheads, and so far
1713 no need for it has been found.</para>
1714
1715 <para>As on x86/AMD64, IEEE754 exceptions are not supported: all floating
1716 point exceptions are handled using the default IEEE fixup actions.
1717 Valgrind detects, ignores, and can warn about, attempts to unmask
1718 the 5 IEEE FP exception kinds by writing to the floating-point status
1719 and control register (fpscr).
1720 </para>
1721
1722 <para>Vector (Altivec, VMX): essentially as with x86/AMD64 SSE/SSE2:
1723 no exceptions, and limited observance of rounding mode.
1724 For Altivec, FP arithmetic
1725 is done in IEEE/Java mode, which is more accurate than the Linux default
1726 setting. "More accurate" means that denormals are handled properly,
1727 rather than simply being flushed to zero.</para>
1728 </listitem>
1729 </itemizedlist>
1730
1731 <para>Programs which are known not to work are:</para>
1732 <itemizedlist>
1733 <listitem>
1734 <para>emacs starts up but immediately concludes it is out of
1735 memory and aborts. It may be that Memcheck does not provide
1736 a good enough emulation of the
1737 <computeroutput>mallinfo</computeroutput> function.
1738 Emacs works fine if you build it to use
1739 the standard malloc/free routines.</para>
1740 </listitem>
1741 </itemizedlist>
1742
1743</sect1>
1744
1745
1746<sect1 id="manual-core.example" xreflabel="An Example Run">
1747<title>An Example Run</title>
1748
1749<para>This is the log for a run of a small program using Memcheck.
1750The program is in fact correct, and the reported error is as the
1751result of a potentially serious code generation bug in GNU g++
1752(snapshot 20010527).</para>
1753
1754<programlisting><![CDATA[
1755sewardj@phoenix:~/newmat10$ ~/Valgrind-6/valgrind -v ./bogon
1756==25832== Valgrind 0.10, a memory error detector for x86 RedHat 7.1.
1757==25832== Copyright (C) 2000-2001, and GNU GPL'd, by Julian Seward.
1758==25832== Startup, with flags:
1759==25832== --suppressions=/home/sewardj/Valgrind/redhat71.supp
1760==25832== reading syms from /lib/ld-linux.so.2
1761==25832== reading syms from /lib/libc.so.6
1762==25832== reading syms from /mnt/pima/jrs/Inst/lib/libgcc_s.so.0
1763==25832== reading syms from /lib/libm.so.6
1764==25832== reading syms from /mnt/pima/jrs/Inst/lib/libstdc++.so.3
1765==25832== reading syms from /home/sewardj/Valgrind/valgrind.so
1766==25832== reading syms from /proc/self/exe
1767==25832==
1768==25832== Invalid read of size 4
1769==25832== at 0x8048724: BandMatrix::ReSize(int,int,int) (bogon.cpp:45)
1770==25832== by 0x80487AF: main (bogon.cpp:66)
1771==25832== Address 0xBFFFF74C is not stack'd, malloc'd or free'd
1772==25832==
1773==25832== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
1774==25832== malloc/free: in use at exit: 0 bytes in 0 blocks.
1775==25832== malloc/free: 0 allocs, 0 frees, 0 bytes allocated.
1776==25832== For a detailed leak analysis, rerun with: --leak-check=yes
1777]]></programlisting>
1778
1779<para>The GCC folks fixed this about a week before gcc-3.0
1780shipped.</para>
1781
1782</sect1>
1783
1784
1785<sect1 id="manual-core.warnings" xreflabel="Warning Messages">
1786<title>Warning Messages You Might See</title>
1787
1788<para>Most of these only appear if you run in verbose mode
1789(enabled by <computeroutput>-v</computeroutput>):</para>
1790
1791 <itemizedlist>
1792
1793 <listitem>
1794 <para><computeroutput>More than 100 errors detected. Subsequent
1795 errors will still be recorded, but in less detail than
1796 before.</computeroutput></para>
1797
1798 <para>After 100 different errors have been shown, Valgrind becomes
1799 more conservative about collecting them. It then requires only the
1800 program counters in the top two stack frames to match when deciding
1801 whether or not two errors are really the same one. Prior to this
1802 point, the PCs in the top four frames are required to match. This
1803 hack has the effect of slowing down the appearance of new errors
1804 after the first 100. The 100 constant can be changed by recompiling
1805 Valgrind.</para>
1806 </listitem>
1807
1808 <listitem>
1809 <para><computeroutput>More than 1000 errors detected. I'm not
1810 reporting any more. Final error counts may be inaccurate. Go fix
1811 your program!</computeroutput></para>
1812
1813 <para>After 1000 different errors have been detected, Valgrind
1814 ignores any more. It seems unlikely that collecting even more
1815 different ones would be of practical help to anybody, and it avoids
1816 the danger that Valgrind spends more and more of its time comparing
1817 new errors against an ever-growing collection. As above, the 1000
1818 number is a compile-time constant.</para>
1819 </listitem>
1820
1821 <listitem>
1822 <para><computeroutput>Warning: client switching stacks?</computeroutput></para>
1823
1824 <para>Valgrind spotted such a large change in the stack pointer
1825 that it guesses the client is switching to
1826 a different stack. At this point it makes a kludgey guess where the
1827 base of the new stack is, and sets memory permissions accordingly.
1828 You may get many bogus error messages following this, if Valgrind
1829 guesses wrong. At the moment "large change" is defined as a change
1830 of more that 2000000 in the value of the
1831 stack pointer register.</para>
1832 </listitem>
1833
1834 <listitem>
1835 <para><computeroutput>Warning: client attempted to close Valgrind's
1836 logfile fd &lt;number&gt;</computeroutput></para>
1837
1838 <para>Valgrind doesn't allow the client to close the logfile,
1839 because you'd never see any diagnostic information after that point.
1840 If you see this message, you may want to use the
1841 <option>--log-fd=&lt;number&gt;</option> option to specify a
1842 different logfile file-descriptor number.</para>
1843 </listitem>
1844
1845 <listitem>
1846 <para><computeroutput>Warning: noted but unhandled ioctl
1847 &lt;number&gt;</computeroutput></para>
1848
1849 <para>Valgrind observed a call to one of the vast family of
1850 <computeroutput>ioctl</computeroutput> system calls, but did not
1851 modify its memory status info (because nobody has yet written a
1852 suitable wrapper). The call will still have gone through, but you may get
1853 spurious errors after this as a result of the non-update of the
1854 memory info.</para>
1855 </listitem>
1856
1857 <listitem>
1858 <para><computeroutput>Warning: set address range perms: large range
1859 &lt;number></computeroutput></para>
1860
1861 <para>Diagnostic message, mostly for benefit of the Valgrind
1862 developers, to do with memory permissions.</para>
1863 </listitem>
1864
1865 </itemizedlist>
1866
1867</sect1>
1868
1869
1870
sewardjf5a491c2006-03-13 13:40:57 +00001871
1872
sewardja737e652006-03-19 18:19:11 +00001873
njn3e986b22004-11-30 10:43:45 +00001874</chapter>