@@ -25,25 +25,27 @@ ways. The subjects covered are:</para>
25
25
26
26
<para >Valgrind has a trapdoor mechanism via which the client
27
27
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 >
30
31
31
32
<para >For your convenience, a subset of these so-called client
32
33
requests is provided to allow you to tell Valgrind facts about
33
34
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 >
38
38
39
39
<para >Clients need to include a header file to make this work.
40
40
Which header file depends on which client requests you use. Some
41
41
client requests are handled by the core, and are defined in the
42
42
header file <filename >valgrind/valgrind.h</filename >. Tool-specific
43
43
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 >
47
49
48
50
<para >The macros in these header files have the magical property
49
51
that they generate code in-line which Valgrind can spot.
@@ -58,7 +60,7 @@ and is probably undetectable except in tight loops.
58
60
However, if you really wish to compile out the client requests, you can
59
61
compile with <option >-DNVALGRIND</option > (analogous to
60
62
<option >-DNDEBUG</option >'s effect on
61
- <computeroutput >assert()</ computeroutput >).
63
+ <function >assert</ function >).
62
64
</para >
63
65
64
66
<para >You are encouraged to copy the <filename >valgrind/*.h</filename > headers
@@ -94,7 +96,7 @@ tool-specific macros).</para>
94
96
Valgrind to make new translations of that code, which is
95
97
probably the semantics you want. Note that code invalidations
96
98
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.
98
100
Note that you can be clever about
99
101
this: you only need to call it when an area which previously
100
102
contained code is overwritten with new code. You can choose
@@ -126,9 +128,9 @@ tool-specific macros).</para>
126
128
<term ><command ><computeroutput >VALGRIND_MALLOCLIKE_BLOCK</computeroutput >:</command ></term >
127
129
<listitem >
128
130
<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
132
134
information about heap blocks will not do nearly as good a
133
135
job. For example, Memcheck won't detect nearly as many
134
136
errors, and the error messages won't be as informative. To
@@ -144,7 +146,7 @@ tool-specific macros).</para>
144
146
<listitem >
145
147
<para >This should be used in conjunction with
146
148
<computeroutput >VALGRIND_MALLOCLIKE_BLOCK</computeroutput >.
147
- Again, see <filename >memcheck/memcheck .h</filename > for
149
+ Again, see <filename >valgrind .h</filename > for
148
150
information on how to use it.</para >
149
151
</listitem >
150
152
</varlistentry >
@@ -193,43 +195,42 @@ tool-specific macros).</para>
193
195
<varlistentry >
194
196
<term ><command ><computeroutput >VALGRIND_NON_SIMD_CALL[0123]</computeroutput >:</command ></term >
195
197
<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 >
201
205
202
206
<para ><command >Warning:</command > Only use these if you
203
207
<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.
210
210
</para >
211
211
</listitem >
212
212
</varlistentry >
213
213
214
214
<varlistentry >
215
215
<term ><command ><computeroutput >VALGRIND_PRINTF(format, ...)</computeroutput >:</command ></term >
216
216
<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 >
225
225
</listitem >
226
226
</varlistentry >
227
227
228
228
<varlistentry >
229
229
<term ><command ><computeroutput >VALGRIND_PRINTF_BACKTRACE(format, ...)</computeroutput >:</command ></term >
230
230
<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 >
233
234
</listitem >
234
235
</varlistentry >
235
236
@@ -245,6 +246,9 @@ tool-specific macros).</para>
245
246
to a new stack. Use this if you're using a user-level thread package
246
247
and are noticing spurious errors from Valgrind about uninitialized
247
248
memory reads.</para >
249
+
250
+ <para ><command >Warning:</command > Unfortunately, this client request is
251
+ unreliable and best avoided.</para >
248
252
</listitem >
249
253
</varlistentry >
250
254
@@ -254,6 +258,9 @@ tool-specific macros).</para>
254
258
<para >Deregisters a previously registered stack. Informs
255
259
Valgrind that previously registered memory range with stack id
256
260
<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 >
257
264
</listitem >
258
265
</varlistentry >
259
266
@@ -265,16 +272,14 @@ tool-specific macros).</para>
265
272
<computeroutput >id</computeroutput > has changed its start and end
266
273
values. Use this if your user-level thread package implements
267
274
stack growth.</para >
275
+
276
+ <para ><command >Warning:</command > Unfortunately, this client request is
277
+ unreliable and best avoided.</para >
268
278
</listitem >
269
279
</varlistentry >
270
280
271
281
</variablelist >
272
282
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
-
278
283
</sect1 >
279
284
280
285
@@ -285,22 +290,21 @@ in your client if you include a tool-specific header.</para>
285
290
<title >Function wrapping</title >
286
291
287
292
<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 >
294
298
295
299
<para >
296
300
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
300
304
of memory status changes associated with message arrival/departure.
301
305
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 >
304
308
305
309
<sect2 id =" manual-core-adv.wrapping.example" xreflabel =" A Simple Example" >
306
310
<title >A Simple Example</title >
@@ -313,7 +317,7 @@ int foo ( int x, int y ) { return x + y; }]]></programlisting>
313
317
<para >A wrapper is a function of identical type, but with a special name
314
318
which identifies it as the wrapper for <computeroutput >foo</computeroutput >.
315
319
Wrappers need to include
316
- supporting macros from <computeroutput >valgrind.h</computeroutput >.
320
+ supporting macros from <filename >valgrind.h</filename >.
317
321
Here is a simple wrapper which prints the arguments and return value:</para >
318
322
319
323
<programlisting ><![CDATA[
@@ -367,8 +371,8 @@ in the same thread.</para>
367
371
<para ><computeroutput >CALL_FN_W_WW</computeroutput >: eventually we will
368
372
want to call the function being
369
373
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,
372
376
<computeroutput >OrigFn</computeroutput > and arguments are
373
377
handed to one of a family of macros of the form
374
378
<computeroutput >CALL_FN_*</computeroutput >. These
@@ -469,27 +473,27 @@ appear and disappear (are dlopen'd and dlclose'd) on the fly.
469
473
Valgrind tries to maintain sensible behaviour in such situations.</para >
470
474
471
475
<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
473
477
<computeroutput >function1</computeroutput >. It starts to use
474
478
<computeroutput >function1</computeroutput > immediately.</para >
475
479
476
- <para >After a while it dlopens <computeroutput >wrappers.so</computeroutput >,
480
+ <para >After a while it dlopens <filename >wrappers.so</filename >,
477
481
which contains a wrapper
478
482
for <computeroutput >function1</computeroutput > in (soname)
479
- <computeroutput >object1.so</computeroutput >. All subsequent calls to
483
+ <filename >object1.so</filename >. All subsequent calls to
480
484
<computeroutput >function1</computeroutput > are rerouted to the wrapper.</para >
481
485
482
- <para >If <computeroutput >wrappers.so</computeroutput > is
486
+ <para >If <filename >wrappers.so</filename > is
483
487
later dlclose'd, calls to <computeroutput >function1</computeroutput > are
484
488
naturally routed back to the original.</para >
485
489
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 >
489
493
becomes inactive, since there
490
494
is no way to get to it - there is no original to call any more. However,
491
495
Valgrind remembers that the wrapper is still present. If
492
- <computeroutput >object1.so</computeroutput > is
496
+ <filename >object1.so</filename > is
493
497
eventually dlopen'd again, the wrapper will become active again.</para >
494
498
495
499
<para >In short, valgrind inspects all code loading/unloading events to
@@ -511,7 +515,7 @@ can be difficult. The
511
515
this possible
512
516
by showing the complete state of the redirection subsystem after
513
517
every
514
- <computeroutput >mmap</computeroutput >/<computeroutput >munmap</computeroutput >
518
+ <function >mmap</function >/<function >munmap</function >
515
519
event affecting code (text).</para >
516
520
517
521
<para >There are two central concepts:</para >
@@ -525,15 +529,15 @@ event affecting code (text).</para>
525
529
<computeroutput >I_WRAP_SONAME_FNNAME_{ZZ,_ZU}</computeroutput >
526
530
macros.</para ></listitem >
527
531
528
- <listitem ><para >An "active redirection" is code-address to
532
+ <listitem ><para >An "active redirection" is a code-address to
529
533
code-address binding currently in effect.</para ></listitem >
530
534
531
535
</itemizedlist >
532
536
533
537
<para >The state of the wrapping-and-redirection subsystem comprises a set of
534
538
specifications and a set of active bindings. The specifications are
535
539
acquired/discarded by watching all
536
- <computeroutput >mmap</computeroutput >/<computeroutput >munmap</computeroutput >
540
+ <function >mmap</function >/<function >munmap</function >
537
541
events on code (text)
538
542
sections. The active binding set is (conceptually) recomputed from
539
543
the specifications, and all known symbol names, following any change
@@ -551,7 +555,7 @@ flags.</para>
551
555
<para >One final comment. The function-wrapping facility is closely
552
556
tied to Valgrind's ability to replace (redirect) specified
553
557
functions, for example to redirect calls to
554
- <computeroutput >malloc</computeroutput > to its
558
+ <function >malloc</function > to its
555
559
own implementation. Indeed, a replacement function can be
556
560
regarded as a wrapper function which does not call the original.
557
561
However, to make the implementation more robust, the two kinds
@@ -581,8 +585,8 @@ other wrapped function. Once you have the
581
585
calls between, recursion between, and longjumps out of wrappers
582
586
should work correctly. There is never any interaction between wrapped
583
587
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.
586
590
</para >
587
591
588
592
<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.
619
623
The currently available macros are:</para >
620
624
621
625
<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 )
624
628
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 )
627
631
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 )
630
634
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 )
633
637
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 )
637
641
and so on, up to
638
642
CALL_FN_W_12W
639
643
]]> </programlisting >
640
644
641
645
<para >The set of supported types can be expanded as needed. It is
642
646
regrettable that this limitation exists. Function wrapping has proven
643
647
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
645
649
arrangement appears to be the least-worst tradeoff. At least it works
646
650
reliably in the presence of dynamic linking and dynamic code
647
651
loading/unloading.</para >
648
652
649
653
<para >You should not attempt to wrap a function of one type signature with a
650
654
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
652
656
of the function wrapping implementation, merely a reflection of the
653
657
fact that it gives you sweeping powers to shoot yourself in the foot
654
658
if you are not careful. Imagine the instant havoc you could wreak by
@@ -661,10 +665,10 @@ process.</para>
661
665
<title >Examples</title >
662
666
663
667
<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
665
669
examples, ranging from very simple to quite advanced.</para >
666
670
667
- <para ><computeroutput >auxprogs /libmpiwrap.c</computeroutput > is an example
671
+ <para ><filename >mpi /libmpiwrap.c</filename > is an example
668
672
of wrapping a big, complex API (the MPI-2 interface). This file defines
669
673
almost 300 different wrappers.</para >
670
674
</sect2 >
0 commit comments