如何使用gcov/lcov为fork()的孩子生成覆盖率报告? [英] How do I generate coverage reports for fork()'d children using gcov/lcov?

查看:347
本文介绍了如何使用gcov/lcov为fork()的孩子生成覆盖率报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为我的一个项目生成覆盖率报告时遇到了麻烦-似乎从未触及到分叉后的子进程中的行,尽管它们显然是现实的.

I'm having trouble generating coverage reports for a project of mine -- it seems that the lines in the child process after a fork are never hit, althought they clearly are in reality.

这是分叉的工作服报告部分(与lcov + genhtml的结果相同),以及构建日志

Here is the coveralls report of the forking part (The results are the same with lcov+genhtml), and the build logs.

该项目使用带有libtool的自动工具进行构建,并将所有内容打包为静态库. ( configure.ac

The project uses autotools with libtool to build, and packs everything as a static library. (configure.ac, library makefile.am, tests makefile.am)

我尝试将覆盖率标志添加到测试中,并在CFLAGS中添加--coverage,但无济于事.

I tried to add the coverage flags to the tests, and --coverage in CFLAGS, but to no avail.

最让我烦恼的是,我试图在一个简单的C文件上重现该行为,如下所示:

What bugs me most is that I tried to reproduce the behaviour on a simple C file, as follows:

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>

int main(void)
{
    pid_t pid;
    if (!(pid = fork())) {
        puts("In child");
    } else {
        puts("In parent");
        waitpid(pid, NULL, 0);
    }
    return 0;
}

通过以下shell会话:

With the following shell session:

/bin/sh ./libtool  --tag=CC   --mode=compile gcc -DHAVE_CONFIG_H -I. -I./src    -Wall -Wextra -Wno-unused-result -Wno-missing-field-initializers -std=gnu99 -fplan9-extensions -I./include/ -I./dependencies/csptr/include/ -O0 --coverage -fprofile-arcs -ftest-coverage -g -O0 -MT test.lo -MD -MP -MF test.Tpo -c -o test.lo test.c
/bin/sh ./libtool  --tag=CC   --mode=link gcc -Wall -Wextra -Wno-unused-result -Wno-missing-field-initializers -std=gnu99 -fplan9-extensions -I./include/ -I./dependencies/csptr/include/ -O0 --coverage -fprofile-arcs -ftest-coverage -g -O0 -lgcov  -o test -rpath /usr/local/lib test.lo
#The two lines above are adapted versions of what autotools with libtool run to compile my project.

./test
mkdir -p coverage
lcov --compat-libtool --directory . --capture --output-file cov.info && genhtml -o coverage cov.info

...但是生成的报告宣布100%的覆盖率.

... but the generated report announce 100% coverage.

怎么了?我的构建被破坏了吗?

What's wrong ? Is my build broken ?

推荐答案

一段时间后,当我重新调查该问题后,便能够对其进行跟踪:

After some time when I reinvestigated the issue, I was able to track it down:

我正在使用_exit()终止子进程,它具有绕过该进程的任何终结处理以及调用__gcov_flush()的属性-这就是为什么我没有得到任何覆盖的原因.

I was using _exit() to terminate the child process, and it has the property of bypassing any finalization on the process, and with it the call to __gcov_flush() -- this is why I wasn't getting any coverage.

这篇关于如何使用gcov/lcov为fork()的孩子生成覆盖率报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆