gcov.rst 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. Using gcov with the Linux kernel
  2. ================================
  3. gcov profiling kernel support enables the use of GCC's coverage testing
  4. tool gcov_ with the Linux kernel. Coverage data of a running kernel
  5. is exported in gcov-compatible format via the "gcov" debugfs directory.
  6. To get coverage data for a specific file, change to the kernel build
  7. directory and use gcov with the ``-o`` option as follows (requires root)::
  8. # cd /tmp/linux-out
  9. # gcov -o /sys/kernel/debug/gcov/tmp/linux-out/kernel spinlock.c
  10. This will create source code files annotated with execution counts
  11. in the current directory. In addition, graphical gcov front-ends such
  12. as lcov_ can be used to automate the process of collecting data
  13. for the entire kernel and provide coverage overviews in HTML format.
  14. Possible uses:
  15. * debugging (has this line been reached at all?)
  16. * test improvement (how do I change my test to cover these lines?)
  17. * minimizing kernel configurations (do I need this option if the
  18. associated code is never run?)
  19. .. _gcov: http://gcc.gnu.org/onlinedocs/gcc/Gcov.html
  20. .. _lcov: http://ltp.sourceforge.net/coverage/lcov.php
  21. Preparation
  22. -----------
  23. Configure the kernel with::
  24. CONFIG_DEBUG_FS=y
  25. CONFIG_GCOV_KERNEL=y
  26. select the gcc's gcov format, default is autodetect based on gcc version::
  27. CONFIG_GCOV_FORMAT_AUTODETECT=y
  28. and to get coverage data for the entire kernel::
  29. CONFIG_GCOV_PROFILE_ALL=y
  30. Note that kernels compiled with profiling flags will be significantly
  31. larger and run slower. Also CONFIG_GCOV_PROFILE_ALL may not be supported
  32. on all architectures.
  33. Profiling data will only become accessible once debugfs has been
  34. mounted::
  35. mount -t debugfs none /sys/kernel/debug
  36. Customization
  37. -------------
  38. To enable profiling for specific files or directories, add a line
  39. similar to the following to the respective kernel Makefile:
  40. - For a single file (e.g. main.o)::
  41. GCOV_PROFILE_main.o := y
  42. - For all files in one directory::
  43. GCOV_PROFILE := y
  44. To exclude files from being profiled even when CONFIG_GCOV_PROFILE_ALL
  45. is specified, use::
  46. GCOV_PROFILE_main.o := n
  47. and::
  48. GCOV_PROFILE := n
  49. Only files which are linked to the main kernel image or are compiled as
  50. kernel modules are supported by this mechanism.
  51. Files
  52. -----
  53. The gcov kernel support creates the following files in debugfs:
  54. ``/sys/kernel/debug/gcov``
  55. Parent directory for all gcov-related files.
  56. ``/sys/kernel/debug/gcov/reset``
  57. Global reset file: resets all coverage data to zero when
  58. written to.
  59. ``/sys/kernel/debug/gcov/path/to/compile/dir/file.gcda``
  60. The actual gcov data file as understood by the gcov
  61. tool. Resets file coverage data to zero when written to.
  62. ``/sys/kernel/debug/gcov/path/to/compile/dir/file.gcno``
  63. Symbolic link to a static data file required by the gcov
  64. tool. This file is generated by gcc when compiling with
  65. option ``-ftest-coverage``.
  66. Modules
  67. -------
  68. Kernel modules may contain cleanup code which is only run during
  69. module unload time. The gcov mechanism provides a means to collect
  70. coverage data for such code by keeping a copy of the data associated
  71. with the unloaded module. This data remains available through debugfs.
  72. Once the module is loaded again, the associated coverage counters are
  73. initialized with the data from its previous instantiation.
  74. This behavior can be deactivated by specifying the gcov_persist kernel
  75. parameter::
  76. gcov_persist=0
  77. At run-time, a user can also choose to discard data for an unloaded
  78. module by writing to its data file or the global reset file.
  79. Separated build and test machines
  80. ---------------------------------
  81. The gcov kernel profiling infrastructure is designed to work out-of-the
  82. box for setups where kernels are built and run on the same machine. In
  83. cases where the kernel runs on a separate machine, special preparations
  84. must be made, depending on where the gcov tool is used:
  85. a) gcov is run on the TEST machine
  86. The gcov tool version on the test machine must be compatible with the
  87. gcc version used for kernel build. Also the following files need to be
  88. copied from build to test machine:
  89. from the source tree:
  90. - all C source files + headers
  91. from the build tree:
  92. - all C source files + headers
  93. - all .gcda and .gcno files
  94. - all links to directories
  95. It is important to note that these files need to be placed into the
  96. exact same file system location on the test machine as on the build
  97. machine. If any of the path components is symbolic link, the actual
  98. directory needs to be used instead (due to make's CURDIR handling).
  99. b) gcov is run on the BUILD machine
  100. The following files need to be copied after each test case from test
  101. to build machine:
  102. from the gcov directory in sysfs:
  103. - all .gcda files
  104. - all links to .gcno files
  105. These files can be copied to any location on the build machine. gcov
  106. must then be called with the -o option pointing to that directory.
  107. Example directory setup on the build machine::
  108. /tmp/linux: kernel source tree
  109. /tmp/out: kernel build directory as specified by make O=
  110. /tmp/coverage: location of the files copied from the test machine
  111. [user@build] cd /tmp/out
  112. [user@build] gcov -o /tmp/coverage/tmp/out/init main.c
  113. Troubleshooting
  114. ---------------
  115. Problem
  116. Compilation aborts during linker step.
  117. Cause
  118. Profiling flags are specified for source files which are not
  119. linked to the main kernel or which are linked by a custom
  120. linker procedure.
  121. Solution
  122. Exclude affected source files from profiling by specifying
  123. ``GCOV_PROFILE := n`` or ``GCOV_PROFILE_basename.o := n`` in the
  124. corresponding Makefile.
  125. Problem
  126. Files copied from sysfs appear empty or incomplete.
  127. Cause
  128. Due to the way seq_file works, some tools such as cp or tar
  129. may not correctly copy files from sysfs.
  130. Solution
  131. Use ``cat``' to read ``.gcda`` files and ``cp -d`` to copy links.
  132. Alternatively use the mechanism shown in Appendix B.
  133. Appendix A: gather_on_build.sh
  134. ------------------------------
  135. Sample script to gather coverage meta files on the build machine
  136. (see 6a)::
  137. #!/bin/bash
  138. KSRC=$1
  139. KOBJ=$2
  140. DEST=$3
  141. if [ -z "$KSRC" ] || [ -z "$KOBJ" ] || [ -z "$DEST" ]; then
  142. echo "Usage: $0 <ksrc directory> <kobj directory> <output.tar.gz>" >&2
  143. exit 1
  144. fi
  145. KSRC=$(cd $KSRC; printf "all:\n\t@echo \${CURDIR}\n" | make -f -)
  146. KOBJ=$(cd $KOBJ; printf "all:\n\t@echo \${CURDIR}\n" | make -f -)
  147. find $KSRC $KOBJ \( -name '*.gcno' -o -name '*.[ch]' -o -type l \) -a \
  148. -perm /u+r,g+r | tar cfz $DEST -P -T -
  149. if [ $? -eq 0 ] ; then
  150. echo "$DEST successfully created, copy to test system and unpack with:"
  151. echo " tar xfz $DEST -P"
  152. else
  153. echo "Could not create file $DEST"
  154. fi
  155. Appendix B: gather_on_test.sh
  156. -----------------------------
  157. Sample script to gather coverage data files on the test machine
  158. (see 6b)::
  159. #!/bin/bash -e
  160. DEST=$1
  161. GCDA=/sys/kernel/debug/gcov
  162. if [ -z "$DEST" ] ; then
  163. echo "Usage: $0 <output.tar.gz>" >&2
  164. exit 1
  165. fi
  166. TEMPDIR=$(mktemp -d)
  167. echo Collecting data..
  168. find $GCDA -type d -exec mkdir -p $TEMPDIR/\{\} \;
  169. find $GCDA -name '*.gcda' -exec sh -c 'cat < $0 > '$TEMPDIR'/$0' {} \;
  170. find $GCDA -name '*.gcno' -exec sh -c 'cp -d $0 '$TEMPDIR'/$0' {} \;
  171. tar czf $DEST -C $TEMPDIR sys
  172. rm -rf $TEMPDIR
  173. echo "$DEST successfully created, copy to build system and unpack with:"
  174. echo " tar xfz $DEST"