1 Building and installing it
2 ~~~~~~~~~~~~~~~~~~~~~~~~~~
3 To build/install from the GIT repository or from a distribution
4 tarball, refer to the section with the same name in README.
6 Building Valgrind requires autoconf, GNU make and a suitable C
10 Building and not installing it
11 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 To run Valgrind without having to install it, run coregrind/valgrind
13 with the VALGRIND_LIB environment variable set, where <dir> is the root
14 of the source tree (and must be an absolute path). Eg:
16 VALGRIND_LIB=~/grind/head4/.in_place ~/grind/head4/coregrind/valgrind
18 This allows you to compile and run with "make" instead of "make install",
21 Or, you can use the 'vg-in-place' script which does that for you.
23 I recommend compiling with "make --quiet" to further reduce the amount of
24 output spewed out during compilation, letting you actually see any errors,
28 Building a distribution tarball
29 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30 To build a distribution tarball from the valgrind sources:
34 In addition to compiling, linking and packaging everything up, the command
35 will also attempt to build the documentation.
37 If you only want to test whether the generated tarball is complete and runs
38 regression tests successfully, building documentation is not needed.
40 make dist BUILD_ALL_DOCS=no
42 If you insist on building documentation some embarrassing instructions
43 can be found in docs/README.
46 Running the regression tests
47 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49 Running the regression tests requires GNU sed, python 3.9 or later, gdb,
50 and a suitable C++ compiler
52 To build and run all the regression tests, run "make [--quiet] regtest".
54 To run a subset of the regression tests, execute:
56 perl tests/vg_regtest <name>
58 where <name> is a directory (all tests within will be run) or a single
59 .vgtest test file, or the name of a program which has a like-named .vgtest
62 perl tests/vg_regtest memcheck
63 perl tests/vg_regtest memcheck/tests/badfree.vgtest
64 perl tests/vg_regtest memcheck/tests/badfree
66 The details of each vgtest run are logged to individual "vgtest.log"
67 files. These are listed, and non-passing tests detailed, in the
68 test-suite-overall.log file. (Token *.trs and test-suite.log files
69 are also created, for emulating automake-style testsuites, as expected
70 by tools such as bunsen.)
73 Running the performance tests
74 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
75 To build and run all the performance tests, run "make [--quiet] perf".
77 To run a subset of the performance suite, execute:
79 perl perf/vg_perf <name>
81 where <name> is a directory (all tests within will be run) or a single
82 .vgperf test file, or the name of a program which has a like-named .vgperf
85 perl perf/vg_perf perf/
86 perl perf/vg_perf perf/bz2.vgperf
87 perl perf/vg_perf perf/bz2
89 To compare multiple versions of Valgrind, use the --vg= option multiple
90 times. For example, if you have two Valgrinds next to each other, one in
91 trunk1/ and one in trunk2/, from within either trunk1/ or trunk2/ do this to
92 compare them on all the performance tests:
94 perl perf/vg_perf --vg=../trunk1 --vg=../trunk2 perf/
96 Writing regression tests
97 ~~~~~~~~~~~~~~~~~~~~~~~~
99 Each tool has a tests directory containing regression tests. There is also
100 the gdbserver_tests directory at the top level. Test directories may have
101 architecture, OS and and architecture-OS sub-directories for tests that are
102 specific to one architecture, OS or both.
104 Once you have written a C or C++ executable that performs the required
105 tests you will have to modify and create several files. The new files that
107 (a) a .vgtest file, which controls the test
108 (b) a .stderr.exp file, which is the golden reference for any Valgrind output
109 Note that even if Valgrind doesn't produce any output you will need to
110 create an empty .stderr.exp file.
111 (c) [optional] a .stdout.exp file, the golden reference for any output from the
113 (d) [optional] filter files (see the test directories for examples).
115 Quite often the output will depend on the platform the test is run on.
116 Getting the tests to be 'portable' can require filtering or adding multiple
117 .stderr.exp reference files.
119 If the test only runs under certain conditions like the availability of functions
120 in libc or C++ standard versions you will need to modify configure.ac in the
121 top level directory. See AC_CHECK_FUNCS and the various blocks starting with
124 In the test directory, modify Makefile.am. Add to EXTRA_DIST the .vgtest,
125 .stderr.exp and .stderr.out files. Add any filters to dist_noinst_SCRIPTS.
126 Add the test executable name to check_PROGRAMS. Try to respect the
127 formatting and alphabetical ordering of the Makefile.am. For simple C files that is
128 sufficient. If you needed to add a feature test to configure.ac then you should
129 use the same condition to add the executable name to check_PROGRAMS. If the
130 executable uses C++ you need to add exename_SOURCES. If the executable needs
131 special compilation or link options, use exename_CFLAGS, exename_CXXFLAGS,
132 exename_LDFLAGS or exename_LDADD. Finally in Makefile.am it's nice not to
133 have any warnings, even if they were done on purpose. See configure.ac
134 and various Makefile.am files for examples of using FLAG_W_*.
136 The vgtest file contains the instructions for running the test. Typically
137 it will contain (with examples in quotes)
138 (a) the name of the test executable: "prog: exename"
139 (b) arguments for the test executable: "args: hello world"
140 (c) arguments for the Valgrind tool: "vgopts: -q"
141 (d) [optional] a check for prerequisites: "prereq: ! ../../tests/os_test darwin"
142 (e) [optional] a filter: "stderr_filter: filter_fdleak"
144 See tests/vg_regtest for a full description of all possible vgtest directives.
146 The easiest way to generate the expected files is to run the test. Create empty
147 files with touch (otherwise the test won't run) then run the test from the
148 top directory using perl and vg_regtest script (as in the "Running the
149 regression tests" section. Then copy "tool/tests/newtest.stderr.out" to
150 "tool/tests/newtest.stderr.exp". It is better to generate the .stdout.exp
151 file directly from the testcase. You can do that by redirecting stdout to
152 the expected file, for instance (in the test directory)
153 ./newtest arg1 arg2 > newtest.stdout.exp
154 This is not always possible - sometimes there are tests that depend on the use
155 of client requests or have imperfect emulation of opcodes.
156 Make sure that the test runs and passes.
158 The last file to change is .gitignore in the top directory. Add a new entry,
159 for example "/tool/tests/newtest".
161 Check for mistakes in Makefile.am. In the top directory run
162 make post-regtest-checks
165 ...checking makefile consistency
166 ...checking header files and include directives
167 and no messages related to EXTRA_DIST.
169 Commit access and try branches
170 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
171 To get commit access to the valgrind git repository on sourceware
172 you will have to ask an existing developer and fill in the following
173 form: https://sourceware.org/cgi-bin/pdw/ps_form.cgi
175 Every developer with commit access can use try branches. If you want to try a
176 branch before pushing you can push to a special named try branch as follows:
178 git push origin $BRANCH:users/$USERNAME/try-$BRANCH
180 Where $BRANCH is the branch name and $USERNAME is your user name.
182 You can see the status of the builders here:
183 https://builder.sourceware.org/buildbot/#/builders?tags=valgrind-try
185 The buildbot will also sent the patch author multiple success/failure emails.
187 Afterwards you can delete the branch again:
189 git push origin :users/$USERNAME/try-$BRANCH
192 Debugging Valgrind with GDB
193 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
194 To debug the valgrind launcher program (<prefix>/bin/valgrind) just
195 run it under gdb in the normal way.
197 Debugging the main body of the valgrind code (and/or the code for
198 a particular tool) requires a bit more trickery but can be achieved
199 without too much problem by following these steps:
201 (1) Set VALGRIND_LAUNCHER to point to the valgrind executable. Eg:
203 export VALGRIND_LAUNCHER=/usr/local/bin/valgrind
205 or for an uninstalled version in a source directory $DIR:
207 export VALGRIND_LAUNCHER=$DIR/coregrind/valgrind
208 export VALGRIND_LIB=$DIR/.in_place
210 VALGRIND_LIB is where the default.supp and vgpreload_ libraries
211 are found (which is under /usr/libexec/valgrind for an installed
214 (2) Run gdb on the tool executable. Eg:
216 gdb /usr/local/lib/valgrind/lackey-ppc32-linux
220 gdb $DIR/.in_place/memcheck-x86-linux
222 (3) Do "handle SIGSEGV SIGILL nostop noprint" in GDB to prevent GDB from
223 stopping on a SIGSEGV or SIGILL:
225 (gdb) handle SIGILL SIGSEGV nostop noprint
227 If you are using lldb, then the equivalent command is
229 (lldb) pro hand -p true -s false -n false SIGILL SIGSEGV
231 (4) Set any breakpoints you want and proceed as normal for gdb. The
232 macro VG_(FUNC) is expanded to vgPlain_FUNC, so If you want to set
233 a breakpoint VG_(do_exec_inner), you could do like this in GDB:
235 (gdb) b vgPlain_do_exec_inner
237 Note: This is just an example, for various reasons internal
238 function names might be renamed or optimized out
239 (for example when building with --enable-lto).
241 (5) Run the tool with required options (the --tool option is required
242 for correct setup), e.g.
244 (gdb) run --tool=lackey pwd
246 Steps (1)--(3) can be put in a .gdbinit file, but any directory names must
247 be fully expanded (ie. not an environment variable).
249 A different and possibly easier way is as follows:
251 (1) Run Valgrind as normal, but add the flag --wait-for-gdb=yes. This
252 puts the tool executable into a wait loop soon after it gains
253 control. This delays startup for a few seconds.
255 (2) In a different shell, do "gdb /proc/<pid>/exe <pid>", where
256 <pid> you read from the output printed by (1). This attaches
257 GDB to the tool executable, which should be in the above mentioned
260 (3) Do "cont" to continue. After the loop finishes spinning, startup
261 will continue as normal. Note that comment (3) above re passing
262 signals applies here too.
264 The default build of Valgrind uses "-g -O2". This is OK most of the
265 time, but with sophisticated optimization it can be difficult to
266 see the contents of variables. A quick way to get to see function
267 variables is to temporarily add "__attribute__((optnone))" before
268 the function definition and rebuild. Alternatively modify
269 Makefile.all.am and remove -O2 from AM_CFLAGS_BASE. That will
270 require you to reconfigure and rebuild Valgrind.
274 This section explains:
275 (A) How to configure Valgrind to run under Valgrind.
276 Such a setup is called self hosting, or outer/inner setup.
277 (B) How to run Valgrind regression tests in a 'self-hosting' mode,
278 e.g. to verify Valgrind has no bugs such as memory leaks.
279 (C) How to run Valgrind performance tests in a 'self-hosting' mode,
280 to analyse and optimise the performance of Valgrind and its tools.
282 (A) How to configure Valgrind to run under Valgrind:
284 (1) Check out 2 trees, "Inner" and "Outer". Inner runs the app
285 directly. Outer runs Inner.
287 (2) Configure Inner with --enable-inner and build as usual.
289 (3) Configure Outer normally and build+install as usual.
290 Note: You must use a "make install"-ed valgrind.
291 Do *not* use vg-in-place for the Outer valgrind.
293 (4) Choose a very simple program (date) and try
295 outer/.../bin/valgrind --sim-hints=enable-outer --trace-children=yes \
296 --smc-check=all-non-file \
297 --run-libc-freeres=no --tool=cachegrind -v \
298 inner/.../vg-in-place --vgdb-prefix=./inner --tool=none -v prog
300 If you omit the --trace-children=yes, you'll only monitor Inner's launcher
301 program, not its stage2. Outer needs --run-libc-freeres=no, as otherwise
302 it will try to find and run __libc_freeres in the inner, while libc is not
303 used by the inner. Inner needs --vgdb-prefix=./inner to avoid inner
304 gdbserver colliding with outer gdbserver.
305 Currently, inner does *not* use the client request
306 VALGRIND_DISCARD_TRANSLATIONS for the JITted code or the code patched for
307 translation chaining. So the outer needs --smc-check=all-non-file to
308 detect the modified code.
310 Debugging the whole thing might imply to use up to 3 GDB:
311 * a GDB attached to the Outer valgrind, allowing
312 to examine the state of Outer.
313 * a GDB using Outer gdbserver, allowing to
314 examine the state of Inner.
315 * a GDB using Inner gdbserver, allowing to
316 examine the state of prog.
318 The whole thing is fragile, confusing and slow, but it does work well enough
319 for you to get some useful performance data. Inner has most of
320 its output (ie. those lines beginning with "==<pid>==") prefixed with a '>',
321 which helps a lot. However, when running regression tests in an Outer/Inner
322 setup, this prefix causes the reg test diff to fail. Give
323 --sim-hints=no-inner-prefix to the Inner to disable the production
324 of the prefix in the stdout/stderr output of Inner.
326 The allocators in coregrind/m_mallocfree.c and VEX/priv/main_util.h are
327 annotated with client requests so Memcheck can be used to find leaks
328 and use after free in an Inner Valgrind.
330 The Valgrind "big lock" is annotated with helgrind client requests
331 so Helgrind and DRD can be used to find race conditions in an Inner
334 All this has not been tested much, so don't be surprised if you hit problems.
336 When using self-hosting with an outer Callgrind tool, use '--pop-on-jump'
337 (on the outer). Otherwise, Callgrind has much higher memory requirements.
339 (B) Regression tests in an outer/inner setup:
341 To run all the regression tests with an outer memcheck, do :
342 perl tests/vg_regtest --outer-valgrind=../outer/.../bin/valgrind \
345 To run a specific regression tests with an outer memcheck, do:
346 perl tests/vg_regtest --outer-valgrind=../outer/.../bin/valgrind \
347 none/tests/args.vgtest
349 To run regression tests with another outer tool:
350 perl tests/vg_regtest --outer-valgrind=../outer/.../bin/valgrind \
351 --outer-tool=helgrind --all
353 --outer-args allows to give specific arguments to the outer tool,
354 replacing the default one provided by vg_regtest.
356 Note: --outer-valgrind must be a "make install"-ed valgrind.
357 Do *not* use vg-in-place.
359 When an outer valgrind runs an inner valgrind, a regression test
360 produces one additional file <testname>.outer.log which contains the
361 errors detected by the outer valgrind. E.g. for an outer memcheck, it
362 contains the leaks found in the inner, for an outer helgrind or drd,
363 it contains the detected race conditions.
365 The file tests/outer_inner.supp contains suppressions for
366 the irrelevant or benign errors found in the inner.
368 A regression test running in the inner (e.g. memcheck/tests/badrw) will
369 cause the inner to report an error, which is expected and checked
370 as usual when running the regtests in an outer/inner setup.
371 However, the outer will often also observe an error, e.g. a jump
372 using uninitialised data, or a read/write outside the bounds of a heap
373 block. When the outer reports such an error, it will output the
374 inner host stacktrace. To this stacktrace, it will append the
375 stacktrace of the inner guest program. For example, this is an error
376 reported by the outer when the inner runs the badrw regtest:
377 ==8119== Invalid read of size 2
378 ==8119== at 0x7F2EFD7AF: ???
379 ==8119== by 0x7F2C82EAF: ???
380 ==8119== by 0x7F180867F: ???
381 ==8119== by 0x40051D: main (badrw.c:5)
382 ==8119== by 0x7F180867F: ???
383 ==8119== by 0x1BFF: ???
384 ==8119== by 0x3803B7F0: _______VVVVVVVV_appended_inner_guest_stack_VVVVVVVV_______ (m_execontext.c:332)
385 ==8119== by 0x40055C: main (badrw.c:22)
386 ==8119== Address 0x55cd03c is 4 bytes before a block of size 16 alloc'd
387 ==8119== at 0x2804E26D: vgPlain_arena_malloc (m_mallocfree.c:1914)
388 ==8119== by 0x2800BAB4: vgMemCheck_new_block (mc_malloc_wrappers.c:368)
389 ==8119== by 0x2800BC87: vgMemCheck_malloc (mc_malloc_wrappers.c:403)
390 ==8119== by 0x28097EAE: do_client_request (scheduler.c:1861)
391 ==8119== by 0x28097EAE: vgPlain_scheduler (scheduler.c:1425)
392 ==8119== by 0x280A7237: thread_wrapper (syswrap-linux.c:103)
393 ==8119== by 0x280A7237: run_a_thread_NORETURN (syswrap-linux.c:156)
394 ==8119== by 0x3803B7F0: _______VVVVVVVV_appended_inner_guest_stack_VVVVVVVV_______ (m_execontext.c:332)
395 ==8119== by 0x4C294C4: malloc (vg_replace_malloc.c:298)
396 ==8119== by 0x40051D: main (badrw.c:5)
397 In the above, the first stacktrace starts with the inner host stacktrace,
398 which in this case is some JITted code. Such code sometimes contains IPs
399 that points in the inner guest code (0x40051D: main (badrw.c:5)).
400 After the separator, we have the inner guest stacktrace.
401 The second stacktrace gives the stacktrace where the heap block that was
402 overrun was allocated. We see it was allocated by the inner valgrind
403 in the client arena (first part of the stacktrace). The second part is
404 the guest stacktrace that did the allocation.
407 (C) Performance tests in an outer/inner setup:
409 To run all the performance tests with an outer cachegrind, do :
410 perl perf/vg_perf --outer-valgrind=../outer/.../bin/valgrind perf
412 To run a specific perf test (e.g. bz2) in this setup, do :
413 perl perf/vg_perf --outer-valgrind=../outer/.../bin/valgrind perf/bz2
415 To run all the performance tests with an outer callgrind, do :
416 perl perf/vg_perf --outer-valgrind=../outer/.../bin/valgrind \
417 --outer-tool=callgrind perf
419 Note: --outer-valgrind must be a "make install"-ed valgrind.
420 Do *not* use vg-in-place.
422 To compare the performance of multiple Valgrind versions, do :
423 perl perf/vg_perf --outer-valgrind=../outer/.../bin/valgrind \
424 --outer-tool=callgrind \
425 --vg=../inner_xxxx --vg=../inner_yyyy perf
426 (where inner_xxxx and inner_yyyy are the toplevel directories of
427 the versions to compare).
428 Cachegrind and cg_diff are particularly handy to obtain a delta
429 between the two versions.
431 When the outer tool is callgrind or cachegrind, the following
432 output files will be created for each test:
433 <outertoolname>.out.<inner_valgrind_dir>.<tt>.<perftestname>.<pid>
434 <outertoolname>.outer.log.<inner_valgrind_dir>.<tt>.<perftestname>.<pid>
435 (where tt is the two letters abbreviation for the inner tool(s) run).
437 For example, the command
439 --outer-valgrind=../outer_trunk/install/bin/valgrind \
440 --outer-tool=callgrind \
441 --vg=../inner_tchain --vg=../inner_trunk perf/many-loss-records
444 callgrind.out.inner_tchain.no.many-loss-records.18465
445 callgrind.outer.log.inner_tchain.no.many-loss-records.18465
446 callgrind.out.inner_tchain.me.many-loss-records.21899
447 callgrind.outer.log.inner_tchain.me.many-loss-records.21899
448 callgrind.out.inner_trunk.no.many-loss-records.21224
449 callgrind.outer.log.inner_trunk.no.many-loss-records.21224
450 callgrind.out.inner_trunk.me.many-loss-records.22916
451 callgrind.outer.log.inner_trunk.me.many-loss-records.22916
454 Printing out problematic blocks
455 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
456 If you want to print out a disassembly of a particular block that
457 causes a crash, do the following.
459 Try running with "--vex-guest-chase=no --trace-flags=10000000
460 --trace-notbelow=999999". This should print one line for each block
461 translated, and that includes the address.
463 Then re-run with 999999 changed to the highest bb number shown.
464 This will print the one line per block, and also will print a
465 disassembly of the block in which the fault occurred.
468 Formatting the code with clang-format
469 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
470 clang-format is a tool to format C/C++/... code. The root directory of the
471 Valgrind tree contains file .clang-format which is a configuration for this tool
472 and specifies a style for Valgrind. This gives you an option to use
473 clang-format to easily format Valgrind code which you are modifying.
475 The Valgrind codebase is not globally formatted with clang-format. It means
476 that you should not use the tool to format a complete file after making changes
477 in it because that would lead to creating unrelated modifications.
479 The right approach is to format only updated or new code. By using an
480 integration with a text editor, it is possible to reformat arbitrary blocks
481 of code with a single keystroke. Refer to the upstream documentation which
482 describes integration with various editors and IDEs:
483 https://clang.llvm.org/docs/ClangFormat.html.