mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Kirill A. Shutemov" <kirill@shutemov.name>
To: Dmitry Vyukov <dvyukov@google.com>
Cc: akpm@linux-foundation.org, drysdale@google.com,
	keescook@google.com, quentin.casasnovas@oracle.com,
	sasha.levin@oracle.com, vegard.nossum@oracle.com,
	linux-kernel@vger.kernel.org, edumazet@google.com,
	taviso@google.com, bhelgaas@google.com,
	syzkaller@googlegroups.com, kcc@google.com, glider@google.com,
	ryabinin.a.a@gmail.com
Subject: Re: [PATCH v3] kernel: add kcov code coverage
Date: Thu, 14 Jan 2016 18:25:43 +0200	[thread overview]
Message-ID: <20160114162542.GC28386@node.shutemov.name> (raw)
In-Reply-To: <1452781341-34178-1-git-send-email-dvyukov@google.com>

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)?

-- 
 Kirill A. Shutemov

  reply	other threads:[~2016-01-14 16:25 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-14 14:22 Dmitry Vyukov
2016-01-14 16:25 ` Kirill A. Shutemov [this message]
2016-01-14 16:31   ` Dmitry Vyukov
2016-01-15 12:42 ` Kirill A. Shutemov
2016-01-15 14:21   ` Dmitry Vyukov
2016-01-18 11:21     ` Dmitry Vyukov
2016-01-18 12:42 ` Kirill A. Shutemov
2016-01-18 19:28   ` Dmitry Vyukov
2016-01-18 21:52     ` Kirill A. Shutemov
2016-01-19 12:47       ` Dmitry Vyukov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160114162542.GC28386@node.shutemov.name \
    --to=kirill@shutemov.name \
    --cc=akpm@linux-foundation.org \
    --cc=bhelgaas@google.com \
    --cc=drysdale@google.com \
    --cc=dvyukov@google.com \
    --cc=edumazet@google.com \
    --cc=glider@google.com \
    --cc=kcc@google.com \
    --cc=keescook@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=quentin.casasnovas@oracle.com \
    --cc=ryabinin.a.a@gmail.com \
    --cc=sasha.levin@oracle.com \
    --cc=syzkaller@googlegroups.com \
    --cc=taviso@google.com \
    --cc=vegard.nossum@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome