using gcov/lcov with the linux kernel

GCOV/LCOV are amazing tools to figure out code coverage in the Linux kernel. It is fairly trivial to setup on your own machine. First, enable the following in your kernel configuration:


Build the kernel and install headers/debug/images to your machine. Note there may be issues if you run this on a separate machine so consult the official documentation for additional information.

Boot the machine with the instrumented kernel and use the following to verify GCOV_KERNEL was setup properly:

sudo apt-get install lcov gcc
SRC_PATH=~/src/linux # or whatever your path to your kernel tree is
gcov ${SRC_PATH}/kernel/sched/core.c -o /sys/kernel/debug/gcov/${SRC_PATH}/kernel/sched/

Obviously this would be useful for quick tests, but perhaps we want to test coverage after a test, and display results graphically. We can reset counters and use lcov to accomplish this:

# reset counters
sudo lcov --zerocounters
# run test
# generate coverage info and generate webpage
sudo lcov -c -o
sudo genhtml -o /var/www/html