`

C/C++代码覆盖工具gcov与lcov入门

 
阅读更多

代码覆盖率——gcov lcov的使用

2008-07-25 15:57

一、关于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编译参数的编译后的文件运行所产生的,它包含了弧跳变的次数和其他的概要信息。
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

查看结果:
        -:   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 create the gcov output file.
-l
    Create long file names for included source files. For example, if the header file `x.h' contains code, and was included in the

file `a.c', then running gcov on the file `a.c' will produce an output file called `a.c.x.h.gcov' instead of `x.h.gcov'. This can

be useful if `x.h' is included in multiple source files.
-f
    Output summaries for each function in addition to the file level summary.
-o
    The directory where the object files live. Gcov will search for `.bb', `.bbg', and `.da' files in this directory. 
新版的是这么说的
     -o directory│file
       --object-directory directory
       --object-file file
           Specify either the directory containing the gcov data files, or the
           object path name. The .gcno, and .gcda data files are searched for
           using this option. If a directory is specified, the data files are
           in that directory and named after the source file name, without its
           extension. If a file is specified here, the data files are named
           after that file, without its extension. If this option is not sup-
           plied, it defaults to the current directory.
其他的更有新版的-u,
     -u
       --unconditional-branches
           When branch counts are given, include those of unconditional
           branches. Unconditional branches are normally not interesting.
      -p
       --preserve-paths
           Preserve complete path information in the names of generated .gcov
           files. Without this option, just the filename component is used.
           With this option, all directories are used, with ’/’ characters
           translated to ’#’ characters, ’.’ directory components removed and
           ’..’ components renamed to ’^’. This is useful if sourcefiles are
           in several different directories. It also affects the -l option.
  

二、关于lcov

Lcov则是上的gcov 结果展现的一个前端,可以将覆盖率信息转换成html展现。
1、如何使用lcov
Makefile 在编译和link环节都加入 -fprofile-arcs -ftest-coverage 选项     

收集覆盖率数据生成app.info文件
命令:cov --directory .   --capture --output-file myapp.info
Capturing coverage data from .
Found gcov version: 3.4.6
Scanning . for .gcda files ...
Found 1 data files in .
Processing ./TestQuery.gcda
Finished .info-file creation

转换成html格式
命令:genhtml -o results app.info 
Reading data file app.info
Found 18 entries.
Found common filename prefix "/home/search/isearch_yb/src"
Writing .css and .png files.
Generating output.
Processing file cpp/core/basis/GlobalDef.h
Processing file cpp/core/search/QueryCache.h
...
Writing directory view page.
Overall coverage rate: 117 of 514 lines (22.8%)

2、查看html文件
html包含代码覆盖的详细信息

更多命令选项

http://ltp.sourceforge.net/coverage/lcov/lcov.1.php?PHPSESSID=26d7173d1f492f5f691715ef8b7d0b40

参考资料
1、http://ltp.sourceforge.net/coverage/

一、关于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编译参数的编译后的文件运行所产生的,它包含了弧跳变的次数和其他的概要信息。
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

查看结果:
        -:   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 create the gcov output file.
-l
    Create long file names for included source files. For example, if the header file `x.h' contains code, and was included in the

file `a.c', then running gcov on the file `a.c' will produce an output file called `a.c.x.h.gcov' instead of `x.h.gcov'. This can

be useful if `x.h' is included in multiple source files.
-f
    Output summaries for each function in addition to the file level summary.
-o
    The directory where the object files live. Gcov will search for `.bb', `.bbg', and `.da' files in this directory. 
新版的是这么说的
     -o directory│file
       --object-directory directory
       --object-file file
           Specify either the directory containing the gcov data files, or the
           object path name. The .gcno, and .gcda data files are searched for
           using this option. If a directory is specified, the data files are
           in that directory and named after the source file name, without its
           extension. If a file is specified here, the data files are named
           after that file, without its extension. If this option is not sup-
           plied, it defaults to the current directory.
其他的更有新版的-u,
     -u
       --unconditional-branches
           When branch counts are given, include those of unconditional
           branches. Unconditional branches are normally not interesting.
      -p
       --preserve-paths
           Preserve complete path information in the names of generated .gcov
           files. Without this option, just the filename component is used.
           With this option, all directories are used, with ’/’ characters
           translated to ’#’ characters, ’.’ directory components removed and
           ’..’ components renamed to ’^’. This is useful if sourcefiles are
           in several different directories. It also affects the -l option.
  

二、关于lcov

Lcov则是上的gcov 结果展现的一个前端,可以将覆盖率信息转换成html展现。
1、如何使用lcov
Makefile 在编译和link环节都加入 -fprofile-arcs -ftest-coverage 选项     

收集覆盖率数据生成app.info文件
命令:cov --directory .   --capture --output-file myapp.info
Capturing coverage data from .
Found gcov version: 3.4.6
Scanning . for .gcda files ...
Found 1 data files in .
Processing ./TestQuery.gcda
Finished .info-file creation

转换成html格式
命令:genhtml -o results app.info 
Reading data file app.info
Found 18 entries.
Found common filename prefix "/home/search/isearch_yb/src"
Writing .css and .png files.
Generating output.
Processing file cpp/core/basis/GlobalDef.h
Processing file cpp/core/search/QueryCache.h
...
Writing directory view page.
Overall coverage rate: 117 of 514 lines (22.8%)

2、查看html文件
html包含代码覆盖的详细信息

更多命令选项

http://ltp.sourceforge.net/coverage/lcov/lcov.1.php?PHPSESSID=26d7173d1f492f5f691715ef8b7d0b40

参考资料
1、http://ltp.sourceforge.net/coverage/

 

转自:http://hi.baidu.com/kbbvtoenhjbfhnd/item/14d7c02e464dee0977272cfa

分享到:
评论

相关推荐

    OpenHarmony覆盖率测试,gcov及lcov的原理和使用简介

    采用覆盖率驱动的验证方式可以量化验证进度,保证验证的完备性。本文主要介绍了openharmony代码和分支覆盖率测试中所使用的工具(即gcov及lcov)的简介、工作原理及使用方法。

    gcov_lcov 演练工程源码

    一个gcov和lcov使用和练习的源码,仅用来记录自我学习和使用gcov的过程和经验,以便后续开发中可以借鉴。

    C/C++覆盖率在NGINX测试中的应用

    C/C++程序的代码覆盖率统计工具非常少,与JAVA相比开源免费的工具更是寥寥无几,好用又开源的简直是凤毛麟角。左挑右选最后看中了基于GCOV的LCOV作为NGINX测试的覆盖率统计工具。选择LCOV的原因很简单:一是适合GCOV...

    使用gcov完成代码覆盖率的测试

    使用gcov完成代码覆盖率的测试.Gcov作为gnu/gcc工作组件之一,是一款的免费的代码覆盖率测试工具,而且可以结合lcov生成美观的html的测试报表。本文介绍一些gcov的使用方法,基本原理,一些实际中可能会遇到的问题...

    lcov for linux gcov

    lcov for linux gcovlcov for linux gcovlcov for linux gcovlcov for linux gcovlcov for linux gcovlcov for linux gcov

    使用gcc/gcov生成代码覆盖率报告

    使用gcc/gcov生成代码覆盖率报告-源码

    linux gcov 工具详细介绍

    • 伴随GCC发布,配合GCC共同实现对C/C++文件的语句覆盖和分支覆盖测试; • 与程序概要分析工具(profiling tool,例如gprof)一起工作,可以估计程序中哪一段代码最耗时; 注:程序概要分析工具是分析代码性能的工具

    gcov-tools-1.0.tar.gz

    该程序是从gcc-4.1.2源代码中抽取出来的与gcov和gcov-dump相关的文件组成的小项目,包含两个工具gcov和gcov-dump。希望能对通过gcc覆盖率测试工具GCOV进行覆盖率测试相关工作的同行有些许的帮助。 其中,gcov的输入...

    gcov命令 测试代码覆盖率

    gcov命令是一款测试程序的代码覆盖率的工具。 gcov可以统计每一行代码的执行频率,实际上哪些代码确实被执行了,每一段代码的执行时间。 gcov只在使用GCC编译的代码上工作。它与任何其他概要或测试覆盖机制不兼容。...

    gcov-1.0.tar.gz

    该程序是从gcc-4.1.2源代码中抽取出来的与gcov相关的文件组成的小项目。希望能对通过gcc覆盖率测试工具GCOV进行覆盖率测试相关工作的同行有些许的帮助。 gcov的输入是一个.c文件,前提是已经编译生成了.gcno文件并...

    如何使用lcov生成diff代码覆盖率报告

    lcov是建立在gcov之上的一个可以生成html代码覆盖率报告的工具,最近公司开始尝试引入代码覆盖来提高产品质量,lcov很好地满足了我们的需求,虽然lcov本身支持生成代码覆盖率的diff报告,但是跟我们的需求不太符合。...

    lcov-1.14 tools

    /=========================/ linux下配合gcov使用的可视化工具lcov。

    cpp-gcov-lcov-test

    Kiss-cpp-makefile-template Kiss C ++ Makefile模板结构-(保持简短) 对于那些厌倦了发布超级复杂的makefile却一无所获的人...

    CodeCoverageExampleQt:在 Mac OS 上使用 Qt 5 和 GCOV 的代码覆盖率

    在 Mac OS 上使用 Qt 5 和 GCOV 的代码覆盖率 这是如何在 Mac OS 上使用 Qt 5 和 GCOV 生成代码覆盖率的示例。 您可以在上阅读整个教程。

    lcov-1.10.tar.gz

    lcov,配合gcov可以进行图形化的代码覆盖率查看

    afl-cov, 使用afl模糊测试案例生成gcov的代码覆盖率结果.zip

    afl-cov, 使用afl模糊测试案例生成gcov的代码覆盖率结果 AFL Fuzzing Fuzzing Fuzzing Fuzzing简介afl-cov 使用 AFL fuzzer afl-fuzz 生成的测试用例文件为目标二进制文件生成gcov代码覆盖率结果。 为了确定AFL的新...

    gcov-dump-1.0.tar.gz

    该程序是从gcc-4.1.2源代码中抽取出来的与gcov-dump相关的文件组成的小项目。希望能对通过gcc覆盖率测试工具GCOV进行覆盖率测试相关工作的同行有些许的帮助。 gcov-dump是一个dump程序,输入是一个gcov的文件,或者....

    lcov的源代码,可以生称html文件

    lcov,可以将gcov生成的测试结果转换为html

    dexcov:并排生成Dextool Mutate与LCOV

    生成突变(C / C ++突变测试)与 (C / C ++代码覆盖率)并排演示的工具 示例1:原始主题 $ dexcov example/lcov_html example/dextool_html -o example/out $ xdg-open example/out/lz4.c.html 示例2:LCOV主题 $ ...

Global site tag (gtag.js) - Google Analytics