GCOV简易使用

在工作中如果涉及到代码的覆盖率,那么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-test$ 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就可以查看详细的命中率情况了

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值