blob: 9fa3645bf19d8d403db3c8a3ead596f859353e4f [file] [log] [blame]
njn3e986b22004-11-30 10:43:45 +00001<?xml version="1.0"?> <!-- -*- sgml -*- -->
2<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
de252c6142005-11-27 04:10:00 +00003 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"
4[ <!ENTITY % vg-entities SYSTEM "vg-entities.xml"> %vg-entities; ]>
njn3e986b22004-11-30 10:43:45 +00005
debad57fc2005-12-03 22:33:29 +00006
njn3e986b22004-11-30 10:43:45 +00007<chapter id="writing-tools" xreflabel="Writing a New Valgrind Tool">
8<title>Writing a New Valgrind Tool</title>
9
10<sect1 id="writing-tools.intro" xreflabel="Introduction">
11<title>Introduction</title>
12
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
tomdf2b4a62005-11-04 12:27:58 +000026<para>[Nb: What follows is slightly out of date.]</para>
njnc7561b92005-06-19 01:24:32 +000027
njn3e986b22004-11-30 10:43:45 +000028</sect2>
29
30
31<sect2 id="writing-tools.tools" xreflabel="Tools">
32<title>Tools</title>
33
34<para>The key idea behind Valgrind's architecture is the division
35between its "core" and "tools".</para>
36
37<para>The core provides the common low-level infrastructure to
toma721cec2005-11-04 14:11:05 +000038support program instrumentation, including the JIT
njn3e986b22004-11-30 10:43:45 +000039compiler, low-level memory manager, signal handling and a
40scheduler (for pthreads). It also provides certain services that
41are useful to some but not all tools, such as support for error
42recording and suppression.</para>
43
44<para>But the core leaves certain operations undefined, which
45must be filled by tools. Most notably, tools define how program
toma721cec2005-11-04 14:11:05 +000046code should be instrumented. They can also call certain
47functions to indicate to the core that they would like to use
njn3e986b22004-11-30 10:43:45 +000048certain services, or be notified when certain interesting events
49occur. But the core takes care of all the hard work.</para>
50
51</sect2>
52
53
54<sect2 id="writing-tools.execspaces" xreflabel="Execution Spaces">
55<title>Execution Spaces</title>
56
57<para>An important concept to understand before writing a tool is
58that there are three spaces in which program code executes:</para>
59
60
61<orderedlist>
62
63 <listitem>
64 <para>User space: this covers most of the program's execution.
65 The tool is given the code and can instrument it any way it
66 likes, providing (more or less) total control over the
67 code.</para>
68
69 <para>Code executed in user space includes all the program
70 code, almost all of the C library (including things like the
71 dynamic linker), and almost all parts of all other
72 libraries.</para>
73 </listitem>
74
75 <listitem>
76 <para>Core space: a small proportion of the program's execution
77 takes place entirely within Valgrind's core. This includes:</para>
78 <itemizedlist>
79 <listitem>
80 <para>Dynamic memory management
81 (<computeroutput>malloc()</computeroutput> etc.)</para>
82 </listitem>
83 <listitem>
toma721cec2005-11-04 14:11:05 +000084 <para>Thread scheduling</para>
njn3e986b22004-11-30 10:43:45 +000085 </listitem>
86 <listitem>
87 <para>Signal handling</para>
88 </listitem>
89 </itemizedlist>
90
91 <para>A tool has no control over these operations; it never
92 "sees" the code doing this work and thus cannot instrument it.
93 However, the core provides hooks so a tool can be notified
tomb931c582005-11-04 12:17:20 +000094 when certain interesting events happen, for example when
njn3e986b22004-11-30 10:43:45 +000095 dynamic memory is allocated or freed, the stack pointer is
96 changed, or a pthread mutex is locked, etc.</para>
97
98 <para>Note that these hooks only notify tools of events
99 relevant to user space. For example, when the core allocates
100 some memory for its own use, the tool is not notified of this,
101 because it's not directly part of the supervised program's
102 execution.</para>
103 </listitem>
104
105 <listitem>
106 <para>Kernel space: execution in the kernel. Two kinds:</para>
107 <orderedlist>
108 <listitem>
109 <para>System calls: can't be directly observed by either
110 the tool or the core. But the core does have some idea of
111 what happens to the arguments, and it provides hooks for a
112 tool to wrap system calls.</para>
113 </listitem>
114 <listitem>
115 <para>Other: all other kernel activity (e.g. process
116 scheduling) is totally opaque and irrelevant to the
117 program.</para>
118 </listitem>
119 </orderedlist>
120 </listitem>
121
122 <listitem>
123 <para>It should be noted that a tool only has direct control
124 over code executed in user space. This is the vast majority
125 of code executed, but it is not absolutely all of it, so any
126 profiling information recorded by a tool won't be totally
127 accurate.</para>
128 </listitem>
129
130</orderedlist>
131
132</sect2>
133
134</sect1>
135
136
137
138<sect1 id="writing-tools.writingatool" xreflabel="Writing a Tool">
139<title>Writing a Tool</title>
140
141
142<sect2 id="writing-tools.whywriteatool" xreflabel="Why write a tool?">
143<title>Why write a tool?</title>
144
145<para>Before you write a tool, you should have some idea of what
146it should do. What is it you want to know about your programs of
147interest? Consider some existing tools:</para>
148
149<itemizedlist>
150
151 <listitem>
152 <para><command>memcheck</command>: among other things, performs
153 fine-grained validity and addressibility checks of every memory
154 reference performed by the program.</para>
155 </listitem>
156
157 <listitem>
njn3e986b22004-11-30 10:43:45 +0000158 <para><command>cachegrind</command>: tracks every instruction
159 and memory reference to simulate instruction and data caches,
160 tracking cache accesses and misses that occur on every line in
161 the program.</para>
162 </listitem>
163
164 <listitem>
165 <para><command>helgrind</command>: tracks every memory access
166 and mutex lock/unlock to determine if a program contains any
167 data races.</para>
168 </listitem>
169
170 <listitem>
171 <para><command>lackey</command>: does simple counting of
172 various things: the number of calls to a particular function
173 (<computeroutput>_dl_runtime_resolve()</computeroutput>); the
toma721cec2005-11-04 14:11:05 +0000174 number of basic blocks, guest instructions, VEX instructions
njn3e986b22004-11-30 10:43:45 +0000175 executed; the number of branches executed and the proportion of
tomb931c582005-11-04 12:17:20 +0000176 them which were taken.</para>
njn3e986b22004-11-30 10:43:45 +0000177 </listitem>
178</itemizedlist>
179
180<para>These examples give a reasonable idea of what kinds of
181things Valgrind can be used for. The instrumentation can range
182from very lightweight (e.g. counting the number of times a
183particular function is called) to very intrusive (e.g.
184memcheck's memory checking).</para>
185
186</sect2>
187
188
189<sect2 id="writing-tools.suggestedtools" xreflabel="Suggested tools">
190<title>Suggested tools</title>
191
192<para>Here is a list of ideas we have had for tools that should
193not be too hard to implement.</para>
194
195<itemizedlist>
196 <listitem>
197 <para><command>branch profiler</command>: A machine's branch
198 prediction hardware could be simulated, and each branch
199 annotated with the number of predicted and mispredicted
200 branches. Would be implemented quite similarly to Cachegrind,
201 and could reuse the
202 <computeroutput>cg_annotate</computeroutput> script to annotate
203 source code.</para>
204
205 <para>The biggest difficulty with this is the simulation; the
206 chip-makers are very cagey about how their chips do branch
207 prediction. But implementing one or more of the basic
208 algorithms could still give good information.</para>
209 </listitem>
210
211 <listitem>
212 <para><command>coverage tool</command>: Cachegrind can already
213 be used for doing test coverage, but it's massive overkill to
214 use it just for that.</para>
215
216 <para>It would be easy to write a coverage tool that records
217 how many times each basic block was recorded. Again, the
218 <computeroutput>cg_annotate</computeroutput> script could be
219 used for annotating source code with the gathered information.
220 Although, <computeroutput>cg_annotate</computeroutput> is only
221 designed for working with single program runs. It could be
222 extended relatively easily to deal with multiple runs of a
223 program, so that the coverage of a whole test suite could be
224 determined.</para>
225
226 <para>In addition to the standard coverage information, such a
227 tool could record extra information that would help a user
228 generate test cases to exercise unexercised paths. For
229 example, for each conditional branch, the tool could record all
230 inputs to the conditional test, and print these out when
231 annotating.</para>
232 </listitem>
233
234 <listitem>
235 <para><command>run-time type checking</command>: A nice example
236 of a dynamic checker is given in this paper:</para>
237 <address>Debugging via Run-Time Type Checking
238 Alexey Loginov, Suan Hsi Yong, Susan Horwitz and Thomas Reps
239 Proceedings of Fundamental Approaches to Software Engineering
240 April 2001.
241 </address>
242
243 <para>Similar is the tool described in this paper:</para>
244 <address>Run-Time Type Checking for Binary Programs
245 Michael Burrows, Stephen N. Freund, Janet L. Wiener
246 Proceedings of the 12th International Conference on Compiler Construction (CC 2003)
247 April 2003.
248 </address>
249
250 <para>This approach can find quite a range of bugs,
251 particularly in C and C++ programs, and could be implemented
252 quite nicely as a Valgrind tool.</para>
253
254 <para>Ways to speed up this run-time type checking are
255 described in this paper:</para>
256 <address>Reducing the Overhead of Dynamic Analysis
257 Suan Hsi Yong and Susan Horwitz
258 Proceedings of Runtime Verification '02
259 July 2002.
260 </address>
261
262 <para>Valgrind's client requests could be used to pass
263 information to a tool about which elements need instrumentation
264 and which don't.</para>
265 </listitem>
266</itemizedlist>
267
268<para>We would love to hear from anyone who implements these or
269other tools.</para>
270
271</sect2>
272
273
274<sect2 id="writing-tools.howtoolswork" xreflabel="How tools work">
275<title>How tools work</title>
276
debad57fc2005-12-03 22:33:29 +0000277<para>Tools must define various functions for instrumenting programs
278that are called by Valgrind's core. They are then linked against the
279coregrind library (<filename>libcoregrind.a</filename>) that valgrind
280provides as well as the VEX library (<filename>libvex.a</filename>) that
281also comes with valgrind and provides the JIT engine.</para>
njn3e986b22004-11-30 10:43:45 +0000282
debad57fc2005-12-03 22:33:29 +0000283<para>Each tool is linked as a statically linked program and placed in
284the valgrind library directory from where valgrind will load it
285automatically when the <option>--tool</option> option is used to select
286it.</para>
njn3e986b22004-11-30 10:43:45 +0000287
288</sect2>
289
290
291<sect2 id="writing-tools.gettingcode" xreflabel="Getting the code">
292<title>Getting the code</title>
293
debad57fc2005-12-03 22:33:29 +0000294<para>To write your own tool, you'll need the Valgrind source code. A
295normal source distribution should do, although you might want to check
296out the latest code from the Subversion repository. See the information
297about how to do so at <ulink url="&vg-svn-repo;">the Valgrind
298website</ulink>.</para>
njn3e986b22004-11-30 10:43:45 +0000299
300</sect2>
301
302
303<sect2 id="writing-tools.gettingstarted" xreflabel="Getting started">
304<title>Getting started</title>
305
debad57fc2005-12-03 22:33:29 +0000306<para>Valgrind uses GNU <computeroutput>automake</computeroutput> and
307<computeroutput>autoconf</computeroutput> for the creation of Makefiles
308and configuration. But don't worry, these instructions should be enough
309to get you started even if you know nothing about those tools.</para>
njn3e986b22004-11-30 10:43:45 +0000310
311<para>In what follows, all filenames are relative to Valgrind's
312top-level directory <computeroutput>valgrind/</computeroutput>.</para>
313
314<orderedlist>
315 <listitem>
debad57fc2005-12-03 22:33:29 +0000316 <para>Choose a name for the tool, and an abbreviation that can be used
317 as a short prefix. We'll use <computeroutput>foobar</computeroutput>
318 and <computeroutput>fb</computeroutput> as an example.</para>
njn3e986b22004-11-30 10:43:45 +0000319 </listitem>
320
321 <listitem>
debad57fc2005-12-03 22:33:29 +0000322 <para>Make a new directory <computeroutput>foobar/</computeroutput>
323 which will hold the tool.</para>
njn3e986b22004-11-30 10:43:45 +0000324 </listitem>
325
326 <listitem>
debad57fc2005-12-03 22:33:29 +0000327 <para>Copy <filename>none/Makefile.am</filename> into
328 <computeroutput>foobar/</computeroutput>. Edit it by replacing all
329 occurrences of the string <computeroutput>"none"</computeroutput> with
330 <computeroutput>"foobar"</computeroutput> and the one occurrence of
331 the string <computeroutput>"nl_"</computeroutput> with
332 <computeroutput>"fb_"</computeroutput>. It might be worth trying to
333 understand this file, at least a little; you might have to do more
334 complicated things with it later on. In particular, the name of the
335 <computeroutput>foobar_SOURCES</computeroutput> variable determines
336 the name of the tool, which determines what name must be passed to the
337 <option>--tool</option> option to use the tool.</para>
njn3e986b22004-11-30 10:43:45 +0000338 </listitem>
339
340 <listitem>
341 <para>Copy <filename>none/nl_main.c</filename> into
342 <computeroutput>foobar/</computeroutput>, renaming it as
debad57fc2005-12-03 22:33:29 +0000343 <filename>fb_main.c</filename>. Edit it by changing the lines in
344 <function>pre_clo_init()</function> to something appropriate for the
345 tool. These fields are used in the startup message, except for
346 <computeroutput>bug_reports_to</computeroutput> which is used if a
347 tool assertion fails.</para>
njn3e986b22004-11-30 10:43:45 +0000348 </listitem>
349
350 <listitem>
debad57fc2005-12-03 22:33:29 +0000351 <para>Edit <filename>Makefile.am</filename>, adding the new directory
njn3e986b22004-11-30 10:43:45 +0000352 <computeroutput>foobar</computeroutput> to the
353 <computeroutput>SUBDIRS</computeroutput> variable.</para>
354 </listitem>
355
356 <listitem>
debad57fc2005-12-03 22:33:29 +0000357 <para>Edit <filename>configure.in</filename>, adding
358 <filename>foobar/Makefile</filename> to the
njn3e986b22004-11-30 10:43:45 +0000359 <computeroutput>AC_OUTPUT</computeroutput> list.</para>
360 </listitem>
361
362 <listitem>
363 <para>Run:</para>
364<programlisting><![CDATA[
365 autogen.sh
366 ./configure --prefix=`pwd`/inst
367 make install]]></programlisting>
368
debad57fc2005-12-03 22:33:29 +0000369 <para>It should automake, configure and compile without errors,
370 putting copies of the tool in
njn3e986b22004-11-30 10:43:45 +0000371 <computeroutput>foobar/</computeroutput> and
372 <computeroutput>inst/lib/valgrind/</computeroutput>.</para>
373 </listitem>
374
375 <listitem>
376 <para>You can test it with a command like:</para>
377<programlisting><![CDATA[
378 inst/bin/valgrind --tool=foobar date]]></programlisting>
379
380 <para>(almost any program should work;
381 <computeroutput>date</computeroutput> is just an example).
382 The output should be something like this:</para>
383<programlisting><![CDATA[
384 ==738== foobar-0.0.1, a foobarring tool for x86-linux.
385 ==738== Copyright (C) 1066AD, and GNU GPL'd, by J. Random Hacker.
386 ==738== Built with valgrind-1.1.0, a program execution monitor.
387 ==738== Copyright (C) 2000-2003, and GNU GPL'd, by Julian Seward.
388 ==738== Estimated CPU clock rate is 1400 MHz
389 ==738== For more details, rerun with: -v
390 ==738== Wed Sep 25 10:31:54 BST 2002
391 ==738==]]></programlisting>
392
393 <para>The tool does nothing except run the program
394 uninstrumented.</para>
395 </listitem>
396
397</orderedlist>
398
debad57fc2005-12-03 22:33:29 +0000399<para>These steps don't have to be followed exactly - you can choose
400different names for your source files, and use a different
401<option>--prefix</option> for
njn3e986b22004-11-30 10:43:45 +0000402<computeroutput>./configure</computeroutput>.</para>
403
debad57fc2005-12-03 22:33:29 +0000404<para>Now that we've setup, built and tested the simplest possible tool,
405onto the interesting stuff...</para>
njn3e986b22004-11-30 10:43:45 +0000406
407</sect2>
408
409
410
411<sect2 id="writing-tools.writingcode" xreflabel="Writing the Code">
412<title>Writing the code</title>
413
414<para>A tool must define at least these four functions:</para>
415<programlisting><![CDATA[
njn51d827b2005-05-09 01:02:08 +0000416 pre_clo_init()
417 post_clo_init()
418 instrument()
419 fini()]]></programlisting>
njn3e986b22004-11-30 10:43:45 +0000420
421<para>Also, it must use the macro
debad57fc2005-12-03 22:33:29 +0000422<computeroutput>VG_DETERMINE_INTERFACE_VERSION</computeroutput> exactly
423once in its source code. If it doesn't, you will get a link error
424involving <computeroutput>VG_(tool_interface_version)</computeroutput>.
425This macro is used to ensure the core/tool interface used by the core
426and a plugged-in tool are binary compatible.</para>
njn3e986b22004-11-30 10:43:45 +0000427
debad57fc2005-12-03 22:33:29 +0000428<para>In addition, if a tool wants to use some of the optional services
429provided by the core, it may have to define other functions and tell the
430code about them.</para>
njn3e986b22004-11-30 10:43:45 +0000431
432</sect2>
433
434
435
436<sect2 id="writing-tools.init" xreflabel="Initialisation">
437<title>Initialisation</title>
438
439<para>Most of the initialisation should be done in
debad57fc2005-12-03 22:33:29 +0000440<function>pre_clo_init()</function>. Only use
441<function>post_clo_init()</function> if a tool provides command line
442options and must do some initialisation after option processing takes
443place (<computeroutput>"clo"</computeroutput> stands for "command line
njn3e986b22004-11-30 10:43:45 +0000444options").</para>
445
debad57fc2005-12-03 22:33:29 +0000446<para>First of all, various "details" need to be set for a tool, using
447the functions <function>VG_(details_*)()</function>. Some are all
448compulsory, some aren't. Some are used when constructing the startup
449message, <computeroutput>detail_bug_reports_to</computeroutput> is used
450if <computeroutput>VG_(tool_panic)()</computeroutput> is ever called, or
451a tool assertion fails. Others have other uses.</para>
njn3e986b22004-11-30 10:43:45 +0000452
debad57fc2005-12-03 22:33:29 +0000453<para>Second, various "needs" can be set for a tool, using the functions
454<function>VG_(needs_*)()</function>. They are mostly booleans, and can
455be left untouched (they default to <varname>False</varname>). They
456determine whether a tool can do various things such as: record, report
457and suppress errors; process command line options; wrap system calls;
458record extra information about malloc'd blocks, etc.</para>
njn3e986b22004-11-30 10:43:45 +0000459
debad57fc2005-12-03 22:33:29 +0000460<para>For example, if a tool wants the core's help in recording and
461reporting errors, it must call
462<function>VG_(needs_tool_errors)</function> and provide definitions of
463eight functions for comparing errors, printing out errors, reading
464suppressions from a suppressions file, etc. While writing these
465functions requires some work, it's much less than doing error handling
466from scratch because the core is doing most of the work. See the
467function <function>VG_(needs_tool_errors)</function> in
468<filename>include/pub_tool_tooliface.h</filename> for full details of
469all the needs.</para>
njn3e986b22004-11-30 10:43:45 +0000470
debad57fc2005-12-03 22:33:29 +0000471<para>Third, the tool can indicate which events in core it wants to be
472notified about, using the functions <function>VG_(track_*)()</function>.
473These include things such as blocks of memory being malloc'd, the stack
474pointer changing, a mutex being locked, etc. If a tool wants to know
475about this, it should provide a pointer to a function, which will be
476called when that event happens.</para>
njn3e986b22004-11-30 10:43:45 +0000477
debad57fc2005-12-03 22:33:29 +0000478<para>For example, if the tool want to be notified when a new block of
479memory is malloc'd, it should call
480<function>VG_(track_new_mem_heap)()</function> with an appropriate
481function pointer, and the assigned function will be called each time
482this happens.</para>
njn3e986b22004-11-30 10:43:45 +0000483
debad57fc2005-12-03 22:33:29 +0000484<para>More information about "details", "needs" and "trackable events"
485can be found in
tomb931c582005-11-04 12:17:20 +0000486<filename>include/pub_tool_tooliface.h</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000487
488</sect2>
489
490
491
492<sect2 id="writing-tools.instr" xreflabel="Instrumentation">
493<title>Instrumentation</title>
494
debad57fc2005-12-03 22:33:29 +0000495<para><function>instrument()</function> is the interesting one. It
496allows you to instrument <emphasis>VEX IR</emphasis>, which is
497Valgrind's RISC-like intermediate language. VEX IR is described in
njn3e986b22004-11-30 10:43:45 +0000498<xref linkend="mc-tech-docs.ucode"/>.</para>
499
toma721cec2005-11-04 14:11:05 +0000500<para>The easiest way to instrument VEX IR is to insert calls to C
njn3e986b22004-11-30 10:43:45 +0000501functions when interesting things happen. See the tool "Lackey"
debad57fc2005-12-03 22:33:29 +0000502(<filename>lackey/lk_main.c</filename>) for a simple example of this, or
503Cachegrind (<filename>cachegrind/cg_main.c</filename>) for a more
504complex example.</para>
njn3e986b22004-11-30 10:43:45 +0000505
njn3e986b22004-11-30 10:43:45 +0000506</sect2>
507
508
509
510<sect2 id="writing-tools.fini" xreflabel="Finalisation">
511<title>Finalisation</title>
512
debad57fc2005-12-03 22:33:29 +0000513<para>This is where you can present the final results, such as a summary
514of the information collected. Any log files should be written out at
515this point.</para>
njn3e986b22004-11-30 10:43:45 +0000516
517</sect2>
518
519
520
521<sect2 id="writing-tools.otherinfo" xreflabel="Other Important Information">
522<title>Other Important Information</title>
523
debad57fc2005-12-03 22:33:29 +0000524<para>Please note that the core/tool split infrastructure is quite
525complex and not brilliantly documented. Here are some important points,
526but there are undoubtedly many others that I should note but haven't
527thought of.</para>
njn3e986b22004-11-30 10:43:45 +0000528
debad57fc2005-12-03 22:33:29 +0000529<para>The files <filename>include/pub_tool_*.h</filename> contain all
530the types, macros, functions, etc. that a tool should (hopefully) need,
531and are the only <filename>.h</filename> files a tool should need to
njn3e986b22004-11-30 10:43:45 +0000532<computeroutput>#include</computeroutput>.</para>
533
debad57fc2005-12-03 22:33:29 +0000534<para>In particular, you can't use anything from the C library (there
535are deep reasons for this, trust us). Valgrind provides an
536implementation of a reasonable subset of the C library, details of which
537are in <filename>pub_tool_libc*.h</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000538
debad57fc2005-12-03 22:33:29 +0000539<para>Similarly, when writing a tool, you shouldn't need to look at any
540of the code in Valgrind's core. Although it might be useful sometimes
541to help understand something.</para>
njn3e986b22004-11-30 10:43:45 +0000542
tomdf2b4a62005-11-04 12:27:58 +0000543<para>The <filename>pub_tool_*.h</filename> files have a reasonable
njn1d0825f2006-03-27 11:37:07 +0000544amount of documentation in it that should hopefully be enough to get
545you going. But ultimately, the tools distributed (Memcheck,
546Cachegrind, Lackey, etc.) are probably the best
547documentation of all, for the moment.</para>
njn3e986b22004-11-30 10:43:45 +0000548
toma721cec2005-11-04 14:11:05 +0000549<para>Note that the <computeroutput>VG_</computeroutput> macro is used
debad57fc2005-12-03 22:33:29 +0000550heavily. This just prepends a longer string in front of names to avoid
551potential namespace clashes.</para>
njn3e986b22004-11-30 10:43:45 +0000552
553</sect2>
554
555
556<sect2 id="writing-tools.advice" xreflabel="Words of Advice">
557<title>Words of Advice</title>
558
559<para>Writing and debugging tools is not trivial. Here are some
560suggestions for solving common problems.</para>
561
562
563<sect3 id="writing-tools.segfaults">
564<title>Segmentation Faults</title>
565
debad57fc2005-12-03 22:33:29 +0000566<para>If you are getting segmentation faults in C functions used by your
567tool, the usual GDB command:</para>
568
njn3e986b22004-11-30 10:43:45 +0000569<screen><![CDATA[
570 gdb <prog> core]]></screen>
571<para>usually gives the location of the segmentation fault.</para>
572
573</sect3>
574
575
576<sect3 id="writing-tools.debugfns">
577<title>Debugging C functions</title>
578
debad57fc2005-12-03 22:33:29 +0000579<para>If you want to debug C functions used by your tool, you can
tom605535f2005-11-29 09:59:32 +0000580achieve this by following these steps:</para>
njn3e986b22004-11-30 10:43:45 +0000581<orderedlist>
debad57fc2005-12-03 22:33:29 +0000582 <listitem>
583 <para>Set <computeroutput>VALGRIND_LAUNCHER</computeroutput> to
584 <computeroutput><![CDATA[<prefix>/bin/valgrind]]></computeroutput>:</para>
ded3c35372005-11-29 16:06:55 +0000585<programlisting>
586 export VALGRIND_LAUNCHER=/usr/local/bin/valgrind</programlisting>
debad57fc2005-12-03 22:33:29 +0000587 </listitem>
njn3e986b22004-11-30 10:43:45 +0000588
debad57fc2005-12-03 22:33:29 +0000589 <listitem>
590 <para>Then run <computeroutput><![CDATA[ gdb <prefix>/lib/valgrind/<platform>/<tool>:]]></computeroutput></para>
ded3c35372005-11-29 16:06:55 +0000591<programlisting>
592 gdb /usr/local/lib/valgrind/ppc32-linux/lackey</programlisting>
debad57fc2005-12-03 22:33:29 +0000593 </listitem>
njn3e986b22004-11-30 10:43:45 +0000594
debad57fc2005-12-03 22:33:29 +0000595 <listitem>
596 <para>Do <computeroutput>handle SIGSEGV SIGILL nostop
597 noprint</computeroutput> in GDB to prevent GDB from stopping on a
598 SIGSEGV or SIGILL:</para>
ded3c35372005-11-29 16:06:55 +0000599<programlisting>
600 (gdb) handle SIGILL SIGSEGV nostop noprint</programlisting>
debad57fc2005-12-03 22:33:29 +0000601 </listitem>
tom605535f2005-11-29 09:59:32 +0000602
debad57fc2005-12-03 22:33:29 +0000603 <listitem>
604 <para>Set any breakpoints you want and proceed as normal for GDB:</para>
ded3c35372005-11-29 16:06:55 +0000605<programlisting>
606 (gdb) b vgPlain_do_exec</programlisting>
debad57fc2005-12-03 22:33:29 +0000607 <para>The macro VG_(FUNC) is expanded to vgPlain_FUNC, so If you
608 want to set a breakpoint VG_(do_exec), you could do like this in
609 GDB.</para>
610 </listitem>
tom605535f2005-11-29 09:59:32 +0000611
debad57fc2005-12-03 22:33:29 +0000612 <listitem>
613 <para>Run the tool with required options:</para>
ded3c35372005-11-29 16:06:55 +0000614<programlisting>
615 (gdb) run `pwd`</programlisting>
debad57fc2005-12-03 22:33:29 +0000616 </listitem>
njn3e986b22004-11-30 10:43:45 +0000617
618</orderedlist>
619
debad57fc2005-12-03 22:33:29 +0000620<para>GDB may be able to give you useful information. Note that by
621default most of the system is built with
622<option>-fomit-frame-pointer</option>, and you'll need to get rid of
623this to extract useful tracebacks from GDB.</para>
njn3e986b22004-11-30 10:43:45 +0000624
625</sect3>
626
627
628<sect3 id="writing-tools.ucode-probs">
629<title>UCode Instrumentation Problems</title>
630
debad57fc2005-12-03 22:33:29 +0000631<para>If you are having problems with your VEX UIR instrumentation, it's
632likely that GDB won't be able to help at all. In this case, Valgrind's
633<option>--trace-flags</option> option is invaluable for observing the
634results of instrumentation.</para>
njn3e986b22004-11-30 10:43:45 +0000635
636</sect3>
637
638
639<sect3 id="writing-tools.misc">
640<title>Miscellaneous</title>
641
debad57fc2005-12-03 22:33:29 +0000642<para>If you just want to know whether a program point has been reached,
643using the <computeroutput>OINK</computeroutput> macro (in
644<filename>include/pub_tool_libcprint.h</filename>) can be easier than
645using GDB.</para>
njn3e986b22004-11-30 10:43:45 +0000646
debad57fc2005-12-03 22:33:29 +0000647<para>The other debugging command line options can be useful too (run
648<computeroutput>valgrind --help-debug</computeroutput> for the
njn3e986b22004-11-30 10:43:45 +0000649list).</para>
650
651</sect3>
652
653</sect2>
654
655</sect1>
656
657
658
659<sect1 id="writing-tools.advtopics" xreflabel="Advanced Topics">
660<title>Advanced Topics</title>
661
662<para>Once a tool becomes more complicated, there are some extra
663things you may want/need to do.</para>
664
665<sect2 id="writing-tools.suppressions" xreflabel="Suppressions">
666<title>Suppressions</title>
667
debad57fc2005-12-03 22:33:29 +0000668<para>If your tool reports errors and you want to suppress some common
669ones, you can add suppressions to the suppression files. The relevant
670files are <filename>valgrind/*.supp</filename>; the final suppression
671file is aggregated from these files by combining the relevant
672<filename>.supp</filename> files depending on the versions of linux, X
673and glibc on a system.</para>
njn3e986b22004-11-30 10:43:45 +0000674
675<para>Suppression types have the form
676<computeroutput>tool_name:suppression_name</computeroutput>. The
debad57fc2005-12-03 22:33:29 +0000677<computeroutput>tool_name</computeroutput> here is the name you specify
678for the tool during initialisation with
679<function>VG_(details_name)()</function>.</para>
njn3e986b22004-11-30 10:43:45 +0000680
681</sect2>
682
683
njn3e986b22004-11-30 10:43:45 +0000684<sect2 id="writing-tools.docs" xreflabel="Documentation">
685<title>Documentation</title>
686
debad57fc2005-12-03 22:33:29 +0000687<para>As of version 3.0.0, Valgrind documentation has been converted to
688XML. Why? See <ulink url="http://www.ucc.ie/xml/">The XML FAQ</ulink>.
njn3e986b22004-11-30 10:43:45 +0000689</para>
690
691
692<sect3 id="writing-tools.xml" xreflabel="The XML Toolchain">
693<title>The XML Toolchain</title>
694
695<para>If you are feeling conscientious and want to write some
696documentation for your tool, please use XML. The Valgrind
697Docs use the following toolchain and versions:</para>
698
699<programlisting>
700 xmllint: using libxml version 20607
701 xsltproc: using libxml 20607, libxslt 10102 and libexslt 802
702 pdfxmltex: pdfTeX (Web2C 7.4.5) 3.14159-1.10b
703 pdftops: version 3.00
704 DocBook: version 4.2
705</programlisting>
706
707<para><command>Latency:</command> you should note that latency is
708a big problem: DocBook is constantly being updated, but the tools
709tend to lag behind somewhat. It is important that the versions
710get on with each other, so if you decide to upgrade something,
711then you need to ascertain whether things still work nicely -
712this *cannot* be assumed.</para>
713
714<para><command>Stylesheets:</command> The Valgrind docs use
715various custom stylesheet layers, all of which are in
716<computeroutput>valgrind/docs/lib/</computeroutput>. You
717shouldn't need to modify these in any way.</para>
718
debad57fc2005-12-03 22:33:29 +0000719<para><command>Catalogs:</command> Catalogs provide a mapping from
720generic addresses to specific local directories on a given machine.
721Most recent Linux distributions have adopted a common place for storing
722catalogs (<filename>/etc/xml/</filename>). Assuming that you have the
723various tools listed above installed, you probably won't need to modify
724your catalogs. But if you do, then just add another
725<computeroutput>group</computeroutput> to this file, reflecting your
726local installation.</para>
njn3e986b22004-11-30 10:43:45 +0000727
728</sect3>
729
730
731<sect3 id="writing-tools.writing" xreflabel="Writing the Documentation">
732<title>Writing the Documentation</title>
733
734<para>Follow these steps (using <computeroutput>foobar</computeroutput>
735as the example tool name again):</para>
736
737<orderedlist>
738
739 <listitem>
740 <para>Make a directory
debad57fc2005-12-03 22:33:29 +0000741 <computeroutput>valgrind/foobar/docs/</computeroutput>.</para>
742 </listitem>
njn3e986b22004-11-30 10:43:45 +0000743
debad57fc2005-12-03 22:33:29 +0000744 <listitem>
745 <para>Copy the XML documentation file for the tool Nulgrind from
746 <filename>valgrind/none/docs/nl-manual.xml</filename> to
747 <computeroutput>foobar/docs/</computeroutput>, and rename it to
748 <filename>foobar/docs/fb-manual.xml</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000749
debad57fc2005-12-03 22:33:29 +0000750 <para><command>Note</command>: there is a *really stupid* tetex bug
751 with underscores in filenames, so don't use '_'.</para>
752 </listitem>
njn3e986b22004-11-30 10:43:45 +0000753
debad57fc2005-12-03 22:33:29 +0000754 <listitem>
755 <para>Write the documentation. There are some helpful bits and
756 pieces on using xml markup in
757 <filename>valgrind/docs/xml/xml_help.txt</filename>.</para>
758 </listitem>
njn3e986b22004-11-30 10:43:45 +0000759
debad57fc2005-12-03 22:33:29 +0000760 <listitem>
761 <para>Include it in the User Manual by adding the relevant entry to
762 <filename>valgrind/docs/xml/manual.xml</filename>. Copy and edit an
763 existing entry.</para>
764 </listitem>
765
766 <listitem>
767 <para>Validate <filename>foobar/docs/fb-manual.xml</filename> using
768 the following command from within <filename>valgrind/docs/</filename>:
njn3e986b22004-11-30 10:43:45 +0000769 </para>
770<screen><![CDATA[
771% make valid
772]]></screen>
773
774 <para>You will probably get errors that look like this:</para>
775
776<screen><![CDATA[
777./xml/index.xml:5: element chapter: validity error : No declaration for
778attribute base of element chapter
779]]></screen>
780
debad57fc2005-12-03 22:33:29 +0000781 <para>Ignore (only) these -- they're not important.</para>
njn3e986b22004-11-30 10:43:45 +0000782
debad57fc2005-12-03 22:33:29 +0000783 <para>Because the xml toolchain is fragile, it is important to ensure
784 that <filename>fb-manual.xml</filename> won't break the documentation
785 set build. Note that just because an xml file happily transforms to
786 html does not necessarily mean the same holds true for pdf/ps.</para>
787 </listitem>
njn3e986b22004-11-30 10:43:45 +0000788
debad57fc2005-12-03 22:33:29 +0000789 <listitem>
790 <para>You can (re-)generate the HTML docs while you are writing
791 <filename>fb-manual.xml</filename> to help you see how it's looking.
792 The generated files end up in
793 <filename>valgrind/docs/html/</filename>. Use the following
794 command, within <filename>valgrind/docs/</filename>:</para>
njn3e986b22004-11-30 10:43:45 +0000795<screen><![CDATA[
796% make html-docs
797]]></screen>
debad57fc2005-12-03 22:33:29 +0000798 </listitem>
njn3e986b22004-11-30 10:43:45 +0000799
debad57fc2005-12-03 22:33:29 +0000800 <listitem>
801 <para>When you have finished, also generate pdf and ps output to
802 check all is well, from within <filename>valgrind/docs/</filename>:
njn3e986b22004-11-30 10:43:45 +0000803 </para>
njn3e986b22004-11-30 10:43:45 +0000804<screen><![CDATA[
805% make print-docs
806]]></screen>
807
debad57fc2005-12-03 22:33:29 +0000808 <para>Check the output <filename>.pdf</filename> and
809 <filename>.ps</filename> files in
810 <computeroutput>valgrind/docs/print/</computeroutput>.</para>
811 </listitem>
njn3e986b22004-11-30 10:43:45 +0000812
813</orderedlist>
814
815</sect3>
816
817</sect2>
818
819
820<sect2 id="writing-tools.regtests" xreflabel="Regression Tests">
821<title>Regression Tests</title>
822
debad57fc2005-12-03 22:33:29 +0000823<para>Valgrind has some support for regression tests. If you want to
824write regression tests for your tool:</para>
njn3e986b22004-11-30 10:43:45 +0000825
826<orderedlist>
debad57fc2005-12-03 22:33:29 +0000827 <listitem>
828 <para>Make a directory
829 <computeroutput>foobar/tests/</computeroutput>. Make sure the name
830 of the directory is <computeroutput>tests/</computeroutput> as the
831 build system assumes that any tests for the tool will be in a
832 directory by that name.</para>
833 </listitem>
njn3e986b22004-11-30 10:43:45 +0000834
debad57fc2005-12-03 22:33:29 +0000835 <listitem>
836 <para>Edit <filename>configure.in</filename>, adding
837 <filename>foobar/tests/Makefile</filename> to the
838 <computeroutput>AC_OUTPUT</computeroutput> list.</para>
839 </listitem>
njn3e986b22004-11-30 10:43:45 +0000840
debad57fc2005-12-03 22:33:29 +0000841 <listitem>
842 <para>Write <filename>foobar/tests/Makefile.am</filename>. Use
843 <filename>memcheck/tests/Makefile.am</filename> as an
844 example.</para>
845 </listitem>
njn3e986b22004-11-30 10:43:45 +0000846
debad57fc2005-12-03 22:33:29 +0000847 <listitem>
848 <para>Write the tests, <computeroutput>.vgtest</computeroutput> test
849 description files, <computeroutput>.stdout.exp</computeroutput> and
850 <computeroutput>.stderr.exp</computeroutput> expected output files.
851 (Note that Valgrind's output goes to stderr.) Some details on
852 writing and running tests are given in the comments at the top of
853 the testing script
854 <computeroutput>tests/vg_regtest</computeroutput>.</para>
855 </listitem>
njn3e986b22004-11-30 10:43:45 +0000856
debad57fc2005-12-03 22:33:29 +0000857 <listitem>
858 <para>Write a filter for stderr results
859 <computeroutput>foobar/tests/filter_stderr</computeroutput>. It can
860 call the existing filters in
861 <computeroutput>tests/</computeroutput>. See
862 <computeroutput>memcheck/tests/filter_stderr</computeroutput> for an
863 example; in particular note the
864 <computeroutput>$dir</computeroutput> trick that ensures the filter
865 works correctly from any directory.</para>
866 </listitem>
njn3e986b22004-11-30 10:43:45 +0000867
868</orderedlist>
869
870</sect2>
871
872
873
874<sect2 id="writing-tools.profiling" xreflabel="Profiling">
875<title>Profiling</title>
876
njnbcd75fc2005-12-19 22:48:39 +0000877<para>To profile a tool, use Cachegrind on it. Read README_DEVELOPERS for
878details on running Valgrind under Valgrind.</para>
njn31066fd2005-03-26 00:42:02 +0000879
njn3e986b22004-11-30 10:43:45 +0000880<para>To do simple tick-based profiling of a tool, include the
881line:</para>
882<programlisting><![CDATA[
883 #include "vg_profile.c"]]></programlisting>
njn3e986b22004-11-30 10:43:45 +0000884
debad57fc2005-12-03 22:33:29 +0000885<para>in the tool somewhere, and rebuild (you may have to
886<computeroutput>make clean</computeroutput> first). Then run Valgrind
887with the <option>--profile=yes</option> option.</para>
888
889<para>The profiler is stack-based; you can register a profiling event
890with <function>VG_(register_profile_event)()</function> and then use the
891<computeroutput>VGP_PUSHCC</computeroutput> and
892<computeroutput>VGP_POPCC</computeroutput> macros to record time spent
893doing certain things. New profiling event numbers must not overlap with
894the core profiling event numbers. See
895<filename>include/pub_tool_profile.h</filename> for details and Memcheck
896for an example.</para>
njn3e986b22004-11-30 10:43:45 +0000897
898</sect2>
899
900
901
902<sect2 id="writing-tools.mkhackery" xreflabel="Other Makefile Hackery">
903<title>Other Makefile Hackery</title>
904
905<para>If you add any directories under
debad57fc2005-12-03 22:33:29 +0000906<computeroutput>valgrind/foobar/</computeroutput>, you will need to add
907an appropriate <filename>Makefile.am</filename> to it, and add a
908corresponding entry to the <computeroutput>AC_OUTPUT</computeroutput>
909list in <filename>valgrind/configure.in</filename>.</para>
njn3e986b22004-11-30 10:43:45 +0000910
911<para>If you add any scripts to your tool (see Cachegrind for an
912example) you need to add them to the
913<computeroutput>bin_SCRIPTS</computeroutput> variable in
914<filename>valgrind/foobar/Makefile.am</filename>.</para>
915
916</sect2>
917
918
919
920<sect2 id="writing-tools.ifacever" xreflabel="Core/tool Interface Versions">
921<title>Core/tool Interface Versions</title>
922
debad57fc2005-12-03 22:33:29 +0000923<para>In order to allow for the core/tool interface to evolve over time,
924Valgrind uses a basic interface versioning system. All a tool has to do
925is use the
926<computeroutput>VG_DETERMINE_INTERFACE_VERSION</computeroutput> macro
927exactly once in its code. If not, a link error will occur when the tool
928is built.</para>
njn3e986b22004-11-30 10:43:45 +0000929
debad57fc2005-12-03 22:33:29 +0000930<para>The interface version number has the form X.Y. Changes in Y
931indicate binary compatible changes. Changes in X indicate binary
932incompatible changes. If the core and tool has the same major version
933number X they should work together. If X doesn't match, Valgrind will
934abort execution with an explanation of the problem.</para>
njn3e986b22004-11-30 10:43:45 +0000935
debad57fc2005-12-03 22:33:29 +0000936<para>This approach was chosen so that if the interface changes in the
937future, old tools won't work and the reason will be clearly explained,
938instead of possibly crashing mysteriously. We have attempted to
939minimise the potential for binary incompatible changes by means such as
940minimising the use of naked structs in the interface.</para>
njn3e986b22004-11-30 10:43:45 +0000941
942</sect2>
943
944</sect1>
945
946
947
948<sect1 id="writing-tools.finalwords" xreflabel="Final Words">
949<title>Final Words</title>
950
951<para>This whole core/tool business is under active development,
952although it's slowly maturing.</para>
953
debad57fc2005-12-03 22:33:29 +0000954<para>The first consequence of this is that the core/tool interface will
955continue to change in the future; we have no intention of freezing it
956and then regretting the inevitable stupidities. Hopefully most of the
957future changes will be to add new features, hooks, functions, etc,
958rather than to change old ones, which should cause a minimum of trouble
959for existing tools, and we've put some effort into future-proofing the
960interface to avoid binary incompatibility. But we can't guarantee
961anything. The versioning system should catch any incompatibilities.
962Just something to be aware of.</para>
njn3e986b22004-11-30 10:43:45 +0000963
debad57fc2005-12-03 22:33:29 +0000964<para>The second consequence of this is that we'd love to hear your
965feedback about it:</para>
njn3e986b22004-11-30 10:43:45 +0000966
967<itemizedlist>
debad57fc2005-12-03 22:33:29 +0000968 <listitem>
969 <para>If you love it or hate it</para>
970 </listitem>
971 <listitem>
972 <para>If you find bugs</para>
973 </listitem>
974 <listitem>
975 <para>If you write a tool</para>
976 </listitem>
977 <listitem>
978 <para>If you have suggestions for new features, needs, trackable
979 events, functions</para>
980 </listitem>
981 <listitem>
982 <para>If you have suggestions for making tools easier to
983 write</para>
984 </listitem>
985 <listitem>
986 <para>If you have suggestions for improving this
987 documentation</para>
988 </listitem>
989 <listitem>
990 <para>If you don't understand something</para>
991 </listitem>
njn3e986b22004-11-30 10:43:45 +0000992</itemizedlist>
993
994<para>or anything else!</para>
995
996<para>Happy programming.</para>
997
998</sect1>
999
1000</chapter>