您的位置:首页 > 运维架构 > Linux

GCOV简易使用

2015-11-01 14:49 507 查看
在工作中如果涉及到代码的覆盖率,那么gcov是很好的选择,同时可以配合lcov和gcov。下面就对这个星期使用的gcov做个小小的总结

GCOV概述

gcov is a test coverage program. Use it in concert with GCC to analyze your programs to help create more efficient, faster running code and to discover untested parts of your program.

You can use gcov as a profiling tool to help discover where your optimization efforts will best affect your code. You can also use gcov along with the other profiling tool, gprof, to assess which parts of your code use the greatest amount of computing time.

GCOV使用

编译

When using gcov, you must first compile your program with two special GCC options: ‘-fprofile-arcs -ftest-coverage’.

This tells the compiler to generate additional information needed by gcov (basically a flow graph of the program) and also includes additional code in the object files for generating the extra profiling information needed by gcov. These additional files are placed in the directory where the object file is located.

编译时加上-fprofile-arcs -ftest-coverage选项,这样编译后结果会生成***.gcno文件。之后,运行生成的可执行文件,会生成***.gcda文件。最后通过gcov命令,会生成***.c.gcov(可执行程序执行过程中调用的所有文件,会生成相应的gcov),其中包括覆盖率的详细信息。

The .gcno notes file is generated when the source file is compiled with the GCC -ftest-coverage option. It contains information to reconstruct the basic block graphs and assign source line numbers to blocks.

The .gcda count data file is generated when a program containing object files built with the GCC -fprofile-arcs option is executed. A separate .gcda file is created for each object file compiled with this option. It contains arc transition counts, value profile counts, and some summary information.

tingshuai@yantingshuaideMacBook-Air ~/project/gcov-test$ gcc -fprofile-arcs -ftest-coverage test.c
tingshuaideMacBook-Air ~/project/gcov-test$ ls
test      test.c     test.gcno
tingshuai@yantingshuaideMacBook-Air ~/project/gcov-test$ ./test
tingshuaideMacBook-Air ~/project/gcov-test$ ls
test      test.c    test.gcda test.gcno
tingshuai@yantingshuaideMacBook-Air ~/project/gcov-test$ gcov test.c
File 'test.c'
Lines executed:77.78% of 9
test.c:creating 'test.c.gcov'
tingshuai@yantingshuaideMacBook-Air ~/project/gcov-t
4000
est$ cat test.c.gcov
-:    0:Source:test.c
-:    0:Graph:test.gcno
-:    0:Data:test.gcda
-:    0:Runs:1
-:    0:Programs:1
-:    1:#include<stdio.h>
-:    2:
-:    3:int main(int argc, char *argv[])
-:    4:{
1:    5:  int i = 0;
202:    6:  for (i = 0; i < 100; i++) {
100:    7:    printf("add i atomic");
100:    8:  }
-:    9:
1:   10:  if (i > 200) {
#####:   11:    printf("i is bigger than 200");
#####:   12:  } else {
1:   13:    printf("is is less then 200");
-:   14:  }
1:   15:  return 0;
-:   16:}


通过test.c.gcov就可以查看详细的命中率情况了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  代码覆盖率 linux gcov