gcov.txt 7.6 KB

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