Re: [PATCH v3] kernel: add kcov code coverage

From: Dmitry Vyukov
Date: Thu Jan 14 2016 - 11:31:56 EST


On Thu, Jan 14, 2016 at 5:25 PM, Kirill A. Shutemov
<kirill@xxxxxxxxxxxxx> wrote:
> On Thu, Jan 14, 2016 at 03:22:21PM +0100, Dmitry Vyukov wrote:
>> +The following program demonstrates kcov usage from within a test program:
>> +
>> +#include <stdio.h>
>> +#include <stddef.h>
>> +#include <stdint.h>
>> +#include <sys/types.h>
>> +#include <sys/stat.h>
>> +#include <sys/ioctl.h>
>> +#include <sys/mman.h>
>> +#include <fcntl.h>
>> +
>> +#define KCOV_INIT_TRACE _IOR('c', 1, unsigned long)
>> +#define KCOV_ENABLE _IO('c', 100)
>> +#define KCOV_DISABLE _IO('c', 101)
>> +#define COVER_SIZE (64<<10)
>> +
>> +int main(int argc, char **argv)
>> +{
>> + int fd;
>> + uint32_t *cover, n, i;
>> +
>> + /* A single fd descriptor allows coverage collection on a single
>> + * thread.
>> + */
>> + fd = open("/sys/kernel/debug/kcov", O_RDWR);
>> + if (fd == -1)
>> + perror("open");
>> + /* Setup trace mode and trace size. */
>> + if (ioctl(fd, KCOV_INIT_TRACE, COVER_SIZE))
>> + perror("ioctl");
>> + /* Mmap buffer shared between kernel- and user-space. */
>> + cover = (uint32_t*)mmap(NULL, COVER_SIZE * sizeof(uint32_t),
>> + PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
>> + if ((void*)cover == MAP_FAILED)
>> + perror("mmap");
>> + /* Enable coverage collection on the current thread. */
>> + if (ioctl(fd, KCOV_ENABLE, 0))
>> + perror("ioctl");
>> + /* Reset coverage from the tail of the ioctl() call. */
>> + __atomic_store_n(&cover[0], 0, __ATOMIC_RELAXED);
>> + /* That's the target syscal call. */
>> + read(-1, NULL, 0);
>> + /* Read number of PCs collected. */
>> + n = __atomic_load_n(&cover[0], __ATOMIC_RELAXED);
>> + /* PCs are shorten to uint32_t, so we need to restore the upper part. */
>> + for (i = 0; i < n; i++)
>> + printf("0xffffffff%0lx\n", (unsigned long)cover[i + 1]);
>> + /* Disable coverage collection for the current thread. After this call
>> + * coverage can be enabled for a different thread.
>> + */
>> + if (ioctl(fd, KCOV_DISABLE, 0))
>> + perror("ioctl");
>> + /* Free resources. */
>> + if (munmap(cover, COVER_SIZE * sizeof(uint32_t)))
>> + perror("munmap");
>> + if (close(fd))
>> + perror("close");
>> + return 0;
>> +}
>
> Do we really need ioctl in this interface? Why not just plain write(2)?

Initially I had writes (similar to kmemleak interface). Quentin's
original patch used ioctl's and he said that ioctls are cleaner (no
string formatting and parsing). Note that as opposed to kmemleak, kcov
is not supposed to be used from command line. I don't care too much
either way. I guess this is matter of personal preferences to some
degree and there are precedents of both ioctls and writes...