gcov和lcov代码覆盖率工具

本文介绍了代码覆盖率工具GCOV和LCOV的使用方法。GCOV是GCC编译器自带的代码覆盖率工具,通过-fprofile-arcs -ftest-coverage选项生成覆盖率信息。LCOV则是GCOV结果的前端,可以将覆盖率信息转换成HTML展示。文章详细讲解了GCOV的使用,包括如何查看分支覆盖率、生成覆盖率文件以及清理数据。同时,还介绍了如何结合LCOV生成HTML报告,便于分析代码覆盖率。此外,文章还提到了Linux内核的覆盖率测试,并给出了获取特定程序运行时段内核覆盖率的步骤。
摘要由CSDN通过智能技术生成

1.GCOV查看arm-linux代码覆盖率

一、           关于gcov工具

gcov伴随gcc 发布。gcc编译加入-fprofile-arcs -ftest-coverage 参数生成二进制程序,执行测试用例生成代码覆盖率信息。
1、如何使用gcov

用GCC编译的时候加上-fprofile-arcs -ftest-coverage选项,链接的时候也加上。
fprofile-arcs参数使gcc创建一个程序的流图,之后找到适合图的生成树。只有不在生成树中的弧被操纵(instrumented):gcc添加了代码来清点这些弧执行的次数。当这段弧是一个块的唯一出口或入口时,操纵工具代码(instrumentation code)将会添加到块中,否则创建一个基础块来包含操纵工具代码。

gcov主要使用.gcno和.gcda两个文件。
.gcno是由-ftest-coverage产生的,它包含了重建基本块图和相应的块的源码的行号的信息。
.gcda是由加了-fprofile-arcs编译参数的编译后的文件运行所产生的,它包含了弧跳变的次数和其他的概要信息(而gcda只能在程序运行完毕后才能产生的)。
Gcov执行函数覆盖、语句覆盖和分支覆盖。

举个例子,程序代码由main.c和tmp.c两个文件组成,编译、链接、运行程序
编译:gcc -fprofile-arcs -ftest-coverage -o myapp main.c tmp.c
运行:./myapp
然后 输入
命令: gcov main.c,gcov tmp.c

这个时候当前目录下有了新的文档main.c.gcov,和tmp.c.gcov
若想保存覆盖率文件,上述命令修改为:
命令:gcov main.c >>yourfilename,gcov tmp.c >>yourfilename

而这时候的main.c.gcov,和tmp.c.gcov就包含了函数和代码执行次数的信息,我们可以查看结果:
      -:   65:/***************************************************************************************
      -:   66: * name         : main
      -:   67: * return       : 0 OK
      -:   68: *                other ERROR
      -:   69: * history      : 2006-06-13
      -:   70:****************************************************************************************/
      -:   71:int main( int argc, char *argv[] )                                                      /* the entrance for program

*/
function main called 4 returned 100% blocks executed 81%
      4:   72:{
      4:   73:        int loop = 0 ;
      4:   74:        int ret = OK ;
      4:   75:        int empty_line = 0 ;
      4:   76:        int code_line = 0 ;
      4:   77:        int annotation_line = 0 ;
      4:   78:        struct stat file_stat ;                                                         /* use for file state */
      4:   79:        char recu_name[256] ;
      4:   80:        char *pwd = NULL ;
      4:   81:        char *tmp = NULL ;
      -:   82:
      4:   83:        if( argc = MAX_FILE ){                                    /* file size larger than max size */
  #####:   98:                        printf( "file [%s] size is over 64K! /ncontinue..../n", argv[loop] ) ;
  #####:   99:                        continue ;
      -: 100:                }

##### 这就是表示没跑到的

    
各个参数使用如下:     
gcov [-b] [-c] [-v] [-n] [-l] [-f] [-o directory] sourcefile
-b
  Write branch frequencies to the output file, and write branch summary info to the standard output. This option allows you to

see how often each branch in your program was taken.
  //b(ranch),分支测试
-c
  Write branch frequencies as the number of branches taken, rather than the percentage of branches taken.
-v
  Display the gcov version number (on the standard error stream).
  //太简单了吧,我上面用了
-n
  Do not

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值