blob: 2bc552e7e1178a109e821c3a90f0ce43b44a0263 [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"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"[
4<!ENTITY % vg-entities SYSTEM "../../docs/xml/vg-entities.xml"> %vg-entities;
5]>
6
7<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
13<sect2 id="writing-tools.supexec" xreflabel="Supervised Execution">
14<title>Supervised Execution</title>
15
16<para>Valgrind provides a generic infrastructure for supervising
17the execution of programs. This is done by providing a way to
18instrument programs in very precise ways, making it relatively
19easy to support activities such as dynamic error detection and
20profiling.</para>
21
22<para>Although writing a tool is not easy, and requires learning
23quite a few things about Valgrind, it is much easier than
24instrumenting a program from scratch yourself.</para>
25
njnc7561b92005-06-19 01:24:32 +000026<para>[Nb: What follows is slightly out of date. In particular,
27there are various references to a file include/tool.h which has been
28split into a number of header files in include/.]</para>
29
njn3e986b22004-11-30 10:43:45 +000030</sect2>
31
32
33<sect2 id="writing-tools.tools" xreflabel="Tools">
34<title>Tools</title>
35
36<para>The key idea behind Valgrind's architecture is the division
37between its "core" and "tools".</para>
38
39<para>The core provides the common low-level infrastructure to
40support program instrumentation, including the x86-to-x86 JIT
41compiler, low-level memory manager, signal handling and a
42scheduler (for pthreads). It also provides certain services that
43are useful to some but not all tools, such as support for error
44recording and suppression.</para>
45
46<para>But the core leaves certain operations undefined, which
47must be filled by tools. Most notably, tools define how program
48code should be instrumented. They can also define certain
49variables to indicate to the core that they would like to use
50certain services, or be notified when certain interesting events
51occur. But the core takes care of all the hard work.</para>
52
53</sect2>
54
55
56<sect2 id="writing-tools.execspaces" xreflabel="Execution Spaces">
57<title>Execution Spaces</title>
58
59<para>An important concept to understand before writing a tool is
60that there are three spaces in which program code executes:</para>
61
62
63<orderedlist>
64
65 <listitem>
66 <para>User space: this covers most of the program's execution.
67 The tool is given the code and can instrument it any way it
68 likes, providing (more or less) total control over the
69 code.</para>
70
71 <para>Code executed in user space includes all the program
72 code, almost all of the C library (including things like the
73 dynamic linker), and almost all parts of all other
74 libraries.</para>
75 </listitem>
76
77 <listitem>
78 <para>Core space: a small proportion of the program's execution
79 takes place entirely within Valgrind's core. This includes:</para>
80 <itemizedlist>
81 <listitem>
82 <para>Dynamic memory management
83 (<computeroutput>malloc()</computeroutput> etc.)</para>
84 </listitem>
85 <listitem>
86 <para>Pthread operations and scheduling</para>
87 </listitem>
88 <listitem>
89 <para>Signal handling</para>
90 </listitem>
91 </itemizedlist>
92
93 <para>A tool has no control over these operations; it never
94 "sees" the code doing this work and thus cannot instrument it.
95 However, the core provides hooks so a tool can be notified
96 when certain interesting events happen, for example when when
97 dynamic memory is allocated or freed, the stack pointer is
98 changed, or a pthread mutex is locked, etc.</para>
99
100 <para>Note that these hooks only notify tools of events
101 relevant to user space. For example, when the core allocates
102 some memory for its own use, the tool is not notified of this,
103 because it's not directly part of the supervised program's
104 execution.</para>
105 </listitem>
106
107 <listitem>
108 <para>Kernel space: execution in the kernel. Two kinds:</para>
109 <orderedlist>
110 <listitem>
111 <para>System calls: can't be directly observed by either
112 the tool or the core. But the core does have some idea of
113 what happens to the arguments, and it provides hooks for a
114 tool to wrap system calls.</para>
115 </listitem>
116 <listitem>
117 <para>Other: all other kernel activity (e.g. process
118 scheduling) is totally opaque and irrelevant to the
119 program.</para>
120 </listitem>
121 </orderedlist>
122 </listitem>
123
124 <listitem>
125 <para>It should be noted that a tool only has direct control
126 over code executed in user space. This is the vast majority
127 of code executed, but it is not absolutely all of it, so any
128 profiling information recorded by a tool won't be totally
129 accurate.</para>
130 </listitem>
131
132</orderedlist>
133
134</sect2>
135
136</sect1>
137
138
139
140<sect1 id="writing-tools.writingatool" xreflabel="Writing a Tool">
141<title>Writing a Tool</title>
142
143
144<sect2 id="writing-tools.whywriteatool" xreflabel="Why write a tool?">
145<title>Why write a tool?</title>
146
147<para>Before you write a tool, you should have some idea of what
148it should do. What is it you want to know about your programs of
149interest? Consider some existing tools:</para>
150
151<itemizedlist>
152
153 <listitem>
154 <para><command>memcheck</command>: among other things, performs
155 fine-grained validity and addressibility checks of every memory
156 reference performed by the program.</para>
157 </listitem>
158
159 <listitem>
160 <para><command>addrcheck</command>: performs lighterweight
161 addressibility checks of every memory reference performed by
162 the program.</para>
163 </listitem>
164
165 <listitem>
166 <para><command>cachegrind</command>: tracks every instruction
167 and memory reference to simulate instruction and data caches,
168 tracking cache accesses and misses that occur on every line in
169 the program.</para>
170 </listitem>
171
172 <listitem>
173 <para><command>helgrind</command>: tracks every memory access
174 and mutex lock/unlock to determine if a program contains any
175 data races.</para>
176 </listitem>
177
178 <listitem>
179 <para><command>lackey</command>: does simple counting of
180 various things: the number of calls to a particular function
181 (<computeroutput>_dl_runtime_resolve()</computeroutput>); the
182 number of basic blocks, x86 instruction, UCode instructions
183 executed; the number of branches executed and the proportion of
184 those which were taken.</para>
185 </listitem>
186</itemizedlist>
187
188<para>These examples give a reasonable idea of what kinds of
189things Valgrind can be used for. The instrumentation can range
190from very lightweight (e.g. counting the number of times a
191particular function is called) to very intrusive (e.g.
192memcheck's memory checking).</para>
193
194</sect2>
195
196
197<sect2 id="writing-tools.suggestedtools" xreflabel="Suggested tools">
198<title>Suggested tools</title>
199
200<para>Here is a list of ideas we have had for tools that should
201not be too hard to implement.</para>
202
203<itemizedlist>
204 <listitem>
205 <para><command>branch profiler</command>: A machine's branch
206 prediction hardware could be simulated, and each branch
207 annotated with the number of predicted and mispredicted
208 branches. Would be implemented quite similarly to Cachegrind,
209 and could reuse the
210 <computeroutput>cg_annotate</computeroutput> script to annotate
211 source code.</para>
212
213 <para>The biggest difficulty with this is the simulation; the
214 chip-makers are very cagey about how their chips do branch
215 prediction. But implementing one or more of the basic
216 algorithms could still give good information.</para>
217 </listitem>
218
219 <listitem>
220 <para><command>coverage tool</command>: Cachegrind can already
221 be used for doing test coverage, but it's massive overkill to
222 use it just for that.</para>
223
224 <para>It would be easy to write a coverage tool that records
225 how many times each basic block was recorded. Again, the
226 <computeroutput>cg_annotate</computeroutput> script could be
227 used for annotating source code with the gathered information.
228 Although, <computeroutput>cg_annotate</computeroutput> is only
229 designed for working with single program runs. It could be
230 extended relatively easily to deal with multiple runs of a
231 program, so that the coverage of a whole test suite could be
232 determined.</para>
233
234 <para>In addition to the standard coverage information, such a
235 tool could record extra information that would help a user
236 generate test cases to exercise unexercised paths. For
237 example, for each conditional branch, the tool could record all
238 inputs to the conditional test, and print these out when
239 annotating.</para>
240 </listitem>
241
242 <listitem>
243 <para><command>run-time type checking</command>: A nice example
244 of a dynamic checker is given in this paper:</para>
245 <address>Debugging via Run-Time Type Checking
246 Alexey Loginov, Suan Hsi Yong, Susan Horwitz and Thomas Reps
247 Proceedings of Fundamental Approaches to Software Engineering
248 April 2001.
249 </address>
250
251 <para>Similar is the tool described in this paper:</para>
252 <address>Run-Time Type Checking for Binary Programs
253 Michael Burrows, Stephen N. Freund, Janet L. Wiener
254 Proceedings of the 12th International Conference on Compiler Construction (CC 2003)
255 April 2003.
256 </address>
257
258 <para>This approach can find quite a range of bugs,
259 particularly in C and C++ programs, and could be implemented
260 quite nicely as a Valgrind tool.</para>
261
262 <para>Ways to speed up this run-time type checking are
263 described in this paper:</para>
264 <address>Reducing the Overhead of Dynamic Analysis
265 Suan Hsi Yong and Susan Horwitz
266 Proceedings of Runtime Verification '02
267 July 2002.
268 </address>
269
270 <para>Valgrind's client requests could be used to pass
271 information to a tool about which elements need instrumentation
272 and which don't.</para>
273 </listitem>
274</itemizedlist>
275
276<para>We would love to hear from anyone who implements these or
277other tools.</para>
278
279</sect2>
280
281
282<sect2 id="writing-tools.howtoolswork" xreflabel="How tools work">
283<title>How tools work</title>
284
285<para>Tools must define various functions for instrumenting
286programs that are called by Valgrind's core, yet they must be
287implemented in such a way that they can be written and compiled
288without touching Valgrind's core. This is important, because one
289of our aims is to allow people to write and distribute their own
290tools that can be plugged into Valgrind's core easily.</para>
291
292<para>This is achieved by packaging each tool into a separate
293shared object which is then loaded ahead of the core shared
294object <computeroutput>valgrind.so</computeroutput>, using the
295dynamic linker's <computeroutput>LD_PRELOAD</computeroutput>
296variable. Any functions defined in the tool that share the name
297with a function defined in core (such as the instrumentation
njn51d827b2005-05-09 01:02:08 +0000298function <computeroutput>instrument()</computeroutput>)
njn3e986b22004-11-30 10:43:45 +0000299override the core's definition. Thus the core can call the
300necessary tool functions.</para>
301
302<para>This magic is all done for you; the shared object used is
303chosen with the <computeroutput>--tool</computeroutput> option to
304the <computeroutput>valgrind</computeroutput> startup script.
305The default tool used is
306<computeroutput>memcheck</computeroutput>, Valgrind's original
307memory checker.</para>
308
309</sect2>
310
311
312<sect2 id="writing-tools.gettingcode" xreflabel="Getting the code">
313<title>Getting the code</title>
314
315<para>To write your own tool, you'll need to check out a copy of
316Valgrind from the CVS repository, rather than using a packaged
317distribution. This is because it contains several extra files
318needed for writing tools.</para>
319
320<para>To check out the code from the CVS repository, first login:</para>
321<programlisting><![CDATA[
322cvs -d:pserver:anonymous@cvs.valgrind.sourceforge.net:/cvsroot/valgrind
323login]]></programlisting>
324
325<para>Then checkout the code. To get a copy of the current
326development version (recommended for the brave only):</para>
327<programlisting><![CDATA[
328cvs -z3 -d:pserver:anonymous@cvs.valgrind.sourceforge.net:/cvsroot/valgrind
329co valgrind]]></programlisting>
330
331<para>To get a copy of the stable released branch:</para>
332<programlisting><![CDATA[
333cvs -z3 -d:pserver:anonymous@cvs.valgrind.sourceforge.net:/cvsroot/valgrind
334co -r <TAG> valgrind]]></programlisting>
335
336<para>where &lt;<computeroutput>TAG</computeroutput>&gt; has the
337form <computeroutput>VALGRIND_X_Y_Z</computeroutput> for version
338X.Y.Z.</para>
339
340</sect2>
341
342
343<sect2 id="writing-tools.gettingstarted" xreflabel="Getting started">
344<title>Getting started</title>
345
346<para>Valgrind uses GNU <computeroutput>automake</computeroutput>
347and <computeroutput>autoconf</computeroutput> for the creation of
348Makefiles and configuration. But don't worry, these instructions
349should be enough to get you started even if you know nothing
350about those tools.</para>
351
352<para>In what follows, all filenames are relative to Valgrind's
353top-level directory <computeroutput>valgrind/</computeroutput>.</para>
354
355<orderedlist>
356 <listitem>
357 <para>Choose a name for the tool, and an abbreviation that can
358 be used as a short prefix. We'll use
359 <computeroutput>foobar</computeroutput> and
360 <computeroutput>fb</computeroutput> as an example.</para>
361 </listitem>
362
363 <listitem>
364 <para>Make a new directory
365 <computeroutput>foobar/</computeroutput> which will hold the
366 tool.</para>
367 </listitem>
368
369 <listitem>
370 <para>Copy <computeroutput>none/Makefile.am</computeroutput>
371 into <computeroutput>foobar/</computeroutput>. Edit it by
372 replacing all occurrences of the string
373 <computeroutput>"none"</computeroutput> with
374 <computeroutput>"foobar"</computeroutput> and the one
375 occurrence of the string <computeroutput>"nl_"</computeroutput>
376 with <computeroutput>"fb_"</computeroutput>. It might be worth
377 trying to understand this file, at least a little; you might
378 have to do more complicated things with it later on. In
379 particular, the name of the
njnc4fcca32004-12-01 00:02:36 +0000380 <computeroutput>vgtool_foobar_so_SOURCES</computeroutput>
njn3e986b22004-11-30 10:43:45 +0000381 variable determines the name of the tool's shared object, which
382 determines what name must be passed to the
383 <computeroutput>--tool</computeroutput> option to use the
384 tool.</para>
385 </listitem>
386
387 <listitem>
388 <para>Copy <filename>none/nl_main.c</filename> into
389 <computeroutput>foobar/</computeroutput>, renaming it as
390 <filename>fb_main.c</filename>. Edit it by changing the lines
njn51d827b2005-05-09 01:02:08 +0000391 in <computeroutput>pre_clo_init()</computeroutput> to
njn3e986b22004-11-30 10:43:45 +0000392 something appropriate for the tool. These fields are used in
393 the startup message, except for
394 <computeroutput>bug_reports_to</computeroutput> which is used
395 if a tool assertion fails.</para>
396 </listitem>
397
398 <listitem>
399 <para>Edit <computeroutput>Makefile.am</computeroutput>,
400 adding the new directory
401 <computeroutput>foobar</computeroutput> to the
402 <computeroutput>SUBDIRS</computeroutput> variable.</para>
403 </listitem>
404
405 <listitem>
406 <para>Edit <computeroutput>configure.in</computeroutput>,
407 adding <computeroutput>foobar/Makefile</computeroutput> to the
408 <computeroutput>AC_OUTPUT</computeroutput> list.</para>
409 </listitem>
410
411 <listitem>
412 <para>Run:</para>
413<programlisting><![CDATA[
414 autogen.sh
415 ./configure --prefix=`pwd`/inst
416 make install]]></programlisting>
417
418 <para>It should automake, configure and compile without
419 errors, putting copies of the tool's shared object
njnc4fcca32004-12-01 00:02:36 +0000420 <computeroutput>vgtool_foobar.so</computeroutput> in
njn3e986b22004-11-30 10:43:45 +0000421 <computeroutput>foobar/</computeroutput> and
422 <computeroutput>inst/lib/valgrind/</computeroutput>.</para>
423 </listitem>
424
425 <listitem>
426 <para>You can test it with a command like:</para>
427<programlisting><![CDATA[
428 inst/bin/valgrind --tool=foobar date]]></programlisting>
429
430 <para>(almost any program should work;
431 <computeroutput>date</computeroutput> is just an example).
432 The output should be something like this:</para>
433<programlisting><![CDATA[
434 ==738== foobar-0.0.1, a foobarring tool for x86-linux.
435 ==738== Copyright (C) 1066AD, and GNU GPL'd, by J. Random Hacker.
436 ==738== Built with valgrind-1.1.0, a program execution monitor.
437 ==738== Copyright (C) 2000-2003, and GNU GPL'd, by Julian Seward.
438 ==738== Estimated CPU clock rate is 1400 MHz
439 ==738== For more details, rerun with: -v
440 ==738== Wed Sep 25 10:31:54 BST 2002
441 ==738==]]></programlisting>
442
443 <para>The tool does nothing except run the program
444 uninstrumented.</para>
445 </listitem>
446
447</orderedlist>
448
449<para>These steps don't have to be followed exactly - you can
450choose different names for your source files, and use a different
451<computeroutput>--prefix</computeroutput> for
452<computeroutput>./configure</computeroutput>.</para>
453
454<para>Now that we've setup, built and tested the simplest
455possible tool, onto the interesting stuff...</para>
456
457</sect2>
458
459
460
461<sect2 id="writing-tools.writingcode" xreflabel="Writing the Code">
462<title>Writing the code</title>
463
464<para>A tool must define at least these four functions:</para>
465<programlisting><![CDATA[
njn51d827b2005-05-09 01:02:08 +0000466 pre_clo_init()
467 post_clo_init()
468 instrument()
469 fini()]]></programlisting>
njn3e986b22004-11-30 10:43:45 +0000470
471<para>Also, it must use the macro
472<computeroutput>VG_DETERMINE_INTERFACE_VERSION</computeroutput>
473exactly once in its source code. If it doesn't, you will get a
474link error involving
njn51d827b2005-05-09 01:02:08 +0000475<computeroutput>VG_(tool_interface_version)</computeroutput>.
njn3e986b22004-11-30 10:43:45 +0000476This macro is used to ensure the core/tool interface used by the
477core and a plugged-in tool are binary compatible.</para>
478
479<para>In addition, if a tool wants to use some of the optional
480services provided by the core, it may have to define other
481functions.</para>
482
483</sect2>
484
485
486
487<sect2 id="writing-tools.init" xreflabel="Initialisation">
488<title>Initialisation</title>
489
490<para>Most of the initialisation should be done in
njn51d827b2005-05-09 01:02:08 +0000491<computeroutput>pre_clo_init()</computeroutput>. Only use
492<computeroutput>post_clo_init()</computeroutput> if a tool
njn3e986b22004-11-30 10:43:45 +0000493provides command line options and must do some initialisation
494after option processing takes place
495(<computeroutput>"clo"</computeroutput> stands for "command line
496options").</para>
497
498<para>First of all, various "details" need to be set for a tool,
499using the functions
500<computeroutput>VG_(details_*)()</computeroutput>. Some are all
501compulsory, some aren't. Some are used when constructing the
502startup message,
503<computeroutput>detail_bug_reports_to</computeroutput> is used if
njnc4fcca32004-12-01 00:02:36 +0000504<computeroutput>VG_(tool_panic)()</computeroutput> is ever
njn3e986b22004-11-30 10:43:45 +0000505called, or a tool assertion fails. Others have other uses.</para>
506
507<para>Second, various "needs" can be set for a tool, using the
508functions <computeroutput>VG_(needs_*)()</computeroutput>. They
509are mostly booleans, and can be left untouched (they default to
510<computeroutput>False</computeroutput>). They determine whether
511a tool can do various things such as: record, report and suppress
512errors; process command line options; wrap system calls; record
513extra information about malloc'd blocks, etc.</para>
514
515<para>For example, if a tool wants the core's help in recording
516and reporting errors, it must set the
njnc4fcca32004-12-01 00:02:36 +0000517<computeroutput>tool_errors</computeroutput> need to
njn3e986b22004-11-30 10:43:45 +0000518<computeroutput>True</computeroutput>, and then provide
519definitions of six functions for comparing errors, printing out
520errors, reading suppressions from a suppressions file, etc.
521While writing these functions requires some work, it's much less
522than doing error handling from scratch because the core is doing
523most of the work. See the type
524<computeroutput>VgNeeds</computeroutput> in
njnc4fcca32004-12-01 00:02:36 +0000525<filename>include/tool.h</filename> for full details of all
njn3e986b22004-11-30 10:43:45 +0000526the needs.</para>
527
528<para>Third, the tool can indicate which events in core it wants
529to be notified about, using the functions
530<computeroutput>VG_(track_*)()</computeroutput>. These include
531things such as blocks of memory being malloc'd, the stack pointer
532changing, a mutex being locked, etc. If a tool wants to know
533about this, it should set the relevant pointer in the structure
534to point to a function, which will be called when that event
535happens.</para>
536
537<para>For example, if the tool want to be notified when a new
538block of memory is malloc'd, it should call
539<computeroutput>VG_(track_new_mem_heap)()</computeroutput> with
540an appropriate function pointer, and the assigned function will
541be called each time this happens.</para>
542
543<para>More information about "details", "needs" and "trackable
544events" can be found in
njnc4fcca32004-12-01 00:02:36 +0000545<filename>include/tool.h</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000546
547</sect2>
548
549
550
551<sect2 id="writing-tools.instr" xreflabel="Instrumentation">
552<title>Instrumentation</title>
553
njn51d827b2005-05-09 01:02:08 +0000554<para><computeroutput>instrument()</computeroutput> is the
njn3e986b22004-11-30 10:43:45 +0000555interesting one. It allows you to instrument
556<emphasis>UCode</emphasis>, which is Valgrind's RISC-like
557intermediate language. UCode is described in
558<xref linkend="mc-tech-docs.ucode"/>.</para>
559
560<para>The easiest way to instrument UCode is to insert calls to C
561functions when interesting things happen. See the tool "Lackey"
562(<filename>lackey/lk_main.c</filename>) for a simple example of
563this, or Cachegrind (<filename>cachegrind/cg_main.c</filename>)
564for a more complex example.</para>
565
566<para>A much more complicated way to instrument UCode, albeit one
567that might result in faster instrumented programs, is to extend
568UCode with new UCode instructions. This is recommended for
569advanced Valgrind hackers only! See Memcheck for an example.</para>
570
571</sect2>
572
573
574
575<sect2 id="writing-tools.fini" xreflabel="Finalisation">
576<title>Finalisation</title>
577
578<para>This is where you can present the final results, such as a
579summary of the information collected. Any log files should be
580written out at this point.</para>
581
582</sect2>
583
584
585
586<sect2 id="writing-tools.otherinfo" xreflabel="Other Important Information">
587<title>Other Important Information</title>
588
589<para>Please note that the core/tool split infrastructure is
590quite complex and not brilliantly documented. Here are some
591important points, but there are undoubtedly many others that I
592should note but haven't thought of.</para>
593
njnc4fcca32004-12-01 00:02:36 +0000594<para>The file <filename>include/tool.h</filename> contains
njn3e986b22004-11-30 10:43:45 +0000595all the types, macros, functions, etc. that a tool should
596(hopefully) need, and is the only <filename>.h</filename> file a
597tool should need to
598<computeroutput>#include</computeroutput>.</para>
599
600<para>In particular, you probably shouldn't use anything from the
601C library (there are deep reasons for this, trust us). Valgrind
602provides an implementation of a reasonable subset of the C
603library, details of which are in
njnc4fcca32004-12-01 00:02:36 +0000604<filename>tool.h</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000605
606<para>Similarly, when writing a tool, you shouldn't need to look
607at any of the code in Valgrind's core. Although it might be
608useful sometimes to help understand something.</para>
609
njnc4fcca32004-12-01 00:02:36 +0000610<para><filename>tool.h</filename> has a reasonable amount of
njn3e986b22004-11-30 10:43:45 +0000611documentation in it that should hopefully be enough to get you
612going. But ultimately, the tools distributed (Memcheck,
613Addrcheck, Cachegrind, Lackey, etc.) are probably the best
614documentation of all, for the moment.</para>
615
616<para>Note that the <computeroutput>VG_</computeroutput> and
njnc4fcca32004-12-01 00:02:36 +0000617<computeroutput>TL_</computeroutput> macros are used heavily.
njn3e986b22004-11-30 10:43:45 +0000618These just prepend longer strings in front of names to avoid
619potential namespace clashes. We strongly recommend using the
njnc4fcca32004-12-01 00:02:36 +0000620<computeroutput>TL_</computeroutput> macro for any global
njn3e986b22004-11-30 10:43:45 +0000621functions and variables in your tool, or writing a similar
622macro.</para>
623
624</sect2>
625
626
627<sect2 id="writing-tools.advice" xreflabel="Words of Advice">
628<title>Words of Advice</title>
629
630<para>Writing and debugging tools is not trivial. Here are some
631suggestions for solving common problems.</para>
632
633
634<sect3 id="writing-tools.segfaults">
635<title>Segmentation Faults</title>
636
637<para>If you are getting segmentation faults in C functions used
638by your tool, the usual GDB command:</para>
639<screen><![CDATA[
640 gdb <prog> core]]></screen>
641<para>usually gives the location of the segmentation fault.</para>
642
643</sect3>
644
645
646<sect3 id="writing-tools.debugfns">
647<title>Debugging C functions</title>
648
649<para>If you want to debug C functions used by your tool, you can
650attach GDB to Valgrind with some effort:</para>
651<orderedlist>
652 <listitem>
653 <para>Enable the following code in
654 <filename>coregrind/vg_main.c</filename> by changing
655 <computeroutput>if (0)</computeroutput>
656 into <computeroutput>if (1)</computeroutput>:
657<programlisting><![CDATA[
658 /* Hook to delay things long enough so we can get the pid and
659 attach GDB in another shell. */
660 if (0) {
661 Int p, q;
662 for ( p = 0; p < 50000; p++ )
663 for ( q = 0; q < 50000; q++ ) ;
664 }]]></programlisting>
665 and rebuild Valgrind.</para>
666 </listitem>
667
668 <listitem>
669 <para>Then run:</para>
670<programlisting><![CDATA[
671 valgrind <prog>]]></programlisting>
672 <para>Valgrind starts the program, printing its process id, and
673 then delays for a few seconds (you may have to change the loop
674 bounds to get a suitable delay).</para>
675 </listitem>
676
677 <listitem>
678 <para>In a second shell run:</para>
679<programlisting><![CDATA[
680 gdb <prog pid>]]></programlisting>
681 </listitem>
682
683</orderedlist>
684
685<para>GDB may be able to give you useful information. Note that
686by default most of the system is built with
687<computeroutput>-fomit-frame-pointer</computeroutput>, and you'll
688need to get rid of this to extract useful tracebacks from GDB.</para>
689
690</sect3>
691
692
693<sect3 id="writing-tools.ucode-probs">
694<title>UCode Instrumentation Problems</title>
695
696<para>If you are having problems with your UCode instrumentation,
697it's likely that GDB won't be able to help at all. In this case,
698Valgrind's <computeroutput>--trace-codegen</computeroutput>
699option is invaluable for observing the results of
700instrumentation.</para>
701
702</sect3>
703
704
705<sect3 id="writing-tools.misc">
706<title>Miscellaneous</title>
707
708<para>If you just want to know whether a program point has been
709reached, using the <computeroutput>OINK</computeroutput> macro
njnc4fcca32004-12-01 00:02:36 +0000710(in <filename>include/tool.h</filename>) can be easier than
njn3e986b22004-11-30 10:43:45 +0000711using GDB.</para>
712
713<para>The other debugging command line options can be useful too
714(run <computeroutput>valgrind -h</computeroutput> for the
715list).</para>
716
717</sect3>
718
719</sect2>
720
721</sect1>
722
723
724
725<sect1 id="writing-tools.advtopics" xreflabel="Advanced Topics">
726<title>Advanced Topics</title>
727
728<para>Once a tool becomes more complicated, there are some extra
729things you may want/need to do.</para>
730
731<sect2 id="writing-tools.suppressions" xreflabel="Suppressions">
732<title>Suppressions</title>
733
734<para>If your tool reports errors and you want to suppress some
735common ones, you can add suppressions to the suppression files.
736The relevant files are
737<computeroutput>valgrind/*.supp</computeroutput>; the final
738suppression file is aggregated from these files by combining the
739relevant <computeroutput>.supp</computeroutput> files depending
740on the versions of linux, X and glibc on a system.</para>
741
742<para>Suppression types have the form
743<computeroutput>tool_name:suppression_name</computeroutput>. The
744<computeroutput>tool_name</computeroutput> here is the name you
745specify for the tool during initialisation with
746<computeroutput>VG_(details_name)()</computeroutput>.</para>
747
748</sect2>
749
750
751<!--
752<sect2 id="writing-tools.docs" xreflabel="Documentation">
753<title>Documentation</title>
754
755<para>As of version &rel-version;, Valgrind documentation has
756been converted to XML. Why?
757See <ulink url="http://www.ucc.ie/xml/">The XML FAQ</ulink>.
758</para>
759
760
761<sect3 id="writing-tools.xml" xreflabel="The XML Toolchain">
762<title>The XML Toolchain</title>
763
764<para>If you are feeling conscientious and want to write some
765documentation for your tool, please use XML. The Valgrind
766Docs use the following toolchain and versions:</para>
767
768<programlisting>
769 xmllint: using libxml version 20607
770 xsltproc: using libxml 20607, libxslt 10102 and libexslt 802
771 pdfxmltex: pdfTeX (Web2C 7.4.5) 3.14159-1.10b
772 pdftops: version 3.00
773 DocBook: version 4.2
774</programlisting>
775
776<para><command>Latency:</command> you should note that latency is
777a big problem: DocBook is constantly being updated, but the tools
778tend to lag behind somewhat. It is important that the versions
779get on with each other, so if you decide to upgrade something,
780then you need to ascertain whether things still work nicely -
781this *cannot* be assumed.</para>
782
783<para><command>Stylesheets:</command> The Valgrind docs use
784various custom stylesheet layers, all of which are in
785<computeroutput>valgrind/docs/lib/</computeroutput>. You
786shouldn't need to modify these in any way.</para>
787
788<para><command>Catalogs:</command> Assuming that you have the
789various tools listed above installed, you will probably need to
790modify
791<computeroutput>valgrind/docs/lib/vg-catalog.xml</computeroutput>
792so that the parser can find your DocBook installation. Catalogs
793provide a mapping from generic addresses to specific local
794directories on a given machine. Just add another
795<computeroutput>group</computeroutput> to this file, reflecting
796your local installation.</para>
797
798</sect3>
799
800
801<sect3 id="writing-tools.writing" xreflabel="Writing the Documentation">
802<title>Writing the Documentation</title>
803
804<para>If you aren't confident using XML, or you have problems
805with the toolchain, then write your documentation in text format,
806email it to
807<computeroutput>valgrind@valgrind.org</computeroutput>, and
808someone will convert it to XML for you. Otherwise, follow these
809steps (using <computeroutput>foobar</computeroutput> as the
810example tool name again):</para>
811
812<orderedlist>
813
814 <listitem>
815 <para>Make a directory
816 <computeroutput>valgrind/foobar/docs/</computeroutput>.</para>
817 </listitem>
818
819 <listitem>
820 <para>Copy the xml tool documentation template file
821 <computeroutput>valgrind/docs/xml/tool-template.xml</computeroutput>
822 to <computeroutput>foobar/docs/</computeroutput>, and rename it
823 to
824 <computeroutput>foobar/docs/fb-manual.xml</computeroutput>.</para>
825 <para><command>Note</command>: there is a *really stupid* tetex
826 bug with underscores in filenames, so don't use '_'.</para>
827 </listitem>
828
829 <listitem>
830 <para>Write the documentation. There are some helpful bits and
831 pieces on using xml markup in
832 <filename>valgrind/docs/xml/xml_help.txt</filename>.</para>
833 </listitem>
834
835 <listitem>
836 <para>Validate <computeroutput>foobar/docs/fb-manual.xml</computeroutput>
837 using the shell script
838 <filename>valgrind/docs/lib/xmlproc.sh</filename>.</para>
839<screen><![CDATA[
840% cd valgrind/docs/lib/
841% ./xmlproc.sh -valid ../../foobar/docs/fb-manual.xml
842]]></screen>
843
844 <para>If you have linked to other documents in the Valgrind
845 Documentation Set, you will get errors of the form:</para>
846
847<screen><![CDATA[
848fb-manual.xml:1632: element xref: validity error :
849 IDREF attribute linkend references an unknown ID "mc-tech-docs"
850]]></screen>
851
852 <para>Ignore (only) these - they will disappear when
853 <filename>fb-manual.xml</filename> is integrated into the
854 Set.</para>
855
856 <para>Because the xml toolchain is fragile, it is important to
857 ensure that <computeroutput>fb-manual.xml</computeroutput> won't
858 break the documentation set build. Note that just because an
859 xml file happily transforms to html does not necessarily mean
860 the same holds true for pdf/ps.</para>
861 </listitem>
862
863 <listitem>
864 <para>You can (re-)generate <filename>fb-manual.html</filename>
865 while you are writing <filename>fb-manual.xml</filename> to help
866 you see how it's looking. The generated file
867 <filename>fb-manual.html</filename> will be output in
868 <computeroutput>foobar/docs/</computeroutput>.</para>
869
870<screen><![CDATA[
871% ./xmlproc.sh -html ../../foobar/docs/fb-manual.xml
872]]></screen>
873 </listitem>
874
875 <listitem>
876 <para>When you have finished, generate html, pdf and ps output
877 to check all is well:</para>
878
879<screen><![CDATA[
880% cp ../../foobar/fb-manual.xml .
881% ./xmlproc.sh -test fb-manual.xml
882]]></screen>
883
884 <para>Check the output files (<filename>index.html,
885 fb-manual.pdf, fb-manual.ps</filename>) in
886 <computeroutput>/lib/test/</computeroutput> with the relevant
887 viewers. When you are happy and have finished tinkering with
888 <computeroutput>fb-manual.xml</computeroutput>:</para>
889
890<screen><![CDATA[
891% ./xmlproc.sh -clean fb-manual.xml
892]]></screen>
893</listitem>
894
895 <listitem>
896 <para>In order for your documentation to be included in the
897 User Manual, the relevant entries must be made in
898 <filename>/valgrind/docs/xml/vg-bookset.xml</filename> in this
899 format (hopefully, it should be pretty obvious):</para>
900
901<programlisting><![CDATA[
902<!ENTITY fb-manual SYSTEM "../../foobar/docs/fb-manual.xml">
903... ...
904&fb-manual;
905]]></programlisting>
906
907 <para>Send a patch for this to
908 <computeroutput>valgrind@valgrind.org</computeroutput>.</para>
909
910 <para>To achieve true anality, try for a full doc-set build:</para>
911<screen><![CDATA[
912% cd valgrind/docs/
913% make all
914]]></screen>
915 </listitem>
916
917</orderedlist>
918
919</sect3>
920
921</sect2>
922-->
923<sect2 id="writing-tools.docs" xreflabel="Documentation">
924<title>Documentation</title>
925
926<para>As of version &rel-version;, Valgrind documentation has
927been converted to XML. Why?
928See <ulink url="http://www.ucc.ie/xml/">The XML FAQ</ulink>.
929</para>
930
931
932<sect3 id="writing-tools.xml" xreflabel="The XML Toolchain">
933<title>The XML Toolchain</title>
934
935<para>If you are feeling conscientious and want to write some
936documentation for your tool, please use XML. The Valgrind
937Docs use the following toolchain and versions:</para>
938
939<programlisting>
940 xmllint: using libxml version 20607
941 xsltproc: using libxml 20607, libxslt 10102 and libexslt 802
942 pdfxmltex: pdfTeX (Web2C 7.4.5) 3.14159-1.10b
943 pdftops: version 3.00
944 DocBook: version 4.2
945</programlisting>
946
947<para><command>Latency:</command> you should note that latency is
948a big problem: DocBook is constantly being updated, but the tools
949tend to lag behind somewhat. It is important that the versions
950get on with each other, so if you decide to upgrade something,
951then you need to ascertain whether things still work nicely -
952this *cannot* be assumed.</para>
953
954<para><command>Stylesheets:</command> The Valgrind docs use
955various custom stylesheet layers, all of which are in
956<computeroutput>valgrind/docs/lib/</computeroutput>. You
957shouldn't need to modify these in any way.</para>
958
959<para><command>Catalogs:</command> Assuming that you have the
960various tools listed above installed, you will probably need to
961modify
962<computeroutput>valgrind/docs/lib/vg-catalog.xml</computeroutput>
963so that the parser can find your DocBook installation. Catalogs
964provide a mapping from generic addresses to specific local
965directories on a given machine. Just add another
966<computeroutput>group</computeroutput> to this file, reflecting
967your local installation.</para>
968
969</sect3>
970
971
972<sect3 id="writing-tools.writing" xreflabel="Writing the Documentation">
973<title>Writing the Documentation</title>
974
975<para>Follow these steps (using <computeroutput>foobar</computeroutput>
976as the example tool name again):</para>
977
978<orderedlist>
979
980 <listitem>
981 <para>Make a directory
982 <computeroutput>valgrind/foobar/docs/</computeroutput>.</para>
983 </listitem>
984
985 <listitem>
986 <para>Copy the XML documentation file for the tool Nulgrind from
987 <computeroutput>valgrind/none/docs/nl-manual.xml</computeroutput>
988 to <computeroutput>foobar/docs/</computeroutput>, and rename it
989 to
990 <computeroutput>foobar/docs/fb-manual.xml</computeroutput>.</para>
991 <para><command>Note</command>: there is a *really stupid* tetex
992 bug with underscores in filenames, so don't use '_'.</para>
993 </listitem>
994
995 <listitem>
996 <para>Write the documentation. There are some helpful bits and
997 pieces on using xml markup in
998 <filename>valgrind/docs/xml/xml_help.txt</filename>.</para>
999 </listitem>
1000
1001 <listitem>
1002 <para>Include it in the User Manual by adding the relevant entry must
1003 be added to <filename>valgrind/docs/xml/manual.xml</filename>. Copy
1004 and edit an existing entry.</para>
1005 </listitem>
1006
1007 <listitem>
1008 <para>Validate <computeroutput>foobar/docs/fb-manual.xml</computeroutput>
1009 using the following command from within <filename>valgrind/docs/</filename>:
1010 </para>
1011<screen><![CDATA[
1012% make valid
1013]]></screen>
1014
1015 <para>You will probably get errors that look like this:</para>
1016
1017<screen><![CDATA[
1018./xml/index.xml:5: element chapter: validity error : No declaration for
1019attribute base of element chapter
1020]]></screen>
1021
1022 <para>Ignore (only) these -- they're not important.</para>
1023
1024 <para>Because the xml toolchain is fragile, it is important to
1025 ensure that <filename>fb-manual.xml</filename> won't
1026 break the documentation set build. Note that just because an
1027 xml file happily transforms to html does not necessarily mean
1028 the same holds true for pdf/ps.</para>
1029 </listitem>
1030
1031 <listitem>
1032 <para>You can (re-)generate the HTML docs
1033 while you are writing <filename>fb-manual.xml</filename> to help
1034 you see how it's looking. The generated files end up in
1035 <filename>valgrind/docs/html/</filename>. Use the following
1036 command, within <filename>valgrind/docs/</filename>:</para>
1037
1038<screen><![CDATA[
1039% make html-docs
1040]]></screen>
1041 </listitem>
1042
1043 <listitem>
1044 <para>When you have finished, also generate pdf and ps output
1045 to check all is well, from within <filename>valgrind/docs/</filename>:
1046 </para>
1047
1048<screen><![CDATA[
1049% make print-docs
1050]]></screen>
1051
1052 <para>Check the output <filename>.pdf</filename> and
1053 <filename>.ps</filename> files in
1054 <computeroutput>valgrind/docs/print/</computeroutput>.
1055 </para>
1056</listitem>
1057
1058</orderedlist>
1059
1060</sect3>
1061
1062</sect2>
1063
1064
1065<sect2 id="writing-tools.regtests" xreflabel="Regression Tests">
1066<title>Regression Tests</title>
1067
1068<para>Valgrind has some support for regression tests. If you
1069want to write regression tests for your tool:</para>
1070
1071<orderedlist>
1072 <listitem>
1073 <para>Make a directory
1074 <computeroutput>foobar/tests/</computeroutput>.</para>
1075 </listitem>
1076
1077 <listitem>
1078 <para>Edit <computeroutput>foobar/Makefile.am</computeroutput>,
1079 adding <computeroutput>tests</computeroutput> to the
1080 <computeroutput>SUBDIRS</computeroutput> variable.</para>
1081 </listitem>
1082
1083 <listitem>
1084 <para>Edit <computeroutput>configure.in</computeroutput>,
1085 adding <computeroutput>foobar/tests/Makefile</computeroutput>
1086 to the <computeroutput>AC_OUTPUT</computeroutput> list.</para>
1087 </listitem>
1088
1089 <listitem>
1090 <para>Write
1091 <computeroutput>foobar/tests/Makefile.am</computeroutput>. Use
1092 <computeroutput>memcheck/tests/Makefile.am</computeroutput> as
1093 an example.</para>
1094 </listitem>
1095
1096 <listitem>
1097 <para>Write the tests, <computeroutput>.vgtest</computeroutput>
1098 test description files,
1099 <computeroutput>.stdout.exp</computeroutput> and
1100 <computeroutput>.stderr.exp</computeroutput> expected output
1101 files. (Note that Valgrind's output goes to stderr.) Some
1102 details on writing and running tests are given in the comments
1103 at the top of the testing script
1104 <computeroutput>tests/vg_regtest</computeroutput>.</para>
1105 </listitem>
1106
1107 <listitem>
1108 <para>Write a filter for stderr results
1109 <computeroutput>foobar/tests/filter_stderr</computeroutput>.
1110 It can call the existing filters in
1111 <computeroutput>tests/</computeroutput>. See
1112 <computeroutput>memcheck/tests/filter_stderr</computeroutput>
1113 for an example; in particular note the
1114 <computeroutput>$dir</computeroutput> trick that ensures the
1115 filter works correctly from any directory.</para>
1116 </listitem>
1117
1118</orderedlist>
1119
1120</sect2>
1121
1122
1123
1124<sect2 id="writing-tools.profiling" xreflabel="Profiling">
1125<title>Profiling</title>
1126
njn31066fd2005-03-26 00:42:02 +00001127<para>Nb: as of 25-Mar-2005, the profiling is broken, and has been
1128for a long time...</para>
1129
njn3e986b22004-11-30 10:43:45 +00001130<para>To do simple tick-based profiling of a tool, include the
1131line:</para>
1132<programlisting><![CDATA[
1133 #include "vg_profile.c"]]></programlisting>
1134<para>in the tool somewhere, and rebuild (you may have to
1135<computeroutput>make clean</computeroutput> first). Then run
1136Valgrind with the <computeroutput>--profile=yes</computeroutput>
1137option.</para>
1138
1139<para>The profiler is stack-based; you can register a profiling
1140event with
njn31066fd2005-03-26 00:42:02 +00001141<computeroutput>VG_(register_profile_event)()</computeroutput>
njn3e986b22004-11-30 10:43:45 +00001142and then use the <computeroutput>VGP_PUSHCC</computeroutput> and
1143<computeroutput>VGP_POPCC</computeroutput> macros to record time
1144spent doing certain things. New profiling event numbers must not
1145overlap with the core profiling event numbers. See
njnc4fcca32004-12-01 00:02:36 +00001146<filename>include/tool.h</filename> for details and Memcheck
njn3e986b22004-11-30 10:43:45 +00001147for an example.</para>
1148
1149</sect2>
1150
1151
1152
1153<sect2 id="writing-tools.mkhackery" xreflabel="Other Makefile Hackery">
1154<title>Other Makefile Hackery</title>
1155
1156<para>If you add any directories under
1157<computeroutput>valgrind/foobar/</computeroutput>, you will need
1158to add an appropriate <filename>Makefile.am</filename> to it, and
1159add a corresponding entry to the
1160<computeroutput>AC_OUTPUT</computeroutput> list in
1161<filename>valgrind/configure.in</filename>.</para>
1162
1163<para>If you add any scripts to your tool (see Cachegrind for an
1164example) you need to add them to the
1165<computeroutput>bin_SCRIPTS</computeroutput> variable in
1166<filename>valgrind/foobar/Makefile.am</filename>.</para>
1167
1168</sect2>
1169
1170
1171
1172<sect2 id="writing-tools.ifacever" xreflabel="Core/tool Interface Versions">
1173<title>Core/tool Interface Versions</title>
1174
1175<para>In order to allow for the core/tool interface to evolve
1176over time, Valgrind uses a basic interface versioning system.
1177All a tool has to do is use the
1178<computeroutput>VG_DETERMINE_INTERFACE_VERSION</computeroutput>
1179macro exactly once in its code. If not, a link error will occur
1180when the tool is built.</para>
1181
1182<para>The interface version number has the form X.Y. Changes in
1183Y indicate binary compatible changes. Changes in X indicate
1184binary incompatible changes. If the core and tool has the same
1185major version number X they should work together. If X doesn't
1186match, Valgrind will abort execution with an explanation of the
1187problem.</para>
1188
1189<para>This approach was chosen so that if the interface changes
1190in the future, old tools won't work and the reason will be
1191clearly explained, instead of possibly crashing mysteriously. We
1192have attempted to minimise the potential for binary incompatible
1193changes by means such as minimising the use of naked structs in
1194the interface.</para>
1195
1196</sect2>
1197
1198</sect1>
1199
1200
1201
1202<sect1 id="writing-tools.finalwords" xreflabel="Final Words">
1203<title>Final Words</title>
1204
1205<para>This whole core/tool business is under active development,
1206although it's slowly maturing.</para>
1207
1208<para>The first consequence of this is that the core/tool
1209interface will continue to change in the future; we have no
1210intention of freezing it and then regretting the inevitable
1211stupidities. Hopefully most of the future changes will be to add
1212new features, hooks, functions, etc, rather than to change old
1213ones, which should cause a minimum of trouble for existing tools,
1214and we've put some effort into future-proofing the interface to
1215avoid binary incompatibility. But we can't guarantee anything.
1216The versioning system should catch any incompatibilities. Just
1217something to be aware of.</para>
1218
1219<para>The second consequence of this is that we'd love to hear
1220your feedback about it:</para>
1221
1222<itemizedlist>
1223 <listitem>
1224 <para>If you love it or hate it</para>
1225 </listitem>
1226 <listitem>
1227 <para>If you find bugs</para>
1228 </listitem>
1229 <listitem>
1230 <para>If you write a tool</para>
1231 </listitem>
1232 <listitem>
1233 <para>If you have suggestions for new features, needs,
1234 trackable events, functions</para>
1235 </listitem>
1236 <listitem>
1237 <para>If you have suggestions for making tools easier to
1238 write</para>
1239 </listitem>
1240 <listitem>
1241 <para>If you have suggestions for improving this
1242 documentation</para>
1243 </listitem>
1244 <listitem>
1245 <para>If you don't understand something</para>
1246 </listitem>
1247</itemizedlist>
1248
1249<para>or anything else!</para>
1250
1251<para>Happy programming.</para>
1252
1253</sect1>
1254
1255</chapter>