Linux覆盖率一点研究:获取覆盖率数据

   首先,当然哥不介意你鄙视我在网上找的资料研究!

  白盒覆盖率是啥东东这个问题大家自己查百度啦!我也不太懂,就知道它不是个东西,就这样开始吧(MT一般是先摸四蹄呢还是先黑金币呢?这是个问题)!

 

  首先:linux先Clear一下,然后呢再ls下。然后呢再找个CPP文件。其实生成覆盖率主要就一个参数的问题。因为g++那啥已经带了获取覆盖率的功能。

     下面是执行获取覆盖率的一个流程:其中红色字体是敲入的重要命令。

  

[lixiang@localhost cjj]$ clear
[lixiang@localhost cjj]$ ls
ctest.c voctorTest.cpp
[lixiang@localhost cjj]$ g++ -fprofile-arcs -ftest-coverage voctorTest.cpp -o test
[lixiang@localhost cjj]$ ls
ctest.c test voctorTest.bb voctorTest.bbg voctorTest.cpp
[lixiang@localhost cjj]$ ./test
start test:

vector include num:120

end test
[lixiang@localhost cjj]$ ls
ctest.c test voctorTest.bb voctorTest.bbg voctorTest.cpp voctorTest.da
[lixiang@localhost cjj]$ gcov voctorTest.cpp
100.00% of 1 source lines executed in file /usr/include/c++/3.2.2/new
Creating new.gcov.
100.00% of 1 source lines executed in file /usr/include/c++/3.2.2/iostream
Creating iostream.gcov.
100.00% of 2 source lines executed in file /usr/include/c++/3.2.2/bits/stl_uninitialized.h
Creating stl_uninitialized.h.gcov.
42.86% of 14 source lines executed in file /usr/include/c++/3.2.2/bits/stl_algobase.h
Creating stl_algobase.h.gcov.
100.00% of 6 source lines executed in file /usr/include/c++/3.2.2/bits/stl_iterator.h
Creating stl_iterator.h.gcov.
100.00% of 4 source lines executed in file /usr/include/c++/3.2.2/bits/stl_construct.h
Creating stl_construct.h.gcov.
73.81% of 42 source lines executed in file /usr/include/c++/3.2.2/bits/stl_vector.h
Creating stl_vector.h.gcov.
100.00% of 6 source lines executed in file /usr/include/c++/3.2.2/bits/stl_alloc.h
Creating stl_alloc.h.gcov.
100.00% of 8 source lines executed in file voctorTest.cpp
Creating voctorTest.cpp.gcov.
[lixiang@localhost cjj]$ ls
ctest.c stl_construct.h.gcov voctorTest.bb
iostream.gcov stl_iterator.h.gcov voctorTest.bbg
new.gcov stl_uninitialized.h.gcov voctorTest.cpp
stl_algobase.h.gcov stl_vector.h.gcov voctorTest.cpp.gcov
stl_alloc.h.gcov test voctorTest.da

 

执行完后:其中覆盖率数据保存在voctorTest.cpp.gcov这个文件中。可以用cat命令查看覆盖率信息,查看的结果如下:

[lixiang@localhost cjj]$ cat voctorTest.cpp.gcov
#include "iostream"
#include<vector>
using namespace std;
int main()
1 {
3 vector<int> vec;
1 vec.push_back(120);
1 cout<<"start test:rn"<<endl;
1 cout<<"vector include num:"<<vec[0]<<"rn"<<endl;
1 cout<<"end test"<<endl;
1 return 0;
1 }
[lixiang@localhost cjj]$

其中文件每行前面的数字代表着执行的次数。如下图解释的一样一样的。该图也是拷贝其他文章的奥。

Linux覆盖率一点研究:获取覆盖率数据

 

OK,这里覆盖率数据就算得到和显示出来并看到了,然后其实他们有的人还用LCov把该报告转换成html文件,由于哥今天安装了WinSCP神器软件,所以俺也准备试试,可以下载到本地来看看,有意思,好玩,好玩!LCov主要是看到第二篇文章。

坑爹了.........    哥hai 没安装LCov........    无语中:

下载一个Lcov来安装,提示依赖项没有,添加个命令来执行nodeps  其中在nodeps前面要加”--“   记住是两个”-“,不然会提示”-nodeps:unknown option“

安装时又提示:cannot get exclusive lock  on /var/lib/rpm/packages

该问题我猜是由于安装软件莫得权限,然后查资料果然正确,需要root权限,但是想到root权限安装的软件普通用户是不是基本都能用呢?所以我试下:

经过上面的折腾,觉得应该正确安装命令应该是:

rpm -ihv --nodeps lcov-1.10-1.noarch.rpm

参考文章:http://linux.sheup.com/linux/linux5218.htm

http://www.jb51.net/os/RedHat/1277.html

好辛苦,终于安装LCov成功了。安装过程中还遇到几个问题。

rpmdb:unable to join the environment

该问题主要是由于文件出了问题样,应该是系统文件,然后参考如下文章:http://blog.csdn.net/avilifans/article/details/17751545

使用export LD_ASSUME_KERNEL =2.2.25这个命令,即可解决了。

 

 

安装成功LCov后:

[lixiang@localhost lixiang]$ lcov
lcov: Need one of options -z, -c, -a, -e, -r, -l, --diff or --summary
Use lcov --help to get usage information
[lixiang@localhost lixiang]$

 

 

然后执行LCov,下面是整个命令过程,红色加粗为输入命令:

[lixiang@localhost lixiang]$ ls
file SaveSoft
[lixiang@localhost lixiang]$ lcov
lcov: Need one of options -z, -c, -a, -e, -r, -l, --diff or --summary
Use lcov --help to get usage information
[lixiang@localhost lixiang]$ [lixiang@localhost lixiang]$ lcov
-bash: [lixiang@localhost: command not found
[lixiang@localhost lixiang]$ lcov: Need one of options -z, -c, -a, -e, -r, -l, --diff or --summary
-bash: lcov:: command not found
[lixiang@localhost lixiang]$ Use lcov --help to get usage information
-bash: Use: command not found
[lixiang@localhost lixiang]$ [lixiang@localhost lixiang]$
-bash: [lixiang@localhost: command not found
[lixiang@localhost lixiang]$ ls
file SaveSoft
[lixiang@localhost lixiang]$ cd file
[lixiang@localhost file]$ ls
addpe c shellscript
[lixiang@localhost file]$ cd addpe
[lixiang@localhost addpe]$ ls
add.c add.o cjj main.c makefile prcessadmin
add.h CExample1.c ExTest main.o oldfile TestOpenFile.c
[lixiang@localhost addpe]$ cd cjj
[lixiang@localhost cjj]$ ls
ctest.c stl_construct.h.gcov voctorTest.bb
iostream.gcov stl_iterator.h.gcov voctorTest.bbg
new.gcov stl_uninitialized.h.gcov voctorTest.cpp
stl_algobase.h.gcov stl_vector.h.gcov voctorTest.cpp.gcov
stl_alloc.h.gcov test voctorTest.da
[lixiang@localhost cjj]$ lcov -c -d ./ -o app.info
Capturing coverage data from ./
Found gcov version: 3.2.2
Scanning ./ for .da files ...
Found 1 data files in ./
Processing voctorTest.da
geninfo: WARNING: cannot find an entry for iostream.gcov in .bb file, skipping file!
geninfo: WARNING: cannot find an entry for new.gcov in .bb file, skipping file!
geninfo: WARNING: cannot find an entry for stl_algobase.h.gcov in .bb file, skipping file!
geninfo: WARNING: cannot find an entry for stl_alloc.h.gcov in .bb file, skipping file!
geninfo: WARNING: cannot find an entry for stl_construct.h.gcov in .bb file, skipping file!
geninfo: WARNING: cannot find an entry for stl_iterator.h.gcov in .bb file, skipping file!
geninfo: WARNING: cannot find an entry for stl_uninitialized.h.gcov in .bb file, skipping file!
geninfo: WARNING: cannot find an entry for stl_vector.h.gcov in .bb file, skipping file!
geninfo: WARNING: no data found for /usr/include/c++/3.2.2/iostream
geninfo: WARNING: no data found for /usr/include/c++/3.2.2/bits/stl_algobase.h
geninfo: WARNING: no data found for /usr/include/c++/3.2.2/bits/stl_vector.h
geninfo: WARNING: no data found for /usr/include/c++/3.2.2/bits/stl_alloc.h
geninfo: WARNING: no data found for /usr/include/c++/3.2.2/bits/stl_construct.h
geninfo: WARNING: no data found for /usr/include/c++/3.2.2/bits/stl_iterator.h
geninfo: WARNING: no data found for /usr/include/c++/3.2.2/bits/stl_uninitialized.h
geninfo: WARNING: no data found for /usr/include/c++/3.2.2/new
Finished .info-file creation
[lixiang@localhost cjj]$ ls
app.info test voctorTest.bbg voctorTest.da
ctest.c voctorTest.bb voctorTest.cpp
[lixiang@localhost cjj]$ cat app.info
TN:
SF:/home/lixiang/file/addpe/cjj/voctorTest.cpp
DA:5,1
DA:6,3
DA:7,1
DA:8,1
DA:9,1
DA:10,1
DA:11,1
DA:12,1
LF:8
LH:8
end_of_record
[lixiang@localhost cjj]$ genhtml app.info -o cc_result
Reading data file app.info
Found 1 entries.
Found common filename prefix "/home/lixiang/file/addpe"
Writing .css and .png files.
Generating output.
Processing file cjj/voctorTest.cpp
Writing directory view page.
Overall coverage rate:
lines......: 100.0% (8 of 8 lines)
functions..: no data found
[lixiang@localhost cjj]$

 

其中:

lcov:我想查看下LCov是否安装成功。(证明了在root下安装的软件可以在普通用户下使用。但是注意,我安装的时候用普通用户的保存的安装包好像有点问题。)

lcov -c -d ./ -o app.info:这条命令是创建覆盖率中间文件app.info 该文件可以用cat、vi查看。前提该文件夹下有gcov后缀的文件存在,即该文件是生成覆盖率信息的文件夹下。

genhtml app.info -o cc_result:通过lcov自带的genhtml转换为html文件并放在cc_result文件夹下。

获取到的覆盖率结果如下:

Linux覆盖率一点研究:获取覆盖率数据

 

 

总结获取覆盖率步骤:

      1. 编译链接带覆盖率参数的源代码;

  2. 运行测试程序;

  3. 使用gcov获取文本形式的覆盖率数据;

  4. 使用lcov获取html形式的覆盖率数据;

 

参考了这篇文章:

http://blog.163.com/bobile45@126/blog/static/96061992201382025729313/

http://linux.ctocio.com.cn/85/12444085.shtml

   

  

原文链接: https://www.cnblogs.com/MyNameIsMT/p/3591007.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    Linux覆盖率一点研究:获取覆盖率数据

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/122644

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月10日 下午7:38
下一篇 2023年2月10日 下午7:47

相关推荐