Using gcov with the Linux kernel on a typical single system

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?)
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:

To exclude files from being profiled even when CONFIG_GCOV_PROFILE_ALL is specified, use:

GCOV_PROFILE_main.o := n

Only files which are linked to the main kernel image or are compiled as kernel modules are supported by
this mechanism.

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.

0 0 vote
Article Rating
Notify of
Inline Feedbacks
View all comments
Back to top