Debian Codegraph @

docs/html/manual-core-adv.html

raw
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4 <title>3. Using and understanding the Valgrind core: Advanced Topics</title>
5 <link rel="stylesheet" type="text/css" href="vg_basic.css">
6 <meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
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">
21 <div class="titlepage"><div><div><h1 class="title">
22 <a name="manual-core-adv"></a>3. Using and understanding the Valgrind core: Advanced Topics</h1></div></div></div>
23 <div class="toc">
24 <p><b>Table of Contents</b></p>
25 <dl class="toc">
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.gdbserver">3.2. Debugging your program using Valgrind gdbserver and GDB</a></span></dt>
28 <dd><dl>
29 <dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.gdbserver-simple">3.2.1. Quick Start: debugging in 3 steps</a></span></dt>
30 <dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.gdbserver-concept">3.2.2. Valgrind gdbserver overall organisation</a></span></dt>
31 <dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.gdbserver-gdb">3.2.3. Connecting GDB to a Valgrind gdbserver</a></span></dt>
32 <dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.gdbserver-gdb-android">3.2.4. Connecting to an Android gdbserver</a></span></dt>
33 <dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.gdbserver-commandhandling">3.2.5. Monitor command handling by the Valgrind gdbserver</a></span></dt>
34 <dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.gdbserver-gdbmonitorfrontend">3.2.6. GDB front end commands for Valgrind gdbserver monitor commands</a></span></dt>
35 <dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.gdbserver-threads">3.2.7. Valgrind gdbserver thread information</a></span></dt>
36 <dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.gdbserver-shadowregisters">3.2.8. Examining and modifying Valgrind shadow registers</a></span></dt>
37 <dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.gdbserver-limitations">3.2.9. Limitations of the Valgrind gdbserver</a></span></dt>
38 <dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.vgdb">3.2.10. vgdb command line options</a></span></dt>
39 <dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.valgrind-monitor-commands">3.2.11. Valgrind monitor commands</a></span></dt>
40 </dl></dd>
41 <dt><span class="sect1"><a href="manual-core-adv.html#manual-core-adv.wrapping">3.3. Function wrapping</a></span></dt>
42 <dd><dl>
43 <dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.wrapping.example">3.3.1. A Simple Example</a></span></dt>
44 <dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.wrapping.specs">3.3.2. Wrapping Specifications</a></span></dt>
45 <dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.wrapping.semantics">3.3.3. Wrapping Semantics</a></span></dt>
46 <dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.wrapping.debugging">3.3.4. Debugging</a></span></dt>
47 <dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.wrapping.limitations-cf">3.3.5. Limitations - control flow</a></span></dt>
48 <dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.wrapping.limitations-sigs">3.3.6. Limitations - original function signatures</a></span></dt>
49 <dt><span class="sect2"><a href="manual-core-adv.html#manual-core-adv.wrapping.examples">3.3.7. Examples</a></span></dt>
50 </dl></dd>
51 </dl>
52 </div>
53 <p>This chapter describes advanced aspects of the Valgrind core
54 services, which are mostly of interest to power users who wish to
55 customise and modify Valgrind's default behaviours in certain useful
56 ways. The subjects covered are:</p>
57 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
58 <li class="listitem"><p>The "Client Request" mechanism</p></li>
59 <li class="listitem"><p>Debugging your program using Valgrind's gdbserver
60 and GDB</p></li>
61 <li class="listitem"><p>Function Wrapping</p></li>
62 </ul></div>
63 <div class="sect1">
64 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
65 <a name="manual-core-adv.clientreq"></a>3.1. The Client Request mechanism</h2></div></div></div>
66 <p>Valgrind has a trapdoor mechanism via which the client
67 program can pass all manner of requests and queries to Valgrind
68 and the current tool. Internally, this is used extensively
69 to make various things work, although that's not visible from the
70 outside.</p>
71 <p>For your convenience, a subset of these so-called client
72 requests is provided to allow you to tell Valgrind facts about
73 the behaviour of your program, and also to make queries.
74 In particular, your program can tell Valgrind about things that it
75 otherwise would not know, leading to better results.
76 </p>
77 <p>Clients need to include a header file to make this work.
78 Which header file depends on which client requests you use. Some
79 client requests are handled by the core, and are defined in the
80 header file <code class="filename">valgrind/valgrind.h</code>. Tool-specific
81 header files are named after the tool, e.g.
82 <code class="filename">valgrind/memcheck.h</code>. Each tool-specific header file
83 includes <code class="filename">valgrind/valgrind.h</code> so you don't need to
84 include it in your client if you include a tool-specific header. All header
85 files can be found in the <code class="literal">include/valgrind</code> directory of
86 wherever Valgrind was installed.</p>
87 <p>The macros in these header files have the magical property
88 that they generate code in-line which Valgrind can spot.
89 However, the code does nothing when not run on Valgrind, so you
90 are not forced to run your program under Valgrind just because you
91 use the macros in this file. Also, you are not required to link your
92 program with any extra supporting libraries.</p>
93 <p>The code added to your binary has negligible performance impact:
94 on x86, amd64, ppc32, ppc64 and ARM, the overhead is 6 simple integer
95 instructions and is probably undetectable except in tight loops.
96 However, if you really wish to compile out the client requests, you
97 can compile with <code class="option">-DNVALGRIND</code> (analogous to
98 <code class="option">-DNDEBUG</code>'s effect on
99 <code class="function">assert</code>).
100 </p>
101 <p>You are encouraged to copy the <code class="filename">valgrind/*.h</code> headers
102 into your project's include directory, so your program doesn't have a
103 compile-time dependency on Valgrind being installed. The Valgrind headers,
104 unlike most of the rest of the code, are under a BSD-style license so you may
105 include them without worrying about license incompatibility.</p>
106 <p>Here is a brief description of the macros available in
107 <code class="filename">valgrind.h</code>, which work with more than one
108 tool (see the tool-specific documentation for explanations of the
109 tool-specific macros).</p>
110 <div class="variablelist"><dl class="variablelist">
111 <dt><span class="term"><span class="command"><strong><code class="computeroutput">RUNNING_ON_VALGRIND</code></strong></span>:</span></dt>
112 <dd><p>Returns 1 if running on Valgrind, 0 if running on the
113 real CPU. If you are running Valgrind on itself, returns the
114 number of layers of Valgrind emulation you're running on.
115 </p></dd>
116 <dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_DISCARD_TRANSLATIONS</code>:</strong></span></span></dt>
117 <dd>
118 <p>Discards translations of code in the specified address
119 range. Useful if you are debugging a JIT compiler or some other
120 dynamic code generation system. After this call, attempts to
121 execute code in the invalidated address range will cause
122 Valgrind to make new translations of that code, which is
123 probably the semantics you want. Note that code invalidations
124 are expensive because finding all the relevant translations
125 quickly is very difficult, so try not to call it often.
126 Note that you can be clever about
127 this: you only need to call it when an area which previously
128 contained code is overwritten with new code. You can choose
129 to write code into fresh memory, and just call this
130 occasionally to discard large chunks of old code all at
131 once.</p>
132 <p>
133 Alternatively, for transparent self-modifying-code support,
134 use<code class="option">--smc-check=all</code>, or run
135 on ppc32/Linux, ppc64/Linux or ARM/Linux.
136 </p>
137 </dd>
138 <dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_COUNT_ERRORS</code>:</strong></span></span></dt>
139 <dd><p>Returns the number of errors found so far by Valgrind. Can be
140 useful in test harness code when combined with the
141 <code class="option">--log-fd=-1</code> option; this runs Valgrind silently,
142 but the client program can detect when errors occur. Only useful
143 for tools that report errors, e.g. it's useful for Memcheck, but for
144 Cachegrind it will always return zero because Cachegrind doesn't
145 report errors.</p></dd>
146 <dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_MALLOCLIKE_BLOCK</code>:</strong></span></span></dt>
147 <dd><p>If your program manages its own memory instead of using
148 the standard <code class="function">malloc</code> /
149 <code class="function">new</code> /
150 <code class="function">new[]</code>, tools that track
151 information about heap blocks will not do nearly as good a
152 job. For example, Memcheck won't detect nearly as many
153 errors, and the error messages won't be as informative. To
154 improve this situation, use this macro just after your custom
155 allocator allocates some new memory. See the comments in
156 <code class="filename">valgrind.h</code> for information on how to use
157 it.</p></dd>
158 <dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_FREELIKE_BLOCK</code>:</strong></span></span></dt>
159 <dd><p>This should be used in conjunction with
160 <code class="computeroutput">VALGRIND_MALLOCLIKE_BLOCK</code>.
161 Again, see <code class="filename">valgrind.h</code> for
162 information on how to use it.</p></dd>
163 <dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_RESIZEINPLACE_BLOCK</code>:</strong></span></span></dt>
164 <dd><p>Informs a Valgrind tool that the size of an allocated block has been
165 modified but not its address. See <code class="filename">valgrind.h</code> for
166 more information on how to use it.</p></dd>
167 <dt><span class="term">
168 <span class="command"><strong><code class="computeroutput">VALGRIND_CREATE_MEMPOOL</code></strong></span>,
169 <span class="command"><strong><code class="computeroutput">VALGRIND_DESTROY_MEMPOOL</code></strong></span>,
170 <span class="command"><strong><code class="computeroutput">VALGRIND_MEMPOOL_ALLOC</code></strong></span>,
171 <span class="command"><strong><code class="computeroutput">VALGRIND_MEMPOOL_FREE</code></strong></span>,
172 <span class="command"><strong><code class="computeroutput">VALGRIND_MOVE_MEMPOOL</code></strong></span>,
173 <span class="command"><strong><code class="computeroutput">VALGRIND_MEMPOOL_CHANGE</code></strong></span>,
174 <span class="command"><strong><code class="computeroutput">VALGRIND_MEMPOOL_EXISTS</code></strong></span>:
175 </span></dt>
176 <dd><p>These are similar to
177 <code class="computeroutput">VALGRIND_MALLOCLIKE_BLOCK</code> and
178 <code class="computeroutput">VALGRIND_FREELIKE_BLOCK</code>
179 but are tailored towards code that uses memory pools. See
180 <a class="xref" href="mc-manual.html#mc-manual.mempools" title="4.8. Memory Pools: describing and working with custom allocators">Memory Pools</a> for a detailed description.</p></dd>
181 <dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_NON_SIMD_CALL[0123]</code>:</strong></span></span></dt>
182 <dd>
183 <p>Executes a function in the client program on the
184 <span class="emphasis"><em>real</em></span> CPU, not the virtual CPU that Valgrind
185 normally runs code on. The function must take an integer (holding a
186 thread ID) as the first argument and then 0, 1, 2 or 3 more arguments
187 (depending on which client request is used). These are used in various
188 ways internally to Valgrind. They might be useful to client
189 programs.</p>
190 <p><span class="command"><strong>Warning:</strong></span> Only use these if you
191 <span class="emphasis"><em>really</em></span> know what you are doing. They aren't
192 entirely reliable, and can cause Valgrind to crash. See
193 <code class="filename">valgrind.h</code> for more details.
194 </p>
195 </dd>
196 <dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_PRINTF(format, ...)</code>:</strong></span></span></dt>
197 <dd><p>Print a printf-style message to the Valgrind log file. The
198 message is prefixed with the PID between a pair of
199 <code class="computeroutput">**</code> markers. (Like all client requests,
200 nothing is output if the client program is not running under Valgrind.)
201 Output is not produced until a newline is encountered, or subsequent
202 Valgrind output is printed; this allows you to build up a single line of
203 output over multiple calls. Returns the number of characters output,
204 excluding the PID prefix.</p></dd>
205 <dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_PRINTF_BACKTRACE(format, ...)</code>:</strong></span></span></dt>
206 <dd><p>Like <code class="computeroutput">VALGRIND_PRINTF</code> (in
207 particular, the return value is identical), but prints a stack backtrace
208 immediately afterwards.</p></dd>
209 <dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_MONITOR_COMMAND(command)</code>:</strong></span></span></dt>
210 <dd><p>Execute the given monitor command (a string).
211 Returns 0 if command is recognised. Returns 1 if command is not recognised.
212 Note that some monitor commands provide access to a functionality
213 also accessible via a specific client request. For example,
214 memcheck leak search can be requested from the client program
215 using VALGRIND_DO_LEAK_CHECK or via the monitor command "leak_search".
216 Note that the syntax of the command string is only verified at
217 run-time. So, if it exists, it is preferable to use a specific
218 client request to have better compile time verifications of the
219 arguments.
220 </p></dd>
221 <dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_CLO_CHANGE(option)</code>:</strong></span></span></dt>
222 <dd><p>Changes the value of a dynamically changeable option (a string).
223 See <a class="xref" href="manual-core.html#manual-core.dynopts" title="2.7.8. Dynamically Changing Options">Dynamically Change Options</a>.
224 </p></dd>
225 <dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_STACK_REGISTER(start, end)</code>:</strong></span></span></dt>
226 <dd>
227 <p>Registers a new stack. Informs Valgrind that the memory range
228 between start and end is a unique stack. Returns a stack identifier
229 that can be used with other
230 <code class="computeroutput">VALGRIND_STACK_*</code> calls.</p>
231 <p>Valgrind will use this information to determine if a change
232 to the stack pointer is an item pushed onto the stack or a change
233 over to a new stack. Use this if you're using a user-level thread
234 package and are noticing crashes in stack trace recording or
235 spurious errors from Valgrind about uninitialized memory
236 reads.</p>
237 <p><span class="command"><strong>Warning:</strong></span> Unfortunately, this client request is
238 unreliable and best avoided.</p>
239 </dd>
240 <dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_STACK_DEREGISTER(id)</code>:</strong></span></span></dt>
241 <dd>
242 <p>Deregisters a previously registered stack. Informs
243 Valgrind that previously registered memory range with stack id
244 <code class="computeroutput">id</code> is no longer a stack.</p>
245 <p><span class="command"><strong>Warning:</strong></span> Unfortunately, this client request is
246 unreliable and best avoided.</p>
247 </dd>
248 <dt><span class="term"><span class="command"><strong><code class="computeroutput">VALGRIND_STACK_CHANGE(id, start, end)</code>:</strong></span></span></dt>
249 <dd>
250 <p>Changes a previously registered stack. Informs
251 Valgrind that the previously registered stack with stack id
252 <code class="computeroutput">id</code> has changed its start and end
253 values. Use this if your user-level thread package implements
254 stack growth.</p>
255 <p><span class="command"><strong>Warning:</strong></span> Unfortunately, this client request is
256 unreliable and best avoided.</p>
257 </dd>
258 </dl></div>
259 </div>
260 <div class="sect1">
261 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
262 <a name="manual-core-adv.gdbserver"></a>3.2. Debugging your program using Valgrind gdbserver and GDB</h2></div></div></div>
263 <p>A program running under Valgrind is not executed directly by the CPU.
264 Instead it runs on a synthetic CPU provided by Valgrind. This is why a debugger
265 cannot natively debug your program when it runs on Valgrind.
266 </p>
267 <p>
268 This section describes how GDB can interact with the
269 Valgrind gdbserver to provide a fully debuggable program under
270 Valgrind. Used in this way, GDB also provides an interactive usage of
271 Valgrind core or tool functionalities, including incremental leak search
272 under Memcheck and on-demand Massif snapshot production.
273 </p>
274 <div class="sect2">
275 <div class="titlepage"><div><div><h3 class="title">
276 <a name="manual-core-adv.gdbserver-simple"></a>3.2.1. Quick Start: debugging in 3 steps</h3></div></div></div>
277 <p>The simplest way to get started is to run Valgrind with the
278 flag <code class="option">--vgdb-error=0</code>. Then follow the on-screen
279 directions, which give you the precise commands needed to start GDB
280 and connect it to your program.</p>
281 <p>Otherwise, here's a slightly more verbose overview.</p>
282 <p>If you want to debug a program with GDB when using the Memcheck
283 tool, start Valgrind like this:
284 </p>
285 <pre class="screen">
286 valgrind --vgdb=yes --vgdb-error=0 prog
287 </pre>
288 <p>In another shell, start GDB:
289 </p>
290 <pre class="screen">
291 gdb prog
292 </pre>
293 <p>Then give the following command to GDB:
294 </p>
295 <pre class="screen">
296 (gdb) target remote | vgdb
297 </pre>
298 <p>You can now debug your program e.g. by inserting a breakpoint
299 and then using the GDB <code class="computeroutput">continue</code>
300 command.</p>
301 <p>This quick start information is enough for basic usage of the
302 Valgrind gdbserver. The sections below describe more advanced
303 functionality provided by the combination of Valgrind and GDB. Note
304 that the command line flag <code class="option">--vgdb=yes</code> can be omitted,
305 as this is the default value.
306 </p>
307 </div>
308 <div class="sect2">
309 <div class="titlepage"><div><div><h3 class="title">
310 <a name="manual-core-adv.gdbserver-concept"></a>3.2.2. Valgrind gdbserver overall organisation</h3></div></div></div>
311 <p>The GNU GDB debugger is typically used to debug a process
312 running on the same machine. In this mode, GDB uses system calls to
313 control and query the program being debugged. This works well, but
314 only allows GDB to debug a program running on the same computer.
315 </p>
316 <p>GDB can also debug processes running on a different computer.
317 To achieve this, GDB defines a protocol (that is, a set of query and
318 reply packets) that facilitates fetching the value of memory or
319 registers, setting breakpoints, etc. A gdbserver is an implementation
320 of this "GDB remote debugging" protocol. To debug a process running
321 on a remote computer, a gdbserver (sometimes called a GDB stub)
322 must run at the remote computer side.
323 </p>
324 <p>The Valgrind core provides a built-in gdbserver implementation,
325 which is activated using <code class="option">--vgdb=yes</code>
326 or <code class="option">--vgdb=full</code>. This gdbserver allows the process
327 running on Valgrind's synthetic CPU to be debugged remotely.
328 GDB sends protocol query packets (such as "get register contents") to
329 the Valgrind embedded gdbserver. The gdbserver executes the queries
330 (for example, it will get the register values of the synthetic CPU)
331 and gives the results back to GDB.
332 </p>
333 <p>GDB can use various kinds of channels (TCP/IP, serial line, etc)
334 to communicate with the gdbserver. In the case of Valgrind's
335 gdbserver, communication is done via a pipe and a small helper program
336 called <a class="xref" href="manual-core-adv.html#manual-core-adv.vgdb" title="3.2.10. vgdb command line options">vgdb</a>, which acts as an
337 intermediary. If no GDB is in use, vgdb can also be
338 used to send monitor commands to the Valgrind gdbserver from a shell
339 command line.
340 </p>
341 </div>
342 <div class="sect2">
343 <div class="titlepage"><div><div><h3 class="title">
344 <a name="manual-core-adv.gdbserver-gdb"></a>3.2.3. Connecting GDB to a Valgrind gdbserver</h3></div></div></div>
345 <p>To debug a program "<code class="filename">prog</code>" running under
346 Valgrind, you must ensure that the Valgrind gdbserver is activated by
347 specifying either <code class="option">--vgdb=yes</code>
348 or <code class="option">--vgdb=full</code>. A secondary command line option,
349 <code class="option">--vgdb-error=number</code>, can be used to tell the gdbserver
350 only to become active once the specified number of errors have been
351 shown. A value of zero will therefore cause
352 the gdbserver to become active at startup, which allows you to
353 insert breakpoints before starting the run. For example:
354 </p>
355 <pre class="screen">
356 valgrind --tool=memcheck --vgdb=yes --vgdb-error=0 ./prog
357 </pre>
358 <p>The Valgrind gdbserver is invoked at startup
359 and indicates it is waiting for a connection from a GDB:</p>
360 <pre class="programlisting">
361 ==2418== Memcheck, a memory error detector
362 ==2418== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
363 ==2418== Using Valgrind-3.14.0.GIT and LibVEX; rerun with -h for copyright info
364 ==2418== Command: ./prog
365 ==2418==
366 ==2418== (action at startup) vgdb me ...
367 </pre>
368 <p>GDB (in another shell) can then be connected to the Valgrind gdbserver.
369 For this, GDB must be started on the program <code class="filename">prog</code>:
370 </p>
371 <pre class="screen">
372 gdb ./prog
373 </pre>
374 <p>You then indicate to GDB that you want to debug a remote target:
375 </p>
376 <pre class="screen">
377 (gdb) target remote | vgdb
378 </pre>
379 <p>
380 GDB then starts a vgdb relay application to communicate with the
381 Valgrind embedded gdbserver:</p>
382 <pre class="programlisting">
383 (gdb) target remote | vgdb
384 Remote debugging using | vgdb
385 relaying data between gdb and process 2418
386 Reading symbols from /lib/ld-linux.so.2...done.
387 Reading symbols from /usr/lib/debug/lib/ld-2.11.2.so.debug...done.
388 Loaded symbols for /lib/ld-linux.so.2
389 [Switching to Thread 2418]
390 0x001f2850 in _start () from /lib/ld-linux.so.2
391 (gdb)
392 </pre>
393 <p>Note that vgdb is provided as part of the Valgrind
394 distribution. You do not need to install it separately.</p>
395 <p>If vgdb detects that there are multiple Valgrind gdbservers that
396 can be connected to, it will list all such servers and their PIDs, and
397 then exit. You can then reissue the GDB "target" command, but
398 specifying the PID of the process you want to debug:
399 </p>
400 <pre class="programlisting">
401 (gdb) target remote | vgdb
402 Remote debugging using | vgdb
403 no --pid= arg given and multiple valgrind pids found:
404 use --pid=2479 for valgrind --tool=memcheck --vgdb=yes --vgdb-error=0 ./prog
405 use --pid=2481 for valgrind --tool=memcheck --vgdb=yes --vgdb-error=0 ./prog
406 use --pid=2483 for valgrind --vgdb=yes --vgdb-error=0 ./another_prog
407 Remote communication error: Resource temporarily unavailable.
408 (gdb) target remote | vgdb --pid=2479
409 Remote debugging using | vgdb --pid=2479
410 relaying data between gdb and process 2479
411 Reading symbols from /lib/ld-linux.so.2...done.
412 Reading symbols from /usr/lib/debug/lib/ld-2.11.2.so.debug...done.
413 Loaded symbols for /lib/ld-linux.so.2
414 [Switching to Thread 2479]
415 0x001f2850 in _start () from /lib/ld-linux.so.2
416 (gdb)
417 </pre>
418 <p>If you want to use the <code class="option">--multi</code> mode which makes vgdb start in extended-remote mode, set the following in GDB:
419 </p>
420 <pre class="screen">
421 # gdb prog
422 (gdb) set remote exec-file prog
423 (gdb) set sysroot /
424 (gdb) target extended-remote | vgdb --multi --vargs -q
425 (gdb) start
426 Temporary breakpoint 1 at 0x24e0
427 Starting program: prog
428 relaying data between gdb and process 2999348
429
430 Temporary breakpoint 1, 0x000000000010a4a0 in main ()
431 (gdb)
432 </pre>
433 <p>
434 </p>
435 <p>Note that in <code class="option">--multi</code> mode you don't have to
436 start valgrind separately. vgdb will start valgrind for
437 you. vgdb <code class="option">--multi</code> mode is experimental and currently
438 has some limitations like not being able to see program stdin and
439 stdout. Also you have to explicitly set the remote exec-file and
440 sysroot to tell GDB the "remote" and local files are the same.</p>
441 <p>Once GDB is connected to the Valgrind gdbserver, it can be used
442 in the same way as if you were debugging the program natively:</p>
443 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
444 <li class="listitem"><p>Breakpoints can be inserted or deleted.</p></li>
445 <li class="listitem"><p>Variables and register values can be examined or modified.
446 </p></li>
447 <li class="listitem"><p>Signal handling can be configured (printing, ignoring).
448 </p></li>
449 <li class="listitem"><p>Execution can be controlled (continue, step, next, stepi, etc).
450 </p></li>
451 <li class="listitem"><p>Program execution can be interrupted using Control-C.</p></li>
452 </ul></div>
453 <p>And so on. Refer to the GDB user manual for a complete
454 description of GDB's functionality.
455 </p>
456 </div>
457 <div class="sect2">
458 <div class="titlepage"><div><div><h3 class="title">
459 <a name="manual-core-adv.gdbserver-gdb-android"></a>3.2.4. Connecting to an Android gdbserver</h3></div></div></div>
460 <p> When developping applications for Android, you will typically use
461 a development system (on which the Android NDK is installed) to compile your
462 application. An Android target system or emulator will be used to run
463 the application.
464 In this setup, Valgrind and vgdb will run on the Android system,
465 while GDB will run on the development system. GDB will connect
466 to the vgdb running on the Android system using the Android NDK
467 'adb forward' application.
468 </p>
469 <p> Example: on the Android system, execute the following:
470 </p>
471 <pre class="screen">
472 valgrind --vgdb-error=0 --vgdb=yes prog
473 # and then in another shell, run:
474 vgdb --port=1234
475 </pre>
476 <p>
477 </p>
478 <p> On the development system, execute the following commands:
479 </p>
480 <pre class="screen">
481 adb forward tcp:1234 tcp:1234
482 gdb prog
483 (gdb) target remote :1234
484 </pre>
485 <p>
486 GDB will use a local tcp/ip connection to connect to the Android adb forwarder.
487 Adb will establish a relay connection between the host system and the Android
488 target system. Be sure to use the GDB delivered in the
489 Android NDK system (typically, arm-linux-androideabi-gdb), as the host
490 GDB is probably not able to debug Android arm applications.
491 Note that the local port nr (used by GDB) must not necessarily be equal
492 to the port number used by vgdb: adb can forward tcp/ip between different
493 port numbers.
494 </p>
495 <p>In the current release, the GDB server is not enabled by default
496 for Android, due to problems in establishing a suitable directory in
497 which Valgrind can create the necessary FIFOs (named pipes) for
498 communication purposes. You can stil try to use the GDB server, but
499 you will need to explicitly enable it using the flag
500 <code class="computeroutput">--vgdb=yes</code> or
501 <code class="computeroutput">--vgdb=full</code>.
502 </p>
503 <p>Additionally, you
504 will need to select a temporary directory which is (a) writable
505 by Valgrind, and (b) supports FIFOs. This is the main difficult
506 point. Often, <code class="computeroutput">/sdcard</code> satisfies
507 requirement (a), but fails for (b) because it is a VFAT file system
508 and VFAT does not support pipes. Possibilities you could try are
509 <code class="computeroutput">/data/local</code>,
510 <code class="computeroutput">/data/local/Inst</code> (if you
511 installed Valgrind there), or
512 <code class="computeroutput">/data/data/name.of.my.app</code>, if you
513 are running a specific application and it has its own directory of
514 that form. This last possibility may have the highest probability
515 of success.</p>
516 <p>You can specify the temporary directory to use either via
517 the <code class="computeroutput">--with-tmpdir=</code> configure time
518 flag, or by setting environment variable TMPDIR when running Valgrind
519 (on the Android device, not on the Android NDK development host).
520 Another alternative is to specify the directory for the FIFOs using
521 the <code class="computeroutput">--vgdb-prefix=</code> Valgrind command
522 line option.
523 </p>
524 <p>We hope to have a better story for temporary directory handling
525 on Android in the future. The difficulty is that, unlike in standard
526 Unixes, there is no single temporary file directory that reliably
527 works across all devices and scenarios.
528 </p>
529 </div>
530 <div class="sect2">
531 <div class="titlepage"><div><div><h3 class="title">
532 <a name="manual-core-adv.gdbserver-commandhandling"></a>3.2.5. Monitor command handling by the Valgrind gdbserver</h3></div></div></div>
533 <p>The Valgrind gdbserver provides additional Valgrind-specific
534 functionality via "monitor commands". Such monitor commands can be
535 sent from the GDB command line or from the shell command line or
536 requested by the client program using the VALGRIND_MONITOR_COMMAND
537 client request. See
538 <a class="xref" href="manual-core-adv.html#manual-core-adv.valgrind-monitor-commands" title="3.2.11. Valgrind monitor commands">Valgrind monitor commands</a> for the
539 list of the Valgrind core monitor commands available regardless of the
540 Valgrind tool selected.
541 </p>
542 <p>The following tools provide tool-specific monitor commands:
543 </p>
544 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
545 <li class="listitem"><p><a class="xref" href="mc-manual.html#mc-manual.monitor-commands" title="4.6. Memcheck Monitor Commands">Memcheck Monitor Commands</a></p></li>
546 <li class="listitem"><p><a class="xref" href="cl-manual.html#cl-manual.monitor-commands" title="6.4. Callgrind Monitor Commands">Callgrind Monitor Commands</a></p></li>
547 <li class="listitem"><p><a class="xref" href="ms-manual.html#ms-manual.monitor-commands" title="9.5. Massif Monitor Commands">Massif Monitor Commands</a></p></li>
548 <li class="listitem"><p><a class="xref" href="hg-manual.html#hg-manual.monitor-commands" title="7.7. Helgrind Monitor Commands">Helgrind Monitor Commands</a></p></li>
549 </ul></div>
550 <p>
551 </p>
552 <p>An example of a tool specific monitor command is the Memcheck monitor
553 command <code class="computeroutput">leak_check full
554 reachable any</code>. This requests a full reporting of the
555 allocated memory blocks. To have this leak check executed, use the GDB
556 command:
557 </p>
558 <pre class="screen">
559 (gdb) monitor leak_check full reachable any
560 </pre>
561 <p>
562 </p>
563 <p>GDB sends as a single string all what follows 'monitor' to the Valgrind
564 gdbserver. The Valgrind gdbserver parses the string and will execute the
565 monitor command itself, if it recognises it to be a Valgrind core monitor
566 command. If it is not recognised as such, it is assumed to be tool-specific and
567 is handed to the tool for execution. For example:
568 </p>
569 <pre class="programlisting">
570 (gdb) monitor leak_check full reachable any
571 ==2418== 100 bytes in 1 blocks are still reachable in loss record 1 of 1
572 ==2418== at 0x4006E9E: malloc (vg_replace_malloc.c:236)
573 ==2418== by 0x804884F: main (prog.c:88)
574 ==2418==
575 ==2418== LEAK SUMMARY:
576 ==2418== definitely lost: 0 bytes in 0 blocks
577 ==2418== indirectly lost: 0 bytes in 0 blocks
578 ==2418== possibly lost: 0 bytes in 0 blocks
579 ==2418== still reachable: 100 bytes in 1 blocks
580 ==2418== suppressed: 0 bytes in 0 blocks
581 ==2418==
582 (gdb)
583 </pre>
584 <p>Similarly to GDB, the Valgrind gdbserver will accept abbreviated monitor
585 command names and arguments, as long as the given abbreviation is unambiguous.
586 For example, the above
587 <code class="computeroutput">leak_check</code>
588 command can also be typed as:
589 </p>
590 <pre class="screen">
591 (gdb) mo l f r a
592 </pre>
593 <p>
594
595 The letters <code class="computeroutput">mo</code> are recognised by GDB as being
596 an abbreviation for <code class="computeroutput">monitor</code>. So GDB sends the
597 string <code class="computeroutput">l f r a</code> to the Valgrind
598 gdbserver. The letters provided in this string are unambiguous for the
599 Valgrind gdbserver. This therefore gives the same output as the
600 unabbreviated command and arguments. If the provided abbreviation is
601 ambiguous, the Valgrind gdbserver will report the list of commands (or
602 argument values) that can match:
603 </p>
604 <pre class="programlisting">
605 (gdb) mo v. n
606 v. can match v.set v.info v.wait v.kill v.translate v.do
607 (gdb) mo v.i n
608 n_errs_found 0 n_errs_shown 0 (vgdb-error 0)
609 (gdb)
610 </pre>
611 <p>
612 </p>
613 <p>Instead of sending a monitor command from GDB, you can also send
614 these from a shell command line. For example, the following command
615 lines, when given in a shell, will cause the same leak search to be executed
616 by the process 3145:
617 </p>
618 <pre class="screen">
619 vgdb --pid=3145 leak_check full reachable any
620 vgdb --pid=3145 l f r a
621 </pre>
622 <p>Note that the Valgrind gdbserver automatically continues the
623 execution of the program after a standalone invocation of
624 vgdb. Monitor commands sent from GDB do not cause the program to
625 continue: the program execution is controlled explicitly using GDB
626 commands such as "continue" or "next".</p>
627 <p>Many monitor commands (e.g. v.info location, memcheck who_points_at, ...) require an address
628 argument and an optional length: <code class="varname">&lt;addr&gt; [&lt;len&gt;]</code>.
629 The arguments can also be provided by using a 'C array like syntax' by providing the address
630 followed by the length between square brackets.</p>
631 <p>For example, the following two monitor commands provide the same information:</p>
632 <pre class="programlisting">
633 (gdb) mo xb 0x804a2f0 10
634 ...
635 (gdb) mo xb 0x804a2f0[10]
636 ...
637 </pre>
638 </div>
639 <div class="sect2">
640 <div class="titlepage"><div><div><h3 class="title">
641 <a name="manual-core-adv.gdbserver-gdbmonitorfrontend"></a>3.2.6. GDB front end commands for Valgrind gdbserver monitor commands</h3></div></div></div>
642 <p>As explained in
643 <a class="xref" href="manual-core-adv.html#manual-core-adv.gdbserver-commandhandling" title="3.2.5. Monitor command handling by the Valgrind gdbserver">Monitor command handling by the Valgrind gdbserver</a>, valgrind monitor
644 commands consist in strings that are not interpreted by GDB. GDB has no
645 knowledge of these valgrind monitor commands. The GDB 'command line
646 interface' infrastructure however provides interesting functionalities to help
647 typing commands such as auto-completion, command specific help, searching for
648 a command or command help matching a regexp, ...
649 </p>
650 <p>To have a better integration of the valgrind monitor commands in the GDB
651 command line interface, Valgrind provides python code defining a GDB front end
652 command for each valgrind monitor command. Similarly, for each tool specific
653 monitor command, the python code provides a matching GDB front end command.
654 </p>
655 <p>Like other GDB commands, the GDB front end Valgrind monitor commands are
656 hierarchically structured starting from 5 "top" GDB commands. Subcommands are
657 defined below these "top" commands. To ease typing, shorter aliases are also
658 provided.
659
660 </p>
661 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
662 <li class="listitem"><p><code class="computeroutput">valgrind</code> (aliased
663 by <code class="computeroutput">vg</code>
664 and <code class="computeroutput">v</code>) is the top GDB command providing
665 front end commands to the Valgrind general monitor commands.</p></li>
666 <li class="listitem"><p><code class="computeroutput">memcheck</code> (aliased
667 by <code class="computeroutput">mc</code>) is the top GDB command
668 providing the front end commands corresponding to the memcheck specific
669 monitor commands.</p></li>
670 <li class="listitem"><p><code class="computeroutput">callgrind</code> (aliased
671 by <code class="computeroutput">cg</code>) is the top GDB command
672 providing the front end commands corresponding to the callgrind specific
673 monitor commands.</p></li>
674 <li class="listitem"><p><code class="computeroutput">massif</code> (aliased
675 by <code class="computeroutput">ms</code>) is the top GDB command
676 providing the front end commands corresponding to the massif specific
677 monitor commands.</p></li>
678 <li class="listitem"><p><code class="computeroutput">helgrind</code> (aliased
679 by <code class="computeroutput">hg</code>) is the top GDB command
680 providing the front end commands corresponding to the helgrind specific
681 monitor commands.</p></li>
682 </ul></div>
683 <p>
684 </p>
685 <p>The usage of a GDB front end command is compatible with a direct usage of
686 the Valgrind monitor command. The below example shows a direct usage of the
687 Memcheck monitor command <code class="computeroutput">xb</code> to examine the
688 definedness status of the some_mem array and equivalent usages based on the GDB
689 front end commands.
690
691 </p>
692 <pre class="programlisting">
693 (gdb) list
694 1 int main()
695 2 {
696 3 char some_mem[5];
697 4 return 0;
698 5 }
699 (gdb) p &amp;some_mem
700 $2 = (char (*)[5]) 0x1ffefffe5b
701 (gdb) p sizeof(some_mem)
702 $3 = 5
703 (gdb) monitor xb 0x1ffefffe5b 5
704 ff ff ff ff ff
705 0x1FFEFFFE5B: 0x00 0x00 0x00 0x00 0x00
706 (gdb) memcheck xb 0x1ffefffe5b 5
707 ff ff ff ff ff
708 0x1FFEFFFE5B: 0x00 0x00 0x00 0x00 0x00
709 (gdb) mc xb &amp;some_mem sizeof(some_mem)
710 ff ff ff ff ff
711 0x1FFEFFFE5B: 0x00 0x00 0x00 0x00 0x00
712 (gdb)
713 </pre>
714 <p>
715
716 It is worth noting down that the third command uses the
717 alias <code class="computeroutput">mc</code>. This command also shows a
718 significant advantage of using the GDB front end commands: as GDB "understands"
719 the structure of these front end commands, where relevant, these front end
720 commands will evaluate their arguments. In the case of
721 the <code class="computeroutput">xb</code> command, the
722 GDB <code class="computeroutput">xb</code> command evaluates its second argument
723 (which must be an address expression) and its optional second argument (which
724 must be an integer expression).
725 </p>
726 <p>GDB will auto-load the python code defining the Valgrind front end
727 commands as soon as GDB detects that the executable being debugged is running
728 under valgrind. This detection is based on observing that the Valgrind
729 process has loaded a specific Valgrind shared library. The loading of this
730 library is done by the dynamic loader very early on in the execution of the
731 process. If GDB is used to connect to a Valgrind process that has not yet
732 started its execution (such as when Valgrind was started with the
733 option <code class="computeroutput">--vgdb-stop-at=startup</code>
734 or <code class="computeroutput">--vgdb-error=0</code>), then the GDB front end
735 commands will not yet be auto-loaded. To have the GDB front end commands
736 auto-loaded, you can put a breakpoint e.g. in main and use the GDB
737 command <code class="computeroutput">continue</code>. Alternatively, you can add
738 in your .gdbinit a line that loads the python code at GDB startup such as:
739 </p>
740 <pre class="programlisting">
741 source /path/to/valgrind/python/code/valgrind-monitor.py
742 </pre>
743 <p>
744 </p>
745 <p>
746 The exact path to use in the source command depends on your Valgrind
747 installation. The output of the shell command <code class="computeroutput">vgdb
748 --help</code> contains the absolute path name for the python file you
749 can source in your .gdbinit to define the GDB valgrind front end monitor
750 commands.
751 </p>
752 </div>
753 <div class="sect2">
754 <div class="titlepage"><div><div><h3 class="title">
755 <a name="manual-core-adv.gdbserver-threads"></a>3.2.7. Valgrind gdbserver thread information</h3></div></div></div>
756 <p>Valgrind's gdbserver enriches the output of the
757 GDB <code class="computeroutput">info threads</code> command
758 with Valgrind-specific information.
759 The operating system's thread number is followed
760 by Valgrind's internal index for that thread ("tid") and by
761 the Valgrind scheduler thread state:</p>
762 <pre class="programlisting">
763 (gdb) info threads
764 4 Thread 6239 (tid 4 VgTs_Yielding) 0x001f2832 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
765 * 3 Thread 6238 (tid 3 VgTs_Runnable) make_error (s=0x8048b76 "called from London") at prog.c:20
766 2 Thread 6237 (tid 2 VgTs_WaitSys) 0x001f2832 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
767 1 Thread 6234 (tid 1 VgTs_Yielding) main (argc=1, argv=0xbedcc274) at prog.c:105
768 (gdb)
769 </pre>
770 </div>
771 <div class="sect2">
772 <div class="titlepage"><div><div><h3 class="title">
773 <a name="manual-core-adv.gdbserver-shadowregisters"></a>3.2.8. Examining and modifying Valgrind shadow registers</h3></div></div></div>
774 <p> When the option <code class="option">--vgdb-shadow-registers=yes</code> is
775 given, the Valgrind gdbserver will let GDB examine and/or modify
776 Valgrind's shadow registers. GDB version 7.1 or later is needed for this
777 to work. For x86 and amd64, GDB version 7.2 or later is needed.</p>
778 <p>For each CPU register, the Valgrind core maintains two
779 shadow register sets. These shadow registers can be accessed from
780 GDB by giving a postfix <code class="computeroutput">s1</code>
781 or <code class="computeroutput">s2</code> for respectively the first
782 and second shadow register. For example, the x86 register
783 <code class="computeroutput">eax</code> and its two shadows
784 can be examined using the following commands:</p>
785 <pre class="programlisting">
786 (gdb) p $eax
787 $1 = 0
788 (gdb) p $eaxs1
789 $2 = 0
790 (gdb) p $eaxs2
791 $3 = 0
792 (gdb)
793 </pre>
794 <p>Float shadow registers are shown by GDB as unsigned integer
795 values instead of float values, as it is expected that these
796 shadow values are mostly used for memcheck validity bits. </p>
797 <p>Intel/amd64 AVX registers <code class="computeroutput">ymm0</code>
798 to <code class="computeroutput">ymm15</code> have also their shadow
799 registers. However, GDB presents the shadow values using two
800 "half" registers. For example, the half shadow registers for
801 <code class="computeroutput">ymm9</code> are
802 <code class="computeroutput">xmm9s1</code> (lower half for set 1),
803 <code class="computeroutput">ymm9hs1</code> (upper half for set 1),
804 <code class="computeroutput">xmm9s2</code> (lower half for set 2),
805 <code class="computeroutput">ymm9hs2</code> (upper half for set 2).
806 Note the inconsistent notation for the names of the half registers:
807 the lower part starts with an <code class="computeroutput">x</code>,
808 the upper part starts with an <code class="computeroutput">y</code>
809 and has an <code class="computeroutput">h</code> before the shadow postfix.
810 </p>
811 <p>The special presentation of the AVX shadow registers is due to
812 the fact that GDB independently retrieves the lower and upper half of
813 the <code class="computeroutput">ymm</code> registers. GDB does not
814 however know that the shadow half registers have to be shown combined.
815 </p>
816 </div>
817 <div class="sect2">
818 <div class="titlepage"><div><div><h3 class="title">
819 <a name="manual-core-adv.gdbserver-limitations"></a>3.2.9. Limitations of the Valgrind gdbserver</h3></div></div></div>
820 <p>Debugging with the Valgrind gdbserver is very similar to native
821 debugging. Valgrind's gdbserver implementation is quite
822 complete, and so provides most of the GDB debugging functionality. There
823 are however some limitations and peculiarities:</p>
824 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
825 <li class="listitem">
826 <p>Precision of "stop-at" commands.</p>
827 <p>
828 GDB commands such as "step", "next", "stepi", breakpoints
829 and watchpoints, will stop the execution of the process. With
830 the option <code class="option">--vgdb=yes</code>, the process might not
831 stop at the exact requested instruction. Instead, it might
832 continue execution of the current basic block and stop at one
833 of the following basic blocks. This is linked to the fact that
834 Valgrind gdbserver has to instrument a block to allow stopping
835 at the exact instruction requested. Currently,
836 re-instrumentation of the block currently being executed is not
837 supported. So, if the action requested by GDB (e.g. single
838 stepping or inserting a breakpoint) implies re-instrumentation
839 of the current block, the GDB action may not be executed
840 precisely.
841 </p>
842 <p>
843 This limitation applies when the basic block
844 currently being executed has not yet been instrumented for debugging.
845 This typically happens when the gdbserver is activated due to the
846 tool reporting an error or to a watchpoint. If the gdbserver
847 block has been activated following a breakpoint, or if a
848 breakpoint has been inserted in the block before its execution,
849 then the block has already been instrumented for debugging.
850 </p>
851 <p>
852 If you use the option <code class="option">--vgdb=full</code>, then GDB
853 "stop-at" commands will be obeyed precisely. The
854 downside is that this requires each instruction to be
855 instrumented with an additional call to a gdbserver helper
856 function, which gives considerable overhead (+500% for memcheck)
857 compared to <code class="option">--vgdb=no</code>.
858 Option <code class="option">--vgdb=yes</code> has neglectible overhead compared
859 to <code class="option">--vgdb=no</code>.
860 </p>
861 </li>
862 <li class="listitem">
863 <p>Processor registers and flags values.</p>
864 <p>When Valgrind gdbserver stops on an error, on a breakpoint
865 or when single stepping, registers and flags values might not be always
866 up to date due to the optimisations done by the Valgrind core.
867 The default value
868 <code class="option">--vex-iropt-register-updates=unwindregs-at-mem-access</code>
869 ensures that the registers needed to make a stack trace (typically
870 PC/SP/FP) are up to date at each memory access (i.e. memory exception
871 points).
872 Disabling some optimisations using the following values will increase
873 the precision of registers and flags values (a typical performance
874 impact for memcheck is given for each option).
875 </p>
876 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; ">
877 <li class="listitem"><p>
878 <code class="option">--vex-iropt-register-updates=allregs-at-mem-access</code> (+10%)
879 ensures that all registers and flags are up to date at each memory
880 access.
881 </p></li>
882 <li class="listitem"><p>
883 <code class="option">--vex-iropt-register-updates=allregs-at-each-insn</code> (+25%)
884 ensures that all registers and flags are up to date at each instruction.
885 </p></li>
886 </ul></div>
887 <p>
888 Note that <code class="option">--vgdb=full</code> (+500%, see above
889 Precision of "stop-at" commands) automatically
890 activates <code class="option">--vex-iropt-register-updates=allregs-at-each-insn</code>.
891 </p>
892 </li>
893 <li class="listitem">
894 <p>Hardware watchpoint support by the Valgrind
895 gdbserver.</p>
896 <p> The Valgrind gdbserver can simulate hardware watchpoints
897 if the selected tool provides support for it. Currently,
898 only Memcheck provides hardware watchpoint simulation. The
899 hardware watchpoint simulation provided by Memcheck is much
900 faster that GDB software watchpoints, which are implemented by
901 GDB checking the value of the watched zone(s) after each
902 instruction. Hardware watchpoint simulation also provides read
903 watchpoints. The hardware watchpoint simulation by Memcheck has
904 some limitations compared to real hardware
905 watchpoints. However, the number and length of simulated
906 watchpoints are not limited.
907 </p>
908 <p>Typically, the number of (real) hardware watchpoints is
909 limited. For example, the x86 architecture supports a maximum of
910 4 hardware watchpoints, each watchpoint watching 1, 2, 4 or 8
911 bytes. The Valgrind gdbserver does not have any limitation on the
912 number of simulated hardware watchpoints. It also has no
913 limitation on the length of the memory zone being
914 watched. Using GDB version 7.4 or later allow full use of the
915 flexibility of the Valgrind gdbserver's simulated hardware watchpoints.
916 Previous GDB versions do not understand that Valgrind gdbserver
917 watchpoints have no length limit.
918 </p>
919 <p>Memcheck implements hardware watchpoint simulation by
920 marking the watched address ranges as being unaddressable. When
921 a hardware watchpoint is removed, the range is marked as
922 addressable and defined. Hardware watchpoint simulation of
923 addressable-but-undefined memory zones works properly, but has
924 the undesirable side effect of marking the zone as defined when
925 the watchpoint is removed.
926 </p>
927 <p>Write watchpoints might not be reported at the
928 exact instruction that writes the monitored area,
929 unless option <code class="option">--vgdb=full</code> is given. Read watchpoints
930 will always be reported at the exact instruction reading the
931 watched memory.
932 </p>
933 <p>It is better to avoid using hardware watchpoint of not
934 addressable (yet) memory: in such a case, GDB will fall back to
935 extremely slow software watchpoints. Also, if you do not quit GDB
936 between two debugging sessions, the hardware watchpoints of the
937 previous sessions will be re-inserted as software watchpoints if
938 the watched memory zone is not addressable at program startup.
939 </p>
940 </li>
941 <li class="listitem">
942 <p>Stepping inside shared libraries on ARM.</p>
943 <p>For unknown reasons, stepping inside shared
944 libraries on ARM may fail. A workaround is to use the
945 <code class="computeroutput">ldd</code> command
946 to find the list of shared libraries and their loading address
947 and inform GDB of the loading address using the GDB command
948 "add-symbol-file". Example:
949 </p>
950 <pre class="programlisting">
951 (gdb) shell ldd ./prog
952 libc.so.6 =&gt; /lib/libc.so.6 (0x4002c000)
953 /lib/ld-linux.so.3 (0x40000000)
954 (gdb) add-symbol-file /lib/libc.so.6 0x4002c000
955 add symbol table from file "/lib/libc.so.6" at
956 .text_addr = 0x4002c000
957 (y or n) y
958 Reading symbols from /lib/libc.so.6...(no debugging symbols found)...done.
959 (gdb)
960 </pre>
961 <p>
962 </p>
963 </li>
964 <li class="listitem">
965 <p>GDB version needed for ARM and PPC32/64.</p>
966 <p>You must use a GDB version which is able to read XML
967 target description sent by a gdbserver. This is the standard setup
968 if GDB was configured and built with the "expat"
969 library. If your GDB was not configured with XML support, it
970 will report an error message when using the "target"
971 command. Debugging will not work because GDB will then not be
972 able to fetch the registers from the Valgrind gdbserver.
973 For ARM programs using the Thumb instruction set, you must use
974 a GDB version of 7.1 or later, as earlier versions have problems
975 with next/step/breakpoints in Thumb code.
976 </p>
977 </li>
978 <li class="listitem">
979 <p>Stack unwinding on PPC32/PPC64. </p>
980 <p>On PPC32/PPC64, stack unwinding for leaf functions
981 (functions that do not call any other functions) works properly
982 only when you give the option
983 <code class="option">--vex-iropt-register-updates=allregs-at-mem-access</code>
984 or <code class="option">--vex-iropt-register-updates=allregs-at-each-insn</code>.
985 You must also pass this option in order to get a precise stack when
986 a signal is trapped by GDB.
987 </p>
988 </li>
989 <li class="listitem">
990 <p>Breakpoints encountered multiple times.</p>
991 <p>Some instructions (e.g. x86 "rep movsb")
992 are translated by Valgrind using a loop. If a breakpoint is placed
993 on such an instruction, the breakpoint will be encountered
994 multiple times -- once for each step of the "implicit" loop
995 implementing the instruction.
996 </p>
997 </li>
998 <li class="listitem">
999 <p>Execution of Inferior function calls by the Valgrind
1000 gdbserver.</p>
1001 <p>GDB allows the user to "call" functions inside the process
1002 being debugged. Such calls are named "inferior calls" in the GDB
1003 terminology. A typical use of an inferior call is to execute
1004 a function that prints a human-readable version of a complex data
1005 structure. To make an inferior call, use the GDB "print" command
1006 followed by the function to call and its arguments. As an
1007 example, the following GDB command causes an inferior call to the
1008 libc "printf" function to be executed by the process
1009 being debugged:
1010 </p>
1011 <pre class="programlisting">
1012 (gdb) p printf("process being debugged has pid %d\n", getpid())
1013 $5 = 36
1014 (gdb)
1015 </pre>
1016 <p>The Valgrind gdbserver supports inferior function calls.
1017 Whilst an inferior call is running, the Valgrind tool will report
1018 errors as usual. If you do not want to have such errors stop the
1019 execution of the inferior call, you can
1020 use <code class="computeroutput">v.set vgdb-error</code> to set a
1021 big value before the call, then manually reset it to its original
1022 value when the call is complete.</p>
1023 <p>To execute inferior calls, GDB changes registers such as
1024 the program counter, and then continues the execution of the
1025 program. In a multithreaded program, all threads are continued,
1026 not just the thread instructed to make the inferior call. If
1027 another thread reports an error or encounters a breakpoint, the
1028 evaluation of the inferior call is abandoned.</p>
1029 <p>Note that inferior function calls are a powerful GDB
1030 feature, but should be used with caution. For example, if
1031 the program being debugged is stopped inside the function "printf",
1032 forcing a recursive call to printf via an inferior call will
1033 very probably create problems. The Valgrind tool might also add
1034 another level of complexity to inferior calls, e.g. by reporting
1035 tool errors during the Inferior call or due to the
1036 instrumentation done.
1037 </p>
1038 </li>
1039 <li class="listitem">
1040 <p>Connecting to or interrupting a Valgrind process blocked in
1041 a system call.</p>
1042 <p>Connecting to or interrupting a Valgrind process blocked in
1043 a system call requires the "ptrace" system call to be usable.
1044 This may be disabled in your kernel for security reasons.
1045 </p>
1046 <p>When running your program, Valgrind's scheduler
1047 periodically checks whether there is any work to be handled by
1048 the gdbserver. Unfortunately this check is only done if at least
1049 one thread of the process is runnable. If all the threads of the
1050 process are blocked in a system call, then the checks do not
1051 happen, and the Valgrind scheduler will not invoke the gdbserver.
1052 In such a case, the vgdb relay application will "force" the
1053 gdbserver to be invoked, without the intervention of the Valgrind
1054 scheduler.
1055 </p>
1056 <p>Such forced invocation of the Valgrind gdbserver is
1057 implemented by vgdb using ptrace system calls. On a properly
1058 implemented kernel, the ptrace calls done by vgdb will not
1059 influence the behaviour of the program running under Valgrind.
1060 If however they do, giving the
1061 option <code class="option">--max-invoke-ms=0</code> to the vgdb relay
1062 application will disable the usage of ptrace calls. The
1063 consequence of disabling ptrace usage in vgdb is that a Valgrind
1064 process blocked in a system call cannot be woken up or
1065 interrupted from GDB until it executes enough basic blocks to let
1066 the Valgrind scheduler's normal checking take effect.
1067 </p>
1068 <p>When ptrace is disabled in vgdb, you can increase the
1069 responsiveness of the Valgrind gdbserver to commands or
1070 interrupts by giving a lower value to the
1071 option <code class="option">--vgdb-poll</code>. If your application is
1072 blocked in system calls most of the time, using a very low value
1073 for <code class="option">--vgdb-poll</code> will cause a the gdbserver to be
1074 invoked sooner. The gdbserver polling done by Valgrind's
1075 scheduler is very efficient, so the increased polling frequency
1076 should not cause significant performance degradation.
1077 </p>
1078 <p>When ptrace is disabled in vgdb, a query packet sent by GDB
1079 may take significant time to be handled by the Valgrind
1080 gdbserver. In such cases, GDB might encounter a protocol
1081 timeout. To avoid this,
1082 you can increase the value of the timeout by using the GDB
1083 command "set remotetimeout".
1084 </p>
1085 <p>Ubuntu versions 10.10 and later may restrict the scope of
1086 ptrace to the children of the process calling ptrace. As the
1087 Valgrind process is not a child of vgdb, such restricted scoping
1088 causes the ptrace calls to fail. To avoid that, Valgrind will
1089 automatically allow all processes belonging to the same userid to
1090 "ptrace" a Valgrind process, by using PR_SET_PTRACER.</p>
1091 <p>Unblocking processes blocked in system calls is not
1092 currently implemented on Mac OS X and Android. So you cannot
1093 connect to or interrupt a process blocked in a system call on Mac
1094 OS X or Android.
1095 </p>
1096 <p>Unblocking processes blocked in system calls is implemented
1097 via agent thread on Solaris. This is quite a different approach
1098 than using ptrace on Linux, but leads to equivalent result - Valgrind
1099 gdbserver is invoked. Note that agent thread is a Solaris OS
1100 feature and cannot be disabled.
1101 </p>
1102 </li>
1103 <li class="listitem">
1104 <p>Changing register values.</p>
1105 <p>The Valgrind gdbserver will only modify the values of the
1106 thread's registers when the thread is in status Runnable or
1107 Yielding. In other states (typically, WaitSys), attempts to
1108 change register values will fail. Amongst other things, this
1109 means that inferior calls are not executed for a thread which is
1110 in a system call, since the Valgrind gdbserver does not implement
1111 system call restart.
1112 </p>
1113 </li>
1114 <li class="listitem">
1115 <p>Unsupported GDB functionality.</p>
1116 <p>GDB provides a lot of debugging functionality and not all
1117 of it is supported. Specifically, the following are not
1118 supported: reversible debugging and tracepoints.
1119 </p>
1120 </li>
1121 <li class="listitem">
1122 <p>Unknown limitations or problems.</p>
1123 <p>The combination of GDB, Valgrind and the Valgrind gdbserver
1124 probably has unknown other limitations and problems. If you
1125 encounter strange or unexpected behaviour, feel free to report a
1126 bug. But first please verify that the limitation or problem is
1127 not inherent to GDB or the GDB remote protocol. You may be able
1128 to do so by checking the behaviour when using standard gdbserver
1129 part of the GDB package.
1130 </p>
1131 </li>
1132 </ul></div>
1133 </div>
1134 <div class="sect2">
1135 <div class="titlepage"><div><div><h3 class="title">
1136 <a name="manual-core-adv.vgdb"></a>3.2.10. vgdb command line options</h3></div></div></div>
1137 <p> Usage: <code class="computeroutput">vgdb [OPTION]... [[-c] COMMAND]...</code></p>
1138 <p> vgdb ("Valgrind to GDB") is a small program that is used as an
1139 intermediary between Valgrind and GDB or a shell.
1140 It has three usage modes:
1141 </p>
1142 <div class="orderedlist">
1143 <a name="vgdb.desc.modes"></a><ol class="orderedlist" type="1">
1144 <li class="listitem"><p><a name="manual-core-adv.vgdb-standalone"></a>As a standalone utility, it is used from a shell command
1145 line to send monitor commands to a process running under
1146 Valgrind. For this usage, the vgdb OPTION(s) must be followed by
1147 the monitor command to send. To send more than one command,
1148 separate them with the <code class="option">-c</code> option.
1149 </p></li>
1150 <li class="listitem"><p><a name="manual-core-adv.vgdb-relay"></a>In combination with GDB "target remote |" command, it is
1151 used as the relay application between GDB and the Valgrind
1152 gdbserver. For this usage, only OPTION(s) can be given, but no
1153 COMMAND can be given.
1154 </p></li>
1155 <li class="listitem"><p><a name="manual-core-adv.vgdb-multi"></a>In the <code class="option">--multi</code> mode, vgdb uses the extended
1156 remote protocol to communicate with GDB. This allows you to view
1157 output from both valgrind and GDB in the GDB session. This is
1158 accomplished via the "target extended-remote | vgdb --multi". In
1159 this mode you no longer need to start valgrind yourself. vgdb will
1160 start up valgrind when gdb tells it to run a new program. For this
1161 usage, the vgdb OPTIONS(s) can also include <code class="option">--valgrind</code>
1162 and <code class="option">--vargs</code> to describe how valgrind should be
1163 started.
1164 </p></li>
1165 </ol>
1166 </div>
1167 <p><code class="computeroutput">vgdb</code> accepts the following
1168 options:</p>
1169 <div class="variablelist">
1170 <a name="vgdb.opts.list"></a><dl class="variablelist">
1171 <dt><span class="term"><code class="option">--pid=&lt;number&gt;</code></span></dt>
1172 <dd><p>Specifies the PID of
1173 the process to which vgdb must connect to. This option is useful
1174 in case more than one Valgrind gdbserver can be connected to. If
1175 the <code class="option">--pid</code> argument is not given and multiple
1176 Valgrind gdbserver processes are running, vgdb will report the
1177 list of such processes and then exit.</p></dd>
1178 <dt><span class="term"><code class="option">--vgdb-prefix</code></span></dt>
1179 <dd><p>Must be given to both
1180 Valgrind and vgdb if you want to change the default prefix for the
1181 FIFOs (named pipes) used for communication between the Valgrind
1182 gdbserver and vgdb.</p></dd>
1183 <dt><span class="term"><code class="option">--wait=&lt;number&gt;</code></span></dt>
1184 <dd><p>Instructs vgdb to
1185 search for available Valgrind gdbservers for the specified number
1186 of seconds. This makes it possible start a vgdb process
1187 before starting the Valgrind gdbserver with which you intend the
1188 vgdb to communicate. This option is useful when used in
1189 conjunction with a <code class="option">--vgdb-prefix</code> that is
1190 unique to the process you want to wait for.
1191 Also, if you use the <code class="option">--wait</code> argument in the GDB
1192 "target remote" command, you must set the GDB remotetimeout to a
1193 value bigger than the --wait argument value. See option
1194 <code class="option">--max-invoke-ms</code> (just below)
1195 for an example of setting the remotetimeout value.</p></dd>
1196 <dt><span class="term"><code class="option">--max-invoke-ms=&lt;number&gt;</code></span></dt>
1197 <dd>
1198 <p>Gives the
1199 number of milliseconds after which vgdb will force the invocation
1200 of gdbserver embedded in Valgrind. The default value is 100
1201 milliseconds. A value of 0 disables forced invocation. The forced
1202 invocation is used when vgdb is connected to a Valgrind gdbserver,
1203 and the Valgrind process has all its threads blocked in a system
1204 call.
1205 </p>
1206 <p>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 <code class="option">--max-invoke-ms</code> value. For
1210 example, for <code class="option">--max-invoke-ms=5000</code>, the following
1211 GDB command is suitable:
1212 </p>
1213 <pre class="screen">
1214 (gdb) set remotetimeout 6
1215 </pre>
1216 <p>
1217 </p>
1218 </dd>
1219 <dt><span class="term"><code class="option">--cmd-time-out=&lt;number&gt;</code></span></dt>
1220 <dd><p>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.</p></dd>
1224 <dt><span class="term"><code class="option">--port=&lt;portnr&gt;</code></span></dt>
1225 <dd>
1226 <p>Instructs vgdb to
1227 use tcp/ip and listen for GDB on the specified port nr rather than
1228 to use a pipe to communicate with GDB. Using tcp/ip allows to have
1229 GDB running on one computer and debugging a Valgrind process
1230 running on another target computer.
1231 Example:
1232 </p>
1233 <pre class="screen">
1234 # On the target computer, start your program under valgrind using
1235 valgrind --vgdb-error=0 prog
1236 # and then in another shell, run:
1237 vgdb --port=1234
1238 </pre>
1239 <p>On the computer which hosts GDB, execute the command:
1240 </p>
1241 <pre class="screen">
1242 gdb prog
1243 (gdb) target remote targetip:1234
1244 </pre>
1245 <p>
1246 where targetip is the ip address or hostname of the target computer.
1247 </p>
1248 </dd>
1249 <dt><span class="term"><code class="option">--vgdb-multi</code></span></dt>
1250 <dd><p>Makes vgdb start in extended-remote mode and to wait
1251 for gdb to tell us what to run.</p></dd>
1252 <dt><span class="term"><code class="option">--valgrind</code></span></dt>
1253 <dd><p>The path to valgrind to use, in extended-remote
1254 mode. If not specified, the system valgrind will be
1255 launched.</p></dd>
1256 <dt><span class="term"><code class="option">--vargs</code></span></dt>
1257 <dd><p>Options to run valgrind with, in extended-remote
1258 mode. For example <code class="option">-q</code>. Everything
1259 following <code class="option">--vargs</code> will be provided as arguments
1260 to valgrind as is. </p></dd>
1261 <dt><span class="term"><code class="option">-c</code></span></dt>
1262 <dd>
1263 <p>To give more than one command to a
1264 standalone vgdb, separate the commands by an
1265 option <code class="option">-c</code>. Example:
1266 </p>
1267 <pre class="screen">
1268 vgdb v.set log_output -c leak_check any
1269 </pre>
1270 </dd>
1271 <dt><span class="term"><code class="option">-l</code></span></dt>
1272 <dd><p>Instructs a standalone vgdb to report
1273 the list of the Valgrind gdbserver processes running and then
1274 exit.</p></dd>
1275 <dt><span class="term"><code class="option">-T</code></span></dt>
1276 <dd><p>Instructs vgdb to add timestamps to vgdb
1277 information messages.
1278 </p></dd>
1279 <dt><span class="term"><code class="option">-D</code></span></dt>
1280 <dd><p>Instructs a standalone vgdb to show the
1281 state of the shared memory used by the Valgrind gdbserver. vgdb
1282 will exit after having shown the Valgrind gdbserver shared memory
1283 state.</p></dd>
1284 <dt><span class="term"><code class="option">-d</code></span></dt>
1285 <dd><p>Instructs vgdb to produce debugging
1286 output. Give multiple <code class="option">-d</code> args to increase the
1287 verbosity. When giving <code class="option">-d</code> to a relay vgdb, you better
1288 redirect the standard error (stderr) of vgdb to a file to avoid
1289 interaction between GDB and vgdb debugging output.</p></dd>
1290 </dl>
1291 </div>
1292 </div>
1293 <div class="sect2">
1294 <div class="titlepage"><div><div><h3 class="title">
1295 <a name="manual-core-adv.valgrind-monitor-commands"></a>3.2.11. Valgrind monitor commands</h3></div></div></div>
1296 <p>This section describes the Valgrind monitor commands, available
1297 regardless of the Valgrind tool selected. For the tool specific
1298 commands, refer to <a class="xref" href="mc-manual.html#mc-manual.monitor-commands" title="4.6. Memcheck Monitor Commands">Memcheck Monitor Commands</a>,
1299 <a class="xref" href="hg-manual.html#hg-manual.monitor-commands" title="7.7. Helgrind Monitor Commands">Helgrind Monitor Commands</a>,
1300 <a class="xref" href="cl-manual.html#cl-manual.monitor-commands" title="6.4. Callgrind Monitor Commands">Callgrind Monitor Commands</a> and
1301 <a class="xref" href="ms-manual.html#ms-manual.monitor-commands" title="9.5. Massif Monitor Commands">Massif Monitor Commands</a>. </p>
1302 <p> The monitor commands can be sent either from a shell command line, by using a
1303 standalone vgdb, or from GDB, by using GDB's "monitor"
1304 command (see <a class="xref" href="manual-core-adv.html#manual-core-adv.gdbserver-commandhandling" title="3.2.5. Monitor command handling by the Valgrind gdbserver">Monitor command handling by the Valgrind gdbserver</a>) or
1305 by GDB's "valgrind" front end commands (see
1306 <a class="xref" href="manual-core-adv.html#manual-core-adv.gdbserver-gdbmonitorfrontend" title="3.2.6. GDB front end commands for Valgrind gdbserver monitor commands">GDB front end commands for Valgrind gdbserver monitor commands</a>).
1307 They can also be launched by the client program, using the VALGRIND_MONITOR_COMMAND
1308 client request.
1309 </p>
1310 <p>Whatever the way the monitor command is launched, it will behave the same
1311 way. However, using the GDB's valgrind front end commands allows to benefit
1312 from the GDB infrastructure, such as expression evaluation. When relevant,
1313 the description of a monitor command below describes the additional
1314 flexibility provided by the GDB valgrind front end command. To launch a
1315 valgrind monitor command via its GDB front end command, instead of prefixing
1316 the command with "monitor", you must use the GDB <code class="varname">valgrind</code>
1317 command (or the shorter aliases <code class="varname">vg</code>
1318 or <code class="varname">v</code>). In GDB, you can use <code class="varname">help
1319 valgrind</code> to get help about the valgrind front end monitor commands
1320 and you can use <code class="varname">apropos valgrind</code> to get all the commands
1321 mentionning the word "valgrind" in their name or on-line help.
1322 </p>
1323 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
1324 <li class="listitem">
1325 <p><code class="varname">help [debug]</code> instructs Valgrind's gdbserver
1326 to give the list of all monitor commands of the Valgrind core and
1327 of the tool. The optional "debug" argument tells to also give help
1328 for the monitor commands aimed at Valgrind internals debugging.
1329 </p>
1330 <p>Note that this monitor command produces the help information as
1331 provided by valgrind gdbserver. The GDB help given e.g. by <code class="varname">help
1332 valgrind</code> or <code class="varname">help valgrind v.info</code> provides the
1333 help of the GDB front end command for the equivalent valgrind gdbserver
1334 monitor command. This "GDB help" describes the additional flexibility
1335 provided by the GDB front end command.
1336 </p>
1337 </li>
1338 <li class="listitem">
1339 <p><code class="varname">v.info all_errors [also_suppressed]</code> shows all errors found
1340 so far.</p>
1341 <p> The optional "also_suppressed" argument indicates to also output
1342 the suppressed errors.</p>
1343 </li>
1344 <li class="listitem"><p><code class="varname">v.info last_error</code> shows the last error
1345 found.</p></li>
1346 <li class="listitem">
1347 <p><code class="varname">v.info location &lt;addr&gt;</code> outputs
1348 information about the location &lt;addr&gt;. Possibly, the
1349 following are described: global variables, local (stack)
1350 variables, allocated or freed blocks, ... The information
1351 produced depends on the tool and on the options given to valgrind.
1352 Some tools (e.g. memcheck and helgrind) produce more detailed
1353 information for client heap blocks. For example, these tools show
1354 the stacktrace where the heap block was allocated. If a tool does
1355 not replace the malloc/free/... functions, then client heap blocks
1356 will not be described. Use the
1357 option <code class="varname">--read-var-info=yes</code> to obtain more
1358 detailed information about global or local (stack) variables.
1359 </p>
1360 <pre class="programlisting">
1361 (gdb) monitor v.info location 0x1130a0
1362 Location 0x1130a0 is 0 bytes inside global var "mx"
1363 declared at tc19_shadowmem.c:19
1364 (gdb) mo v.in loc 0x1ffefffe10
1365 Location 0x1ffefffe10 is 0 bytes inside info.child,
1366 declared at tc19_shadowmem.c:139, in frame #1 of thread 1
1367 (gdb)
1368 </pre>
1369 <p>The GDB valgrind front end command <code class="varname">valgrind v.info location ADDR</code>
1370 accepts any address expression for its ADDR argument. In the below examples, mx is
1371 a global struct and info is a pointer to a structure. Instead of having to print the
1372 addresses of the structure and printing the pointer variable, you can directly use the
1373 expressions in the GDB valgrind front end command argument.
1374 </p>
1375 <pre class="programlisting">
1376 (gdb) valgrind v.info location &amp;mx
1377 Location 0x1130a0 is 0 bytes inside global var "mx"
1378 declared at tc19_shadowmem.c:19
1379 (gdb) v v.i lo info
1380 Location 0x1ffefffe10 is 0 bytes inside info.child,
1381 declared at tc19_shadowmem.c:139, in frame #1 of thread 1
1382 (gdb)
1383 </pre>
1384 </li>
1385 <li class="listitem"><p><code class="varname">v.info n_errs_found [msg]</code> shows the number of
1386 errors found so far, the nr of errors shown so far and the current
1387 value of the <code class="option">--vgdb-error</code> argument. The optional
1388 <code class="computeroutput">msg</code> (one or more words) is appended.
1389 Typically, this can be used to insert markers in a process output
1390 file between several tests executed in sequence by a process
1391 started only once. This allows to associate the errors reported
1392 by Valgrind with the specific test that produced these errors.
1393 </p></li>
1394 <li class="listitem"><p><code class="varname">v.info open_fds</code> shows the list of open file
1395 descriptors and details related to the file descriptor.
1396 This only works if <code class="option">--track-fds=yes</code> or
1397 <code class="option">--track-fds=all</code> (to include
1398 <code class="computeroutput">stdin</code>,
1399 <code class="computeroutput">stdout</code> and
1400 <code class="computeroutput">stderr</code>) was given at Valgrind startup.</p></li>
1401 <li class="listitem">
1402 <p><code class="varname">v.clo &lt;clo_option&gt;...</code> changes one or more
1403 dynamic command line options. If no clo_option is given, lists the
1404 dynamically changeable options. See
1405 <a class="xref" href="manual-core.html#manual-core.dynopts" title="2.7.8. Dynamically Changing Options">Dynamically Change Options</a>.
1406 The below shows example of changing the value of <code class="varname">--vgdb-error</code>
1407 using directly the valgrind monitor command, using the equivalent GDB valgrind front end command.
1408 It also shows how a more flexible setting can be done using the GDB eval command.
1409 </p>
1410 <pre class="programlisting">
1411 (gdb) mo v.clo --vgdb-error=10
1412 ==2808839== Handling new value --vgdb-error=10 for option --vgdb-error
1413 (gdb) v v.clo --vgdb-error=11
1414 ==2808839== Handling new value --vgdb-error=11 for option --vgdb-error
1415 (gdb) set var $nnn = 15
1416 (gdb) eval "v v.clo --vgdb-error=%d", $nnn + 1
1417 ==2808839== Handling new value --vgdb-error=16 for option --vgdb-error
1418 (gdb)
1419 </pre>
1420 </li>
1421 <li class="listitem">
1422 <p><code class="varname">v.set {gdb_output | log_output |
1423 mixed_output}</code> allows redirection of the Valgrind output
1424 (e.g. the errors detected by the tool). The default setting is
1425 <code class="computeroutput">mixed_output</code>.</p>
1426 <p>With <code class="computeroutput">mixed_output</code>, the
1427 Valgrind output goes to the Valgrind log (typically stderr) while
1428 the output of the interactive GDB monitor commands (e.g.
1429 <code class="computeroutput">v.info last_error</code>)
1430 is displayed by GDB.</p>
1431 <p>With <code class="computeroutput">gdb_output</code>, both the
1432 Valgrind output and the interactive GDB monitor commands output are
1433 displayed by GDB.</p>
1434 <p>With <code class="computeroutput">log_output</code>, both the
1435 Valgrind output and the interactive GDB monitor commands output go
1436 to the Valgrind log.</p>
1437 </li>
1438 <li class="listitem">
1439 <p><code class="varname">v.wait [ms (default 0)]</code> instructs
1440 Valgrind gdbserver to sleep "ms" milli-seconds and then
1441 continue. When sent from a standalone vgdb, if this is the last
1442 command, the Valgrind process will continue the execution of the
1443 guest process. The typical usage of this is to use vgdb to send a
1444 "no-op" command to a Valgrind gdbserver so as to continue the
1445 execution of the guest process.
1446 </p>
1447 <p>The GDB valgrind front end command <code class="varname">valgrind v.wait
1448 MS</code> accepts any integer expression for its MS argument, while the
1449 monitor command accepts only integer numbers.
1450 </p>
1451 </li>
1452 <li class="listitem"><p><code class="varname">v.kill</code> requests the gdbserver to kill
1453 the process. This can be used from a standalone vgdb to properly
1454 kill a Valgrind process which is currently expecting a vgdb
1455 connection.</p></li>
1456 <li class="listitem">
1457 <p><code class="varname">v.set vgdb-error &lt;errornr&gt;</code>
1458 dynamically changes the value of the
1459 <code class="option">--vgdb-error</code> valgrind command line argument. A
1460 typical usage of this is to start with
1461 <code class="option">--vgdb-error=0</code> on the
1462 command line, then set a few breakpoints, set the vgdb-error value
1463 to a huge value and continue execution. Note that you can also change
1464 this value using e.g. <code class="varname">v.clo --vgdb-error=12</code></p>
1465 <p>The GDB valgrind front end command <code class="varname">valgrind v.set vgdb-error
1466 NUM</code> accepts any integer expression for its ERRORNR argument, while
1467 the monitor command accepts only integer numbers.
1468 </p>
1469 </li>
1470 <li class="listitem">
1471 <p><code class="varname">v.set merge-recursive-frames &lt;num&gt;</code>
1472 dynamically changes the value of the
1473 <code class="option">--merge-recursive-frames</code> valgrind command line argument.
1474 Note that you can also change
1475 this value using e.g. <code class="varname">v.clo --merge-recursive-frames=5</code></p>
1476 <p>The GDB valgrind front end command <code class="varname">valgrind v.set merge-recursive-frames
1477 NUM</code> accepts any integer expression for its NUM argument, while
1478 the monitor command accepts only integer numbers.
1479 </p>
1480 </li>
1481 <li class="listitem"><p><code class="varname">xtmemory [&lt;filename&gt; default xtmemory.kcg.%p.%n]</code>
1482 requests the tool (Memcheck, Massif, Helgrind) to produce an xtree heap memory report.
1483 See <a class="xref" href="manual-core.html#manual-core.xtree" title="2.10. Execution Trees">Execution Trees</a> for
1484 a detailed explanation about execution trees. </p></li>
1485 </ul></div>
1486 <p>The following Valgrind monitor commands are useful for
1487 investigating the behaviour of Valgrind or its gdbserver in case of
1488 problems or bugs.</p>
1489 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
1490 <li class="listitem"><p><code class="varname">v.do expensive_sanity_check_general</code>
1491 executes various sanity checks. In particular, the sanity of the
1492 Valgrind heap is verified. This can be useful if you suspect that
1493 your program and/or Valgrind has a bug corrupting Valgrind data
1494 structure. It can also be used when a Valgrind tool
1495 reports a client error to the connected GDB, in order to verify
1496 the sanity of Valgrind before continuing the execution.
1497 </p></li>
1498 <li class="listitem"><p><code class="varname">v.info gdbserver_status</code> shows the
1499 gdbserver status. In case of problems (e.g. of communications),
1500 this shows the values of some relevant Valgrind gdbserver internal
1501 variables. Note that the variables related to breakpoints and
1502 watchpoints (e.g. the number of breakpoint addresses and the number of
1503 watchpoints) will be zero, as GDB by default removes all
1504 watchpoints and breakpoints when execution stops, and re-inserts
1505 them when resuming the execution of the debugged process. You can
1506 change this GDB behaviour by using the GDB command
1507 <code class="computeroutput">set breakpoint always-inserted on</code>.
1508 </p></li>
1509 <li class="listitem"><p><code class="varname">v.info memory [aspacemgr]</code> shows the statistics of
1510 Valgrind's internal heap management. If
1511 option <code class="option">--profile-heap=yes</code> was given, detailed
1512 statistics will be output. With the optional argument
1513 <code class="computeroutput">aspacemgr</code>. the segment list maintained
1514 by valgrind address space manager will be output. Note that
1515 this list of segments is always output on the Valgrind log.
1516 </p></li>
1517 <li class="listitem"><p><code class="varname">v.info exectxt</code> shows information about
1518 the "executable contexts" (i.e. the stack traces) recorded by
1519 Valgrind. For some programs, Valgrind can record a very high
1520 number of such stack traces, causing a high memory usage. This
1521 monitor command shows all the recorded stack traces, followed by
1522 some statistics. This can be used to analyse the reason for having
1523 a big number of stack traces. Typically, you will use this command
1524 if <code class="varname">v.info memory</code> has shown significant memory
1525 usage by the "exectxt" arena.
1526 </p></li>
1527 <li class="listitem">
1528 <p><code class="varname">v.info scheduler</code> shows various
1529 information about threads. First, it outputs the host stack trace,
1530 i.e. the Valgrind code being executed. Then, for each thread, it
1531 outputs the thread state. For non terminated threads, the state is
1532 followed by the guest (client) stack trace. Finally, for each
1533 active thread or for each terminated thread slot not yet re-used,
1534 it shows the max usage of the valgrind stack.</p>
1535 <p>Showing the client stack traces allows to compare the stack
1536 traces produced by the Valgrind unwinder with the stack traces
1537 produced by GDB+Valgrind gdbserver. Pay attention that GDB and
1538 Valgrind scheduler status have their own thread numbering
1539 scheme. To make the link between the GDB thread number and the
1540 corresponding Valgrind scheduler thread number, use the GDB
1541 command <code class="computeroutput">info threads</code>. The output
1542 of this command shows the GDB thread number and the valgrind
1543 'tid'. The 'tid' is the thread number output
1544 by <code class="computeroutput">v.info scheduler</code>. When using
1545 the callgrind tool, the callgrind monitor command
1546 <code class="computeroutput">status</code> outputs internal callgrind
1547 information about the stack/call graph it maintains.
1548 </p>
1549 </li>
1550 <li class="listitem"><p><code class="varname">v.info stats</code> shows various valgrind core and
1551 tool statistics. With this, Valgrind and tool statistics can
1552 be examined while running, even without option <code class="option">--stats=yes</code>.
1553 </p></li>
1554 <li class="listitem">
1555 <p><code class="varname">v.info unwind &lt;addr&gt; [&lt;len&gt;]</code> shows
1556 the CFI unwind debug info for the address range [addr, addr+len-1].
1557 The default value of &lt;len&gt; is 1, giving the unwind information
1558 for the instruction at &lt;addr&gt;.
1559 </p>
1560 <p>The GDB valgrind front end command <code class="varname">valgrind v.info unwind
1561 ADDR [LEN]</code> accepts any address expression for its first ADDR
1562 argument, such as $pc. The second optional argument is any integer
1563 expression. Note that these 2 arguments must be separated by a space.
1564 </p>
1565 </li>
1566 <li class="listitem">
1567 <p><code class="varname">v.set debuglog &lt;intvalue&gt;</code> sets the
1568 Valgrind debug log level to &lt;intvalue&gt;. This allows to
1569 dynamically change the log level of Valgrind e.g. when a problem
1570 is detected.</p>
1571 <p>The GDB valgrind front end command <code class="varname">valgrind v.set debuglog
1572 LEVEL</code> accepts any address expression for its LEVEL argument.
1573 </p>
1574 </li>
1575 <li class="listitem">
1576 <p><code class="varname">v.set hostvisibility [yes*|no]</code> The value
1577 "yes" indicates to gdbserver that GDB can look at the Valgrind
1578 'host' (internal) status/memory. "no" disables this access.
1579 When hostvisibility is activated, GDB can e.g. look at Valgrind
1580 global variables. As an example, to examine a Valgrind global
1581 variable of the memcheck tool on an x86, do the following setup:</p>
1582 <pre class="screen">
1583 (gdb) monitor v.set hostvisibility yes
1584 Enabled access to Valgrind memory/status by GDB
1585 If not yet done, tell GDB which valgrind file(s) to use, typically:
1586 add-symbol-file /home/philippe/valgrind/git/improve/Inst/libexec/valgrind/memcheck-amd64-linux 0x58001000
1587 (gdb) add-symbol-file /home/philippe/valgrind/git/improve/Inst/libexec/valgrind/memcheck-amd64-linux 0x58001000
1588 add symbol table from file "/home/philippe/valgrind/git/improve/Inst/libexec/valgrind/memcheck-amd64-linux" at
1589 .text_addr = 0x58001000
1590 (y or n) y
1591 Reading symbols from /home/philippe/valgrind/git/improve/Inst/libexec/valgrind/memcheck-amd64-linux...
1592 (gdb)
1593 </pre>
1594 <p>After that, variables defined in memcheck-x86-linux can be accessed, e.g.</p>
1595 <pre class="screen">
1596 (gdb) p /x vgPlain_threads[1].os_state
1597 $3 = {lwpid = 0x4688, threadgroup = 0x4688, parent = 0x0,
1598 valgrind_stack_base = 0x62e78000, valgrind_stack_init_SP = 0x62f79fe0,
1599 exitcode = 0x0, fatalsig = 0x0}
1600 (gdb) p vex_control
1601 $5 = {iropt_verbosity = 0, iropt_level = 2,
1602 iropt_register_updates = VexRegUpdUnwindregsAtMemAccess,
1603 iropt_unroll_thresh = 120, guest_max_insns = 60, guest_chase_thresh = 10}
1604 (gdb)
1605 </pre>
1606 </li>
1607 <li class="listitem">
1608 <p><code class="varname">v.translate &lt;address&gt;
1609 [&lt;traceflags&gt;]</code> shows the translation of the block
1610 containing <code class="computeroutput">address</code> with the given
1611 trace flags. The <code class="computeroutput">traceflags</code> value
1612 bit patterns have similar meaning to Valgrind's
1613 <code class="option">--trace-flags</code> option. It can be given
1614 in hexadecimal (e.g. 0x20) or decimal (e.g. 32) or in binary 1s
1615 and 0s bit (e.g. 0b00100000). The default value of the traceflags
1616 is 0b00100000, corresponding to "show after instrumentation".
1617 The output of this command always goes to the Valgrind
1618 log.</p>
1619 <p>The additional bit flag 0b100000000 (bit 8)
1620 has no equivalent in the <code class="option">--trace-flags</code> option.
1621 It enables tracing of the gdbserver specific instrumentation. Note
1622 that this bit 8 can only enable the addition of gdbserver
1623 instrumentation in the trace. Setting it to 0 will not
1624 disable the tracing of the gdbserver instrumentation if it is
1625 active for some other reason, for example because there is a breakpoint at
1626 this address or because gdbserver is in single stepping
1627 mode.</p>
1628 <p>The GDB valgrind front end command <code class="varname">valgrind v.translate ADDR
1629 [TRACEFLAG]</code> accepts any address expression for its first ADDR
1630 argument, such as $pc. The second optional argument is any integer
1631 expression. Note that these 2 arguments must be separated by a space.
1632 </p>
1633 </li>
1634 </ul></div>
1635 </div>
1636 </div>
1637 <div class="sect1">
1638 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
1639 <a name="manual-core-adv.wrapping"></a>3.3. Function wrapping</h2></div></div></div>
1640 <p>
1641 Valgrind allows calls to some specified functions to be intercepted and
1642 rerouted to a different, user-supplied function. This can do whatever it
1643 likes, typically examining the arguments, calling onwards to the original,
1644 and possibly examining the result. Any number of functions may be
1645 wrapped.</p>
1646 <p>
1647 Function wrapping is useful for instrumenting an API in some way. For
1648 example, Helgrind wraps functions in the POSIX pthreads API so it can know
1649 about thread status changes, and the core is able to wrap
1650 functions in the MPI (message-passing) API so it can know
1651 of memory status changes associated with message arrival/departure.
1652 Such information is usually passed to Valgrind by using client
1653 requests in the wrapper functions, although the exact mechanism may vary.
1654 </p>
1655 <div class="sect2">
1656 <div class="titlepage"><div><div><h3 class="title">
1657 <a name="manual-core-adv.wrapping.example"></a>3.3.1. A Simple Example</h3></div></div></div>
1658 <p>Supposing we want to wrap some function</p>
1659 <pre class="programlisting">
1660 int foo ( int x, int y ) { return x + y; }</pre>
1661 <p>A wrapper is a function of identical type, but with a special name
1662 which identifies it as the wrapper for <code class="computeroutput">foo</code>.
1663 Wrappers need to include
1664 supporting macros from <code class="filename">valgrind.h</code>.
1665 Here is a simple wrapper which prints the arguments and return value:</p>
1666 <pre class="programlisting">
1667 #include &lt;stdio.h&gt;
1668 #include "valgrind.h"
1669 int I_WRAP_SONAME_FNNAME_ZU(NONE,foo)( int x, int y )
1670 {
1671 int result;
1672 OrigFn fn;
1673 VALGRIND_GET_ORIG_FN(fn);
1674 printf("foo's wrapper: args %d %d\n", x, y);
1675 CALL_FN_W_WW(result, fn, x,y);
1676 printf("foo's wrapper: result %d\n", result);
1677 return result;
1678 }
1679 </pre>
1680 <p>To become active, the wrapper merely needs to be present in a text
1681 section somewhere in the same process' address space as the function
1682 it wraps, and for its ELF symbol name to be visible to Valgrind. In
1683 practice, this means either compiling to a
1684 <code class="computeroutput">.o</code> and linking it in, or
1685 compiling to a <code class="computeroutput">.so</code> and
1686 <code class="computeroutput">LD_PRELOAD</code>ing it in. The latter is more
1687 convenient in that it doesn't require relinking.</p>
1688 <p>All wrappers have approximately the above form. There are three
1689 crucial macros:</p>
1690 <p><code class="computeroutput">I_WRAP_SONAME_FNNAME_ZU</code>:
1691 this generates the real name of the wrapper.
1692 This is an encoded name which Valgrind notices when reading symbol
1693 table information. What it says is: I am the wrapper for any function
1694 named <code class="computeroutput">foo</code> which is found in
1695 an ELF shared object with an empty
1696 ("<code class="computeroutput">NONE</code>") soname field. The specification
1697 mechanism is powerful in
1698 that wildcards are allowed for both sonames and function names.
1699 The details are discussed below.</p>
1700 <p><code class="computeroutput">VALGRIND_GET_ORIG_FN</code>:
1701 once in the wrapper, the first priority is
1702 to get hold of the address of the original (and any other supporting
1703 information needed). This is stored in a value of opaque
1704 type <code class="computeroutput">OrigFn</code>.
1705 The information is acquired using
1706 <code class="computeroutput">VALGRIND_GET_ORIG_FN</code>. It is crucial
1707 to make this macro call before calling any other wrapped function
1708 in the same thread.</p>
1709 <p><code class="computeroutput">CALL_FN_W_WW</code>: eventually we will
1710 want to call the function being
1711 wrapped. Calling it directly does not work, since that just gets us
1712 back to the wrapper and leads to an infinite loop. Instead, the result
1713 lvalue,
1714 <code class="computeroutput">OrigFn</code> and arguments are
1715 handed to one of a family of macros of the form
1716 <code class="computeroutput">CALL_FN_*</code>. These
1717 cause Valgrind to call the original and avoid recursion back to the
1718 wrapper.</p>
1719 </div>
1720 <div class="sect2">
1721 <div class="titlepage"><div><div><h3 class="title">
1722 <a name="manual-core-adv.wrapping.specs"></a>3.3.2. Wrapping Specifications</h3></div></div></div>
1723 <p>This scheme has the advantage of being self-contained. A library of
1724 wrappers can be compiled to object code in the normal way, and does
1725 not rely on an external script telling Valgrind which wrappers pertain
1726 to which originals.</p>
1727 <p>Each wrapper has a name which, in the most general case says: I am the
1728 wrapper for any function whose name matches FNPATT and whose ELF
1729 "soname" matches SOPATT. Both FNPATT and SOPATT may contain wildcards
1730 (asterisks) and other characters (spaces, dots, @, etc) which are not
1731 generally regarded as valid C identifier names.</p>
1732 <p>This flexibility is needed to write robust wrappers for POSIX pthread
1733 functions, where typically we are not completely sure of either the
1734 function name or the soname, or alternatively we want to wrap a whole
1735 set of functions at once.</p>
1736 <p>For example, <code class="computeroutput">pthread_create</code>
1737 in GNU libpthread is usually a
1738 versioned symbol - one whose name ends in, eg,
1739 <code class="computeroutput">@GLIBC_2.3</code>. Hence we
1740 are not sure what its real name is. We also want to cover any soname
1741 of the form <code class="computeroutput">libpthread.so*</code>.
1742 So the header of the wrapper will be</p>
1743 <pre class="programlisting">
1744 int I_WRAP_SONAME_FNNAME_ZZ(libpthreadZdsoZd0,pthreadZucreateZAZa)
1745 ( ... formals ... )
1746 { ... body ... }
1747 </pre>
1748 <p>In order to write unusual characters as valid C function names, a
1749 Z-encoding scheme is used. Names are written literally, except that
1750 a capital Z acts as an escape character, with the following encoding:</p>
1751 <pre class="programlisting">
1752 Za encodes *
1753 Zp +
1754 Zc :
1755 Zd .
1756 Zu _
1757 Zh -
1758 Zs (space)
1759 ZA @
1760 ZZ Z
1761 ZL ( # only in valgrind 3.3.0 and later
1762 ZR ) # only in valgrind 3.3.0 and later
1763 </pre>
1764 <p>Hence <code class="computeroutput">libpthreadZdsoZd0</code> is an
1765 encoding of the soname <code class="computeroutput">libpthread.so.0</code>
1766 and <code class="computeroutput">pthreadZucreateZAZa</code> is an encoding
1767 of the function name <code class="computeroutput">pthread_create@*</code>.
1768 </p>
1769 <p>The macro <code class="computeroutput">I_WRAP_SONAME_FNNAME_ZZ</code>
1770 constructs a wrapper name in which
1771 both the soname (first component) and function name (second component)
1772 are Z-encoded. Encoding the function name can be tiresome and is
1773 often unnecessary, so a second macro,
1774 <code class="computeroutput">I_WRAP_SONAME_FNNAME_ZU</code>, can be
1775 used instead. The <code class="computeroutput">_ZU</code> variant is
1776 also useful for writing wrappers for
1777 C++ functions, in which the function name is usually already mangled
1778 using some other convention in which Z plays an important role. Having
1779 to encode a second time quickly becomes confusing.</p>
1780 <p>Since the function name field may contain wildcards, it can be
1781 anything, including just <code class="computeroutput">*</code>.
1782 The same is true for the soname.
1783 However, some ELF objects - specifically, main executables - do not
1784 have sonames. Any object lacking a soname is treated as if its soname
1785 was <code class="computeroutput">NONE</code>, which is why the original
1786 example above had a name
1787 <code class="computeroutput">I_WRAP_SONAME_FNNAME_ZU(NONE,foo)</code>.</p>
1788 <p>Note that the soname of an ELF object is not the same as its
1789 file name, although it is often similar. You can find the soname of
1790 an object <code class="computeroutput">libfoo.so</code> using the command
1791 <code class="computeroutput">readelf -a libfoo.so | grep soname</code>.</p>
1792 </div>
1793 <div class="sect2">
1794 <div class="titlepage"><div><div><h3 class="title">
1795 <a name="manual-core-adv.wrapping.semantics"></a>3.3.3. Wrapping Semantics</h3></div></div></div>
1796 <p>The ability for a wrapper to replace an infinite family of functions
1797 is powerful but brings complications in situations where ELF objects
1798 appear and disappear (are dlopen'd and dlclose'd) on the fly.
1799 Valgrind tries to maintain sensible behaviour in such situations.</p>
1800 <p>For example, suppose a process has dlopened (an ELF object with
1801 soname) <code class="filename">object1.so</code>, which contains
1802 <code class="computeroutput">function1</code>. It starts to use
1803 <code class="computeroutput">function1</code> immediately.</p>
1804 <p>After a while it dlopens <code class="filename">wrappers.so</code>,
1805 which contains a wrapper
1806 for <code class="computeroutput">function1</code> in (soname)
1807 <code class="filename">object1.so</code>. All subsequent calls to
1808 <code class="computeroutput">function1</code> are rerouted to the wrapper.</p>
1809 <p>If <code class="filename">wrappers.so</code> is
1810 later dlclose'd, calls to <code class="computeroutput">function1</code> are
1811 naturally routed back to the original.</p>
1812 <p>Alternatively, if <code class="filename">object1.so</code>
1813 is dlclose'd but <code class="filename">wrappers.so</code> remains,
1814 then the wrapper exported by <code class="filename">wrappers.so</code>
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 <code class="filename">object1.so</code> is
1819 eventually dlopen'd again, the wrapper will become active again.</p>
1820 <p>In short, valgrind inspects all code loading/unloading events to
1821 ensure that the set of currently active wrappers remains consistent.</p>
1822 <p>A second possible problem is that of conflicting wrappers. It is
1823 easily possible to load two or more wrappers, both of which claim
1824 to be wrappers for some third function. In such cases Valgrind will
1825 complain about conflicting wrappers when the second one appears, and
1826 will honour only the first one.</p>
1827 </div>
1828 <div class="sect2">
1829 <div class="titlepage"><div><div><h3 class="title">
1830 <a name="manual-core-adv.wrapping.debugging"></a>3.3.4. Debugging</h3></div></div></div>
1831 <p>Figuring out what's going on given the dynamic nature of wrapping
1832 can be difficult. The
1833 <code class="option">--trace-redir=yes</code> option makes
1834 this possible
1835 by showing the complete state of the redirection subsystem after
1836 every
1837 <code class="function">mmap</code>/<code class="function">munmap</code>
1838 event affecting code (text).</p>
1839 <p>There are two central concepts:</p>
1840 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
1841 <li class="listitem"><p>A "redirection specification" is a binding of
1842 a (soname pattern, fnname pattern) pair to a code address.
1843 These bindings are created by writing functions with names
1844 made with the
1845 <code class="computeroutput">I_WRAP_SONAME_FNNAME_{ZZ,_ZU}</code>
1846 macros.</p></li>
1847 <li class="listitem"><p>An "active redirection" is a code-address to
1848 code-address binding currently in effect.</p></li>
1849 </ul></div>
1850 <p>The state of the wrapping-and-redirection subsystem comprises a set of
1851 specifications and a set of active bindings. The specifications are
1852 acquired/discarded by watching all
1853 <code class="function">mmap</code>/<code class="function">munmap</code>
1854 events on code (text)
1855 sections. The active binding set is (conceptually) recomputed from
1856 the specifications, and all known symbol names, following any change
1857 to the specification set.</p>
1858 <p><code class="option">--trace-redir=yes</code> shows the contents
1859 of both sets following any such event.</p>
1860 <p><code class="option">-v</code> prints a line of text each
1861 time an active specification is used for the first time.</p>
1862 <p>Hence for maximum debugging effectiveness you will need to use both
1863 options.</p>
1864 <p>One final comment. The function-wrapping facility is closely
1865 tied to Valgrind's ability to replace (redirect) specified
1866 functions, for example to redirect calls to
1867 <code class="function">malloc</code> to its
1868 own implementation. Indeed, a replacement function can be
1869 regarded as a wrapper function which does not call the original.
1870 However, to make the implementation more robust, the two kinds
1871 of interception (wrapping vs replacement) are treated differently.
1872 </p>
1873 <p><code class="option">--trace-redir=yes</code> shows
1874 specifications and bindings for both
1875 replacement and wrapper functions. To differentiate the
1876 two, replacement bindings are printed using
1877 <code class="computeroutput">R-&gt;</code> whereas
1878 wraps are printed using <code class="computeroutput">W-&gt;</code>.
1879 </p>
1880 </div>
1881 <div class="sect2">
1882 <div class="titlepage"><div><div><h3 class="title">
1883 <a name="manual-core-adv.wrapping.limitations-cf"></a>3.3.5. Limitations - control flow</h3></div></div></div>
1884 <p>For the most part, the function wrapping implementation is robust.
1885 The only important caveat is: in a wrapper, get hold of
1886 the <code class="computeroutput">OrigFn</code> information using
1887 <code class="computeroutput">VALGRIND_GET_ORIG_FN</code> before calling any
1888 other wrapped function. Once you have the
1889 <code class="computeroutput">OrigFn</code>, arbitrary
1890 calls between, recursion between, and longjumps out of wrappers
1891 should work correctly. There is never any interaction between wrapped
1892 functions and merely replaced functions
1893 (eg <code class="function">malloc</code>), so you can call
1894 <code class="function">malloc</code> etc safely from within wrappers.
1895 </p>
1896 <p>The above comments are true for {x86,amd64,ppc32,arm,mips32,s390}-linux.
1897 On
1898 ppc64-linux function wrapping is more fragile due to the (arguably
1899 poorly designed) ppc64-linux ABI. This mandates the use of a shadow
1900 stack which tracks entries/exits of both wrapper and replacement
1901 functions. This gives two limitations: firstly, longjumping out of
1902 wrappers will rapidly lead to disaster, since the shadow stack will
1903 not get correctly cleared. Secondly, since the shadow stack has
1904 finite size, recursion between wrapper/replacement functions is only
1905 possible to a limited depth, beyond which Valgrind has to abort the
1906 run. This depth is currently 16 calls.</p>
1907 <p>For all platforms ({x86,amd64,ppc32,ppc64,arm,mips32,s390}-linux)
1908 all the above
1909 comments apply on a per-thread basis. In other words, wrapping is
1910 thread-safe: each thread must individually observe the above
1911 restrictions, but there is no need for any kind of inter-thread
1912 cooperation.</p>
1913 </div>
1914 <div class="sect2">
1915 <div class="titlepage"><div><div><h3 class="title">
1916 <a name="manual-core-adv.wrapping.limitations-sigs"></a>3.3.6. Limitations - original function signatures</h3></div></div></div>
1917 <p>As shown in the above example, to call the original you must use a
1918 macro of the form <code class="computeroutput">CALL_FN_*</code>.
1919 For technical reasons it is impossible
1920 to create a single macro to deal with all argument types and numbers,
1921 so a family of macros covering the most common cases is supplied. In
1922 what follows, 'W' denotes a machine-word-typed value (a pointer or a
1923 C <code class="computeroutput">long</code>),
1924 and 'v' denotes C's <code class="computeroutput">void</code> type.
1925 The currently available macros are:</p>
1926 <pre class="programlisting">
1927 CALL_FN_v_v -- call an original of type void fn ( void )
1928 CALL_FN_W_v -- call an original of type long fn ( void )
1929
1930 CALL_FN_v_W -- call an original of type void fn ( long )
1931 CALL_FN_W_W -- call an original of type long fn ( long )
1932
1933 CALL_FN_v_WW -- call an original of type void fn ( long, long )
1934 CALL_FN_W_WW -- call an original of type long fn ( long, long )
1935
1936 CALL_FN_v_WWW -- call an original of type void fn ( long, long, long )
1937 CALL_FN_W_WWW -- call an original of type long fn ( long, long, long )
1938
1939 CALL_FN_W_WWWW -- call an original of type long fn ( long, long, long, long )
1940 CALL_FN_W_5W -- call an original of type long fn ( long, long, long, long, long )
1941 CALL_FN_W_6W -- call an original of type long fn ( long, long, long, long, long, long )
1942 and so on, up to
1943 CALL_FN_W_12W
1944 </pre>
1945 <p>The set of supported types can be expanded as needed. It is
1946 regrettable that this limitation exists. Function wrapping has proven
1947 difficult to implement, with a certain apparently unavoidable level of
1948 ickiness. After several implementation attempts, the present
1949 arrangement appears to be the least-worst tradeoff. At least it works
1950 reliably in the presence of dynamic linking and dynamic code
1951 loading/unloading.</p>
1952 <p>You should not attempt to wrap a function of one type signature with a
1953 wrapper of a different type signature. Such trickery will surely lead
1954 to crashes or strange behaviour. This is not a limitation
1955 of the function wrapping implementation, merely a reflection of the
1956 fact that it gives you sweeping powers to shoot yourself in the foot
1957 if you are not careful. Imagine the instant havoc you could wreak by
1958 writing a wrapper which matched any function name in any soname - in
1959 effect, one which claimed to be a wrapper for all functions in the
1960 process.</p>
1961 </div>
1962 <div class="sect2">
1963 <div class="titlepage"><div><div><h3 class="title">
1964 <a name="manual-core-adv.wrapping.examples"></a>3.3.7. Examples</h3></div></div></div>
1965 <p>In the source tree,
1966 <code class="filename">memcheck/tests/wrap[1-8].c</code> provide a series of
1967 examples, ranging from very simple to quite advanced.</p>
1968 <p><code class="filename">mpi/libmpiwrap.c</code> is an example
1969 of wrapping a big, complex API (the MPI-2 interface). This file defines
1970 almost 300 different wrappers.</p>
1971 </div>
1972 </div>
1973 </div>
1974 <div>
1975 <br><table class="nav" width="100%" cellspacing="3" cellpadding="2" border="0" summary="Navigation footer">
1976 <tr>
1977 <td rowspan="2" width="40%" align="left">
1978 <a accesskey="p" href="manual-core.html">&lt;&lt; 2. Using and understanding the Valgrind core</a> </td>
1979 <td width="20%" align="center"><a accesskey="u" href="manual.html">Up</a></td>
1980 <td rowspan="2" width="40%" align="right"> <a accesskey="n" href="mc-manual.html">4. Memcheck: a memory error detector &gt;&gt;</a>
1981 </td>
1982 </tr>
1983 <tr><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td></tr>
1984 </table>
1985 </div>
1986 </body>
1987 </html>