blob: 55da11522f193a896f33e8e9aeea92a9b1f0dead [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
njn8663d222007-02-16 11:57:18 +0000126 <computeroutput>TOOLS</computeroutput> and
127 <computeroutput>SUBDIRS</computeroutput>variables.</para>
njn3e986b22004-11-30 10:43:45 +0000128 </listitem>
129
130 <listitem>
debad57fc2005-12-03 22:33:29 +0000131 <para>Edit <filename>configure.in</filename>, adding
njne5eaf3a2006-10-23 21:21:48 +0000132 <filename>foobar/Makefile</filename>,
133 <filename>foobar/docs/Makefile</filename> and
134 <filename>foobar/tests/Makefile</filename> to the
njn3e986b22004-11-30 10:43:45 +0000135 <computeroutput>AC_OUTPUT</computeroutput> list.</para>
136 </listitem>
137
138 <listitem>
139 <para>Run:</para>
140<programlisting><![CDATA[
141 autogen.sh
142 ./configure --prefix=`pwd`/inst
143 make install]]></programlisting>
144
debad57fc2005-12-03 22:33:29 +0000145 <para>It should automake, configure and compile without errors,
146 putting copies of the tool in
njne5eaf3a2006-10-23 21:21:48 +0000147 <filename>foobar/</filename> and
148 <filename>inst/lib/valgrind/</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000149 </listitem>
150
151 <listitem>
152 <para>You can test it with a command like:</para>
153<programlisting><![CDATA[
154 inst/bin/valgrind --tool=foobar date]]></programlisting>
155
156 <para>(almost any program should work;
157 <computeroutput>date</computeroutput> is just an example).
158 The output should be something like this:</para>
159<programlisting><![CDATA[
160 ==738== foobar-0.0.1, a foobarring tool for x86-linux.
161 ==738== Copyright (C) 1066AD, and GNU GPL'd, by J. Random Hacker.
162 ==738== Built with valgrind-1.1.0, a program execution monitor.
163 ==738== Copyright (C) 2000-2003, and GNU GPL'd, by Julian Seward.
164 ==738== Estimated CPU clock rate is 1400 MHz
165 ==738== For more details, rerun with: -v
166 ==738== Wed Sep 25 10:31:54 BST 2002
167 ==738==]]></programlisting>
168
169 <para>The tool does nothing except run the program
170 uninstrumented.</para>
171 </listitem>
172
173</orderedlist>
174
debad57fc2005-12-03 22:33:29 +0000175<para>These steps don't have to be followed exactly - you can choose
176different names for your source files, and use a different
177<option>--prefix</option> for
njn3e986b22004-11-30 10:43:45 +0000178<computeroutput>./configure</computeroutput>.</para>
179
debad57fc2005-12-03 22:33:29 +0000180<para>Now that we've setup, built and tested the simplest possible tool,
181onto the interesting stuff...</para>
njn3e986b22004-11-30 10:43:45 +0000182
183</sect2>
184
185
186
187<sect2 id="writing-tools.writingcode" xreflabel="Writing the Code">
188<title>Writing the code</title>
189
190<para>A tool must define at least these four functions:</para>
191<programlisting><![CDATA[
njn51d827b2005-05-09 01:02:08 +0000192 pre_clo_init()
193 post_clo_init()
194 instrument()
195 fini()]]></programlisting>
njn3e986b22004-11-30 10:43:45 +0000196
njne5eaf3a2006-10-23 21:21:48 +0000197<para>The names can be different to the above, but these are the usual
198names. The first one is registered using the macro
199<computeroutput>VG_DETERMINE_INTERFACE_VERSION</computeroutput> (which also
200checks that the core/tool interface of the tool matches that of the core).
201The last three are registered using the
sewardj7aeb10f2006-12-10 02:59:16 +0000202<computeroutput>VG_(basic_tool_funcs)</computeroutput> function.</para>
njn3e986b22004-11-30 10:43:45 +0000203
debad57fc2005-12-03 22:33:29 +0000204<para>In addition, if a tool wants to use some of the optional services
205provided by the core, it may have to define other functions and tell the
njne5eaf3a2006-10-23 21:21:48 +0000206core about them.</para>
njn3e986b22004-11-30 10:43:45 +0000207
208</sect2>
209
210
211
212<sect2 id="writing-tools.init" xreflabel="Initialisation">
213<title>Initialisation</title>
214
215<para>Most of the initialisation should be done in
debad57fc2005-12-03 22:33:29 +0000216<function>pre_clo_init()</function>. Only use
217<function>post_clo_init()</function> if a tool provides command line
218options and must do some initialisation after option processing takes
219place (<computeroutput>"clo"</computeroutput> stands for "command line
njn3e986b22004-11-30 10:43:45 +0000220options").</para>
221
debad57fc2005-12-03 22:33:29 +0000222<para>First of all, various "details" need to be set for a tool, using
223the functions <function>VG_(details_*)()</function>. Some are all
224compulsory, some aren't. Some are used when constructing the startup
225message, <computeroutput>detail_bug_reports_to</computeroutput> is used
226if <computeroutput>VG_(tool_panic)()</computeroutput> is ever called, or
227a tool assertion fails. Others have other uses.</para>
njn3e986b22004-11-30 10:43:45 +0000228
debad57fc2005-12-03 22:33:29 +0000229<para>Second, various "needs" can be set for a tool, using the functions
230<function>VG_(needs_*)()</function>. They are mostly booleans, and can
231be left untouched (they default to <varname>False</varname>). They
232determine whether a tool can do various things such as: record, report
233and suppress errors; process command line options; wrap system calls;
234record extra information about malloc'd blocks, etc.</para>
njn3e986b22004-11-30 10:43:45 +0000235
debad57fc2005-12-03 22:33:29 +0000236<para>For example, if a tool wants the core's help in recording and
237reporting errors, it must call
238<function>VG_(needs_tool_errors)</function> and provide definitions of
239eight functions for comparing errors, printing out errors, reading
240suppressions from a suppressions file, etc. While writing these
241functions requires some work, it's much less than doing error handling
242from scratch because the core is doing most of the work. See the
243function <function>VG_(needs_tool_errors)</function> in
244<filename>include/pub_tool_tooliface.h</filename> for full details of
245all the needs.</para>
njn3e986b22004-11-30 10:43:45 +0000246
debad57fc2005-12-03 22:33:29 +0000247<para>Third, the tool can indicate which events in core it wants to be
248notified about, using the functions <function>VG_(track_*)()</function>.
249These include things such as blocks of memory being malloc'd, the stack
250pointer changing, a mutex being locked, etc. If a tool wants to know
251about this, it should provide a pointer to a function, which will be
252called when that event happens.</para>
njn3e986b22004-11-30 10:43:45 +0000253
debad57fc2005-12-03 22:33:29 +0000254<para>For example, if the tool want to be notified when a new block of
255memory is malloc'd, it should call
256<function>VG_(track_new_mem_heap)()</function> with an appropriate
257function pointer, and the assigned function will be called each time
258this happens.</para>
njn3e986b22004-11-30 10:43:45 +0000259
debad57fc2005-12-03 22:33:29 +0000260<para>More information about "details", "needs" and "trackable events"
261can be found in
tomb931c582005-11-04 12:17:20 +0000262<filename>include/pub_tool_tooliface.h</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000263
264</sect2>
265
266
267
268<sect2 id="writing-tools.instr" xreflabel="Instrumentation">
269<title>Instrumentation</title>
270
debad57fc2005-12-03 22:33:29 +0000271<para><function>instrument()</function> is the interesting one. It
272allows you to instrument <emphasis>VEX IR</emphasis>, which is
njne5eaf3a2006-10-23 21:21:48 +0000273Valgrind's RISC-like intermediate language. VEX IR is best described in
274the header file <filename>VEX/pub/libvex_ir.h</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000275
toma721cec2005-11-04 14:11:05 +0000276<para>The easiest way to instrument VEX IR is to insert calls to C
njn3e986b22004-11-30 10:43:45 +0000277functions when interesting things happen. See the tool "Lackey"
debad57fc2005-12-03 22:33:29 +0000278(<filename>lackey/lk_main.c</filename>) for a simple example of this, or
279Cachegrind (<filename>cachegrind/cg_main.c</filename>) for a more
280complex example.</para>
njn3e986b22004-11-30 10:43:45 +0000281
njn3e986b22004-11-30 10:43:45 +0000282</sect2>
283
284
285
286<sect2 id="writing-tools.fini" xreflabel="Finalisation">
287<title>Finalisation</title>
288
debad57fc2005-12-03 22:33:29 +0000289<para>This is where you can present the final results, such as a summary
290of the information collected. Any log files should be written out at
291this point.</para>
njn3e986b22004-11-30 10:43:45 +0000292
293</sect2>
294
295
296
297<sect2 id="writing-tools.otherinfo" xreflabel="Other Important Information">
298<title>Other Important Information</title>
299
debad57fc2005-12-03 22:33:29 +0000300<para>Please note that the core/tool split infrastructure is quite
301complex and not brilliantly documented. Here are some important points,
302but there are undoubtedly many others that I should note but haven't
303thought of.</para>
njn3e986b22004-11-30 10:43:45 +0000304
njne5eaf3a2006-10-23 21:21:48 +0000305<para>The files <filename>include/pub_tool_*.h</filename> contain all the
306types, macros, functions, etc. that a tool should (hopefully) need, and are
307the only <filename>.h</filename> files a tool should need to
njn3e986b22004-11-30 10:43:45 +0000308<computeroutput>#include</computeroutput>.</para>
309
debad57fc2005-12-03 22:33:29 +0000310<para>In particular, you can't use anything from the C library (there
311are deep reasons for this, trust us). Valgrind provides an
312implementation of a reasonable subset of the C library, details of which
313are in <filename>pub_tool_libc*.h</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000314
debad57fc2005-12-03 22:33:29 +0000315<para>Similarly, when writing a tool, you shouldn't need to look at any
316of the code in Valgrind's core. Although it might be useful sometimes
317to help understand something.</para>
njn3e986b22004-11-30 10:43:45 +0000318
tomdf2b4a62005-11-04 12:27:58 +0000319<para>The <filename>pub_tool_*.h</filename> files have a reasonable
njn1d0825f2006-03-27 11:37:07 +0000320amount of documentation in it that should hopefully be enough to get
njne5eaf3a2006-10-23 21:21:48 +0000321you going.
322Also, <filename>VEX/pub/libvex_basictypes.h</filename> and
323<filename>VEX/pub/libvex_ir.h</filename> have some more details that are
324worth reading, particularly about VEX IR. But ultimately, the tools
325distributed (Memcheck, Cachegrind, Lackey, etc.) are probably the best
njn1d0825f2006-03-27 11:37:07 +0000326documentation of all, for the moment.</para>
njn3e986b22004-11-30 10:43:45 +0000327
toma721cec2005-11-04 14:11:05 +0000328<para>Note that the <computeroutput>VG_</computeroutput> macro is used
debad57fc2005-12-03 22:33:29 +0000329heavily. This just prepends a longer string in front of names to avoid
330potential namespace clashes.</para>
njn3e986b22004-11-30 10:43:45 +0000331
332</sect2>
333
334
335<sect2 id="writing-tools.advice" xreflabel="Words of Advice">
336<title>Words of Advice</title>
337
338<para>Writing and debugging tools is not trivial. Here are some
339suggestions for solving common problems.</para>
340
341
342<sect3 id="writing-tools.segfaults">
343<title>Segmentation Faults</title>
344
debad57fc2005-12-03 22:33:29 +0000345<para>If you are getting segmentation faults in C functions used by your
346tool, the usual GDB command:</para>
347
njn3e986b22004-11-30 10:43:45 +0000348<screen><![CDATA[
349 gdb <prog> core]]></screen>
350<para>usually gives the location of the segmentation fault.</para>
351
352</sect3>
353
354
355<sect3 id="writing-tools.debugfns">
356<title>Debugging C functions</title>
357
debad57fc2005-12-03 22:33:29 +0000358<para>If you want to debug C functions used by your tool, you can
tom605535f2005-11-29 09:59:32 +0000359achieve this by following these steps:</para>
njn3e986b22004-11-30 10:43:45 +0000360<orderedlist>
debad57fc2005-12-03 22:33:29 +0000361 <listitem>
362 <para>Set <computeroutput>VALGRIND_LAUNCHER</computeroutput> to
363 <computeroutput><![CDATA[<prefix>/bin/valgrind]]></computeroutput>:</para>
ded3c35372005-11-29 16:06:55 +0000364<programlisting>
365 export VALGRIND_LAUNCHER=/usr/local/bin/valgrind</programlisting>
debad57fc2005-12-03 22:33:29 +0000366 </listitem>
njn3e986b22004-11-30 10:43:45 +0000367
debad57fc2005-12-03 22:33:29 +0000368 <listitem>
369 <para>Then run <computeroutput><![CDATA[ gdb <prefix>/lib/valgrind/<platform>/<tool>:]]></computeroutput></para>
ded3c35372005-11-29 16:06:55 +0000370<programlisting>
371 gdb /usr/local/lib/valgrind/ppc32-linux/lackey</programlisting>
debad57fc2005-12-03 22:33:29 +0000372 </listitem>
njn3e986b22004-11-30 10:43:45 +0000373
debad57fc2005-12-03 22:33:29 +0000374 <listitem>
375 <para>Do <computeroutput>handle SIGSEGV SIGILL nostop
376 noprint</computeroutput> in GDB to prevent GDB from stopping on a
377 SIGSEGV or SIGILL:</para>
ded3c35372005-11-29 16:06:55 +0000378<programlisting>
379 (gdb) handle SIGILL SIGSEGV nostop noprint</programlisting>
debad57fc2005-12-03 22:33:29 +0000380 </listitem>
tom605535f2005-11-29 09:59:32 +0000381
debad57fc2005-12-03 22:33:29 +0000382 <listitem>
383 <para>Set any breakpoints you want and proceed as normal for GDB:</para>
ded3c35372005-11-29 16:06:55 +0000384<programlisting>
385 (gdb) b vgPlain_do_exec</programlisting>
debad57fc2005-12-03 22:33:29 +0000386 <para>The macro VG_(FUNC) is expanded to vgPlain_FUNC, so If you
387 want to set a breakpoint VG_(do_exec), you could do like this in
388 GDB.</para>
389 </listitem>
tom605535f2005-11-29 09:59:32 +0000390
debad57fc2005-12-03 22:33:29 +0000391 <listitem>
392 <para>Run the tool with required options:</para>
ded3c35372005-11-29 16:06:55 +0000393<programlisting>
394 (gdb) run `pwd`</programlisting>
debad57fc2005-12-03 22:33:29 +0000395 </listitem>
njn3e986b22004-11-30 10:43:45 +0000396
397</orderedlist>
398
debad57fc2005-12-03 22:33:29 +0000399<para>GDB may be able to give you useful information. Note that by
400default most of the system is built with
401<option>-fomit-frame-pointer</option>, and you'll need to get rid of
402this to extract useful tracebacks from GDB.</para>
njn3e986b22004-11-30 10:43:45 +0000403
404</sect3>
405
406
407<sect3 id="writing-tools.ucode-probs">
njne5eaf3a2006-10-23 21:21:48 +0000408<title>IR Instrumentation Problems</title>
njn3e986b22004-11-30 10:43:45 +0000409
njne5eaf3a2006-10-23 21:21:48 +0000410<para>If you are having problems with your VEX IR instrumentation, it's
debad57fc2005-12-03 22:33:29 +0000411likely that GDB won't be able to help at all. In this case, Valgrind's
412<option>--trace-flags</option> option is invaluable for observing the
413results of instrumentation.</para>
njn3e986b22004-11-30 10:43:45 +0000414
415</sect3>
416
417
418<sect3 id="writing-tools.misc">
419<title>Miscellaneous</title>
420
debad57fc2005-12-03 22:33:29 +0000421<para>If you just want to know whether a program point has been reached,
422using the <computeroutput>OINK</computeroutput> macro (in
423<filename>include/pub_tool_libcprint.h</filename>) can be easier than
424using GDB.</para>
njn3e986b22004-11-30 10:43:45 +0000425
debad57fc2005-12-03 22:33:29 +0000426<para>The other debugging command line options can be useful too (run
427<computeroutput>valgrind --help-debug</computeroutput> for the
njn3e986b22004-11-30 10:43:45 +0000428list).</para>
429
430</sect3>
431
432</sect2>
433
434</sect1>
435
436
437
438<sect1 id="writing-tools.advtopics" xreflabel="Advanced Topics">
439<title>Advanced Topics</title>
440
441<para>Once a tool becomes more complicated, there are some extra
442things you may want/need to do.</para>
443
444<sect2 id="writing-tools.suppressions" xreflabel="Suppressions">
445<title>Suppressions</title>
446
debad57fc2005-12-03 22:33:29 +0000447<para>If your tool reports errors and you want to suppress some common
448ones, you can add suppressions to the suppression files. The relevant
449files are <filename>valgrind/*.supp</filename>; the final suppression
450file is aggregated from these files by combining the relevant
451<filename>.supp</filename> files depending on the versions of linux, X
452and glibc on a system.</para>
njn3e986b22004-11-30 10:43:45 +0000453
454<para>Suppression types have the form
455<computeroutput>tool_name:suppression_name</computeroutput>. The
debad57fc2005-12-03 22:33:29 +0000456<computeroutput>tool_name</computeroutput> here is the name you specify
457for the tool during initialisation with
458<function>VG_(details_name)()</function>.</para>
njn3e986b22004-11-30 10:43:45 +0000459
460</sect2>
461
462
njn3e986b22004-11-30 10:43:45 +0000463<sect2 id="writing-tools.docs" xreflabel="Documentation">
464<title>Documentation</title>
465
debad57fc2005-12-03 22:33:29 +0000466<para>As of version 3.0.0, Valgrind documentation has been converted to
467XML. Why? See <ulink url="http://www.ucc.ie/xml/">The XML FAQ</ulink>.
njn3e986b22004-11-30 10:43:45 +0000468</para>
469
470
471<sect3 id="writing-tools.xml" xreflabel="The XML Toolchain">
472<title>The XML Toolchain</title>
473
474<para>If you are feeling conscientious and want to write some
475documentation for your tool, please use XML. The Valgrind
476Docs use the following toolchain and versions:</para>
477
478<programlisting>
479 xmllint: using libxml version 20607
480 xsltproc: using libxml 20607, libxslt 10102 and libexslt 802
481 pdfxmltex: pdfTeX (Web2C 7.4.5) 3.14159-1.10b
482 pdftops: version 3.00
483 DocBook: version 4.2
484</programlisting>
485
486<para><command>Latency:</command> you should note that latency is
487a big problem: DocBook is constantly being updated, but the tools
488tend to lag behind somewhat. It is important that the versions
489get on with each other, so if you decide to upgrade something,
490then you need to ascertain whether things still work nicely -
491this *cannot* be assumed.</para>
492
493<para><command>Stylesheets:</command> The Valgrind docs use
494various custom stylesheet layers, all of which are in
495<computeroutput>valgrind/docs/lib/</computeroutput>. You
496shouldn't need to modify these in any way.</para>
497
debad57fc2005-12-03 22:33:29 +0000498<para><command>Catalogs:</command> Catalogs provide a mapping from
499generic addresses to specific local directories on a given machine.
500Most recent Linux distributions have adopted a common place for storing
501catalogs (<filename>/etc/xml/</filename>). Assuming that you have the
502various tools listed above installed, you probably won't need to modify
503your catalogs. But if you do, then just add another
504<computeroutput>group</computeroutput> to this file, reflecting your
505local installation.</para>
njn3e986b22004-11-30 10:43:45 +0000506
507</sect3>
508
509
510<sect3 id="writing-tools.writing" xreflabel="Writing the Documentation">
511<title>Writing the Documentation</title>
512
513<para>Follow these steps (using <computeroutput>foobar</computeroutput>
514as the example tool name again):</para>
515
516<orderedlist>
517
518 <listitem>
njne5eaf3a2006-10-23 21:21:48 +0000519 <para>The docs go in
520 <computeroutput>valgrind/foobar/docs/</computeroutput>, which you will
521 have created when you started writing the tool.</para>
522 </listitem>
523
524 <listitem>
525 <para>Write <filename>foobar/docs/Makefile.am</filename>. Use
526 <filename>memcheck/docs/Makefile.am</filename> as an
527 example.</para>
debad57fc2005-12-03 22:33:29 +0000528 </listitem>
njn3e986b22004-11-30 10:43:45 +0000529
debad57fc2005-12-03 22:33:29 +0000530 <listitem>
531 <para>Copy the XML documentation file for the tool Nulgrind from
532 <filename>valgrind/none/docs/nl-manual.xml</filename> to
533 <computeroutput>foobar/docs/</computeroutput>, and rename it to
534 <filename>foobar/docs/fb-manual.xml</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000535
debad57fc2005-12-03 22:33:29 +0000536 <para><command>Note</command>: there is a *really stupid* tetex bug
537 with underscores in filenames, so don't use '_'.</para>
538 </listitem>
njn3e986b22004-11-30 10:43:45 +0000539
debad57fc2005-12-03 22:33:29 +0000540 <listitem>
541 <para>Write the documentation. There are some helpful bits and
542 pieces on using xml markup in
543 <filename>valgrind/docs/xml/xml_help.txt</filename>.</para>
544 </listitem>
njn3e986b22004-11-30 10:43:45 +0000545
debad57fc2005-12-03 22:33:29 +0000546 <listitem>
547 <para>Include it in the User Manual by adding the relevant entry to
548 <filename>valgrind/docs/xml/manual.xml</filename>. Copy and edit an
549 existing entry.</para>
550 </listitem>
551
552 <listitem>
553 <para>Validate <filename>foobar/docs/fb-manual.xml</filename> using
554 the following command from within <filename>valgrind/docs/</filename>:
njn3e986b22004-11-30 10:43:45 +0000555 </para>
556<screen><![CDATA[
557% make valid
558]]></screen>
559
560 <para>You will probably get errors that look like this:</para>
561
562<screen><![CDATA[
563./xml/index.xml:5: element chapter: validity error : No declaration for
564attribute base of element chapter
565]]></screen>
566
debad57fc2005-12-03 22:33:29 +0000567 <para>Ignore (only) these -- they're not important.</para>
njn3e986b22004-11-30 10:43:45 +0000568
debad57fc2005-12-03 22:33:29 +0000569 <para>Because the xml toolchain is fragile, it is important to ensure
570 that <filename>fb-manual.xml</filename> won't break the documentation
571 set build. Note that just because an xml file happily transforms to
572 html does not necessarily mean the same holds true for pdf/ps.</para>
573 </listitem>
njn3e986b22004-11-30 10:43:45 +0000574
debad57fc2005-12-03 22:33:29 +0000575 <listitem>
576 <para>You can (re-)generate the HTML docs while you are writing
577 <filename>fb-manual.xml</filename> to help you see how it's looking.
578 The generated files end up in
579 <filename>valgrind/docs/html/</filename>. Use the following
580 command, within <filename>valgrind/docs/</filename>:</para>
njn3e986b22004-11-30 10:43:45 +0000581<screen><![CDATA[
582% make html-docs
583]]></screen>
debad57fc2005-12-03 22:33:29 +0000584 </listitem>
njn3e986b22004-11-30 10:43:45 +0000585
debad57fc2005-12-03 22:33:29 +0000586 <listitem>
587 <para>When you have finished, also generate pdf and ps output to
588 check all is well, from within <filename>valgrind/docs/</filename>:
njn3e986b22004-11-30 10:43:45 +0000589 </para>
njn3e986b22004-11-30 10:43:45 +0000590<screen><![CDATA[
591% make print-docs
592]]></screen>
593
debad57fc2005-12-03 22:33:29 +0000594 <para>Check the output <filename>.pdf</filename> and
595 <filename>.ps</filename> files in
596 <computeroutput>valgrind/docs/print/</computeroutput>.</para>
597 </listitem>
njn3e986b22004-11-30 10:43:45 +0000598
599</orderedlist>
600
601</sect3>
602
603</sect2>
604
605
606<sect2 id="writing-tools.regtests" xreflabel="Regression Tests">
607<title>Regression Tests</title>
608
debad57fc2005-12-03 22:33:29 +0000609<para>Valgrind has some support for regression tests. If you want to
610write regression tests for your tool:</para>
njn3e986b22004-11-30 10:43:45 +0000611
612<orderedlist>
debad57fc2005-12-03 22:33:29 +0000613 <listitem>
njne5eaf3a2006-10-23 21:21:48 +0000614 <para>The tests go in <computeroutput>foobar/tests/</computeroutput>,
615 which you will have created when you started writing the tool.</para>
debad57fc2005-12-03 22:33:29 +0000616 </listitem>
njn3e986b22004-11-30 10:43:45 +0000617
debad57fc2005-12-03 22:33:29 +0000618 <listitem>
619 <para>Write <filename>foobar/tests/Makefile.am</filename>. Use
620 <filename>memcheck/tests/Makefile.am</filename> as an
621 example.</para>
622 </listitem>
njn3e986b22004-11-30 10:43:45 +0000623
debad57fc2005-12-03 22:33:29 +0000624 <listitem>
625 <para>Write the tests, <computeroutput>.vgtest</computeroutput> test
626 description files, <computeroutput>.stdout.exp</computeroutput> and
627 <computeroutput>.stderr.exp</computeroutput> expected output files.
628 (Note that Valgrind's output goes to stderr.) Some details on
629 writing and running tests are given in the comments at the top of
630 the testing script
631 <computeroutput>tests/vg_regtest</computeroutput>.</para>
632 </listitem>
njn3e986b22004-11-30 10:43:45 +0000633
debad57fc2005-12-03 22:33:29 +0000634 <listitem>
635 <para>Write a filter for stderr results
636 <computeroutput>foobar/tests/filter_stderr</computeroutput>. It can
637 call the existing filters in
638 <computeroutput>tests/</computeroutput>. See
639 <computeroutput>memcheck/tests/filter_stderr</computeroutput> for an
640 example; in particular note the
641 <computeroutput>$dir</computeroutput> trick that ensures the filter
642 works correctly from any directory.</para>
643 </listitem>
njn3e986b22004-11-30 10:43:45 +0000644
645</orderedlist>
646
647</sect2>
648
649
650
651<sect2 id="writing-tools.profiling" xreflabel="Profiling">
652<title>Profiling</title>
653
njnbcd75fc2005-12-19 22:48:39 +0000654<para>To profile a tool, use Cachegrind on it. Read README_DEVELOPERS for
655details on running Valgrind under Valgrind.</para>
njn31066fd2005-03-26 00:42:02 +0000656
njn3e986b22004-11-30 10:43:45 +0000657</sect2>
658
659
660
661<sect2 id="writing-tools.mkhackery" xreflabel="Other Makefile Hackery">
662<title>Other Makefile Hackery</title>
663
664<para>If you add any directories under
debad57fc2005-12-03 22:33:29 +0000665<computeroutput>valgrind/foobar/</computeroutput>, you will need to add
666an appropriate <filename>Makefile.am</filename> to it, and add a
667corresponding entry to the <computeroutput>AC_OUTPUT</computeroutput>
668list in <filename>valgrind/configure.in</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000669
670<para>If you add any scripts to your tool (see Cachegrind for an
671example) you need to add them to the
672<computeroutput>bin_SCRIPTS</computeroutput> variable in
673<filename>valgrind/foobar/Makefile.am</filename>.</para>
674
675</sect2>
676
677
678
679<sect2 id="writing-tools.ifacever" xreflabel="Core/tool Interface Versions">
680<title>Core/tool Interface Versions</title>
681
debad57fc2005-12-03 22:33:29 +0000682<para>In order to allow for the core/tool interface to evolve over time,
683Valgrind uses a basic interface versioning system. All a tool has to do
684is use the
685<computeroutput>VG_DETERMINE_INTERFACE_VERSION</computeroutput> macro
686exactly once in its code. If not, a link error will occur when the tool
687is built.</para>
njn3e986b22004-11-30 10:43:45 +0000688
njne5eaf3a2006-10-23 21:21:48 +0000689<para>The interface version number is changed when binary incompatible
690changes are made to the interface. If the core and tool has the same major
691version number X they should work together. If X doesn't match, Valgrind
692will abort execution with an explanation of the problem.</para>
njn3e986b22004-11-30 10:43:45 +0000693
debad57fc2005-12-03 22:33:29 +0000694<para>This approach was chosen so that if the interface changes in the
695future, old tools won't work and the reason will be clearly explained,
696instead of possibly crashing mysteriously. We have attempted to
697minimise the potential for binary incompatible changes by means such as
698minimising the use of naked structs in the interface.</para>
njn3e986b22004-11-30 10:43:45 +0000699
700</sect2>
701
702</sect1>
703
704
705
706<sect1 id="writing-tools.finalwords" xreflabel="Final Words">
707<title>Final Words</title>
708
njne5eaf3a2006-10-23 21:21:48 +0000709<para>The core/tool interface is not fixed. It's pretty stable these days,
710but it does change. We deliberately do not provide backward compatibility
711with old interfaces, because it is too difficult and too restrictive.
712The interface checking should catch any incompatibilities. We view this as
713a good thing -- if we had to be backward compatible with earlier versions,
714many improvements now in the system could not have been added.
715</para>
njn3e986b22004-11-30 10:43:45 +0000716
njn3e986b22004-11-30 10:43:45 +0000717
718<para>Happy programming.</para>
719
720</sect1>
721
722</chapter>