blob: 0ef164847d25eccb2c1f13209e46b7d82d70a415 [file] [log] [blame]
njn3e986b22004-11-30 10:43:45 +00001<?xml version="1.0"?> <!-- -*- sgml -*- -->
2<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
de252c6142005-11-27 04:10:00 +00003 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"
4[ <!ENTITY % vg-entities SYSTEM "vg-entities.xml"> %vg-entities; ]>
njn3e986b22004-11-30 10:43:45 +00005
debad57fc2005-12-03 22:33:29 +00006
njn3e986b22004-11-30 10:43:45 +00007<chapter id="writing-tools" xreflabel="Writing a New Valgrind Tool">
8<title>Writing a New Valgrind Tool</title>
9
10<sect1 id="writing-tools.intro" xreflabel="Introduction">
11<title>Introduction</title>
12
13<sect2 id="writing-tools.supexec" xreflabel="Supervised Execution">
14<title>Supervised Execution</title>
15
16<para>Valgrind provides a generic infrastructure for supervising
17the execution of programs. This is done by providing a way to
18instrument programs in very precise ways, making it relatively
19easy to support activities such as dynamic error detection and
20profiling.</para>
21
22<para>Although writing a tool is not easy, and requires learning
23quite a few things about Valgrind, it is much easier than
24instrumenting a program from scratch yourself.</para>
25
tomdf2b4a62005-11-04 12:27:58 +000026<para>[Nb: What follows is slightly out of date.]</para>
njnc7561b92005-06-19 01:24:32 +000027
njn3e986b22004-11-30 10:43:45 +000028</sect2>
29
30
31<sect2 id="writing-tools.tools" xreflabel="Tools">
32<title>Tools</title>
33
34<para>The key idea behind Valgrind's architecture is the division
35between its "core" and "tools".</para>
36
37<para>The core provides the common low-level infrastructure to
toma721cec2005-11-04 14:11:05 +000038support program instrumentation, including the JIT
njn3e986b22004-11-30 10:43:45 +000039compiler, low-level memory manager, signal handling and a
40scheduler (for pthreads). It also provides certain services that
41are useful to some but not all tools, such as support for error
42recording and suppression.</para>
43
44<para>But the core leaves certain operations undefined, which
45must be filled by tools. Most notably, tools define how program
toma721cec2005-11-04 14:11:05 +000046code should be instrumented. They can also call certain
47functions to indicate to the core that they would like to use
njn3e986b22004-11-30 10:43:45 +000048certain services, or be notified when certain interesting events
49occur. But the core takes care of all the hard work.</para>
50
51</sect2>
52
53
54<sect2 id="writing-tools.execspaces" xreflabel="Execution Spaces">
55<title>Execution Spaces</title>
56
57<para>An important concept to understand before writing a tool is
58that there are three spaces in which program code executes:</para>
59
60
61<orderedlist>
62
63 <listitem>
64 <para>User space: this covers most of the program's execution.
65 The tool is given the code and can instrument it any way it
66 likes, providing (more or less) total control over the
67 code.</para>
68
69 <para>Code executed in user space includes all the program
70 code, almost all of the C library (including things like the
71 dynamic linker), and almost all parts of all other
72 libraries.</para>
73 </listitem>
74
75 <listitem>
76 <para>Core space: a small proportion of the program's execution
77 takes place entirely within Valgrind's core. This includes:</para>
78 <itemizedlist>
79 <listitem>
80 <para>Dynamic memory management
81 (<computeroutput>malloc()</computeroutput> etc.)</para>
82 </listitem>
83 <listitem>
toma721cec2005-11-04 14:11:05 +000084 <para>Thread scheduling</para>
njn3e986b22004-11-30 10:43:45 +000085 </listitem>
86 <listitem>
87 <para>Signal handling</para>
88 </listitem>
89 </itemizedlist>
90
91 <para>A tool has no control over these operations; it never
92 "sees" the code doing this work and thus cannot instrument it.
93 However, the core provides hooks so a tool can be notified
tomb931c582005-11-04 12:17:20 +000094 when certain interesting events happen, for example when
njn3e986b22004-11-30 10:43:45 +000095 dynamic memory is allocated or freed, the stack pointer is
96 changed, or a pthread mutex is locked, etc.</para>
97
98 <para>Note that these hooks only notify tools of events
99 relevant to user space. For example, when the core allocates
100 some memory for its own use, the tool is not notified of this,
101 because it's not directly part of the supervised program's
102 execution.</para>
103 </listitem>
104
105 <listitem>
106 <para>Kernel space: execution in the kernel. Two kinds:</para>
107 <orderedlist>
108 <listitem>
109 <para>System calls: can't be directly observed by either
110 the tool or the core. But the core does have some idea of
111 what happens to the arguments, and it provides hooks for a
112 tool to wrap system calls.</para>
113 </listitem>
114 <listitem>
115 <para>Other: all other kernel activity (e.g. process
116 scheduling) is totally opaque and irrelevant to the
117 program.</para>
118 </listitem>
119 </orderedlist>
120 </listitem>
121
122 <listitem>
123 <para>It should be noted that a tool only has direct control
124 over code executed in user space. This is the vast majority
125 of code executed, but it is not absolutely all of it, so any
126 profiling information recorded by a tool won't be totally
127 accurate.</para>
128 </listitem>
129
130</orderedlist>
131
132</sect2>
133
134</sect1>
135
136
137
138<sect1 id="writing-tools.writingatool" xreflabel="Writing a Tool">
139<title>Writing a Tool</title>
140
141
142<sect2 id="writing-tools.whywriteatool" xreflabel="Why write a tool?">
143<title>Why write a tool?</title>
144
145<para>Before you write a tool, you should have some idea of what
146it should do. What is it you want to know about your programs of
147interest? Consider some existing tools:</para>
148
149<itemizedlist>
150
151 <listitem>
152 <para><command>memcheck</command>: among other things, performs
153 fine-grained validity and addressibility checks of every memory
154 reference performed by the program.</para>
155 </listitem>
156
157 <listitem>
158 <para><command>addrcheck</command>: performs lighterweight
159 addressibility checks of every memory reference performed by
160 the program.</para>
161 </listitem>
162
163 <listitem>
164 <para><command>cachegrind</command>: tracks every instruction
165 and memory reference to simulate instruction and data caches,
166 tracking cache accesses and misses that occur on every line in
167 the program.</para>
168 </listitem>
169
170 <listitem>
171 <para><command>helgrind</command>: tracks every memory access
172 and mutex lock/unlock to determine if a program contains any
173 data races.</para>
174 </listitem>
175
176 <listitem>
177 <para><command>lackey</command>: does simple counting of
178 various things: the number of calls to a particular function
179 (<computeroutput>_dl_runtime_resolve()</computeroutput>); the
toma721cec2005-11-04 14:11:05 +0000180 number of basic blocks, guest instructions, VEX instructions
njn3e986b22004-11-30 10:43:45 +0000181 executed; the number of branches executed and the proportion of
tomb931c582005-11-04 12:17:20 +0000182 them which were taken.</para>
njn3e986b22004-11-30 10:43:45 +0000183 </listitem>
184</itemizedlist>
185
186<para>These examples give a reasonable idea of what kinds of
187things Valgrind can be used for. The instrumentation can range
188from very lightweight (e.g. counting the number of times a
189particular function is called) to very intrusive (e.g.
190memcheck's memory checking).</para>
191
192</sect2>
193
194
195<sect2 id="writing-tools.suggestedtools" xreflabel="Suggested tools">
196<title>Suggested tools</title>
197
198<para>Here is a list of ideas we have had for tools that should
199not be too hard to implement.</para>
200
201<itemizedlist>
202 <listitem>
203 <para><command>branch profiler</command>: A machine's branch
204 prediction hardware could be simulated, and each branch
205 annotated with the number of predicted and mispredicted
206 branches. Would be implemented quite similarly to Cachegrind,
207 and could reuse the
208 <computeroutput>cg_annotate</computeroutput> script to annotate
209 source code.</para>
210
211 <para>The biggest difficulty with this is the simulation; the
212 chip-makers are very cagey about how their chips do branch
213 prediction. But implementing one or more of the basic
214 algorithms could still give good information.</para>
215 </listitem>
216
217 <listitem>
218 <para><command>coverage tool</command>: Cachegrind can already
219 be used for doing test coverage, but it's massive overkill to
220 use it just for that.</para>
221
222 <para>It would be easy to write a coverage tool that records
223 how many times each basic block was recorded. Again, the
224 <computeroutput>cg_annotate</computeroutput> script could be
225 used for annotating source code with the gathered information.
226 Although, <computeroutput>cg_annotate</computeroutput> is only
227 designed for working with single program runs. It could be
228 extended relatively easily to deal with multiple runs of a
229 program, so that the coverage of a whole test suite could be
230 determined.</para>
231
232 <para>In addition to the standard coverage information, such a
233 tool could record extra information that would help a user
234 generate test cases to exercise unexercised paths. For
235 example, for each conditional branch, the tool could record all
236 inputs to the conditional test, and print these out when
237 annotating.</para>
238 </listitem>
239
240 <listitem>
241 <para><command>run-time type checking</command>: A nice example
242 of a dynamic checker is given in this paper:</para>
243 <address>Debugging via Run-Time Type Checking
244 Alexey Loginov, Suan Hsi Yong, Susan Horwitz and Thomas Reps
245 Proceedings of Fundamental Approaches to Software Engineering
246 April 2001.
247 </address>
248
249 <para>Similar is the tool described in this paper:</para>
250 <address>Run-Time Type Checking for Binary Programs
251 Michael Burrows, Stephen N. Freund, Janet L. Wiener
252 Proceedings of the 12th International Conference on Compiler Construction (CC 2003)
253 April 2003.
254 </address>
255
256 <para>This approach can find quite a range of bugs,
257 particularly in C and C++ programs, and could be implemented
258 quite nicely as a Valgrind tool.</para>
259
260 <para>Ways to speed up this run-time type checking are
261 described in this paper:</para>
262 <address>Reducing the Overhead of Dynamic Analysis
263 Suan Hsi Yong and Susan Horwitz
264 Proceedings of Runtime Verification '02
265 July 2002.
266 </address>
267
268 <para>Valgrind's client requests could be used to pass
269 information to a tool about which elements need instrumentation
270 and which don't.</para>
271 </listitem>
272</itemizedlist>
273
274<para>We would love to hear from anyone who implements these or
275other tools.</para>
276
277</sect2>
278
279
280<sect2 id="writing-tools.howtoolswork" xreflabel="How tools work">
281<title>How tools work</title>
282
debad57fc2005-12-03 22:33:29 +0000283<para>Tools must define various functions for instrumenting programs
284that are called by Valgrind's core. They are then linked against the
285coregrind library (<filename>libcoregrind.a</filename>) that valgrind
286provides as well as the VEX library (<filename>libvex.a</filename>) that
287also comes with valgrind and provides the JIT engine.</para>
njn3e986b22004-11-30 10:43:45 +0000288
debad57fc2005-12-03 22:33:29 +0000289<para>Each tool is linked as a statically linked program and placed in
290the valgrind library directory from where valgrind will load it
291automatically when the <option>--tool</option> option is used to select
292it.</para>
njn3e986b22004-11-30 10:43:45 +0000293
294</sect2>
295
296
297<sect2 id="writing-tools.gettingcode" xreflabel="Getting the code">
298<title>Getting the code</title>
299
debad57fc2005-12-03 22:33:29 +0000300<para>To write your own tool, you'll need the Valgrind source code. A
301normal source distribution should do, although you might want to check
302out the latest code from the Subversion repository. See the information
303about how to do so at <ulink url="&vg-svn-repo;">the Valgrind
304website</ulink>.</para>
njn3e986b22004-11-30 10:43:45 +0000305
306</sect2>
307
308
309<sect2 id="writing-tools.gettingstarted" xreflabel="Getting started">
310<title>Getting started</title>
311
debad57fc2005-12-03 22:33:29 +0000312<para>Valgrind uses GNU <computeroutput>automake</computeroutput> and
313<computeroutput>autoconf</computeroutput> for the creation of Makefiles
314and configuration. But don't worry, these instructions should be enough
315to get you started even if you know nothing about those tools.</para>
njn3e986b22004-11-30 10:43:45 +0000316
317<para>In what follows, all filenames are relative to Valgrind's
318top-level directory <computeroutput>valgrind/</computeroutput>.</para>
319
320<orderedlist>
321 <listitem>
debad57fc2005-12-03 22:33:29 +0000322 <para>Choose a name for the tool, and an abbreviation that can be used
323 as a short prefix. We'll use <computeroutput>foobar</computeroutput>
324 and <computeroutput>fb</computeroutput> as an example.</para>
njn3e986b22004-11-30 10:43:45 +0000325 </listitem>
326
327 <listitem>
debad57fc2005-12-03 22:33:29 +0000328 <para>Make a new directory <computeroutput>foobar/</computeroutput>
329 which will hold the tool.</para>
njn3e986b22004-11-30 10:43:45 +0000330 </listitem>
331
332 <listitem>
debad57fc2005-12-03 22:33:29 +0000333 <para>Copy <filename>none/Makefile.am</filename> into
334 <computeroutput>foobar/</computeroutput>. Edit it by replacing all
335 occurrences of the string <computeroutput>"none"</computeroutput> with
336 <computeroutput>"foobar"</computeroutput> and the one occurrence of
337 the string <computeroutput>"nl_"</computeroutput> with
338 <computeroutput>"fb_"</computeroutput>. It might be worth trying to
339 understand this file, at least a little; you might have to do more
340 complicated things with it later on. In particular, the name of the
341 <computeroutput>foobar_SOURCES</computeroutput> variable determines
342 the name of the tool, which determines what name must be passed to the
343 <option>--tool</option> option to use the tool.</para>
njn3e986b22004-11-30 10:43:45 +0000344 </listitem>
345
346 <listitem>
347 <para>Copy <filename>none/nl_main.c</filename> into
348 <computeroutput>foobar/</computeroutput>, renaming it as
debad57fc2005-12-03 22:33:29 +0000349 <filename>fb_main.c</filename>. Edit it by changing the lines in
350 <function>pre_clo_init()</function> to something appropriate for the
351 tool. These fields are used in the startup message, except for
352 <computeroutput>bug_reports_to</computeroutput> which is used if a
353 tool assertion fails.</para>
njn3e986b22004-11-30 10:43:45 +0000354 </listitem>
355
356 <listitem>
debad57fc2005-12-03 22:33:29 +0000357 <para>Edit <filename>Makefile.am</filename>, adding the new directory
njn3e986b22004-11-30 10:43:45 +0000358 <computeroutput>foobar</computeroutput> to the
359 <computeroutput>SUBDIRS</computeroutput> variable.</para>
360 </listitem>
361
362 <listitem>
debad57fc2005-12-03 22:33:29 +0000363 <para>Edit <filename>configure.in</filename>, adding
364 <filename>foobar/Makefile</filename> to the
njn3e986b22004-11-30 10:43:45 +0000365 <computeroutput>AC_OUTPUT</computeroutput> list.</para>
366 </listitem>
367
368 <listitem>
369 <para>Run:</para>
370<programlisting><![CDATA[
371 autogen.sh
372 ./configure --prefix=`pwd`/inst
373 make install]]></programlisting>
374
debad57fc2005-12-03 22:33:29 +0000375 <para>It should automake, configure and compile without errors,
376 putting copies of the tool in
njn3e986b22004-11-30 10:43:45 +0000377 <computeroutput>foobar/</computeroutput> and
378 <computeroutput>inst/lib/valgrind/</computeroutput>.</para>
379 </listitem>
380
381 <listitem>
382 <para>You can test it with a command like:</para>
383<programlisting><![CDATA[
384 inst/bin/valgrind --tool=foobar date]]></programlisting>
385
386 <para>(almost any program should work;
387 <computeroutput>date</computeroutput> is just an example).
388 The output should be something like this:</para>
389<programlisting><![CDATA[
390 ==738== foobar-0.0.1, a foobarring tool for x86-linux.
391 ==738== Copyright (C) 1066AD, and GNU GPL'd, by J. Random Hacker.
392 ==738== Built with valgrind-1.1.0, a program execution monitor.
393 ==738== Copyright (C) 2000-2003, and GNU GPL'd, by Julian Seward.
394 ==738== Estimated CPU clock rate is 1400 MHz
395 ==738== For more details, rerun with: -v
396 ==738== Wed Sep 25 10:31:54 BST 2002
397 ==738==]]></programlisting>
398
399 <para>The tool does nothing except run the program
400 uninstrumented.</para>
401 </listitem>
402
403</orderedlist>
404
debad57fc2005-12-03 22:33:29 +0000405<para>These steps don't have to be followed exactly - you can choose
406different names for your source files, and use a different
407<option>--prefix</option> for
njn3e986b22004-11-30 10:43:45 +0000408<computeroutput>./configure</computeroutput>.</para>
409
debad57fc2005-12-03 22:33:29 +0000410<para>Now that we've setup, built and tested the simplest possible tool,
411onto the interesting stuff...</para>
njn3e986b22004-11-30 10:43:45 +0000412
413</sect2>
414
415
416
417<sect2 id="writing-tools.writingcode" xreflabel="Writing the Code">
418<title>Writing the code</title>
419
420<para>A tool must define at least these four functions:</para>
421<programlisting><![CDATA[
njn51d827b2005-05-09 01:02:08 +0000422 pre_clo_init()
423 post_clo_init()
424 instrument()
425 fini()]]></programlisting>
njn3e986b22004-11-30 10:43:45 +0000426
427<para>Also, it must use the macro
debad57fc2005-12-03 22:33:29 +0000428<computeroutput>VG_DETERMINE_INTERFACE_VERSION</computeroutput> exactly
429once in its source code. If it doesn't, you will get a link error
430involving <computeroutput>VG_(tool_interface_version)</computeroutput>.
431This macro is used to ensure the core/tool interface used by the core
432and a plugged-in tool are binary compatible.</para>
njn3e986b22004-11-30 10:43:45 +0000433
debad57fc2005-12-03 22:33:29 +0000434<para>In addition, if a tool wants to use some of the optional services
435provided by the core, it may have to define other functions and tell the
436code about them.</para>
njn3e986b22004-11-30 10:43:45 +0000437
438</sect2>
439
440
441
442<sect2 id="writing-tools.init" xreflabel="Initialisation">
443<title>Initialisation</title>
444
445<para>Most of the initialisation should be done in
debad57fc2005-12-03 22:33:29 +0000446<function>pre_clo_init()</function>. Only use
447<function>post_clo_init()</function> if a tool provides command line
448options and must do some initialisation after option processing takes
449place (<computeroutput>"clo"</computeroutput> stands for "command line
njn3e986b22004-11-30 10:43:45 +0000450options").</para>
451
debad57fc2005-12-03 22:33:29 +0000452<para>First of all, various "details" need to be set for a tool, using
453the functions <function>VG_(details_*)()</function>. Some are all
454compulsory, some aren't. Some are used when constructing the startup
455message, <computeroutput>detail_bug_reports_to</computeroutput> is used
456if <computeroutput>VG_(tool_panic)()</computeroutput> is ever called, or
457a tool assertion fails. Others have other uses.</para>
njn3e986b22004-11-30 10:43:45 +0000458
debad57fc2005-12-03 22:33:29 +0000459<para>Second, various "needs" can be set for a tool, using the functions
460<function>VG_(needs_*)()</function>. They are mostly booleans, and can
461be left untouched (they default to <varname>False</varname>). They
462determine whether a tool can do various things such as: record, report
463and suppress errors; process command line options; wrap system calls;
464record extra information about malloc'd blocks, etc.</para>
njn3e986b22004-11-30 10:43:45 +0000465
debad57fc2005-12-03 22:33:29 +0000466<para>For example, if a tool wants the core's help in recording and
467reporting errors, it must call
468<function>VG_(needs_tool_errors)</function> and provide definitions of
469eight functions for comparing errors, printing out errors, reading
470suppressions from a suppressions file, etc. While writing these
471functions requires some work, it's much less than doing error handling
472from scratch because the core is doing most of the work. See the
473function <function>VG_(needs_tool_errors)</function> in
474<filename>include/pub_tool_tooliface.h</filename> for full details of
475all the needs.</para>
njn3e986b22004-11-30 10:43:45 +0000476
debad57fc2005-12-03 22:33:29 +0000477<para>Third, the tool can indicate which events in core it wants to be
478notified about, using the functions <function>VG_(track_*)()</function>.
479These include things such as blocks of memory being malloc'd, the stack
480pointer changing, a mutex being locked, etc. If a tool wants to know
481about this, it should provide a pointer to a function, which will be
482called when that event happens.</para>
njn3e986b22004-11-30 10:43:45 +0000483
debad57fc2005-12-03 22:33:29 +0000484<para>For example, if the tool want to be notified when a new block of
485memory is malloc'd, it should call
486<function>VG_(track_new_mem_heap)()</function> with an appropriate
487function pointer, and the assigned function will be called each time
488this happens.</para>
njn3e986b22004-11-30 10:43:45 +0000489
debad57fc2005-12-03 22:33:29 +0000490<para>More information about "details", "needs" and "trackable events"
491can be found in
tomb931c582005-11-04 12:17:20 +0000492<filename>include/pub_tool_tooliface.h</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000493
494</sect2>
495
496
497
498<sect2 id="writing-tools.instr" xreflabel="Instrumentation">
499<title>Instrumentation</title>
500
debad57fc2005-12-03 22:33:29 +0000501<para><function>instrument()</function> is the interesting one. It
502allows you to instrument <emphasis>VEX IR</emphasis>, which is
503Valgrind's RISC-like intermediate language. VEX IR is described in
njn3e986b22004-11-30 10:43:45 +0000504<xref linkend="mc-tech-docs.ucode"/>.</para>
505
toma721cec2005-11-04 14:11:05 +0000506<para>The easiest way to instrument VEX IR is to insert calls to C
njn3e986b22004-11-30 10:43:45 +0000507functions when interesting things happen. See the tool "Lackey"
debad57fc2005-12-03 22:33:29 +0000508(<filename>lackey/lk_main.c</filename>) for a simple example of this, or
509Cachegrind (<filename>cachegrind/cg_main.c</filename>) for a more
510complex example.</para>
njn3e986b22004-11-30 10:43:45 +0000511
njn3e986b22004-11-30 10:43:45 +0000512</sect2>
513
514
515
516<sect2 id="writing-tools.fini" xreflabel="Finalisation">
517<title>Finalisation</title>
518
debad57fc2005-12-03 22:33:29 +0000519<para>This is where you can present the final results, such as a summary
520of the information collected. Any log files should be written out at
521this point.</para>
njn3e986b22004-11-30 10:43:45 +0000522
523</sect2>
524
525
526
527<sect2 id="writing-tools.otherinfo" xreflabel="Other Important Information">
528<title>Other Important Information</title>
529
debad57fc2005-12-03 22:33:29 +0000530<para>Please note that the core/tool split infrastructure is quite
531complex and not brilliantly documented. Here are some important points,
532but there are undoubtedly many others that I should note but haven't
533thought of.</para>
njn3e986b22004-11-30 10:43:45 +0000534
debad57fc2005-12-03 22:33:29 +0000535<para>The files <filename>include/pub_tool_*.h</filename> contain all
536the types, macros, functions, etc. that a tool should (hopefully) need,
537and are the only <filename>.h</filename> files a tool should need to
njn3e986b22004-11-30 10:43:45 +0000538<computeroutput>#include</computeroutput>.</para>
539
debad57fc2005-12-03 22:33:29 +0000540<para>In particular, you can't use anything from the C library (there
541are deep reasons for this, trust us). Valgrind provides an
542implementation of a reasonable subset of the C library, details of which
543are in <filename>pub_tool_libc*.h</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000544
debad57fc2005-12-03 22:33:29 +0000545<para>Similarly, when writing a tool, you shouldn't need to look at any
546of the code in Valgrind's core. Although it might be useful sometimes
547to help understand something.</para>
njn3e986b22004-11-30 10:43:45 +0000548
tomdf2b4a62005-11-04 12:27:58 +0000549<para>The <filename>pub_tool_*.h</filename> files have a reasonable
debad57fc2005-12-03 22:33:29 +0000550amount of documentation in it that should hopefully be enough to get you
551going. But ultimately, the tools distributed (Memcheck, Addrcheck,
552Cachegrind, Lackey, etc.) are probably the best documentation of all,
553for the moment.</para>
njn3e986b22004-11-30 10:43:45 +0000554
toma721cec2005-11-04 14:11:05 +0000555<para>Note that the <computeroutput>VG_</computeroutput> macro is used
debad57fc2005-12-03 22:33:29 +0000556heavily. This just prepends a longer string in front of names to avoid
557potential namespace clashes.</para>
njn3e986b22004-11-30 10:43:45 +0000558
559</sect2>
560
561
562<sect2 id="writing-tools.advice" xreflabel="Words of Advice">
563<title>Words of Advice</title>
564
565<para>Writing and debugging tools is not trivial. Here are some
566suggestions for solving common problems.</para>
567
568
569<sect3 id="writing-tools.segfaults">
570<title>Segmentation Faults</title>
571
debad57fc2005-12-03 22:33:29 +0000572<para>If you are getting segmentation faults in C functions used by your
573tool, the usual GDB command:</para>
574
njn3e986b22004-11-30 10:43:45 +0000575<screen><![CDATA[
576 gdb <prog> core]]></screen>
577<para>usually gives the location of the segmentation fault.</para>
578
579</sect3>
580
581
582<sect3 id="writing-tools.debugfns">
583<title>Debugging C functions</title>
584
debad57fc2005-12-03 22:33:29 +0000585<para>If you want to debug C functions used by your tool, you can
tom605535f2005-11-29 09:59:32 +0000586achieve this by following these steps:</para>
njn3e986b22004-11-30 10:43:45 +0000587<orderedlist>
debad57fc2005-12-03 22:33:29 +0000588 <listitem>
589 <para>Set <computeroutput>VALGRIND_LAUNCHER</computeroutput> to
590 <computeroutput><![CDATA[<prefix>/bin/valgrind]]></computeroutput>:</para>
ded3c35372005-11-29 16:06:55 +0000591<programlisting>
592 export VALGRIND_LAUNCHER=/usr/local/bin/valgrind</programlisting>
debad57fc2005-12-03 22:33:29 +0000593 </listitem>
njn3e986b22004-11-30 10:43:45 +0000594
debad57fc2005-12-03 22:33:29 +0000595 <listitem>
596 <para>Then run <computeroutput><![CDATA[ gdb <prefix>/lib/valgrind/<platform>/<tool>:]]></computeroutput></para>
ded3c35372005-11-29 16:06:55 +0000597<programlisting>
598 gdb /usr/local/lib/valgrind/ppc32-linux/lackey</programlisting>
debad57fc2005-12-03 22:33:29 +0000599 </listitem>
njn3e986b22004-11-30 10:43:45 +0000600
debad57fc2005-12-03 22:33:29 +0000601 <listitem>
602 <para>Do <computeroutput>handle SIGSEGV SIGILL nostop
603 noprint</computeroutput> in GDB to prevent GDB from stopping on a
604 SIGSEGV or SIGILL:</para>
ded3c35372005-11-29 16:06:55 +0000605<programlisting>
606 (gdb) handle SIGILL SIGSEGV nostop noprint</programlisting>
debad57fc2005-12-03 22:33:29 +0000607 </listitem>
tom605535f2005-11-29 09:59:32 +0000608
debad57fc2005-12-03 22:33:29 +0000609 <listitem>
610 <para>Set any breakpoints you want and proceed as normal for GDB:</para>
ded3c35372005-11-29 16:06:55 +0000611<programlisting>
612 (gdb) b vgPlain_do_exec</programlisting>
debad57fc2005-12-03 22:33:29 +0000613 <para>The macro VG_(FUNC) is expanded to vgPlain_FUNC, so If you
614 want to set a breakpoint VG_(do_exec), you could do like this in
615 GDB.</para>
616 </listitem>
tom605535f2005-11-29 09:59:32 +0000617
debad57fc2005-12-03 22:33:29 +0000618 <listitem>
619 <para>Run the tool with required options:</para>
ded3c35372005-11-29 16:06:55 +0000620<programlisting>
621 (gdb) run `pwd`</programlisting>
debad57fc2005-12-03 22:33:29 +0000622 </listitem>
njn3e986b22004-11-30 10:43:45 +0000623
624</orderedlist>
625
debad57fc2005-12-03 22:33:29 +0000626<para>GDB may be able to give you useful information. Note that by
627default most of the system is built with
628<option>-fomit-frame-pointer</option>, and you'll need to get rid of
629this to extract useful tracebacks from GDB.</para>
njn3e986b22004-11-30 10:43:45 +0000630
631</sect3>
632
633
634<sect3 id="writing-tools.ucode-probs">
635<title>UCode Instrumentation Problems</title>
636
debad57fc2005-12-03 22:33:29 +0000637<para>If you are having problems with your VEX UIR instrumentation, it's
638likely that GDB won't be able to help at all. In this case, Valgrind's
639<option>--trace-flags</option> option is invaluable for observing the
640results of instrumentation.</para>
njn3e986b22004-11-30 10:43:45 +0000641
642</sect3>
643
644
645<sect3 id="writing-tools.misc">
646<title>Miscellaneous</title>
647
debad57fc2005-12-03 22:33:29 +0000648<para>If you just want to know whether a program point has been reached,
649using the <computeroutput>OINK</computeroutput> macro (in
650<filename>include/pub_tool_libcprint.h</filename>) can be easier than
651using GDB.</para>
njn3e986b22004-11-30 10:43:45 +0000652
debad57fc2005-12-03 22:33:29 +0000653<para>The other debugging command line options can be useful too (run
654<computeroutput>valgrind --help-debug</computeroutput> for the
njn3e986b22004-11-30 10:43:45 +0000655list).</para>
656
657</sect3>
658
659</sect2>
660
661</sect1>
662
663
664
665<sect1 id="writing-tools.advtopics" xreflabel="Advanced Topics">
666<title>Advanced Topics</title>
667
668<para>Once a tool becomes more complicated, there are some extra
669things you may want/need to do.</para>
670
671<sect2 id="writing-tools.suppressions" xreflabel="Suppressions">
672<title>Suppressions</title>
673
debad57fc2005-12-03 22:33:29 +0000674<para>If your tool reports errors and you want to suppress some common
675ones, you can add suppressions to the suppression files. The relevant
676files are <filename>valgrind/*.supp</filename>; the final suppression
677file is aggregated from these files by combining the relevant
678<filename>.supp</filename> files depending on the versions of linux, X
679and glibc on a system.</para>
njn3e986b22004-11-30 10:43:45 +0000680
681<para>Suppression types have the form
682<computeroutput>tool_name:suppression_name</computeroutput>. The
debad57fc2005-12-03 22:33:29 +0000683<computeroutput>tool_name</computeroutput> here is the name you specify
684for the tool during initialisation with
685<function>VG_(details_name)()</function>.</para>
njn3e986b22004-11-30 10:43:45 +0000686
687</sect2>
688
689
njn3e986b22004-11-30 10:43:45 +0000690<sect2 id="writing-tools.docs" xreflabel="Documentation">
691<title>Documentation</title>
692
debad57fc2005-12-03 22:33:29 +0000693<para>As of version 3.0.0, Valgrind documentation has been converted to
694XML. Why? See <ulink url="http://www.ucc.ie/xml/">The XML FAQ</ulink>.
njn3e986b22004-11-30 10:43:45 +0000695</para>
696
697
698<sect3 id="writing-tools.xml" xreflabel="The XML Toolchain">
699<title>The XML Toolchain</title>
700
701<para>If you are feeling conscientious and want to write some
702documentation for your tool, please use XML. The Valgrind
703Docs use the following toolchain and versions:</para>
704
705<programlisting>
706 xmllint: using libxml version 20607
707 xsltproc: using libxml 20607, libxslt 10102 and libexslt 802
708 pdfxmltex: pdfTeX (Web2C 7.4.5) 3.14159-1.10b
709 pdftops: version 3.00
710 DocBook: version 4.2
711</programlisting>
712
713<para><command>Latency:</command> you should note that latency is
714a big problem: DocBook is constantly being updated, but the tools
715tend to lag behind somewhat. It is important that the versions
716get on with each other, so if you decide to upgrade something,
717then you need to ascertain whether things still work nicely -
718this *cannot* be assumed.</para>
719
720<para><command>Stylesheets:</command> The Valgrind docs use
721various custom stylesheet layers, all of which are in
722<computeroutput>valgrind/docs/lib/</computeroutput>. You
723shouldn't need to modify these in any way.</para>
724
debad57fc2005-12-03 22:33:29 +0000725<para><command>Catalogs:</command> Catalogs provide a mapping from
726generic addresses to specific local directories on a given machine.
727Most recent Linux distributions have adopted a common place for storing
728catalogs (<filename>/etc/xml/</filename>). Assuming that you have the
729various tools listed above installed, you probably won't need to modify
730your catalogs. But if you do, then just add another
731<computeroutput>group</computeroutput> to this file, reflecting your
732local installation.</para>
njn3e986b22004-11-30 10:43:45 +0000733
734</sect3>
735
736
737<sect3 id="writing-tools.writing" xreflabel="Writing the Documentation">
738<title>Writing the Documentation</title>
739
740<para>Follow these steps (using <computeroutput>foobar</computeroutput>
741as the example tool name again):</para>
742
743<orderedlist>
744
745 <listitem>
746 <para>Make a directory
debad57fc2005-12-03 22:33:29 +0000747 <computeroutput>valgrind/foobar/docs/</computeroutput>.</para>
748 </listitem>
njn3e986b22004-11-30 10:43:45 +0000749
debad57fc2005-12-03 22:33:29 +0000750 <listitem>
751 <para>Copy the XML documentation file for the tool Nulgrind from
752 <filename>valgrind/none/docs/nl-manual.xml</filename> to
753 <computeroutput>foobar/docs/</computeroutput>, and rename it to
754 <filename>foobar/docs/fb-manual.xml</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000755
debad57fc2005-12-03 22:33:29 +0000756 <para><command>Note</command>: there is a *really stupid* tetex bug
757 with underscores in filenames, so don't use '_'.</para>
758 </listitem>
njn3e986b22004-11-30 10:43:45 +0000759
debad57fc2005-12-03 22:33:29 +0000760 <listitem>
761 <para>Write the documentation. There are some helpful bits and
762 pieces on using xml markup in
763 <filename>valgrind/docs/xml/xml_help.txt</filename>.</para>
764 </listitem>
njn3e986b22004-11-30 10:43:45 +0000765
debad57fc2005-12-03 22:33:29 +0000766 <listitem>
767 <para>Include it in the User Manual by adding the relevant entry to
768 <filename>valgrind/docs/xml/manual.xml</filename>. Copy and edit an
769 existing entry.</para>
770 </listitem>
771
772 <listitem>
773 <para>Validate <filename>foobar/docs/fb-manual.xml</filename> using
774 the following command from within <filename>valgrind/docs/</filename>:
njn3e986b22004-11-30 10:43:45 +0000775 </para>
776<screen><![CDATA[
777% make valid
778]]></screen>
779
780 <para>You will probably get errors that look like this:</para>
781
782<screen><![CDATA[
783./xml/index.xml:5: element chapter: validity error : No declaration for
784attribute base of element chapter
785]]></screen>
786
debad57fc2005-12-03 22:33:29 +0000787 <para>Ignore (only) these -- they're not important.</para>
njn3e986b22004-11-30 10:43:45 +0000788
debad57fc2005-12-03 22:33:29 +0000789 <para>Because the xml toolchain is fragile, it is important to ensure
790 that <filename>fb-manual.xml</filename> won't break the documentation
791 set build. Note that just because an xml file happily transforms to
792 html does not necessarily mean the same holds true for pdf/ps.</para>
793 </listitem>
njn3e986b22004-11-30 10:43:45 +0000794
debad57fc2005-12-03 22:33:29 +0000795 <listitem>
796 <para>You can (re-)generate the HTML docs while you are writing
797 <filename>fb-manual.xml</filename> to help you see how it's looking.
798 The generated files end up in
799 <filename>valgrind/docs/html/</filename>. Use the following
800 command, within <filename>valgrind/docs/</filename>:</para>
njn3e986b22004-11-30 10:43:45 +0000801<screen><![CDATA[
802% make html-docs
803]]></screen>
debad57fc2005-12-03 22:33:29 +0000804 </listitem>
njn3e986b22004-11-30 10:43:45 +0000805
debad57fc2005-12-03 22:33:29 +0000806 <listitem>
807 <para>When you have finished, also generate pdf and ps output to
808 check all is well, from within <filename>valgrind/docs/</filename>:
njn3e986b22004-11-30 10:43:45 +0000809 </para>
njn3e986b22004-11-30 10:43:45 +0000810<screen><![CDATA[
811% make print-docs
812]]></screen>
813
debad57fc2005-12-03 22:33:29 +0000814 <para>Check the output <filename>.pdf</filename> and
815 <filename>.ps</filename> files in
816 <computeroutput>valgrind/docs/print/</computeroutput>.</para>
817 </listitem>
njn3e986b22004-11-30 10:43:45 +0000818
819</orderedlist>
820
821</sect3>
822
823</sect2>
824
825
826<sect2 id="writing-tools.regtests" xreflabel="Regression Tests">
827<title>Regression Tests</title>
828
debad57fc2005-12-03 22:33:29 +0000829<para>Valgrind has some support for regression tests. If you want to
830write regression tests for your tool:</para>
njn3e986b22004-11-30 10:43:45 +0000831
832<orderedlist>
debad57fc2005-12-03 22:33:29 +0000833 <listitem>
834 <para>Make a directory
835 <computeroutput>foobar/tests/</computeroutput>. Make sure the name
836 of the directory is <computeroutput>tests/</computeroutput> as the
837 build system assumes that any tests for the tool will be in a
838 directory by that name.</para>
839 </listitem>
njn3e986b22004-11-30 10:43:45 +0000840
debad57fc2005-12-03 22:33:29 +0000841 <listitem>
842 <para>Edit <filename>configure.in</filename>, adding
843 <filename>foobar/tests/Makefile</filename> to the
844 <computeroutput>AC_OUTPUT</computeroutput> list.</para>
845 </listitem>
njn3e986b22004-11-30 10:43:45 +0000846
debad57fc2005-12-03 22:33:29 +0000847 <listitem>
848 <para>Write <filename>foobar/tests/Makefile.am</filename>. Use
849 <filename>memcheck/tests/Makefile.am</filename> as an
850 example.</para>
851 </listitem>
njn3e986b22004-11-30 10:43:45 +0000852
debad57fc2005-12-03 22:33:29 +0000853 <listitem>
854 <para>Write the tests, <computeroutput>.vgtest</computeroutput> test
855 description files, <computeroutput>.stdout.exp</computeroutput> and
856 <computeroutput>.stderr.exp</computeroutput> expected output files.
857 (Note that Valgrind's output goes to stderr.) Some details on
858 writing and running tests are given in the comments at the top of
859 the testing script
860 <computeroutput>tests/vg_regtest</computeroutput>.</para>
861 </listitem>
njn3e986b22004-11-30 10:43:45 +0000862
debad57fc2005-12-03 22:33:29 +0000863 <listitem>
864 <para>Write a filter for stderr results
865 <computeroutput>foobar/tests/filter_stderr</computeroutput>. It can
866 call the existing filters in
867 <computeroutput>tests/</computeroutput>. See
868 <computeroutput>memcheck/tests/filter_stderr</computeroutput> for an
869 example; in particular note the
870 <computeroutput>$dir</computeroutput> trick that ensures the filter
871 works correctly from any directory.</para>
872 </listitem>
njn3e986b22004-11-30 10:43:45 +0000873
874</orderedlist>
875
876</sect2>
877
878
879
880<sect2 id="writing-tools.profiling" xreflabel="Profiling">
881<title>Profiling</title>
882
debad57fc2005-12-03 22:33:29 +0000883<para>Nb: as of 25-Mar-2005, the profiling is broken, and has been for a
884long time...</para>
njn31066fd2005-03-26 00:42:02 +0000885
njn3e986b22004-11-30 10:43:45 +0000886<para>To do simple tick-based profiling of a tool, include the
887line:</para>
888<programlisting><![CDATA[
889 #include "vg_profile.c"]]></programlisting>
njn3e986b22004-11-30 10:43:45 +0000890
debad57fc2005-12-03 22:33:29 +0000891<para>in the tool somewhere, and rebuild (you may have to
892<computeroutput>make clean</computeroutput> first). Then run Valgrind
893with the <option>--profile=yes</option> option.</para>
894
895<para>The profiler is stack-based; you can register a profiling event
896with <function>VG_(register_profile_event)()</function> and then use the
897<computeroutput>VGP_PUSHCC</computeroutput> and
898<computeroutput>VGP_POPCC</computeroutput> macros to record time spent
899doing certain things. New profiling event numbers must not overlap with
900the core profiling event numbers. See
901<filename>include/pub_tool_profile.h</filename> for details and Memcheck
902for an example.</para>
njn3e986b22004-11-30 10:43:45 +0000903
904</sect2>
905
906
907
908<sect2 id="writing-tools.mkhackery" xreflabel="Other Makefile Hackery">
909<title>Other Makefile Hackery</title>
910
911<para>If you add any directories under
debad57fc2005-12-03 22:33:29 +0000912<computeroutput>valgrind/foobar/</computeroutput>, you will need to add
913an appropriate <filename>Makefile.am</filename> to it, and add a
914corresponding entry to the <computeroutput>AC_OUTPUT</computeroutput>
915list in <filename>valgrind/configure.in</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000916
917<para>If you add any scripts to your tool (see Cachegrind for an
918example) you need to add them to the
919<computeroutput>bin_SCRIPTS</computeroutput> variable in
920<filename>valgrind/foobar/Makefile.am</filename>.</para>
921
922</sect2>
923
924
925
926<sect2 id="writing-tools.ifacever" xreflabel="Core/tool Interface Versions">
927<title>Core/tool Interface Versions</title>
928
debad57fc2005-12-03 22:33:29 +0000929<para>In order to allow for the core/tool interface to evolve over time,
930Valgrind uses a basic interface versioning system. All a tool has to do
931is use the
932<computeroutput>VG_DETERMINE_INTERFACE_VERSION</computeroutput> macro
933exactly once in its code. If not, a link error will occur when the tool
934is built.</para>
njn3e986b22004-11-30 10:43:45 +0000935
debad57fc2005-12-03 22:33:29 +0000936<para>The interface version number has the form X.Y. Changes in Y
937indicate binary compatible changes. Changes in X indicate binary
938incompatible changes. If the core and tool has the same major version
939number X they should work together. If X doesn't match, Valgrind will
940abort execution with an explanation of the problem.</para>
njn3e986b22004-11-30 10:43:45 +0000941
debad57fc2005-12-03 22:33:29 +0000942<para>This approach was chosen so that if the interface changes in the
943future, old tools won't work and the reason will be clearly explained,
944instead of possibly crashing mysteriously. We have attempted to
945minimise the potential for binary incompatible changes by means such as
946minimising the use of naked structs in the interface.</para>
njn3e986b22004-11-30 10:43:45 +0000947
948</sect2>
949
950</sect1>
951
952
953
954<sect1 id="writing-tools.finalwords" xreflabel="Final Words">
955<title>Final Words</title>
956
957<para>This whole core/tool business is under active development,
958although it's slowly maturing.</para>
959
debad57fc2005-12-03 22:33:29 +0000960<para>The first consequence of this is that the core/tool interface will
961continue to change in the future; we have no intention of freezing it
962and then regretting the inevitable stupidities. Hopefully most of the
963future changes will be to add new features, hooks, functions, etc,
964rather than to change old ones, which should cause a minimum of trouble
965for existing tools, and we've put some effort into future-proofing the
966interface to avoid binary incompatibility. But we can't guarantee
967anything. The versioning system should catch any incompatibilities.
968Just something to be aware of.</para>
njn3e986b22004-11-30 10:43:45 +0000969
debad57fc2005-12-03 22:33:29 +0000970<para>The second consequence of this is that we'd love to hear your
971feedback about it:</para>
njn3e986b22004-11-30 10:43:45 +0000972
973<itemizedlist>
debad57fc2005-12-03 22:33:29 +0000974 <listitem>
975 <para>If you love it or hate it</para>
976 </listitem>
977 <listitem>
978 <para>If you find bugs</para>
979 </listitem>
980 <listitem>
981 <para>If you write a tool</para>
982 </listitem>
983 <listitem>
984 <para>If you have suggestions for new features, needs, trackable
985 events, functions</para>
986 </listitem>
987 <listitem>
988 <para>If you have suggestions for making tools easier to
989 write</para>
990 </listitem>
991 <listitem>
992 <para>If you have suggestions for improving this
993 documentation</para>
994 </listitem>
995 <listitem>
996 <para>If you don't understand something</para>
997 </listitem>
njn3e986b22004-11-30 10:43:45 +0000998</itemizedlist>
999
1000<para>or anything else!</para>
1001
1002<para>Happy programming.</para>
1003
1004</sect1>
1005
1006</chapter>