Prepare MySQL

You will need to first build a MySQL distribution, with the following additional flags:

cmake .. -DCMAKE_CXXFLAGS="--coverage" # compatible with Clang

This will ask the compiler GCC to instrument the binary for recording coverage purpose, for how it works, see GCC 3.12 Program Instrumentation Options.

For a quick reference of how to build MySQL from source, you may check my previous posts How to Install MySQL From Source.

Set Privileges

You need to make sure your mysqld binary could create necessary directory, read and write the contents of the object folder. If you follow my previous post, you will do the following:

cd $mysql_root
cd bld # the cmake folder
cd ../../ # the parent folder of the source
chmod -R 777 $mysql_root # you may want to adjust the command

This will prevent you from having the error Profiling:Cannot create directory xxx.

Generate the Coverage Report

After you build and start the mysqld process, you could run any queries you want as normal. The coverage statistics generated by the instrumented binary is accumulative, as long as you don not remove the *.gcda files. To check if you have the correct files, run:

cd $mysql_root
find . -type f -name "*.gcda" | wc -l # should be > 0

Then you could run lcov to generate a coverage info file for a directory as following:

cd $mysql_root
lcov --directory . -c -o --no-external

The option --no-external asks the tool to only process files under our given directory, this will skip libraries like boost. For a full documentation, see lcov - a graphical GCOV front-end. The main difference between lcov and gcov is that, lcov could process multiple object/source files while gcov only process one.

Last we could get the report from the coverage info file:

genhtml -o . --ignore-errors source
ls *.html

You could find the index.html and other html files which contain the coverage report. You could open them in a browser.