2. Writing a New Valgrind Tool
So you want to write a Valgrind tool? Here are some instructions that may help.
2.1. Introduction
The key idea behind Valgrind’s architecture is the division between its
core
and
tools
.
The core provides the common low-level infrastructure to support program instrumentation, including the JIT
compiler, low-level memory manager, signal handling and a thread scheduler.
It also provides certain services
that are useful to some but not all tools, such as support for error recording, and support for replacing heap allocation
functions such as
malloc
.
But the core leaves certain operations undefined, which must be filled by tools.
Most notably, tools define how
program code should be instrumented. They can also call certain functions to indicate to the core that they would like
to use certain services, or be notified when certain interesting events occur.
But the core takes care of all the hard
work.
2.2. Basics
2.2.1. How tools work
Tools must define various functions for instrumenting programs that are called by Valgrind’s core. They are then
linked against Valgrind’s core to define a complete Valgrind tool which will be used when the
--tool
option is used
to select it.
2.2.2. Getting the code
To write your own tool, you’ll need the Valgrind source code. You’ll need a check-out of the Subversion repository for
the automake/autoconf build instructions to work. See the information about how to do check-out from the repository
at the Valgrind website.
2.2.3. Getting started
Valgrind uses GNU
automake
and
autoconf
for the creation of Makefiles and configuration.
But don’t worry,
these instructions should be enough to get you started even if you know nothing about those tools.
In what follows, all filenames are relative to Valgrind’s top-level directory
valgrind/
.
1. Choose a name for the tool, and a two-letter abbreviation that can be used as a short prefix.
We’ll use
foobar
and
fb
as an example.
2. Make three new directories
foobar/
,
foobar/docs/
and
foobar/tests/
.
3. Create an empty file
foobar/tests/Makefile.am
.
4. Copy
none/Makefile.am
into
foobar/
. Edit it by replacing all occurrences of the strings
"none"
,
"nl_"
and
"nl-"
with
"foobar"
,
"fb_"
and
"fb-"
respectively.
2