gcov profiling kernel support enables the use of GCC’s coverage testing tool gcov with the Linux kernel. Coverage data of a running kernel is exported in gcov-compatible format via the “gcov” debugfs directory. To get coverage data for a specific file, change to the kernel build directory and use gcov with the -o option as follows (requires root): root@debian:~# cd /home/jeffrin/upstream/linux-kselftest root@debian:/home/jeffrin/upstream/linux-kselftest# export PS1=# #gcov -o /sys/kernel/debug/gcov/home/jeffrin/upstream/linux-kselftest/kernel/locking/ spinlock.c This will create source code files annotated with execution counts in the current directory. In addition, graphical gcov front-ends such as lcov can be used to automate the process of collecting data for the entire kernel and provide coverage overviews in HTML format. Possible uses: debugging (has this line been reached at all?) test improvement (how do I change my test to cover these lines?) minimizing kernel configurations (do I need this option if the associated code is never run?) Preparation Configure the kernel with: CONFIG_DEBUG_FS=y CONFIG_GCOV_KERNEL=y and to get coverage data for the entire kernel: CONFIG_GCOV_PROFILE_ALL=y Note that kernels compiled with profiling flags will be significantly larger and run slower. Also CONFIG_GCOV_PROFILE_ALL may not be supported on all architectures. Profiling data will only become accessible once debugfs has been mounted: mount -t debugfs none /sys/kernel/debug Customization To enable profiling for specific files or directories, add a line similar to the following to the respective kernel Makefile: For a single file (e.g. main.o): GCOV_PROFILE_main.o := y For all files in one directory: GCOV_PROFILE := y To exclude files from being profiled even when CONFIG_GCOV_PROFILE_ALL is specified, use: GCOV_PROFILE_main.o := n and: GCOV_PROFILE := n Only files which are linked to the main kernel image or are compiled as kernel modules are supported by this mechanism. Files The gcov kernel support creates the following files in debugfs: /sys/kernel/debug/gcov Parent directory for all gcov-related files. /sys/kernel/debug/gcov/reset Global reset file: resets all coverage data to zero when written to. /sys/kernel/debug/gcov/path/to/compile/dir/file.gcda The actual gcov data file as understood by the gcov tool. Resets file coverage data to zero when written to. /sys/kernel/debug/gcov/path/to/compile/dir/file.gcno Symbolic link to a static data file required by the gcov tool. This file is generated by gcc when compiling with option -ftest-coverage. Modules Kernel modules may contain cleanup code which is only run during module unload time. The gcov mechanism provides a means to collect coverage data for such code by keeping a copy of the data associated with the unloaded module. This data remains available through debugfs. Once the module is loaded again, the associated coverage counters are initialized with the data from its previous instantiation. This behavior can be deactivated by specifying the gcov_persist kernel parameter: gcov_persist=0 At run-time, a user can also choose to discard data for an unloaded module by writing to its data file or the global reset file.