How do I generate coverage reports for fork()'d children using gcov/lcov?

我的未来我决定 提交于 2019-12-06 03:44:08

问题


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.

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

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

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

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;
}

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

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

What's wrong ? Is my build broken ?


回答1:


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

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.



来源:https://stackoverflow.com/questions/28408811/how-do-i-generate-coverage-reports-for-forkd-children-using-gcov-lcov

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!