fix typos from #28614
[tor.git] / doc / HACKING / HelpfulTools.md
blobd49923852681c28a9067c2f6b037d5569c104f33
1 Useful tools
2 ============
4 These aren't strictly necessary for hacking on Tor, but they can help track
5 down bugs.
7 Travis/Appveyor CI
8 ------------------
9 It's CI.
11 Looks like this:
12 * https://travis-ci.org/torproject/tor
13 * https://ci.appveyor.com/project/torproject/tor
15 Travis builds and runs tests on Linux, and eventually macOS (#24629).
16 Appveyor builds and runs tests on Windows (using Windows Services for Linux).
18 Runs automatically on Pull Requests sent to torproject/tor. You can set it up
19 for your fork to build commits outside of PRs too:
21 1. sign up for GitHub: https://github.com/join
22 2. fork https://github.com/torproject/tor:
23    https://help.github.com/articles/fork-a-repo/
24 3. follow https://docs.travis-ci.com/user/getting-started/#To-get-started-with-Travis-CI.
25    skip steps involving `.travis.yml` (we already have one).
26 4. go to https://ci.appveyor.com/login , log in with github, and select
27    "NEW PROJECT"
29 Builds should show up on the web at travis-ci.com and on IRC at #tor-ci on
30 OFTC. If they don't, ask #tor-dev (also on OFTC).
32 Jenkins
33 -------
35 It's CI/builders. Looks like this: https://jenkins.torproject.org
37 Runs automatically on commits merged to git.torproject.org. We CI the
38 master branch and all supported tor versions. We also build nightly debian
39 packages from master.
41 Builds Linux and Windows cross-compilation. Runs Linux tests.
43 Builds should show up on the web at jenkins.torproject.org and on IRC at
44 #tor-bots on OFTC. If they don't, ask #tor-dev (also on OFTC).
46 Valgrind
47 --------
49     valgrind --leak-check=yes --error-limit=no --show-reachable=yes src/app/tor
51 (Note that if you get a zillion openssl warnings, you will also need to
52 pass `--undef-value-errors=no` to valgrind, or rebuild your openssl
53 with `-DPURIFY`.)
55 Coverity
56 --------
58 Nick regularly runs the coverity static analyzer on the Tor codebase.
60 The preprocessor define `__COVERITY__` is used to work around instances
61 where coverity picks up behavior that we wish to permit.
63 clang Static Analyzer
64 ---------------------
66 The clang static analyzer can be run on the Tor codebase using Xcode (WIP)
67 or a command-line build.
69 The preprocessor define `__clang_analyzer__` is used to work around instances
70 where clang picks up behavior that we wish to permit.
72 clang Runtime Sanitizers
73 ------------------------
75 To build the Tor codebase with the clang Address and Undefined Behavior
76 sanitizers, see the file `contrib/clang/sanitize_blacklist.txt`.
78 Preprocessor workarounds for instances where clang picks up behavior that
79 we wish to permit are also documented in the blacklist file.
81 Running lcov for unit test coverage
82 -----------------------------------
84 Lcov is a utility that generates pretty HTML reports of test code coverage.
85 To generate such a report:
87     ./configure --enable-coverage
88     make
89     make coverage-html
90     $BROWSER ./coverage_html/index.html
92 This will run the tor unit test suite `./src/test/test` and generate the HTML
93 coverage code report under the directory `./coverage_html/`. To change the
94 output directory, use `make coverage-html HTML_COVER_DIR=./funky_new_cov_dir`.
96 Coverage diffs using lcov are not currently implemented, but are being
97 investigated (as of July 2014).
99 Running the unit tests
100 ----------------------
102 To quickly run all the tests distributed with Tor:
104     make check
106 To run the fast unit tests only:
108     make test
110 To selectively run just some tests (the following can be combined
111 arbitrarily):
113     ./src/test/test <name_of_test> [<name of test 2>] ...
114     ./src/test/test <prefix_of_name_of_test>.. [<prefix_of_name_of_test2>..] ...
115     ./src/test/test :<name_of_excluded_test> [:<name_of_excluded_test2]...
117 To run all tests, including those based on Stem or Chutney:
119     make test-full
121 To run all tests, including those based on Stem or Chutney that require a
122 working connection to the internet:
124     make test-full-online
126 Running gcov for unit test coverage
127 -----------------------------------
129     ./configure --enable-coverage
130     make
131     make check
132     # or--- make test-full ? make test-full-online?
133     mkdir coverage-output
134     ./scripts/test/coverage coverage-output
136 (On OSX, you'll need to start with `--enable-coverage CC=clang`.)
138 If that doesn't work:
140    * Try configuring Tor with `--disable-gcc-hardening`
141    * You might need to run `make clean` after you run `./configure`.
143 Then, look at the .gcov files in `coverage-output`.  '-' before a line means
144 that the compiler generated no code for that line.  '######' means that the
145 line was never reached.  Lines with numbers were called that number of times.
147 For more details about how to read gcov output, see the [Invoking
148 gcov](https://gcc.gnu.org/onlinedocs/gcc/Invoking-Gcov.html) chapter
149 of the GCC manual.
151 If you make changes to Tor and want to get another set of coverage results,
152 you can run `make reset-gcov` to clear the intermediary gcov output.
154 If you have two different `coverage-output` directories, and you want to see
155 a meaningful diff between them, you can run:
157     ./scripts/test/cov-diff coverage-output1 coverage-output2 | less
159 In this diff, any lines that were visited at least once will have coverage "1",
160 and line numbers are deleted.  This lets you inspect what you (probably) really
161 want to know: which untested lines were changed?  Are there any new untested
162 lines?
164 If you run ./scripts/test/cov-exclude, it marks excluded unreached
165 lines with 'x', and excluded reached lines with '!!!'.
167 Running integration tests
168 -------------------------
170 We have the beginnings of a set of scripts to run integration tests using
171 Chutney. To try them, set CHUTNEY_PATH to your chutney source directory, and
172 run `make test-network`.
174 We also have scripts to run integration tests using Stem.  To try them, set
175 `STEM_SOURCE_DIR` to your Stem source directory, and run `test-stem`.
177 Profiling Tor
178 -------------
180 Ongoing notes about Tor profiling can be found at
181 https://pad.riseup.net/p/profiling-tor
183 Profiling Tor with oprofile
184 ---------------------------
186 The oprofile tool runs (on Linux only!) to tell you what functions Tor is
187 spending its CPU time in, so we can identify performance bottlenecks.
189 Here are some basic instructions
191  - Build tor with debugging symbols (you probably already have, unless
192    you messed with CFLAGS during the build process).
193  - Build all the libraries you care about with debugging symbols
194    (probably you only care about libssl, maybe zlib and Libevent).
195  - Copy this tor to a new directory
196  - Copy all the libraries it uses to that dir too (`ldd ./tor` will
197    tell you)
198  - Set LD_LIBRARY_PATH to include that dir.  `ldd ./tor` should now
199    show you it's using the libs in that dir
200  - Run that tor
201  - Reset oprofiles counters/start it
202    * `opcontrol --reset; opcontrol --start`, if Nick remembers right.
203  - After a while, have it dump the stats on tor and all the libs
204    in that dir you created.
205    * `opcontrol --dump;`
206    * `opreport -l that_dir/*`
207  - Profit
209 Profiling Tor with perf
210 -----------------------
212 This works with a running Tor, and requires root.
214 1. Decide how long you want to profile for. Start with (say) 30 seconds. If that
215    works, try again with longer times.
217 2. Find the PID of your running tor process.
219 3. Run `perf record --call-graph dwarf -p <PID> sleep <SECONDS>`
221    (You may need to do this as root.)
223    You might need to add `-e cpu-clock` as an option to the perf record line
224    above, if you are on an older CPU without access to hardware profiling
225    events, or in a VM, or something.
227 4. Now you have a perf.data file. Have a look at it with `perf report
228    --no-children --sort symbol,dso` or `perf report --no-children --sort
229    symbol,dso --stdio --header`. How does it look?
231 5a. Once you have a nice big perf.data file, you can compress it, encrypt it,
232     and send it to your favorite Tor developers.
234 5b. Or maybe you'd rather not send a nice big perf.data file. Who knows what's
235     in that!? It's kinda scary. To generate a less scary file, you can use `perf
236     report -g > <FILENAME>.out`. Then you can compress that and put it somewhere
237     public.
239 Profiling Tor with gperftools aka Google-performance-tools
240 ----------------------------------------------------------
242 This should work on nearly any unixy system. It doesn't seem to be compatible
243 with RunAsDaemon though.
245 Beforehand, install google-perftools.
247 1. You need to rebuild Tor, hack the linking steps to add `-lprofiler` to the
248    libs. You can do this by adding `LIBS=-lprofiler` when you call `./configure`.
250 Now you can run Tor with profiling enabled, and use the pprof utility to look at
251 performance! See the gperftools manual for more info, but basically:
253 2. Run `env CPUPROFILE=/tmp/profile src/app/tor -f <path/torrc>`. The profile file
254    is not written to until Tor finishes execuction.
256 3. Run `pprof src/app/tor /tm/profile` to start the REPL.
258 Generating and analyzing a callgraph
259 ------------------------------------
261 0. Build Tor on linux or mac, ideally with -O0 or -fno-inline.
263 1. Clone 'https://gitweb.torproject.org/user/nickm/calltool.git/' .
264    Follow the README in that repository.
266 Note that currently the callgraph generator can't detect calls that pass
267 through function pointers.
269 Getting emacs to edit Tor source properly
270 -----------------------------------------
272 Nick likes to put the following snippet in his .emacs file:
275     (add-hook 'c-mode-hook
276           (lambda ()
277             (font-lock-mode 1)
278             (set-variable 'show-trailing-whitespace t)
280             (let ((fname (expand-file-name (buffer-file-name))))
281               (cond
282                ((string-match "^/home/nickm/src/libevent" fname)
283                 (set-variable 'indent-tabs-mode t)
284                 (set-variable 'c-basic-offset 4)
285                 (set-variable 'tab-width 4))
286                ((string-match "^/home/nickm/src/tor" fname)
287                 (set-variable 'indent-tabs-mode nil)
288                 (set-variable 'c-basic-offset 2))
289                ((string-match "^/home/nickm/src/openssl" fname)
290                 (set-variable 'indent-tabs-mode t)
291                 (set-variable 'c-basic-offset 8)
292                 (set-variable 'tab-width 8))
293             ))))
296 You'll note that it defaults to showing all trailing whitespace.  The `cond`
297 test detects whether the file is one of a few C free software projects that I
298 often edit, and sets up the indentation level and tab preferences to match
299 what they want.
301 If you want to try this out, you'll need to change the filename regex
302 patterns to match where you keep your Tor files.
304 If you use emacs for editing Tor and nothing else, you could always just say:
307     (add-hook 'c-mode-hook
308         (lambda ()
309             (font-lock-mode 1)
310             (set-variable 'show-trailing-whitespace t)
311             (set-variable 'indent-tabs-mode nil)
312             (set-variable 'c-basic-offset 2)))
315 There is probably a better way to do this.  No, we are probably not going
316 to clutter the files with emacs stuff.
319 Doxygen
320 -------
322 We use the 'doxygen' utility to generate documentation from our
323 source code. Here's how to use it:
325   1. Begin every file that should be documented with
327          /**
328           * \file filename.c
329           * \brief Short description of the file.
330           */
332      (Doxygen will recognize any comment beginning with /** as special.)
334   2. Before any function, structure, #define, or variable you want to
335      document, add a comment of the form:
337         /** Describe the function's actions in imperative sentences.
338          *
339          * Use blank lines for paragraph breaks
340          *   - and
341          *   - hyphens
342          *   - for
343          *   - lists.
344          *
345          * Write <b>argument_names</b> in boldface.
346          *
347          * \code
348          *     place_example_code();
349          *     between_code_and_endcode_commands();
350          * \endcode
351          */
353   3. Make sure to escape the characters `<`, `>`, `\`, `%` and `#` as `\<`,
354      `\>`, `\\`, `\%` and `\#`.
356   4. To document structure members, you can use two forms:
358         struct foo {
359           /** You can put the comment before an element; */
360           int a;
361           int b; /**< Or use the less-than symbol to put the comment
362                  * after the element. */
363         };
365   5. To generate documentation from the Tor source code, type:
367         $ doxygen -g
369      to generate a file called `Doxyfile`.  Edit that file and run
370      `doxygen` to generate the API documentation.
372   6. See the Doxygen manual for more information; this summary just
373      scratches the surface.