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