HelpfulTools.txt 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. Useful tools
  2. ------------
  3. These aren't strictly necessary for hacking on Tor, but they can help track
  4. down bugs.
  5. Jenkins
  6. ~~~~~~~
  7. https://jenkins.torproject.org
  8. Dmalloc
  9. ~~~~~~~
  10. The dmalloc library will keep track of memory allocation, so you can find out
  11. if we're leaking memory, doing any double-frees, or so on.
  12. dmalloc -l ~/dmalloc.log
  13. (run the commands it tells you)
  14. ./configure --with-dmalloc
  15. Valgrind
  16. ~~~~~~~~
  17. valgrind --leak-check=yes --error-limit=no --show-reachable=yes src/or/tor
  18. (Note that if you get a zillion openssl warnings, you will also need to
  19. pass --undef-value-errors=no to valgrind, or rebuild your openssl
  20. with -DPURIFY.)
  21. Coverity
  22. ~~~~~~~~
  23. Nick regularly runs the coverity static analyzer on the Tor codebase.
  24. The preprocessor define __COVERITY__ is used to work around instances
  25. where coverity picks up behavior that we wish to permit.
  26. clang Static Analyzer
  27. ~~~~~~~~~~~~~~~~~~~~~
  28. The clang static analyzer can be run on the Tor codebase using Xcode (WIP)
  29. or a command-line build.
  30. The preprocessor define __clang_analyzer__ is used to work around instances
  31. where clang picks up behavior that we wish to permit.
  32. clang Runtime Sanitizers
  33. ~~~~~~~~~~~~~~~~
  34. To build the Tor codebase with the clang Address and Undefined Behavior
  35. sanitizers, see the file contrib/clang/sanitize_blacklist.txt.
  36. Preprocessor workarounds for instances where clang picks up behavior that
  37. we wish to permit are also documented in the blacklist file.
  38. Running lcov for unit test coverage
  39. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  40. Lcov is a utility that generates pretty HTML reports of test code coverage.
  41. To generate such a report:
  42. -----
  43. ./configure --enable-coverage
  44. make
  45. make coverage-html
  46. $BROWSER ./coverage_html/index.html
  47. -----
  48. This will run the tor unit test suite `./src/test/test` and generate the HTML
  49. coverage code report under the directory ./coverage_html/. To change the
  50. output directory, use `make coverage-html HTML_COVER_DIR=./funky_new_cov_dir`.
  51. Coverage diffs using lcov are not currently implemented, but are being
  52. investigated (as of July 2014).
  53. Running the unit tests
  54. ~~~~~~~~~~~~~~~~~~~~~~
  55. To quickly run all the tests distributed with Tor:
  56. -----
  57. make check
  58. -----
  59. To run the fast unit tests only:
  60. -----
  61. make test
  62. -----
  63. To selectively run just some tests (the following can be combined
  64. arbitrarily):
  65. -----
  66. ./src/test/test <name_of_test> [<name of test 2>] ...
  67. ./src/test/test <prefix_of_name_of_test>.. [<prefix_of_name_of_test2>..] ...
  68. ./src/test/test :<name_of_excluded_test> [:<name_of_excluded_test2]...
  69. -----
  70. To run all tests, including those based on Stem or Chutney:
  71. -----
  72. make test-full
  73. -----
  74. To run all tests, including those basedd on Stem or Chutney that require a
  75. working connection to the internet:
  76. -----
  77. make test-full-online
  78. -----
  79. Running gcov for unit test coverage
  80. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  81. -----
  82. ./configure --enable-coverage
  83. make
  84. make check
  85. # or--- make test-full ? make test-full-online?
  86. mkdir coverage-output
  87. ./scripts/test/coverage coverage-output
  88. -----
  89. (On OSX, you'll need to start with "--enable-coverage CC=clang".)
  90. Then, look at the .gcov files in coverage-output. '-' before a line means
  91. that the compiler generated no code for that line. '######' means that the
  92. line was never reached. Lines with numbers were called that number of times.
  93. If that doesn't work:
  94. * Try configuring Tor with --disable-gcc-hardening
  95. * You might need to run 'make clean' after you run './configure'.
  96. If you make changes to Tor and want to get another set of coverage results,
  97. you can run "make reset-gcov" to clear the intermediary gcov output.
  98. If you have two different "coverage-output" directories, and you want to see
  99. a meaningful diff between them, you can run:
  100. -----
  101. ./scripts/test/cov-diff coverage-output1 coverage-output2 | less
  102. -----
  103. In this diff, any lines that were visited at least once will have coverage
  104. "1". This lets you inspect what you (probably) really want to know: which
  105. untested lines were changed? Are there any new untested lines?
  106. Running integration tests
  107. ~~~~~~~~~~~~~~~~~~~~~~~~~
  108. We have the beginnings of a set of scripts to run integration tests using
  109. Chutney. To try them, set CHUTNEY_PATH to your chutney source directory, and
  110. run "make test-network".
  111. We also have scripts to run integration tests using Stem. To try them, set
  112. STEM_SOURCE_DIR to your Stem source directory, and run "test-stem".
  113. Profiling Tor with oprofile
  114. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  115. The oprofile tool runs (on Linux only!) to tell you what functions Tor is
  116. spending its CPU time in, so we can identify berformance pottlenecks.
  117. Here are some basic instructions
  118. - Build tor with debugging symbols (you probably already have, unless
  119. you messed with CFLAGS during the build process).
  120. - Build all the libraries you care about with debugging symbols
  121. (probably you only care about libssl, maybe zlib and Libevent).
  122. - Copy this tor to a new directory
  123. - Copy all the libraries it uses to that dir too (ldd ./tor will
  124. tell you)
  125. - Set LD_LIBRARY_PATH to include that dir. ldd ./tor should now
  126. show you it's using the libs in that dir
  127. - Run that tor
  128. - Reset oprofiles counters/start it
  129. * "opcontrol --reset; opcontrol --start", if Nick remembers right.
  130. - After a while, have it dump the stats on tor and all the libs
  131. in that dir you created.
  132. * "opcontrol --dump;"
  133. * "opreport -l that_dir/*"
  134. - Profit
  135. Generating and analyzing a callgraph
  136. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  137. 1. Run ./scripts/maint/generate_callgraph.sh . This will generate a
  138. bunch of files in a new ./callgraph directory.
  139. 2. Run ./scripts/maint/analyze_callgraph.py callgraph/src/*/* . This
  140. will do a lot of graph operations and then dump out a new
  141. "callgraph.pkl" file, containing data in Python's "pickle" format.
  142. 3. Run ./scripts/maint/display_callgraph.py . It will display:
  143. - the number of functions reachable from each function.
  144. - all strongly-connnected components in the Tor callgraph
  145. - the largest bottlenecks in the largest SCC in the Tor callgraph.
  146. Note that currently the callgraph generator can't detect calls that pass
  147. through function pointers.
  148. Getting emacs to edit Tor source properly
  149. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  150. Nick likes to put the following snippet in his .emacs file:
  151. -----
  152. (add-hook 'c-mode-hook
  153. (lambda ()
  154. (font-lock-mode 1)
  155. (set-variable 'show-trailing-whitespace t)
  156. (let ((fname (expand-file-name (buffer-file-name))))
  157. (cond
  158. ((string-match "^/home/nickm/src/libevent" fname)
  159. (set-variable 'indent-tabs-mode t)
  160. (set-variable 'c-basic-offset 4)
  161. (set-variable 'tab-width 4))
  162. ((string-match "^/home/nickm/src/tor" fname)
  163. (set-variable 'indent-tabs-mode nil)
  164. (set-variable 'c-basic-offset 2))
  165. ((string-match "^/home/nickm/src/openssl" fname)
  166. (set-variable 'indent-tabs-mode t)
  167. (set-variable 'c-basic-offset 8)
  168. (set-variable 'tab-width 8))
  169. ))))
  170. -----
  171. You'll note that it defaults to showing all trailing whitespace. The "cond"
  172. test detects whether the file is one of a few C free software projects that I
  173. often edit, and sets up the indentation level and tab preferences to match
  174. what they want.
  175. If you want to try this out, you'll need to change the filename regex
  176. patterns to match where you keep your Tor files.
  177. If you use emacs for editing Tor and nothing else, you could always just say:
  178. -----
  179. (add-hook 'c-mode-hook
  180. (lambda ()
  181. (font-lock-mode 1)
  182. (set-variable 'show-trailing-whitespace t)
  183. (set-variable 'indent-tabs-mode nil)
  184. (set-variable 'c-basic-offset 2)))
  185. -----
  186. There is probably a better way to do this. No, we are probably not going
  187. to clutter the files with emacs stuff.
  188. Doxygen
  189. ~~~~~~~~
  190. We use the 'doxygen' utility to generate documentation from our
  191. source code. Here's how to use it:
  192. 1. Begin every file that should be documented with
  193. /**
  194. * \file filename.c
  195. * \brief Short description of the file.
  196. **/
  197. (Doxygen will recognize any comment beginning with /** as special.)
  198. 2. Before any function, structure, #define, or variable you want to
  199. document, add a comment of the form:
  200. /** Describe the function's actions in imperative sentences.
  201. *
  202. * Use blank lines for paragraph breaks
  203. * - and
  204. * - hyphens
  205. * - for
  206. * - lists.
  207. *
  208. * Write <b>argument_names</b> in boldface.
  209. *
  210. * \code
  211. * place_example_code();
  212. * between_code_and_endcode_commands();
  213. * \endcode
  214. */
  215. 3. Make sure to escape the characters "<", ">", "\", "%" and "#" as "\<",
  216. "\>", "\\", "\%", and "\#".
  217. 4. To document structure members, you can use two forms:
  218. struct foo {
  219. /** You can put the comment before an element; */
  220. int a;
  221. int b; /**< Or use the less-than symbol to put the comment
  222. * after the element. */
  223. };
  224. 5. To generate documentation from the Tor source code, type:
  225. $ doxygen -g
  226. To generate a file called 'Doxyfile'. Edit that file and run
  227. 'doxygen' to generate the API documentation.
  228. 6. See the Doxygen manual for more information; this summary just
  229. scratches the surface.