Content1. Lcov是什么?2. 如何在Linux平台安装Lcov?3. 如何使用Lcov?(1) 使用lcov收集覆盖率数据并写入文件(2) 使用genhtml生成基于HTML的输出(3) 该例子的图形显示4. 编译lcov自带例子5. 其他相关工具(1) gcov-dump(2) ggcov
1. Lcov是什么? - 是Linux Test Project维护的开放源代码工具,最初被设计用来支持Linux内核覆盖率的度量
- 输出包括概述、覆盖率百分比、图表,能快速浏览覆盖率数据
- 支持大项目,提供三个级别的视图:目录视图、文件视图、源码视图
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?
# wget http://downloads.sourceforge.net/ltp/lcov-1.9.tar.gz# tar -zxvf lcov-1.9.tar.gz # cd lcov-1.9# lsbin contrib descriptions.tests lcovrc man rpmCHANGES COPYING example Makefile README# make install
不需要编译,直接安装即可,lcov, gendesc, genhtml, geninfo, genpng将被安装到/usr/bin目录。 3. 如何使用Lcov? 以Linux平台代码覆盖率测试工具GCOV简介一文的例子为例。 (1) 使用lcov收集覆盖率数据并写入文件
# lcov --capture --directory . --output-file test.info --test-name testCapturing coverage data from .Found gcov version: 4.1.2Scanning . for .gcda files ...Found 1 data files in .Processing test.gcdaFinished .info-file creation
.表示当前目录,收集coverage data,即.gcda文件中的信息,并写入test.info文件,且取名为test。其他选项请参考lcov的manual页。 test.info文件内容如下。
TN:testSF:/home/zubo/gcc/2011-04-10.sample/test.cFN:4,mainFNDA:1,mainFNF:1FNH:1BRDA:9,2,0,10BRDA:9,2,1,1BRDA:12,0,0,0BRDA:12,0,1,1BRF:4BRH:3DA:4,1DA:7,1DA:9,11DA:10,10DA:12,1DA:13,0DA:15,1DA:16,1LF:8LH:7end_of_record
(2) 使用genhtml生成基于HTML的输出 # genhtml test.info --output-directory output --title "a simple test" --show-details --legendReading data file test.infoFound 1 entries.Found common filename prefix "/home/zubo"Writing .css and .png files.Generating output.Processing file gcc/2011-04-10.sample/test.cWriting directory view page.Overall coverage rate: lines......: 87.5% (7 of 8 lines) functions..: 100.0% (1 of 1 function) branches...: 75.0% (3 of 4 branches)
选项解释请参考genhtml的manual页。cd到output目录,可以看到,生成了很多相关文件,如下。
# cd output# lsamber.png gcov.css index-sort-b.html ruby.pngemerald.png glass.png index-sort-f.html snow.pnggcc index.html index-sort-l.html updown.png
(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。 Referencelcov的manual页genhtml的manual页geninfo的manual页lcov的readme文件,本文/usr/src/lcov-1.9/READMElcov的makefile文件,本文为/usr/src/lcov-1.9/Makefile