gcov的覆盖率统计
创始人
2025-05-28 09:13:51

gcov的覆盖率统计

程序代码

先写个程序测试用例

#include 
#include int main(int argc, char *argv[])
{   if (argc >=2) {printf("=====argc>=2\n");return 1;}printf("helloworld begin\n");if (argc <2){printf("=====argc<2\n");return 2;}return 3;
}

执行:

gcc -fprofile-arcs -ftest-coverage helloworld_gcov.c

-fprofile-arcs -ftest-coverage告诉编译器生成gcov需要的额外信息,并在目标文件中插入gcov需要的extra profiling information。因此,该命令在生成可执行文件的同时生成gcov note文件(helloworld_gcov.gcno)。

收集更多信息指标

./a.out

数据报告

gcov helloworld_gcov.c

输出:

File 'helloworld_gcov.c'
Lines executed:66.67% of 9
Creating 'helloworld_gcov.c.gcov'

会生成新的文件 helloworld_gcov.c.gcov

        -:    0:Source:helloworld_gcov.c-:    0:Graph:helloworld_gcov.gcno-:    0:Data:helloworld_gcov.gcda-:    0:Runs:1-:    1:#include -:    2:#include -:    3:1:    4:int main(int argc, char *argv[])-:    5:{   1:    6:    if (argc >=2) {#####:    7:        printf("=====argc>=2\n");#####:    8:        return 1;-:    9:    }1:   10:    printf("helloworld begin\n");-:   11:1:   12:    if (argc <2){1:   13:        printf("=====argc<2\n");1:   14:        return 2;-:   15:    }#####:   16:    return 3;-:   17:}

其中#####表示未运行的行
每行前面的数字表示行运行的次数

文件名及含义:
helloworld_gcov.gcda 生成gcov data文件
helloworld_gcov.s 汇编语言源程序; 操作: 汇编
helloworld_gcov.c.gcov 可视化执行代码报告
helloworld_gcov.gcno .gcno是由-ftest-coverage产生的,它包含了重建基本块图和相应的块的源码的行号的信息

可视化及覆盖率

apt install lcov -y
或者
https://github.com/linux-test-project/lcov
make install
lcov --capture --directory . --output-file coverage.inf  genhtml coverage.info --output-directory out

image-20230315115827842

image-20230315115737098

合并覆盖率统计信息

./a.out i 4 执行的时候传个参数 程序执行的路径就会发生变化

lcov -c -d . o coverage2.info 生成新的info文件

genhtml -o 2 coverage2.info 生成新的html

genhtml -o combine coverage.info coverage2.info 进行合并info文件覆盖率

下图分别是1、2、合并。共三张图

image-20230315115737098

image-20230315121057205

image-20230315121107890

看了上图不知道你有没有发现啥异常,提示一下 我不能在同一文件夹下进行操作,必须加上rm helloworld_gcov.gcda 保障生成的是新的。

不信我再测试一下,不删除的情况下,进行生成info(更换参数)

./a.out i 1

image-20230315121621090

如果删除了gcda文件之后就正常了 (重在理解 不可照搬)

image-20230315133350616

image-20230315133908768

这是正常合并的内容。

编译器

持续更新

相关内容

热门资讯

守护最北的日出星辰 转自:成都日报锦观江水深沉 江湾静谧守护最北的日出星辰 游客在漠河“神州北极”碑前打卡拍照,碑...
万华化学福建码头公司增资至10... 经济观察网 天眼查App显示,12月16日,万华化学(福建)码头有限公司发生工商变更,注册资本由4亿...
文旅“融”光焕发 共绘“诗与... ●兵团日报全媒体记者 姜蒙 通讯员 张培丽 胥强 仲冬时节,寒意渐浓,五师八十三团十连...
情暖援疆路 医心护健康 ●蔡伟 “医生,我的胃老是不舒服,吃了东西总不消化。”12月16日,在四师总医院可克达拉市院区...
以行践诺 服务暖心 ………… “我们将以更大力度推动‘万企兴万村’行动走深走实,团结引领兵团广大民营企业发挥优势,...