blob: a78254cccb3c677c3145a9f332aaa53cb9c19a00 [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
njn117a6b82008-02-03 22:35:21 +0000126 <computeroutput>TOOLS</computeroutput> or
127 <computeroutput>EXP_TOOLS</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.
njnd57b78d2007-11-27 01:59:02 +0000161 ==738== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
162 ==738== Using LibVEX rev 1791, a library for dynamic binary translation.
163 ==738== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
164 ==738== Using valgrind-3.3.0, a dynamic binary instrumentation framework.
165 ==738== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
njn3e986b22004-11-30 10:43:45 +0000166 ==738== For more details, rerun with: -v
njnd57b78d2007-11-27 01:59:02 +0000167 ==738==
168 Tue Nov 27 12:40:49 EST 2007
njn3e986b22004-11-30 10:43:45 +0000169 ==738==]]></programlisting>
170
njnd57b78d2007-11-27 01:59:02 +0000171 <para>The tool does nothing except run the program uninstrumented.</para>
njn3e986b22004-11-30 10:43:45 +0000172 </listitem>
173
174</orderedlist>
175
debad57fc2005-12-03 22:33:29 +0000176<para>These steps don't have to be followed exactly - you can choose
177different names for your source files, and use a different
178<option>--prefix</option> for
njn3e986b22004-11-30 10:43:45 +0000179<computeroutput>./configure</computeroutput>.</para>
180
debad57fc2005-12-03 22:33:29 +0000181<para>Now that we've setup, built and tested the simplest possible tool,
182onto the interesting stuff...</para>
njn3e986b22004-11-30 10:43:45 +0000183
184</sect2>
185
186
187
188<sect2 id="writing-tools.writingcode" xreflabel="Writing the Code">
189<title>Writing the code</title>
190
191<para>A tool must define at least these four functions:</para>
192<programlisting><![CDATA[
njn51d827b2005-05-09 01:02:08 +0000193 pre_clo_init()
194 post_clo_init()
195 instrument()
196 fini()]]></programlisting>
njn3e986b22004-11-30 10:43:45 +0000197
njne5eaf3a2006-10-23 21:21:48 +0000198<para>The names can be different to the above, but these are the usual
199names. The first one is registered using the macro
200<computeroutput>VG_DETERMINE_INTERFACE_VERSION</computeroutput> (which also
201checks that the core/tool interface of the tool matches that of the core).
202The last three are registered using the
sewardj7aeb10f2006-12-10 02:59:16 +0000203<computeroutput>VG_(basic_tool_funcs)</computeroutput> function.</para>
njn3e986b22004-11-30 10:43:45 +0000204
debad57fc2005-12-03 22:33:29 +0000205<para>In addition, if a tool wants to use some of the optional services
206provided by the core, it may have to define other functions and tell the
njne5eaf3a2006-10-23 21:21:48 +0000207core about them.</para>
njn3e986b22004-11-30 10:43:45 +0000208
209</sect2>
210
211
212
213<sect2 id="writing-tools.init" xreflabel="Initialisation">
214<title>Initialisation</title>
215
216<para>Most of the initialisation should be done in
debad57fc2005-12-03 22:33:29 +0000217<function>pre_clo_init()</function>. Only use
218<function>post_clo_init()</function> if a tool provides command line
219options and must do some initialisation after option processing takes
220place (<computeroutput>"clo"</computeroutput> stands for "command line
njn3e986b22004-11-30 10:43:45 +0000221options").</para>
222
debad57fc2005-12-03 22:33:29 +0000223<para>First of all, various "details" need to be set for a tool, using
224the functions <function>VG_(details_*)()</function>. Some are all
225compulsory, some aren't. Some are used when constructing the startup
226message, <computeroutput>detail_bug_reports_to</computeroutput> is used
227if <computeroutput>VG_(tool_panic)()</computeroutput> is ever called, or
228a tool assertion fails. Others have other uses.</para>
njn3e986b22004-11-30 10:43:45 +0000229
debad57fc2005-12-03 22:33:29 +0000230<para>Second, various "needs" can be set for a tool, using the functions
231<function>VG_(needs_*)()</function>. They are mostly booleans, and can
232be left untouched (they default to <varname>False</varname>). They
233determine whether a tool can do various things such as: record, report
234and suppress errors; process command line options; wrap system calls;
235record extra information about malloc'd blocks, etc.</para>
njn3e986b22004-11-30 10:43:45 +0000236
debad57fc2005-12-03 22:33:29 +0000237<para>For example, if a tool wants the core's help in recording and
238reporting errors, it must call
239<function>VG_(needs_tool_errors)</function> and provide definitions of
240eight functions for comparing errors, printing out errors, reading
241suppressions from a suppressions file, etc. While writing these
242functions requires some work, it's much less than doing error handling
243from scratch because the core is doing most of the work. See the
244function <function>VG_(needs_tool_errors)</function> in
245<filename>include/pub_tool_tooliface.h</filename> for full details of
246all the needs.</para>
njn3e986b22004-11-30 10:43:45 +0000247
debad57fc2005-12-03 22:33:29 +0000248<para>Third, the tool can indicate which events in core it wants to be
249notified about, using the functions <function>VG_(track_*)()</function>.
250These include things such as blocks of memory being malloc'd, the stack
251pointer changing, a mutex being locked, etc. If a tool wants to know
252about this, it should provide a pointer to a function, which will be
253called when that event happens.</para>
njn3e986b22004-11-30 10:43:45 +0000254
debad57fc2005-12-03 22:33:29 +0000255<para>For example, if the tool want to be notified when a new block of
256memory is malloc'd, it should call
257<function>VG_(track_new_mem_heap)()</function> with an appropriate
258function pointer, and the assigned function will be called each time
259this happens.</para>
njn3e986b22004-11-30 10:43:45 +0000260
debad57fc2005-12-03 22:33:29 +0000261<para>More information about "details", "needs" and "trackable events"
262can be found in
tomb931c582005-11-04 12:17:20 +0000263<filename>include/pub_tool_tooliface.h</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000264
265</sect2>
266
267
268
269<sect2 id="writing-tools.instr" xreflabel="Instrumentation">
270<title>Instrumentation</title>
271
debad57fc2005-12-03 22:33:29 +0000272<para><function>instrument()</function> is the interesting one. It
273allows you to instrument <emphasis>VEX IR</emphasis>, which is
njnd57b78d2007-11-27 01:59:02 +0000274Valgrind's RISC-like intermediate language. VEX IR is described fairly well
275in the comments of the header file
276<filename>VEX/pub/libvex_ir.h</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000277
toma721cec2005-11-04 14:11:05 +0000278<para>The easiest way to instrument VEX IR is to insert calls to C
njn3e986b22004-11-30 10:43:45 +0000279functions when interesting things happen. See the tool "Lackey"
debad57fc2005-12-03 22:33:29 +0000280(<filename>lackey/lk_main.c</filename>) for a simple example of this, or
281Cachegrind (<filename>cachegrind/cg_main.c</filename>) for a more
282complex example.</para>
njn3e986b22004-11-30 10:43:45 +0000283
njn3e986b22004-11-30 10:43:45 +0000284</sect2>
285
286
287
288<sect2 id="writing-tools.fini" xreflabel="Finalisation">
289<title>Finalisation</title>
290
debad57fc2005-12-03 22:33:29 +0000291<para>This is where you can present the final results, such as a summary
292of the information collected. Any log files should be written out at
293this point.</para>
njn3e986b22004-11-30 10:43:45 +0000294
295</sect2>
296
297
298
299<sect2 id="writing-tools.otherinfo" xreflabel="Other Important Information">
300<title>Other Important Information</title>
301
debad57fc2005-12-03 22:33:29 +0000302<para>Please note that the core/tool split infrastructure is quite
303complex and not brilliantly documented. Here are some important points,
304but there are undoubtedly many others that I should note but haven't
305thought of.</para>
njn3e986b22004-11-30 10:43:45 +0000306
njne5eaf3a2006-10-23 21:21:48 +0000307<para>The files <filename>include/pub_tool_*.h</filename> contain all the
308types, macros, functions, etc. that a tool should (hopefully) need, and are
309the only <filename>.h</filename> files a tool should need to
njnd57b78d2007-11-27 01:59:02 +0000310<computeroutput>#include</computeroutput>. They have a reasonable amount of
311documentation in it that should hopefully be enough to get you going.</para>
njn3e986b22004-11-30 10:43:45 +0000312
njnd57b78d2007-11-27 01:59:02 +0000313<para>Note that you can't use anything from the C library (there
debad57fc2005-12-03 22:33:29 +0000314are deep reasons for this, trust us). Valgrind provides an
315implementation of a reasonable subset of the C library, details of which
316are in <filename>pub_tool_libc*.h</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000317
njnd57b78d2007-11-27 01:59:02 +0000318<para>When writing a tool, you shouldn't need to look at any of the code in
319Valgrind's core. Although it might be useful sometimes to help understand
320something.</para>
njn3e986b22004-11-30 10:43:45 +0000321
njnd57b78d2007-11-27 01:59:02 +0000322<para>The <filename>include/pub_tool_basics.h</filename> and
323<filename>VEX/pub/libvex_basictypes.h</filename> files file have some basic
324types that are widely used.</para>
325
326<para>Ultimately, the tools distributed (Memcheck, Cachegrind, Lackey, etc.)
327are probably the best documentation of all, for the moment.</para>
njn3e986b22004-11-30 10:43:45 +0000328
toma721cec2005-11-04 14:11:05 +0000329<para>Note that the <computeroutput>VG_</computeroutput> macro is used
debad57fc2005-12-03 22:33:29 +0000330heavily. This just prepends a longer string in front of names to avoid
njnd57b78d2007-11-27 01:59:02 +0000331potential namespace clashes. It is defined in
332<filename>include/pub_tool_basics_asm.h</filename>.</para>
333
334<para>There are some assorted notes about various aspects of the
335implementation in <filename>docs/internals/</filename>. Much of it
336isn't that relevant to tool-writers, however.</para>
njn3e986b22004-11-30 10:43:45 +0000337
338</sect2>
339
340
341<sect2 id="writing-tools.advice" xreflabel="Words of Advice">
342<title>Words of Advice</title>
343
344<para>Writing and debugging tools is not trivial. Here are some
345suggestions for solving common problems.</para>
346
347
348<sect3 id="writing-tools.segfaults">
349<title>Segmentation Faults</title>
350
debad57fc2005-12-03 22:33:29 +0000351<para>If you are getting segmentation faults in C functions used by your
352tool, the usual GDB command:</para>
353
njn3e986b22004-11-30 10:43:45 +0000354<screen><![CDATA[
355 gdb <prog> core]]></screen>
356<para>usually gives the location of the segmentation fault.</para>
357
358</sect3>
359
360
361<sect3 id="writing-tools.debugfns">
362<title>Debugging C functions</title>
363
debad57fc2005-12-03 22:33:29 +0000364<para>If you want to debug C functions used by your tool, you can
tom605535f2005-11-29 09:59:32 +0000365achieve this by following these steps:</para>
njn3e986b22004-11-30 10:43:45 +0000366<orderedlist>
debad57fc2005-12-03 22:33:29 +0000367 <listitem>
368 <para>Set <computeroutput>VALGRIND_LAUNCHER</computeroutput> to
369 <computeroutput><![CDATA[<prefix>/bin/valgrind]]></computeroutput>:</para>
ded3c35372005-11-29 16:06:55 +0000370<programlisting>
371 export VALGRIND_LAUNCHER=/usr/local/bin/valgrind</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>Then run <computeroutput><![CDATA[ gdb <prefix>/lib/valgrind/<platform>/<tool>:]]></computeroutput></para>
ded3c35372005-11-29 16:06:55 +0000376<programlisting>
377 gdb /usr/local/lib/valgrind/ppc32-linux/lackey</programlisting>
debad57fc2005-12-03 22:33:29 +0000378 </listitem>
njn3e986b22004-11-30 10:43:45 +0000379
debad57fc2005-12-03 22:33:29 +0000380 <listitem>
381 <para>Do <computeroutput>handle SIGSEGV SIGILL nostop
382 noprint</computeroutput> in GDB to prevent GDB from stopping on a
383 SIGSEGV or SIGILL:</para>
ded3c35372005-11-29 16:06:55 +0000384<programlisting>
385 (gdb) handle SIGILL SIGSEGV nostop noprint</programlisting>
debad57fc2005-12-03 22:33:29 +0000386 </listitem>
tom605535f2005-11-29 09:59:32 +0000387
debad57fc2005-12-03 22:33:29 +0000388 <listitem>
389 <para>Set any breakpoints you want and proceed as normal for GDB:</para>
ded3c35372005-11-29 16:06:55 +0000390<programlisting>
391 (gdb) b vgPlain_do_exec</programlisting>
debad57fc2005-12-03 22:33:29 +0000392 <para>The macro VG_(FUNC) is expanded to vgPlain_FUNC, so If you
393 want to set a breakpoint VG_(do_exec), you could do like this in
394 GDB.</para>
395 </listitem>
tom605535f2005-11-29 09:59:32 +0000396
debad57fc2005-12-03 22:33:29 +0000397 <listitem>
398 <para>Run the tool with required options:</para>
ded3c35372005-11-29 16:06:55 +0000399<programlisting>
400 (gdb) run `pwd`</programlisting>
debad57fc2005-12-03 22:33:29 +0000401 </listitem>
njn3e986b22004-11-30 10:43:45 +0000402
403</orderedlist>
404
debad57fc2005-12-03 22:33:29 +0000405<para>GDB may be able to give you useful information. Note that by
406default most of the system is built with
407<option>-fomit-frame-pointer</option>, and you'll need to get rid of
408this to extract useful tracebacks from GDB.</para>
njn3e986b22004-11-30 10:43:45 +0000409
410</sect3>
411
412
413<sect3 id="writing-tools.ucode-probs">
njne5eaf3a2006-10-23 21:21:48 +0000414<title>IR Instrumentation Problems</title>
njn3e986b22004-11-30 10:43:45 +0000415
njne5eaf3a2006-10-23 21:21:48 +0000416<para>If you are having problems with your VEX IR instrumentation, it's
debad57fc2005-12-03 22:33:29 +0000417likely that GDB won't be able to help at all. In this case, Valgrind's
418<option>--trace-flags</option> option is invaluable for observing the
419results of instrumentation.</para>
njn3e986b22004-11-30 10:43:45 +0000420
421</sect3>
422
423
424<sect3 id="writing-tools.misc">
425<title>Miscellaneous</title>
426
debad57fc2005-12-03 22:33:29 +0000427<para>If you just want to know whether a program point has been reached,
428using the <computeroutput>OINK</computeroutput> macro (in
429<filename>include/pub_tool_libcprint.h</filename>) can be easier than
430using GDB.</para>
njn3e986b22004-11-30 10:43:45 +0000431
debad57fc2005-12-03 22:33:29 +0000432<para>The other debugging command line options can be useful too (run
433<computeroutput>valgrind --help-debug</computeroutput> for the
njn3e986b22004-11-30 10:43:45 +0000434list).</para>
435
436</sect3>
437
438</sect2>
439
440</sect1>
441
442
443
444<sect1 id="writing-tools.advtopics" xreflabel="Advanced Topics">
445<title>Advanced Topics</title>
446
447<para>Once a tool becomes more complicated, there are some extra
448things you may want/need to do.</para>
449
450<sect2 id="writing-tools.suppressions" xreflabel="Suppressions">
451<title>Suppressions</title>
452
debad57fc2005-12-03 22:33:29 +0000453<para>If your tool reports errors and you want to suppress some common
454ones, you can add suppressions to the suppression files. The relevant
455files are <filename>valgrind/*.supp</filename>; the final suppression
456file is aggregated from these files by combining the relevant
457<filename>.supp</filename> files depending on the versions of linux, X
458and glibc on a system.</para>
njn3e986b22004-11-30 10:43:45 +0000459
460<para>Suppression types have the form
461<computeroutput>tool_name:suppression_name</computeroutput>. The
debad57fc2005-12-03 22:33:29 +0000462<computeroutput>tool_name</computeroutput> here is the name you specify
463for the tool during initialisation with
464<function>VG_(details_name)()</function>.</para>
njn3e986b22004-11-30 10:43:45 +0000465
466</sect2>
467
468
njn3e986b22004-11-30 10:43:45 +0000469<sect2 id="writing-tools.docs" xreflabel="Documentation">
470<title>Documentation</title>
471
debad57fc2005-12-03 22:33:29 +0000472<para>As of version 3.0.0, Valgrind documentation has been converted to
473XML. Why? See <ulink url="http://www.ucc.ie/xml/">The XML FAQ</ulink>.
njn3e986b22004-11-30 10:43:45 +0000474</para>
475
476
477<sect3 id="writing-tools.xml" xreflabel="The XML Toolchain">
478<title>The XML Toolchain</title>
479
480<para>If you are feeling conscientious and want to write some
481documentation for your tool, please use XML. The Valgrind
482Docs use the following toolchain and versions:</para>
483
484<programlisting>
485 xmllint: using libxml version 20607
486 xsltproc: using libxml 20607, libxslt 10102 and libexslt 802
487 pdfxmltex: pdfTeX (Web2C 7.4.5) 3.14159-1.10b
488 pdftops: version 3.00
489 DocBook: version 4.2
490</programlisting>
491
492<para><command>Latency:</command> you should note that latency is
493a big problem: DocBook is constantly being updated, but the tools
494tend to lag behind somewhat. It is important that the versions
495get on with each other, so if you decide to upgrade something,
496then you need to ascertain whether things still work nicely -
497this *cannot* be assumed.</para>
498
499<para><command>Stylesheets:</command> The Valgrind docs use
500various custom stylesheet layers, all of which are in
501<computeroutput>valgrind/docs/lib/</computeroutput>. You
502shouldn't need to modify these in any way.</para>
503
debad57fc2005-12-03 22:33:29 +0000504<para><command>Catalogs:</command> Catalogs provide a mapping from
505generic addresses to specific local directories on a given machine.
506Most recent Linux distributions have adopted a common place for storing
507catalogs (<filename>/etc/xml/</filename>). Assuming that you have the
508various tools listed above installed, you probably won't need to modify
509your catalogs. But if you do, then just add another
510<computeroutput>group</computeroutput> to this file, reflecting your
511local installation.</para>
njn3e986b22004-11-30 10:43:45 +0000512
513</sect3>
514
515
516<sect3 id="writing-tools.writing" xreflabel="Writing the Documentation">
517<title>Writing the Documentation</title>
518
519<para>Follow these steps (using <computeroutput>foobar</computeroutput>
520as the example tool name again):</para>
521
522<orderedlist>
523
524 <listitem>
njne5eaf3a2006-10-23 21:21:48 +0000525 <para>The docs go in
526 <computeroutput>valgrind/foobar/docs/</computeroutput>, which you will
527 have created when you started writing the tool.</para>
528 </listitem>
529
530 <listitem>
531 <para>Write <filename>foobar/docs/Makefile.am</filename>. Use
532 <filename>memcheck/docs/Makefile.am</filename> as an
533 example.</para>
debad57fc2005-12-03 22:33:29 +0000534 </listitem>
njn3e986b22004-11-30 10:43:45 +0000535
debad57fc2005-12-03 22:33:29 +0000536 <listitem>
537 <para>Copy the XML documentation file for the tool Nulgrind from
538 <filename>valgrind/none/docs/nl-manual.xml</filename> to
539 <computeroutput>foobar/docs/</computeroutput>, and rename it to
540 <filename>foobar/docs/fb-manual.xml</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000541
debad57fc2005-12-03 22:33:29 +0000542 <para><command>Note</command>: there is a *really stupid* tetex bug
543 with underscores in filenames, so don't use '_'.</para>
544 </listitem>
njn3e986b22004-11-30 10:43:45 +0000545
debad57fc2005-12-03 22:33:29 +0000546 <listitem>
547 <para>Write the documentation. There are some helpful bits and
548 pieces on using xml markup in
549 <filename>valgrind/docs/xml/xml_help.txt</filename>.</para>
550 </listitem>
njn3e986b22004-11-30 10:43:45 +0000551
debad57fc2005-12-03 22:33:29 +0000552 <listitem>
553 <para>Include it in the User Manual by adding the relevant entry to
554 <filename>valgrind/docs/xml/manual.xml</filename>. Copy and edit an
555 existing entry.</para>
556 </listitem>
557
558 <listitem>
559 <para>Validate <filename>foobar/docs/fb-manual.xml</filename> using
560 the following command from within <filename>valgrind/docs/</filename>:
njn3e986b22004-11-30 10:43:45 +0000561 </para>
562<screen><![CDATA[
563% make valid
564]]></screen>
565
566 <para>You will probably get errors that look like this:</para>
567
568<screen><![CDATA[
569./xml/index.xml:5: element chapter: validity error : No declaration for
570attribute base of element chapter
571]]></screen>
572
debad57fc2005-12-03 22:33:29 +0000573 <para>Ignore (only) these -- they're not important.</para>
njn3e986b22004-11-30 10:43:45 +0000574
debad57fc2005-12-03 22:33:29 +0000575 <para>Because the xml toolchain is fragile, it is important to ensure
576 that <filename>fb-manual.xml</filename> won't break the documentation
577 set build. Note that just because an xml file happily transforms to
578 html does not necessarily mean the same holds true for pdf/ps.</para>
579 </listitem>
njn3e986b22004-11-30 10:43:45 +0000580
debad57fc2005-12-03 22:33:29 +0000581 <listitem>
582 <para>You can (re-)generate the HTML docs while you are writing
583 <filename>fb-manual.xml</filename> to help you see how it's looking.
584 The generated files end up in
585 <filename>valgrind/docs/html/</filename>. Use the following
586 command, within <filename>valgrind/docs/</filename>:</para>
njn3e986b22004-11-30 10:43:45 +0000587<screen><![CDATA[
588% make html-docs
589]]></screen>
debad57fc2005-12-03 22:33:29 +0000590 </listitem>
njn3e986b22004-11-30 10:43:45 +0000591
debad57fc2005-12-03 22:33:29 +0000592 <listitem>
593 <para>When you have finished, also generate pdf and ps output to
594 check all is well, from within <filename>valgrind/docs/</filename>:
njn3e986b22004-11-30 10:43:45 +0000595 </para>
njn3e986b22004-11-30 10:43:45 +0000596<screen><![CDATA[
597% make print-docs
598]]></screen>
599
debad57fc2005-12-03 22:33:29 +0000600 <para>Check the output <filename>.pdf</filename> and
601 <filename>.ps</filename> files in
602 <computeroutput>valgrind/docs/print/</computeroutput>.</para>
603 </listitem>
njn3e986b22004-11-30 10:43:45 +0000604
605</orderedlist>
606
607</sect3>
608
609</sect2>
610
611
612<sect2 id="writing-tools.regtests" xreflabel="Regression Tests">
613<title>Regression Tests</title>
614
debad57fc2005-12-03 22:33:29 +0000615<para>Valgrind has some support for regression tests. If you want to
616write regression tests for your tool:</para>
njn3e986b22004-11-30 10:43:45 +0000617
618<orderedlist>
debad57fc2005-12-03 22:33:29 +0000619 <listitem>
njne5eaf3a2006-10-23 21:21:48 +0000620 <para>The tests go in <computeroutput>foobar/tests/</computeroutput>,
621 which you will have created when you started writing the tool.</para>
debad57fc2005-12-03 22:33:29 +0000622 </listitem>
njn3e986b22004-11-30 10:43:45 +0000623
debad57fc2005-12-03 22:33:29 +0000624 <listitem>
625 <para>Write <filename>foobar/tests/Makefile.am</filename>. Use
626 <filename>memcheck/tests/Makefile.am</filename> as an
627 example.</para>
628 </listitem>
njn3e986b22004-11-30 10:43:45 +0000629
debad57fc2005-12-03 22:33:29 +0000630 <listitem>
631 <para>Write the tests, <computeroutput>.vgtest</computeroutput> test
632 description files, <computeroutput>.stdout.exp</computeroutput> and
633 <computeroutput>.stderr.exp</computeroutput> expected output files.
634 (Note that Valgrind's output goes to stderr.) Some details on
635 writing and running tests are given in the comments at the top of
636 the testing script
637 <computeroutput>tests/vg_regtest</computeroutput>.</para>
638 </listitem>
njn3e986b22004-11-30 10:43:45 +0000639
debad57fc2005-12-03 22:33:29 +0000640 <listitem>
641 <para>Write a filter for stderr results
642 <computeroutput>foobar/tests/filter_stderr</computeroutput>. It can
643 call the existing filters in
644 <computeroutput>tests/</computeroutput>. See
645 <computeroutput>memcheck/tests/filter_stderr</computeroutput> for an
646 example; in particular note the
647 <computeroutput>$dir</computeroutput> trick that ensures the filter
648 works correctly from any directory.</para>
649 </listitem>
njn3e986b22004-11-30 10:43:45 +0000650
651</orderedlist>
652
653</sect2>
654
655
656
657<sect2 id="writing-tools.profiling" xreflabel="Profiling">
658<title>Profiling</title>
659
njnbcd75fc2005-12-19 22:48:39 +0000660<para>To profile a tool, use Cachegrind on it. Read README_DEVELOPERS for
661details on running Valgrind under Valgrind.</para>
njn31066fd2005-03-26 00:42:02 +0000662
njnd57b78d2007-11-27 01:59:02 +0000663<para>Alternatively, you can use OProfile. In most cases, it is better than
664Cachegrind because it's much faster, and gives real times, as opposed to
665instruction and cache hit/miss counts.</para>
666
njn3e986b22004-11-30 10:43:45 +0000667</sect2>
668
669
670
671<sect2 id="writing-tools.mkhackery" xreflabel="Other Makefile Hackery">
672<title>Other Makefile Hackery</title>
673
674<para>If you add any directories under
debad57fc2005-12-03 22:33:29 +0000675<computeroutput>valgrind/foobar/</computeroutput>, you will need to add
676an appropriate <filename>Makefile.am</filename> to it, and add a
677corresponding entry to the <computeroutput>AC_OUTPUT</computeroutput>
678list in <filename>valgrind/configure.in</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000679
680<para>If you add any scripts to your tool (see Cachegrind for an
681example) you need to add them to the
682<computeroutput>bin_SCRIPTS</computeroutput> variable in
683<filename>valgrind/foobar/Makefile.am</filename>.</para>
684
685</sect2>
686
687
688
689<sect2 id="writing-tools.ifacever" xreflabel="Core/tool Interface Versions">
690<title>Core/tool Interface Versions</title>
691
debad57fc2005-12-03 22:33:29 +0000692<para>In order to allow for the core/tool interface to evolve over time,
693Valgrind uses a basic interface versioning system. All a tool has to do
694is use the
695<computeroutput>VG_DETERMINE_INTERFACE_VERSION</computeroutput> macro
696exactly once in its code. If not, a link error will occur when the tool
697is built.</para>
njn3e986b22004-11-30 10:43:45 +0000698
njne5eaf3a2006-10-23 21:21:48 +0000699<para>The interface version number is changed when binary incompatible
700changes are made to the interface. If the core and tool has the same major
701version number X they should work together. If X doesn't match, Valgrind
702will abort execution with an explanation of the problem.</para>
njn3e986b22004-11-30 10:43:45 +0000703
debad57fc2005-12-03 22:33:29 +0000704<para>This approach was chosen so that if the interface changes in the
705future, old tools won't work and the reason will be clearly explained,
706instead of possibly crashing mysteriously. We have attempted to
707minimise the potential for binary incompatible changes by means such as
708minimising the use of naked structs in the interface.</para>
njn3e986b22004-11-30 10:43:45 +0000709
710</sect2>
711
712</sect1>
713
714
715
716<sect1 id="writing-tools.finalwords" xreflabel="Final Words">
717<title>Final Words</title>
718
njne5eaf3a2006-10-23 21:21:48 +0000719<para>The core/tool interface is not fixed. It's pretty stable these days,
720but it does change. We deliberately do not provide backward compatibility
721with old interfaces, because it is too difficult and too restrictive.
722The interface checking should catch any incompatibilities. We view this as
723a good thing -- if we had to be backward compatible with earlier versions,
724many improvements now in the system could not have been added.
725</para>
njn3e986b22004-11-30 10:43:45 +0000726
njn3e986b22004-11-30 10:43:45 +0000727
728<para>Happy programming.</para>
729
730</sect1>
731
732</chapter>