HACKING 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. Hacking Tor: An Incomplete Guide
  2. ================================
  3. Useful tools
  4. ------------
  5. The buildbot
  6. ~~~~~~~~~~~~
  7. https://buildbot.vidalia-project.net/one_line_per_build
  8. Useful command-lines
  9. ~~~~~~~~~~~~~~~~~~~~
  10. Dmalloc
  11. ^^^^^^^
  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. Running gcov for unit test coverage
  22. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  23. -----
  24. make clean
  25. make CFLAGS='-g -fprofile-arcs -ftest-coverage'
  26. ./src/test/test
  27. cd src/common; gcov *.[ch]
  28. cd ../or; gcov *.[ch]
  29. -----
  30. Then, look at the .gcov files. '-' before a line means that the
  31. compiler generated no code for that line. '######' means that the
  32. line was never reached. Lines with numbers were called that number
  33. of times.
  34. Coding conventions
  35. ------------------
  36. Whitespace and C conformance
  37. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  38. Invoke "make check-spaces" from time to time, so it can tell you about
  39. deviations from our C whitespace style. Generally, we use:
  40. - Unix-style line endings
  41. - K&R-style indentation
  42. - No space before newlines
  43. - A blank line at the end of each file
  44. - Never more than one blank line in a row
  45. - Always spaces, never tabs
  46. - No more than 79-columns per line.
  47. - Two spaces per indent.
  48. - A space between control keywords and their corresponding paren
  49. "if (x)", "while (x)", and "switch (x)", never "if(x)", "while(x)", or
  50. "switch(x)".
  51. - A space between anything and an open brace.
  52. - No space between a function name and an opening paren. "puts(x)", not
  53. "puts (x)".
  54. - Function declarations at the start of the line.
  55. We try hard to build without warnings everywhere. In particular, if you're
  56. using gcc, you should invoke the configure script with the option
  57. "--enable-gcc-warnings". This will give a bunch of extra warning flags to
  58. the compiler, and help us find divergences from our preferred C style.
  59. Getting emacs to edit Tor source properly
  60. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  61. Hi, folks! Nick here. I like to put the following snippet in my .emacs
  62. file:
  63. -----
  64. (add-hook 'c-mode-hook
  65. (lambda ()
  66. (font-lock-mode 1)
  67. (set-variable 'show-trailing-whitespace t)
  68. (let ((fname (expand-file-name (buffer-file-name))))
  69. (cond
  70. ((string-match "^/home/nickm/src/libevent" fname)
  71. (set-variable 'indent-tabs-mode t)
  72. (set-variable 'c-basic-offset 4)
  73. (set-variable 'tab-width 4))
  74. ((string-match "^/home/nickm/src/tor" fname)
  75. (set-variable 'indent-tabs-mode nil)
  76. (set-variable 'c-basic-offset 2))
  77. ((string-match "^/home/nickm/src/openssl" fname)
  78. (set-variable 'indent-tabs-mode t)
  79. (set-variable 'c-basic-offset 8)
  80. (set-variable 'tab-width 8))
  81. ))))
  82. -----
  83. You'll note that it defaults to showing all trailing whitespace. The "cond"
  84. test detects whether the file is one of a few C free software projects that I
  85. often edit, and sets up the indentation level and tab preferences to match
  86. what they want.
  87. If you want to try this out, you'll need to change the filename regex
  88. patterns to match where you keep your Tor files.
  89. If you *only* use emacs to edit Tor, you could always just say:
  90. -----
  91. (add-hook 'c-mode-hook
  92. (lambda ()
  93. (font-lock-mode 1)
  94. (set-variable 'show-trailing-whitespace t)
  95. (set-variable 'indent-tabs-mode nil)
  96. (set-variable 'c-basic-offset 2)))
  97. -----
  98. There is probably a better way to do this. No, we are probably not going
  99. to clutter the files with emacs stuff.
  100. Details
  101. ~~~~~~~
  102. Use tor_malloc, tor_free, tor_strdup, and tor_gettimeofday instead of their
  103. generic equivalents. (They always succeed or exit.)
  104. You can get a full list of the compatibility functions that Tor provides by
  105. looking through src/common/util.h and src/common/compat.h. You can see the
  106. available containers in src/common/containers.h. You should probably
  107. familiarize yourself with these modules before you write too much code, or
  108. else you'll wind up reinventing the wheel.
  109. Use 'INLINE' instead of 'inline', so that we work properly on Windows.
  110. Calling and naming conventions
  111. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  112. Whenever possible, functions should return -1 on error and 0 on success.
  113. For multi-word identifiers, use lowercase words combined with
  114. underscores. (e.g., "multi_word_identifier"). Use ALL_CAPS for macros and
  115. constants.
  116. Typenames should end with "_t".
  117. Function names should be prefixed with a module name or object name. (In
  118. general, code to manipulate an object should be a module with the same name
  119. as the object, so it's hard to tell which convention is used.)
  120. Functions that do things should have imperative-verb names
  121. (e.g. buffer_clear, buffer_resize); functions that return booleans should
  122. have predicate names (e.g. buffer_is_empty, buffer_needs_resizing).
  123. If you find that you have four or more possible return code values, it's
  124. probably time to create an enum. If you find that you are passing three or
  125. more flags to a function, it's probably time to create a flags argument that
  126. takes a bitfield.
  127. What To Optimize
  128. ~~~~~~~~~~~~~~~~
  129. Don't optimize anything if it's not in the critical path. Right now, the
  130. critical path seems to be AES, logging, and the network itself. Feel free to
  131. do your own profiling to determine otherwise.
  132. Log conventions
  133. ~~~~~~~~~~~~~~~
  134. https://wiki.torproject.org/noreply/TheOnionRouter/TorFAQ#LogLevels
  135. No error or warning messages should be expected during normal OR or OP
  136. operation.
  137. If a library function is currently called such that failure always means ERR,
  138. then the library function should log WARN and let the caller log ERR.
  139. [XXX Proposed convention: every message of severity INFO or higher should
  140. either (A) be intelligible to end-users who don't know the Tor source; or (B)
  141. somehow inform the end-users that they aren't expected to understand the
  142. message (perhaps with a string like "internal error"). Option (A) is to be
  143. preferred to option (B). -NM]
  144. Doxygen
  145. ~~~~~~~~
  146. We use the 'doxygen' utility to generate documentation from our
  147. source code. Here's how to use it:
  148. 1. Begin every file that should be documented with
  149. /**
  150. * \file filename.c
  151. * \brief Short description of the file.
  152. **/
  153. (Doxygen will recognize any comment beginning with /** as special.)
  154. 2. Before any function, structure, #define, or variable you want to
  155. document, add a comment of the form:
  156. /** Describe the function's actions in imperative sentences.
  157. *
  158. * Use blank lines for paragraph breaks
  159. * - and
  160. * - hyphens
  161. * - for
  162. * - lists.
  163. *
  164. * Write <b>argument_names</b> in boldface.
  165. *
  166. * \code
  167. * place_example_code();
  168. * between_code_and_endcode_commands();
  169. * \endcode
  170. */
  171. 3. Make sure to escape the characters "<", ">", "\", "%" and "#" as "\<",
  172. "\>", "\\", "\%", and "\#".
  173. 4. To document structure members, you can use two forms:
  174. struct foo {
  175. /** You can put the comment before an element; */
  176. int a;
  177. int b; /**< Or use the less-than symbol to put the comment
  178. * after the element. */
  179. };
  180. 5. To generate documentation from the Tor source code, type:
  181. $ doxygen -g
  182. To generate a file called 'Doxyfile'. Edit that file and run
  183. 'doxygen' to generate the API documentation.
  184. 6. See the Doxygen manual for more information; this summary just
  185. scratches the surface.
  186. Doxygen comment conventions
  187. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  188. Say what functions do as a series of one or more imperative sentences, as
  189. though you were telling somebody how to be the function. In other words, DO
  190. NOT say:
  191. /** The strtol function parses a number.
  192. *
  193. * nptr -- the string to parse. It can include whitespace.
  194. * endptr -- a string pointer to hold the first thing that is not part
  195. * of the number, if present.
  196. * base -- the numeric base.
  197. * returns: the resulting number.
  198. */
  199. long strtol(const char *nptr, char **nptr, int base);
  200. Instead, please DO say:
  201. /** Parse a number in radix <b>base</b> from the string <b>nptr</b>,
  202. * and return the result. Skip all leading whitespace. If
  203. * <b>endptr</b> is not NULL, set *<b>endptr</b> to the first character
  204. * after the number parsed.
  205. **/
  206. long strtol(const char *nptr, char **nptr, int base);
  207. Doxygen comments are the contract in our abstraction-by-contract world: if
  208. the functions that call your function rely on it doing something, then your
  209. function should mention that it does that something in the documentation. If
  210. you rely on a function doing something beyond what is in its documentation,
  211. then you should watch out, or it might do something else later.