Skip to content

Commit a437a60

Browse files
author
njn
committed
Various clean-ups, mostly in chapter 3.
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@10705 a5019735-40e9-0310-863c-91ae7b9d1cf9
1 parent 230c8f0 commit a437a60

File tree

2 files changed

+90
-86
lines changed

2 files changed

+90
-86
lines changed

docs/xml/manual-core-adv.xml

Lines changed: 88 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,27 @@ ways. The subjects covered are:</para>
2525

2626
<para>Valgrind has a trapdoor mechanism via which the client
2727
program can pass all manner of requests and queries to Valgrind
28-
and the current tool. Internally, this is used extensively to
29-
make malloc, free, etc, work, although you don't see that.</para>
28+
and the current tool. Internally, this is used extensively
29+
to make various things work, although that's not visible from the
30+
outside.</para>
3031

3132
<para>For your convenience, a subset of these so-called client
3233
requests is provided to allow you to tell Valgrind facts about
3334
the behaviour of your program, and also to make queries.
34-
In particular, your program can tell Valgrind about changes in
35-
memory range permissions that Valgrind would not otherwise know
36-
about, and so allows clients to get Valgrind to do arbitrary
37-
custom checks.</para>
35+
In particular, your program can tell Valgrind about things that it
36+
otherwise would not know, leading to better results.
37+
</para>
3838

3939
<para>Clients need to include a header file to make this work.
4040
Which header file depends on which client requests you use. Some
4141
client requests are handled by the core, and are defined in the
4242
header file <filename>valgrind/valgrind.h</filename>. Tool-specific
4343
header files are named after the tool, e.g.
44-
<filename>valgrind/memcheck.h</filename>. All header files can be found
45-
in the <literal>include/valgrind</literal> directory of wherever Valgrind
46-
was installed.</para>
44+
<filename>valgrind/memcheck.h</filename>. Each tool-specific header file
45+
includes <filename>valgrind/valgrind.h</filename> so you don't need to
46+
include it in your client if you include a tool-specific header. All header
47+
files can be found in the <literal>include/valgrind</literal> directory of
48+
wherever Valgrind was installed.</para>
4749

4850
<para>The macros in these header files have the magical property
4951
that they generate code in-line which Valgrind can spot.
@@ -58,7 +60,7 @@ and is probably undetectable except in tight loops.
5860
However, if you really wish to compile out the client requests, you can
5961
compile with <option>-DNVALGRIND</option> (analogous to
6062
<option>-DNDEBUG</option>'s effect on
61-
<computeroutput>assert()</computeroutput>).
63+
<function>assert</function>).
6264
</para>
6365

6466
<para>You are encouraged to copy the <filename>valgrind/*.h</filename> headers
@@ -94,7 +96,7 @@ tool-specific macros).</para>
9496
Valgrind to make new translations of that code, which is
9597
probably the semantics you want. Note that code invalidations
9698
are expensive because finding all the relevant translations
97-
quickly is very difficult. So try not to call it often.
99+
quickly is very difficult, so try not to call it often.
98100
Note that you can be clever about
99101
this: you only need to call it when an area which previously
100102
contained code is overwritten with new code. You can choose
@@ -126,9 +128,9 @@ tool-specific macros).</para>
126128
<term><command><computeroutput>VALGRIND_MALLOCLIKE_BLOCK</computeroutput>:</command></term>
127129
<listitem>
128130
<para>If your program manages its own memory instead of using
129-
the standard <computeroutput>malloc()</computeroutput> /
130-
<computeroutput>new</computeroutput> /
131-
<computeroutput>new[]</computeroutput>, tools that track
131+
the standard <function>malloc</function> /
132+
<function>new</function> /
133+
<function>new[]</function>, tools that track
132134
information about heap blocks will not do nearly as good a
133135
job. For example, Memcheck won't detect nearly as many
134136
errors, and the error messages won't be as informative. To
@@ -144,7 +146,7 @@ tool-specific macros).</para>
144146
<listitem>
145147
<para>This should be used in conjunction with
146148
<computeroutput>VALGRIND_MALLOCLIKE_BLOCK</computeroutput>.
147-
Again, see <filename>memcheck/memcheck.h</filename> for
149+
Again, see <filename>valgrind.h</filename> for
148150
information on how to use it.</para>
149151
</listitem>
150152
</varlistentry>
@@ -193,43 +195,42 @@ tool-specific macros).</para>
193195
<varlistentry>
194196
<term><command><computeroutput>VALGRIND_NON_SIMD_CALL[0123]</computeroutput>:</command></term>
195197
<listitem>
196-
<para>Executes a function of 0, 1, 2 or 3 args in the client
197-
program on the <emphasis>real</emphasis> CPU, not the virtual
198-
CPU that Valgrind normally runs code on. These are used in
199-
various ways internally to Valgrind. They might be useful to
200-
client programs.</para>
198+
<para>Executes a function in the client program on the
199+
<emphasis>real</emphasis> CPU, not the virtual CPU that Valgrind
200+
normally runs code on. The function must take an integer (holding a
201+
thread ID) as the first argument and then 0, 1, 2 or 3 more arguments
202+
(depending on which client request is used). These are used in various
203+
ways internally to Valgrind. They might be useful to client
204+
programs.</para>
201205

202206
<para><command>Warning:</command> Only use these if you
203207
<emphasis>really</emphasis> know what you are doing. They aren't
204-
entirely reliable, and can cause Valgrind to crash.
205-
Generally, your prospects of these working are made higher if the called
206-
function does not refer to any global variables, and does not refer to any
207-
libc or other functions (printf et al). Any kind of entanglement with libc
208-
or dynamic linking is likely to have a bad outcome, for tricky reasons
209-
which we've grappled with a lot in the past.
208+
entirely reliable, and can cause Valgrind to crash. See
209+
<filename>valgrind.h</filename> for more details.
210210
</para>
211211
</listitem>
212212
</varlistentry>
213213

214214
<varlistentry>
215215
<term><command><computeroutput>VALGRIND_PRINTF(format, ...)</computeroutput>:</command></term>
216216
<listitem>
217-
<para>printf a message to the log file when running under
218-
Valgrind, prefixed with the PID between a pair of
219-
<computeroutput>**</computeroutput> markers. Nothing is output if not
220-
running under Valgrind. Output is not produced until a newline is
221-
encountered, or subequent Valgrind output is printed; this allows you
222-
to build up a single line of output over multiple calls.
223-
Returns the number of characters output, excluding the PID at the
224-
start.</para>
217+
<para>Print a printf-style message to the Valgrind log file. The
218+
message is prefixed with the PID between a pair of
219+
<computeroutput>**</computeroutput> markers. (Like all client requests,
220+
nothing is output if the client program is not running under Valgrind.)
221+
Output is not produced until a newline is encountered, or subequent
222+
Valgrind output is printed; this allows you to build up a single line of
223+
output over multiple calls. Returns the number of characters output,
224+
excluding the PID prefix.</para>
225225
</listitem>
226226
</varlistentry>
227227

228228
<varlistentry>
229229
<term><command><computeroutput>VALGRIND_PRINTF_BACKTRACE(format, ...)</computeroutput>:</command></term>
230230
<listitem>
231-
<para>Like <computeroutput>VALGRIND_PRINTF</computeroutput>, but prints
232-
a stack backtrace immediately afterwards.</para>
231+
<para>Like <computeroutput>VALGRIND_PRINTF</computeroutput> (in
232+
particular, the return value is identical), but prints a stack backtrace
233+
immediately afterwards.</para>
233234
</listitem>
234235
</varlistentry>
235236

@@ -245,6 +246,9 @@ tool-specific macros).</para>
245246
to a new stack. Use this if you're using a user-level thread package
246247
and are noticing spurious errors from Valgrind about uninitialized
247248
memory reads.</para>
249+
250+
<para><command>Warning:</command> Unfortunately, this client request is
251+
unreliable and best avoided.</para>
248252
</listitem>
249253
</varlistentry>
250254

@@ -254,6 +258,9 @@ tool-specific macros).</para>
254258
<para>Deregisters a previously registered stack. Informs
255259
Valgrind that previously registered memory range with stack id
256260
<computeroutput>id</computeroutput> is no longer a stack.</para>
261+
262+
<para><command>Warning:</command> Unfortunately, this client request is
263+
unreliable and best avoided.</para>
257264
</listitem>
258265
</varlistentry>
259266

@@ -265,16 +272,14 @@ tool-specific macros).</para>
265272
<computeroutput>id</computeroutput> has changed its start and end
266273
values. Use this if your user-level thread package implements
267274
stack growth.</para>
275+
276+
<para><command>Warning:</command> Unfortunately, this client request is
277+
unreliable and best avoided.</para>
268278
</listitem>
269279
</varlistentry>
270280

271281
</variablelist>
272282

273-
<para>Note that <filename>valgrind.h</filename> is included by
274-
all the tool-specific header files (such as
275-
<filename>memcheck.h</filename>), so you don't need to include it
276-
in your client if you include a tool-specific header.</para>
277-
278283
</sect1>
279284

280285

@@ -285,22 +290,21 @@ in your client if you include a tool-specific header.</para>
285290
<title>Function wrapping</title>
286291

287292
<para>
288-
Valgrind versions 3.2.0 and above can do function wrapping on all
289-
supported targets. In function wrapping, calls to some specified
290-
function are intercepted and rerouted to a different, user-supplied
291-
function. This can do whatever it likes, typically examining the
292-
arguments, calling onwards to the original, and possibly examining the
293-
result. Any number of functions may be wrapped.</para>
293+
Valgrind allows calls to some specified functions to be intercepted and
294+
rerouted to a different, user-supplied function. This can do whatever it
295+
likes, typically examining the arguments, calling onwards to the original,
296+
and possibly examining the result. Any number of functions may be
297+
wrapped.</para>
294298

295299
<para>
296300
Function wrapping is useful for instrumenting an API in some way. For
297-
example, wrapping functions in the POSIX pthreads API makes it
298-
possible to notify Valgrind of thread status changes, and wrapping
299-
functions in the MPI (message-passing) API allows notifying Valgrind
301+
example, Helgrind wraps functions in the POSIX pthreads API so it can know
302+
about thread status changes, and the core is able to wrap
303+
functions in the MPI (message-passing) API so it can know
300304
of memory status changes associated with message arrival/departure.
301305
Such information is usually passed to Valgrind by using client
302-
requests in the wrapper functions, although that is not of relevance
303-
here.</para>
306+
requests in the wrapper functions, although the exact mechanism may vary.
307+
</para>
304308

305309
<sect2 id="manual-core-adv.wrapping.example" xreflabel="A Simple Example">
306310
<title>A Simple Example</title>
@@ -313,7 +317,7 @@ int foo ( int x, int y ) { return x + y; }]]></programlisting>
313317
<para>A wrapper is a function of identical type, but with a special name
314318
which identifies it as the wrapper for <computeroutput>foo</computeroutput>.
315319
Wrappers need to include
316-
supporting macros from <computeroutput>valgrind.h</computeroutput>.
320+
supporting macros from <filename>valgrind.h</filename>.
317321
Here is a simple wrapper which prints the arguments and return value:</para>
318322

319323
<programlisting><![CDATA[
@@ -367,8 +371,8 @@ in the same thread.</para>
367371
<para><computeroutput>CALL_FN_W_WW</computeroutput>: eventually we will
368372
want to call the function being
369373
wrapped. Calling it directly does not work, since that just gets us
370-
back to the wrapper and tends to kill the program in short order by
371-
stack overflow. Instead, the result lvalue,
374+
back to the wrapper and leads to an infinite loop. Instead, the result
375+
lvalue,
372376
<computeroutput>OrigFn</computeroutput> and arguments are
373377
handed to one of a family of macros of the form
374378
<computeroutput>CALL_FN_*</computeroutput>. These
@@ -469,27 +473,27 @@ appear and disappear (are dlopen'd and dlclose'd) on the fly.
469473
Valgrind tries to maintain sensible behaviour in such situations.</para>
470474

471475
<para>For example, suppose a process has dlopened (an ELF object with
472-
soname) <computeroutput>object1.so</computeroutput>, which contains
476+
soname) <filename>object1.so</filename>, which contains
473477
<computeroutput>function1</computeroutput>. It starts to use
474478
<computeroutput>function1</computeroutput> immediately.</para>
475479

476-
<para>After a while it dlopens <computeroutput>wrappers.so</computeroutput>,
480+
<para>After a while it dlopens <filename>wrappers.so</filename>,
477481
which contains a wrapper
478482
for <computeroutput>function1</computeroutput> in (soname)
479-
<computeroutput>object1.so</computeroutput>. All subsequent calls to
483+
<filename>object1.so</filename>. All subsequent calls to
480484
<computeroutput>function1</computeroutput> are rerouted to the wrapper.</para>
481485

482-
<para>If <computeroutput>wrappers.so</computeroutput> is
486+
<para>If <filename>wrappers.so</filename> is
483487
later dlclose'd, calls to <computeroutput>function1</computeroutput> are
484488
naturally routed back to the original.</para>
485489

486-
<para>Alternatively, if <computeroutput>object1.so</computeroutput>
487-
is dlclose'd but wrappers.so remains,
488-
then the wrapper exported by <computeroutput>wrapper.so</computeroutput>
490+
<para>Alternatively, if <filename>object1.so</filename>
491+
is dlclose'd but <filename>wrappers.so</filename> remains,
492+
then the wrapper exported by <filename>wrappers.so</filename>
489493
becomes inactive, since there
490494
is no way to get to it - there is no original to call any more. However,
491495
Valgrind remembers that the wrapper is still present. If
492-
<computeroutput>object1.so</computeroutput> is
496+
<filename>object1.so</filename> is
493497
eventually dlopen'd again, the wrapper will become active again.</para>
494498

495499
<para>In short, valgrind inspects all code loading/unloading events to
@@ -511,7 +515,7 @@ can be difficult. The
511515
this possible
512516
by showing the complete state of the redirection subsystem after
513517
every
514-
<computeroutput>mmap</computeroutput>/<computeroutput>munmap</computeroutput>
518+
<function>mmap</function>/<function>munmap</function>
515519
event affecting code (text).</para>
516520

517521
<para>There are two central concepts:</para>
@@ -525,15 +529,15 @@ event affecting code (text).</para>
525529
<computeroutput>I_WRAP_SONAME_FNNAME_{ZZ,_ZU}</computeroutput>
526530
macros.</para></listitem>
527531

528-
<listitem><para>An "active redirection" is code-address to
532+
<listitem><para>An "active redirection" is a code-address to
529533
code-address binding currently in effect.</para></listitem>
530534

531535
</itemizedlist>
532536

533537
<para>The state of the wrapping-and-redirection subsystem comprises a set of
534538
specifications and a set of active bindings. The specifications are
535539
acquired/discarded by watching all
536-
<computeroutput>mmap</computeroutput>/<computeroutput>munmap</computeroutput>
540+
<function>mmap</function>/<function>munmap</function>
537541
events on code (text)
538542
sections. The active binding set is (conceptually) recomputed from
539543
the specifications, and all known symbol names, following any change
@@ -551,7 +555,7 @@ flags.</para>
551555
<para>One final comment. The function-wrapping facility is closely
552556
tied to Valgrind's ability to replace (redirect) specified
553557
functions, for example to redirect calls to
554-
<computeroutput>malloc</computeroutput> to its
558+
<function>malloc</function> to its
555559
own implementation. Indeed, a replacement function can be
556560
regarded as a wrapper function which does not call the original.
557561
However, to make the implementation more robust, the two kinds
@@ -581,8 +585,8 @@ other wrapped function. Once you have the
581585
calls between, recursion between, and longjumps out of wrappers
582586
should work correctly. There is never any interaction between wrapped
583587
functions and merely replaced functions
584-
(eg <computeroutput>malloc</computeroutput>), so you can call
585-
<computeroutput>malloc</computeroutput> etc safely from within wrappers.
588+
(eg <function>malloc</function>), so you can call
589+
<function>malloc</function> etc safely from within wrappers.
586590
</para>
587591

588592
<para>The above comments are true for {x86,amd64,ppc32}-linux. On
@@ -619,36 +623,36 @@ and 'v' denotes C's <computeroutput>void</computeroutput> type.
619623
The currently available macros are:</para>
620624

621625
<programlisting><![CDATA[
622-
CALL_FN_v_v -- call an original of type void fn ( void )
623-
CALL_FN_W_v -- call an original of type long fn ( void )
626+
CALL_FN_v_v -- call an original of type void fn ( void )
627+
CALL_FN_W_v -- call an original of type long fn ( void )
624628
625-
CALL_FN_v_W -- void fn ( long )
626-
CALL_FN_W_W -- long fn ( long )
629+
CALL_FN_v_W -- call an original of type void fn ( long )
630+
CALL_FN_W_W -- call an original of type long fn ( long )
627631
628-
CALL_FN_v_WW -- void fn ( long, long )
629-
CALL_FN_W_WW -- long fn ( long, long )
632+
CALL_FN_v_WW -- call an original of type void fn ( long, long )
633+
CALL_FN_W_WW -- call an original of type long fn ( long, long )
630634
631-
CALL_FN_v_WWW -- void fn ( long, long, long )
632-
CALL_FN_W_WWW -- long fn ( long, long, long )
635+
CALL_FN_v_WWW -- call an original of type void fn ( long, long, long )
636+
CALL_FN_W_WWW -- call an original of type long fn ( long, long, long )
633637
634-
CALL_FN_W_WWWW -- long fn ( long, long, long, long )
635-
CALL_FN_W_5W -- long fn ( long, long, long, long, long )
636-
CALL_FN_W_6W -- long fn ( long, long, long, long, long, long )
638+
CALL_FN_W_WWWW -- call an original of type long fn ( long, long, long, long )
639+
CALL_FN_W_5W -- call an original of type long fn ( long, long, long, long, long )
640+
CALL_FN_W_6W -- call an original of type long fn ( long, long, long, long, long, long )
637641
and so on, up to
638642
CALL_FN_W_12W
639643
]]></programlisting>
640644

641645
<para>The set of supported types can be expanded as needed. It is
642646
regrettable that this limitation exists. Function wrapping has proven
643647
difficult to implement, with a certain apparently unavoidable level of
644-
ickyness. After several implementation attempts, the present
648+
ickiness. After several implementation attempts, the present
645649
arrangement appears to be the least-worst tradeoff. At least it works
646650
reliably in the presence of dynamic linking and dynamic code
647651
loading/unloading.</para>
648652

649653
<para>You should not attempt to wrap a function of one type signature with a
650654
wrapper of a different type signature. Such trickery will surely lead
651-
to crashes or strange behaviour. This is not of course a limitation
655+
to crashes or strange behaviour. This is not a limitation
652656
of the function wrapping implementation, merely a reflection of the
653657
fact that it gives you sweeping powers to shoot yourself in the foot
654658
if you are not careful. Imagine the instant havoc you could wreak by
@@ -661,10 +665,10 @@ process.</para>
661665
<title>Examples</title>
662666

663667
<para>In the source tree,
664-
<computeroutput>memcheck/tests/wrap[1-8].c</computeroutput> provide a series of
668+
<filename>memcheck/tests/wrap[1-8].c</filename> provide a series of
665669
examples, ranging from very simple to quite advanced.</para>
666670

667-
<para><computeroutput>auxprogs/libmpiwrap.c</computeroutput> is an example
671+
<para><filename>mpi/libmpiwrap.c</filename> is an example
668672
of wrapping a big, complex API (the MPI-2 interface). This file defines
669673
almost 300 different wrappers.</para>
670674
</sect2>

memcheck/docs/mc-manual.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,7 @@ buffer which is too small.</para>
15621562

15631563
<para>Unlike most of the rest of Valgrind, the wrapper library is subject to a
15641564
BSD-style license, so you can link it into any code base you like.
1565-
See the top of <computeroutput>auxprogs/libmpiwrap.c</computeroutput>
1565+
See the top of <computeroutput>mpi/libmpiwrap.c</computeroutput>
15661566
for license details.</para>
15671567

15681568

@@ -1866,7 +1866,7 @@ to make the library thread-safe.</para>
18661866
checks.</para>
18671867

18681868
<para>Writing new wrappers should be fairly easy. The source file is
1869-
<computeroutput>auxprogs/libmpiwrap.c</computeroutput>. If possible,
1869+
<computeroutput>mpi/libmpiwrap.c</computeroutput>. If possible,
18701870
find an existing wrapper for a function of similar behaviour to the
18711871
one you want to wrap, and use it as a starting point. The wrappers
18721872
are organised in sections in the same order as the MPI 1.1 spec, to

0 commit comments

Comments
 (0)