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