Content
1. Lcov是什么?
2. 如何在Linux平台安装Lcov?
3. 如何使用Lcov?
(1) 使用lcov收集覆盖率数据并写入文件
(2) 使用genhtml生成基于HTML的输出
(3) 该例子的图形显示
4. 编译lcov自带例子
5. 其他相关工具
(1) gcov-dump
(2) ggcov
1. Lcov是什么?
Use lcov to collect coverage data and genhtml to create HTML pages. Coverage data can either be collected from the currently running Linux kernel or from a user space application. To do this, you have to complete the following preparation steps:
For Linux kernel coverage:
Follow the setup instructions for the gcov-kernel infrastructure:
http://ltp.sourceforge.net/coverage/gcov.php
For user space application coverage:
Compile the application with GCC using the options "-fprofile-arcs" and "-ftest-coverage".
2. 如何在Linux平台安装Lcov?
不需要编译,直接安装即可,lcov, gendesc, genhtml, geninfo, genpng将被安装到/usr/bin目录。
3. 如何使用Lcov?
(1) 使用lcov收集覆盖率数据并写入文件
.表示当前目录,收集coverage data,即.gcda文件中的信息,并写入test.info文件,且取名为test。其他选项请参考lcov的manual页。
test.info文件内容如下。
(2) 使用genhtml生成基于HTML的输出
选项解释请参考genhtml的manual页。cd到output目录,可以看到,生成了很多相关文件,如下。
(3) 该例子的图形显示
(3.1) top level的视图
(3.2) 文件或函数的视图
4. 编译lcov自带例子
# cd /usr/src/lcov-1.9/example
# make
编译、运行自带例子并查看结果是快速学习某个工具最好的方法。从example的makefile文件和编译输出,都可以学习相关概念和命令的使用方法。Html输出可以由/usr/src/lcov-1.9/example/output/index.html查看。读者可自行实验。
5. 其他相关工具
(1) gcov-dump
或许,我们还可以使用gcov-dump命令输出gcov的相关数据,但gcc默认不编译gcov-dump,因此,要使用它,可能需要重新编译gcc。
(2) ggcov
Ggcov is a Graphical tool for displaying gcov test coverage data. 详细信息可参考http://ggcov.sourceforge.net。
Reference
lcov的manual页
genhtml的manual页
geninfo的manual页
lcov的readme文件,本文/usr/src/lcov-1.9/README
lcov的makefile文件,本文为/usr/src/lcov-1.9/Makefile