gcov的覆盖率统计

gcov的覆盖率统计

程序代码

先写个程序测试用例

#include <stdio.h>
#include <string.h>

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 <stdio.h>
        -:    2:#include <string.h>
        -:    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

这是正常合并的内容。

编译器

持续更新

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小范今天学Java了嘛?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值