Open () wrapper for unit testing will make gcov unable to open the gcda file

I use testing for some C program. To do this, I use GCC -Wl,--wrap=opento mock the stdlib function open()and verify that it was called with the correct parameters.

However, gcov has some problems writing the file .gcda. I assume that the layout that I define is used not only by my tests, but also by gcov. Here is a small example of how to reproduce this:

#include <stdio.h>

int __wrap_open(const char *path, int flags, int mode)
{
    printf("hello from __wrap_open\n");
    return -1;
}


int main(void)
{
    return 0;
}

And compile it with gcc main.c -Wl,--wrap=open -fprofile-arcs -ftest-coverage -lgcov. To simplify a simple example, I removed the module testing part with CMocka to show the error I am encountering.

a.out GCC gcov 6.3.0:

$ ./a.out
hello from __wrap_open
hello from __wrap_open
profiling:/home/romain/wrap-bug/main.gcda:Cannot open

open() gcov ? , gcov __real_open()?

+4
1

:

  • , , open(),
  • , __real_open()

.

int __real_open(const char *path, int flags, int mode);

int __wrap_open(const char *path, int flags, int mode)
{
    if (strlen(path) > 5 && !strcmp(path + strlen(path) - 5, ".gcda"))
        return __real_open(path, flags, mode);
    printf("hello from __wrap_open\n");
    return -1;
}

, , , , , .

+3

Source: https://habr.com/ru/post/1689001/


All Articles