blob: 9f1761d21c9462ea4338744f941486c095534085 [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
njn01dded72009-07-29 03:34:56 +00007<chapter id="manual-writing-tools" xreflabel="Writing a New Valgrind Tool">
njn3e986b22004-11-30 10:43:45 +00008<title>Writing a New Valgrind Tool</title>
9
njne5eaf3a2006-10-23 21:21:48 +000010So you want to write a Valgrind tool? Here are some instructions that may
njn05a89172009-07-29 02:36:21 +000011help.
njn3e986b22004-11-30 10:43:45 +000012
njn01dded72009-07-29 03:34:56 +000013<sect1 id="manual-writing-tools.intro" xreflabel="Introduction">
14<title>Introduction</title>
njn3e986b22004-11-30 10:43:45 +000015
16<para>The key idea behind Valgrind's architecture is the division
njnbf826bf2009-08-07 04:55:15 +000017between its <emphasis>core</emphasis> and <emphasis>tools</emphasis>.</para>
njn3e986b22004-11-30 10:43:45 +000018
19<para>The core provides the common low-level infrastructure to
toma721cec2005-11-04 14:11:05 +000020support program instrumentation, including the JIT
njn3e986b22004-11-30 10:43:45 +000021compiler, low-level memory manager, signal handling and a
njnbf826bf2009-08-07 04:55:15 +000022thread scheduler. It also provides certain services that
njn3e986b22004-11-30 10:43:45 +000023are useful to some but not all tools, such as support for error
njnbf826bf2009-08-07 04:55:15 +000024recording, and support for replacing heap allocation functions such as
25<function>malloc</function>.</para>
njn3e986b22004-11-30 10:43:45 +000026
27<para>But the core leaves certain operations undefined, which
28must be filled by tools. Most notably, tools define how program
toma721cec2005-11-04 14:11:05 +000029code should be instrumented. They can also call certain
30functions to indicate to the core that they would like to use
njn3e986b22004-11-30 10:43:45 +000031certain services, or be notified when certain interesting events
32occur. But the core takes care of all the hard work.</para>
33
njn3e986b22004-11-30 10:43:45 +000034</sect1>
35
36
37
njnbf826bf2009-08-07 04:55:15 +000038<sect1 id="manual-writing-tools.writingatool" xreflabel="Basics">
39<title>Basics</title>
njn3e986b22004-11-30 10:43:45 +000040
njn01dded72009-07-29 03:34:56 +000041<sect2 id="manual-writing-tools.howtoolswork" xreflabel="How tools work">
njn3e986b22004-11-30 10:43:45 +000042<title>How tools work</title>
43
njnbf826bf2009-08-07 04:55:15 +000044<para>Tools must define various functions for instrumenting programs
njne5eaf3a2006-10-23 21:21:48 +000045that are called by Valgrind's core. They are then linked against
46Valgrind's core to define a complete Valgrind tool which will be used
47when the <option>--tool</option> option is used to select it.</para>
njn3e986b22004-11-30 10:43:45 +000048
49</sect2>
50
51
njn01dded72009-07-29 03:34:56 +000052<sect2 id="manual-writing-tools.gettingcode" xreflabel="Getting the code">
njn3e986b22004-11-30 10:43:45 +000053<title>Getting the code</title>
54
njne5eaf3a2006-10-23 21:21:48 +000055<para>To write your own tool, you'll need the Valgrind source code. You'll
56need a check-out of the Subversion repository for the automake/autoconf
57build instructions to work. See the information about how to do check-out
njn25ac3842009-08-07 02:58:11 +000058from the repository at <ulink url="&vg-repo-url;">the Valgrind
debad57fc2005-12-03 22:33:29 +000059website</ulink>.</para>
njn3e986b22004-11-30 10:43:45 +000060
61</sect2>
62
63
njn01dded72009-07-29 03:34:56 +000064<sect2 id="manual-writing-tools.gettingstarted" xreflabel="Getting started">
njn3e986b22004-11-30 10:43:45 +000065<title>Getting started</title>
66
debad57fc2005-12-03 22:33:29 +000067<para>Valgrind uses GNU <computeroutput>automake</computeroutput> and
68<computeroutput>autoconf</computeroutput> for the creation of Makefiles
69and configuration. But don't worry, these instructions should be enough
70to get you started even if you know nothing about those tools.</para>
njn3e986b22004-11-30 10:43:45 +000071
72<para>In what follows, all filenames are relative to Valgrind's
73top-level directory <computeroutput>valgrind/</computeroutput>.</para>
74
75<orderedlist>
76 <listitem>
njne5eaf3a2006-10-23 21:21:48 +000077 <para>Choose a name for the tool, and a two-letter abbreviation that can
78 be used as a short prefix. We'll use
79 <computeroutput>foobar</computeroutput> and
80 <computeroutput>fb</computeroutput> as an example.</para>
njn3e986b22004-11-30 10:43:45 +000081 </listitem>
82
83 <listitem>
njne5eaf3a2006-10-23 21:21:48 +000084 <para>Make three new directories <filename>foobar/</filename>,
85 <filename>foobar/docs/</filename> and
86 <filename>foobar/tests/</filename>.
87 </para>
88 </listitem>
89
90 <listitem>
njnbf826bf2009-08-07 04:55:15 +000091 <para>Create an empty file <filename>foobar/tests/Makefile.am</filename>.
njne5eaf3a2006-10-23 21:21:48 +000092 </para>
njn3e986b22004-11-30 10:43:45 +000093 </listitem>
94
95 <listitem>
debad57fc2005-12-03 22:33:29 +000096 <para>Copy <filename>none/Makefile.am</filename> into
njne5eaf3a2006-10-23 21:21:48 +000097 <filename>foobar/</filename>. Edit it by replacing all
njnbf826bf2009-08-07 04:55:15 +000098 occurrences of the strings
99 <computeroutput>"none"</computeroutput>,
100 <computeroutput>"nl_"</computeroutput> and
101 <computeroutput>"nl-"</computeroutput> with
102 <computeroutput>"foobar"</computeroutput>,
103 <computeroutput>"fb_"</computeroutput> and
104 <computeroutput>"fb-"</computeroutput> respectively.</para>
njn3e986b22004-11-30 10:43:45 +0000105 </listitem>
106
107 <listitem>
108 <para>Copy <filename>none/nl_main.c</filename> into
109 <computeroutput>foobar/</computeroutput>, renaming it as
njne5eaf3a2006-10-23 21:21:48 +0000110 <filename>fb_main.c</filename>. Edit it by changing the
111 <computeroutput>details</computeroutput> lines in
njnbf826bf2009-08-07 04:55:15 +0000112 <function>nl_pre_clo_init</function> to something appropriate for the
debad57fc2005-12-03 22:33:29 +0000113 tool. These fields are used in the startup message, except for
114 <computeroutput>bug_reports_to</computeroutput> which is used if a
njnbf826bf2009-08-07 04:55:15 +0000115 tool assertion fails. Also, replace the string
116 <computeroutput>"nl_"</computeroutput> throughout with
njne5eaf3a2006-10-23 21:21:48 +0000117 <computeroutput>"fb_"</computeroutput> again.</para>
njn3e986b22004-11-30 10:43:45 +0000118 </listitem>
119
120 <listitem>
debad57fc2005-12-03 22:33:29 +0000121 <para>Edit <filename>Makefile.am</filename>, adding the new directory
njne5eaf3a2006-10-23 21:21:48 +0000122 <filename>foobar</filename> to the
njn117a6b82008-02-03 22:35:21 +0000123 <computeroutput>TOOLS</computeroutput> or
njnbf826bf2009-08-07 04:55:15 +0000124 <computeroutput>EXP_TOOLS</computeroutput> variables.</para>
njn3e986b22004-11-30 10:43:45 +0000125 </listitem>
126
127 <listitem>
debad57fc2005-12-03 22:33:29 +0000128 <para>Edit <filename>configure.in</filename>, adding
njnbf826bf2009-08-07 04:55:15 +0000129 <filename>foobar/Makefile</filename> and
njne5eaf3a2006-10-23 21:21:48 +0000130 <filename>foobar/tests/Makefile</filename> to the
njn3e986b22004-11-30 10:43:45 +0000131 <computeroutput>AC_OUTPUT</computeroutput> list.</para>
132 </listitem>
133
134 <listitem>
135 <para>Run:</para>
136<programlisting><![CDATA[
137 autogen.sh
138 ./configure --prefix=`pwd`/inst
njn8b68b642009-06-24 00:37:09 +0000139 make
njn3e986b22004-11-30 10:43:45 +0000140 make install]]></programlisting>
141
debad57fc2005-12-03 22:33:29 +0000142 <para>It should automake, configure and compile without errors,
143 putting copies of the tool in
njne5eaf3a2006-10-23 21:21:48 +0000144 <filename>foobar/</filename> and
145 <filename>inst/lib/valgrind/</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000146 </listitem>
147
148 <listitem>
149 <para>You can test it with a command like:</para>
150<programlisting><![CDATA[
151 inst/bin/valgrind --tool=foobar date]]></programlisting>
152
153 <para>(almost any program should work;
154 <computeroutput>date</computeroutput> is just an example).
155 The output should be something like this:</para>
156<programlisting><![CDATA[
njn53162bf2009-07-29 23:34:49 +0000157 ==738== foobar-0.0.1, a foobarring tool.
Elliott Hughesed398002017-06-21 14:41:24 -0700158 ==738== Copyright (C) 2002-2017, and GNU GPL'd, by J. Programmer.
159 ==738== Using Valgrind-3.13.0.SVN and LibVEX; rerun with -h for copyright info
njn53162bf2009-07-29 23:34:49 +0000160 ==738== Command: date
njnd57b78d2007-11-27 01:59:02 +0000161 ==738==
162 Tue Nov 27 12:40:49 EST 2007
njn3e986b22004-11-30 10:43:45 +0000163 ==738==]]></programlisting>
164
njnd57b78d2007-11-27 01:59:02 +0000165 <para>The tool does nothing except run the program uninstrumented.</para>
njn3e986b22004-11-30 10:43:45 +0000166 </listitem>
167
168</orderedlist>
169
njnbf826bf2009-08-07 04:55:15 +0000170<para>These steps don't have to be followed exactly -- you can choose
debad57fc2005-12-03 22:33:29 +0000171different names for your source files, and use a different
172<option>--prefix</option> for
njn3e986b22004-11-30 10:43:45 +0000173<computeroutput>./configure</computeroutput>.</para>
174
debad57fc2005-12-03 22:33:29 +0000175<para>Now that we've setup, built and tested the simplest possible tool,
176onto the interesting stuff...</para>
njn3e986b22004-11-30 10:43:45 +0000177
178</sect2>
179
180
181
njn01dded72009-07-29 03:34:56 +0000182<sect2 id="manual-writing-tools.writingcode" xreflabel="Writing the Code">
njn3e986b22004-11-30 10:43:45 +0000183<title>Writing the code</title>
184
185<para>A tool must define at least these four functions:</para>
186<programlisting><![CDATA[
njn51d827b2005-05-09 01:02:08 +0000187 pre_clo_init()
188 post_clo_init()
189 instrument()
190 fini()]]></programlisting>
njn3e986b22004-11-30 10:43:45 +0000191
njne5eaf3a2006-10-23 21:21:48 +0000192<para>The names can be different to the above, but these are the usual
193names. The first one is registered using the macro
njnbf826bf2009-08-07 04:55:15 +0000194<computeroutput>VG_DETERMINE_INTERFACE_VERSION</computeroutput>.
njne5eaf3a2006-10-23 21:21:48 +0000195The last three are registered using the
sewardj7aeb10f2006-12-10 02:59:16 +0000196<computeroutput>VG_(basic_tool_funcs)</computeroutput> function.</para>
njn3e986b22004-11-30 10:43:45 +0000197
debad57fc2005-12-03 22:33:29 +0000198<para>In addition, if a tool wants to use some of the optional services
199provided by the core, it may have to define other functions and tell the
njne5eaf3a2006-10-23 21:21:48 +0000200core about them.</para>
njn3e986b22004-11-30 10:43:45 +0000201
202</sect2>
203
204
205
njn01dded72009-07-29 03:34:56 +0000206<sect2 id="manual-writing-tools.init" xreflabel="Initialisation">
njn3e986b22004-11-30 10:43:45 +0000207<title>Initialisation</title>
208
209<para>Most of the initialisation should be done in
njnbf826bf2009-08-07 04:55:15 +0000210<function>pre_clo_init</function>. Only use
211<function>post_clo_init</function> if a tool provides command line
debad57fc2005-12-03 22:33:29 +0000212options and must do some initialisation after option processing takes
213place (<computeroutput>"clo"</computeroutput> stands for "command line
njn3e986b22004-11-30 10:43:45 +0000214options").</para>
215
debad57fc2005-12-03 22:33:29 +0000216<para>First of all, various "details" need to be set for a tool, using
njnbf826bf2009-08-07 04:55:15 +0000217the functions <function>VG_(details_*)</function>. Some are all
debad57fc2005-12-03 22:33:29 +0000218compulsory, some aren't. Some are used when constructing the startup
219message, <computeroutput>detail_bug_reports_to</computeroutput> is used
njnbf826bf2009-08-07 04:55:15 +0000220if <computeroutput>VG_(tool_panic)</computeroutput> is ever called, or
debad57fc2005-12-03 22:33:29 +0000221a tool assertion fails. Others have other uses.</para>
njn3e986b22004-11-30 10:43:45 +0000222
debad57fc2005-12-03 22:33:29 +0000223<para>Second, various "needs" can be set for a tool, using the functions
njnbf826bf2009-08-07 04:55:15 +0000224<function>VG_(needs_*)</function>. They are mostly booleans, and can
debad57fc2005-12-03 22:33:29 +0000225be left untouched (they default to <varname>False</varname>). They
226determine whether a tool can do various things such as: record, report
227and suppress errors; process command line options; wrap system calls;
njnbf826bf2009-08-07 04:55:15 +0000228record extra information about heap blocks; etc.</para>
njn3e986b22004-11-30 10:43:45 +0000229
debad57fc2005-12-03 22:33:29 +0000230<para>For example, if a tool wants the core's help in recording and
231reporting errors, it must call
232<function>VG_(needs_tool_errors)</function> and provide definitions of
233eight functions for comparing errors, printing out errors, reading
234suppressions from a suppressions file, etc. While writing these
235functions requires some work, it's much less than doing error handling
njnbf826bf2009-08-07 04:55:15 +0000236from scratch because the core is doing most of the work.
237</para>
njn3e986b22004-11-30 10:43:45 +0000238
debad57fc2005-12-03 22:33:29 +0000239<para>Third, the tool can indicate which events in core it wants to be
njnbf826bf2009-08-07 04:55:15 +0000240notified about, using the functions <function>VG_(track_*)</function>.
njn7316df22009-08-04 01:16:01 +0000241These include things such as heap blocks being allocated, the stack
debad57fc2005-12-03 22:33:29 +0000242pointer changing, a mutex being locked, etc. If a tool wants to know
243about this, it should provide a pointer to a function, which will be
244called when that event happens.</para>
njn3e986b22004-11-30 10:43:45 +0000245
njn7316df22009-08-04 01:16:01 +0000246<para>For example, if the tool want to be notified when a new heap block
247is allocated, it should call
njnbf826bf2009-08-07 04:55:15 +0000248<function>VG_(track_new_mem_heap)</function> with an appropriate
debad57fc2005-12-03 22:33:29 +0000249function pointer, and the assigned function will be called each time
250this happens.</para>
njn3e986b22004-11-30 10:43:45 +0000251
debad57fc2005-12-03 22:33:29 +0000252<para>More information about "details", "needs" and "trackable events"
253can be found in
tomb931c582005-11-04 12:17:20 +0000254<filename>include/pub_tool_tooliface.h</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000255
256</sect2>
257
258
259
njn01dded72009-07-29 03:34:56 +0000260<sect2 id="manual-writing-tools.instr" xreflabel="Instrumentation">
njn3e986b22004-11-30 10:43:45 +0000261<title>Instrumentation</title>
262
njnbf826bf2009-08-07 04:55:15 +0000263<para><function>instrument</function> is the interesting one. It
debad57fc2005-12-03 22:33:29 +0000264allows you to instrument <emphasis>VEX IR</emphasis>, which is
njnbf826bf2009-08-07 04:55:15 +0000265Valgrind's RISC-like intermediate language. VEX IR is described
njnd57b78d2007-11-27 01:59:02 +0000266in the comments of the header file
267<filename>VEX/pub/libvex_ir.h</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000268
toma721cec2005-11-04 14:11:05 +0000269<para>The easiest way to instrument VEX IR is to insert calls to C
njn3e986b22004-11-30 10:43:45 +0000270functions when interesting things happen. See the tool "Lackey"
debad57fc2005-12-03 22:33:29 +0000271(<filename>lackey/lk_main.c</filename>) for a simple example of this, or
272Cachegrind (<filename>cachegrind/cg_main.c</filename>) for a more
273complex example.</para>
njn3e986b22004-11-30 10:43:45 +0000274
njn3e986b22004-11-30 10:43:45 +0000275</sect2>
276
277
278
njn01dded72009-07-29 03:34:56 +0000279<sect2 id="manual-writing-tools.fini" xreflabel="Finalisation">
njn3e986b22004-11-30 10:43:45 +0000280<title>Finalisation</title>
281
debad57fc2005-12-03 22:33:29 +0000282<para>This is where you can present the final results, such as a summary
283of the information collected. Any log files should be written out at
284this point.</para>
njn3e986b22004-11-30 10:43:45 +0000285
286</sect2>
287
288
289
njn01dded72009-07-29 03:34:56 +0000290<sect2 id="manual-writing-tools.otherinfo" xreflabel="Other Important Information">
njn3e986b22004-11-30 10:43:45 +0000291<title>Other Important Information</title>
292
debad57fc2005-12-03 22:33:29 +0000293<para>Please note that the core/tool split infrastructure is quite
294complex and not brilliantly documented. Here are some important points,
295but there are undoubtedly many others that I should note but haven't
296thought of.</para>
njn3e986b22004-11-30 10:43:45 +0000297
njne5eaf3a2006-10-23 21:21:48 +0000298<para>The files <filename>include/pub_tool_*.h</filename> contain all the
299types, macros, functions, etc. that a tool should (hopefully) need, and are
300the only <filename>.h</filename> files a tool should need to
njnd57b78d2007-11-27 01:59:02 +0000301<computeroutput>#include</computeroutput>. They have a reasonable amount of
302documentation in it that should hopefully be enough to get you going.</para>
njn3e986b22004-11-30 10:43:45 +0000303
njnd57b78d2007-11-27 01:59:02 +0000304<para>Note that you can't use anything from the C library (there
debad57fc2005-12-03 22:33:29 +0000305are deep reasons for this, trust us). Valgrind provides an
306implementation of a reasonable subset of the C library, details of which
307are in <filename>pub_tool_libc*.h</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000308
njnbf826bf2009-08-07 04:55:15 +0000309<para>When writing a tool, in theory you shouldn't need to look at any of
310the code in Valgrind's core, but in practice it might be useful sometimes to
311help understand something.</para>
njn3e986b22004-11-30 10:43:45 +0000312
njnd57b78d2007-11-27 01:59:02 +0000313<para>The <filename>include/pub_tool_basics.h</filename> and
njnbf826bf2009-08-07 04:55:15 +0000314<filename>VEX/pub/libvex_basictypes.h</filename> files have some basic
njnd57b78d2007-11-27 01:59:02 +0000315types that are widely used.</para>
316
317<para>Ultimately, the tools distributed (Memcheck, Cachegrind, Lackey, etc.)
318are probably the best documentation of all, for the moment.</para>
njn3e986b22004-11-30 10:43:45 +0000319
njnbf826bf2009-08-07 04:55:15 +0000320<para>The <computeroutput>VG_</computeroutput> macro is used
debad57fc2005-12-03 22:33:29 +0000321heavily. This just prepends a longer string in front of names to avoid
njnd57b78d2007-11-27 01:59:02 +0000322potential namespace clashes. It is defined in
njnbf826bf2009-08-07 04:55:15 +0000323<filename>include/pub_tool_basics.h</filename>.</para>
njnd57b78d2007-11-27 01:59:02 +0000324
325<para>There are some assorted notes about various aspects of the
326implementation in <filename>docs/internals/</filename>. Much of it
327isn't that relevant to tool-writers, however.</para>
njn3e986b22004-11-30 10:43:45 +0000328
329</sect2>
330
331
njn3e986b22004-11-30 10:43:45 +0000332</sect1>
333
334
335
njn01dded72009-07-29 03:34:56 +0000336<sect1 id="manual-writing-tools.advtopics" xreflabel="Advanced Topics">
njn3e986b22004-11-30 10:43:45 +0000337<title>Advanced Topics</title>
338
339<para>Once a tool becomes more complicated, there are some extra
340things you may want/need to do.</para>
341
njnbf826bf2009-08-07 04:55:15 +0000342<sect2 id="manual-writing-tools.advice" xreflabel="Debugging Tips">
343<title>Debugging Tips</title>
344
345<para>Writing and debugging tools is not trivial. Here are some
346suggestions for solving common problems.</para>
347
348<para>If you are getting segmentation faults in C functions used by your
349tool, the usual GDB command:</para>
350
351<screen><![CDATA[
352 gdb <prog> core]]></screen>
353<para>usually gives the location of the segmentation fault.</para>
354
355<para>If you want to debug C functions used by your tool, there are
356instructions on how to do so in the file
357<filename>README_DEVELOPERS</filename>.</para>
358
359<para>If you are having problems with your VEX IR instrumentation, it's
360likely that GDB won't be able to help at all. In this case, Valgrind's
361<option>--trace-flags</option> option is invaluable for observing the
362results of instrumentation.</para>
363
364<para>If you just want to know whether a program point has been reached,
365using the <computeroutput>OINK</computeroutput> macro (in
366<filename>include/pub_tool_libcprint.h</filename>) can be easier than
367using GDB.</para>
368
369<para>The other debugging command line options can be useful too (run
370<computeroutput>valgrind --help-debug</computeroutput> for the
371list).</para>
372
373</sect2>
374
njn01dded72009-07-29 03:34:56 +0000375<sect2 id="manual-writing-tools.suppressions" xreflabel="Suppressions">
njn3e986b22004-11-30 10:43:45 +0000376<title>Suppressions</title>
377
debad57fc2005-12-03 22:33:29 +0000378<para>If your tool reports errors and you want to suppress some common
379ones, you can add suppressions to the suppression files. The relevant
njnbf826bf2009-08-07 04:55:15 +0000380files are <filename>*.supp</filename>; the final suppression
debad57fc2005-12-03 22:33:29 +0000381file is aggregated from these files by combining the relevant
382<filename>.supp</filename> files depending on the versions of linux, X
383and glibc on a system.</para>
njn3e986b22004-11-30 10:43:45 +0000384
385<para>Suppression types have the form
386<computeroutput>tool_name:suppression_name</computeroutput>. The
debad57fc2005-12-03 22:33:29 +0000387<computeroutput>tool_name</computeroutput> here is the name you specify
388for the tool during initialisation with
njnbf826bf2009-08-07 04:55:15 +0000389<function>VG_(details_name)</function>.</para>
njn3e986b22004-11-30 10:43:45 +0000390
391</sect2>
392
393
njn01dded72009-07-29 03:34:56 +0000394<sect2 id="manual-writing-tools.docs" xreflabel="Documentation">
njn3e986b22004-11-30 10:43:45 +0000395<title>Documentation</title>
396
njn3e986b22004-11-30 10:43:45 +0000397<para>If you are feeling conscientious and want to write some
njnbf826bf2009-08-07 04:55:15 +0000398documentation for your tool, please use XML as the rest of Valgrind does.
399The file <filename>docs/README</filename> has more details on getting
400the XML toolchain to work; this can be difficult, unfortunately.</para>
njn3e986b22004-11-30 10:43:45 +0000401
njnbf826bf2009-08-07 04:55:15 +0000402<para>To write the documentation, follow these steps (using
403<computeroutput>foobar</computeroutput> as the example tool name
404again):</para>
njn3e986b22004-11-30 10:43:45 +0000405
406<orderedlist>
407
408 <listitem>
njne5eaf3a2006-10-23 21:21:48 +0000409 <para>The docs go in
njnbf826bf2009-08-07 04:55:15 +0000410 <computeroutput>foobar/docs/</computeroutput>, which you will
njne5eaf3a2006-10-23 21:21:48 +0000411 have created when you started writing the tool.</para>
412 </listitem>
413
414 <listitem>
debad57fc2005-12-03 22:33:29 +0000415 <para>Copy the XML documentation file for the tool Nulgrind from
njnbf826bf2009-08-07 04:55:15 +0000416 <filename>none/docs/nl-manual.xml</filename> to
debad57fc2005-12-03 22:33:29 +0000417 <computeroutput>foobar/docs/</computeroutput>, and rename it to
418 <filename>foobar/docs/fb-manual.xml</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000419
njnbf826bf2009-08-07 04:55:15 +0000420 <para><command>Note</command>: there is a tetex bug
421 involving underscores in filenames, so don't use '_'.</para>
debad57fc2005-12-03 22:33:29 +0000422 </listitem>
njn3e986b22004-11-30 10:43:45 +0000423
debad57fc2005-12-03 22:33:29 +0000424 <listitem>
425 <para>Write the documentation. There are some helpful bits and
njnbf826bf2009-08-07 04:55:15 +0000426 pieces on using XML markup in
427 <filename>docs/xml/xml_help.txt</filename>.</para>
debad57fc2005-12-03 22:33:29 +0000428 </listitem>
njn3e986b22004-11-30 10:43:45 +0000429
debad57fc2005-12-03 22:33:29 +0000430 <listitem>
431 <para>Include it in the User Manual by adding the relevant entry to
njnbf826bf2009-08-07 04:55:15 +0000432 <filename>docs/xml/manual.xml</filename>. Copy and edit an
debad57fc2005-12-03 22:33:29 +0000433 existing entry.</para>
434 </listitem>
435
436 <listitem>
njnbf826bf2009-08-07 04:55:15 +0000437 <para>Include it in the man page by adding the relevant entry to
438 <filename>docs/xml/valgrind-manpage.xml</filename>. Copy and
439 edit an existing entry.</para>
440 </listitem>
441
442 <listitem>
debad57fc2005-12-03 22:33:29 +0000443 <para>Validate <filename>foobar/docs/fb-manual.xml</filename> using
njnbf826bf2009-08-07 04:55:15 +0000444 the following command from within <filename>docs/</filename>:
njn3e986b22004-11-30 10:43:45 +0000445 </para>
446<screen><![CDATA[
njnbf826bf2009-08-07 04:55:15 +0000447make valid
njn3e986b22004-11-30 10:43:45 +0000448]]></screen>
449
njnbf826bf2009-08-07 04:55:15 +0000450 <para>You may get errors that look like this:</para>
njn3e986b22004-11-30 10:43:45 +0000451
452<screen><![CDATA[
453./xml/index.xml:5: element chapter: validity error : No declaration for
454attribute base of element chapter
455]]></screen>
456
debad57fc2005-12-03 22:33:29 +0000457 <para>Ignore (only) these -- they're not important.</para>
njn3e986b22004-11-30 10:43:45 +0000458
njnbf826bf2009-08-07 04:55:15 +0000459 <para>Because the XML toolchain is fragile, it is important to ensure
debad57fc2005-12-03 22:33:29 +0000460 that <filename>fb-manual.xml</filename> won't break the documentation
njnbf826bf2009-08-07 04:55:15 +0000461 set build. Note that just because an XML file happily transforms to
debad57fc2005-12-03 22:33:29 +0000462 html does not necessarily mean the same holds true for pdf/ps.</para>
463 </listitem>
njn3e986b22004-11-30 10:43:45 +0000464
debad57fc2005-12-03 22:33:29 +0000465 <listitem>
466 <para>You can (re-)generate the HTML docs while you are writing
467 <filename>fb-manual.xml</filename> to help you see how it's looking.
468 The generated files end up in
njnbf826bf2009-08-07 04:55:15 +0000469 <filename>docs/html/</filename>. Use the following
470 command, within <filename>docs/</filename>:</para>
njn3e986b22004-11-30 10:43:45 +0000471<screen><![CDATA[
njnbf826bf2009-08-07 04:55:15 +0000472make html-docs
njn3e986b22004-11-30 10:43:45 +0000473]]></screen>
debad57fc2005-12-03 22:33:29 +0000474 </listitem>
njn3e986b22004-11-30 10:43:45 +0000475
debad57fc2005-12-03 22:33:29 +0000476 <listitem>
njnbf826bf2009-08-07 04:55:15 +0000477 <para>When you have finished, try to generate PDF and PostScript output to
478 check all is well, from within <filename>docs/</filename>:
njn3e986b22004-11-30 10:43:45 +0000479 </para>
njn3e986b22004-11-30 10:43:45 +0000480<screen><![CDATA[
njnbf826bf2009-08-07 04:55:15 +0000481make print-docs
njn3e986b22004-11-30 10:43:45 +0000482]]></screen>
483
debad57fc2005-12-03 22:33:29 +0000484 <para>Check the output <filename>.pdf</filename> and
485 <filename>.ps</filename> files in
njnbf826bf2009-08-07 04:55:15 +0000486 <computeroutput>docs/print/</computeroutput>.</para>
487
488 <para>Note that the toolchain is even more fragile for the print docs,
489 so don't feel too bad if you can't get it working.</para>
debad57fc2005-12-03 22:33:29 +0000490 </listitem>
njn3e986b22004-11-30 10:43:45 +0000491
492</orderedlist>
493
njn3e986b22004-11-30 10:43:45 +0000494</sect2>
495
496
njn01dded72009-07-29 03:34:56 +0000497<sect2 id="manual-writing-tools.regtests" xreflabel="Regression Tests">
njn3e986b22004-11-30 10:43:45 +0000498<title>Regression Tests</title>
499
debad57fc2005-12-03 22:33:29 +0000500<para>Valgrind has some support for regression tests. If you want to
501write regression tests for your tool:</para>
njn3e986b22004-11-30 10:43:45 +0000502
503<orderedlist>
debad57fc2005-12-03 22:33:29 +0000504 <listitem>
njne5eaf3a2006-10-23 21:21:48 +0000505 <para>The tests go in <computeroutput>foobar/tests/</computeroutput>,
506 which you will have created when you started writing the tool.</para>
debad57fc2005-12-03 22:33:29 +0000507 </listitem>
njn3e986b22004-11-30 10:43:45 +0000508
debad57fc2005-12-03 22:33:29 +0000509 <listitem>
510 <para>Write <filename>foobar/tests/Makefile.am</filename>. Use
511 <filename>memcheck/tests/Makefile.am</filename> as an
512 example.</para>
513 </listitem>
njn3e986b22004-11-30 10:43:45 +0000514
debad57fc2005-12-03 22:33:29 +0000515 <listitem>
516 <para>Write the tests, <computeroutput>.vgtest</computeroutput> test
517 description files, <computeroutput>.stdout.exp</computeroutput> and
518 <computeroutput>.stderr.exp</computeroutput> expected output files.
519 (Note that Valgrind's output goes to stderr.) Some details on
520 writing and running tests are given in the comments at the top of
521 the testing script
522 <computeroutput>tests/vg_regtest</computeroutput>.</para>
523 </listitem>
njn3e986b22004-11-30 10:43:45 +0000524
debad57fc2005-12-03 22:33:29 +0000525 <listitem>
526 <para>Write a filter for stderr results
527 <computeroutput>foobar/tests/filter_stderr</computeroutput>. It can
528 call the existing filters in
529 <computeroutput>tests/</computeroutput>. See
530 <computeroutput>memcheck/tests/filter_stderr</computeroutput> for an
531 example; in particular note the
532 <computeroutput>$dir</computeroutput> trick that ensures the filter
533 works correctly from any directory.</para>
534 </listitem>
njn3e986b22004-11-30 10:43:45 +0000535
536</orderedlist>
537
538</sect2>
539
540
541
njn01dded72009-07-29 03:34:56 +0000542<sect2 id="manual-writing-tools.profiling" xreflabel="Profiling">
njn3e986b22004-11-30 10:43:45 +0000543<title>Profiling</title>
544
njnbf826bf2009-08-07 04:55:15 +0000545<para>Lots of profiling tools have trouble running Valgrind. For example,
546trying to use gprof is hopeless.</para>
njn31066fd2005-03-26 00:42:02 +0000547
njnbf826bf2009-08-07 04:55:15 +0000548<para>Probably the best way to profile a tool is with OProfile on Linux.</para>
549
550<para>You can also use Cachegrind on it. Read
551<filename>README_DEVELOPERS</filename> for details on running Valgrind under
552Valgrind; it's a bit fragile but can usually be made to work.</para>
njnd57b78d2007-11-27 01:59:02 +0000553
njn3e986b22004-11-30 10:43:45 +0000554</sect2>
555
556
557
njn01dded72009-07-29 03:34:56 +0000558<sect2 id="manual-writing-tools.mkhackery" xreflabel="Other Makefile Hackery">
njn3e986b22004-11-30 10:43:45 +0000559<title>Other Makefile Hackery</title>
560
561<para>If you add any directories under
njnbf826bf2009-08-07 04:55:15 +0000562<computeroutput>foobar/</computeroutput>, you will need to add
debad57fc2005-12-03 22:33:29 +0000563an appropriate <filename>Makefile.am</filename> to it, and add a
564corresponding entry to the <computeroutput>AC_OUTPUT</computeroutput>
njnbf826bf2009-08-07 04:55:15 +0000565list in <filename>configure.in</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000566
567<para>If you add any scripts to your tool (see Cachegrind for an
568example) you need to add them to the
569<computeroutput>bin_SCRIPTS</computeroutput> variable in
njnbf826bf2009-08-07 04:55:15 +0000570<filename>foobar/Makefile.am</filename> and possible also to the
571<computeroutput>AC_OUTPUT</computeroutput> list in
572<filename>configure.in</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000573
574</sect2>
575
576
577
njn01dded72009-07-29 03:34:56 +0000578<sect2 id="manual-writing-tools.ifacever" xreflabel="Core/tool Interface Versions">
njnbf826bf2009-08-07 04:55:15 +0000579<title>The Core/tool Interface</title>
njn3e986b22004-11-30 10:43:45 +0000580
njnbf826bf2009-08-07 04:55:15 +0000581<para>The core/tool interface evolves over time, but it's pretty stable.
582We deliberately do not provide backward compatibility with old interfaces,
583because it is too difficult and too restrictive. We view this as a good
584thing -- if we had to be backward compatible with earlier versions, many
585improvements now in the system could not have been added.</para>
njn3e986b22004-11-30 10:43:45 +0000586
njnbf826bf2009-08-07 04:55:15 +0000587<para>Because tools are statically linked with the core, if a tool compiles
588successfully then it should be compatible with the core. We would not
589deliberately violate this property by, for example, changing the behaviour
590of a core function without changing its prototype.</para>
njn3e986b22004-11-30 10:43:45 +0000591
592</sect2>
593
594</sect1>
595
596
597
njn01dded72009-07-29 03:34:56 +0000598<sect1 id="manual-writing-tools.finalwords" xreflabel="Final Words">
njn3e986b22004-11-30 10:43:45 +0000599<title>Final Words</title>
600
njnbf826bf2009-08-07 04:55:15 +0000601<para>Writing a new Valgrind tool is not easy, but the tools you can write
602with Valgrind are among the most powerful programming tools there are.
603Happy programming!</para>
njn3e986b22004-11-30 10:43:45 +0000604
605</sect1>
606
607</chapter>