gcov介绍+使用实例
2014-05-21 19:33
183 查看
1、关于gcov工具:
gcov是gnu/gcc工具库中的一个组件,一般来说,都会被安装的
可以用whereis gcov来找一下
gcov伴随gcc 发布。gcc编译加入-fprofile-arcs -ftest-coverage 参数生成二进制程序,执行测试用例生成代码覆盖率信息。
2、gcov是什么?
Gcov is GCC Coverage
是一个测试代码覆盖率的工具
是一个命令行方式的控制台程序
伴随GCC发布,配合GCC共同实现对C/C++文件的语句覆盖和分支覆盖测试;
与程序概要分析工具(profiling tool,例如gprof)一起工作,可以估计程序中哪一段代码最耗时
总的来说:gcov是一个保险测试工具。当构建一个程序时,gcov会监视一个程序的执行,并且会标识出执行了哪一行源码,哪一行没有执行。更进一步,gcov可以标识出某一行源执行的次数,这对于执行配置很有用(程序在哪里花费了大多数的时间)。因为gcov可以分辨出哪一行没有执行,这对于保险测试工具是很有用的。
3、使用gcov的3个阶段
test.c:
(1) 编译
# gcc -fprofile-arcs -ftest-coverage -o test test.c
# ls
test test.c test.gcno
-fprofile-arcs -ftest-coverage告诉编译器生成gcov需要的额外信息,并在目标文件中插入gcov需要的extra
profiling information。因此,该命令在生成可执行文件test的同时生成test.gcno文件(gcov
note文件)。
(2) 收集信息
# ./test
Success
# ls
test test.c test.gcda test.gcno
执行该程序,生成test.gcda文件(gcov
data文件)。
(3) 报告
# gcov test.c
File 'test.c'
Lines executed:87.50% of 8
test.c:creating 'test.c.gcov'
# ls
test test.c test.c.gcov test.gcda test.gcno
生成test.c.gcov文件,该文件记录了每行代码被执行的次数。
test.c.gcov文件内容如下,蓝色表示添加的注释。
-: 0:Source:test.c
-: 0:Graph:test.gcno
-: 0:Data:test.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:#include //前面的数字表明该clause被执行的次数,下同
-: 2:
-: 3:int main (void)
1: 4:{
-: 5: int i, total;
-: 6:
1: 7: total = 0;
-: 8:
11: 9: for (i = 0; i < 10; i++) //前面的数字11表明该clause被执行11次
10: 10: total += i;
-: 11:
1: 12: if (total != 45)
#####: 13: printf ("Failure/n");
-: 14: else
1: 15: printf ("Success/n");
1: 16: return 0;
-: 17:}
-: 18:
gcov的选项
gcov的选项不多,也好理解,此处选3个典型的选项并结合例子加以说明。
(1) -a, --all-blocks
在.gcov文件中输出每个基本块(basic
block)的执行次数。如果没有-a选项,则输出'main'函数这个block的执行次数,如上所示。使用该选项可以
Write individual execution counts for every basic block. Normally
gcov outputs execution counts onlyfor the main blocks of a line. With
this option you can determine if blocks within a single line are not being executed.
# gcov -a test.c
File 'test.c'
Lines executed:87.50% of 8
test.c:creating 'test.c.gcov'
Test.c.gcov文件内容。
-: 0:Source:test.c
-: 0:Graph:test.gcno
-: 0:Data:test.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:#include
-: 2:
-: 3:int
main (void)
1: 4:{
-: 5: int
i, total;
-: 6:
1: 7: total
= 0;
-: 8:
11: 9: for
(i = 0; i < 10; i++)
1: 9-block 0
10: 9-block 1
11: 9-block 2
10: 10: total
+= i;
-: 11:
1: 12: if
(total != 45)
1: 12-block 0
#####: 13: printf
("Failure/n");
$$$$$: 13-block 0
-: 14: else
1: 15: printf
("Success/n");
1: 15-block 0
1: 16: return
0;
1: 16-block 0
-: 17:}
-: 18:
(2) -b, --branch-probabilities
在.gcov文件中输出每个分支的执行频率,并有分支统计信息。
# gcov -b test.c
File 'test.c'
Lines executed:87.50% of 8
Branches executed:100.00% of 4
Taken at least once:75.00% of 4
Calls executed:50.00% of 2
test.c:creating 'test.c.gcov'
-: 0:Source:test.c
-: 0:Graph:test.gcno
-: 0:Data:test.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:#include
-: 2:
-: 3:int
main (void)
function main called 1 returned 100% blocks executed 86%
1: 4:{
-: 5: int
i, total;
-: 6:
1: 7: total
= 0;
-: 8:
11: 9: for
(i = 0; i < 10; i++)
branch 0 taken 91%
branch 1 taken 9% (fallthrough)
10: 10: total
+= i;
-: 11:
1: 12: if
(total != 45)
branch 0 taken 0% (fallthrough)
branch 1 taken 100%
#####: 13: printf
("Failure/n");
call 0 never executed
-: 14: else
1: 15: printf
("Success/n");
call 0 returned 100%
1: 16: return
0;
-: 17:}
-: 18:
(3) -c, --branch-counts
在.gcov文件中输出每个分支的执行次数。
# gcov -c test.c
File 'test.c'
Lines executed:87.50% of 8
test.c:creating 'test.c.gcov'
-c是默认选项,其结果与"gcov
test.c"执行结果相同。
参考:
/article/1451072.html
http://gcc.gnu.org/onlinedocs/gcc/Gcov.html
http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html
http://dev.firnow.com/course/6_system/linux/Linuxjs/20071129/88999.html
gcov是gnu/gcc工具库中的一个组件,一般来说,都会被安装的
可以用whereis gcov来找一下
gcov伴随gcc 发布。gcc编译加入-fprofile-arcs -ftest-coverage 参数生成二进制程序,执行测试用例生成代码覆盖率信息。
2、gcov是什么?
Gcov is GCC Coverage
是一个测试代码覆盖率的工具
是一个命令行方式的控制台程序
伴随GCC发布,配合GCC共同实现对C/C++文件的语句覆盖和分支覆盖测试;
与程序概要分析工具(profiling tool,例如gprof)一起工作,可以估计程序中哪一段代码最耗时
总的来说:gcov是一个保险测试工具。当构建一个程序时,gcov会监视一个程序的执行,并且会标识出执行了哪一行源码,哪一行没有执行。更进一步,gcov可以标识出某一行源执行的次数,这对于执行配置很有用(程序在哪里花费了大多数的时间)。因为gcov可以分辨出哪一行没有执行,这对于保险测试工具是很有用的。
3、使用gcov的3个阶段
test.c:
#include<stdio.h> int main(void) { int i,total; total=0; for(i=0;i<10;i++) total+=i; if (total!=45) printf("failure\n"); else printf("success\n"); return 0; }
(1) 编译
# gcc -fprofile-arcs -ftest-coverage -o test test.c
# ls
test test.c test.gcno
-fprofile-arcs -ftest-coverage告诉编译器生成gcov需要的额外信息,并在目标文件中插入gcov需要的extra
profiling information。因此,该命令在生成可执行文件test的同时生成test.gcno文件(gcov
note文件)。
(2) 收集信息
# ./test
Success
# ls
test test.c test.gcda test.gcno
执行该程序,生成test.gcda文件(gcov
data文件)。
(3) 报告
# gcov test.c
File 'test.c'
Lines executed:87.50% of 8
test.c:creating 'test.c.gcov'
# ls
test test.c test.c.gcov test.gcda test.gcno
生成test.c.gcov文件,该文件记录了每行代码被执行的次数。
test.c.gcov文件内容如下,蓝色表示添加的注释。
-: 0:Source:test.c
-: 0:Graph:test.gcno
-: 0:Data:test.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:#include //前面的数字表明该clause被执行的次数,下同
-: 2:
-: 3:int main (void)
1: 4:{
-: 5: int i, total;
-: 6:
1: 7: total = 0;
-: 8:
11: 9: for (i = 0; i < 10; i++) //前面的数字11表明该clause被执行11次
10: 10: total += i;
-: 11:
1: 12: if (total != 45)
#####: 13: printf ("Failure/n");
-: 14: else
1: 15: printf ("Success/n");
1: 16: return 0;
-: 17:}
-: 18:
gcov的选项
gcov的选项不多,也好理解,此处选3个典型的选项并结合例子加以说明。
(1) -a, --all-blocks
在.gcov文件中输出每个基本块(basic
block)的执行次数。如果没有-a选项,则输出'main'函数这个block的执行次数,如上所示。使用该选项可以
Write individual execution counts for every basic block. Normally
gcov outputs execution counts onlyfor the main blocks of a line. With
this option you can determine if blocks within a single line are not being executed.
# gcov -a test.c
File 'test.c'
Lines executed:87.50% of 8
test.c:creating 'test.c.gcov'
Test.c.gcov文件内容。
-: 0:Source:test.c
-: 0:Graph:test.gcno
-: 0:Data:test.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:#include
-: 2:
-: 3:int
main (void)
1: 4:{
-: 5: int
i, total;
-: 6:
1: 7: total
= 0;
-: 8:
11: 9: for
(i = 0; i < 10; i++)
1: 9-block 0
10: 9-block 1
11: 9-block 2
10: 10: total
+= i;
-: 11:
1: 12: if
(total != 45)
1: 12-block 0
#####: 13: printf
("Failure/n");
$$$$$: 13-block 0
-: 14: else
1: 15: printf
("Success/n");
1: 15-block 0
1: 16: return
0;
1: 16-block 0
-: 17:}
-: 18:
(2) -b, --branch-probabilities
在.gcov文件中输出每个分支的执行频率,并有分支统计信息。
# gcov -b test.c
File 'test.c'
Lines executed:87.50% of 8
Branches executed:100.00% of 4
Taken at least once:75.00% of 4
Calls executed:50.00% of 2
test.c:creating 'test.c.gcov'
-: 0:Source:test.c
-: 0:Graph:test.gcno
-: 0:Data:test.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:#include
-: 2:
-: 3:int
main (void)
function main called 1 returned 100% blocks executed 86%
1: 4:{
-: 5: int
i, total;
-: 6:
1: 7: total
= 0;
-: 8:
11: 9: for
(i = 0; i < 10; i++)
branch 0 taken 91%
branch 1 taken 9% (fallthrough)
10: 10: total
+= i;
-: 11:
1: 12: if
(total != 45)
branch 0 taken 0% (fallthrough)
branch 1 taken 100%
#####: 13: printf
("Failure/n");
call 0 never executed
-: 14: else
1: 15: printf
("Success/n");
call 0 returned 100%
1: 16: return
0;
-: 17:}
-: 18:
(3) -c, --branch-counts
在.gcov文件中输出每个分支的执行次数。
# gcov -c test.c
File 'test.c'
Lines executed:87.50% of 8
test.c:creating 'test.c.gcov'
-c是默认选项,其结果与"gcov
test.c"执行结果相同。
参考:
/article/1451072.html
http://gcc.gnu.org/onlinedocs/gcc/Gcov.html
http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html
http://dev.firnow.com/course/6_system/linux/Linuxjs/20071129/88999.html
相关文章推荐
- Web Service介绍使用及实例-C#.Net
- 实例介绍joomla! xml解析器的使用 【转】
- 监视和截获指定进程网络数据传输WSockExpert0.6 工具介绍和使用实例
- Linux Shell 通配符、元字符、转义符使用实例介绍
- 2011 wireshark 抓包工具 使用说明 实用过滤表达式(针对ip、协议、端口、长度和内容) 实例介绍
- JDK5的ProcessBuilder介绍和使用实例
- Linux Shell 正则表达式之 通配符、元字符、转义符使用实例介绍
- Linux Shell 通配符、元字符、转义符使用实例介绍
- Linux Shell 通配符、元字符、转义符使用实例介绍
- Linux Shell 通配符、元字符、转义符,正则表达式使用实例介绍
- 以实例简单介绍如何使用SVN
- Delphi接口使用实例介绍
- Linux Shell 通配符、元字符、转义符使用实例介绍
- Android中抽屉(SlidingDrawer)的使用介绍及实例记录
- Linux Shell 通配符、元字符、转义符使用实例介绍
- Web Service介绍使用及实例-C#.Net
- Linux Shell 通配符、元字符、转义符使用实例介绍
- Android中抽屉(SlidingDrawer)的使用介绍及实例记录
- 实例介绍如何配置使用 VMControl 多网络支持功能
- Linux Shell 通配符、元字符、转义符使用实例介绍