Writing

Feed Software, technology, sysadmin war stories, and more.

Tuesday, August 2, 2011

Updated code coverage HTML generation script

A couple of months ago, I wrote a post on using gcov and lcov to get code coverage data.

I've improved my recipe since then. In the interest of sharing, here is my updated 'coverage' script:

#!/bin/sh
 
OUTPUT_DIR=/some/path
 
if (! test -d $OUTPUT_DIR )
then
  echo "Missing path: $OUTPUT_DIR"
  exit 1
fi
 
make clean || exit 1
 
export LDFLAGS="-lgcov"
export CPPFLAGS="-fprofile-arcs -ftest-coverage -g"
 
make build-all || exit 1
lcov -c -i -d . -o .coverage.base
 
make run-tests || exit 1
lcov -c -d . -o .coverage.run
 
lcov -d . -a .coverage.base -a .coverage.run -o .coverage.total
genhtml -q --no-branch-coverage -o $OUTPUT_DIR .coverage.total
rm -f .coverage.base .coverage.run .coverage.total
 
make clean || exit 1

This one makes the important distinction of initializing the coverage data (lcov's -i option) after building everything but before running any code. This way you can see things which never get visited.

It also switches on extra debugging info for gcc (-g) in case things start getting hairy, like they did for me last month when I ran into fork() crashing in coverage builds on Mac OS X.

Naturally, you will need to adjust either this script or your Makefiles to have suitable targets to build everything and test everything in one go.

Happy hacking!


April 14, 2012: This post has an update.