1 <?xml version="1.0"?> <!-- -*- sgml -*- -->
2 <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"
4 [ <!ENTITY % vg-entities SYSTEM "vg-entities.xml"> %vg-entities; ]>
7 <chapter id="manual-core-adv" xreflabel="Valgrind's core: advanced topics">
8 <title>Using and understanding the Valgrind core: Advanced Topics</title>
10 <para>This chapter describes advanced aspects of the Valgrind core
11 services, which are mostly of interest to power users who wish to
12 customise and modify Valgrind's default behaviours in certain useful
13 ways. The subjects covered are:</para>
16 <listitem><para>The "Client Request" mechanism</para></listitem>
17 <listitem><para>Debugging your program using Valgrind's gdbserver
18 and GDB</para></listitem>
19 <listitem><para>Function Wrapping</para></listitem>
24 <sect1 id="manual-core-adv.clientreq"
25 xreflabel="The Client Request mechanism">
26 <title>The Client Request mechanism</title>
28 <para>Valgrind has a trapdoor mechanism via which the client
29 program can pass all manner of requests and queries to Valgrind
30 and the current tool. Internally, this is used extensively
31 to make various things work, although that's not visible from the
34 <para>For your convenience, a subset of these so-called client
35 requests is provided to allow you to tell Valgrind facts about
36 the behaviour of your program, and also to make queries.
37 In particular, your program can tell Valgrind about things that it
38 otherwise would not know, leading to better results.
41 <para>Clients need to include a header file to make this work.
42 Which header file depends on which client requests you use. Some
43 client requests are handled by the core, and are defined in the
44 header file <filename>valgrind/valgrind.h</filename>. Tool-specific
45 header files are named after the tool, e.g.
46 <filename>valgrind/memcheck.h</filename>. Each tool-specific header file
47 includes <filename>valgrind/valgrind.h</filename> so you don't need to
48 include it in your client if you include a tool-specific header. All header
49 files can be found in the <literal>include/valgrind</literal> directory of
50 wherever Valgrind was installed.</para>
52 <para>The macros in these header files have the magical property
53 that they generate code in-line which Valgrind can spot.
54 However, the code does nothing when not run on Valgrind, so you
55 are not forced to run your program under Valgrind just because you
56 use the macros in this file. Also, you are not required to link your
57 program with any extra supporting libraries.</para>
59 <para>The code added to your binary has negligible performance impact:
60 on x86, amd64, ppc32, ppc64 and ARM, the overhead is 6 simple integer
61 instructions and is probably undetectable except in tight loops.
62 However, if you really wish to compile out the client requests, you
63 can compile with <option>-DNVALGRIND</option> (analogous to
64 <option>-DNDEBUG</option>'s effect on
65 <function>assert</function>).
68 <para>You are encouraged to copy the <filename>valgrind/*.h</filename> headers
69 into your project's include directory, so your program doesn't have a
70 compile-time dependency on Valgrind being installed. The Valgrind headers,
71 unlike most of the rest of the code, are under a BSD-style license so you may
72 include them without worrying about license incompatibility.</para>
74 <para>Here is a brief description of the macros available in
75 <filename>valgrind.h</filename>, which work with more than one
76 tool (see the tool-specific documentation for explanations of the
77 tool-specific macros).</para>
82 <term><command><computeroutput>RUNNING_ON_VALGRIND</computeroutput></command>:</term>
84 <para>Returns 1 if running on Valgrind, 0 if running on the
85 real CPU. If you are running Valgrind on itself, returns the
86 number of layers of Valgrind emulation you're running on.
92 <term><command><computeroutput>VALGRIND_DISCARD_TRANSLATIONS</computeroutput>:</command></term>
94 <para>Discards translations of code in the specified address
95 range. Useful if you are debugging a JIT compiler or some other
96 dynamic code generation system. After this call, attempts to
97 execute code in the invalidated address range will cause
98 Valgrind to make new translations of that code, which is
99 probably the semantics you want. Note that code invalidations
100 are expensive because finding all the relevant translations
101 quickly is very difficult, so try not to call it often.
102 Note that you can be clever about
103 this: you only need to call it when an area which previously
104 contained code is overwritten with new code. You can choose
105 to write code into fresh memory, and just call this
106 occasionally to discard large chunks of old code all at
109 Alternatively, for transparent self-modifying-code support,
110 use<option>--smc-check=all</option>, or run
111 on ppc32/Linux, ppc64/Linux or ARM/Linux.
117 <term><command><computeroutput>VALGRIND_COUNT_ERRORS</computeroutput>:</command></term>
119 <para>Returns the number of errors found so far by Valgrind. Can be
120 useful in test harness code when combined with the
121 <option>--log-fd=-1</option> option; this runs Valgrind silently,
122 but the client program can detect when errors occur. Only useful
123 for tools that report errors, e.g. it's useful for Memcheck, but for
124 Cachegrind it will always return zero because Cachegrind doesn't
125 report errors.</para>
130 <term><command><computeroutput>VALGRIND_MALLOCLIKE_BLOCK</computeroutput>:</command></term>
132 <para>If your program manages its own memory instead of using
133 the standard <function>malloc</function> /
134 <function>new</function> /
135 <function>new[]</function>, tools that track
136 information about heap blocks will not do nearly as good a
137 job. For example, Memcheck won't detect nearly as many
138 errors, and the error messages won't be as informative. To
139 improve this situation, use this macro just after your custom
140 allocator allocates some new memory. See the comments in
141 <filename>valgrind.h</filename> for information on how to use
147 <term><command><computeroutput>VALGRIND_FREELIKE_BLOCK</computeroutput>:</command></term>
149 <para>This should be used in conjunction with
150 <computeroutput>VALGRIND_MALLOCLIKE_BLOCK</computeroutput>.
151 Again, see <filename>valgrind.h</filename> for
152 information on how to use it.</para>
157 <term><command><computeroutput>VALGRIND_RESIZEINPLACE_BLOCK</computeroutput>:</command></term>
159 <para>Informs a Valgrind tool that the size of an allocated block has been
160 modified but not its address. See <filename>valgrind.h</filename> for
161 more information on how to use it.</para>
167 <command><computeroutput>VALGRIND_CREATE_MEMPOOL</computeroutput></command>,
168 <command><computeroutput>VALGRIND_DESTROY_MEMPOOL</computeroutput></command>,
169 <command><computeroutput>VALGRIND_MEMPOOL_ALLOC</computeroutput></command>,
170 <command><computeroutput>VALGRIND_MEMPOOL_FREE</computeroutput></command>,
171 <command><computeroutput>VALGRIND_MOVE_MEMPOOL</computeroutput></command>,
172 <command><computeroutput>VALGRIND_MEMPOOL_CHANGE</computeroutput></command>,
173 <command><computeroutput>VALGRIND_MEMPOOL_EXISTS</computeroutput></command>:
176 <para>These are similar to
177 <computeroutput>VALGRIND_MALLOCLIKE_BLOCK</computeroutput> and
178 <computeroutput>VALGRIND_FREELIKE_BLOCK</computeroutput>
179 but are tailored towards code that uses memory pools. See
180 <xref linkend="mc-manual.mempools"/> for a detailed description.</para>
185 <term><command><computeroutput>VALGRIND_NON_SIMD_CALL[0123]</computeroutput>:</command></term>
187 <para>Executes a function in the client program on the
188 <emphasis>real</emphasis> CPU, not the virtual CPU that Valgrind
189 normally runs code on. The function must take an integer (holding a
190 thread ID) as the first argument and then 0, 1, 2 or 3 more arguments
191 (depending on which client request is used). These are used in various
192 ways internally to Valgrind. They might be useful to client
195 <para><command>Warning:</command> Only use these if you
196 <emphasis>really</emphasis> know what you are doing. They aren't
197 entirely reliable, and can cause Valgrind to crash. See
198 <filename>valgrind.h</filename> for more details.
204 <term><command><computeroutput>VALGRIND_PRINTF(format, ...)</computeroutput>:</command></term>
206 <para>Print a printf-style message to the Valgrind log file. The
207 message is prefixed with the PID between a pair of
208 <computeroutput>**</computeroutput> markers. (Like all client requests,
209 nothing is output if the client program is not running under Valgrind.)
210 Output is not produced until a newline is encountered, or subsequent
211 Valgrind output is printed; this allows you to build up a single line of
212 output over multiple calls. Returns the number of characters output,
213 excluding the PID prefix.</para>
218 <term><command><computeroutput>VALGRIND_PRINTF_BACKTRACE(format, ...)</computeroutput>:</command></term>
220 <para>Like <computeroutput>VALGRIND_PRINTF</computeroutput> (in
221 particular, the return value is identical), but prints a stack backtrace
222 immediately afterwards.</para>
227 <term><command><computeroutput>VALGRIND_MONITOR_COMMAND(command)</computeroutput>:</command></term>
229 <para>Execute the given monitor command (a string).
230 Returns 0 if command is recognised. Returns 1 if command is not recognised.
231 Note that some monitor commands provide access to a functionality
232 also accessible via a specific client request. For example,
233 memcheck leak search can be requested from the client program
234 using VALGRIND_DO_LEAK_CHECK or via the monitor command "leak_search".
235 Note that the syntax of the command string is only verified at
236 run-time. So, if it exists, it is preferable to use a specific
237 client request to have better compile time verifications of the
244 <term><command><computeroutput>VALGRIND_CLO_CHANGE(option)</computeroutput>:</command></term>
246 <para>Changes the value of a dynamically changeable option (a string).
247 See <xref linkend="manual-core.dynopts"/>.
253 <term><command><computeroutput>VALGRIND_STACK_REGISTER(start, end)</computeroutput>:</command></term>
255 <para>Registers a new stack. Informs Valgrind that the memory range
256 between start and end is a unique stack. Returns a stack identifier
257 that can be used with other
258 <computeroutput>VALGRIND_STACK_*</computeroutput> calls.</para>
259 <para>Valgrind will use this information to determine if a change
260 to the stack pointer is an item pushed onto the stack or a change
261 over to a new stack. Use this if you're using a user-level thread
262 package and are noticing crashes in stack trace recording or
263 spurious errors from Valgrind about uninitialized memory
266 <para><command>Warning:</command> Unfortunately, this client request is
267 unreliable and best avoided.</para>
272 <term><command><computeroutput>VALGRIND_STACK_DEREGISTER(id)</computeroutput>:</command></term>
274 <para>Deregisters a previously registered stack. Informs
275 Valgrind that previously registered memory range with stack id
276 <computeroutput>id</computeroutput> is no longer a stack.</para>
278 <para><command>Warning:</command> Unfortunately, this client request is
279 unreliable and best avoided.</para>
284 <term><command><computeroutput>VALGRIND_STACK_CHANGE(id, start, end)</computeroutput>:</command></term>
286 <para>Changes a previously registered stack. Informs
287 Valgrind that the previously registered stack with stack id
288 <computeroutput>id</computeroutput> has changed its start and end
289 values. Use this if your user-level thread package implements
292 <para><command>Warning:</command> Unfortunately, this client request is
293 unreliable and best avoided.</para>
306 <!-- Referenced from both the manual and manpage -->
307 <sect1 id="&vg-gdbserver-id;"
308 xreflabel="&vg-gdbserver-label;">
309 <title>Debugging your program using Valgrind gdbserver and GDB</title>
311 <para>A program running under Valgrind is not executed directly by the
312 CPU. Instead it runs on a synthetic CPU provided by Valgrind. This is
313 why a debugger cannot debug your program when it runs on Valgrind.
316 This section describes how GDB can interact with the
317 Valgrind gdbserver to provide a fully debuggable program under
318 Valgrind. Used in this way, GDB also provides an interactive usage of
319 Valgrind core or tool functionalities, including incremental leak search
320 under Memcheck and on-demand Massif snapshot production.
323 <sect2 id="manual-core-adv.gdbserver-simple"
324 xreflabel="gdbserver simple example">
325 <title>Quick Start: debugging in 3 steps</title>
327 <para>The simplest way to get started is to run Valgrind with the
328 flag <option>--vgdb-error=0</option>. Then follow the on-screen
329 directions, which give you the precise commands needed to start GDB
330 and connect it to your program.</para>
332 <para>Otherwise, here's a slightly more verbose overview.</para>
334 <para>If you want to debug a program with GDB when using the Memcheck
335 tool, start Valgrind like this:
337 valgrind --vgdb=yes --vgdb-error=0 prog
340 <para>In another shell, start GDB:
345 <para>Then give the following command to GDB:
347 (gdb) target remote | vgdb
350 <para>You can now debug your program e.g. by inserting a breakpoint
351 and then using the GDB <computeroutput>continue</computeroutput>
354 <para>This quick start information is enough for basic usage of the
355 Valgrind gdbserver. The sections below describe more advanced
356 functionality provided by the combination of Valgrind and GDB. Note
357 that the command line flag <option>--vgdb=yes</option> can be omitted,
358 as this is the default value.
363 <sect2 id="manual-core-adv.gdbserver-concept"
364 xreflabel="gdbserver">
365 <title>Valgrind gdbserver overall organisation</title>
366 <para>The GNU GDB debugger is typically used to debug a process
367 running on the same machine. In this mode, GDB uses system calls to
368 control and query the program being debugged. This works well, but
369 only allows GDB to debug a program running on the same computer.
372 <para>GDB can also debug processes running on a different computer.
373 To achieve this, GDB defines a protocol (that is, a set of query and
374 reply packets) that facilitates fetching the value of memory or
375 registers, setting breakpoints, etc. A gdbserver is an implementation
376 of this "GDB remote debugging" protocol. To debug a process running
377 on a remote computer, a gdbserver (sometimes called a GDB stub)
378 must run at the remote computer side.
381 <para>The Valgrind core provides a built-in gdbserver implementation,
382 which is activated using <option>--vgdb=yes</option>
383 or <option>--vgdb=full</option>. This gdbserver allows the process
384 running on Valgrind's synthetic CPU to be debugged remotely.
385 GDB sends protocol query packets (such as "get register contents") to
386 the Valgrind embedded gdbserver. The gdbserver executes the queries
387 (for example, it will get the register values of the synthetic CPU)
388 and gives the results back to GDB.
391 <para>GDB can use various kinds of channels (TCP/IP, serial line, etc)
392 to communicate with the gdbserver. In the case of Valgrind's
393 gdbserver, communication is done via a pipe and a small helper program
394 called <xref linkend="&vg-vgdb-id;"/>, which acts as an
395 intermediary. If no GDB is in use, vgdb can also be
396 used to send monitor commands to the Valgrind gdbserver from a shell
402 <sect2 id="manual-core-adv.gdbserver-gdb"
403 xreflabel="Connecting GDB to a Valgrind gdbserver">
404 <title>Connecting GDB to a Valgrind gdbserver</title>
405 <para>To debug a program "<filename>prog</filename>" running under
406 Valgrind, you must ensure that the Valgrind gdbserver is activated by
407 specifying either <option>--vgdb=yes</option>
408 or <option>--vgdb=full</option>. A secondary command line option,
409 <option>--vgdb-error=number</option>, can be used to tell the gdbserver
410 only to become active once the specified number of errors have been
411 shown. A value of zero will therefore cause
412 the gdbserver to become active at startup, which allows you to
413 insert breakpoints before starting the run. For example:
415 valgrind --tool=memcheck --vgdb=yes --vgdb-error=0 ./prog
418 <para>The Valgrind gdbserver is invoked at startup
419 and indicates it is waiting for a connection from a GDB:</para>
421 <programlisting><![CDATA[
422 ==2418== Memcheck, a memory error detector
423 ==2418== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
424 ==2418== Using Valgrind-3.14.0.GIT and LibVEX; rerun with -h for copyright info
425 ==2418== Command: ./prog
427 ==2418== (action at startup) vgdb me ...
431 <para>GDB (in another shell) can then be connected to the Valgrind gdbserver.
432 For this, GDB must be started on the program <filename>prog</filename>:
438 <para>You then indicate to GDB that you want to debug a remote target:
440 (gdb) target remote | vgdb
442 GDB then starts a vgdb relay application to communicate with the
443 Valgrind embedded gdbserver:</para>
445 <programlisting><![CDATA[
446 (gdb) target remote | vgdb
447 Remote debugging using | vgdb
448 relaying data between gdb and process 2418
449 Reading symbols from /lib/ld-linux.so.2...done.
450 Reading symbols from /usr/lib/debug/lib/ld-2.11.2.so.debug...done.
451 Loaded symbols for /lib/ld-linux.so.2
452 [Switching to Thread 2418]
453 0x001f2850 in _start () from /lib/ld-linux.so.2
457 <para>Note that vgdb is provided as part of the Valgrind
458 distribution. You do not need to install it separately.</para>
460 <para>If vgdb detects that there are multiple Valgrind gdbservers that
461 can be connected to, it will list all such servers and their PIDs, and
462 then exit. You can then reissue the GDB "target" command, but
463 specifying the PID of the process you want to debug:
466 <programlisting><![CDATA[
467 (gdb) target remote | vgdb
468 Remote debugging using | vgdb
469 no --pid= arg given and multiple valgrind pids found:
470 use --pid=2479 for valgrind --tool=memcheck --vgdb=yes --vgdb-error=0 ./prog
471 use --pid=2481 for valgrind --tool=memcheck --vgdb=yes --vgdb-error=0 ./prog
472 use --pid=2483 for valgrind --vgdb=yes --vgdb-error=0 ./another_prog
473 Remote communication error: Resource temporarily unavailable.
474 (gdb) target remote | vgdb --pid=2479
475 Remote debugging using | vgdb --pid=2479
476 relaying data between gdb and process 2479
477 Reading symbols from /lib/ld-linux.so.2...done.
478 Reading symbols from /usr/lib/debug/lib/ld-2.11.2.so.debug...done.
479 Loaded symbols for /lib/ld-linux.so.2
480 [Switching to Thread 2479]
481 0x001f2850 in _start () from /lib/ld-linux.so.2
485 <para>Once GDB is connected to the Valgrind gdbserver, it can be used
486 in the same way as if you were debugging the program natively:</para>
489 <para>Breakpoints can be inserted or deleted.</para>
492 <para>Variables and register values can be examined or modified.
496 <para>Signal handling can be configured (printing, ignoring).
500 <para>Execution can be controlled (continue, step, next, stepi, etc).
504 <para>Program execution can be interrupted using Control-C.</para>
508 <para>And so on. Refer to the GDB user manual for a complete
509 description of GDB's functionality.
514 <sect2 id="manual-core-adv.gdbserver-gdb-android"
515 xreflabel="Connecting to an Android gdbserver">
516 <title>Connecting to an Android gdbserver</title>
517 <para> When developping applications for Android, you will typically use
518 a development system (on which the Android NDK is installed) to compile your
519 application. An Android target system or emulator will be used to run
521 In this setup, Valgrind and vgdb will run on the Android system,
522 while GDB will run on the development system. GDB will connect
523 to the vgdb running on the Android system using the Android NDK
524 'adb forward' application.
526 <para> Example: on the Android system, execute the following:
528 valgrind --vgdb-error=0 --vgdb=yes prog
529 # and then in another shell, run:
534 <para> On the development system, execute the following commands:
536 adb forward tcp:1234 tcp:1234
538 (gdb) target remote :1234
540 GDB will use a local tcp/ip connection to connect to the Android adb forwarder.
541 Adb will establish a relay connection between the host system and the Android
542 target system. Be sure to use the GDB delivered in the
543 Android NDK system (typically, arm-linux-androideabi-gdb), as the host
544 GDB is probably not able to debug Android arm applications.
545 Note that the local port nr (used by GDB) must not necessarily be equal
546 to the port number used by vgdb: adb can forward tcp/ip between different
550 <para>In the current release, the GDB server is not enabled by default
551 for Android, due to problems in establishing a suitable directory in
552 which Valgrind can create the necessary FIFOs (named pipes) for
553 communication purposes. You can stil try to use the GDB server, but
554 you will need to explicitly enable it using the flag
555 <computeroutput>--vgdb=yes</computeroutput> or
556 <computeroutput>--vgdb=full</computeroutput>.
559 <para>Additionally, you
560 will need to select a temporary directory which is (a) writable
561 by Valgrind, and (b) supports FIFOs. This is the main difficult
562 point. Often, <computeroutput>/sdcard</computeroutput> satisfies
563 requirement (a), but fails for (b) because it is a VFAT file system
564 and VFAT does not support pipes. Possibilities you could try are
565 <computeroutput>/data/local</computeroutput>,
566 <computeroutput>/data/local/Inst</computeroutput> (if you
567 installed Valgrind there), or
568 <computeroutput>/data/data/name.of.my.app</computeroutput>, if you
569 are running a specific application and it has its own directory of
570 that form. This last possibility may have the highest probability
573 <para>You can specify the temporary directory to use either via
574 the <computeroutput>--with-tmpdir=</computeroutput> configure time
575 flag, or by setting environment variable TMPDIR when running Valgrind
576 (on the Android device, not on the Android NDK development host).
577 Another alternative is to specify the directory for the FIFOs using
578 the <computeroutput>--vgdb-prefix=</computeroutput> Valgrind command
582 <para>We hope to have a better story for temporary directory handling
583 on Android in the future. The difficulty is that, unlike in standard
584 Unixes, there is no single temporary file directory that reliably
585 works across all devices and scenarios.
590 <sect2 id="manual-core-adv.gdbserver-commandhandling"
591 xreflabel="Monitor command handling by the Valgrind gdbserver">
592 <title>Monitor command handling by the Valgrind gdbserver</title>
594 <para> The Valgrind gdbserver provides additional Valgrind-specific
595 functionality via "monitor commands". Such monitor commands can be
596 sent from the GDB command line or from the shell command line or
597 requested by the client program using the VALGRIND_MONITOR_COMMAND
599 <xref linkend="&vg-monitor-id;"/> for the
600 list of the Valgrind core monitor commands available regardless of the
601 Valgrind tool selected.
604 <para>The following tools provide tool-specific monitor commands:
607 <para><xref linkend="mc-manual.monitor-commands"/></para>
610 <para><xref linkend="cl-manual.monitor-commands"/></para>
613 <para><xref linkend="ms-manual.monitor-commands"/></para>
616 <para><xref linkend="hg-manual.monitor-commands"/></para>
621 <para>An example of a tool specific monitor command is the Memcheck monitor
622 command <computeroutput>leak_check full
623 reachable any</computeroutput>. This requests a full reporting of the
624 allocated memory blocks. To have this leak check executed, use the GDB
627 (gdb) monitor leak_check full reachable any
631 <para>GDB will send the <computeroutput>leak_check</computeroutput>
632 command to the Valgrind gdbserver. The Valgrind gdbserver will
633 execute the monitor command itself, if it recognises it to be a Valgrind core
634 monitor command. If it is not recognised as such, it is assumed to
635 be tool-specific and is handed to the tool for execution. For example:
637 <programlisting><![CDATA[
638 (gdb) monitor leak_check full reachable any
639 ==2418== 100 bytes in 1 blocks are still reachable in loss record 1 of 1
640 ==2418== at 0x4006E9E: malloc (vg_replace_malloc.c:236)
641 ==2418== by 0x804884F: main (prog.c:88)
643 ==2418== LEAK SUMMARY:
644 ==2418== definitely lost: 0 bytes in 0 blocks
645 ==2418== indirectly lost: 0 bytes in 0 blocks
646 ==2418== possibly lost: 0 bytes in 0 blocks
647 ==2418== still reachable: 100 bytes in 1 blocks
648 ==2418== suppressed: 0 bytes in 0 blocks
653 <para>As with other GDB commands, the Valgrind gdbserver will accept
654 abbreviated monitor command names and arguments, as long as the given
655 abbreviation is unambiguous. For example, the above
656 <computeroutput>leak_check</computeroutput>
657 command can also be typed as:
662 The letters <computeroutput>mo</computeroutput> are recognised by GDB as being
663 an abbreviation for <computeroutput>monitor</computeroutput>. So GDB sends the
664 string <computeroutput>l f r a</computeroutput> to the Valgrind
665 gdbserver. The letters provided in this string are unambiguous for the
666 Valgrind gdbserver. This therefore gives the same output as the
667 unabbreviated command and arguments. If the provided abbreviation is
668 ambiguous, the Valgrind gdbserver will report the list of commands (or
669 argument values) that can match:
670 <programlisting><![CDATA[
672 v. can match v.set v.info v.wait v.kill v.translate v.do
674 n_errs_found 0 n_errs_shown 0 (vgdb-error 0)
679 <para>Instead of sending a monitor command from GDB, you can also send
680 these from a shell command line. For example, the following command
681 lines, when given in a shell, will cause the same leak search to be executed
684 vgdb --pid=3145 leak_check full reachable any
685 vgdb --pid=3145 l f r a
688 <para>Note that the Valgrind gdbserver automatically continues the
689 execution of the program after a standalone invocation of
690 vgdb. Monitor commands sent from GDB do not cause the program to
691 continue: the program execution is controlled explicitly using GDB
692 commands such as "continue" or "next".</para>
694 <para>Many monitor commands (e.g. v.info location, memcheck who_points_at, ...) require an address
695 argument and an optional length: <varname><addr> [<len>]</varname>.
696 The arguments can also be provided by using a 'C array like syntax' by providing the address
697 followed by the length between square brackets.</para>
698 <para>For example, the following two monitor commands provide the same information:</para>
699 <programlisting><![CDATA[
700 (gdb) mo xb 0x804a2f0 10
702 (gdb) mo xb 0x804a2f0[10]
708 <sect2 id="manual-core-adv.gdbserver-threads"
709 xreflabel="Valgrind gdbserver thread information">
710 <title>Valgrind gdbserver thread information</title>
712 <para>Valgrind's gdbserver enriches the output of the
713 GDB <computeroutput>info threads</computeroutput> command
714 with Valgrind-specific information.
715 The operating system's thread number is followed
716 by Valgrind's internal index for that thread ("tid") and by
717 the Valgrind scheduler thread state:</para>
719 <programlisting><![CDATA[
721 4 Thread 6239 (tid 4 VgTs_Yielding) 0x001f2832 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
722 * 3 Thread 6238 (tid 3 VgTs_Runnable) make_error (s=0x8048b76 "called from London") at prog.c:20
723 2 Thread 6237 (tid 2 VgTs_WaitSys) 0x001f2832 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
724 1 Thread 6234 (tid 1 VgTs_Yielding) main (argc=1, argv=0xbedcc274) at prog.c:105
730 <sect2 id="manual-core-adv.gdbserver-shadowregisters"
731 xreflabel="Examining and modifying Valgrind shadow registers">
732 <title>Examining and modifying Valgrind shadow registers</title>
734 <para> When the option <option>--vgdb-shadow-registers=yes</option> is
735 given, the Valgrind gdbserver will let GDB examine and/or modify
736 Valgrind's shadow registers. GDB version 7.1 or later is needed for this
737 to work. For x86 and amd64, GDB version 7.2 or later is needed.</para>
739 <para>For each CPU register, the Valgrind core maintains two
740 shadow register sets. These shadow registers can be accessed from
741 GDB by giving a postfix <computeroutput>s1</computeroutput>
742 or <computeroutput>s2</computeroutput> for respectively the first
743 and second shadow register. For example, the x86 register
744 <computeroutput>eax</computeroutput> and its two shadows
745 can be examined using the following commands:</para>
747 <programlisting><![CDATA[
757 <para>Float shadow registers are shown by GDB as unsigned integer
758 values instead of float values, as it is expected that these
759 shadow values are mostly used for memcheck validity bits. </para>
761 <para>Intel/amd64 AVX registers <computeroutput>ymm0</computeroutput>
762 to <computeroutput>ymm15</computeroutput> have also their shadow
763 registers. However, GDB presents the shadow values using two
764 "half" registers. For example, the half shadow registers for
765 <computeroutput>ymm9</computeroutput> are
766 <computeroutput>xmm9s1</computeroutput> (lower half for set 1),
767 <computeroutput>ymm9hs1</computeroutput> (upper half for set 1),
768 <computeroutput>xmm9s2</computeroutput> (lower half for set 2),
769 <computeroutput>ymm9hs2</computeroutput> (upper half for set 2).
770 Note the inconsistent notation for the names of the half registers:
771 the lower part starts with an <computeroutput>x</computeroutput>,
772 the upper part starts with an <computeroutput>y</computeroutput>
773 and has an <computeroutput>h</computeroutput> before the shadow postfix.
775 <para>The special presentation of the AVX shadow registers is due to
776 the fact that GDB independently retrieves the lower and upper half of
777 the <computeroutput>ymm</computeroutput> registers. GDB does not
778 however know that the shadow half registers have to be shown combined.
783 <sect2 id="manual-core-adv.gdbserver-limitations"
784 xreflabel="Limitations of the Valgrind gdbserver">
785 <title>Limitations of the Valgrind gdbserver</title>
787 <para>Debugging with the Valgrind gdbserver is very similar to native
788 debugging. Valgrind's gdbserver implementation is quite
789 complete, and so provides most of the GDB debugging functionality. There
790 are however some limitations and peculiarities:</para>
793 <para>Precision of "stop-at" commands.</para>
795 GDB commands such as "step", "next", "stepi", breakpoints
796 and watchpoints, will stop the execution of the process. With
797 the option <option>--vgdb=yes</option>, the process might not
798 stop at the exact requested instruction. Instead, it might
799 continue execution of the current basic block and stop at one
800 of the following basic blocks. This is linked to the fact that
801 Valgrind gdbserver has to instrument a block to allow stopping
802 at the exact instruction requested. Currently,
803 re-instrumentation of the block currently being executed is not
804 supported. So, if the action requested by GDB (e.g. single
805 stepping or inserting a breakpoint) implies re-instrumentation
806 of the current block, the GDB action may not be executed
810 This limitation applies when the basic block
811 currently being executed has not yet been instrumented for debugging.
812 This typically happens when the gdbserver is activated due to the
813 tool reporting an error or to a watchpoint. If the gdbserver
814 block has been activated following a breakpoint, or if a
815 breakpoint has been inserted in the block before its execution,
816 then the block has already been instrumented for debugging.
819 If you use the option <option>--vgdb=full</option>, then GDB
820 "stop-at" commands will be obeyed precisely. The
821 downside is that this requires each instruction to be
822 instrumented with an additional call to a gdbserver helper
823 function, which gives considerable overhead (+500% for memcheck)
824 compared to <option>--vgdb=no</option>.
825 Option <option>--vgdb=yes</option> has neglectible overhead compared
826 to <option>--vgdb=no</option>.
831 <para>Processor registers and flags values.</para>
832 <para>When Valgrind gdbserver stops on an error, on a breakpoint
833 or when single stepping, registers and flags values might not be always
834 up to date due to the optimisations done by the Valgrind core.
836 <option>--vex-iropt-register-updates=unwindregs-at-mem-access</option>
837 ensures that the registers needed to make a stack trace (typically
838 PC/SP/FP) are up to date at each memory access (i.e. memory exception
840 Disabling some optimisations using the following values will increase
841 the precision of registers and flags values (a typical performance
842 impact for memcheck is given for each option).
845 <option>--vex-iropt-register-updates=allregs-at-mem-access</option> (+10%)
846 ensures that all registers and flags are up to date at each memory
850 <option>--vex-iropt-register-updates=allregs-at-each-insn</option> (+25%)
851 ensures that all registers and flags are up to date at each instruction.
854 Note that <option>--vgdb=full</option> (+500%, see above
855 Precision of "stop-at" commands) automatically
856 activates <option>--vex-iropt-register-updates=allregs-at-each-insn</option>.
861 <para>Hardware watchpoint support by the Valgrind
864 <para> The Valgrind gdbserver can simulate hardware watchpoints
865 if the selected tool provides support for it. Currently,
866 only Memcheck provides hardware watchpoint simulation. The
867 hardware watchpoint simulation provided by Memcheck is much
868 faster that GDB software watchpoints, which are implemented by
869 GDB checking the value of the watched zone(s) after each
870 instruction. Hardware watchpoint simulation also provides read
871 watchpoints. The hardware watchpoint simulation by Memcheck has
872 some limitations compared to real hardware
873 watchpoints. However, the number and length of simulated
874 watchpoints are not limited.
876 <para>Typically, the number of (real) hardware watchpoints is
877 limited. For example, the x86 architecture supports a maximum of
878 4 hardware watchpoints, each watchpoint watching 1, 2, 4 or 8
879 bytes. The Valgrind gdbserver does not have any limitation on the
880 number of simulated hardware watchpoints. It also has no
881 limitation on the length of the memory zone being
882 watched. Using GDB version 7.4 or later allow full use of the
883 flexibility of the Valgrind gdbserver's simulated hardware watchpoints.
884 Previous GDB versions do not understand that Valgrind gdbserver
885 watchpoints have no length limit.
887 <para>Memcheck implements hardware watchpoint simulation by
888 marking the watched address ranges as being unaddressable. When
889 a hardware watchpoint is removed, the range is marked as
890 addressable and defined. Hardware watchpoint simulation of
891 addressable-but-undefined memory zones works properly, but has
892 the undesirable side effect of marking the zone as defined when
893 the watchpoint is removed.
895 <para>Write watchpoints might not be reported at the
896 exact instruction that writes the monitored area,
897 unless option <option>--vgdb=full</option> is given. Read watchpoints
898 will always be reported at the exact instruction reading the
901 <para>It is better to avoid using hardware watchpoint of not
902 addressable (yet) memory: in such a case, GDB will fall back to
903 extremely slow software watchpoints. Also, if you do not quit GDB
904 between two debugging sessions, the hardware watchpoints of the
905 previous sessions will be re-inserted as software watchpoints if
906 the watched memory zone is not addressable at program startup.
911 <para>Stepping inside shared libraries on ARM.</para>
912 <para>For unknown reasons, stepping inside shared
913 libraries on ARM may fail. A workaround is to use the
914 <computeroutput>ldd</computeroutput> command
915 to find the list of shared libraries and their loading address
916 and inform GDB of the loading address using the GDB command
917 "add-symbol-file". Example:
918 <programlisting><![CDATA[
919 (gdb) shell ldd ./prog
920 libc.so.6 => /lib/libc.so.6 (0x4002c000)
921 /lib/ld-linux.so.3 (0x40000000)
922 (gdb) add-symbol-file /lib/libc.so.6 0x4002c000
923 add symbol table from file "/lib/libc.so.6" at
924 .text_addr = 0x4002c000
926 Reading symbols from /lib/libc.so.6...(no debugging symbols found)...done.
933 <para>GDB version needed for ARM and PPC32/64.</para>
934 <para>You must use a GDB version which is able to read XML
935 target description sent by a gdbserver. This is the standard setup
936 if GDB was configured and built with the "expat"
937 library. If your GDB was not configured with XML support, it
938 will report an error message when using the "target"
939 command. Debugging will not work because GDB will then not be
940 able to fetch the registers from the Valgrind gdbserver.
941 For ARM programs using the Thumb instruction set, you must use
942 a GDB version of 7.1 or later, as earlier versions have problems
943 with next/step/breakpoints in Thumb code.
948 <para>Stack unwinding on PPC32/PPC64. </para>
949 <para>On PPC32/PPC64, stack unwinding for leaf functions
950 (functions that do not call any other functions) works properly
951 only when you give the option
952 <option>--vex-iropt-register-updates=allregs-at-mem-access</option>
953 or <option>--vex-iropt-register-updates=allregs-at-each-insn</option>.
954 You must also pass this option in order to get a precise stack when
955 a signal is trapped by GDB.
960 <para>Breakpoints encountered multiple times.</para>
961 <para>Some instructions (e.g. x86 "rep movsb")
962 are translated by Valgrind using a loop. If a breakpoint is placed
963 on such an instruction, the breakpoint will be encountered
964 multiple times -- once for each step of the "implicit" loop
965 implementing the instruction.
970 <para>Execution of Inferior function calls by the Valgrind
973 <para>GDB allows the user to "call" functions inside the process
974 being debugged. Such calls are named "inferior calls" in the GDB
975 terminology. A typical use of an inferior call is to execute
976 a function that prints a human-readable version of a complex data
977 structure. To make an inferior call, use the GDB "print" command
978 followed by the function to call and its arguments. As an
979 example, the following GDB command causes an inferior call to the
980 libc "printf" function to be executed by the process
983 <programlisting><![CDATA[
984 (gdb) p printf("process being debugged has pid %d\n", getpid())
989 <para>The Valgrind gdbserver supports inferior function calls.
990 Whilst an inferior call is running, the Valgrind tool will report
991 errors as usual. If you do not want to have such errors stop the
992 execution of the inferior call, you can
993 use <computeroutput>v.set vgdb-error</computeroutput> to set a
994 big value before the call, then manually reset it to its original
995 value when the call is complete.</para>
997 <para>To execute inferior calls, GDB changes registers such as
998 the program counter, and then continues the execution of the
999 program. In a multithreaded program, all threads are continued,
1000 not just the thread instructed to make the inferior call. If
1001 another thread reports an error or encounters a breakpoint, the
1002 evaluation of the inferior call is abandoned.</para>
1004 <para>Note that inferior function calls are a powerful GDB
1005 feature, but should be used with caution. For example, if
1006 the program being debugged is stopped inside the function "printf",
1007 forcing a recursive call to printf via an inferior call will
1008 very probably create problems. The Valgrind tool might also add
1009 another level of complexity to inferior calls, e.g. by reporting
1010 tool errors during the Inferior call or due to the
1011 instrumentation done.
1017 <para>Connecting to or interrupting a Valgrind process blocked in
1018 a system call.</para>
1020 <para>Connecting to or interrupting a Valgrind process blocked in
1021 a system call requires the "ptrace" system call to be usable.
1022 This may be disabled in your kernel for security reasons.
1025 <para>When running your program, Valgrind's scheduler
1026 periodically checks whether there is any work to be handled by
1027 the gdbserver. Unfortunately this check is only done if at least
1028 one thread of the process is runnable. If all the threads of the
1029 process are blocked in a system call, then the checks do not
1030 happen, and the Valgrind scheduler will not invoke the gdbserver.
1031 In such a case, the vgdb relay application will "force" the
1032 gdbserver to be invoked, without the intervention of the Valgrind
1036 <para>Such forced invocation of the Valgrind gdbserver is
1037 implemented by vgdb using ptrace system calls. On a properly
1038 implemented kernel, the ptrace calls done by vgdb will not
1039 influence the behaviour of the program running under Valgrind.
1040 If however they do, giving the
1041 option <option>--max-invoke-ms=0</option> to the vgdb relay
1042 application will disable the usage of ptrace calls. The
1043 consequence of disabling ptrace usage in vgdb is that a Valgrind
1044 process blocked in a system call cannot be woken up or
1045 interrupted from GDB until it executes enough basic blocks to let
1046 the Valgrind scheduler's normal checking take effect.
1049 <para>When ptrace is disabled in vgdb, you can increase the
1050 responsiveness of the Valgrind gdbserver to commands or
1051 interrupts by giving a lower value to the
1052 option <option>--vgdb-poll</option>. If your application is
1053 blocked in system calls most of the time, using a very low value
1054 for <option>--vgdb-poll</option> will cause a the gdbserver to be
1055 invoked sooner. The gdbserver polling done by Valgrind's
1056 scheduler is very efficient, so the increased polling frequency
1057 should not cause significant performance degradation.
1060 <para>When ptrace is disabled in vgdb, a query packet sent by GDB
1061 may take significant time to be handled by the Valgrind
1062 gdbserver. In such cases, GDB might encounter a protocol
1063 timeout. To avoid this,
1064 you can increase the value of the timeout by using the GDB
1065 command "set remotetimeout".
1068 <para>Ubuntu versions 10.10 and later may restrict the scope of
1069 ptrace to the children of the process calling ptrace. As the
1070 Valgrind process is not a child of vgdb, such restricted scoping
1071 causes the ptrace calls to fail. To avoid that, Valgrind will
1072 automatically allow all processes belonging to the same userid to
1073 "ptrace" a Valgrind process, by using PR_SET_PTRACER.</para>
1075 <para>Unblocking processes blocked in system calls is not
1076 currently implemented on Mac OS X and Android. So you cannot
1077 connect to or interrupt a process blocked in a system call on Mac
1081 <para>Unblocking processes blocked in system calls is implemented
1082 via agent thread on Solaris. This is quite a different approach
1083 than using ptrace on Linux, but leads to equivalent result - Valgrind
1084 gdbserver is invoked. Note that agent thread is a Solaris OS
1085 feature and cannot be disabled.
1090 <para>Changing register values.</para>
1091 <para>The Valgrind gdbserver will only modify the values of the
1092 thread's registers when the thread is in status Runnable or
1093 Yielding. In other states (typically, WaitSys), attempts to
1094 change register values will fail. Amongst other things, this
1095 means that inferior calls are not executed for a thread which is
1096 in a system call, since the Valgrind gdbserver does not implement
1097 system call restart.
1102 <para>Unsupported GDB functionality.</para>
1103 <para>GDB provides a lot of debugging functionality and not all
1104 of it is supported. Specifically, the following are not
1105 supported: reversible debugging and tracepoints.
1110 <para>Unknown limitations or problems.</para>
1111 <para>The combination of GDB, Valgrind and the Valgrind gdbserver
1112 probably has unknown other limitations and problems. If you
1113 encounter strange or unexpected behaviour, feel free to report a
1114 bug. But first please verify that the limitation or problem is
1115 not inherent to GDB or the GDB remote protocol. You may be able
1116 to do so by checking the behaviour when using standard gdbserver
1117 part of the GDB package.
1125 <!-- Referenced from both the manual and manpage -->
1126 <sect2 id="&vg-vgdb-id;"
1127 xreflabel="&vg-vgdb-label;">
1128 <title>vgdb command line options</title>
1129 <para> Usage: <computeroutput>vgdb [OPTION]... [[-c] COMMAND]...</computeroutput></para>
1131 <para> vgdb ("Valgrind to GDB") is a small program that is used as an
1132 intermediary between Valgrind and GDB or a shell.
1133 Therefore, it has two usage modes:
1135 <!-- start of xi:include in the manpage -->
1136 <orderedlist id="vgdb.desc.modes">
1137 <listitem id="manual-core-adv.vgdb-standalone" xreflabel="vgdb standalone">
1138 <para>As a standalone utility, it is used from a shell command
1139 line to send monitor commands to a process running under
1140 Valgrind. For this usage, the vgdb OPTION(s) must be followed by
1141 the monitor command to send. To send more than one command,
1142 separate them with the <option>-c</option> option.
1146 <listitem id="manual-core-adv.vgdb-relay" xreflabel="vgdb relay">
1147 <para>In combination with GDB "target remote |" command, it is
1148 used as the relay application between GDB and the Valgrind
1149 gdbserver. For this usage, only OPTION(s) can be given, but no
1150 COMMAND can be given.
1155 <!-- end of xi:include in the manpage -->
1157 <para><computeroutput>vgdb</computeroutput> accepts the following
1159 <!-- start of xi:include in the manpage -->
1160 <variablelist id="vgdb.opts.list">
1162 <term><option>--pid=<number></option></term>
1163 <listitem><para>Specifies the PID of
1164 the process to which vgdb must connect to. This option is useful
1165 in case more than one Valgrind gdbserver can be connected to. If
1166 the <option>--pid</option> argument is not given and multiple
1167 Valgrind gdbserver processes are running, vgdb will report the
1168 list of such processes and then exit.</para></listitem>
1172 <term><option>--vgdb-prefix</option></term>
1173 <listitem><para>Must be given to both
1174 Valgrind and vgdb if you want to change the default prefix for the
1175 FIFOs (named pipes) used for communication between the Valgrind
1176 gdbserver and vgdb.</para></listitem>
1180 <term><option>--wait=<number></option></term>
1181 <listitem><para>Instructs vgdb to
1182 search for available Valgrind gdbservers for the specified number
1183 of seconds. This makes it possible start a vgdb process
1184 before starting the Valgrind gdbserver with which you intend the
1185 vgdb to communicate. This option is useful when used in
1186 conjunction with a <option>--vgdb-prefix</option> that is
1187 unique to the process you want to wait for.
1188 Also, if you use the <option>--wait</option> argument in the GDB
1189 "target remote" command, you must set the GDB remotetimeout to a
1190 value bigger than the --wait argument value. See option
1191 <option>--max-invoke-ms</option> (just below)
1192 for an example of setting the remotetimeout value.</para></listitem>
1196 <term><option>--max-invoke-ms=<number></option></term>
1197 <listitem><para>Gives the
1198 number of milliseconds after which vgdb will force the invocation
1199 of gdbserver embedded in Valgrind. The default value is 100
1200 milliseconds. A value of 0 disables forced invocation. The forced
1201 invocation is used when vgdb is connected to a Valgrind gdbserver,
1202 and the Valgrind process has all its threads blocked in a system
1206 <para>If you specify a large value, you might need to increase the
1207 GDB "remotetimeout" value from its default value of 2 seconds.
1208 You should ensure that the timeout (in seconds) is
1209 bigger than the <option>--max-invoke-ms</option> value. For
1210 example, for <option>--max-invoke-ms=5000</option>, the following
1211 GDB command is suitable:
1213 (gdb) set remotetimeout 6
1219 <term><option>--cmd-time-out=<number></option></term>
1220 <listitem><para>Instructs a
1221 standalone vgdb to exit if the Valgrind gdbserver it is connected
1222 to does not process a command in the specified number of seconds.
1223 The default value is to never time out.</para></listitem>
1227 <term><option>--port=<portnr></option></term>
1228 <listitem><para>Instructs vgdb to
1229 use tcp/ip and listen for GDB on the specified port nr rather than
1230 to use a pipe to communicate with GDB. Using tcp/ip allows to have
1231 GDB running on one computer and debugging a Valgrind process
1232 running on another target computer.
1235 # On the target computer, start your program under valgrind using
1236 valgrind --vgdb-error=0 prog
1237 # and then in another shell, run:
1240 <para>On the computer which hosts GDB, execute the command:
1243 (gdb) target remote targetip:1234
1245 where targetip is the ip address or hostname of the target computer.
1250 <term><option>-c</option></term>
1251 <listitem><para>To give more than one command to a
1252 standalone vgdb, separate the commands by an
1253 option <option>-c</option>. Example:
1255 vgdb v.set log_output -c leak_check any
1256 ]]></screen></para></listitem>
1260 <term><option>-l</option></term>
1261 <listitem><para>Instructs a standalone vgdb to report
1262 the list of the Valgrind gdbserver processes running and then
1263 exit.</para></listitem>
1267 <term><option>-T</option></term>
1268 <listitem><para>Instructs vgdb to add timestamps to vgdb
1269 information messages.
1274 <term><option>-D</option></term>
1275 <listitem><para>Instructs a standalone vgdb to show the
1276 state of the shared memory used by the Valgrind gdbserver. vgdb
1277 will exit after having shown the Valgrind gdbserver shared memory
1278 state.</para></listitem>
1282 <term><option>-d</option></term>
1283 <listitem><para>Instructs vgdb to produce debugging
1284 output. Give multiple <option>-d</option> args to increase the
1285 verbosity. When giving <option>-d</option> to a relay vgdb, you better
1286 redirect the standard error (stderr) of vgdb to a file to avoid
1287 interaction between GDB and vgdb debugging output.</para></listitem>
1291 <!-- end of xi:include in the manpage -->
1296 <!-- Referenced from both the manual and manpage -->
1297 <sect2 id="&vg-monitor-id;"
1298 xreflabel="&vg-monitor-label;">
1299 <title>Valgrind monitor commands</title>
1301 <para>This section describes the Valgrind monitor commands, available
1302 regardless of the Valgrind tool selected. For the tool specific
1303 commands, refer to <xref linkend="mc-manual.monitor-commands"/>,
1304 <xref linkend="hg-manual.monitor-commands"/>,
1305 <xref linkend="cl-manual.monitor-commands"/> and
1306 <xref linkend="ms-manual.monitor-commands"/>. </para>
1308 <para> The monitor commands can be sent either from a shell command line, by using a
1309 standalone vgdb, or from GDB, by using GDB's "monitor"
1310 command (see <xref linkend="manual-core-adv.gdbserver-commandhandling"/>).
1311 They can also be launched by the client program, using the VALGRIND_MONITOR_COMMAND
1317 <para><varname>help [debug]</varname> instructs Valgrind's gdbserver
1318 to give the list of all monitor commands of the Valgrind core and
1319 of the tool. The optional "debug" argument tells to also give help
1320 for the monitor commands aimed at Valgrind internals debugging.
1325 <para><varname>v.info all_errors</varname> shows all errors found
1329 <para><varname>v.info last_error</varname> shows the last error
1334 <para><varname>v.info location <addr></varname> outputs
1335 information about the location <addr>. Possibly, the
1336 following are described: global variables, local (stack)
1337 variables, allocated or freed blocks, ... The information
1338 produced depends on the tool and on the options given to valgrind.
1339 Some tools (e.g. memcheck and helgrind) produce more detailed
1340 information for client heap blocks. For example, these tools show
1341 the stacktrace where the heap block was allocated. If a tool does
1342 not replace the malloc/free/... functions, then client heap blocks
1343 will not be described. Use the
1344 option <varname>--read-var-info=yes</varname> to obtain more
1345 detailed information about global or local (stack) variables.
1347 <programlisting><![CDATA[
1348 (gdb) monitor v.info location 0x8050b20
1349 Location 0x8050b20 is 0 bytes inside global var "mx"
1350 declared at tc19_shadowmem.c:19
1352 (gdb) mo v.in loc 0x582f33c
1353 Location 0x582f33c is 0 bytes inside local var "info"
1354 declared at tc19_shadowmem.c:282, in frame #1 of thread 3
1356 ]]></programlisting>
1360 <para><varname>v.info n_errs_found [msg]</varname> shows the number of
1361 errors found so far, the nr of errors shown so far and the current
1362 value of the <option>--vgdb-error</option> argument. The optional
1363 <computeroutput>msg</computeroutput> (one or more words) is appended.
1364 Typically, this can be used to insert markers in a process output
1365 file between several tests executed in sequence by a process
1366 started only once. This allows to associate the errors reported
1367 by Valgrind with the specific test that produced these errors.
1372 <para><varname>v.info open_fds</varname> shows the list of open file
1373 descriptors and details related to the file descriptor.
1374 This only works if <option>--track-fds=yes</option>
1375 was given at Valgrind startup.</para>
1379 <para><varname>v.clo <clo_option>...</varname> changes one or more
1380 dynamic command line options. If no clo_option is given, lists the
1381 dynamically changeable options. See
1382 <xref linkend="manual-core.dynopts"/>.
1387 <para><varname>v.set {gdb_output | log_output |
1388 mixed_output}</varname> allows redirection of the Valgrind output
1389 (e.g. the errors detected by the tool). The default setting is
1390 <computeroutput>mixed_output</computeroutput>.</para>
1392 <para>With <computeroutput>mixed_output</computeroutput>, the
1393 Valgrind output goes to the Valgrind log (typically stderr) while
1394 the output of the interactive GDB monitor commands (e.g.
1395 <computeroutput>v.info last_error</computeroutput>)
1396 is displayed by GDB.</para>
1398 <para>With <computeroutput>gdb_output</computeroutput>, both the
1399 Valgrind output and the interactive GDB monitor commands output are
1400 displayed by GDB.</para>
1402 <para>With <computeroutput>log_output</computeroutput>, both the
1403 Valgrind output and the interactive GDB monitor commands output go
1404 to the Valgrind log.</para>
1408 <para><varname>v.wait [ms (default 0)]</varname> instructs
1409 Valgrind gdbserver to sleep "ms" milli-seconds and then
1410 continue. When sent from a standalone vgdb, if this is the last
1411 command, the Valgrind process will continue the execution of the
1412 guest process. The typical usage of this is to use vgdb to send a
1413 "no-op" command to a Valgrind gdbserver so as to continue the
1414 execution of the guest process.
1419 <para><varname>v.kill</varname> requests the gdbserver to kill
1420 the process. This can be used from a standalone vgdb to properly
1421 kill a Valgrind process which is currently expecting a vgdb
1426 <para><varname>v.set vgdb-error <errornr></varname>
1427 dynamically changes the value of the
1428 <option>--vgdb-error</option> argument. A
1429 typical usage of this is to start with
1430 <option>--vgdb-error=0</option> on the
1431 command line, then set a few breakpoints, set the vgdb-error value
1432 to a huge value and continue execution.</para>
1436 <para><varname>xtmemory [<filename> default xtmemory.kcg.%p.%n]</varname>
1437 requests the tool (Memcheck, Massif, Helgrind) to produce an xtree heap memory report.
1438 See <xref linkend="manual-core.xtree"/> for
1439 a detailed explanation about execution trees. </para>
1444 <para>The following Valgrind monitor commands are useful for
1445 investigating the behaviour of Valgrind or its gdbserver in case of
1446 problems or bugs.</para>
1451 <para><varname>v.do expensive_sanity_check_general</varname>
1452 executes various sanity checks. In particular, the sanity of the
1453 Valgrind heap is verified. This can be useful if you suspect that
1454 your program and/or Valgrind has a bug corrupting Valgrind data
1455 structure. It can also be used when a Valgrind tool
1456 reports a client error to the connected GDB, in order to verify
1457 the sanity of Valgrind before continuing the execution.
1462 <para><varname>v.info gdbserver_status</varname> shows the
1463 gdbserver status. In case of problems (e.g. of communications),
1464 this shows the values of some relevant Valgrind gdbserver internal
1465 variables. Note that the variables related to breakpoints and
1466 watchpoints (e.g. the number of breakpoint addresses and the number of
1467 watchpoints) will be zero, as GDB by default removes all
1468 watchpoints and breakpoints when execution stops, and re-inserts
1469 them when resuming the execution of the debugged process. You can
1470 change this GDB behaviour by using the GDB command
1471 <computeroutput>set breakpoint always-inserted on</computeroutput>.
1476 <para><varname>v.info memory [aspacemgr]</varname> shows the statistics of
1477 Valgrind's internal heap management. If
1478 option <option>--profile-heap=yes</option> was given, detailed
1479 statistics will be output. With the optional argument
1480 <computeroutput>aspacemgr</computeroutput>. the segment list maintained
1481 by valgrind address space manager will be output. Note that
1482 this list of segments is always output on the Valgrind log.
1487 <para><varname>v.info exectxt</varname> shows information about
1488 the "executable contexts" (i.e. the stack traces) recorded by
1489 Valgrind. For some programs, Valgrind can record a very high
1490 number of such stack traces, causing a high memory usage. This
1491 monitor command shows all the recorded stack traces, followed by
1492 some statistics. This can be used to analyse the reason for having
1493 a big number of stack traces. Typically, you will use this command
1494 if <varname>v.info memory</varname> has shown significant memory
1495 usage by the "exectxt" arena.
1500 <para><varname>v.info scheduler</varname> shows various
1501 information about threads. First, it outputs the host stack trace,
1502 i.e. the Valgrind code being executed. Then, for each thread, it
1503 outputs the thread state. For non terminated threads, the state is
1504 followed by the guest (client) stack trace. Finally, for each
1505 active thread or for each terminated thread slot not yet re-used,
1506 it shows the max usage of the valgrind stack.</para>
1507 <para>Showing the client stack traces allows to compare the stack
1508 traces produced by the Valgrind unwinder with the stack traces
1509 produced by GDB+Valgrind gdbserver. Pay attention that GDB and
1510 Valgrind scheduler status have their own thread numbering
1511 scheme. To make the link between the GDB thread number and the
1512 corresponding Valgrind scheduler thread number, use the GDB
1513 command <computeroutput>info threads</computeroutput>. The output
1514 of this command shows the GDB thread number and the valgrind
1515 'tid'. The 'tid' is the thread number output
1516 by <computeroutput>v.info scheduler</computeroutput>. When using
1517 the callgrind tool, the callgrind monitor command
1518 <computeroutput>status</computeroutput> outputs internal callgrind
1519 information about the stack/call graph it maintains.
1524 <para><varname>v.info stats</varname> shows various valgrind core and
1525 tool statistics. With this, Valgrind and tool statistics can
1526 be examined while running, even without option <option>--stats=yes</option>.
1531 <para><varname>v.info unwind <addr> [<len>]</varname> shows
1532 the CFI unwind debug info for the address range [addr, addr+len-1].
1533 The default value of <len> is 1, giving the unwind information
1534 for the instruction at <addr>.
1539 <para><varname>v.set debuglog <intvalue></varname> sets the
1540 Valgrind debug log level to <intvalue>. This allows to
1541 dynamically change the log level of Valgrind e.g. when a problem
1546 <para><varname>v.set hostvisibility [yes*|no]</varname> The value
1547 "yes" indicates to gdbserver that GDB can look at the Valgrind
1548 'host' (internal) status/memory. "no" disables this access.
1549 When hostvisibility is activated, GDB can e.g. look at Valgrind
1550 global variables. As an example, to examine a Valgrind global
1551 variable of the memcheck tool on an x86, do the following setup:</para>
1554 (gdb) monitor v.set hostvisibility yes
1555 (gdb) add-symbol-file /path/to/tool/executable/file/memcheck-x86-linux 0x58000000
1556 add symbol table from file "/path/to/tool/executable/file/memcheck-x86-linux" at
1557 .text_addr = 0x58000000
1559 Reading symbols from /path/to/tool/executable/file/memcheck-x86-linux...done.
1563 <para>After that, variables defined in memcheck-x86-linux can be accessed, e.g.</para>
1566 (gdb) p /x vgPlain_threads[1].os_state
1567 $3 = {lwpid = 0x4688, threadgroup = 0x4688, parent = 0x0,
1568 valgrind_stack_base = 0x62e78000, valgrind_stack_init_SP = 0x62f79fe0,
1569 exitcode = 0x0, fatalsig = 0x0}
1571 $5 = {iropt_verbosity = 0, iropt_level = 2,
1572 iropt_register_updates = VexRegUpdUnwindregsAtMemAccess,
1573 iropt_unroll_thresh = 120, guest_max_insns = 60, guest_chase_thresh = 10}
1579 <para><varname>v.translate <address>
1580 [<traceflags>]</varname> shows the translation of the block
1581 containing <computeroutput>address</computeroutput> with the given
1582 trace flags. The <computeroutput>traceflags</computeroutput> value
1583 bit patterns have similar meaning to Valgrind's
1584 <option>--trace-flags</option> option. It can be given
1585 in hexadecimal (e.g. 0x20) or decimal (e.g. 32) or in binary 1s
1586 and 0s bit (e.g. 0b00100000). The default value of the traceflags
1587 is 0b00100000, corresponding to "show after instrumentation".
1588 The output of this command always goes to the Valgrind
1590 <para>The additional bit flag 0b100000000 (bit 8)
1591 has no equivalent in the <option>--trace-flags</option> option.
1592 It enables tracing of the gdbserver specific instrumentation. Note
1593 that this bit 8 can only enable the addition of gdbserver
1594 instrumentation in the trace. Setting it to 0 will not
1595 disable the tracing of the gdbserver instrumentation if it is
1596 active for some other reason, for example because there is a breakpoint at
1597 this address or because gdbserver is in single stepping
1611 <sect1 id="manual-core-adv.wrapping" xreflabel="Function Wrapping">
1612 <title>Function wrapping</title>
1615 Valgrind allows calls to some specified functions to be intercepted and
1616 rerouted to a different, user-supplied function. This can do whatever it
1617 likes, typically examining the arguments, calling onwards to the original,
1618 and possibly examining the result. Any number of functions may be
1622 Function wrapping is useful for instrumenting an API in some way. For
1623 example, Helgrind wraps functions in the POSIX pthreads API so it can know
1624 about thread status changes, and the core is able to wrap
1625 functions in the MPI (message-passing) API so it can know
1626 of memory status changes associated with message arrival/departure.
1627 Such information is usually passed to Valgrind by using client
1628 requests in the wrapper functions, although the exact mechanism may vary.
1631 <sect2 id="manual-core-adv.wrapping.example" xreflabel="A Simple Example">
1632 <title>A Simple Example</title>
1634 <para>Supposing we want to wrap some function</para>
1636 <programlisting><![CDATA[
1637 int foo ( int x, int y ) { return x + y; }]]></programlisting>
1639 <para>A wrapper is a function of identical type, but with a special name
1640 which identifies it as the wrapper for <computeroutput>foo</computeroutput>.
1641 Wrappers need to include
1642 supporting macros from <filename>valgrind.h</filename>.
1643 Here is a simple wrapper which prints the arguments and return value:</para>
1645 <programlisting><![CDATA[
1647 #include "valgrind.h"
1648 int I_WRAP_SONAME_FNNAME_ZU(NONE,foo)( int x, int y )
1652 VALGRIND_GET_ORIG_FN(fn);
1653 printf("foo's wrapper: args %d %d\n", x, y);
1654 CALL_FN_W_WW(result, fn, x,y);
1655 printf("foo's wrapper: result %d\n", result);
1658 ]]></programlisting>
1660 <para>To become active, the wrapper merely needs to be present in a text
1661 section somewhere in the same process' address space as the function
1662 it wraps, and for its ELF symbol name to be visible to Valgrind. In
1663 practice, this means either compiling to a
1664 <computeroutput>.o</computeroutput> and linking it in, or
1665 compiling to a <computeroutput>.so</computeroutput> and
1666 <computeroutput>LD_PRELOAD</computeroutput>ing it in. The latter is more
1667 convenient in that it doesn't require relinking.</para>
1669 <para>All wrappers have approximately the above form. There are three
1670 crucial macros:</para>
1672 <para><computeroutput>I_WRAP_SONAME_FNNAME_ZU</computeroutput>:
1673 this generates the real name of the wrapper.
1674 This is an encoded name which Valgrind notices when reading symbol
1675 table information. What it says is: I am the wrapper for any function
1676 named <computeroutput>foo</computeroutput> which is found in
1677 an ELF shared object with an empty
1678 ("<computeroutput>NONE</computeroutput>") soname field. The specification
1679 mechanism is powerful in
1680 that wildcards are allowed for both sonames and function names.
1681 The details are discussed below.</para>
1683 <para><computeroutput>VALGRIND_GET_ORIG_FN</computeroutput>:
1684 once in the wrapper, the first priority is
1685 to get hold of the address of the original (and any other supporting
1686 information needed). This is stored in a value of opaque
1687 type <computeroutput>OrigFn</computeroutput>.
1688 The information is acquired using
1689 <computeroutput>VALGRIND_GET_ORIG_FN</computeroutput>. It is crucial
1690 to make this macro call before calling any other wrapped function
1691 in the same thread.</para>
1693 <para><computeroutput>CALL_FN_W_WW</computeroutput>: eventually we will
1694 want to call the function being
1695 wrapped. Calling it directly does not work, since that just gets us
1696 back to the wrapper and leads to an infinite loop. Instead, the result
1698 <computeroutput>OrigFn</computeroutput> and arguments are
1699 handed to one of a family of macros of the form
1700 <computeroutput>CALL_FN_*</computeroutput>. These
1701 cause Valgrind to call the original and avoid recursion back to the
1705 <sect2 id="manual-core-adv.wrapping.specs" xreflabel="Wrapping Specifications">
1706 <title>Wrapping Specifications</title>
1708 <para>This scheme has the advantage of being self-contained. A library of
1709 wrappers can be compiled to object code in the normal way, and does
1710 not rely on an external script telling Valgrind which wrappers pertain
1711 to which originals.</para>
1713 <para>Each wrapper has a name which, in the most general case says: I am the
1714 wrapper for any function whose name matches FNPATT and whose ELF
1715 "soname" matches SOPATT. Both FNPATT and SOPATT may contain wildcards
1716 (asterisks) and other characters (spaces, dots, @, etc) which are not
1717 generally regarded as valid C identifier names.</para>
1719 <para>This flexibility is needed to write robust wrappers for POSIX pthread
1720 functions, where typically we are not completely sure of either the
1721 function name or the soname, or alternatively we want to wrap a whole
1722 set of functions at once.</para>
1724 <para>For example, <computeroutput>pthread_create</computeroutput>
1725 in GNU libpthread is usually a
1726 versioned symbol - one whose name ends in, eg,
1727 <computeroutput>@GLIBC_2.3</computeroutput>. Hence we
1728 are not sure what its real name is. We also want to cover any soname
1729 of the form <computeroutput>libpthread.so*</computeroutput>.
1730 So the header of the wrapper will be</para>
1732 <programlisting><![CDATA[
1733 int I_WRAP_SONAME_FNNAME_ZZ(libpthreadZdsoZd0,pthreadZucreateZAZa)
1736 ]]></programlisting>
1738 <para>In order to write unusual characters as valid C function names, a
1739 Z-encoding scheme is used. Names are written literally, except that
1740 a capital Z acts as an escape character, with the following encoding:</para>
1742 <programlisting><![CDATA[
1752 ZL ( # only in valgrind 3.3.0 and later
1753 ZR ) # only in valgrind 3.3.0 and later
1754 ]]></programlisting>
1756 <para>Hence <computeroutput>libpthreadZdsoZd0</computeroutput> is an
1757 encoding of the soname <computeroutput>libpthread.so.0</computeroutput>
1758 and <computeroutput>pthreadZucreateZAZa</computeroutput> is an encoding
1759 of the function name <computeroutput>pthread_create@*</computeroutput>.
1762 <para>The macro <computeroutput>I_WRAP_SONAME_FNNAME_ZZ</computeroutput>
1763 constructs a wrapper name in which
1764 both the soname (first component) and function name (second component)
1765 are Z-encoded. Encoding the function name can be tiresome and is
1766 often unnecessary, so a second macro,
1767 <computeroutput>I_WRAP_SONAME_FNNAME_ZU</computeroutput>, can be
1768 used instead. The <computeroutput>_ZU</computeroutput> variant is
1769 also useful for writing wrappers for
1770 C++ functions, in which the function name is usually already mangled
1771 using some other convention in which Z plays an important role. Having
1772 to encode a second time quickly becomes confusing.</para>
1774 <para>Since the function name field may contain wildcards, it can be
1775 anything, including just <computeroutput>*</computeroutput>.
1776 The same is true for the soname.
1777 However, some ELF objects - specifically, main executables - do not
1778 have sonames. Any object lacking a soname is treated as if its soname
1779 was <computeroutput>NONE</computeroutput>, which is why the original
1780 example above had a name
1781 <computeroutput>I_WRAP_SONAME_FNNAME_ZU(NONE,foo)</computeroutput>.</para>
1783 <para>Note that the soname of an ELF object is not the same as its
1784 file name, although it is often similar. You can find the soname of
1785 an object <computeroutput>libfoo.so</computeroutput> using the command
1786 <computeroutput>readelf -a libfoo.so | grep soname</computeroutput>.</para>
1789 <sect2 id="manual-core-adv.wrapping.semantics" xreflabel="Wrapping Semantics">
1790 <title>Wrapping Semantics</title>
1792 <para>The ability for a wrapper to replace an infinite family of functions
1793 is powerful but brings complications in situations where ELF objects
1794 appear and disappear (are dlopen'd and dlclose'd) on the fly.
1795 Valgrind tries to maintain sensible behaviour in such situations.</para>
1797 <para>For example, suppose a process has dlopened (an ELF object with
1798 soname) <filename>object1.so</filename>, which contains
1799 <computeroutput>function1</computeroutput>. It starts to use
1800 <computeroutput>function1</computeroutput> immediately.</para>
1802 <para>After a while it dlopens <filename>wrappers.so</filename>,
1803 which contains a wrapper
1804 for <computeroutput>function1</computeroutput> in (soname)
1805 <filename>object1.so</filename>. All subsequent calls to
1806 <computeroutput>function1</computeroutput> are rerouted to the wrapper.</para>
1808 <para>If <filename>wrappers.so</filename> is
1809 later dlclose'd, calls to <computeroutput>function1</computeroutput> are
1810 naturally routed back to the original.</para>
1812 <para>Alternatively, if <filename>object1.so</filename>
1813 is dlclose'd but <filename>wrappers.so</filename> remains,
1814 then the wrapper exported by <filename>wrappers.so</filename>
1815 becomes inactive, since there
1816 is no way to get to it - there is no original to call any more. However,
1817 Valgrind remembers that the wrapper is still present. If
1818 <filename>object1.so</filename> is
1819 eventually dlopen'd again, the wrapper will become active again.</para>
1821 <para>In short, valgrind inspects all code loading/unloading events to
1822 ensure that the set of currently active wrappers remains consistent.</para>
1824 <para>A second possible problem is that of conflicting wrappers. It is
1825 easily possible to load two or more wrappers, both of which claim
1826 to be wrappers for some third function. In such cases Valgrind will
1827 complain about conflicting wrappers when the second one appears, and
1828 will honour only the first one.</para>
1831 <sect2 id="manual-core-adv.wrapping.debugging" xreflabel="Debugging">
1832 <title>Debugging</title>
1834 <para>Figuring out what's going on given the dynamic nature of wrapping
1835 can be difficult. The
1836 <option>--trace-redir=yes</option> option makes
1838 by showing the complete state of the redirection subsystem after
1840 <function>mmap</function>/<function>munmap</function>
1841 event affecting code (text).</para>
1843 <para>There are two central concepts:</para>
1847 <listitem><para>A "redirection specification" is a binding of
1848 a (soname pattern, fnname pattern) pair to a code address.
1849 These bindings are created by writing functions with names
1851 <computeroutput>I_WRAP_SONAME_FNNAME_{ZZ,_ZU}</computeroutput>
1852 macros.</para></listitem>
1854 <listitem><para>An "active redirection" is a code-address to
1855 code-address binding currently in effect.</para></listitem>
1859 <para>The state of the wrapping-and-redirection subsystem comprises a set of
1860 specifications and a set of active bindings. The specifications are
1861 acquired/discarded by watching all
1862 <function>mmap</function>/<function>munmap</function>
1863 events on code (text)
1864 sections. The active binding set is (conceptually) recomputed from
1865 the specifications, and all known symbol names, following any change
1866 to the specification set.</para>
1868 <para><option>--trace-redir=yes</option> shows the contents
1869 of both sets following any such event.</para>
1871 <para><option>-v</option> prints a line of text each
1872 time an active specification is used for the first time.</para>
1874 <para>Hence for maximum debugging effectiveness you will need to use both
1877 <para>One final comment. The function-wrapping facility is closely
1878 tied to Valgrind's ability to replace (redirect) specified
1879 functions, for example to redirect calls to
1880 <function>malloc</function> to its
1881 own implementation. Indeed, a replacement function can be
1882 regarded as a wrapper function which does not call the original.
1883 However, to make the implementation more robust, the two kinds
1884 of interception (wrapping vs replacement) are treated differently.
1887 <para><option>--trace-redir=yes</option> shows
1888 specifications and bindings for both
1889 replacement and wrapper functions. To differentiate the
1890 two, replacement bindings are printed using
1891 <computeroutput>R-></computeroutput> whereas
1892 wraps are printed using <computeroutput>W-></computeroutput>.
1897 <sect2 id="manual-core-adv.wrapping.limitations-cf"
1898 xreflabel="Limitations - control flow">
1899 <title>Limitations - control flow</title>
1901 <para>For the most part, the function wrapping implementation is robust.
1902 The only important caveat is: in a wrapper, get hold of
1903 the <computeroutput>OrigFn</computeroutput> information using
1904 <computeroutput>VALGRIND_GET_ORIG_FN</computeroutput> before calling any
1905 other wrapped function. Once you have the
1906 <computeroutput>OrigFn</computeroutput>, arbitrary
1907 calls between, recursion between, and longjumps out of wrappers
1908 should work correctly. There is never any interaction between wrapped
1909 functions and merely replaced functions
1910 (eg <function>malloc</function>), so you can call
1911 <function>malloc</function> etc safely from within wrappers.
1914 <para>The above comments are true for {x86,amd64,ppc32,arm,mips32,s390}-linux.
1916 ppc64-linux function wrapping is more fragile due to the (arguably
1917 poorly designed) ppc64-linux ABI. This mandates the use of a shadow
1918 stack which tracks entries/exits of both wrapper and replacement
1919 functions. This gives two limitations: firstly, longjumping out of
1920 wrappers will rapidly lead to disaster, since the shadow stack will
1921 not get correctly cleared. Secondly, since the shadow stack has
1922 finite size, recursion between wrapper/replacement functions is only
1923 possible to a limited depth, beyond which Valgrind has to abort the
1924 run. This depth is currently 16 calls.</para>
1926 <para>For all platforms ({x86,amd64,ppc32,ppc64,arm,mips32,s390}-linux)
1928 comments apply on a per-thread basis. In other words, wrapping is
1929 thread-safe: each thread must individually observe the above
1930 restrictions, but there is no need for any kind of inter-thread
1935 <sect2 id="manual-core-adv.wrapping.limitations-sigs"
1936 xreflabel="Limitations - original function signatures">
1937 <title>Limitations - original function signatures</title>
1939 <para>As shown in the above example, to call the original you must use a
1940 macro of the form <computeroutput>CALL_FN_*</computeroutput>.
1941 For technical reasons it is impossible
1942 to create a single macro to deal with all argument types and numbers,
1943 so a family of macros covering the most common cases is supplied. In
1944 what follows, 'W' denotes a machine-word-typed value (a pointer or a
1945 C <computeroutput>long</computeroutput>),
1946 and 'v' denotes C's <computeroutput>void</computeroutput> type.
1947 The currently available macros are:</para>
1949 <programlisting><![CDATA[
1950 CALL_FN_v_v -- call an original of type void fn ( void )
1951 CALL_FN_W_v -- call an original of type long fn ( void )
1953 CALL_FN_v_W -- call an original of type void fn ( long )
1954 CALL_FN_W_W -- call an original of type long fn ( long )
1956 CALL_FN_v_WW -- call an original of type void fn ( long, long )
1957 CALL_FN_W_WW -- call an original of type long fn ( long, long )
1959 CALL_FN_v_WWW -- call an original of type void fn ( long, long, long )
1960 CALL_FN_W_WWW -- call an original of type long fn ( long, long, long )
1962 CALL_FN_W_WWWW -- call an original of type long fn ( long, long, long, long )
1963 CALL_FN_W_5W -- call an original of type long fn ( long, long, long, long, long )
1964 CALL_FN_W_6W -- call an original of type long fn ( long, long, long, long, long, long )
1967 ]]></programlisting>
1969 <para>The set of supported types can be expanded as needed. It is
1970 regrettable that this limitation exists. Function wrapping has proven
1971 difficult to implement, with a certain apparently unavoidable level of
1972 ickiness. After several implementation attempts, the present
1973 arrangement appears to be the least-worst tradeoff. At least it works
1974 reliably in the presence of dynamic linking and dynamic code
1975 loading/unloading.</para>
1977 <para>You should not attempt to wrap a function of one type signature with a
1978 wrapper of a different type signature. Such trickery will surely lead
1979 to crashes or strange behaviour. This is not a limitation
1980 of the function wrapping implementation, merely a reflection of the
1981 fact that it gives you sweeping powers to shoot yourself in the foot
1982 if you are not careful. Imagine the instant havoc you could wreak by
1983 writing a wrapper which matched any function name in any soname - in
1984 effect, one which claimed to be a wrapper for all functions in the
1988 <sect2 id="manual-core-adv.wrapping.examples" xreflabel="Examples">
1989 <title>Examples</title>
1991 <para>In the source tree,
1992 <filename>memcheck/tests/wrap[1-8].c</filename> provide a series of
1993 examples, ranging from very simple to quite advanced.</para>
1995 <para><filename>mpi/libmpiwrap.c</filename> is an example
1996 of wrapping a big, complex API (the MPI-2 interface). This file defines
1997 almost 300 different wrappers.</para>