July 4, 2019
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.
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?)
Configure the kernel with:
and to get coverage data for the entire kernel:
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
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
GCOV_PROFILE := n
Only files which are linked to the main kernel image or are compiled as kernel modules are supported by
The gcov kernel support creates the following files in debugfs:
Parent directory for all gcov-related files.
Global reset file: resets all coverage data to zero when written to.
The actual gcov data file as understood by the gcov tool. Resets file coverage data to zero when written
Symbolic link to a static data file required by the gcov tool. This file is generated by gcc when
compiling with option -ftest-coverage.
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:
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.
Tags: development gcov kernel tools