gcov.txt 7.5 KB

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