How do I generate coverage reports for fork()'

2019-04-10 11:39发布

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条回答
萌系小妹纸
2楼-- · 2019-04-10 11:42

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.

查看更多
登录 后发表回答