| 1 |
<html> |
| 2 |
<head> |
| 3 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| 4 |
<title>2. Writing a New Valgrind Tool</title> |
| 5 |
<link rel="stylesheet" type="text/css" href="vg_basic.css"> |
| 6 |
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot"> |
| 7 |
<link rel="home" href="index.html" title="Valgrind Documentation"> |
| 8 |
<link rel="up" href="tech-docs.html" title="Valgrind Technical Documentation"> |
| 9 |
<link rel="prev" href="design-impl.html" title="1. The Design and Implementation of Valgrind"> |
| 10 |
<link rel="next" href="cl-format.html" title="3. Callgrind Format Specification"> |
| 11 |
</head> |
| 12 |
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
| 13 |
<div><table class="nav" width="100%" cellspacing="3" cellpadding="3" border="0" summary="Navigation header"><tr> |
| 14 |
<td width="22px" align="center" valign="middle"><a accesskey="p" href="design-impl.html"><img src="images/prev.png" width="18" height="21" border="0" alt="Prev"></a></td> |
| 15 |
<td width="25px" align="center" valign="middle"><a accesskey="u" href="tech-docs.html"><img src="images/up.png" width="21" height="18" border="0" alt="Up"></a></td> |
| 16 |
<td width="31px" align="center" valign="middle"><a accesskey="h" href="index.html"><img src="images/home.png" width="27" height="20" border="0" alt="Up"></a></td> |
| 17 |
<th align="center" valign="middle">Valgrind Technical Documentation</th> |
| 18 |
<td width="22px" align="center" valign="middle"><a accesskey="n" href="cl-format.html"><img src="images/next.png" width="18" height="21" border="0" alt="Next"></a></td> |
| 19 |
</tr></table></div> |
| 20 |
<div class="chapter"> |
| 21 |
<div class="titlepage"><div><div><h1 class="title"> |
| 22 |
<a name="manual-writing-tools"></a>2. Writing a New Valgrind Tool</h1></div></div></div> |
| 23 |
<div class="toc"> |
| 24 |
<p><b>Table of Contents</b></p> |
| 25 |
<dl class="toc"> |
| 26 |
<dt><span class="sect1"><a href="manual-writing-tools.html#manual-writing-tools.intro">2.1. Introduction</a></span></dt> |
| 27 |
<dt><span class="sect1"><a href="manual-writing-tools.html#manual-writing-tools.writingatool">2.2. Basics</a></span></dt> |
| 28 |
<dd><dl> |
| 29 |
<dt><span class="sect2"><a href="manual-writing-tools.html#manual-writing-tools.howtoolswork">2.2.1. How tools work</a></span></dt> |
| 30 |
<dt><span class="sect2"><a href="manual-writing-tools.html#manual-writing-tools.gettingcode">2.2.2. Getting the code</a></span></dt> |
| 31 |
<dt><span class="sect2"><a href="manual-writing-tools.html#manual-writing-tools.gettingstarted">2.2.3. Getting started</a></span></dt> |
| 32 |
<dt><span class="sect2"><a href="manual-writing-tools.html#manual-writing-tools.writingcode">2.2.4. Writing the code</a></span></dt> |
| 33 |
<dt><span class="sect2"><a href="manual-writing-tools.html#manual-writing-tools.init">2.2.5. Initialisation</a></span></dt> |
| 34 |
<dt><span class="sect2"><a href="manual-writing-tools.html#manual-writing-tools.instr">2.2.6. Instrumentation</a></span></dt> |
| 35 |
<dt><span class="sect2"><a href="manual-writing-tools.html#manual-writing-tools.fini">2.2.7. Finalisation</a></span></dt> |
| 36 |
<dt><span class="sect2"><a href="manual-writing-tools.html#manual-writing-tools.otherinfo">2.2.8. Other Important Information</a></span></dt> |
| 37 |
</dl></dd> |
| 38 |
<dt><span class="sect1"><a href="manual-writing-tools.html#manual-writing-tools.advtopics">2.3. Advanced Topics</a></span></dt> |
| 39 |
<dd><dl> |
| 40 |
<dt><span class="sect2"><a href="manual-writing-tools.html#manual-writing-tools.advice">2.3.1. Debugging Tips</a></span></dt> |
| 41 |
<dt><span class="sect2"><a href="manual-writing-tools.html#manual-writing-tools.suppressions">2.3.2. Suppressions</a></span></dt> |
| 42 |
<dt><span class="sect2"><a href="manual-writing-tools.html#manual-writing-tools.docs">2.3.3. Documentation</a></span></dt> |
| 43 |
<dt><span class="sect2"><a href="manual-writing-tools.html#manual-writing-tools.regtests">2.3.4. Regression Tests</a></span></dt> |
| 44 |
<dt><span class="sect2"><a href="manual-writing-tools.html#manual-writing-tools.profiling">2.3.5. Profiling</a></span></dt> |
| 45 |
<dt><span class="sect2"><a href="manual-writing-tools.html#manual-writing-tools.mkhackery">2.3.6. Other Makefile Hackery</a></span></dt> |
| 46 |
<dt><span class="sect2"><a href="manual-writing-tools.html#manual-writing-tools.ifacever">2.3.7. The Core/tool Interface</a></span></dt> |
| 47 |
</dl></dd> |
| 48 |
<dt><span class="sect1"><a href="manual-writing-tools.html#manual-writing-tools.finalwords">2.4. Final Words</a></span></dt> |
| 49 |
</dl> |
| 50 |
</div> |
| 51 |
<p>So you want to write a Valgrind tool? Here are some instructions |
| 52 |
that may help.</p> |
| 53 |
<div class="sect1"> |
| 54 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 55 |
<a name="manual-writing-tools.intro"></a>2.1. Introduction</h2></div></div></div> |
| 56 |
<p>The key idea behind Valgrind's architecture is the division |
| 57 |
between its <span class="emphasis"><em>core</em></span> and <span class="emphasis"><em>tools</em></span>.</p> |
| 58 |
<p>The core provides the common low-level infrastructure to |
| 59 |
support program instrumentation, including the JIT |
| 60 |
compiler, low-level memory manager, signal handling and a |
| 61 |
thread scheduler. It also provides certain services that |
| 62 |
are useful to some but not all tools, such as support for error |
| 63 |
recording, and support for replacing heap allocation functions such as |
| 64 |
<code class="function">malloc</code>.</p> |
| 65 |
<p>But the core leaves certain operations undefined, which |
| 66 |
must be filled by tools. Most notably, tools define how program |
| 67 |
code should be instrumented. They can also call certain |
| 68 |
functions to indicate to the core that they would like to use |
| 69 |
certain services, or be notified when certain interesting events |
| 70 |
occur. But the core takes care of all the hard work.</p> |
| 71 |
</div> |
| 72 |
<div class="sect1"> |
| 73 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 74 |
<a name="manual-writing-tools.writingatool"></a>2.2. Basics</h2></div></div></div> |
| 75 |
<div class="sect2"> |
| 76 |
<div class="titlepage"><div><div><h3 class="title"> |
| 77 |
<a name="manual-writing-tools.howtoolswork"></a>2.2.1. How tools work</h3></div></div></div> |
| 78 |
<p>Tools must define various functions for instrumenting programs |
| 79 |
that are called by Valgrind's core. They are then linked against |
| 80 |
Valgrind's core to define a complete Valgrind tool which will be used |
| 81 |
when the <code class="option">--tool</code> option is used to select it.</p> |
| 82 |
</div> |
| 83 |
<div class="sect2"> |
| 84 |
<div class="titlepage"><div><div><h3 class="title"> |
| 85 |
<a name="manual-writing-tools.gettingcode"></a>2.2.2. Getting the code</h3></div></div></div> |
| 86 |
<p>To write your own tool, you'll need the Valgrind source code. You'll |
| 87 |
need a clone from the git repository for the automake/autoconf |
| 88 |
build instructions to work. See the information about how to do clone |
| 89 |
from the repository at <a class="ulink" href="http://www.valgrind.org/downloads/repository.html" target="_top">the Valgrind |
| 90 |
website</a>.</p> |
| 91 |
</div> |
| 92 |
<div class="sect2"> |
| 93 |
<div class="titlepage"><div><div><h3 class="title"> |
| 94 |
<a name="manual-writing-tools.gettingstarted"></a>2.2.3. Getting started</h3></div></div></div> |
| 95 |
<p>Valgrind uses GNU <code class="computeroutput">automake</code> and |
| 96 |
<code class="computeroutput">autoconf</code> for the creation of Makefiles |
| 97 |
and configuration. But don't worry, these instructions should be enough |
| 98 |
to get you started even if you know nothing about those tools.</p> |
| 99 |
<p>In what follows, all filenames are relative to Valgrind's |
| 100 |
top-level directory <code class="computeroutput">valgrind/</code>.</p> |
| 101 |
<div class="orderedlist"><ol class="orderedlist" type="1"> |
| 102 |
<li class="listitem"><p>Choose a name for the tool, and a two-letter abbreviation that can |
| 103 |
be used as a short prefix. We'll use |
| 104 |
<code class="computeroutput">foobar</code> and |
| 105 |
<code class="computeroutput">fb</code> as an example.</p></li> |
| 106 |
<li class="listitem"><p>Make three new directories <code class="filename">foobar/</code>, |
| 107 |
<code class="filename">foobar/docs/</code> and |
| 108 |
<code class="filename">foobar/tests/</code>. |
| 109 |
</p></li> |
| 110 |
<li class="listitem"><p>Create an empty file <code class="filename">foobar/tests/Makefile.am</code>. |
| 111 |
</p></li> |
| 112 |
<li class="listitem"><p>Copy <code class="filename">none/Makefile.am</code> into |
| 113 |
<code class="filename">foobar/</code>. Edit it by replacing all |
| 114 |
occurrences of the strings |
| 115 |
<code class="computeroutput">"none"</code>, |
| 116 |
<code class="computeroutput">"nl_"</code> and |
| 117 |
<code class="computeroutput">"nl-"</code> with |
| 118 |
<code class="computeroutput">"foobar"</code>, |
| 119 |
<code class="computeroutput">"fb_"</code> and |
| 120 |
<code class="computeroutput">"fb-"</code> respectively.</p></li> |
| 121 |
<li class="listitem"><p>Copy <code class="filename">none/nl_main.c</code> into |
| 122 |
<code class="computeroutput">foobar/</code>, renaming it as |
| 123 |
<code class="filename">fb_main.c</code>. Edit it by changing the |
| 124 |
<code class="computeroutput">details</code> lines in |
| 125 |
<code class="function">nl_pre_clo_init</code> to something appropriate for the |
| 126 |
tool. These fields are used in the startup message, except for |
| 127 |
<code class="computeroutput">bug_reports_to</code> which is used if a |
| 128 |
tool assertion fails. Also, replace the string |
| 129 |
<code class="computeroutput">"nl_"</code> throughout with |
| 130 |
<code class="computeroutput">"fb_"</code> again.</p></li> |
| 131 |
<li class="listitem"><p>Edit <code class="filename">Makefile.am</code>, adding the new directory |
| 132 |
<code class="filename">foobar</code> to the |
| 133 |
<code class="computeroutput">TOOLS</code> or |
| 134 |
<code class="computeroutput">EXP_TOOLS</code> variables.</p></li> |
| 135 |
<li class="listitem"><p>Edit <code class="filename">configure.ac</code>, adding |
| 136 |
<code class="filename">foobar/Makefile</code> and |
| 137 |
<code class="filename">foobar/tests/Makefile</code> to the |
| 138 |
<code class="computeroutput">AC_OUTPUT</code> list.</p></li> |
| 139 |
<li class="listitem"> |
| 140 |
<p>Run:</p> |
| 141 |
<pre class="programlisting"> |
| 142 |
autogen.sh |
| 143 |
./configure --prefix=`pwd`/inst |
| 144 |
make |
| 145 |
make install</pre> |
| 146 |
<p>It should automake, configure and compile without errors, |
| 147 |
putting copies of the tool in |
| 148 |
<code class="filename">foobar/</code> and |
| 149 |
<code class="filename">inst/lib/valgrind/</code>.</p> |
| 150 |
</li> |
| 151 |
<li class="listitem"> |
| 152 |
<p>You can test it with a command like:</p> |
| 153 |
<pre class="programlisting"> |
| 154 |
inst/bin/valgrind --tool=foobar date</pre> |
| 155 |
<p>(almost any program should work; |
| 156 |
<code class="computeroutput">date</code> is just an example). |
| 157 |
The output should be something like this:</p> |
| 158 |
<pre class="programlisting"> |
| 159 |
==738== foobar-0.0.1, a foobarring tool. |
| 160 |
==738== Copyright (C) 2002-2017, and GNU GPL'd, by J. Programmer. |
| 161 |
==738== Using Valgrind-3.14.0.GIT and LibVEX; rerun with -h for copyright info |
| 162 |
==738== Command: date |
| 163 |
==738== |
| 164 |
Tue Nov 27 12:40:49 EST 2017 |
| 165 |
==738==</pre> |
| 166 |
<p>The tool does nothing except run the program uninstrumented.</p> |
| 167 |
</li> |
| 168 |
</ol></div> |
| 169 |
<p>These steps don't have to be followed exactly -- you can choose |
| 170 |
different names for your source files, and use a different |
| 171 |
<code class="option">--prefix</code> for |
| 172 |
<code class="computeroutput">./configure</code>.</p> |
| 173 |
<p>Now that we've setup, built and tested the simplest possible tool, |
| 174 |
onto the interesting stuff...</p> |
| 175 |
</div> |
| 176 |
<div class="sect2"> |
| 177 |
<div class="titlepage"><div><div><h3 class="title"> |
| 178 |
<a name="manual-writing-tools.writingcode"></a>2.2.4. Writing the code</h3></div></div></div> |
| 179 |
<p>A tool must define at least these four functions:</p> |
| 180 |
<pre class="programlisting"> |
| 181 |
pre_clo_init() |
| 182 |
post_clo_init() |
| 183 |
instrument() |
| 184 |
fini()</pre> |
| 185 |
<p>The names can be different to the above, but these are the usual |
| 186 |
names. The first one is registered using the macro |
| 187 |
<code class="computeroutput">VG_DETERMINE_INTERFACE_VERSION</code>. |
| 188 |
The last three are registered using the |
| 189 |
<code class="computeroutput">VG_(basic_tool_funcs)</code> function.</p> |
| 190 |
<p>In addition, if a tool wants to use some of the optional services |
| 191 |
provided by the core, it may have to define other functions and tell the |
| 192 |
core about them.</p> |
| 193 |
</div> |
| 194 |
<div class="sect2"> |
| 195 |
<div class="titlepage"><div><div><h3 class="title"> |
| 196 |
<a name="manual-writing-tools.init"></a>2.2.5. Initialisation</h3></div></div></div> |
| 197 |
<p>Most of the initialisation should be done in |
| 198 |
<code class="function">pre_clo_init</code>. Only use |
| 199 |
<code class="function">post_clo_init</code> if a tool provides command line |
| 200 |
options and must do some initialisation after option processing takes |
| 201 |
place (<code class="computeroutput">"clo"</code> stands for "command line |
| 202 |
options").</p> |
| 203 |
<p>First of all, various "details" need to be set for a tool, using |
| 204 |
the functions <code class="function">VG_(details_*)</code>. Some are all |
| 205 |
compulsory, some aren't. Some are used when constructing the startup |
| 206 |
message, <code class="computeroutput">detail_bug_reports_to</code> is used |
| 207 |
if <code class="computeroutput">VG_(tool_panic)</code> is ever called, or |
| 208 |
a tool assertion fails. Others have other uses.</p> |
| 209 |
<p>Second, various "needs" can be set for a tool, using the functions |
| 210 |
<code class="function">VG_(needs_*)</code>. They are mostly booleans, and can |
| 211 |
be left untouched (they default to <code class="varname">False</code>). They |
| 212 |
determine whether a tool can do various things such as: record, report |
| 213 |
and suppress errors; process command line options; wrap system calls; |
| 214 |
record extra information about heap blocks; etc.</p> |
| 215 |
<p>For example, if a tool wants the core's help in recording and |
| 216 |
reporting errors, it must call |
| 217 |
<code class="function">VG_(needs_tool_errors)</code> and provide definitions of |
| 218 |
eight functions for comparing errors, printing out errors, reading |
| 219 |
suppressions from a suppressions file, etc. While writing these |
| 220 |
functions requires some work, it's much less than doing error handling |
| 221 |
from scratch because the core is doing most of the work. |
| 222 |
</p> |
| 223 |
<p>Third, the tool can indicate which events in core it wants to be |
| 224 |
notified about, using the functions <code class="function">VG_(track_*)</code>. |
| 225 |
These include things such as heap blocks being allocated, the stack |
| 226 |
pointer changing, a mutex being locked, etc. If a tool wants to know |
| 227 |
about this, it should provide a pointer to a function, which will be |
| 228 |
called when that event happens.</p> |
| 229 |
<p>For example, if the tool want to be notified when a new heap block |
| 230 |
is allocated, it should call |
| 231 |
<code class="function">VG_(track_new_mem_heap)</code> with an appropriate |
| 232 |
function pointer, and the assigned function will be called each time |
| 233 |
this happens.</p> |
| 234 |
<p>More information about "details", "needs" and "trackable events" |
| 235 |
can be found in |
| 236 |
<code class="filename">include/pub_tool_tooliface.h</code>.</p> |
| 237 |
</div> |
| 238 |
<div class="sect2"> |
| 239 |
<div class="titlepage"><div><div><h3 class="title"> |
| 240 |
<a name="manual-writing-tools.instr"></a>2.2.6. Instrumentation</h3></div></div></div> |
| 241 |
<p><code class="function">instrument</code> is the interesting one. It |
| 242 |
allows you to instrument <span class="emphasis"><em>VEX IR</em></span>, which is |
| 243 |
Valgrind's RISC-like intermediate language. VEX IR is described |
| 244 |
in the comments of the header file |
| 245 |
<code class="filename">VEX/pub/libvex_ir.h</code>.</p> |
| 246 |
<p>The easiest way to instrument VEX IR is to insert calls to C |
| 247 |
functions when interesting things happen. See the tool "Lackey" |
| 248 |
(<code class="filename">lackey/lk_main.c</code>) for a simple example of this, or |
| 249 |
Cachegrind (<code class="filename">cachegrind/cg_main.c</code>) for a more |
| 250 |
complex example.</p> |
| 251 |
</div> |
| 252 |
<div class="sect2"> |
| 253 |
<div class="titlepage"><div><div><h3 class="title"> |
| 254 |
<a name="manual-writing-tools.fini"></a>2.2.7. Finalisation</h3></div></div></div> |
| 255 |
<p>This is where you can present the final results, such as a summary |
| 256 |
of the information collected. Any log files should be written out at |
| 257 |
this point.</p> |
| 258 |
</div> |
| 259 |
<div class="sect2"> |
| 260 |
<div class="titlepage"><div><div><h3 class="title"> |
| 261 |
<a name="manual-writing-tools.otherinfo"></a>2.2.8. Other Important Information</h3></div></div></div> |
| 262 |
<p>Please note that the core/tool split infrastructure is quite |
| 263 |
complex and not brilliantly documented. Here are some important points, |
| 264 |
but there are undoubtedly many others that I should note but haven't |
| 265 |
thought of.</p> |
| 266 |
<p>The files <code class="filename">include/pub_tool_*.h</code> contain all the |
| 267 |
types, macros, functions, etc. that a tool should (hopefully) need, and are |
| 268 |
the only <code class="filename">.h</code> files a tool should need to |
| 269 |
<code class="computeroutput">#include</code>. They have a reasonable amount of |
| 270 |
documentation in it that should hopefully be enough to get you going.</p> |
| 271 |
<p>Note that you can't use anything from the C library (there |
| 272 |
are deep reasons for this, trust us). Valgrind provides an |
| 273 |
implementation of a reasonable subset of the C library, details of which |
| 274 |
are in <code class="filename">pub_tool_libc*.h</code>.</p> |
| 275 |
<p>When writing a tool, in theory you shouldn't need to look at any of |
| 276 |
the code in Valgrind's core, but in practice it might be useful sometimes to |
| 277 |
help understand something.</p> |
| 278 |
<p>The <code class="filename">include/pub_tool_basics.h</code> and |
| 279 |
<code class="filename">VEX/pub/libvex_basictypes.h</code> files have some basic |
| 280 |
types that are widely used.</p> |
| 281 |
<p>Ultimately, the tools distributed (Memcheck, Cachegrind, Lackey, etc.) |
| 282 |
are probably the best documentation of all, for the moment.</p> |
| 283 |
<p>The <code class="computeroutput">VG_</code> macro is used |
| 284 |
heavily. This just prepends a longer string in front of names to avoid |
| 285 |
potential namespace clashes. It is defined in |
| 286 |
<code class="filename">include/pub_tool_basics.h</code>.</p> |
| 287 |
<p>There are some assorted notes about various aspects of the |
| 288 |
implementation in <code class="filename">docs/internals/</code>. Much of it |
| 289 |
isn't that relevant to tool-writers, however.</p> |
| 290 |
</div> |
| 291 |
</div> |
| 292 |
<div class="sect1"> |
| 293 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 294 |
<a name="manual-writing-tools.advtopics"></a>2.3. Advanced Topics</h2></div></div></div> |
| 295 |
<p>Once a tool becomes more complicated, there are some extra |
| 296 |
things you may want/need to do.</p> |
| 297 |
<div class="sect2"> |
| 298 |
<div class="titlepage"><div><div><h3 class="title"> |
| 299 |
<a name="manual-writing-tools.advice"></a>2.3.1. Debugging Tips</h3></div></div></div> |
| 300 |
<p>Writing and debugging tools is not trivial. Here are some |
| 301 |
suggestions for solving common problems.</p> |
| 302 |
<p>If you are getting segmentation faults in C functions used by your |
| 303 |
tool, the usual GDB command:</p> |
| 304 |
<pre class="screen"> |
| 305 |
gdb <prog> core</pre> |
| 306 |
<p>usually gives the location of the segmentation fault.</p> |
| 307 |
<p>If you want to debug C functions used by your tool, there are |
| 308 |
instructions on how to do so in the file |
| 309 |
<code class="filename">README_DEVELOPERS</code>.</p> |
| 310 |
<p>If you are having problems with your VEX IR instrumentation, it's |
| 311 |
likely that GDB won't be able to help at all. In this case, Valgrind's |
| 312 |
<code class="option">--trace-flags</code> option is invaluable for observing the |
| 313 |
results of instrumentation.</p> |
| 314 |
<p>If you just want to know whether a program point has been reached, |
| 315 |
using the <code class="computeroutput">OINK</code> macro (in |
| 316 |
<code class="filename">include/pub_tool_libcprint.h</code>) can be easier than |
| 317 |
using GDB.</p> |
| 318 |
<p>The other debugging command line options can be useful too (run |
| 319 |
<code class="computeroutput">valgrind --help-debug</code> for the |
| 320 |
list).</p> |
| 321 |
</div> |
| 322 |
<div class="sect2"> |
| 323 |
<div class="titlepage"><div><div><h3 class="title"> |
| 324 |
<a name="manual-writing-tools.suppressions"></a>2.3.2. Suppressions</h3></div></div></div> |
| 325 |
<p>If your tool reports errors and you want to suppress some common |
| 326 |
ones, you can add suppressions to the suppression files. The relevant |
| 327 |
files are <code class="filename">*.supp</code>; the final suppression |
| 328 |
file is aggregated from these files by combining the relevant |
| 329 |
<code class="filename">.supp</code> files depending on the versions of linux, X |
| 330 |
and glibc on a system.</p> |
| 331 |
<p>Suppression types have the form |
| 332 |
<code class="computeroutput">tool_name:suppression_name</code>. The |
| 333 |
<code class="computeroutput">tool_name</code> here is the name you specify |
| 334 |
for the tool during initialisation with |
| 335 |
<code class="function">VG_(details_name)</code>.</p> |
| 336 |
</div> |
| 337 |
<div class="sect2"> |
| 338 |
<div class="titlepage"><div><div><h3 class="title"> |
| 339 |
<a name="manual-writing-tools.docs"></a>2.3.3. Documentation</h3></div></div></div> |
| 340 |
<p>If you are feeling conscientious and want to write some |
| 341 |
documentation for your tool, please use XML as the rest of Valgrind does. |
| 342 |
The file <code class="filename">docs/README</code> has more details on getting |
| 343 |
the XML toolchain to work; this can be difficult, unfortunately.</p> |
| 344 |
<p>To write the documentation, follow these steps (using |
| 345 |
<code class="computeroutput">foobar</code> as the example tool name |
| 346 |
again):</p> |
| 347 |
<div class="orderedlist"><ol class="orderedlist" type="1"> |
| 348 |
<li class="listitem"><p>The docs go in |
| 349 |
<code class="computeroutput">foobar/docs/</code>, which you will |
| 350 |
have created when you started writing the tool.</p></li> |
| 351 |
<li class="listitem"> |
| 352 |
<p>Copy the XML documentation file for the tool Nulgrind from |
| 353 |
<code class="filename">none/docs/nl-manual.xml</code> to |
| 354 |
<code class="computeroutput">foobar/docs/</code>, and rename it to |
| 355 |
<code class="filename">foobar/docs/fb-manual.xml</code>.</p> |
| 356 |
<p><span class="command"><strong>Note</strong></span>: there is a tetex bug |
| 357 |
involving underscores in filenames, so don't use '_'.</p> |
| 358 |
</li> |
| 359 |
<li class="listitem"><p>Write the documentation. There are some helpful bits and |
| 360 |
pieces on using XML markup in |
| 361 |
<code class="filename">docs/xml/xml_help.txt</code>.</p></li> |
| 362 |
<li class="listitem"><p>Include it in the User Manual by adding the relevant entry to |
| 363 |
<code class="filename">docs/xml/manual.xml</code>. Copy and edit an |
| 364 |
existing entry.</p></li> |
| 365 |
<li class="listitem"><p>Include it in the man page by adding the relevant entry to |
| 366 |
<code class="filename">docs/xml/valgrind-manpage.xml</code>. Copy and |
| 367 |
edit an existing entry.</p></li> |
| 368 |
<li class="listitem"> |
| 369 |
<p>Validate <code class="filename">foobar/docs/fb-manual.xml</code> using |
| 370 |
the following command from within <code class="filename">docs/</code>: |
| 371 |
</p> |
| 372 |
<pre class="screen"> |
| 373 |
make valid |
| 374 |
</pre> |
| 375 |
<p>You may get errors that look like this:</p> |
| 376 |
<pre class="screen"> |
| 377 |
./xml/index.xml:5: element chapter: validity error : No declaration for |
| 378 |
attribute base of element chapter |
| 379 |
</pre> |
| 380 |
<p>Ignore (only) these -- they're not important.</p> |
| 381 |
<p>Because the XML toolchain is fragile, it is important to ensure |
| 382 |
that <code class="filename">fb-manual.xml</code> won't break the documentation |
| 383 |
set build. Note that just because an XML file happily transforms to |
| 384 |
html does not necessarily mean the same holds true for pdf/ps.</p> |
| 385 |
</li> |
| 386 |
<li class="listitem"> |
| 387 |
<p>You can (re-)generate the HTML docs while you are writing |
| 388 |
<code class="filename">fb-manual.xml</code> to help you see how it's looking. |
| 389 |
The generated files end up in |
| 390 |
<code class="filename">docs/html/</code>. Use the following |
| 391 |
command, within <code class="filename">docs/</code>:</p> |
| 392 |
<pre class="screen"> |
| 393 |
make html-docs |
| 394 |
</pre> |
| 395 |
</li> |
| 396 |
<li class="listitem"> |
| 397 |
<p>When you have finished, try to generate PDF and PostScript output to |
| 398 |
check all is well, from within <code class="filename">docs/</code>: |
| 399 |
</p> |
| 400 |
<pre class="screen"> |
| 401 |
make print-docs |
| 402 |
</pre> |
| 403 |
<p>Check the output <code class="filename">.pdf</code> and |
| 404 |
<code class="filename">.ps</code> files in |
| 405 |
<code class="computeroutput">docs/print/</code>.</p> |
| 406 |
<p>Note that the toolchain is even more fragile for the print docs, |
| 407 |
so don't feel too bad if you can't get it working.</p> |
| 408 |
</li> |
| 409 |
</ol></div> |
| 410 |
</div> |
| 411 |
<div class="sect2"> |
| 412 |
<div class="titlepage"><div><div><h3 class="title"> |
| 413 |
<a name="manual-writing-tools.regtests"></a>2.3.4. Regression Tests</h3></div></div></div> |
| 414 |
<p>Valgrind has some support for regression tests. If you want to |
| 415 |
write regression tests for your tool:</p> |
| 416 |
<div class="orderedlist"><ol class="orderedlist" type="1"> |
| 417 |
<li class="listitem"><p>The tests go in <code class="computeroutput">foobar/tests/</code>, |
| 418 |
which you will have created when you started writing the tool.</p></li> |
| 419 |
<li class="listitem"><p>Write <code class="filename">foobar/tests/Makefile.am</code>. Use |
| 420 |
<code class="filename">memcheck/tests/Makefile.am</code> as an |
| 421 |
example.</p></li> |
| 422 |
<li class="listitem"><p>Write the tests, <code class="computeroutput">.vgtest</code> test |
| 423 |
description files, <code class="computeroutput">.stdout.exp</code> and |
| 424 |
<code class="computeroutput">.stderr.exp</code> expected output files. |
| 425 |
(Note that Valgrind's output goes to stderr.) Some details on |
| 426 |
writing and running tests are given in the comments at the top of |
| 427 |
the testing script |
| 428 |
<code class="computeroutput">tests/vg_regtest</code>.</p></li> |
| 429 |
<li class="listitem"><p>Write a filter for stderr results |
| 430 |
<code class="computeroutput">foobar/tests/filter_stderr</code>. It can |
| 431 |
call the existing filters in |
| 432 |
<code class="computeroutput">tests/</code>. See |
| 433 |
<code class="computeroutput">memcheck/tests/filter_stderr</code> for an |
| 434 |
example; in particular note the |
| 435 |
<code class="computeroutput">$dir</code> trick that ensures the filter |
| 436 |
works correctly from any directory.</p></li> |
| 437 |
</ol></div> |
| 438 |
</div> |
| 439 |
<div class="sect2"> |
| 440 |
<div class="titlepage"><div><div><h3 class="title"> |
| 441 |
<a name="manual-writing-tools.profiling"></a>2.3.5. Profiling</h3></div></div></div> |
| 442 |
<p>Lots of profiling tools have trouble running Valgrind. For example, |
| 443 |
trying to use gprof is hopeless.</p> |
| 444 |
<p>Probably the best way to profile a tool is with OProfile on Linux.</p> |
| 445 |
<p>You can also use Cachegrind on it. Read |
| 446 |
<code class="filename">README_DEVELOPERS</code> for details on running Valgrind under |
| 447 |
Valgrind; it's a bit fragile but can usually be made to work.</p> |
| 448 |
</div> |
| 449 |
<div class="sect2"> |
| 450 |
<div class="titlepage"><div><div><h3 class="title"> |
| 451 |
<a name="manual-writing-tools.mkhackery"></a>2.3.6. Other Makefile Hackery</h3></div></div></div> |
| 452 |
<p>If you add any directories under |
| 453 |
<code class="computeroutput">foobar/</code>, you will need to add |
| 454 |
an appropriate <code class="filename">Makefile.am</code> to it, and add a |
| 455 |
corresponding entry to the <code class="computeroutput">AC_OUTPUT</code> |
| 456 |
list in <code class="filename">configure.ac</code>.</p> |
| 457 |
<p>If you add any scripts to your tool (see Cachegrind for an |
| 458 |
example) you need to add them to the |
| 459 |
<code class="computeroutput">bin_SCRIPTS</code> variable in |
| 460 |
<code class="filename">foobar/Makefile.am</code> and possible also to the |
| 461 |
<code class="computeroutput">AC_OUTPUT</code> list in |
| 462 |
<code class="filename">configure.ac</code>.</p> |
| 463 |
</div> |
| 464 |
<div class="sect2"> |
| 465 |
<div class="titlepage"><div><div><h3 class="title"> |
| 466 |
<a name="manual-writing-tools.ifacever"></a>2.3.7. The Core/tool Interface</h3></div></div></div> |
| 467 |
<p>The core/tool interface evolves over time, but it's pretty stable. |
| 468 |
We deliberately do not provide backward compatibility with old interfaces, |
| 469 |
because it is too difficult and too restrictive. We view this as a good |
| 470 |
thing -- if we had to be backward compatible with earlier versions, many |
| 471 |
improvements now in the system could not have been added.</p> |
| 472 |
<p>Because tools are statically linked with the core, if a tool compiles |
| 473 |
successfully then it should be compatible with the core. We would not |
| 474 |
deliberately violate this property by, for example, changing the behaviour |
| 475 |
of a core function without changing its prototype.</p> |
| 476 |
</div> |
| 477 |
</div> |
| 478 |
<div class="sect1"> |
| 479 |
<div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
| 480 |
<a name="manual-writing-tools.finalwords"></a>2.4. Final Words</h2></div></div></div> |
| 481 |
<p>Writing a new Valgrind tool is not easy, but the tools you can write |
| 482 |
with Valgrind are among the most powerful programming tools there are. |
| 483 |
Happy programming!</p> |
| 484 |
</div> |
| 485 |
</div> |
| 486 |
<div> |
| 487 |
<br><table class="nav" width="100%" cellspacing="3" cellpadding="2" border="0" summary="Navigation footer"> |
| 488 |
<tr> |
| 489 |
<td rowspan="2" width="40%" align="left"> |
| 490 |
<a accesskey="p" href="design-impl.html"><< 1. The Design and Implementation of Valgrind</a> </td> |
| 491 |
<td width="20%" align="center"><a accesskey="u" href="tech-docs.html">Up</a></td> |
| 492 |
<td rowspan="2" width="40%" align="right"> <a accesskey="n" href="cl-format.html">3. Callgrind Format Specification >></a> |
| 493 |
</td> |
| 494 |
</tr> |
| 495 |
<tr><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td></tr> |
| 496 |
</table> |
| 497 |
</div> |
| 498 |
</body> |
| 499 |
</html> |