blob: 06a377f61c580287b696405a43cc66013222f2c8 [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
njn5cf77912005-07-10 15:10:29 +0000315<para>To write your own tool, you'll need the Valgrind source code.
316A normal source distribution should do, although you might want to
317check out the latest code from the Subversion repository. See the
318information about how to do so at <ulink url="http://www.valgrind.org/">the
319Valgrind website</ulink>.</para>
njn3e986b22004-11-30 10:43:45 +0000320
321</sect2>
322
323
324<sect2 id="writing-tools.gettingstarted" xreflabel="Getting started">
325<title>Getting started</title>
326
327<para>Valgrind uses GNU <computeroutput>automake</computeroutput>
328and <computeroutput>autoconf</computeroutput> for the creation of
329Makefiles and configuration. But don't worry, these instructions
330should be enough to get you started even if you know nothing
331about those tools.</para>
332
333<para>In what follows, all filenames are relative to Valgrind's
334top-level directory <computeroutput>valgrind/</computeroutput>.</para>
335
336<orderedlist>
337 <listitem>
338 <para>Choose a name for the tool, and an abbreviation that can
339 be used as a short prefix. We'll use
340 <computeroutput>foobar</computeroutput> and
341 <computeroutput>fb</computeroutput> as an example.</para>
342 </listitem>
343
344 <listitem>
345 <para>Make a new directory
346 <computeroutput>foobar/</computeroutput> which will hold the
347 tool.</para>
348 </listitem>
349
350 <listitem>
351 <para>Copy <computeroutput>none/Makefile.am</computeroutput>
352 into <computeroutput>foobar/</computeroutput>. Edit it by
353 replacing all occurrences of the string
354 <computeroutput>"none"</computeroutput> with
355 <computeroutput>"foobar"</computeroutput> and the one
356 occurrence of the string <computeroutput>"nl_"</computeroutput>
357 with <computeroutput>"fb_"</computeroutput>. It might be worth
358 trying to understand this file, at least a little; you might
359 have to do more complicated things with it later on. In
360 particular, the name of the
njnc4fcca32004-12-01 00:02:36 +0000361 <computeroutput>vgtool_foobar_so_SOURCES</computeroutput>
njn3e986b22004-11-30 10:43:45 +0000362 variable determines the name of the tool's shared object, which
363 determines what name must be passed to the
364 <computeroutput>--tool</computeroutput> option to use the
365 tool.</para>
366 </listitem>
367
368 <listitem>
369 <para>Copy <filename>none/nl_main.c</filename> into
370 <computeroutput>foobar/</computeroutput>, renaming it as
371 <filename>fb_main.c</filename>. Edit it by changing the lines
njn51d827b2005-05-09 01:02:08 +0000372 in <computeroutput>pre_clo_init()</computeroutput> to
njn3e986b22004-11-30 10:43:45 +0000373 something appropriate for the tool. These fields are used in
374 the startup message, except for
375 <computeroutput>bug_reports_to</computeroutput> which is used
376 if a tool assertion fails.</para>
377 </listitem>
378
379 <listitem>
380 <para>Edit <computeroutput>Makefile.am</computeroutput>,
381 adding the new directory
382 <computeroutput>foobar</computeroutput> to the
383 <computeroutput>SUBDIRS</computeroutput> variable.</para>
384 </listitem>
385
386 <listitem>
387 <para>Edit <computeroutput>configure.in</computeroutput>,
388 adding <computeroutput>foobar/Makefile</computeroutput> to the
389 <computeroutput>AC_OUTPUT</computeroutput> list.</para>
390 </listitem>
391
392 <listitem>
393 <para>Run:</para>
394<programlisting><![CDATA[
395 autogen.sh
396 ./configure --prefix=`pwd`/inst
397 make install]]></programlisting>
398
399 <para>It should automake, configure and compile without
400 errors, putting copies of the tool's shared object
njnc4fcca32004-12-01 00:02:36 +0000401 <computeroutput>vgtool_foobar.so</computeroutput> in
njn3e986b22004-11-30 10:43:45 +0000402 <computeroutput>foobar/</computeroutput> and
403 <computeroutput>inst/lib/valgrind/</computeroutput>.</para>
404 </listitem>
405
406 <listitem>
407 <para>You can test it with a command like:</para>
408<programlisting><![CDATA[
409 inst/bin/valgrind --tool=foobar date]]></programlisting>
410
411 <para>(almost any program should work;
412 <computeroutput>date</computeroutput> is just an example).
413 The output should be something like this:</para>
414<programlisting><![CDATA[
415 ==738== foobar-0.0.1, a foobarring tool for x86-linux.
416 ==738== Copyright (C) 1066AD, and GNU GPL'd, by J. Random Hacker.
417 ==738== Built with valgrind-1.1.0, a program execution monitor.
418 ==738== Copyright (C) 2000-2003, and GNU GPL'd, by Julian Seward.
419 ==738== Estimated CPU clock rate is 1400 MHz
420 ==738== For more details, rerun with: -v
421 ==738== Wed Sep 25 10:31:54 BST 2002
422 ==738==]]></programlisting>
423
424 <para>The tool does nothing except run the program
425 uninstrumented.</para>
426 </listitem>
427
428</orderedlist>
429
430<para>These steps don't have to be followed exactly - you can
431choose different names for your source files, and use a different
432<computeroutput>--prefix</computeroutput> for
433<computeroutput>./configure</computeroutput>.</para>
434
435<para>Now that we've setup, built and tested the simplest
436possible tool, onto the interesting stuff...</para>
437
438</sect2>
439
440
441
442<sect2 id="writing-tools.writingcode" xreflabel="Writing the Code">
443<title>Writing the code</title>
444
445<para>A tool must define at least these four functions:</para>
446<programlisting><![CDATA[
njn51d827b2005-05-09 01:02:08 +0000447 pre_clo_init()
448 post_clo_init()
449 instrument()
450 fini()]]></programlisting>
njn3e986b22004-11-30 10:43:45 +0000451
452<para>Also, it must use the macro
453<computeroutput>VG_DETERMINE_INTERFACE_VERSION</computeroutput>
454exactly once in its source code. If it doesn't, you will get a
455link error involving
njn51d827b2005-05-09 01:02:08 +0000456<computeroutput>VG_(tool_interface_version)</computeroutput>.
njn3e986b22004-11-30 10:43:45 +0000457This macro is used to ensure the core/tool interface used by the
458core and a plugged-in tool are binary compatible.</para>
459
460<para>In addition, if a tool wants to use some of the optional
461services provided by the core, it may have to define other
462functions.</para>
463
464</sect2>
465
466
467
468<sect2 id="writing-tools.init" xreflabel="Initialisation">
469<title>Initialisation</title>
470
471<para>Most of the initialisation should be done in
njn51d827b2005-05-09 01:02:08 +0000472<computeroutput>pre_clo_init()</computeroutput>. Only use
473<computeroutput>post_clo_init()</computeroutput> if a tool
njn3e986b22004-11-30 10:43:45 +0000474provides command line options and must do some initialisation
475after option processing takes place
476(<computeroutput>"clo"</computeroutput> stands for "command line
477options").</para>
478
479<para>First of all, various "details" need to be set for a tool,
480using the functions
481<computeroutput>VG_(details_*)()</computeroutput>. Some are all
482compulsory, some aren't. Some are used when constructing the
483startup message,
484<computeroutput>detail_bug_reports_to</computeroutput> is used if
njnc4fcca32004-12-01 00:02:36 +0000485<computeroutput>VG_(tool_panic)()</computeroutput> is ever
njn3e986b22004-11-30 10:43:45 +0000486called, or a tool assertion fails. Others have other uses.</para>
487
488<para>Second, various "needs" can be set for a tool, using the
489functions <computeroutput>VG_(needs_*)()</computeroutput>. They
490are mostly booleans, and can be left untouched (they default to
491<computeroutput>False</computeroutput>). They determine whether
492a tool can do various things such as: record, report and suppress
493errors; process command line options; wrap system calls; record
494extra information about malloc'd blocks, etc.</para>
495
496<para>For example, if a tool wants the core's help in recording
497and reporting errors, it must set the
njnc4fcca32004-12-01 00:02:36 +0000498<computeroutput>tool_errors</computeroutput> need to
njn3e986b22004-11-30 10:43:45 +0000499<computeroutput>True</computeroutput>, and then provide
500definitions of six functions for comparing errors, printing out
501errors, reading suppressions from a suppressions file, etc.
502While writing these functions requires some work, it's much less
503than doing error handling from scratch because the core is doing
504most of the work. See the type
505<computeroutput>VgNeeds</computeroutput> in
njnc4fcca32004-12-01 00:02:36 +0000506<filename>include/tool.h</filename> for full details of all
njn3e986b22004-11-30 10:43:45 +0000507the needs.</para>
508
509<para>Third, the tool can indicate which events in core it wants
510to be notified about, using the functions
511<computeroutput>VG_(track_*)()</computeroutput>. These include
512things such as blocks of memory being malloc'd, the stack pointer
513changing, a mutex being locked, etc. If a tool wants to know
514about this, it should set the relevant pointer in the structure
515to point to a function, which will be called when that event
516happens.</para>
517
518<para>For example, if the tool want to be notified when a new
519block of memory is malloc'd, it should call
520<computeroutput>VG_(track_new_mem_heap)()</computeroutput> with
521an appropriate function pointer, and the assigned function will
522be called each time this happens.</para>
523
524<para>More information about "details", "needs" and "trackable
525events" can be found in
njnc4fcca32004-12-01 00:02:36 +0000526<filename>include/tool.h</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000527
528</sect2>
529
530
531
532<sect2 id="writing-tools.instr" xreflabel="Instrumentation">
533<title>Instrumentation</title>
534
njn51d827b2005-05-09 01:02:08 +0000535<para><computeroutput>instrument()</computeroutput> is the
njn3e986b22004-11-30 10:43:45 +0000536interesting one. It allows you to instrument
537<emphasis>UCode</emphasis>, which is Valgrind's RISC-like
538intermediate language. UCode is described in
539<xref linkend="mc-tech-docs.ucode"/>.</para>
540
541<para>The easiest way to instrument UCode is to insert calls to C
542functions when interesting things happen. See the tool "Lackey"
543(<filename>lackey/lk_main.c</filename>) for a simple example of
544this, or Cachegrind (<filename>cachegrind/cg_main.c</filename>)
545for a more complex example.</para>
546
547<para>A much more complicated way to instrument UCode, albeit one
548that might result in faster instrumented programs, is to extend
549UCode with new UCode instructions. This is recommended for
550advanced Valgrind hackers only! See Memcheck for an example.</para>
551
552</sect2>
553
554
555
556<sect2 id="writing-tools.fini" xreflabel="Finalisation">
557<title>Finalisation</title>
558
559<para>This is where you can present the final results, such as a
560summary of the information collected. Any log files should be
561written out at this point.</para>
562
563</sect2>
564
565
566
567<sect2 id="writing-tools.otherinfo" xreflabel="Other Important Information">
568<title>Other Important Information</title>
569
570<para>Please note that the core/tool split infrastructure is
571quite complex and not brilliantly documented. Here are some
572important points, but there are undoubtedly many others that I
573should note but haven't thought of.</para>
574
njnc4fcca32004-12-01 00:02:36 +0000575<para>The file <filename>include/tool.h</filename> contains
njn3e986b22004-11-30 10:43:45 +0000576all the types, macros, functions, etc. that a tool should
577(hopefully) need, and is the only <filename>.h</filename> file a
578tool should need to
579<computeroutput>#include</computeroutput>.</para>
580
581<para>In particular, you probably shouldn't use anything from the
582C library (there are deep reasons for this, trust us). Valgrind
583provides an implementation of a reasonable subset of the C
584library, details of which are in
njnc4fcca32004-12-01 00:02:36 +0000585<filename>tool.h</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000586
587<para>Similarly, when writing a tool, you shouldn't need to look
588at any of the code in Valgrind's core. Although it might be
589useful sometimes to help understand something.</para>
590
njnc4fcca32004-12-01 00:02:36 +0000591<para><filename>tool.h</filename> has a reasonable amount of
njn3e986b22004-11-30 10:43:45 +0000592documentation in it that should hopefully be enough to get you
593going. But ultimately, the tools distributed (Memcheck,
594Addrcheck, Cachegrind, Lackey, etc.) are probably the best
595documentation of all, for the moment.</para>
596
597<para>Note that the <computeroutput>VG_</computeroutput> and
njnc4fcca32004-12-01 00:02:36 +0000598<computeroutput>TL_</computeroutput> macros are used heavily.
njn3e986b22004-11-30 10:43:45 +0000599These just prepend longer strings in front of names to avoid
600potential namespace clashes. We strongly recommend using the
njnc4fcca32004-12-01 00:02:36 +0000601<computeroutput>TL_</computeroutput> macro for any global
njn3e986b22004-11-30 10:43:45 +0000602functions and variables in your tool, or writing a similar
603macro.</para>
604
605</sect2>
606
607
608<sect2 id="writing-tools.advice" xreflabel="Words of Advice">
609<title>Words of Advice</title>
610
611<para>Writing and debugging tools is not trivial. Here are some
612suggestions for solving common problems.</para>
613
614
615<sect3 id="writing-tools.segfaults">
616<title>Segmentation Faults</title>
617
618<para>If you are getting segmentation faults in C functions used
619by your tool, the usual GDB command:</para>
620<screen><![CDATA[
621 gdb <prog> core]]></screen>
622<para>usually gives the location of the segmentation fault.</para>
623
624</sect3>
625
626
627<sect3 id="writing-tools.debugfns">
628<title>Debugging C functions</title>
629
630<para>If you want to debug C functions used by your tool, you can
631attach GDB to Valgrind with some effort:</para>
632<orderedlist>
633 <listitem>
634 <para>Enable the following code in
635 <filename>coregrind/vg_main.c</filename> by changing
636 <computeroutput>if (0)</computeroutput>
637 into <computeroutput>if (1)</computeroutput>:
638<programlisting><![CDATA[
639 /* Hook to delay things long enough so we can get the pid and
640 attach GDB in another shell. */
641 if (0) {
642 Int p, q;
643 for ( p = 0; p < 50000; p++ )
644 for ( q = 0; q < 50000; q++ ) ;
645 }]]></programlisting>
646 and rebuild Valgrind.</para>
647 </listitem>
648
649 <listitem>
650 <para>Then run:</para>
651<programlisting><![CDATA[
652 valgrind <prog>]]></programlisting>
653 <para>Valgrind starts the program, printing its process id, and
654 then delays for a few seconds (you may have to change the loop
655 bounds to get a suitable delay).</para>
656 </listitem>
657
658 <listitem>
659 <para>In a second shell run:</para>
660<programlisting><![CDATA[
661 gdb <prog pid>]]></programlisting>
662 </listitem>
663
664</orderedlist>
665
666<para>GDB may be able to give you useful information. Note that
667by default most of the system is built with
668<computeroutput>-fomit-frame-pointer</computeroutput>, and you'll
669need to get rid of this to extract useful tracebacks from GDB.</para>
670
671</sect3>
672
673
674<sect3 id="writing-tools.ucode-probs">
675<title>UCode Instrumentation Problems</title>
676
677<para>If you are having problems with your UCode instrumentation,
678it's likely that GDB won't be able to help at all. In this case,
679Valgrind's <computeroutput>--trace-codegen</computeroutput>
680option is invaluable for observing the results of
681instrumentation.</para>
682
683</sect3>
684
685
686<sect3 id="writing-tools.misc">
687<title>Miscellaneous</title>
688
689<para>If you just want to know whether a program point has been
690reached, using the <computeroutput>OINK</computeroutput> macro
njnc4fcca32004-12-01 00:02:36 +0000691(in <filename>include/tool.h</filename>) can be easier than
njn3e986b22004-11-30 10:43:45 +0000692using GDB.</para>
693
694<para>The other debugging command line options can be useful too
695(run <computeroutput>valgrind -h</computeroutput> for the
696list).</para>
697
698</sect3>
699
700</sect2>
701
702</sect1>
703
704
705
706<sect1 id="writing-tools.advtopics" xreflabel="Advanced Topics">
707<title>Advanced Topics</title>
708
709<para>Once a tool becomes more complicated, there are some extra
710things you may want/need to do.</para>
711
712<sect2 id="writing-tools.suppressions" xreflabel="Suppressions">
713<title>Suppressions</title>
714
715<para>If your tool reports errors and you want to suppress some
716common ones, you can add suppressions to the suppression files.
717The relevant files are
718<computeroutput>valgrind/*.supp</computeroutput>; the final
719suppression file is aggregated from these files by combining the
720relevant <computeroutput>.supp</computeroutput> files depending
721on the versions of linux, X and glibc on a system.</para>
722
723<para>Suppression types have the form
724<computeroutput>tool_name:suppression_name</computeroutput>. The
725<computeroutput>tool_name</computeroutput> here is the name you
726specify for the tool during initialisation with
727<computeroutput>VG_(details_name)()</computeroutput>.</para>
728
729</sect2>
730
731
732<!--
733<sect2 id="writing-tools.docs" xreflabel="Documentation">
734<title>Documentation</title>
735
736<para>As of version &rel-version;, Valgrind documentation has
737been converted to XML. Why?
738See <ulink url="http://www.ucc.ie/xml/">The XML FAQ</ulink>.
739</para>
740
741
742<sect3 id="writing-tools.xml" xreflabel="The XML Toolchain">
743<title>The XML Toolchain</title>
744
745<para>If you are feeling conscientious and want to write some
746documentation for your tool, please use XML. The Valgrind
747Docs use the following toolchain and versions:</para>
748
749<programlisting>
750 xmllint: using libxml version 20607
751 xsltproc: using libxml 20607, libxslt 10102 and libexslt 802
752 pdfxmltex: pdfTeX (Web2C 7.4.5) 3.14159-1.10b
753 pdftops: version 3.00
754 DocBook: version 4.2
755</programlisting>
756
757<para><command>Latency:</command> you should note that latency is
758a big problem: DocBook is constantly being updated, but the tools
759tend to lag behind somewhat. It is important that the versions
760get on with each other, so if you decide to upgrade something,
761then you need to ascertain whether things still work nicely -
762this *cannot* be assumed.</para>
763
764<para><command>Stylesheets:</command> The Valgrind docs use
765various custom stylesheet layers, all of which are in
766<computeroutput>valgrind/docs/lib/</computeroutput>. You
767shouldn't need to modify these in any way.</para>
768
769<para><command>Catalogs:</command> Assuming that you have the
770various tools listed above installed, you will probably need to
771modify
772<computeroutput>valgrind/docs/lib/vg-catalog.xml</computeroutput>
773so that the parser can find your DocBook installation. Catalogs
774provide a mapping from generic addresses to specific local
775directories on a given machine. Just add another
776<computeroutput>group</computeroutput> to this file, reflecting
777your local installation.</para>
778
779</sect3>
780
781
782<sect3 id="writing-tools.writing" xreflabel="Writing the Documentation">
783<title>Writing the Documentation</title>
784
785<para>If you aren't confident using XML, or you have problems
786with the toolchain, then write your documentation in text format,
787email it to
788<computeroutput>valgrind@valgrind.org</computeroutput>, and
789someone will convert it to XML for you. Otherwise, follow these
790steps (using <computeroutput>foobar</computeroutput> as the
791example tool name again):</para>
792
793<orderedlist>
794
795 <listitem>
796 <para>Make a directory
797 <computeroutput>valgrind/foobar/docs/</computeroutput>.</para>
798 </listitem>
799
800 <listitem>
801 <para>Copy the xml tool documentation template file
802 <computeroutput>valgrind/docs/xml/tool-template.xml</computeroutput>
803 to <computeroutput>foobar/docs/</computeroutput>, and rename it
804 to
805 <computeroutput>foobar/docs/fb-manual.xml</computeroutput>.</para>
806 <para><command>Note</command>: there is a *really stupid* tetex
807 bug with underscores in filenames, so don't use '_'.</para>
808 </listitem>
809
810 <listitem>
811 <para>Write the documentation. There are some helpful bits and
812 pieces on using xml markup in
813 <filename>valgrind/docs/xml/xml_help.txt</filename>.</para>
814 </listitem>
815
816 <listitem>
817 <para>Validate <computeroutput>foobar/docs/fb-manual.xml</computeroutput>
818 using the shell script
819 <filename>valgrind/docs/lib/xmlproc.sh</filename>.</para>
820<screen><![CDATA[
821% cd valgrind/docs/lib/
822% ./xmlproc.sh -valid ../../foobar/docs/fb-manual.xml
823]]></screen>
824
825 <para>If you have linked to other documents in the Valgrind
826 Documentation Set, you will get errors of the form:</para>
827
828<screen><![CDATA[
829fb-manual.xml:1632: element xref: validity error :
830 IDREF attribute linkend references an unknown ID "mc-tech-docs"
831]]></screen>
832
833 <para>Ignore (only) these - they will disappear when
834 <filename>fb-manual.xml</filename> is integrated into the
835 Set.</para>
836
837 <para>Because the xml toolchain is fragile, it is important to
838 ensure that <computeroutput>fb-manual.xml</computeroutput> won't
839 break the documentation set build. Note that just because an
840 xml file happily transforms to html does not necessarily mean
841 the same holds true for pdf/ps.</para>
842 </listitem>
843
844 <listitem>
845 <para>You can (re-)generate <filename>fb-manual.html</filename>
846 while you are writing <filename>fb-manual.xml</filename> to help
847 you see how it's looking. The generated file
848 <filename>fb-manual.html</filename> will be output in
849 <computeroutput>foobar/docs/</computeroutput>.</para>
850
851<screen><![CDATA[
852% ./xmlproc.sh -html ../../foobar/docs/fb-manual.xml
853]]></screen>
854 </listitem>
855
856 <listitem>
857 <para>When you have finished, generate html, pdf and ps output
858 to check all is well:</para>
859
860<screen><![CDATA[
861% cp ../../foobar/fb-manual.xml .
862% ./xmlproc.sh -test fb-manual.xml
863]]></screen>
864
865 <para>Check the output files (<filename>index.html,
866 fb-manual.pdf, fb-manual.ps</filename>) in
867 <computeroutput>/lib/test/</computeroutput> with the relevant
868 viewers. When you are happy and have finished tinkering with
869 <computeroutput>fb-manual.xml</computeroutput>:</para>
870
871<screen><![CDATA[
872% ./xmlproc.sh -clean fb-manual.xml
873]]></screen>
874</listitem>
875
876 <listitem>
877 <para>In order for your documentation to be included in the
878 User Manual, the relevant entries must be made in
879 <filename>/valgrind/docs/xml/vg-bookset.xml</filename> in this
880 format (hopefully, it should be pretty obvious):</para>
881
882<programlisting><![CDATA[
883<!ENTITY fb-manual SYSTEM "../../foobar/docs/fb-manual.xml">
884... ...
885&fb-manual;
886]]></programlisting>
887
888 <para>Send a patch for this to
889 <computeroutput>valgrind@valgrind.org</computeroutput>.</para>
890
891 <para>To achieve true anality, try for a full doc-set build:</para>
892<screen><![CDATA[
893% cd valgrind/docs/
894% make all
895]]></screen>
896 </listitem>
897
898</orderedlist>
899
900</sect3>
901
902</sect2>
903-->
904<sect2 id="writing-tools.docs" xreflabel="Documentation">
905<title>Documentation</title>
906
907<para>As of version &rel-version;, Valgrind documentation has
908been converted to XML. Why?
909See <ulink url="http://www.ucc.ie/xml/">The XML FAQ</ulink>.
910</para>
911
912
913<sect3 id="writing-tools.xml" xreflabel="The XML Toolchain">
914<title>The XML Toolchain</title>
915
916<para>If you are feeling conscientious and want to write some
917documentation for your tool, please use XML. The Valgrind
918Docs use the following toolchain and versions:</para>
919
920<programlisting>
921 xmllint: using libxml version 20607
922 xsltproc: using libxml 20607, libxslt 10102 and libexslt 802
923 pdfxmltex: pdfTeX (Web2C 7.4.5) 3.14159-1.10b
924 pdftops: version 3.00
925 DocBook: version 4.2
926</programlisting>
927
928<para><command>Latency:</command> you should note that latency is
929a big problem: DocBook is constantly being updated, but the tools
930tend to lag behind somewhat. It is important that the versions
931get on with each other, so if you decide to upgrade something,
932then you need to ascertain whether things still work nicely -
933this *cannot* be assumed.</para>
934
935<para><command>Stylesheets:</command> The Valgrind docs use
936various custom stylesheet layers, all of which are in
937<computeroutput>valgrind/docs/lib/</computeroutput>. You
938shouldn't need to modify these in any way.</para>
939
940<para><command>Catalogs:</command> Assuming that you have the
941various tools listed above installed, you will probably need to
942modify
943<computeroutput>valgrind/docs/lib/vg-catalog.xml</computeroutput>
944so that the parser can find your DocBook installation. Catalogs
945provide a mapping from generic addresses to specific local
946directories on a given machine. Just add another
947<computeroutput>group</computeroutput> to this file, reflecting
948your local installation.</para>
949
950</sect3>
951
952
953<sect3 id="writing-tools.writing" xreflabel="Writing the Documentation">
954<title>Writing the Documentation</title>
955
956<para>Follow these steps (using <computeroutput>foobar</computeroutput>
957as the example tool name again):</para>
958
959<orderedlist>
960
961 <listitem>
962 <para>Make a directory
963 <computeroutput>valgrind/foobar/docs/</computeroutput>.</para>
964 </listitem>
965
966 <listitem>
967 <para>Copy the XML documentation file for the tool Nulgrind from
968 <computeroutput>valgrind/none/docs/nl-manual.xml</computeroutput>
969 to <computeroutput>foobar/docs/</computeroutput>, and rename it
970 to
971 <computeroutput>foobar/docs/fb-manual.xml</computeroutput>.</para>
972 <para><command>Note</command>: there is a *really stupid* tetex
973 bug with underscores in filenames, so don't use '_'.</para>
974 </listitem>
975
976 <listitem>
977 <para>Write the documentation. There are some helpful bits and
978 pieces on using xml markup in
979 <filename>valgrind/docs/xml/xml_help.txt</filename>.</para>
980 </listitem>
981
982 <listitem>
983 <para>Include it in the User Manual by adding the relevant entry must
984 be added to <filename>valgrind/docs/xml/manual.xml</filename>. Copy
985 and edit an existing entry.</para>
986 </listitem>
987
988 <listitem>
989 <para>Validate <computeroutput>foobar/docs/fb-manual.xml</computeroutput>
990 using the following command from within <filename>valgrind/docs/</filename>:
991 </para>
992<screen><![CDATA[
993% make valid
994]]></screen>
995
996 <para>You will probably get errors that look like this:</para>
997
998<screen><![CDATA[
999./xml/index.xml:5: element chapter: validity error : No declaration for
1000attribute base of element chapter
1001]]></screen>
1002
1003 <para>Ignore (only) these -- they're not important.</para>
1004
1005 <para>Because the xml toolchain is fragile, it is important to
1006 ensure that <filename>fb-manual.xml</filename> won't
1007 break the documentation set build. Note that just because an
1008 xml file happily transforms to html does not necessarily mean
1009 the same holds true for pdf/ps.</para>
1010 </listitem>
1011
1012 <listitem>
1013 <para>You can (re-)generate the HTML docs
1014 while you are writing <filename>fb-manual.xml</filename> to help
1015 you see how it's looking. The generated files end up in
1016 <filename>valgrind/docs/html/</filename>. Use the following
1017 command, within <filename>valgrind/docs/</filename>:</para>
1018
1019<screen><![CDATA[
1020% make html-docs
1021]]></screen>
1022 </listitem>
1023
1024 <listitem>
1025 <para>When you have finished, also generate pdf and ps output
1026 to check all is well, from within <filename>valgrind/docs/</filename>:
1027 </para>
1028
1029<screen><![CDATA[
1030% make print-docs
1031]]></screen>
1032
1033 <para>Check the output <filename>.pdf</filename> and
1034 <filename>.ps</filename> files in
1035 <computeroutput>valgrind/docs/print/</computeroutput>.
1036 </para>
1037</listitem>
1038
1039</orderedlist>
1040
1041</sect3>
1042
1043</sect2>
1044
1045
1046<sect2 id="writing-tools.regtests" xreflabel="Regression Tests">
1047<title>Regression Tests</title>
1048
1049<para>Valgrind has some support for regression tests. If you
1050want to write regression tests for your tool:</para>
1051
1052<orderedlist>
1053 <listitem>
1054 <para>Make a directory
1055 <computeroutput>foobar/tests/</computeroutput>.</para>
1056 </listitem>
1057
1058 <listitem>
1059 <para>Edit <computeroutput>foobar/Makefile.am</computeroutput>,
1060 adding <computeroutput>tests</computeroutput> to the
1061 <computeroutput>SUBDIRS</computeroutput> variable.</para>
1062 </listitem>
1063
1064 <listitem>
1065 <para>Edit <computeroutput>configure.in</computeroutput>,
1066 adding <computeroutput>foobar/tests/Makefile</computeroutput>
1067 to the <computeroutput>AC_OUTPUT</computeroutput> list.</para>
1068 </listitem>
1069
1070 <listitem>
1071 <para>Write
1072 <computeroutput>foobar/tests/Makefile.am</computeroutput>. Use
1073 <computeroutput>memcheck/tests/Makefile.am</computeroutput> as
1074 an example.</para>
1075 </listitem>
1076
1077 <listitem>
1078 <para>Write the tests, <computeroutput>.vgtest</computeroutput>
1079 test description files,
1080 <computeroutput>.stdout.exp</computeroutput> and
1081 <computeroutput>.stderr.exp</computeroutput> expected output
1082 files. (Note that Valgrind's output goes to stderr.) Some
1083 details on writing and running tests are given in the comments
1084 at the top of the testing script
1085 <computeroutput>tests/vg_regtest</computeroutput>.</para>
1086 </listitem>
1087
1088 <listitem>
1089 <para>Write a filter for stderr results
1090 <computeroutput>foobar/tests/filter_stderr</computeroutput>.
1091 It can call the existing filters in
1092 <computeroutput>tests/</computeroutput>. See
1093 <computeroutput>memcheck/tests/filter_stderr</computeroutput>
1094 for an example; in particular note the
1095 <computeroutput>$dir</computeroutput> trick that ensures the
1096 filter works correctly from any directory.</para>
1097 </listitem>
1098
1099</orderedlist>
1100
1101</sect2>
1102
1103
1104
1105<sect2 id="writing-tools.profiling" xreflabel="Profiling">
1106<title>Profiling</title>
1107
njn31066fd2005-03-26 00:42:02 +00001108<para>Nb: as of 25-Mar-2005, the profiling is broken, and has been
1109for a long time...</para>
1110
njn3e986b22004-11-30 10:43:45 +00001111<para>To do simple tick-based profiling of a tool, include the
1112line:</para>
1113<programlisting><![CDATA[
1114 #include "vg_profile.c"]]></programlisting>
1115<para>in the tool somewhere, and rebuild (you may have to
1116<computeroutput>make clean</computeroutput> first). Then run
1117Valgrind with the <computeroutput>--profile=yes</computeroutput>
1118option.</para>
1119
1120<para>The profiler is stack-based; you can register a profiling
1121event with
njn31066fd2005-03-26 00:42:02 +00001122<computeroutput>VG_(register_profile_event)()</computeroutput>
njn3e986b22004-11-30 10:43:45 +00001123and then use the <computeroutput>VGP_PUSHCC</computeroutput> and
1124<computeroutput>VGP_POPCC</computeroutput> macros to record time
1125spent doing certain things. New profiling event numbers must not
1126overlap with the core profiling event numbers. See
njnc4fcca32004-12-01 00:02:36 +00001127<filename>include/tool.h</filename> for details and Memcheck
njn3e986b22004-11-30 10:43:45 +00001128for an example.</para>
1129
1130</sect2>
1131
1132
1133
1134<sect2 id="writing-tools.mkhackery" xreflabel="Other Makefile Hackery">
1135<title>Other Makefile Hackery</title>
1136
1137<para>If you add any directories under
1138<computeroutput>valgrind/foobar/</computeroutput>, you will need
1139to add an appropriate <filename>Makefile.am</filename> to it, and
1140add a corresponding entry to the
1141<computeroutput>AC_OUTPUT</computeroutput> list in
1142<filename>valgrind/configure.in</filename>.</para>
1143
1144<para>If you add any scripts to your tool (see Cachegrind for an
1145example) you need to add them to the
1146<computeroutput>bin_SCRIPTS</computeroutput> variable in
1147<filename>valgrind/foobar/Makefile.am</filename>.</para>
1148
1149</sect2>
1150
1151
1152
1153<sect2 id="writing-tools.ifacever" xreflabel="Core/tool Interface Versions">
1154<title>Core/tool Interface Versions</title>
1155
1156<para>In order to allow for the core/tool interface to evolve
1157over time, Valgrind uses a basic interface versioning system.
1158All a tool has to do is use the
1159<computeroutput>VG_DETERMINE_INTERFACE_VERSION</computeroutput>
1160macro exactly once in its code. If not, a link error will occur
1161when the tool is built.</para>
1162
1163<para>The interface version number has the form X.Y. Changes in
1164Y indicate binary compatible changes. Changes in X indicate
1165binary incompatible changes. If the core and tool has the same
1166major version number X they should work together. If X doesn't
1167match, Valgrind will abort execution with an explanation of the
1168problem.</para>
1169
1170<para>This approach was chosen so that if the interface changes
1171in the future, old tools won't work and the reason will be
1172clearly explained, instead of possibly crashing mysteriously. We
1173have attempted to minimise the potential for binary incompatible
1174changes by means such as minimising the use of naked structs in
1175the interface.</para>
1176
1177</sect2>
1178
1179</sect1>
1180
1181
1182
1183<sect1 id="writing-tools.finalwords" xreflabel="Final Words">
1184<title>Final Words</title>
1185
1186<para>This whole core/tool business is under active development,
1187although it's slowly maturing.</para>
1188
1189<para>The first consequence of this is that the core/tool
1190interface will continue to change in the future; we have no
1191intention of freezing it and then regretting the inevitable
1192stupidities. Hopefully most of the future changes will be to add
1193new features, hooks, functions, etc, rather than to change old
1194ones, which should cause a minimum of trouble for existing tools,
1195and we've put some effort into future-proofing the interface to
1196avoid binary incompatibility. But we can't guarantee anything.
1197The versioning system should catch any incompatibilities. Just
1198something to be aware of.</para>
1199
1200<para>The second consequence of this is that we'd love to hear
1201your feedback about it:</para>
1202
1203<itemizedlist>
1204 <listitem>
1205 <para>If you love it or hate it</para>
1206 </listitem>
1207 <listitem>
1208 <para>If you find bugs</para>
1209 </listitem>
1210 <listitem>
1211 <para>If you write a tool</para>
1212 </listitem>
1213 <listitem>
1214 <para>If you have suggestions for new features, needs,
1215 trackable events, functions</para>
1216 </listitem>
1217 <listitem>
1218 <para>If you have suggestions for making tools easier to
1219 write</para>
1220 </listitem>
1221 <listitem>
1222 <para>If you have suggestions for improving this
1223 documentation</para>
1224 </listitem>
1225 <listitem>
1226 <para>If you don't understand something</para>
1227 </listitem>
1228</itemizedlist>
1229
1230<para>or anything else!</para>
1231
1232<para>Happy programming.</para>
1233
1234</sect1>
1235
1236</chapter>