Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 817
Snaipe Avatar asked Feb 09 '15 11:02

Snaipe


People also ask

What is difference between gcov and LCOV?

Lcov is a graphical front-end for gcov. It collects gcov data for multiple source files and creates HTML pages containing the source code annotated with coverage information. It also adds overview pages for easy navigation within the file structure. Lcov supports statement, function, and branch coverage measurement.


1 Answers

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.

like image 186
Snaipe Avatar answered Sep 17 '22 23:09

Snaipe