njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 1 | <?xml version="1.0"?> <!-- -*- sgml -*- --> |
| 2 | <!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" |
de | 252c614 | 2005-11-27 04:10:00 +0000 | [diff] [blame] | 3 | "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" |
| 4 | [ <!ENTITY % vg-entities SYSTEM "vg-entities.xml"> %vg-entities; ]> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 5 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 6 | |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 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 |
| 17 | the execution of programs. This is done by providing a way to |
| 18 | instrument programs in very precise ways, making it relatively |
| 19 | easy to support activities such as dynamic error detection and |
| 20 | profiling.</para> |
| 21 | |
| 22 | <para>Although writing a tool is not easy, and requires learning |
| 23 | quite a few things about Valgrind, it is much easier than |
| 24 | instrumenting a program from scratch yourself.</para> |
| 25 | |
tom | df2b4a6 | 2005-11-04 12:27:58 +0000 | [diff] [blame] | 26 | <para>[Nb: What follows is slightly out of date.]</para> |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 27 | |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 28 | </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 |
| 35 | between its "core" and "tools".</para> |
| 36 | |
| 37 | <para>The core provides the common low-level infrastructure to |
tom | a721cec | 2005-11-04 14:11:05 +0000 | [diff] [blame] | 38 | support program instrumentation, including the JIT |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 39 | compiler, low-level memory manager, signal handling and a |
| 40 | scheduler (for pthreads). It also provides certain services that |
| 41 | are useful to some but not all tools, such as support for error |
| 42 | recording and suppression.</para> |
| 43 | |
| 44 | <para>But the core leaves certain operations undefined, which |
| 45 | must be filled by tools. Most notably, tools define how program |
tom | a721cec | 2005-11-04 14:11:05 +0000 | [diff] [blame] | 46 | code should be instrumented. They can also call certain |
| 47 | functions to indicate to the core that they would like to use |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 48 | certain services, or be notified when certain interesting events |
| 49 | occur. 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 |
| 58 | that 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> |
tom | a721cec | 2005-11-04 14:11:05 +0000 | [diff] [blame] | 84 | <para>Thread scheduling</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 85 | </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 |
tom | b931c58 | 2005-11-04 12:17:20 +0000 | [diff] [blame] | 94 | when certain interesting events happen, for example when |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 95 | 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 |
| 146 | it should do. What is it you want to know about your programs of |
| 147 | interest? 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> |
| 158 | <para><command>addrcheck</command>: performs lighterweight |
| 159 | addressibility checks of every memory reference performed by |
| 160 | the program.</para> |
| 161 | </listitem> |
| 162 | |
| 163 | <listitem> |
| 164 | <para><command>cachegrind</command>: tracks every instruction |
| 165 | and memory reference to simulate instruction and data caches, |
| 166 | tracking cache accesses and misses that occur on every line in |
| 167 | the program.</para> |
| 168 | </listitem> |
| 169 | |
| 170 | <listitem> |
| 171 | <para><command>helgrind</command>: tracks every memory access |
| 172 | and mutex lock/unlock to determine if a program contains any |
| 173 | data races.</para> |
| 174 | </listitem> |
| 175 | |
| 176 | <listitem> |
| 177 | <para><command>lackey</command>: does simple counting of |
| 178 | various things: the number of calls to a particular function |
| 179 | (<computeroutput>_dl_runtime_resolve()</computeroutput>); the |
tom | a721cec | 2005-11-04 14:11:05 +0000 | [diff] [blame] | 180 | number of basic blocks, guest instructions, VEX instructions |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 181 | executed; the number of branches executed and the proportion of |
tom | b931c58 | 2005-11-04 12:17:20 +0000 | [diff] [blame] | 182 | them which were taken.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 183 | </listitem> |
| 184 | </itemizedlist> |
| 185 | |
| 186 | <para>These examples give a reasonable idea of what kinds of |
| 187 | things Valgrind can be used for. The instrumentation can range |
| 188 | from very lightweight (e.g. counting the number of times a |
| 189 | particular function is called) to very intrusive (e.g. |
| 190 | memcheck's memory checking).</para> |
| 191 | |
| 192 | </sect2> |
| 193 | |
| 194 | |
| 195 | <sect2 id="writing-tools.suggestedtools" xreflabel="Suggested tools"> |
| 196 | <title>Suggested tools</title> |
| 197 | |
| 198 | <para>Here is a list of ideas we have had for tools that should |
| 199 | not be too hard to implement.</para> |
| 200 | |
| 201 | <itemizedlist> |
| 202 | <listitem> |
| 203 | <para><command>branch profiler</command>: A machine's branch |
| 204 | prediction hardware could be simulated, and each branch |
| 205 | annotated with the number of predicted and mispredicted |
| 206 | branches. Would be implemented quite similarly to Cachegrind, |
| 207 | and could reuse the |
| 208 | <computeroutput>cg_annotate</computeroutput> script to annotate |
| 209 | source code.</para> |
| 210 | |
| 211 | <para>The biggest difficulty with this is the simulation; the |
| 212 | chip-makers are very cagey about how their chips do branch |
| 213 | prediction. But implementing one or more of the basic |
| 214 | algorithms could still give good information.</para> |
| 215 | </listitem> |
| 216 | |
| 217 | <listitem> |
| 218 | <para><command>coverage tool</command>: Cachegrind can already |
| 219 | be used for doing test coverage, but it's massive overkill to |
| 220 | use it just for that.</para> |
| 221 | |
| 222 | <para>It would be easy to write a coverage tool that records |
| 223 | how many times each basic block was recorded. Again, the |
| 224 | <computeroutput>cg_annotate</computeroutput> script could be |
| 225 | used for annotating source code with the gathered information. |
| 226 | Although, <computeroutput>cg_annotate</computeroutput> is only |
| 227 | designed for working with single program runs. It could be |
| 228 | extended relatively easily to deal with multiple runs of a |
| 229 | program, so that the coverage of a whole test suite could be |
| 230 | determined.</para> |
| 231 | |
| 232 | <para>In addition to the standard coverage information, such a |
| 233 | tool could record extra information that would help a user |
| 234 | generate test cases to exercise unexercised paths. For |
| 235 | example, for each conditional branch, the tool could record all |
| 236 | inputs to the conditional test, and print these out when |
| 237 | annotating.</para> |
| 238 | </listitem> |
| 239 | |
| 240 | <listitem> |
| 241 | <para><command>run-time type checking</command>: A nice example |
| 242 | of a dynamic checker is given in this paper:</para> |
| 243 | <address>Debugging via Run-Time Type Checking |
| 244 | Alexey Loginov, Suan Hsi Yong, Susan Horwitz and Thomas Reps |
| 245 | Proceedings of Fundamental Approaches to Software Engineering |
| 246 | April 2001. |
| 247 | </address> |
| 248 | |
| 249 | <para>Similar is the tool described in this paper:</para> |
| 250 | <address>Run-Time Type Checking for Binary Programs |
| 251 | Michael Burrows, Stephen N. Freund, Janet L. Wiener |
| 252 | Proceedings of the 12th International Conference on Compiler Construction (CC 2003) |
| 253 | April 2003. |
| 254 | </address> |
| 255 | |
| 256 | <para>This approach can find quite a range of bugs, |
| 257 | particularly in C and C++ programs, and could be implemented |
| 258 | quite nicely as a Valgrind tool.</para> |
| 259 | |
| 260 | <para>Ways to speed up this run-time type checking are |
| 261 | described in this paper:</para> |
| 262 | <address>Reducing the Overhead of Dynamic Analysis |
| 263 | Suan Hsi Yong and Susan Horwitz |
| 264 | Proceedings of Runtime Verification '02 |
| 265 | July 2002. |
| 266 | </address> |
| 267 | |
| 268 | <para>Valgrind's client requests could be used to pass |
| 269 | information to a tool about which elements need instrumentation |
| 270 | and which don't.</para> |
| 271 | </listitem> |
| 272 | </itemizedlist> |
| 273 | |
| 274 | <para>We would love to hear from anyone who implements these or |
| 275 | other tools.</para> |
| 276 | |
| 277 | </sect2> |
| 278 | |
| 279 | |
| 280 | <sect2 id="writing-tools.howtoolswork" xreflabel="How tools work"> |
| 281 | <title>How tools work</title> |
| 282 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 283 | <para>Tools must define various functions for instrumenting programs |
| 284 | that are called by Valgrind's core. They are then linked against the |
| 285 | coregrind library (<filename>libcoregrind.a</filename>) that valgrind |
| 286 | provides as well as the VEX library (<filename>libvex.a</filename>) that |
| 287 | also comes with valgrind and provides the JIT engine.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 288 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 289 | <para>Each tool is linked as a statically linked program and placed in |
| 290 | the valgrind library directory from where valgrind will load it |
| 291 | automatically when the <option>--tool</option> option is used to select |
| 292 | it.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 293 | |
| 294 | </sect2> |
| 295 | |
| 296 | |
| 297 | <sect2 id="writing-tools.gettingcode" xreflabel="Getting the code"> |
| 298 | <title>Getting the code</title> |
| 299 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 300 | <para>To write your own tool, you'll need the Valgrind source code. A |
| 301 | normal source distribution should do, although you might want to check |
| 302 | out the latest code from the Subversion repository. See the information |
| 303 | about how to do so at <ulink url="&vg-svn-repo;">the Valgrind |
| 304 | website</ulink>.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 305 | |
| 306 | </sect2> |
| 307 | |
| 308 | |
| 309 | <sect2 id="writing-tools.gettingstarted" xreflabel="Getting started"> |
| 310 | <title>Getting started</title> |
| 311 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 312 | <para>Valgrind uses GNU <computeroutput>automake</computeroutput> and |
| 313 | <computeroutput>autoconf</computeroutput> for the creation of Makefiles |
| 314 | and configuration. But don't worry, these instructions should be enough |
| 315 | to get you started even if you know nothing about those tools.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 316 | |
| 317 | <para>In what follows, all filenames are relative to Valgrind's |
| 318 | top-level directory <computeroutput>valgrind/</computeroutput>.</para> |
| 319 | |
| 320 | <orderedlist> |
| 321 | <listitem> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 322 | <para>Choose a name for the tool, and an abbreviation that can be used |
| 323 | as a short prefix. We'll use <computeroutput>foobar</computeroutput> |
| 324 | and <computeroutput>fb</computeroutput> as an example.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 325 | </listitem> |
| 326 | |
| 327 | <listitem> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 328 | <para>Make a new directory <computeroutput>foobar/</computeroutput> |
| 329 | which will hold the tool.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 330 | </listitem> |
| 331 | |
| 332 | <listitem> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 333 | <para>Copy <filename>none/Makefile.am</filename> into |
| 334 | <computeroutput>foobar/</computeroutput>. Edit it by replacing all |
| 335 | occurrences of the string <computeroutput>"none"</computeroutput> with |
| 336 | <computeroutput>"foobar"</computeroutput> and the one occurrence of |
| 337 | the string <computeroutput>"nl_"</computeroutput> with |
| 338 | <computeroutput>"fb_"</computeroutput>. It might be worth trying to |
| 339 | understand this file, at least a little; you might have to do more |
| 340 | complicated things with it later on. In particular, the name of the |
| 341 | <computeroutput>foobar_SOURCES</computeroutput> variable determines |
| 342 | the name of the tool, which determines what name must be passed to the |
| 343 | <option>--tool</option> option to use the tool.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 344 | </listitem> |
| 345 | |
| 346 | <listitem> |
| 347 | <para>Copy <filename>none/nl_main.c</filename> into |
| 348 | <computeroutput>foobar/</computeroutput>, renaming it as |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 349 | <filename>fb_main.c</filename>. Edit it by changing the lines in |
| 350 | <function>pre_clo_init()</function> to something appropriate for the |
| 351 | tool. These fields are used in the startup message, except for |
| 352 | <computeroutput>bug_reports_to</computeroutput> which is used if a |
| 353 | tool assertion fails.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 354 | </listitem> |
| 355 | |
| 356 | <listitem> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 357 | <para>Edit <filename>Makefile.am</filename>, adding the new directory |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 358 | <computeroutput>foobar</computeroutput> to the |
| 359 | <computeroutput>SUBDIRS</computeroutput> variable.</para> |
| 360 | </listitem> |
| 361 | |
| 362 | <listitem> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 363 | <para>Edit <filename>configure.in</filename>, adding |
| 364 | <filename>foobar/Makefile</filename> to the |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 365 | <computeroutput>AC_OUTPUT</computeroutput> list.</para> |
| 366 | </listitem> |
| 367 | |
| 368 | <listitem> |
| 369 | <para>Run:</para> |
| 370 | <programlisting><![CDATA[ |
| 371 | autogen.sh |
| 372 | ./configure --prefix=`pwd`/inst |
| 373 | make install]]></programlisting> |
| 374 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 375 | <para>It should automake, configure and compile without errors, |
| 376 | putting copies of the tool in |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 377 | <computeroutput>foobar/</computeroutput> and |
| 378 | <computeroutput>inst/lib/valgrind/</computeroutput>.</para> |
| 379 | </listitem> |
| 380 | |
| 381 | <listitem> |
| 382 | <para>You can test it with a command like:</para> |
| 383 | <programlisting><![CDATA[ |
| 384 | inst/bin/valgrind --tool=foobar date]]></programlisting> |
| 385 | |
| 386 | <para>(almost any program should work; |
| 387 | <computeroutput>date</computeroutput> is just an example). |
| 388 | The output should be something like this:</para> |
| 389 | <programlisting><![CDATA[ |
| 390 | ==738== foobar-0.0.1, a foobarring tool for x86-linux. |
| 391 | ==738== Copyright (C) 1066AD, and GNU GPL'd, by J. Random Hacker. |
| 392 | ==738== Built with valgrind-1.1.0, a program execution monitor. |
| 393 | ==738== Copyright (C) 2000-2003, and GNU GPL'd, by Julian Seward. |
| 394 | ==738== Estimated CPU clock rate is 1400 MHz |
| 395 | ==738== For more details, rerun with: -v |
| 396 | ==738== Wed Sep 25 10:31:54 BST 2002 |
| 397 | ==738==]]></programlisting> |
| 398 | |
| 399 | <para>The tool does nothing except run the program |
| 400 | uninstrumented.</para> |
| 401 | </listitem> |
| 402 | |
| 403 | </orderedlist> |
| 404 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 405 | <para>These steps don't have to be followed exactly - you can choose |
| 406 | different names for your source files, and use a different |
| 407 | <option>--prefix</option> for |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 408 | <computeroutput>./configure</computeroutput>.</para> |
| 409 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 410 | <para>Now that we've setup, built and tested the simplest possible tool, |
| 411 | onto the interesting stuff...</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 412 | |
| 413 | </sect2> |
| 414 | |
| 415 | |
| 416 | |
| 417 | <sect2 id="writing-tools.writingcode" xreflabel="Writing the Code"> |
| 418 | <title>Writing the code</title> |
| 419 | |
| 420 | <para>A tool must define at least these four functions:</para> |
| 421 | <programlisting><![CDATA[ |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 422 | pre_clo_init() |
| 423 | post_clo_init() |
| 424 | instrument() |
| 425 | fini()]]></programlisting> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 426 | |
| 427 | <para>Also, it must use the macro |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 428 | <computeroutput>VG_DETERMINE_INTERFACE_VERSION</computeroutput> exactly |
| 429 | once in its source code. If it doesn't, you will get a link error |
| 430 | involving <computeroutput>VG_(tool_interface_version)</computeroutput>. |
| 431 | This macro is used to ensure the core/tool interface used by the core |
| 432 | and a plugged-in tool are binary compatible.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 433 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 434 | <para>In addition, if a tool wants to use some of the optional services |
| 435 | provided by the core, it may have to define other functions and tell the |
| 436 | code about them.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 437 | |
| 438 | </sect2> |
| 439 | |
| 440 | |
| 441 | |
| 442 | <sect2 id="writing-tools.init" xreflabel="Initialisation"> |
| 443 | <title>Initialisation</title> |
| 444 | |
| 445 | <para>Most of the initialisation should be done in |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 446 | <function>pre_clo_init()</function>. Only use |
| 447 | <function>post_clo_init()</function> if a tool provides command line |
| 448 | options and must do some initialisation after option processing takes |
| 449 | place (<computeroutput>"clo"</computeroutput> stands for "command line |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 450 | options").</para> |
| 451 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 452 | <para>First of all, various "details" need to be set for a tool, using |
| 453 | the functions <function>VG_(details_*)()</function>. Some are all |
| 454 | compulsory, some aren't. Some are used when constructing the startup |
| 455 | message, <computeroutput>detail_bug_reports_to</computeroutput> is used |
| 456 | if <computeroutput>VG_(tool_panic)()</computeroutput> is ever called, or |
| 457 | a tool assertion fails. Others have other uses.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 458 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 459 | <para>Second, various "needs" can be set for a tool, using the functions |
| 460 | <function>VG_(needs_*)()</function>. They are mostly booleans, and can |
| 461 | be left untouched (they default to <varname>False</varname>). They |
| 462 | determine whether a tool can do various things such as: record, report |
| 463 | and suppress errors; process command line options; wrap system calls; |
| 464 | record extra information about malloc'd blocks, etc.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 465 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 466 | <para>For example, if a tool wants the core's help in recording and |
| 467 | reporting errors, it must call |
| 468 | <function>VG_(needs_tool_errors)</function> and provide definitions of |
| 469 | eight functions for comparing errors, printing out errors, reading |
| 470 | suppressions from a suppressions file, etc. While writing these |
| 471 | functions requires some work, it's much less than doing error handling |
| 472 | from scratch because the core is doing most of the work. See the |
| 473 | function <function>VG_(needs_tool_errors)</function> in |
| 474 | <filename>include/pub_tool_tooliface.h</filename> for full details of |
| 475 | all the needs.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 476 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 477 | <para>Third, the tool can indicate which events in core it wants to be |
| 478 | notified about, using the functions <function>VG_(track_*)()</function>. |
| 479 | These include things such as blocks of memory being malloc'd, the stack |
| 480 | pointer changing, a mutex being locked, etc. If a tool wants to know |
| 481 | about this, it should provide a pointer to a function, which will be |
| 482 | called when that event happens.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 483 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 484 | <para>For example, if the tool want to be notified when a new block of |
| 485 | memory is malloc'd, it should call |
| 486 | <function>VG_(track_new_mem_heap)()</function> with an appropriate |
| 487 | function pointer, and the assigned function will be called each time |
| 488 | this happens.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 489 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 490 | <para>More information about "details", "needs" and "trackable events" |
| 491 | can be found in |
tom | b931c58 | 2005-11-04 12:17:20 +0000 | [diff] [blame] | 492 | <filename>include/pub_tool_tooliface.h</filename>.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 493 | |
| 494 | </sect2> |
| 495 | |
| 496 | |
| 497 | |
| 498 | <sect2 id="writing-tools.instr" xreflabel="Instrumentation"> |
| 499 | <title>Instrumentation</title> |
| 500 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 501 | <para><function>instrument()</function> is the interesting one. It |
| 502 | allows you to instrument <emphasis>VEX IR</emphasis>, which is |
| 503 | Valgrind's RISC-like intermediate language. VEX IR is described in |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 504 | <xref linkend="mc-tech-docs.ucode"/>.</para> |
| 505 | |
tom | a721cec | 2005-11-04 14:11:05 +0000 | [diff] [blame] | 506 | <para>The easiest way to instrument VEX IR is to insert calls to C |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 507 | functions when interesting things happen. See the tool "Lackey" |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 508 | (<filename>lackey/lk_main.c</filename>) for a simple example of this, or |
| 509 | Cachegrind (<filename>cachegrind/cg_main.c</filename>) for a more |
| 510 | complex example.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 511 | |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 512 | </sect2> |
| 513 | |
| 514 | |
| 515 | |
| 516 | <sect2 id="writing-tools.fini" xreflabel="Finalisation"> |
| 517 | <title>Finalisation</title> |
| 518 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 519 | <para>This is where you can present the final results, such as a summary |
| 520 | of the information collected. Any log files should be written out at |
| 521 | this point.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 522 | |
| 523 | </sect2> |
| 524 | |
| 525 | |
| 526 | |
| 527 | <sect2 id="writing-tools.otherinfo" xreflabel="Other Important Information"> |
| 528 | <title>Other Important Information</title> |
| 529 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 530 | <para>Please note that the core/tool split infrastructure is quite |
| 531 | complex and not brilliantly documented. Here are some important points, |
| 532 | but there are undoubtedly many others that I should note but haven't |
| 533 | thought of.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 534 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 535 | <para>The files <filename>include/pub_tool_*.h</filename> contain all |
| 536 | the types, macros, functions, etc. that a tool should (hopefully) need, |
| 537 | and are the only <filename>.h</filename> files a tool should need to |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 538 | <computeroutput>#include</computeroutput>.</para> |
| 539 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 540 | <para>In particular, you can't use anything from the C library (there |
| 541 | are deep reasons for this, trust us). Valgrind provides an |
| 542 | implementation of a reasonable subset of the C library, details of which |
| 543 | are in <filename>pub_tool_libc*.h</filename>.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 544 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 545 | <para>Similarly, when writing a tool, you shouldn't need to look at any |
| 546 | of the code in Valgrind's core. Although it might be useful sometimes |
| 547 | to help understand something.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 548 | |
tom | df2b4a6 | 2005-11-04 12:27:58 +0000 | [diff] [blame] | 549 | <para>The <filename>pub_tool_*.h</filename> files have a reasonable |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 550 | amount of documentation in it that should hopefully be enough to get you |
| 551 | going. But ultimately, the tools distributed (Memcheck, Addrcheck, |
| 552 | Cachegrind, Lackey, etc.) are probably the best documentation of all, |
| 553 | for the moment.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 554 | |
tom | a721cec | 2005-11-04 14:11:05 +0000 | [diff] [blame] | 555 | <para>Note that the <computeroutput>VG_</computeroutput> macro is used |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 556 | heavily. This just prepends a longer string in front of names to avoid |
| 557 | potential namespace clashes.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 558 | |
| 559 | </sect2> |
| 560 | |
| 561 | |
| 562 | <sect2 id="writing-tools.advice" xreflabel="Words of Advice"> |
| 563 | <title>Words of Advice</title> |
| 564 | |
| 565 | <para>Writing and debugging tools is not trivial. Here are some |
| 566 | suggestions for solving common problems.</para> |
| 567 | |
| 568 | |
| 569 | <sect3 id="writing-tools.segfaults"> |
| 570 | <title>Segmentation Faults</title> |
| 571 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 572 | <para>If you are getting segmentation faults in C functions used by your |
| 573 | tool, the usual GDB command:</para> |
| 574 | |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 575 | <screen><![CDATA[ |
| 576 | gdb <prog> core]]></screen> |
| 577 | <para>usually gives the location of the segmentation fault.</para> |
| 578 | |
| 579 | </sect3> |
| 580 | |
| 581 | |
| 582 | <sect3 id="writing-tools.debugfns"> |
| 583 | <title>Debugging C functions</title> |
| 584 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 585 | <para>If you want to debug C functions used by your tool, you can |
tom | 605535f | 2005-11-29 09:59:32 +0000 | [diff] [blame] | 586 | achieve this by following these steps:</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 587 | <orderedlist> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 588 | <listitem> |
| 589 | <para>Set <computeroutput>VALGRIND_LAUNCHER</computeroutput> to |
| 590 | <computeroutput><![CDATA[<prefix>/bin/valgrind]]></computeroutput>:</para> |
de | d3c3537 | 2005-11-29 16:06:55 +0000 | [diff] [blame] | 591 | <programlisting> |
| 592 | export VALGRIND_LAUNCHER=/usr/local/bin/valgrind</programlisting> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 593 | </listitem> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 594 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 595 | <listitem> |
| 596 | <para>Then run <computeroutput><![CDATA[ gdb <prefix>/lib/valgrind/<platform>/<tool>:]]></computeroutput></para> |
de | d3c3537 | 2005-11-29 16:06:55 +0000 | [diff] [blame] | 597 | <programlisting> |
| 598 | gdb /usr/local/lib/valgrind/ppc32-linux/lackey</programlisting> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 599 | </listitem> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 600 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 601 | <listitem> |
| 602 | <para>Do <computeroutput>handle SIGSEGV SIGILL nostop |
| 603 | noprint</computeroutput> in GDB to prevent GDB from stopping on a |
| 604 | SIGSEGV or SIGILL:</para> |
de | d3c3537 | 2005-11-29 16:06:55 +0000 | [diff] [blame] | 605 | <programlisting> |
| 606 | (gdb) handle SIGILL SIGSEGV nostop noprint</programlisting> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 607 | </listitem> |
tom | 605535f | 2005-11-29 09:59:32 +0000 | [diff] [blame] | 608 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 609 | <listitem> |
| 610 | <para>Set any breakpoints you want and proceed as normal for GDB:</para> |
de | d3c3537 | 2005-11-29 16:06:55 +0000 | [diff] [blame] | 611 | <programlisting> |
| 612 | (gdb) b vgPlain_do_exec</programlisting> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 613 | <para>The macro VG_(FUNC) is expanded to vgPlain_FUNC, so If you |
| 614 | want to set a breakpoint VG_(do_exec), you could do like this in |
| 615 | GDB.</para> |
| 616 | </listitem> |
tom | 605535f | 2005-11-29 09:59:32 +0000 | [diff] [blame] | 617 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 618 | <listitem> |
| 619 | <para>Run the tool with required options:</para> |
de | d3c3537 | 2005-11-29 16:06:55 +0000 | [diff] [blame] | 620 | <programlisting> |
| 621 | (gdb) run `pwd`</programlisting> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 622 | </listitem> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 623 | |
| 624 | </orderedlist> |
| 625 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 626 | <para>GDB may be able to give you useful information. Note that by |
| 627 | default most of the system is built with |
| 628 | <option>-fomit-frame-pointer</option>, and you'll need to get rid of |
| 629 | this to extract useful tracebacks from GDB.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 630 | |
| 631 | </sect3> |
| 632 | |
| 633 | |
| 634 | <sect3 id="writing-tools.ucode-probs"> |
| 635 | <title>UCode Instrumentation Problems</title> |
| 636 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 637 | <para>If you are having problems with your VEX UIR instrumentation, it's |
| 638 | likely that GDB won't be able to help at all. In this case, Valgrind's |
| 639 | <option>--trace-flags</option> option is invaluable for observing the |
| 640 | results of instrumentation.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 641 | |
| 642 | </sect3> |
| 643 | |
| 644 | |
| 645 | <sect3 id="writing-tools.misc"> |
| 646 | <title>Miscellaneous</title> |
| 647 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 648 | <para>If you just want to know whether a program point has been reached, |
| 649 | using the <computeroutput>OINK</computeroutput> macro (in |
| 650 | <filename>include/pub_tool_libcprint.h</filename>) can be easier than |
| 651 | using GDB.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 652 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 653 | <para>The other debugging command line options can be useful too (run |
| 654 | <computeroutput>valgrind --help-debug</computeroutput> for the |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 655 | list).</para> |
| 656 | |
| 657 | </sect3> |
| 658 | |
| 659 | </sect2> |
| 660 | |
| 661 | </sect1> |
| 662 | |
| 663 | |
| 664 | |
| 665 | <sect1 id="writing-tools.advtopics" xreflabel="Advanced Topics"> |
| 666 | <title>Advanced Topics</title> |
| 667 | |
| 668 | <para>Once a tool becomes more complicated, there are some extra |
| 669 | things you may want/need to do.</para> |
| 670 | |
| 671 | <sect2 id="writing-tools.suppressions" xreflabel="Suppressions"> |
| 672 | <title>Suppressions</title> |
| 673 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 674 | <para>If your tool reports errors and you want to suppress some common |
| 675 | ones, you can add suppressions to the suppression files. The relevant |
| 676 | files are <filename>valgrind/*.supp</filename>; the final suppression |
| 677 | file is aggregated from these files by combining the relevant |
| 678 | <filename>.supp</filename> files depending on the versions of linux, X |
| 679 | and glibc on a system.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 680 | |
| 681 | <para>Suppression types have the form |
| 682 | <computeroutput>tool_name:suppression_name</computeroutput>. The |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 683 | <computeroutput>tool_name</computeroutput> here is the name you specify |
| 684 | for the tool during initialisation with |
| 685 | <function>VG_(details_name)()</function>.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 686 | |
| 687 | </sect2> |
| 688 | |
| 689 | |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 690 | <sect2 id="writing-tools.docs" xreflabel="Documentation"> |
| 691 | <title>Documentation</title> |
| 692 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 693 | <para>As of version 3.0.0, Valgrind documentation has been converted to |
| 694 | XML. Why? See <ulink url="http://www.ucc.ie/xml/">The XML FAQ</ulink>. |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 695 | </para> |
| 696 | |
| 697 | |
| 698 | <sect3 id="writing-tools.xml" xreflabel="The XML Toolchain"> |
| 699 | <title>The XML Toolchain</title> |
| 700 | |
| 701 | <para>If you are feeling conscientious and want to write some |
| 702 | documentation for your tool, please use XML. The Valgrind |
| 703 | Docs use the following toolchain and versions:</para> |
| 704 | |
| 705 | <programlisting> |
| 706 | xmllint: using libxml version 20607 |
| 707 | xsltproc: using libxml 20607, libxslt 10102 and libexslt 802 |
| 708 | pdfxmltex: pdfTeX (Web2C 7.4.5) 3.14159-1.10b |
| 709 | pdftops: version 3.00 |
| 710 | DocBook: version 4.2 |
| 711 | </programlisting> |
| 712 | |
| 713 | <para><command>Latency:</command> you should note that latency is |
| 714 | a big problem: DocBook is constantly being updated, but the tools |
| 715 | tend to lag behind somewhat. It is important that the versions |
| 716 | get on with each other, so if you decide to upgrade something, |
| 717 | then you need to ascertain whether things still work nicely - |
| 718 | this *cannot* be assumed.</para> |
| 719 | |
| 720 | <para><command>Stylesheets:</command> The Valgrind docs use |
| 721 | various custom stylesheet layers, all of which are in |
| 722 | <computeroutput>valgrind/docs/lib/</computeroutput>. You |
| 723 | shouldn't need to modify these in any way.</para> |
| 724 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 725 | <para><command>Catalogs:</command> Catalogs provide a mapping from |
| 726 | generic addresses to specific local directories on a given machine. |
| 727 | Most recent Linux distributions have adopted a common place for storing |
| 728 | catalogs (<filename>/etc/xml/</filename>). Assuming that you have the |
| 729 | various tools listed above installed, you probably won't need to modify |
| 730 | your catalogs. But if you do, then just add another |
| 731 | <computeroutput>group</computeroutput> to this file, reflecting your |
| 732 | local installation.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 733 | |
| 734 | </sect3> |
| 735 | |
| 736 | |
| 737 | <sect3 id="writing-tools.writing" xreflabel="Writing the Documentation"> |
| 738 | <title>Writing the Documentation</title> |
| 739 | |
| 740 | <para>Follow these steps (using <computeroutput>foobar</computeroutput> |
| 741 | as the example tool name again):</para> |
| 742 | |
| 743 | <orderedlist> |
| 744 | |
| 745 | <listitem> |
| 746 | <para>Make a directory |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 747 | <computeroutput>valgrind/foobar/docs/</computeroutput>.</para> |
| 748 | </listitem> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 749 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 750 | <listitem> |
| 751 | <para>Copy the XML documentation file for the tool Nulgrind from |
| 752 | <filename>valgrind/none/docs/nl-manual.xml</filename> to |
| 753 | <computeroutput>foobar/docs/</computeroutput>, and rename it to |
| 754 | <filename>foobar/docs/fb-manual.xml</filename>.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 755 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 756 | <para><command>Note</command>: there is a *really stupid* tetex bug |
| 757 | with underscores in filenames, so don't use '_'.</para> |
| 758 | </listitem> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 759 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 760 | <listitem> |
| 761 | <para>Write the documentation. There are some helpful bits and |
| 762 | pieces on using xml markup in |
| 763 | <filename>valgrind/docs/xml/xml_help.txt</filename>.</para> |
| 764 | </listitem> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 765 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 766 | <listitem> |
| 767 | <para>Include it in the User Manual by adding the relevant entry to |
| 768 | <filename>valgrind/docs/xml/manual.xml</filename>. Copy and edit an |
| 769 | existing entry.</para> |
| 770 | </listitem> |
| 771 | |
| 772 | <listitem> |
| 773 | <para>Validate <filename>foobar/docs/fb-manual.xml</filename> using |
| 774 | the following command from within <filename>valgrind/docs/</filename>: |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 775 | </para> |
| 776 | <screen><![CDATA[ |
| 777 | % make valid |
| 778 | ]]></screen> |
| 779 | |
| 780 | <para>You will probably get errors that look like this:</para> |
| 781 | |
| 782 | <screen><![CDATA[ |
| 783 | ./xml/index.xml:5: element chapter: validity error : No declaration for |
| 784 | attribute base of element chapter |
| 785 | ]]></screen> |
| 786 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 787 | <para>Ignore (only) these -- they're not important.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 788 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 789 | <para>Because the xml toolchain is fragile, it is important to ensure |
| 790 | that <filename>fb-manual.xml</filename> won't break the documentation |
| 791 | set build. Note that just because an xml file happily transforms to |
| 792 | html does not necessarily mean the same holds true for pdf/ps.</para> |
| 793 | </listitem> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 794 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 795 | <listitem> |
| 796 | <para>You can (re-)generate the HTML docs while you are writing |
| 797 | <filename>fb-manual.xml</filename> to help you see how it's looking. |
| 798 | The generated files end up in |
| 799 | <filename>valgrind/docs/html/</filename>. Use the following |
| 800 | command, within <filename>valgrind/docs/</filename>:</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 801 | <screen><![CDATA[ |
| 802 | % make html-docs |
| 803 | ]]></screen> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 804 | </listitem> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 805 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 806 | <listitem> |
| 807 | <para>When you have finished, also generate pdf and ps output to |
| 808 | check all is well, from within <filename>valgrind/docs/</filename>: |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 809 | </para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 810 | <screen><![CDATA[ |
| 811 | % make print-docs |
| 812 | ]]></screen> |
| 813 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 814 | <para>Check the output <filename>.pdf</filename> and |
| 815 | <filename>.ps</filename> files in |
| 816 | <computeroutput>valgrind/docs/print/</computeroutput>.</para> |
| 817 | </listitem> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 818 | |
| 819 | </orderedlist> |
| 820 | |
| 821 | </sect3> |
| 822 | |
| 823 | </sect2> |
| 824 | |
| 825 | |
| 826 | <sect2 id="writing-tools.regtests" xreflabel="Regression Tests"> |
| 827 | <title>Regression Tests</title> |
| 828 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 829 | <para>Valgrind has some support for regression tests. If you want to |
| 830 | write regression tests for your tool:</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 831 | |
| 832 | <orderedlist> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 833 | <listitem> |
| 834 | <para>Make a directory |
| 835 | <computeroutput>foobar/tests/</computeroutput>. Make sure the name |
| 836 | of the directory is <computeroutput>tests/</computeroutput> as the |
| 837 | build system assumes that any tests for the tool will be in a |
| 838 | directory by that name.</para> |
| 839 | </listitem> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 840 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 841 | <listitem> |
| 842 | <para>Edit <filename>configure.in</filename>, adding |
| 843 | <filename>foobar/tests/Makefile</filename> to the |
| 844 | <computeroutput>AC_OUTPUT</computeroutput> list.</para> |
| 845 | </listitem> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 846 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 847 | <listitem> |
| 848 | <para>Write <filename>foobar/tests/Makefile.am</filename>. Use |
| 849 | <filename>memcheck/tests/Makefile.am</filename> as an |
| 850 | example.</para> |
| 851 | </listitem> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 852 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 853 | <listitem> |
| 854 | <para>Write the tests, <computeroutput>.vgtest</computeroutput> test |
| 855 | description files, <computeroutput>.stdout.exp</computeroutput> and |
| 856 | <computeroutput>.stderr.exp</computeroutput> expected output files. |
| 857 | (Note that Valgrind's output goes to stderr.) Some details on |
| 858 | writing and running tests are given in the comments at the top of |
| 859 | the testing script |
| 860 | <computeroutput>tests/vg_regtest</computeroutput>.</para> |
| 861 | </listitem> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 862 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 863 | <listitem> |
| 864 | <para>Write a filter for stderr results |
| 865 | <computeroutput>foobar/tests/filter_stderr</computeroutput>. It can |
| 866 | call the existing filters in |
| 867 | <computeroutput>tests/</computeroutput>. See |
| 868 | <computeroutput>memcheck/tests/filter_stderr</computeroutput> for an |
| 869 | example; in particular note the |
| 870 | <computeroutput>$dir</computeroutput> trick that ensures the filter |
| 871 | works correctly from any directory.</para> |
| 872 | </listitem> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 873 | |
| 874 | </orderedlist> |
| 875 | |
| 876 | </sect2> |
| 877 | |
| 878 | |
| 879 | |
| 880 | <sect2 id="writing-tools.profiling" xreflabel="Profiling"> |
| 881 | <title>Profiling</title> |
| 882 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 883 | <para>Nb: as of 25-Mar-2005, the profiling is broken, and has been for a |
| 884 | long time...</para> |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 885 | |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 886 | <para>To do simple tick-based profiling of a tool, include the |
| 887 | line:</para> |
| 888 | <programlisting><![CDATA[ |
| 889 | #include "vg_profile.c"]]></programlisting> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 890 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 891 | <para>in the tool somewhere, and rebuild (you may have to |
| 892 | <computeroutput>make clean</computeroutput> first). Then run Valgrind |
| 893 | with the <option>--profile=yes</option> option.</para> |
| 894 | |
| 895 | <para>The profiler is stack-based; you can register a profiling event |
| 896 | with <function>VG_(register_profile_event)()</function> and then use the |
| 897 | <computeroutput>VGP_PUSHCC</computeroutput> and |
| 898 | <computeroutput>VGP_POPCC</computeroutput> macros to record time spent |
| 899 | doing certain things. New profiling event numbers must not overlap with |
| 900 | the core profiling event numbers. See |
| 901 | <filename>include/pub_tool_profile.h</filename> for details and Memcheck |
| 902 | for an example.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 903 | |
| 904 | </sect2> |
| 905 | |
| 906 | |
| 907 | |
| 908 | <sect2 id="writing-tools.mkhackery" xreflabel="Other Makefile Hackery"> |
| 909 | <title>Other Makefile Hackery</title> |
| 910 | |
| 911 | <para>If you add any directories under |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 912 | <computeroutput>valgrind/foobar/</computeroutput>, you will need to add |
| 913 | an appropriate <filename>Makefile.am</filename> to it, and add a |
| 914 | corresponding entry to the <computeroutput>AC_OUTPUT</computeroutput> |
| 915 | list in <filename>valgrind/configure.in</filename>.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 916 | |
| 917 | <para>If you add any scripts to your tool (see Cachegrind for an |
| 918 | example) you need to add them to the |
| 919 | <computeroutput>bin_SCRIPTS</computeroutput> variable in |
| 920 | <filename>valgrind/foobar/Makefile.am</filename>.</para> |
| 921 | |
| 922 | </sect2> |
| 923 | |
| 924 | |
| 925 | |
| 926 | <sect2 id="writing-tools.ifacever" xreflabel="Core/tool Interface Versions"> |
| 927 | <title>Core/tool Interface Versions</title> |
| 928 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 929 | <para>In order to allow for the core/tool interface to evolve over time, |
| 930 | Valgrind uses a basic interface versioning system. All a tool has to do |
| 931 | is use the |
| 932 | <computeroutput>VG_DETERMINE_INTERFACE_VERSION</computeroutput> macro |
| 933 | exactly once in its code. If not, a link error will occur when the tool |
| 934 | is built.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 935 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 936 | <para>The interface version number has the form X.Y. Changes in Y |
| 937 | indicate binary compatible changes. Changes in X indicate binary |
| 938 | incompatible changes. If the core and tool has the same major version |
| 939 | number X they should work together. If X doesn't match, Valgrind will |
| 940 | abort execution with an explanation of the problem.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 941 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 942 | <para>This approach was chosen so that if the interface changes in the |
| 943 | future, old tools won't work and the reason will be clearly explained, |
| 944 | instead of possibly crashing mysteriously. We have attempted to |
| 945 | minimise the potential for binary incompatible changes by means such as |
| 946 | minimising the use of naked structs in the interface.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 947 | |
| 948 | </sect2> |
| 949 | |
| 950 | </sect1> |
| 951 | |
| 952 | |
| 953 | |
| 954 | <sect1 id="writing-tools.finalwords" xreflabel="Final Words"> |
| 955 | <title>Final Words</title> |
| 956 | |
| 957 | <para>This whole core/tool business is under active development, |
| 958 | although it's slowly maturing.</para> |
| 959 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 960 | <para>The first consequence of this is that the core/tool interface will |
| 961 | continue to change in the future; we have no intention of freezing it |
| 962 | and then regretting the inevitable stupidities. Hopefully most of the |
| 963 | future changes will be to add new features, hooks, functions, etc, |
| 964 | rather than to change old ones, which should cause a minimum of trouble |
| 965 | for existing tools, and we've put some effort into future-proofing the |
| 966 | interface to avoid binary incompatibility. But we can't guarantee |
| 967 | anything. The versioning system should catch any incompatibilities. |
| 968 | Just something to be aware of.</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 969 | |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 970 | <para>The second consequence of this is that we'd love to hear your |
| 971 | feedback about it:</para> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 972 | |
| 973 | <itemizedlist> |
de | bad57fc | 2005-12-03 22:33:29 +0000 | [diff] [blame] | 974 | <listitem> |
| 975 | <para>If you love it or hate it</para> |
| 976 | </listitem> |
| 977 | <listitem> |
| 978 | <para>If you find bugs</para> |
| 979 | </listitem> |
| 980 | <listitem> |
| 981 | <para>If you write a tool</para> |
| 982 | </listitem> |
| 983 | <listitem> |
| 984 | <para>If you have suggestions for new features, needs, trackable |
| 985 | events, functions</para> |
| 986 | </listitem> |
| 987 | <listitem> |
| 988 | <para>If you have suggestions for making tools easier to |
| 989 | write</para> |
| 990 | </listitem> |
| 991 | <listitem> |
| 992 | <para>If you have suggestions for improving this |
| 993 | documentation</para> |
| 994 | </listitem> |
| 995 | <listitem> |
| 996 | <para>If you don't understand something</para> |
| 997 | </listitem> |
njn | 3e986b2 | 2004-11-30 10:43:45 +0000 | [diff] [blame] | 998 | </itemizedlist> |
| 999 | |
| 1000 | <para>or anything else!</para> |
| 1001 | |
| 1002 | <para>Happy programming.</para> |
| 1003 | |
| 1004 | </sect1> |
| 1005 | |
| 1006 | </chapter> |