Thursday, November 13, 2014

using gcov/lcov with the linux kernel

Enable the following in your kernel configuration:
CONFIG_GCOV_KERNEL=y
CONFIG_GCOV_PROFILE_ALL=y
CONFIG_GCOV_FORMAT_AUTODETECT=y

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 [1].

Reboot the machine and use the following to see it things work:
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 use lcov [2] to accomplish this using the following:
# reset counters
sudo lcov --zerocounters
# run test
./test.sh
# generate coverage info and generate webpage
sudo lcov -c -o kerneltest.info
sudo genhtml -o /var/www/html kerneltest.info
  1. https://www.kernel.org/doc/Documentation/gcov.txt
  2. http://ltp.sourceforge.net/documentation/how-to/UsingCodeCoverage.pdf