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