2015-10-09 15:38:59 +02:00
|
|
|
Useful tools
|
2015-11-05 15:13:53 +01:00
|
|
|
============
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
These aren't strictly necessary for hacking on Tor, but they can help track
|
|
|
|
down bugs.
|
|
|
|
|
|
|
|
Jenkins
|
2015-11-05 15:13:53 +01:00
|
|
|
-------
|
2015-10-09 15:38:59 +02:00
|
|
|
|
2015-11-05 15:13:53 +01:00
|
|
|
https://jenkins.torproject.org
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
Dmalloc
|
2015-11-05 15:13:53 +01:00
|
|
|
-------
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
The dmalloc library will keep track of memory allocation, so you can find out
|
|
|
|
if we're leaking memory, doing any double-frees, or so on.
|
|
|
|
|
2015-11-05 15:13:53 +01:00
|
|
|
dmalloc -l -/dmalloc.log
|
|
|
|
(run the commands it tells you)
|
|
|
|
./configure --with-dmalloc
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
Valgrind
|
2015-11-05 15:13:53 +01:00
|
|
|
--------
|
2015-10-09 15:38:59 +02:00
|
|
|
|
2015-11-05 15:13:53 +01:00
|
|
|
valgrind --leak-check=yes --error-limit=no --show-reachable=yes src/or/tor
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
(Note that if you get a zillion openssl warnings, you will also need to
|
2015-11-05 15:13:53 +01:00
|
|
|
pass `--undef-value-errors=no` to valgrind, or rebuild your openssl
|
|
|
|
with `-DPURIFY`.)
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
Coverity
|
2015-11-05 15:13:53 +01:00
|
|
|
--------
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
Nick regularly runs the coverity static analyzer on the Tor codebase.
|
|
|
|
|
2015-11-05 15:13:53 +01:00
|
|
|
The preprocessor define `__COVERITY__` is used to work around instances
|
2015-10-09 15:38:59 +02:00
|
|
|
where coverity picks up behavior that we wish to permit.
|
|
|
|
|
|
|
|
clang Static Analyzer
|
2015-11-05 15:13:53 +01:00
|
|
|
---------------------
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
The clang static analyzer can be run on the Tor codebase using Xcode (WIP)
|
|
|
|
or a command-line build.
|
|
|
|
|
2015-11-05 15:13:53 +01:00
|
|
|
The preprocessor define `__clang_analyzer__` is used to work around instances
|
2015-10-09 15:38:59 +02:00
|
|
|
where clang picks up behavior that we wish to permit.
|
|
|
|
|
|
|
|
clang Runtime Sanitizers
|
2015-11-05 15:13:53 +01:00
|
|
|
------------------------
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
To build the Tor codebase with the clang Address and Undefined Behavior
|
2015-11-05 15:13:53 +01:00
|
|
|
sanitizers, see the file `contrib/clang/sanitize_blacklist.txt`.
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
Preprocessor workarounds for instances where clang picks up behavior that
|
|
|
|
we wish to permit are also documented in the blacklist file.
|
|
|
|
|
|
|
|
Running lcov for unit test coverage
|
2015-11-05 15:13:53 +01:00
|
|
|
-----------------------------------
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
Lcov is a utility that generates pretty HTML reports of test code coverage.
|
|
|
|
To generate such a report:
|
|
|
|
|
2015-11-05 15:13:53 +01:00
|
|
|
./configure --enable-coverage
|
|
|
|
make
|
|
|
|
make coverage-html
|
|
|
|
$BROWSER ./coverage_html/index.html
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
This will run the tor unit test suite `./src/test/test` and generate the HTML
|
2015-11-05 15:13:53 +01:00
|
|
|
coverage code report under the directory `./coverage_html/`. To change the
|
2015-10-09 15:38:59 +02:00
|
|
|
output directory, use `make coverage-html HTML_COVER_DIR=./funky_new_cov_dir`.
|
|
|
|
|
|
|
|
Coverage diffs using lcov are not currently implemented, but are being
|
|
|
|
investigated (as of July 2014).
|
|
|
|
|
|
|
|
Running the unit tests
|
2015-11-05 15:13:53 +01:00
|
|
|
----------------------
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
To quickly run all the tests distributed with Tor:
|
2015-11-05 15:13:53 +01:00
|
|
|
|
|
|
|
make check
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
To run the fast unit tests only:
|
2015-11-05 15:13:53 +01:00
|
|
|
|
|
|
|
make test
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
To selectively run just some tests (the following can be combined
|
|
|
|
arbitrarily):
|
2015-11-05 15:13:53 +01:00
|
|
|
|
|
|
|
./src/test/test <name_of_test> [<name of test 2>] ...
|
|
|
|
./src/test/test <prefix_of_name_of_test>.. [<prefix_of_name_of_test2>..] ...
|
|
|
|
./src/test/test :<name_of_excluded_test> [:<name_of_excluded_test2]...
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
To run all tests, including those based on Stem or Chutney:
|
2015-11-05 15:13:53 +01:00
|
|
|
|
|
|
|
make test-full
|
2015-10-09 15:38:59 +02:00
|
|
|
|
2015-10-29 14:28:17 +01:00
|
|
|
To run all tests, including those based on Stem or Chutney that require a
|
2015-10-09 15:38:59 +02:00
|
|
|
working connection to the internet:
|
|
|
|
|
2015-11-05 15:13:53 +01:00
|
|
|
make test-full-online
|
2015-10-09 15:38:59 +02:00
|
|
|
|
2015-11-05 15:13:53 +01:00
|
|
|
Running gcov for unit test coverage
|
|
|
|
-----------------------------------
|
2015-11-05 15:53:05 +01:00
|
|
|
|
2015-11-05 15:13:53 +01:00
|
|
|
./configure --enable-coverage
|
|
|
|
make
|
|
|
|
make check
|
|
|
|
# or--- make test-full ? make test-full-online?
|
|
|
|
mkdir coverage-output
|
|
|
|
./scripts/test/coverage coverage-output
|
|
|
|
|
|
|
|
(On OSX, you'll need to start with `--enable-coverage CC=clang`.)
|
|
|
|
|
|
|
|
Then, look at the .gcov files in `coverage-output`. '-' before a line means
|
2015-10-09 15:38:59 +02:00
|
|
|
that the compiler generated no code for that line. '######' means that the
|
|
|
|
line was never reached. Lines with numbers were called that number of times.
|
|
|
|
|
|
|
|
If that doesn't work:
|
2015-11-05 15:13:53 +01:00
|
|
|
|
|
|
|
* Try configuring Tor with `--disable-gcc-hardening`
|
|
|
|
* You might need to run `make clean` after you run `./configure`.
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
If you make changes to Tor and want to get another set of coverage results,
|
2015-11-05 15:13:53 +01:00
|
|
|
you can run `make reset-gcov` to clear the intermediary gcov output.
|
2015-10-09 15:38:59 +02:00
|
|
|
|
2015-11-05 15:13:53 +01:00
|
|
|
If you have two different `coverage-output` directories, and you want to see
|
2015-10-09 15:38:59 +02:00
|
|
|
a meaningful diff between them, you can run:
|
|
|
|
|
2015-11-05 15:13:53 +01:00
|
|
|
./scripts/test/cov-diff coverage-output1 coverage-output2 | less
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
In this diff, any lines that were visited at least once will have coverage
|
|
|
|
"1". This lets you inspect what you (probably) really want to know: which
|
|
|
|
untested lines were changed? Are there any new untested lines?
|
|
|
|
|
|
|
|
Running integration tests
|
2015-11-05 15:13:53 +01:00
|
|
|
-------------------------
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
We have the beginnings of a set of scripts to run integration tests using
|
|
|
|
Chutney. To try them, set CHUTNEY_PATH to your chutney source directory, and
|
2015-11-05 15:13:53 +01:00
|
|
|
run `make test-network`.
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
We also have scripts to run integration tests using Stem. To try them, set
|
2015-11-05 15:13:53 +01:00
|
|
|
`STEM_SOURCE_DIR` to your Stem source directory, and run `test-stem`.
|
2015-10-09 15:38:59 +02:00
|
|
|
|
2017-01-13 17:01:49 +01:00
|
|
|
Profiling Tor
|
|
|
|
-------------
|
|
|
|
|
|
|
|
Ongoing notes about Tor profiling can be found at
|
|
|
|
https://pad.riseup.net/p/profiling-tor
|
|
|
|
|
2015-10-09 15:38:59 +02:00
|
|
|
Profiling Tor with oprofile
|
2015-11-05 15:13:53 +01:00
|
|
|
---------------------------
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
The oprofile tool runs (on Linux only!) to tell you what functions Tor is
|
2015-10-29 15:29:21 +01:00
|
|
|
spending its CPU time in, so we can identify performance bottlenecks.
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
Here are some basic instructions
|
|
|
|
|
|
|
|
- Build tor with debugging symbols (you probably already have, unless
|
|
|
|
you messed with CFLAGS during the build process).
|
|
|
|
- Build all the libraries you care about with debugging symbols
|
|
|
|
(probably you only care about libssl, maybe zlib and Libevent).
|
|
|
|
- Copy this tor to a new directory
|
2015-11-05 15:13:53 +01:00
|
|
|
- Copy all the libraries it uses to that dir too (`ldd ./tor` will
|
2015-10-09 15:38:59 +02:00
|
|
|
tell you)
|
2015-11-05 15:13:53 +01:00
|
|
|
- Set LD_LIBRARY_PATH to include that dir. `ldd ./tor` should now
|
2015-10-09 15:38:59 +02:00
|
|
|
show you it's using the libs in that dir
|
|
|
|
- Run that tor
|
|
|
|
- Reset oprofiles counters/start it
|
2015-11-05 15:13:53 +01:00
|
|
|
* `opcontrol --reset; opcontrol --start`, if Nick remembers right.
|
2015-10-09 15:38:59 +02:00
|
|
|
- After a while, have it dump the stats on tor and all the libs
|
|
|
|
in that dir you created.
|
2015-11-05 15:13:53 +01:00
|
|
|
* `opcontrol --dump;`
|
|
|
|
* `opreport -l that_dir/*`
|
2015-10-09 15:38:59 +02:00
|
|
|
- Profit
|
2017-01-13 18:31:06 +01:00
|
|
|
|
2017-01-13 17:01:49 +01:00
|
|
|
Profiling Tor with perf
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
This works with a running Tor, and requires root.
|
2017-01-13 18:31:06 +01:00
|
|
|
|
2017-01-13 17:01:49 +01:00
|
|
|
1. Decide how long you want to profile for. Start with (say) 30 seconds. If that
|
|
|
|
works, try again with longer times.
|
|
|
|
|
|
|
|
2. Find the PID of your running tor process.
|
|
|
|
|
|
|
|
3. Run `perf record --call-graph dwarf -p <PID> sleep <SECONDS>`
|
2017-01-13 18:31:06 +01:00
|
|
|
|
2017-01-13 17:01:49 +01:00
|
|
|
(You may need to do this as root.)
|
2017-01-13 18:31:06 +01:00
|
|
|
|
2017-01-13 17:01:49 +01:00
|
|
|
You might need to add `-e cpu-clock` as an option to the perf record line
|
|
|
|
above, if you are on an older CPU without access to hardware profiling
|
|
|
|
events, or in a VM, or something.
|
2017-01-13 18:31:06 +01:00
|
|
|
|
2017-01-13 17:01:49 +01:00
|
|
|
4. Now you have a perf.data file. Have a look at it with `perf report
|
|
|
|
--no-children --sort symbol,dso` or `perf report --no-children --sort
|
|
|
|
symbol,dso --stdio --header`. How does it look?
|
|
|
|
|
|
|
|
5a. Once you have a nice big perf.data file, you can compress it, encrypt it,
|
|
|
|
and send it to your favorite Tor developers.
|
|
|
|
|
|
|
|
5b. Or maybe you'd rather not send a nice big perf.data file. Who knows what's
|
|
|
|
in that!? It's kinda scary. To generate a less scary file, you can use `perf
|
|
|
|
report -g > <FILENAME>.out`. Then you can compress that and put it somewhere
|
|
|
|
public.
|
|
|
|
|
|
|
|
Profiling Tor with gperftools aka Google-performance-tools
|
|
|
|
----------------------------------------------------------
|
|
|
|
|
|
|
|
This should work on nearly any unixy system. It doesn't seem to be compatible
|
|
|
|
with RunAsDaemon though.
|
|
|
|
|
|
|
|
Beforehand, install google-perftools.
|
|
|
|
|
|
|
|
1. You need to rebuild Tor, hack the linking steps to add `-lprofiler` to the
|
|
|
|
libs. You can do this by adding `LIBS=-lprofiler` when you call `./configure`.
|
|
|
|
|
|
|
|
Now you can run Tor with profiling enabled, and use the pprof utility to look at
|
|
|
|
performance! See the gperftools manual for more info, but basically:
|
|
|
|
|
|
|
|
2. Run `env CPUPROFILE=/tmp/profile src/or/tor -f <path/torrc>`. The profile file
|
|
|
|
is not written to until Tor finishes execuction.
|
2017-01-13 18:31:06 +01:00
|
|
|
|
2017-01-13 17:01:49 +01:00
|
|
|
3. Run `pprof src/or/tor /tm/profile` to start the REPL.
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
Generating and analyzing a callgraph
|
2015-11-05 15:13:53 +01:00
|
|
|
------------------------------------
|
2015-10-09 15:38:59 +02:00
|
|
|
|
2017-06-14 23:44:15 +02:00
|
|
|
0. Build Tor on linux or mac, ideally with -O0 or -fno-inline.
|
2015-10-09 15:38:59 +02:00
|
|
|
|
2017-06-14 23:44:15 +02:00
|
|
|
1. Clone 'https://gitweb.torproject.org/user/nickm/calltool.git/' .
|
|
|
|
Follow the README in that repository.
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
Note that currently the callgraph generator can't detect calls that pass
|
|
|
|
through function pointers.
|
|
|
|
|
|
|
|
Getting emacs to edit Tor source properly
|
2015-11-05 15:13:53 +01:00
|
|
|
-----------------------------------------
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
Nick likes to put the following snippet in his .emacs file:
|
|
|
|
|
2015-11-05 15:13:53 +01:00
|
|
|
|
2015-10-09 15:38:59 +02:00
|
|
|
(add-hook 'c-mode-hook
|
|
|
|
(lambda ()
|
|
|
|
(font-lock-mode 1)
|
|
|
|
(set-variable 'show-trailing-whitespace t)
|
|
|
|
|
|
|
|
(let ((fname (expand-file-name (buffer-file-name))))
|
|
|
|
(cond
|
|
|
|
((string-match "^/home/nickm/src/libevent" fname)
|
|
|
|
(set-variable 'indent-tabs-mode t)
|
|
|
|
(set-variable 'c-basic-offset 4)
|
|
|
|
(set-variable 'tab-width 4))
|
|
|
|
((string-match "^/home/nickm/src/tor" fname)
|
|
|
|
(set-variable 'indent-tabs-mode nil)
|
|
|
|
(set-variable 'c-basic-offset 2))
|
|
|
|
((string-match "^/home/nickm/src/openssl" fname)
|
|
|
|
(set-variable 'indent-tabs-mode t)
|
|
|
|
(set-variable 'c-basic-offset 8)
|
|
|
|
(set-variable 'tab-width 8))
|
|
|
|
))))
|
|
|
|
|
2015-11-05 15:13:53 +01:00
|
|
|
|
|
|
|
You'll note that it defaults to showing all trailing whitespace. The `cond`
|
2015-10-09 15:38:59 +02:00
|
|
|
test detects whether the file is one of a few C free software projects that I
|
|
|
|
often edit, and sets up the indentation level and tab preferences to match
|
|
|
|
what they want.
|
|
|
|
|
|
|
|
If you want to try this out, you'll need to change the filename regex
|
|
|
|
patterns to match where you keep your Tor files.
|
|
|
|
|
|
|
|
If you use emacs for editing Tor and nothing else, you could always just say:
|
|
|
|
|
2015-11-05 15:13:53 +01:00
|
|
|
|
|
|
|
(add-hook 'c-mode-hook
|
|
|
|
(lambda ()
|
2015-10-09 15:38:59 +02:00
|
|
|
(font-lock-mode 1)
|
|
|
|
(set-variable 'show-trailing-whitespace t)
|
|
|
|
(set-variable 'indent-tabs-mode nil)
|
|
|
|
(set-variable 'c-basic-offset 2)))
|
2015-11-05 15:13:53 +01:00
|
|
|
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
There is probably a better way to do this. No, we are probably not going
|
|
|
|
to clutter the files with emacs stuff.
|
|
|
|
|
|
|
|
|
|
|
|
Doxygen
|
2015-11-05 15:13:53 +01:00
|
|
|
-------
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
We use the 'doxygen' utility to generate documentation from our
|
|
|
|
source code. Here's how to use it:
|
|
|
|
|
|
|
|
1. Begin every file that should be documented with
|
2015-11-05 15:53:05 +01:00
|
|
|
|
2015-10-09 15:38:59 +02:00
|
|
|
/**
|
|
|
|
* \file filename.c
|
|
|
|
* \brief Short description of the file.
|
2015-10-29 14:28:17 +01:00
|
|
|
*/
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
(Doxygen will recognize any comment beginning with /** as special.)
|
|
|
|
|
|
|
|
2. Before any function, structure, #define, or variable you want to
|
|
|
|
document, add a comment of the form:
|
|
|
|
|
|
|
|
/** Describe the function's actions in imperative sentences.
|
|
|
|
*
|
|
|
|
* Use blank lines for paragraph breaks
|
|
|
|
* - and
|
|
|
|
* - hyphens
|
|
|
|
* - for
|
|
|
|
* - lists.
|
|
|
|
*
|
|
|
|
* Write <b>argument_names</b> in boldface.
|
|
|
|
*
|
|
|
|
* \code
|
|
|
|
* place_example_code();
|
|
|
|
* between_code_and_endcode_commands();
|
|
|
|
* \endcode
|
|
|
|
*/
|
|
|
|
|
2015-11-05 15:13:53 +01:00
|
|
|
3. Make sure to escape the characters `<`, `>`, `\`, `%` and `#` as `\<`,
|
|
|
|
`\>`, `\\`, `\%` and `\#`.
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
4. To document structure members, you can use two forms:
|
|
|
|
|
2015-11-05 15:13:53 +01:00
|
|
|
struct foo {
|
|
|
|
/** You can put the comment before an element; */
|
|
|
|
int a;
|
|
|
|
int b; /**< Or use the less-than symbol to put the comment
|
2015-10-09 15:38:59 +02:00
|
|
|
* after the element. */
|
2015-11-05 15:13:53 +01:00
|
|
|
};
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
5. To generate documentation from the Tor source code, type:
|
|
|
|
|
2015-11-05 15:13:53 +01:00
|
|
|
$ doxygen -g
|
2015-10-09 15:38:59 +02:00
|
|
|
|
2015-11-05 15:13:53 +01:00
|
|
|
to generate a file called `Doxyfile`. Edit that file and run
|
|
|
|
`doxygen` to generate the API documentation.
|
2015-10-09 15:38:59 +02:00
|
|
|
|
|
|
|
6. See the Doxygen manual for more information; this summary just
|
|
|
|
scratches the surface.
|