From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755198AbcANQZr (ORCPT ); Thu, 14 Jan 2016 11:25:47 -0500 Received: from mail-wm0-f68.google.com ([74.125.82.68]:35269 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752974AbcANQZq (ORCPT ); Thu, 14 Jan 2016 11:25:46 -0500 Date: Thu, 14 Jan 2016 18:25:43 +0200 From: "Kirill A. Shutemov" To: Dmitry Vyukov 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 Message-ID: <20160114162542.GC28386@node.shutemov.name> References: <1452781341-34178-1-git-send-email-dvyukov@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1452781341-34178-1-git-send-email-dvyukov@google.com> User-Agent: Mutt/1.5.23.1 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#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