Software: Apache/2.0.54 (Fedora). PHP/5.0.4 uname -a: Linux mina-info.me 2.6.17-1.2142_FC4smp #1 SMP Tue Jul 11 22:57:02 EDT 2006 i686 uid=48(apache) gid=48(apache) groups=48(apache) Safe-mode: OFF (not secure) /usr/share/doc/valgrind-2.4.0/ drwxr-xr-x |
Viewing file: Select action/file-type: Valgrind ToolsThis guide was last updated on 20030520
Nick Nethercote
Valgrind is licensed under the GNU General Public License,
version 2
Contents of this manual1 Introduction1.1 Supervised Execution1.2 Tools 1.3 Execution Spaces 2 Writing a Tool2.1 Why write a tool?2.2 Suggested tools 2.3 How tools work 2.4 Getting the code 2.5 Getting started 2.6 Writing the code 2.7 Initialisation 2.8 Instrumentation 2.9 Finalisation 2.10 Other important information 2.11 Words of advice 3 Advanced Topics3.1 Suppressions3.2 Documentation 3.3 Regression tests 3.4 Profiling 3.5 Other makefile hackery 3.6 Core/tool interface versions 4 Final Words1 Introduction1.1 Supervised ExecutionValgrind provides a generic infrastructure for supervising the execution of programs. This is done by providing a way to instrument programs in very precise ways, making it relatively easy to support activities such as dynamic error detection and profiling.Although writing a tool is not easy, and requires learning quite a few things about Valgrind, it is much easier than instrumenting a program from scratch yourself. 1.2 ToolsThe 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 x86-to-x86 JIT compiler, low-level memory manager, signal handling and a scheduler (for pthreads). It also provides certain services that are useful to some but not all tools, such as support for error recording and suppression. 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 define certain variables 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. 1.3 Execution SpacesAn important concept to understand before writing a tool is that there are three spaces in which program code executes:
It should be noted that a tool only has direct control over code executed in user space. This is the vast majority of code executed, but it is not absolutely all of it, so any profiling information recorded by a tool won't be totally accurate. 2 Writing a Tool2.1 Why write a tool?Before you write a tool, you should have some idea of what it should do. What is it you want to know about your programs of interest? Consider some existing tools:
2.2 Suggested toolsHere is a list of ideas we have had for tools that should not be too hard to implement.
2.3 How tools workTools must define various functions for instrumenting programs that are called by Valgrind's core, yet they must be implemented in such a way that they can be written and compiled without touching Valgrind's core. This is important, because one of our aims is to allow people to write and distribute their own tools that can be plugged into Valgrind's core easily.
This is achieved by packaging each tool into a separate shared object which is
then loaded ahead of the core shared object
This magic is all done for you; the shared object used is chosen with the
2.4 Getting the codeTo write your own tool, you'll need to check out a copy of Valgrind from the CVS repository, rather than using a packaged distribution. This is because it contains several extra files needed for writing tools.To check out the code from the CVS repository, first login:
Then checkout the code. To get a copy of the current development version
(recommended for the brave only):
To get a copy of the stable released branch:
where TAG has the form VALGRIND_X_Y_Z for
version X.Y.Z.
2.5 Getting startedValgrind uses GNUautomake 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
--prefix for
./configure .Now that we've setup, built and tested the simplest possible tool, onto the interesting stuff... 2.6 Writing the codeA tool must define at least these four functions:SK_(pre_clo_init)() SK_(post_clo_init)() SK_(instrument)() SK_(fini)()Also, it must use the macro VG_DETERMINE_INTERFACE_VERSION
exactly once in its source code. If it doesn't, you will get a link error
involving VG_(skin_interface_major_version) . This macro is
used to ensure the core/tool interface used by the core and a plugged-in
tool are binary compatible.
In addition, if a tool wants to use some of the optional services provided by
the core, it may have to define other functions.
2.7 InitialisationMost of the initialisation should be done inSK_(pre_clo_init)() .
Only use SK_(post_clo_init)() if a tool provides command line
options and must do some initialisation after option processing takes place
(``clo '' stands for ``command line options'').
First of all, various ``details'' need to be set for a tool, using the
functions
Second, various ``needs'' can be set for a tool, using the functions
For example, if a tool wants the core's help in recording and reporting errors,
it must set the
Third, the tool can indicate which events in core it wants to be notified
about, using the functions
For example, if the tool want to be notified when a new block of memory is
malloc'd, it should call
More information about ``details'', ``needs'' and ``trackable events'' can be
found in 2.8 InstrumentationSK_(instrument)() is the interesting one. It allows you to
instrument UCode, which is Valgrind's RISC-like intermediate language.
UCode is described in the technical docs for
Memcheck.
The easiest way to instrument UCode is to insert calls to C functions when
interesting things happen. See the tool ``Lackey''
(lackey/lk_main.c ) for a simple example of this, or
Cachegrind (cachegrind/cg_main.c ) for a more complex
example.A much more complicated way to instrument UCode, albeit one that might result in faster instrumented programs, is to extend UCode with new UCode instructions. This is recommended for advanced Valgrind hackers only! See Memcheck for an example. 2.9 FinalisationThis is where you can present the final results, such as a summary of the information collected. Any log files should be written out at this point.2.10 Other important informationPlease note that the core/tool split infrastructure is quite complex and not brilliantly documented. Here are some important points, but there are undoubtedly many others that I should note but haven't thought of.
The file
In particular, you probably shouldn't use anything from the C library (there
are deep reasons for this, trust us). Valgrind provides an implementation of a
reasonable subset of the C library, details of which are in
Similarly, when writing a tool, you shouldn't need to look at any of the code in Valgrind's core. Although it might be useful sometimes to help understand something.
Note that the 2.11 Words of AdviceWriting and debugging tools is not trivial. Here are some suggestions for solving common problems.If you are getting segmentation faults in C functions used by your tool, the usual GDB command:
usually gives the location of the segmentation fault.
If you want to debug C functions used by your tool, you can attach GDB to
Valgrind with some effort; see the file
GDB may be able to give you useful information. Note that by default
most of the system is built with
If you just want to know whether a program point has been reached, using the
If you are having problems with your UCode instrumentation, it's likely that
GDB won't be able to help at all. In this case, Valgrind's
The other debugging command line options can be useful too (run 3 Advanced TopicsOnce a tool becomes more complicated, there are some extra things you may want/need to do.3.1 SuppressionsIf your tool reports errors and you want to suppress some common ones, you can add suppressions to the suppression files. The relevant files arevalgrind/*.supp ; the final suppression file is aggregated from
these files by combining the relevant .supp files depending on the
versions of linux, X and glibc on a system.
Suppression types have the form 3.2 DocumentationIf you are feeling conscientious and want to write some HTML documentation for your tool, follow these steps (usingfoobar as the example tool
name again):
3.3 Regression testsValgrind has some support for regression tests. If you want to write regression tests for your tool:
3.4 ProfilingTo do simple tick-based profiling of a tool, include the line#include "vg_profile.c"in the tool somewhere, and rebuild (you may have to make clean
first). Then run Valgrind with the --profile=yes option.
The profiler is stack-based; you can register a profiling event with
3.5 Other makefile hackeryIf you add any directories undervalgrind/foobar/ , you will
need to add an appropriate Makefile.am to it, and add a
corresponding entry to the AC_OUTPUT list in
valgrind/configure.in .
If you add any scripts to your tool (see Cachegrind for an example) you need to
add them to the 3.5 Core/tool interface versionsIn order to allow for the core/tool interface to evolve over time, Valgrind uses a basic interface versioning system. All a tool has to do is use theVG_DETERMINE_INTERFACE_VERSION macro exactly once in its code.
If not, a link error will occur when the tool is built.
The interface version number has the form X.Y. Changes in Y indicate binary compatible changes. Changes in X indicate binary incompatible changes. If the core and tool has the same major version number X they should work together. If X doesn't match, Valgrind will abort execution with an explanation of the problem. This approach was chosen so that if the interface changes in the future, old tools won't work and the reason will be clearly explained, instead of possibly crashing mysteriously. We have attempted to minimise the potential for binary incompatible changes by means such as minimising the use of naked structs in the interface. 4 Final WordsThis whole core/tool business under active development, although it's slowly maturing.The first consequence of this is that the core/tool interface will continue to change in the future; we have no intention of freezing it and then regretting the inevitable stupidities. Hopefully most of the future changes will be to add new features, hooks, functions, etc, rather than to change old ones, which should cause a minimum of trouble for existing tools, and we've put some effort into future-proofing the interface to avoid binary incompatibility. But we can't guarantee anything. The versioning system should catch any incompatibilities. Just something to be aware of. The second consequence of this is that we'd love to hear your feedback about it:
Happy programming. |
:: Command execute :: | |
--[ c99shell v. 1.0 pre-release build #16 powered by Captain Crunch Security Team | http://ccteam.ru | Generation time: 0.0029 ]-- |