blob: b491f9880f6afc3c7cad0f4c2dbfb8b650cf3f73 [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
njne5eaf3a2006-10-23 21:21:48 +000013So you want to write a Valgrind tool? Here are some instructions that may
14help. They were last updated for Valgrind 3.2.2.
njn3e986b22004-11-30 10:43:45 +000015
16<sect2 id="writing-tools.tools" xreflabel="Tools">
17<title>Tools</title>
18
19<para>The key idea behind Valgrind's architecture is the division
njne5eaf3a2006-10-23 21:21:48 +000020between its "core" and "tool plug-ins".</para>
njn3e986b22004-11-30 10:43:45 +000021
22<para>The core provides the common low-level infrastructure to
toma721cec2005-11-04 14:11:05 +000023support program instrumentation, including the JIT
njn3e986b22004-11-30 10:43:45 +000024compiler, low-level memory manager, signal handling and a
25scheduler (for pthreads). It also provides certain services that
26are useful to some but not all tools, such as support for error
27recording and suppression.</para>
28
29<para>But the core leaves certain operations undefined, which
30must be filled by tools. Most notably, tools define how program
toma721cec2005-11-04 14:11:05 +000031code should be instrumented. They can also call certain
32functions to indicate to the core that they would like to use
njn3e986b22004-11-30 10:43:45 +000033certain services, or be notified when certain interesting events
34occur. But the core takes care of all the hard work.</para>
35
36</sect2>
37
njn3e986b22004-11-30 10:43:45 +000038</sect1>
39
40
41
42<sect1 id="writing-tools.writingatool" xreflabel="Writing a Tool">
43<title>Writing a Tool</title>
44
njn3e986b22004-11-30 10:43:45 +000045<sect2 id="writing-tools.howtoolswork" xreflabel="How tools work">
46<title>How tools work</title>
47
njne5eaf3a2006-10-23 21:21:48 +000048<para>Tool plug-ins must define various functions for instrumenting programs
49that are called by Valgrind's core. They are then linked against
50Valgrind's core to define a complete Valgrind tool which will be used
51when the <option>--tool</option> option is used to select it.</para>
njn3e986b22004-11-30 10:43:45 +000052
53</sect2>
54
55
56<sect2 id="writing-tools.gettingcode" xreflabel="Getting the code">
57<title>Getting the code</title>
58
njne5eaf3a2006-10-23 21:21:48 +000059<para>To write your own tool, you'll need the Valgrind source code. You'll
60need a check-out of the Subversion repository for the automake/autoconf
61build instructions to work. See the information about how to do check-out
62from the repository at <ulink url="&vg-svn-repo;">the Valgrind
debad57fc2005-12-03 22:33:29 +000063website</ulink>.</para>
njn3e986b22004-11-30 10:43:45 +000064
65</sect2>
66
67
68<sect2 id="writing-tools.gettingstarted" xreflabel="Getting started">
69<title>Getting started</title>
70
debad57fc2005-12-03 22:33:29 +000071<para>Valgrind uses GNU <computeroutput>automake</computeroutput> and
72<computeroutput>autoconf</computeroutput> for the creation of Makefiles
73and configuration. But don't worry, these instructions should be enough
74to get you started even if you know nothing about those tools.</para>
njn3e986b22004-11-30 10:43:45 +000075
76<para>In what follows, all filenames are relative to Valgrind's
77top-level directory <computeroutput>valgrind/</computeroutput>.</para>
78
79<orderedlist>
80 <listitem>
njne5eaf3a2006-10-23 21:21:48 +000081 <para>Choose a name for the tool, and a two-letter abbreviation that can
82 be used as a short prefix. We'll use
83 <computeroutput>foobar</computeroutput> and
84 <computeroutput>fb</computeroutput> as an example.</para>
njn3e986b22004-11-30 10:43:45 +000085 </listitem>
86
87 <listitem>
njne5eaf3a2006-10-23 21:21:48 +000088 <para>Make three new directories <filename>foobar/</filename>,
89 <filename>foobar/docs/</filename> and
90 <filename>foobar/tests/</filename>.
91 </para>
92 </listitem>
93
94 <listitem>
95 <para>Create empty files
96 <filename>foobar/docs/Makefile.am</filename> and
97 <filename>foobar/tests/Makefile.am</filename>.
98 </para>
njn3e986b22004-11-30 10:43:45 +000099 </listitem>
100
101 <listitem>
debad57fc2005-12-03 22:33:29 +0000102 <para>Copy <filename>none/Makefile.am</filename> into
njne5eaf3a2006-10-23 21:21:48 +0000103 <filename>foobar/</filename>. Edit it by replacing all
debad57fc2005-12-03 22:33:29 +0000104 occurrences of the string <computeroutput>"none"</computeroutput> with
njne5eaf3a2006-10-23 21:21:48 +0000105 <computeroutput>"foobar"</computeroutput>, and all occurrences of
debad57fc2005-12-03 22:33:29 +0000106 the string <computeroutput>"nl_"</computeroutput> with
njne5eaf3a2006-10-23 21:21:48 +0000107 <computeroutput>"fb_"</computeroutput>.</para>
njn3e986b22004-11-30 10:43:45 +0000108 </listitem>
109
110 <listitem>
111 <para>Copy <filename>none/nl_main.c</filename> into
112 <computeroutput>foobar/</computeroutput>, renaming it as
njne5eaf3a2006-10-23 21:21:48 +0000113 <filename>fb_main.c</filename>. Edit it by changing the
114 <computeroutput>details</computeroutput> lines in
115 <function>nl_pre_clo_init()</function> to something appropriate for the
debad57fc2005-12-03 22:33:29 +0000116 tool. These fields are used in the startup message, except for
117 <computeroutput>bug_reports_to</computeroutput> which is used if a
njne5eaf3a2006-10-23 21:21:48 +0000118 tool assertion fails. Also replace the string
119 <computeroutput>"nl_"</computeroutput> with
120 <computeroutput>"fb_"</computeroutput> again.</para>
njn3e986b22004-11-30 10:43:45 +0000121 </listitem>
122
123 <listitem>
debad57fc2005-12-03 22:33:29 +0000124 <para>Edit <filename>Makefile.am</filename>, adding the new directory
njne5eaf3a2006-10-23 21:21:48 +0000125 <filename>foobar</filename> to the
126 <computeroutput>TOOLS</computeroutput> variable.</para>
njn3e986b22004-11-30 10:43:45 +0000127 </listitem>
128
129 <listitem>
debad57fc2005-12-03 22:33:29 +0000130 <para>Edit <filename>configure.in</filename>, adding
njne5eaf3a2006-10-23 21:21:48 +0000131 <filename>foobar/Makefile</filename>,
132 <filename>foobar/docs/Makefile</filename> and
133 <filename>foobar/tests/Makefile</filename> to the
njn3e986b22004-11-30 10:43:45 +0000134 <computeroutput>AC_OUTPUT</computeroutput> list.</para>
135 </listitem>
136
137 <listitem>
138 <para>Run:</para>
139<programlisting><![CDATA[
140 autogen.sh
141 ./configure --prefix=`pwd`/inst
142 make install]]></programlisting>
143
debad57fc2005-12-03 22:33:29 +0000144 <para>It should automake, configure and compile without errors,
145 putting copies of the tool in
njne5eaf3a2006-10-23 21:21:48 +0000146 <filename>foobar/</filename> and
147 <filename>inst/lib/valgrind/</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000148 </listitem>
149
150 <listitem>
151 <para>You can test it with a command like:</para>
152<programlisting><![CDATA[
153 inst/bin/valgrind --tool=foobar date]]></programlisting>
154
155 <para>(almost any program should work;
156 <computeroutput>date</computeroutput> is just an example).
157 The output should be something like this:</para>
158<programlisting><![CDATA[
159 ==738== foobar-0.0.1, a foobarring tool for x86-linux.
160 ==738== Copyright (C) 1066AD, and GNU GPL'd, by J. Random Hacker.
161 ==738== Built with valgrind-1.1.0, a program execution monitor.
162 ==738== Copyright (C) 2000-2003, and GNU GPL'd, by Julian Seward.
163 ==738== Estimated CPU clock rate is 1400 MHz
164 ==738== For more details, rerun with: -v
165 ==738== Wed Sep 25 10:31:54 BST 2002
166 ==738==]]></programlisting>
167
168 <para>The tool does nothing except run the program
169 uninstrumented.</para>
170 </listitem>
171
172</orderedlist>
173
debad57fc2005-12-03 22:33:29 +0000174<para>These steps don't have to be followed exactly - you can choose
175different names for your source files, and use a different
176<option>--prefix</option> for
njn3e986b22004-11-30 10:43:45 +0000177<computeroutput>./configure</computeroutput>.</para>
178
debad57fc2005-12-03 22:33:29 +0000179<para>Now that we've setup, built and tested the simplest possible tool,
180onto the interesting stuff...</para>
njn3e986b22004-11-30 10:43:45 +0000181
182</sect2>
183
184
185
186<sect2 id="writing-tools.writingcode" xreflabel="Writing the Code">
187<title>Writing the code</title>
188
189<para>A tool must define at least these four functions:</para>
190<programlisting><![CDATA[
njn51d827b2005-05-09 01:02:08 +0000191 pre_clo_init()
192 post_clo_init()
193 instrument()
194 fini()]]></programlisting>
njn3e986b22004-11-30 10:43:45 +0000195
njne5eaf3a2006-10-23 21:21:48 +0000196<para>The names can be different to the above, but these are the usual
197names. The first one is registered using the macro
198<computeroutput>VG_DETERMINE_INTERFACE_VERSION</computeroutput> (which also
199checks that the core/tool interface of the tool matches that of the core).
200The last three are registered using the
sewardj7aeb10f2006-12-10 02:59:16 +0000201<computeroutput>VG_(basic_tool_funcs)</computeroutput> function.</para>
njn3e986b22004-11-30 10:43:45 +0000202
debad57fc2005-12-03 22:33:29 +0000203<para>In addition, if a tool wants to use some of the optional services
204provided by the core, it may have to define other functions and tell the
njne5eaf3a2006-10-23 21:21:48 +0000205core about them.</para>
njn3e986b22004-11-30 10:43:45 +0000206
207</sect2>
208
209
210
211<sect2 id="writing-tools.init" xreflabel="Initialisation">
212<title>Initialisation</title>
213
214<para>Most of the initialisation should be done in
debad57fc2005-12-03 22:33:29 +0000215<function>pre_clo_init()</function>. Only use
216<function>post_clo_init()</function> if a tool provides command line
217options and must do some initialisation after option processing takes
218place (<computeroutput>"clo"</computeroutput> stands for "command line
njn3e986b22004-11-30 10:43:45 +0000219options").</para>
220
debad57fc2005-12-03 22:33:29 +0000221<para>First of all, various "details" need to be set for a tool, using
222the functions <function>VG_(details_*)()</function>. Some are all
223compulsory, some aren't. Some are used when constructing the startup
224message, <computeroutput>detail_bug_reports_to</computeroutput> is used
225if <computeroutput>VG_(tool_panic)()</computeroutput> is ever called, or
226a tool assertion fails. Others have other uses.</para>
njn3e986b22004-11-30 10:43:45 +0000227
debad57fc2005-12-03 22:33:29 +0000228<para>Second, various "needs" can be set for a tool, using the functions
229<function>VG_(needs_*)()</function>. They are mostly booleans, and can
230be left untouched (they default to <varname>False</varname>). They
231determine whether a tool can do various things such as: record, report
232and suppress errors; process command line options; wrap system calls;
233record extra information about malloc'd blocks, etc.</para>
njn3e986b22004-11-30 10:43:45 +0000234
debad57fc2005-12-03 22:33:29 +0000235<para>For example, if a tool wants the core's help in recording and
236reporting errors, it must call
237<function>VG_(needs_tool_errors)</function> and provide definitions of
238eight functions for comparing errors, printing out errors, reading
239suppressions from a suppressions file, etc. While writing these
240functions requires some work, it's much less than doing error handling
241from scratch because the core is doing most of the work. See the
242function <function>VG_(needs_tool_errors)</function> in
243<filename>include/pub_tool_tooliface.h</filename> for full details of
244all the needs.</para>
njn3e986b22004-11-30 10:43:45 +0000245
debad57fc2005-12-03 22:33:29 +0000246<para>Third, the tool can indicate which events in core it wants to be
247notified about, using the functions <function>VG_(track_*)()</function>.
248These include things such as blocks of memory being malloc'd, the stack
249pointer changing, a mutex being locked, etc. If a tool wants to know
250about this, it should provide a pointer to a function, which will be
251called when that event happens.</para>
njn3e986b22004-11-30 10:43:45 +0000252
debad57fc2005-12-03 22:33:29 +0000253<para>For example, if the tool want to be notified when a new block of
254memory is malloc'd, it should call
255<function>VG_(track_new_mem_heap)()</function> with an appropriate
256function pointer, and the assigned function will be called each time
257this happens.</para>
njn3e986b22004-11-30 10:43:45 +0000258
debad57fc2005-12-03 22:33:29 +0000259<para>More information about "details", "needs" and "trackable events"
260can be found in
tomb931c582005-11-04 12:17:20 +0000261<filename>include/pub_tool_tooliface.h</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000262
263</sect2>
264
265
266
267<sect2 id="writing-tools.instr" xreflabel="Instrumentation">
268<title>Instrumentation</title>
269
debad57fc2005-12-03 22:33:29 +0000270<para><function>instrument()</function> is the interesting one. It
271allows you to instrument <emphasis>VEX IR</emphasis>, which is
njne5eaf3a2006-10-23 21:21:48 +0000272Valgrind's RISC-like intermediate language. VEX IR is best described in
273the header file <filename>VEX/pub/libvex_ir.h</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000274
toma721cec2005-11-04 14:11:05 +0000275<para>The easiest way to instrument VEX IR is to insert calls to C
njn3e986b22004-11-30 10:43:45 +0000276functions when interesting things happen. See the tool "Lackey"
debad57fc2005-12-03 22:33:29 +0000277(<filename>lackey/lk_main.c</filename>) for a simple example of this, or
278Cachegrind (<filename>cachegrind/cg_main.c</filename>) for a more
279complex example.</para>
njn3e986b22004-11-30 10:43:45 +0000280
njn3e986b22004-11-30 10:43:45 +0000281</sect2>
282
283
284
285<sect2 id="writing-tools.fini" xreflabel="Finalisation">
286<title>Finalisation</title>
287
debad57fc2005-12-03 22:33:29 +0000288<para>This is where you can present the final results, such as a summary
289of the information collected. Any log files should be written out at
290this point.</para>
njn3e986b22004-11-30 10:43:45 +0000291
292</sect2>
293
294
295
296<sect2 id="writing-tools.otherinfo" xreflabel="Other Important Information">
297<title>Other Important Information</title>
298
debad57fc2005-12-03 22:33:29 +0000299<para>Please note that the core/tool split infrastructure is quite
300complex and not brilliantly documented. Here are some important points,
301but there are undoubtedly many others that I should note but haven't
302thought of.</para>
njn3e986b22004-11-30 10:43:45 +0000303
njne5eaf3a2006-10-23 21:21:48 +0000304<para>The files <filename>include/pub_tool_*.h</filename> contain all the
305types, macros, functions, etc. that a tool should (hopefully) need, and are
306the only <filename>.h</filename> files a tool should need to
njn3e986b22004-11-30 10:43:45 +0000307<computeroutput>#include</computeroutput>.</para>
308
debad57fc2005-12-03 22:33:29 +0000309<para>In particular, you can't use anything from the C library (there
310are deep reasons for this, trust us). Valgrind provides an
311implementation of a reasonable subset of the C library, details of which
312are in <filename>pub_tool_libc*.h</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000313
debad57fc2005-12-03 22:33:29 +0000314<para>Similarly, when writing a tool, you shouldn't need to look at any
315of the code in Valgrind's core. Although it might be useful sometimes
316to help understand something.</para>
njn3e986b22004-11-30 10:43:45 +0000317
tomdf2b4a62005-11-04 12:27:58 +0000318<para>The <filename>pub_tool_*.h</filename> files have a reasonable
njn1d0825f2006-03-27 11:37:07 +0000319amount of documentation in it that should hopefully be enough to get
njne5eaf3a2006-10-23 21:21:48 +0000320you going.
321Also, <filename>VEX/pub/libvex_basictypes.h</filename> and
322<filename>VEX/pub/libvex_ir.h</filename> have some more details that are
323worth reading, particularly about VEX IR. But ultimately, the tools
324distributed (Memcheck, Cachegrind, Lackey, etc.) are probably the best
njn1d0825f2006-03-27 11:37:07 +0000325documentation of all, for the moment.</para>
njn3e986b22004-11-30 10:43:45 +0000326
toma721cec2005-11-04 14:11:05 +0000327<para>Note that the <computeroutput>VG_</computeroutput> macro is used
debad57fc2005-12-03 22:33:29 +0000328heavily. This just prepends a longer string in front of names to avoid
329potential namespace clashes.</para>
njn3e986b22004-11-30 10:43:45 +0000330
331</sect2>
332
333
334<sect2 id="writing-tools.advice" xreflabel="Words of Advice">
335<title>Words of Advice</title>
336
337<para>Writing and debugging tools is not trivial. Here are some
338suggestions for solving common problems.</para>
339
340
341<sect3 id="writing-tools.segfaults">
342<title>Segmentation Faults</title>
343
debad57fc2005-12-03 22:33:29 +0000344<para>If you are getting segmentation faults in C functions used by your
345tool, the usual GDB command:</para>
346
njn3e986b22004-11-30 10:43:45 +0000347<screen><![CDATA[
348 gdb <prog> core]]></screen>
349<para>usually gives the location of the segmentation fault.</para>
350
351</sect3>
352
353
354<sect3 id="writing-tools.debugfns">
355<title>Debugging C functions</title>
356
debad57fc2005-12-03 22:33:29 +0000357<para>If you want to debug C functions used by your tool, you can
tom605535f2005-11-29 09:59:32 +0000358achieve this by following these steps:</para>
njn3e986b22004-11-30 10:43:45 +0000359<orderedlist>
debad57fc2005-12-03 22:33:29 +0000360 <listitem>
361 <para>Set <computeroutput>VALGRIND_LAUNCHER</computeroutput> to
362 <computeroutput><![CDATA[<prefix>/bin/valgrind]]></computeroutput>:</para>
ded3c35372005-11-29 16:06:55 +0000363<programlisting>
364 export VALGRIND_LAUNCHER=/usr/local/bin/valgrind</programlisting>
debad57fc2005-12-03 22:33:29 +0000365 </listitem>
njn3e986b22004-11-30 10:43:45 +0000366
debad57fc2005-12-03 22:33:29 +0000367 <listitem>
368 <para>Then run <computeroutput><![CDATA[ gdb <prefix>/lib/valgrind/<platform>/<tool>:]]></computeroutput></para>
ded3c35372005-11-29 16:06:55 +0000369<programlisting>
370 gdb /usr/local/lib/valgrind/ppc32-linux/lackey</programlisting>
debad57fc2005-12-03 22:33:29 +0000371 </listitem>
njn3e986b22004-11-30 10:43:45 +0000372
debad57fc2005-12-03 22:33:29 +0000373 <listitem>
374 <para>Do <computeroutput>handle SIGSEGV SIGILL nostop
375 noprint</computeroutput> in GDB to prevent GDB from stopping on a
376 SIGSEGV or SIGILL:</para>
ded3c35372005-11-29 16:06:55 +0000377<programlisting>
378 (gdb) handle SIGILL SIGSEGV nostop noprint</programlisting>
debad57fc2005-12-03 22:33:29 +0000379 </listitem>
tom605535f2005-11-29 09:59:32 +0000380
debad57fc2005-12-03 22:33:29 +0000381 <listitem>
382 <para>Set any breakpoints you want and proceed as normal for GDB:</para>
ded3c35372005-11-29 16:06:55 +0000383<programlisting>
384 (gdb) b vgPlain_do_exec</programlisting>
debad57fc2005-12-03 22:33:29 +0000385 <para>The macro VG_(FUNC) is expanded to vgPlain_FUNC, so If you
386 want to set a breakpoint VG_(do_exec), you could do like this in
387 GDB.</para>
388 </listitem>
tom605535f2005-11-29 09:59:32 +0000389
debad57fc2005-12-03 22:33:29 +0000390 <listitem>
391 <para>Run the tool with required options:</para>
ded3c35372005-11-29 16:06:55 +0000392<programlisting>
393 (gdb) run `pwd`</programlisting>
debad57fc2005-12-03 22:33:29 +0000394 </listitem>
njn3e986b22004-11-30 10:43:45 +0000395
396</orderedlist>
397
debad57fc2005-12-03 22:33:29 +0000398<para>GDB may be able to give you useful information. Note that by
399default most of the system is built with
400<option>-fomit-frame-pointer</option>, and you'll need to get rid of
401this to extract useful tracebacks from GDB.</para>
njn3e986b22004-11-30 10:43:45 +0000402
403</sect3>
404
405
406<sect3 id="writing-tools.ucode-probs">
njne5eaf3a2006-10-23 21:21:48 +0000407<title>IR Instrumentation Problems</title>
njn3e986b22004-11-30 10:43:45 +0000408
njne5eaf3a2006-10-23 21:21:48 +0000409<para>If you are having problems with your VEX IR instrumentation, it's
debad57fc2005-12-03 22:33:29 +0000410likely that GDB won't be able to help at all. In this case, Valgrind's
411<option>--trace-flags</option> option is invaluable for observing the
412results of instrumentation.</para>
njn3e986b22004-11-30 10:43:45 +0000413
414</sect3>
415
416
417<sect3 id="writing-tools.misc">
418<title>Miscellaneous</title>
419
debad57fc2005-12-03 22:33:29 +0000420<para>If you just want to know whether a program point has been reached,
421using the <computeroutput>OINK</computeroutput> macro (in
422<filename>include/pub_tool_libcprint.h</filename>) can be easier than
423using GDB.</para>
njn3e986b22004-11-30 10:43:45 +0000424
debad57fc2005-12-03 22:33:29 +0000425<para>The other debugging command line options can be useful too (run
426<computeroutput>valgrind --help-debug</computeroutput> for the
njn3e986b22004-11-30 10:43:45 +0000427list).</para>
428
429</sect3>
430
431</sect2>
432
433</sect1>
434
435
436
437<sect1 id="writing-tools.advtopics" xreflabel="Advanced Topics">
438<title>Advanced Topics</title>
439
440<para>Once a tool becomes more complicated, there are some extra
441things you may want/need to do.</para>
442
443<sect2 id="writing-tools.suppressions" xreflabel="Suppressions">
444<title>Suppressions</title>
445
debad57fc2005-12-03 22:33:29 +0000446<para>If your tool reports errors and you want to suppress some common
447ones, you can add suppressions to the suppression files. The relevant
448files are <filename>valgrind/*.supp</filename>; the final suppression
449file is aggregated from these files by combining the relevant
450<filename>.supp</filename> files depending on the versions of linux, X
451and glibc on a system.</para>
njn3e986b22004-11-30 10:43:45 +0000452
453<para>Suppression types have the form
454<computeroutput>tool_name:suppression_name</computeroutput>. The
debad57fc2005-12-03 22:33:29 +0000455<computeroutput>tool_name</computeroutput> here is the name you specify
456for the tool during initialisation with
457<function>VG_(details_name)()</function>.</para>
njn3e986b22004-11-30 10:43:45 +0000458
459</sect2>
460
461
njn3e986b22004-11-30 10:43:45 +0000462<sect2 id="writing-tools.docs" xreflabel="Documentation">
463<title>Documentation</title>
464
debad57fc2005-12-03 22:33:29 +0000465<para>As of version 3.0.0, Valgrind documentation has been converted to
466XML. Why? See <ulink url="http://www.ucc.ie/xml/">The XML FAQ</ulink>.
njn3e986b22004-11-30 10:43:45 +0000467</para>
468
469
470<sect3 id="writing-tools.xml" xreflabel="The XML Toolchain">
471<title>The XML Toolchain</title>
472
473<para>If you are feeling conscientious and want to write some
474documentation for your tool, please use XML. The Valgrind
475Docs use the following toolchain and versions:</para>
476
477<programlisting>
478 xmllint: using libxml version 20607
479 xsltproc: using libxml 20607, libxslt 10102 and libexslt 802
480 pdfxmltex: pdfTeX (Web2C 7.4.5) 3.14159-1.10b
481 pdftops: version 3.00
482 DocBook: version 4.2
483</programlisting>
484
485<para><command>Latency:</command> you should note that latency is
486a big problem: DocBook is constantly being updated, but the tools
487tend to lag behind somewhat. It is important that the versions
488get on with each other, so if you decide to upgrade something,
489then you need to ascertain whether things still work nicely -
490this *cannot* be assumed.</para>
491
492<para><command>Stylesheets:</command> The Valgrind docs use
493various custom stylesheet layers, all of which are in
494<computeroutput>valgrind/docs/lib/</computeroutput>. You
495shouldn't need to modify these in any way.</para>
496
debad57fc2005-12-03 22:33:29 +0000497<para><command>Catalogs:</command> Catalogs provide a mapping from
498generic addresses to specific local directories on a given machine.
499Most recent Linux distributions have adopted a common place for storing
500catalogs (<filename>/etc/xml/</filename>). Assuming that you have the
501various tools listed above installed, you probably won't need to modify
502your catalogs. But if you do, then just add another
503<computeroutput>group</computeroutput> to this file, reflecting your
504local installation.</para>
njn3e986b22004-11-30 10:43:45 +0000505
506</sect3>
507
508
509<sect3 id="writing-tools.writing" xreflabel="Writing the Documentation">
510<title>Writing the Documentation</title>
511
512<para>Follow these steps (using <computeroutput>foobar</computeroutput>
513as the example tool name again):</para>
514
515<orderedlist>
516
517 <listitem>
njne5eaf3a2006-10-23 21:21:48 +0000518 <para>The docs go in
519 <computeroutput>valgrind/foobar/docs/</computeroutput>, which you will
520 have created when you started writing the tool.</para>
521 </listitem>
522
523 <listitem>
524 <para>Write <filename>foobar/docs/Makefile.am</filename>. Use
525 <filename>memcheck/docs/Makefile.am</filename> as an
526 example.</para>
debad57fc2005-12-03 22:33:29 +0000527 </listitem>
njn3e986b22004-11-30 10:43:45 +0000528
debad57fc2005-12-03 22:33:29 +0000529 <listitem>
530 <para>Copy the XML documentation file for the tool Nulgrind from
531 <filename>valgrind/none/docs/nl-manual.xml</filename> to
532 <computeroutput>foobar/docs/</computeroutput>, and rename it to
533 <filename>foobar/docs/fb-manual.xml</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000534
debad57fc2005-12-03 22:33:29 +0000535 <para><command>Note</command>: there is a *really stupid* tetex bug
536 with underscores in filenames, so don't use '_'.</para>
537 </listitem>
njn3e986b22004-11-30 10:43:45 +0000538
debad57fc2005-12-03 22:33:29 +0000539 <listitem>
540 <para>Write the documentation. There are some helpful bits and
541 pieces on using xml markup in
542 <filename>valgrind/docs/xml/xml_help.txt</filename>.</para>
543 </listitem>
njn3e986b22004-11-30 10:43:45 +0000544
debad57fc2005-12-03 22:33:29 +0000545 <listitem>
546 <para>Include it in the User Manual by adding the relevant entry to
547 <filename>valgrind/docs/xml/manual.xml</filename>. Copy and edit an
548 existing entry.</para>
549 </listitem>
550
551 <listitem>
552 <para>Validate <filename>foobar/docs/fb-manual.xml</filename> using
553 the following command from within <filename>valgrind/docs/</filename>:
njn3e986b22004-11-30 10:43:45 +0000554 </para>
555<screen><![CDATA[
556% make valid
557]]></screen>
558
559 <para>You will probably get errors that look like this:</para>
560
561<screen><![CDATA[
562./xml/index.xml:5: element chapter: validity error : No declaration for
563attribute base of element chapter
564]]></screen>
565
debad57fc2005-12-03 22:33:29 +0000566 <para>Ignore (only) these -- they're not important.</para>
njn3e986b22004-11-30 10:43:45 +0000567
debad57fc2005-12-03 22:33:29 +0000568 <para>Because the xml toolchain is fragile, it is important to ensure
569 that <filename>fb-manual.xml</filename> won't break the documentation
570 set build. Note that just because an xml file happily transforms to
571 html does not necessarily mean the same holds true for pdf/ps.</para>
572 </listitem>
njn3e986b22004-11-30 10:43:45 +0000573
debad57fc2005-12-03 22:33:29 +0000574 <listitem>
575 <para>You can (re-)generate the HTML docs while you are writing
576 <filename>fb-manual.xml</filename> to help you see how it's looking.
577 The generated files end up in
578 <filename>valgrind/docs/html/</filename>. Use the following
579 command, within <filename>valgrind/docs/</filename>:</para>
njn3e986b22004-11-30 10:43:45 +0000580<screen><![CDATA[
581% make html-docs
582]]></screen>
debad57fc2005-12-03 22:33:29 +0000583 </listitem>
njn3e986b22004-11-30 10:43:45 +0000584
debad57fc2005-12-03 22:33:29 +0000585 <listitem>
586 <para>When you have finished, also generate pdf and ps output to
587 check all is well, from within <filename>valgrind/docs/</filename>:
njn3e986b22004-11-30 10:43:45 +0000588 </para>
njn3e986b22004-11-30 10:43:45 +0000589<screen><![CDATA[
590% make print-docs
591]]></screen>
592
debad57fc2005-12-03 22:33:29 +0000593 <para>Check the output <filename>.pdf</filename> and
594 <filename>.ps</filename> files in
595 <computeroutput>valgrind/docs/print/</computeroutput>.</para>
596 </listitem>
njn3e986b22004-11-30 10:43:45 +0000597
598</orderedlist>
599
600</sect3>
601
602</sect2>
603
604
605<sect2 id="writing-tools.regtests" xreflabel="Regression Tests">
606<title>Regression Tests</title>
607
debad57fc2005-12-03 22:33:29 +0000608<para>Valgrind has some support for regression tests. If you want to
609write regression tests for your tool:</para>
njn3e986b22004-11-30 10:43:45 +0000610
611<orderedlist>
debad57fc2005-12-03 22:33:29 +0000612 <listitem>
njne5eaf3a2006-10-23 21:21:48 +0000613 <para>The tests go in <computeroutput>foobar/tests/</computeroutput>,
614 which you will have created when you started writing the tool.</para>
debad57fc2005-12-03 22:33:29 +0000615 </listitem>
njn3e986b22004-11-30 10:43:45 +0000616
debad57fc2005-12-03 22:33:29 +0000617 <listitem>
618 <para>Write <filename>foobar/tests/Makefile.am</filename>. Use
619 <filename>memcheck/tests/Makefile.am</filename> as an
620 example.</para>
621 </listitem>
njn3e986b22004-11-30 10:43:45 +0000622
debad57fc2005-12-03 22:33:29 +0000623 <listitem>
624 <para>Write the tests, <computeroutput>.vgtest</computeroutput> test
625 description files, <computeroutput>.stdout.exp</computeroutput> and
626 <computeroutput>.stderr.exp</computeroutput> expected output files.
627 (Note that Valgrind's output goes to stderr.) Some details on
628 writing and running tests are given in the comments at the top of
629 the testing script
630 <computeroutput>tests/vg_regtest</computeroutput>.</para>
631 </listitem>
njn3e986b22004-11-30 10:43:45 +0000632
debad57fc2005-12-03 22:33:29 +0000633 <listitem>
634 <para>Write a filter for stderr results
635 <computeroutput>foobar/tests/filter_stderr</computeroutput>. It can
636 call the existing filters in
637 <computeroutput>tests/</computeroutput>. See
638 <computeroutput>memcheck/tests/filter_stderr</computeroutput> for an
639 example; in particular note the
640 <computeroutput>$dir</computeroutput> trick that ensures the filter
641 works correctly from any directory.</para>
642 </listitem>
njn3e986b22004-11-30 10:43:45 +0000643
644</orderedlist>
645
646</sect2>
647
648
649
650<sect2 id="writing-tools.profiling" xreflabel="Profiling">
651<title>Profiling</title>
652
njnbcd75fc2005-12-19 22:48:39 +0000653<para>To profile a tool, use Cachegrind on it. Read README_DEVELOPERS for
654details on running Valgrind under Valgrind.</para>
njn31066fd2005-03-26 00:42:02 +0000655
njn3e986b22004-11-30 10:43:45 +0000656</sect2>
657
658
659
660<sect2 id="writing-tools.mkhackery" xreflabel="Other Makefile Hackery">
661<title>Other Makefile Hackery</title>
662
663<para>If you add any directories under
debad57fc2005-12-03 22:33:29 +0000664<computeroutput>valgrind/foobar/</computeroutput>, you will need to add
665an appropriate <filename>Makefile.am</filename> to it, and add a
666corresponding entry to the <computeroutput>AC_OUTPUT</computeroutput>
667list in <filename>valgrind/configure.in</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000668
669<para>If you add any scripts to your tool (see Cachegrind for an
670example) you need to add them to the
671<computeroutput>bin_SCRIPTS</computeroutput> variable in
672<filename>valgrind/foobar/Makefile.am</filename>.</para>
673
674</sect2>
675
676
677
678<sect2 id="writing-tools.ifacever" xreflabel="Core/tool Interface Versions">
679<title>Core/tool Interface Versions</title>
680
debad57fc2005-12-03 22:33:29 +0000681<para>In order to allow for the core/tool interface to evolve over time,
682Valgrind uses a basic interface versioning system. All a tool has to do
683is use the
684<computeroutput>VG_DETERMINE_INTERFACE_VERSION</computeroutput> macro
685exactly once in its code. If not, a link error will occur when the tool
686is built.</para>
njn3e986b22004-11-30 10:43:45 +0000687
njne5eaf3a2006-10-23 21:21:48 +0000688<para>The interface version number is changed when binary incompatible
689changes are made to the interface. If the core and tool has the same major
690version number X they should work together. If X doesn't match, Valgrind
691will abort execution with an explanation of the problem.</para>
njn3e986b22004-11-30 10:43:45 +0000692
debad57fc2005-12-03 22:33:29 +0000693<para>This approach was chosen so that if the interface changes in the
694future, old tools won't work and the reason will be clearly explained,
695instead of possibly crashing mysteriously. We have attempted to
696minimise the potential for binary incompatible changes by means such as
697minimising the use of naked structs in the interface.</para>
njn3e986b22004-11-30 10:43:45 +0000698
699</sect2>
700
701</sect1>
702
703
704
705<sect1 id="writing-tools.finalwords" xreflabel="Final Words">
706<title>Final Words</title>
707
njne5eaf3a2006-10-23 21:21:48 +0000708<para>The core/tool interface is not fixed. It's pretty stable these days,
709but it does change. We deliberately do not provide backward compatibility
710with old interfaces, because it is too difficult and too restrictive.
711The interface checking should catch any incompatibilities. We view this as
712a good thing -- if we had to be backward compatible with earlier versions,
713many improvements now in the system could not have been added.
714</para>
njn3e986b22004-11-30 10:43:45 +0000715
njn3e986b22004-11-30 10:43:45 +0000716
717<para>Happy programming.</para>
718
719</sect1>
720
721</chapter>