• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4<title>3.�Using and understanding the Valgrind core: Advanced Topics</title>
5<link rel="stylesheet" href="vg_basic.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
7<link rel="home" href="index.html" title="Valgrind Documentation">
8<link rel="up" href="manual.html" title="Valgrind User Manual">
9<link rel="prev" href="manual-core.html" title="2.�Using and understanding the Valgrind core">
10<link rel="next" href="mc-manual.html" title="4.�Memcheck: a memory error detector">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<div><table class="nav" width="100%" cellspacing="3" cellpadding="3" border="0" summary="Navigation header"><tr>
14<td width="22px" align="center" valign="middle"><a accesskey="p" href="manual-core.html"><img src="images/prev.png" width="18" height="21" border="0" alt="Prev"></a></td>
15<td width="25px" align="center" valign="middle"><a accesskey="u" href="manual.html"><img src="images/up.png" width="21" height="18" border="0" alt="Up"></a></td>
16<td width="31px" align="center" valign="middle"><a accesskey="h" href="index.html"><img src="images/home.png" width="27" height="20" border="0" alt="Up"></a></td>
17<th align="center" valign="middle">Valgrind User Manual</th>
18<td width="22px" align="center" valign="middle"><a accesskey="n" href="mc-manual.html"><img src="images/next.png" width="18" height="21" border="0" alt="Next"></a></td>
19</tr></table></div>
20<div class="chapter" title="3.�Using and understanding the Valgrind core: Advanced Topics">
21<div class="titlepage"><div><div><h2 class="title">
22<a name="manual-core-adv"></a>3.�Using and understanding the Valgrind core: Advanced Topics</h2></div></div></div>
23<div class="toc">
24<p><b>Table of Contents</b></p>
25<dl>
26<dt><span class="sect1"><a href="manual-core-adv.html#manual-core-adv.clientreq">3.1. The Client Request mechanism</a></span></dt>
27<dt><span class="sect1"><a href="manual-core-adv.html#manual-core-adv.wrapping">3.2. Function wrapping</a></span></dt>
28<dd><dl>
29<dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.wrapping.example">3.2.1. A Simple Example</a></span></dt>
30<dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.wrapping.specs">3.2.2. Wrapping Specifications</a></span></dt>
31<dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.wrapping.semantics">3.2.3. Wrapping Semantics</a></span></dt>
32<dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.wrapping.debugging">3.2.4. Debugging</a></span></dt>
33<dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.wrapping.limitations-cf">3.2.5. Limitations - control flow</a></span></dt>
34<dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.wrapping.limitations-sigs">3.2.6. Limitations - original function signatures</a></span></dt>
35<dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.wrapping.examples">3.2.7. Examples</a></span></dt>
36</dl></dd>
37</dl>
38</div>
39<p>This chapter describes advanced aspects of the Valgrind core
40services, which are mostly of interest to power users who wish to
41customise and modify Valgrind's default behaviours in certain useful
42ways.  The subjects covered are:</p>
43<div class="itemizedlist"><ul class="itemizedlist" type="disc">
44<li class="listitem"><p>The "Client Request" mechanism</p></li>
45<li class="listitem"><p>Function Wrapping</p></li>
46</ul></div>
47<div class="sect1" title="3.1.�The Client Request mechanism">
48<div class="titlepage"><div><div><h2 class="title" style="clear: both">
49<a name="manual-core-adv.clientreq"></a>3.1.�The Client Request mechanism</h2></div></div></div>
50<p>Valgrind has a trapdoor mechanism via which the client
51program can pass all manner of requests and queries to Valgrind
52and the current tool.  Internally, this is used extensively
53to make various things work, although that's not visible from the
54outside.</p>
55<p>For your convenience, a subset of these so-called client
56requests is provided to allow you to tell Valgrind facts about
57the behaviour of your program, and also to make queries.
58In particular, your program can tell Valgrind about things that it
59otherwise would not know, leading to better results.
60</p>
61<p>Clients need to include a header file to make this work.
62Which header file depends on which client requests you use.  Some
63client requests are handled by the core, and are defined in the
64header file <code class="filename">valgrind/valgrind.h</code>.  Tool-specific
65header files are named after the tool, e.g.
66<code class="filename">valgrind/memcheck.h</code>.  Each tool-specific header file
67includes <code class="filename">valgrind/valgrind.h</code> so you don't need to
68include it in your client if you include a tool-specific header.  All header
69files can be found in the <code class="literal">include/valgrind</code> directory of
70wherever Valgrind was installed.</p>
71<p>The macros in these header files have the magical property
72that they generate code in-line which Valgrind can spot.
73However, the code does nothing when not run on Valgrind, so you
74are not forced to run your program under Valgrind just because you
75use the macros in this file.  Also, you are not required to link your
76program with any extra supporting libraries.</p>
77<p>The code added to your binary has negligible performance impact:
78on x86, amd64, ppc32, ppc64 and ARM, the overhead is 6 simple integer
79instructions and is probably undetectable except in tight loops.
80However, if you really wish to compile out the client requests, you
81can compile with <code class="option">-DNVALGRIND</code> (analogous to
82<code class="option">-DNDEBUG</code>'s effect on
83<code class="function">assert</code>).
84</p>
85<p>You are encouraged to copy the <code class="filename">valgrind/*.h</code> headers
86into your project's include directory, so your program doesn't have a
87compile-time dependency on Valgrind being installed.  The Valgrind headers,
88unlike most of the rest of the code, are under a BSD-style license so you may
89include them without worrying about license incompatibility.</p>
90<p>Here is a brief description of the macros available in
91<code class="filename">valgrind.h</code>, which work with more than one
92tool (see the tool-specific documentation for explanations of the
93tool-specific macros).</p>
94<div class="variablelist"><dl>
95<dt><span class="term"><span class="command"><strong><code class="computeroutput">RUNNING_ON_VALGRIND</code></strong></span>:</span></dt>
96<dd><p>Returns 1 if running on Valgrind, 0 if running on the
97    real CPU.  If you are running Valgrind on itself, returns the
98    number of layers of Valgrind emulation you're running on.
99    </p></dd>
100<dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_DISCARD_TRANSLATIONS</code>:</strong></span></span></dt>
101<dd>
102<p>Discards translations of code in the specified address
103    range.  Useful if you are debugging a JIT compiler or some other
104    dynamic code generation system.  After this call, attempts to
105    execute code in the invalidated address range will cause
106    Valgrind to make new translations of that code, which is
107    probably the semantics you want.  Note that code invalidations
108    are expensive because finding all the relevant translations
109    quickly is very difficult, so try not to call it often.
110    Note that you can be clever about
111    this: you only need to call it when an area which previously
112    contained code is overwritten with new code.  You can choose
113    to write code into fresh memory, and just call this
114    occasionally to discard large chunks of old code all at
115    once.</p>
116<p>
117    Alternatively, for transparent self-modifying-code support,
118    use<code class="option">--smc-check=all</code>, or run
119    on ppc32/Linux, ppc64/Linux or ARM/Linux.
120    </p>
121</dd>
122<dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_COUNT_ERRORS</code>:</strong></span></span></dt>
123<dd><p>Returns the number of errors found so far by Valgrind.  Can be
124    useful in test harness code when combined with the
125    <code class="option">--log-fd=-1</code> option; this runs Valgrind silently,
126    but the client program can detect when errors occur.  Only useful
127    for tools that report errors, e.g. it's useful for Memcheck, but for
128    Cachegrind it will always return zero because Cachegrind doesn't
129    report errors.</p></dd>
130<dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_MALLOCLIKE_BLOCK</code>:</strong></span></span></dt>
131<dd><p>If your program manages its own memory instead of using
132    the standard <code class="function">malloc</code> /
133    <code class="function">new</code> /
134    <code class="function">new[]</code>, tools that track
135    information about heap blocks will not do nearly as good a
136    job.  For example, Memcheck won't detect nearly as many
137    errors, and the error messages won't be as informative.  To
138    improve this situation, use this macro just after your custom
139    allocator allocates some new memory.  See the comments in
140    <code class="filename">valgrind.h</code> for information on how to use
141    it.</p></dd>
142<dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_FREELIKE_BLOCK</code>:</strong></span></span></dt>
143<dd><p>This should be used in conjunction with
144    <code class="computeroutput">VALGRIND_MALLOCLIKE_BLOCK</code>.
145    Again, see <code class="filename">valgrind.h</code> for
146    information on how to use it.</p></dd>
147<dt><span class="term">
148   <span class="command"><strong><code class="computeroutput">VALGRIND_CREATE_MEMPOOL</code></strong></span>,
149   <span class="command"><strong><code class="computeroutput">VALGRIND_DESTROY_MEMPOOL</code></strong></span>,
150   <span class="command"><strong><code class="computeroutput">VALGRIND_MEMPOOL_ALLOC</code></strong></span>,
151   <span class="command"><strong><code class="computeroutput">VALGRIND_MEMPOOL_FREE</code></strong></span>,
152   <span class="command"><strong><code class="computeroutput">VALGRIND_MOVE_MEMPOOL</code></strong></span>,
153   <span class="command"><strong><code class="computeroutput">VALGRIND_MEMPOOL_CHANGE</code></strong></span>,
154   <span class="command"><strong><code class="computeroutput">VALGRIND_MEMPOOL_EXISTS</code></strong></span>:
155   </span></dt>
156<dd><p>These are similar to
157    <code class="computeroutput">VALGRIND_MALLOCLIKE_BLOCK</code> and
158    <code class="computeroutput">VALGRIND_FREELIKE_BLOCK</code>
159    but are tailored towards code that uses memory pools.  See
160    <a class="xref" href="mc-manual.html#mc-manual.mempools" title="4.7.�Memory Pools: describing and working with custom allocators">Memory Pools</a> for a detailed description.</p></dd>
161<dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_NON_SIMD_CALL[0123]</code>:</strong></span></span></dt>
162<dd>
163<p>Executes a function in the client program on the
164    <span class="emphasis"><em>real</em></span> CPU, not the virtual CPU that Valgrind
165    normally runs code on.  The function must take an integer (holding a
166    thread ID) as the first argument and then 0, 1, 2 or 3 more arguments
167    (depending on which client request is used).  These are used in various
168    ways internally to Valgrind.  They might be useful to client
169    programs.</p>
170<p><span class="command"><strong>Warning:</strong></span> Only use these if you
171    <span class="emphasis"><em>really</em></span> know what you are doing.  They aren't
172    entirely reliable, and can cause Valgrind to crash.  See
173    <code class="filename">valgrind.h</code> for more details.
174    </p>
175</dd>
176<dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_PRINTF(format, ...)</code>:</strong></span></span></dt>
177<dd><p>Print a printf-style message to the Valgrind log file.  The
178    message is prefixed with the PID between a pair of
179    <code class="computeroutput">**</code> markers.  (Like all client requests,
180    nothing is output if the client program is not running under Valgrind.)
181    Output is not produced until a newline is encountered, or subsequent
182    Valgrind output is printed; this allows you to build up a single line of
183    output over multiple calls.  Returns the number of characters output,
184    excluding the PID prefix.</p></dd>
185<dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_PRINTF_BACKTRACE(format, ...)</code>:</strong></span></span></dt>
186<dd><p>Like <code class="computeroutput">VALGRIND_PRINTF</code> (in
187    particular, the return value is identical), but prints a stack backtrace
188    immediately afterwards.</p></dd>
189<dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_STACK_REGISTER(start, end)</code>:</strong></span></span></dt>
190<dd>
191<p>Registers a new stack.  Informs Valgrind that the memory range
192    between start and end is a unique stack.  Returns a stack identifier
193    that can be used with other
194    <code class="computeroutput">VALGRIND_STACK_*</code> calls.</p>
195<p>Valgrind will use this information to determine if a change to
196    the stack pointer is an item pushed onto the stack or a change over
197    to a new stack.  Use this if you're using a user-level thread package
198    and are noticing spurious errors from Valgrind about uninitialized
199    memory reads.</p>
200<p><span class="command"><strong>Warning:</strong></span> Unfortunately, this client request is
201    unreliable and best avoided.</p>
202</dd>
203<dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_STACK_DEREGISTER(id)</code>:</strong></span></span></dt>
204<dd>
205<p>Deregisters a previously registered stack.  Informs
206    Valgrind that previously registered memory range with stack id
207    <code class="computeroutput">id</code> is no longer a stack.</p>
208<p><span class="command"><strong>Warning:</strong></span> Unfortunately, this client request is
209    unreliable and best avoided.</p>
210</dd>
211<dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_STACK_CHANGE(id, start, end)</code>:</strong></span></span></dt>
212<dd>
213<p>Changes a previously registered stack.  Informs
214    Valgrind that the previously registered stack with stack id
215    <code class="computeroutput">id</code> has changed its start and end
216    values.  Use this if your user-level thread package implements
217    stack growth.</p>
218<p><span class="command"><strong>Warning:</strong></span> Unfortunately, this client request is
219    unreliable and best avoided.</p>
220</dd>
221</dl></div>
222</div>
223<div class="sect1" title="3.2.�Function wrapping">
224<div class="titlepage"><div><div><h2 class="title" style="clear: both">
225<a name="manual-core-adv.wrapping"></a>3.2.�Function wrapping</h2></div></div></div>
226<p>
227Valgrind allows calls to some specified functions to be intercepted and
228rerouted to a different, user-supplied function.  This can do whatever it
229likes, typically examining the arguments, calling onwards to the original,
230and possibly examining the result.  Any number of functions may be
231wrapped.</p>
232<p>
233Function wrapping is useful for instrumenting an API in some way.  For
234example, Helgrind wraps functions in the POSIX pthreads API so it can know
235about thread status changes, and the core is able to wrap
236functions in the MPI (message-passing) API so it can know
237of memory status changes associated with message arrival/departure.
238Such information is usually passed to Valgrind by using client
239requests in the wrapper functions, although the exact mechanism may vary.
240</p>
241<div class="sect2" title="3.2.1.�A Simple Example">
242<div class="titlepage"><div><div><h3 class="title">
243<a name="manual-core-adv.wrapping.example"></a>3.2.1.�A Simple Example</h3></div></div></div>
244<p>Supposing we want to wrap some function</p>
245<pre class="programlisting">
246int foo ( int x, int y ) { return x + y; }</pre>
247<p>A wrapper is a function of identical type, but with a special name
248which identifies it as the wrapper for <code class="computeroutput">foo</code>.
249Wrappers need to include
250supporting macros from <code class="filename">valgrind.h</code>.
251Here is a simple wrapper which prints the arguments and return value:</p>
252<pre class="programlisting">
253#include &lt;stdio.h&gt;
254#include "valgrind.h"
255int I_WRAP_SONAME_FNNAME_ZU(NONE,foo)( int x, int y )
256{
257   int    result;
258   OrigFn fn;
259   VALGRIND_GET_ORIG_FN(fn);
260   printf("foo's wrapper: args %d %d\n", x, y);
261   CALL_FN_W_WW(result, fn, x,y);
262   printf("foo's wrapper: result %d\n", result);
263   return result;
264}
265</pre>
266<p>To become active, the wrapper merely needs to be present in a text
267section somewhere in the same process' address space as the function
268it wraps, and for its ELF symbol name to be visible to Valgrind.  In
269practice, this means either compiling to a
270<code class="computeroutput">.o</code> and linking it in, or
271compiling to a <code class="computeroutput">.so</code> and
272<code class="computeroutput">LD_PRELOAD</code>ing it in.  The latter is more
273convenient in that it doesn't require relinking.</p>
274<p>All wrappers have approximately the above form.  There are three
275crucial macros:</p>
276<p><code class="computeroutput">I_WRAP_SONAME_FNNAME_ZU</code>:
277this generates the real name of the wrapper.
278This is an encoded name which Valgrind notices when reading symbol
279table information.  What it says is: I am the wrapper for any function
280named <code class="computeroutput">foo</code> which is found in
281an ELF shared object with an empty
282("<code class="computeroutput">NONE</code>") soname field.  The specification
283mechanism is powerful in
284that wildcards are allowed for both sonames and function names.
285The details are discussed below.</p>
286<p><code class="computeroutput">VALGRIND_GET_ORIG_FN</code>:
287once in the the wrapper, the first priority is
288to get hold of the address of the original (and any other supporting
289information needed).  This is stored in a value of opaque
290type <code class="computeroutput">OrigFn</code>.
291The information is acquired using
292<code class="computeroutput">VALGRIND_GET_ORIG_FN</code>.  It is crucial
293to make this macro call before calling any other wrapped function
294in the same thread.</p>
295<p><code class="computeroutput">CALL_FN_W_WW</code>: eventually we will
296want to call the function being
297wrapped.  Calling it directly does not work, since that just gets us
298back to the wrapper and leads to an infinite loop.  Instead, the result
299lvalue,
300<code class="computeroutput">OrigFn</code> and arguments are
301handed to one of a family of macros of the form
302<code class="computeroutput">CALL_FN_*</code>.  These
303cause Valgrind to call the original and avoid recursion back to the
304wrapper.</p>
305</div>
306<div class="sect2" title="3.2.2.�Wrapping Specifications">
307<div class="titlepage"><div><div><h3 class="title">
308<a name="manual-core-adv.wrapping.specs"></a>3.2.2.�Wrapping Specifications</h3></div></div></div>
309<p>This scheme has the advantage of being self-contained.  A library of
310wrappers can be compiled to object code in the normal way, and does
311not rely on an external script telling Valgrind which wrappers pertain
312to which originals.</p>
313<p>Each wrapper has a name which, in the most general case says: I am the
314wrapper for any function whose name matches FNPATT and whose ELF
315"soname" matches SOPATT.  Both FNPATT and SOPATT may contain wildcards
316(asterisks) and other characters (spaces, dots, @, etc) which are not
317generally regarded as valid C identifier names.</p>
318<p>This flexibility is needed to write robust wrappers for POSIX pthread
319functions, where typically we are not completely sure of either the
320function name or the soname, or alternatively we want to wrap a whole
321set of functions at once.</p>
322<p>For example, <code class="computeroutput">pthread_create</code>
323in GNU libpthread is usually a
324versioned symbol - one whose name ends in, eg,
325<code class="computeroutput">@GLIBC_2.3</code>.  Hence we
326are not sure what its real name is.  We also want to cover any soname
327of the form <code class="computeroutput">libpthread.so*</code>.
328So the header of the wrapper will be</p>
329<pre class="programlisting">
330int I_WRAP_SONAME_FNNAME_ZZ(libpthreadZdsoZd0,pthreadZucreateZAZa)
331  ( ... formals ... )
332  { ... body ... }
333</pre>
334<p>In order to write unusual characters as valid C function names, a
335Z-encoding scheme is used.  Names are written literally, except that
336a capital Z acts as an escape character, with the following encoding:</p>
337<pre class="programlisting">
338     Za   encodes    *
339     Zp              +
340     Zc              :
341     Zd              .
342     Zu              _
343     Zh              -
344     Zs              (space)
345     ZA              @
346     ZZ              Z
347     ZL              (       # only in valgrind 3.3.0 and later
348     ZR              )       # only in valgrind 3.3.0 and later
349</pre>
350<p>Hence <code class="computeroutput">libpthreadZdsoZd0</code> is an
351encoding of the soname <code class="computeroutput">libpthread.so.0</code>
352and <code class="computeroutput">pthreadZucreateZAZa</code> is an encoding
353of the function name <code class="computeroutput">pthread_create@*</code>.
354</p>
355<p>The macro <code class="computeroutput">I_WRAP_SONAME_FNNAME_ZZ</code>
356constructs a wrapper name in which
357both the soname (first component) and function name (second component)
358are Z-encoded.  Encoding the function name can be tiresome and is
359often unnecessary, so a second macro,
360<code class="computeroutput">I_WRAP_SONAME_FNNAME_ZU</code>, can be
361used instead.  The <code class="computeroutput">_ZU</code> variant is
362also useful for writing wrappers for
363C++ functions, in which the function name is usually already mangled
364using some other convention in which Z plays an important role.  Having
365to encode a second time quickly becomes confusing.</p>
366<p>Since the function name field may contain wildcards, it can be
367anything, including just <code class="computeroutput">*</code>.
368The same is true for the soname.
369However, some ELF objects - specifically, main executables - do not
370have sonames.  Any object lacking a soname is treated as if its soname
371was <code class="computeroutput">NONE</code>, which is why the original
372example above had a name
373<code class="computeroutput">I_WRAP_SONAME_FNNAME_ZU(NONE,foo)</code>.</p>
374<p>Note that the soname of an ELF object is not the same as its
375file name, although it is often similar.  You can find the soname of
376an object <code class="computeroutput">libfoo.so</code> using the command
377<code class="computeroutput">readelf -a libfoo.so | grep soname</code>.</p>
378</div>
379<div class="sect2" title="3.2.3.�Wrapping Semantics">
380<div class="titlepage"><div><div><h3 class="title">
381<a name="manual-core-adv.wrapping.semantics"></a>3.2.3.�Wrapping Semantics</h3></div></div></div>
382<p>The ability for a wrapper to replace an infinite family of functions
383is powerful but brings complications in situations where ELF objects
384appear and disappear (are dlopen'd and dlclose'd) on the fly.
385Valgrind tries to maintain sensible behaviour in such situations.</p>
386<p>For example, suppose a process has dlopened (an ELF object with
387soname) <code class="filename">object1.so</code>, which contains
388<code class="computeroutput">function1</code>.  It starts to use
389<code class="computeroutput">function1</code> immediately.</p>
390<p>After a while it dlopens <code class="filename">wrappers.so</code>,
391which contains a wrapper
392for <code class="computeroutput">function1</code> in (soname)
393<code class="filename">object1.so</code>.  All subsequent calls to
394<code class="computeroutput">function1</code> are rerouted to the wrapper.</p>
395<p>If <code class="filename">wrappers.so</code> is
396later dlclose'd, calls to <code class="computeroutput">function1</code> are
397naturally routed back to the original.</p>
398<p>Alternatively, if <code class="filename">object1.so</code>
399is dlclose'd but <code class="filename">wrappers.so</code> remains,
400then the wrapper exported by <code class="filename">wrappers.so</code>
401becomes inactive, since there
402is no way to get to it - there is no original to call any more.  However,
403Valgrind remembers that the wrapper is still present.  If
404<code class="filename">object1.so</code> is
405eventually dlopen'd again, the wrapper will become active again.</p>
406<p>In short, valgrind inspects all code loading/unloading events to
407ensure that the set of currently active wrappers remains consistent.</p>
408<p>A second possible problem is that of conflicting wrappers.  It is
409easily possible to load two or more wrappers, both of which claim
410to be wrappers for some third function.  In such cases Valgrind will
411complain about conflicting wrappers when the second one appears, and
412will honour only the first one.</p>
413</div>
414<div class="sect2" title="3.2.4.�Debugging">
415<div class="titlepage"><div><div><h3 class="title">
416<a name="manual-core-adv.wrapping.debugging"></a>3.2.4.�Debugging</h3></div></div></div>
417<p>Figuring out what's going on given the dynamic nature of wrapping
418can be difficult.  The
419<code class="option">--trace-redir=yes</code> option makes
420this possible
421by showing the complete state of the redirection subsystem after
422every
423<code class="function">mmap</code>/<code class="function">munmap</code>
424event affecting code (text).</p>
425<p>There are two central concepts:</p>
426<div class="itemizedlist"><ul class="itemizedlist" type="disc">
427<li class="listitem"><p>A "redirection specification" is a binding of
428  a (soname pattern, fnname pattern) pair to a code address.
429  These bindings are created by writing functions with names
430  made with the
431  <code class="computeroutput">I_WRAP_SONAME_FNNAME_{ZZ,_ZU}</code>
432  macros.</p></li>
433<li class="listitem"><p>An "active redirection" is a code-address to
434  code-address binding currently in effect.</p></li>
435</ul></div>
436<p>The state of the wrapping-and-redirection subsystem comprises a set of
437specifications and a set of active bindings.  The specifications are
438acquired/discarded by watching all
439<code class="function">mmap</code>/<code class="function">munmap</code>
440events on code (text)
441sections.  The active binding set is (conceptually) recomputed from
442the specifications, and all known symbol names, following any change
443to the specification set.</p>
444<p><code class="option">--trace-redir=yes</code> shows the contents
445of both sets following any such event.</p>
446<p><code class="option">-v</code> prints a line of text each
447time an active specification is used for the first time.</p>
448<p>Hence for maximum debugging effectiveness you will need to use both
449options.</p>
450<p>One final comment.  The function-wrapping facility is closely
451tied to Valgrind's ability to replace (redirect) specified
452functions, for example to redirect calls to
453<code class="function">malloc</code> to its
454own implementation.  Indeed, a replacement function can be
455regarded as a wrapper function which does not call the original.
456However, to make the implementation more robust, the two kinds
457of interception (wrapping vs replacement) are treated differently.
458</p>
459<p><code class="option">--trace-redir=yes</code> shows
460specifications and bindings for both
461replacement and wrapper functions.  To differentiate the
462two, replacement bindings are printed using
463<code class="computeroutput">R-&gt;</code> whereas
464wraps are printed using <code class="computeroutput">W-&gt;</code>.
465</p>
466</div>
467<div class="sect2" title="3.2.5.�Limitations - control flow">
468<div class="titlepage"><div><div><h3 class="title">
469<a name="manual-core-adv.wrapping.limitations-cf"></a>3.2.5.�Limitations - control flow</h3></div></div></div>
470<p>For the most part, the function wrapping implementation is robust.
471The only important caveat is: in a wrapper, get hold of
472the <code class="computeroutput">OrigFn</code> information using
473<code class="computeroutput">VALGRIND_GET_ORIG_FN</code> before calling any
474other wrapped function.  Once you have the
475<code class="computeroutput">OrigFn</code>, arbitrary
476calls between, recursion between, and longjumps out of wrappers
477should work correctly.  There is never any interaction between wrapped
478functions and merely replaced functions
479(eg <code class="function">malloc</code>), so you can call
480<code class="function">malloc</code> etc safely from within wrappers.
481</p>
482<p>The above comments are true for {x86,amd64,ppc32,arm}-linux.  On
483ppc64-linux function wrapping is more fragile due to the (arguably
484poorly designed) ppc64-linux ABI.  This mandates the use of a shadow
485stack which tracks entries/exits of both wrapper and replacement
486functions.  This gives two limitations: firstly, longjumping out of
487wrappers will rapidly lead to disaster, since the shadow stack will
488not get correctly cleared.  Secondly, since the shadow stack has
489finite size, recursion between wrapper/replacement functions is only
490possible to a limited depth, beyond which Valgrind has to abort the
491run.  This depth is currently 16 calls.</p>
492<p>For all platforms ({x86,amd64,ppc32,ppc64,arm}-linux) all the above
493comments apply on a per-thread basis.  In other words, wrapping is
494thread-safe: each thread must individually observe the above
495restrictions, but there is no need for any kind of inter-thread
496cooperation.</p>
497</div>
498<div class="sect2" title="3.2.6.�Limitations - original function signatures">
499<div class="titlepage"><div><div><h3 class="title">
500<a name="manual-core-adv.wrapping.limitations-sigs"></a>3.2.6.�Limitations - original function signatures</h3></div></div></div>
501<p>As shown in the above example, to call the original you must use a
502macro of the form <code class="computeroutput">CALL_FN_*</code>.
503For technical reasons it is impossible
504to create a single macro to deal with all argument types and numbers,
505so a family of macros covering the most common cases is supplied.  In
506what follows, 'W' denotes a machine-word-typed value (a pointer or a
507C <code class="computeroutput">long</code>),
508and 'v' denotes C's <code class="computeroutput">void</code> type.
509The currently available macros are:</p>
510<pre class="programlisting">
511CALL_FN_v_v    -- call an original of type  void fn ( void )
512CALL_FN_W_v    -- call an original of type  long fn ( void )
513
514CALL_FN_v_W    -- call an original of type  void fn ( long )
515CALL_FN_W_W    -- call an original of type  long fn ( long )
516
517CALL_FN_v_WW   -- call an original of type  void fn ( long, long )
518CALL_FN_W_WW   -- call an original of type  long fn ( long, long )
519
520CALL_FN_v_WWW  -- call an original of type  void fn ( long, long, long )
521CALL_FN_W_WWW  -- call an original of type  long fn ( long, long, long )
522
523CALL_FN_W_WWWW -- call an original of type  long fn ( long, long, long, long )
524CALL_FN_W_5W   -- call an original of type  long fn ( long, long, long, long, long )
525CALL_FN_W_6W   -- call an original of type  long fn ( long, long, long, long, long, long )
526and so on, up to
527CALL_FN_W_12W
528</pre>
529<p>The set of supported types can be expanded as needed.  It is
530regrettable that this limitation exists.  Function wrapping has proven
531difficult to implement, with a certain apparently unavoidable level of
532ickiness.  After several implementation attempts, the present
533arrangement appears to be the least-worst tradeoff.  At least it works
534reliably in the presence of dynamic linking and dynamic code
535loading/unloading.</p>
536<p>You should not attempt to wrap a function of one type signature with a
537wrapper of a different type signature.  Such trickery will surely lead
538to crashes or strange behaviour.  This is not a limitation
539of the function wrapping implementation, merely a reflection of the
540fact that it gives you sweeping powers to shoot yourself in the foot
541if you are not careful.  Imagine the instant havoc you could wreak by
542writing a wrapper which matched any function name in any soname - in
543effect, one which claimed to be a wrapper for all functions in the
544process.</p>
545</div>
546<div class="sect2" title="3.2.7.�Examples">
547<div class="titlepage"><div><div><h3 class="title">
548<a name="manual-core-adv.wrapping.examples"></a>3.2.7.�Examples</h3></div></div></div>
549<p>In the source tree,
550<code class="filename">memcheck/tests/wrap[1-8].c</code> provide a series of
551examples, ranging from very simple to quite advanced.</p>
552<p><code class="filename">mpi/libmpiwrap.c</code> is an example
553of wrapping a big, complex API (the MPI-2 interface).  This file defines
554almost 300 different wrappers.</p>
555</div>
556</div>
557</div>
558<div>
559<br><table class="nav" width="100%" cellspacing="3" cellpadding="2" border="0" summary="Navigation footer">
560<tr>
561<td rowspan="2" width="40%" align="left">
562<a accesskey="p" href="manual-core.html">&lt;&lt;�2.�Using and understanding the Valgrind core</a>�</td>
563<td width="20%" align="center"><a accesskey="u" href="manual.html">Up</a></td>
564<td rowspan="2" width="40%" align="right">�<a accesskey="n" href="mc-manual.html">4.�Memcheck: a memory error detector�&gt;&gt;</a>
565</td>
566</tr>
567<tr><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td></tr>
568</table>
569</div>
570</body>
571</html>
572