[PATCH 2/4] Adding support for coverage information

Frediano Ziglio
Fri, 01 Feb 2013 14:48:21 -0000

This patch introduce coverage support to Xen.
Currently it allows to compile Xen with coverage support but there is no
way to extract them.

The declarations came from Linux source files (as you can see from file
headers).

The idea is to have some operations mainly
- get coverage information size
- read coverage information
- reset coverage counters

The op parameter in the hypercall will be the operation.
The uarg parameter is a pointer (used to read size or data).

Linux use a file system to export these information. The information will
be a blob to handle with some tools (as usually tools require a bunch of
files but Xen does not handle files at all). I''ll pack them to make
things
simpler as possible.

These information cannot be put in a specific section (allowing a safe
mapping) as gcc use .rodata, .data, .text and .ctors sections.

I added code to handle constructors used in this case to initialize a
linked list of files.

I excluded %.init.o files as they are used before Xen start and should
not have section like .text or .data.

I used a "coverage" configuration option to mimic the
"debug" one.

Signed-off-by: Frediano Ziglio <frediano.ziglio@citrix.com>
---
 .gitignore                         |    2 +
 Config.mk                          |    4 ++
 xen/Rules.mk                       |    5 ++
 xen/arch/x86/setup.c               |    3 ++
 xen/arch/x86/x86_64/compat/entry.S |    4 ++
 xen/arch/x86/x86_64/entry.S        |    4 ++
 xen/arch/x86/xen.lds.S             |    7 +++
 xen/common/Makefile                |    2 +
 xen/common/gcov/Makefile           |    5 ++
 xen/common/gcov/gcov.c             |  105 ++++++++++++++++++++++++++++++++++++
 xen/common/gcov/nogcov.c           |   22 ++++++++
 xen/include/public/gcov.h          |   93 ++++++++++++++++++++++++++++++++
 xen/include/xen/gcov.h             |   32 +++++++++++
 xen/include/xen/hypercall.h        |    6 +++
 14 files changed, 294 insertions(+)
 create mode 100644 xen/common/gcov/Makefile
 create mode 100644 xen/common/gcov/gcov.c
 create mode 100644 xen/common/gcov/nogcov.c
 create mode 100644 xen/include/public/gcov.h
 create mode 100644 xen/include/xen/gcov.h

diff --git a/.gitignore b/.gitignore
index 462e291..af48492 100644
--- a/.gitignore
+++ b/.gitignore
@@ -13,6 +13,8 @@
 *.tmp
 *.spot
 *.spit
+*.gcno
+*.gcda
 TAGS
 GTAGS
 GRTAGS
diff --git a/Config.mk b/Config.mk
index 64541c8..a49b7c9 100644
--- a/Config.mk
+++ b/Config.mk
@@ -228,3 +228,7 @@ QEMU_TAG ?= 2a1354d655d816feaad7dbdb8364f40a208439c1
 # doing and are prepared for some pain.

 CONFIG_TESTS       ?= y
+
+# Test coverage support
+coverage ?= n
+
diff --git a/xen/Rules.mk b/xen/Rules.mk
index c2db449..c9044f5 100644
--- a/xen/Rules.mk
+++ b/xen/Rules.mk
@@ -103,6 +103,11 @@ subdir-all := $(subdir-y) $(subdir-n)

 $(filter %.init.o,$(obj-y) $(obj-bin-y)): CFLAGS += -DINIT_SECTIONS_ONLY

+
+ifeq ($(coverage),y)
+$(filter-out %.init.o,$(obj-y) $(obj-bin-y)): CFLAGS += -fprofile-arcs
-ftest-coverage -DTEST_COVERAGE
+endif
+
 ifeq ($(lto),y)
 # Would like to handle all object files as bitcode, but objects made from
 # pure asm are in a different format and have to be collected separately.
diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c
index f4d3788..ddf5c4d 100644
--- a/xen/arch/x86/setup.c
+++ b/xen/arch/x86/setup.c
@@ -46,6 +46,7 @@
 #include <asm/setup.h>
 #include <xen/cpu.h>
 #include <asm/nmi.h>
+#include <xen/gcov.h>

 /* opt_nosmp: If true, secondary processors are ignored. */
 static bool_t __initdata opt_nosmp;
@@ -1313,6 +1314,8 @@ void __init __start_xen(unsigned long mbi_p)

     init_trace_bufs();

+    init_coverage();
+
     console_endboot();

     /* Hide UART from DOM0 if we''re using it */
diff --git a/xen/arch/x86/x86_64/compat/entry.S
b/xen/arch/x86/x86_64/compat/entry.S
index 7051f90..ebbb6b7 100644
--- a/xen/arch/x86/x86_64/compat/entry.S
+++ b/xen/arch/x86/x86_64/compat/entry.S
@@ -413,6 +413,8 @@ ENTRY(compat_hypercall_table)
         .quad do_domctl
         .quad compat_kexec_op
         .quad do_tmem_op
+        .quad compat_ni_hypercall
+        .quad compat_coverage_op
         .rept __HYPERVISOR_arch_0-((.-compat_hypercall_table)/8)
         .quad compat_ni_hypercall
         .endr
@@ -461,6 +463,8 @@ ENTRY(compat_hypercall_args_table)
         .byte 1 /* do_domctl                */
         .byte 2 /* compat_kexec_op          */
         .byte 1 /* do_tmem_op               */
+        .byte 0 /* compat_ni_hypercall      */
+        .byte 2 /* compat_coverage_op       */
         .rept __HYPERVISOR_arch_0-(.-compat_hypercall_args_table)
         .byte 0 /* compat_ni_hypercall      */
         .endr
diff --git a/xen/arch/x86/x86_64/entry.S b/xen/arch/x86/x86_64/entry.S
index 408c348..670a2e6 100644
--- a/xen/arch/x86/x86_64/entry.S
+++ b/xen/arch/x86/x86_64/entry.S
@@ -738,6 +738,8 @@ ENTRY(hypercall_table)
         .quad do_domctl
         .quad do_kexec_op
         .quad do_tmem_op
+        .quad do_ni_hypercall
+        .quad do_coverage_op
         .rept __HYPERVISOR_arch_0-((.-hypercall_table)/8)
         .quad do_ni_hypercall
         .endr
@@ -786,6 +788,8 @@ ENTRY(hypercall_args_table)
         .byte 1 /* do_domctl            */
         .byte 2 /* do_kexec             */
         .byte 1 /* do_tmem_op           */
+        .byte 0 /* do_ni_hypercall      */
+        .byte 2 /* do_coverage_op       */
         .rept __HYPERVISOR_arch_0-(.-hypercall_args_table)
         .byte 0 /* do_ni_hypercall      */
         .endr
diff --git a/xen/arch/x86/xen.lds.S b/xen/arch/x86/xen.lds.S
index d324afd..5570389 100644
--- a/xen/arch/x86/xen.lds.S
+++ b/xen/arch/x86/xen.lds.S
@@ -108,6 +108,13 @@ SECTIONS
        __trampoline_seg_start = .;
        *(.trampoline_seg)
        __trampoline_seg_stop = .;
+
+       . = ALIGN(8);
+       __CTOR_LIST__ = .;
+       QUAD((__CTOR_END__ - __CTOR_LIST__) / 8 - 2)
+       *(.ctors)
+       QUAD(0)
+       __CTOR_END__ = .;
   } :text
   . = ALIGN(32);
   .init.setup : {
diff --git a/xen/common/Makefile b/xen/common/Makefile
index 1677342..51191d6 100644
--- a/xen/common/Makefile
+++ b/xen/common/Makefile
@@ -59,5 +59,7 @@ subdir-$(CONFIG_COMPAT) += compat

 subdir-$(x86_64) += hvm

+subdir-y += gcov
+
 subdir-y += libelf
 subdir-$(HAS_DEVICE_TREE) += libfdt
diff --git a/xen/common/gcov/Makefile b/xen/common/gcov/Makefile
new file mode 100644
index 0000000..43a692c
--- /dev/null
+++ b/xen/common/gcov/Makefile
@@ -0,0 +1,5 @@
+ifneq ($(coverage),y)
+obj-y += nogcov.o
+endif
+obj-$(coverage) += gcov.o
+
diff --git a/xen/common/gcov/gcov.c b/xen/common/gcov/gcov.c
new file mode 100644
index 0000000..e208596
--- /dev/null
+++ b/xen/common/gcov/gcov.c
@@ -0,0 +1,105 @@
+/*
+ *  This code maintains a list of active profiling data structures.
+ *
+ *    Copyright IBM Corp. 2009
+ *    Author(s): Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
+ *
+ *    Uses gcc-internal data definitions.
+ *    Based on the gcov-kernel patch by:
+ *       Hubertus Franke <frankeh@us.ibm.com>
+ *       Nigel Hinds <nhinds@us.ibm.com>
+ *       Rajan Ravindran <rajancr@us.ibm.com>
+ *       Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
+ *       Paul Larson
+ */
+
+#include <xen/config.h>
+#include <xen/init.h>
+#include <xen/lib.h>
+#include <xen/hypercall.h>
+#include <xen/gcov.h>
+#include <xen/errno.h>
+#include <public/xen.h>
+
+static struct gcov_info *info_list;
+static unsigned num_info = 0;
+
+/*
+ * __gcov_init is called by gcc-generated constructor code for each object
+ * file compiled with -fprofile-arcs.
+ *
+ * Although this function is called only during initialization is called from
+ * a .text section which is still present after initialization so not declare
+ * as __init.
+ */
+void __gcov_init(struct gcov_info *info)
+{
+    /* add new profiling data structure to list */
+    info->next = info_list;
+    info_list = info;
+    ++num_info;
+}
+
+/*
+ * These functions may be referenced by gcc-generated profiling code but serve
+ * no function for Xen.
+ */
+void __gcov_flush(void)
+{
+    /* Unused. */
+}
+
+void __gcov_merge_add(gcov_type *counters, unsigned int n_counters)
+{
+    /* Unused. */
+}
+
+void __gcov_merge_single(gcov_type *counters, unsigned int n_counters)
+{
+    /* Unused. */
+}
+
+void __gcov_merge_delta(gcov_type *counters, unsigned int n_counters)
+{
+    /* Unused. */
+}
+
+typedef void (*ctor_func_t)(void);
+extern struct
+{
+    unsigned long count;
+    ctor_func_t funcs[1];
+} __CTOR_LIST__;
+
+void init_coverage(void)
+{
+    unsigned long n;
+    for ( n = 0; n < __CTOR_LIST__.count; ++n )
+        __CTOR_LIST__.funcs[n]();
+
+#ifndef NDEBUG
+    printk(XENLOG_INFO "Initialized %u coverage strucures\n",
num_info);
+    if ( info_list )
+        printk(XENLOG_INFO "First coverage file %s\n",
info_list->filename);
+#endif
+}
+
+long do_coverage_op(int op, XEN_GUEST_HANDLE_PARAM(void) uarg)
+{
+    return -EINVAL;
+}
+
+#ifdef CONFIG_COMPAT
+int compat_coverage_op(int op, XEN_GUEST_HANDLE_PARAM(void) uarg)
+    __attribute__ ((alias ("do_coverage_op")));
+#endif
+
+/*
+ * Local variables:
+ * mode: C
+ * c-set-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/xen/common/gcov/nogcov.c b/xen/common/gcov/nogcov.c
new file mode 100644
index 0000000..9642c96
--- /dev/null
+++ b/xen/common/gcov/nogcov.c
@@ -0,0 +1,22 @@
+/*
+ * nogov.c
+ *
+ * Compiled if coverage information are not enabled
+ */
+
+#include <xen/config.h>
+#include <xen/lib.h>
+#include <xen/errno.h>
+#include <public/xen.h>
+
+long do_coverage_op(int op, XEN_GUEST_HANDLE_PARAM(void) uarg)
+{
+    return -ENOSYS;
+}
+
+#ifdef CONFIG_COMPAT
+int compat_coverage_op(int op, XEN_GUEST_HANDLE_PARAM(void) uarg)
+    __attribute__ ((alias ("do_coverage_op")));
+#endif
+
+
diff --git a/xen/include/public/gcov.h b/xen/include/public/gcov.h
new file mode 100644
index 0000000..67aedf6
--- /dev/null
+++ b/xen/include/public/gcov.h
@@ -0,0 +1,93 @@
+/*
+ *  Profiling infrastructure declarations.
+ *
+ *  This file is based on gcc-internal definitions. Data structures are
+ *  defined to be compatible with gcc counterparts. For a better
+ *  understanding, refer to gcc source: gcc/gcov-io.h.
+ *
+ *    Copyright IBM Corp. 2009
+ *    Author(s): Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
+ *
+ *    Uses gcc-internal data definitions.
+ */
+
+#ifndef XEN_PUBLIC_GCOV_H
+#define XEN_PUBLIC_GCOV_H XEN_PUBLIC_GCOV_H
+
+/*
+ * Profiling data types used for gcc 3.4 and above - these are defined by
+ * gcc and need to be kept as close to the original definition as possible to
+ * remain compatible.
+ */
+#define GCOV_COUNTERS         5
+#define GCOV_DATA_MAGIC       ((unsigned int) 0x67636461)
+#define GCOV_TAG_FUNCTION     ((unsigned int) 0x01000000)
+#define GCOV_TAG_COUNTER_BASE ((unsigned int) 0x01a10000)
+#define GCOV_TAG_FOR_COUNTER(count) \
+    (GCOV_TAG_COUNTER_BASE + ((unsigned int) (count) << 17))
+
+#if BITS_PER_LONG >= 64
+typedef long gcov_type;
+#else
+typedef long long gcov_type;
+#endif
+
+/**
+ * struct gcov_fn_info - profiling meta data per function
+ * @ident: object file-unique function identifier
+ * @checksum: function checksum
+ * @n_ctrs: number of values per counter type belonging to this function
+ *
+ * This data is generated by gcc during compilation and doesn''t change
+ * at run-time.
+ */
+struct gcov_fn_info
+{
+    unsigned int ident;
+    unsigned int checksum;
+    unsigned int n_ctrs[0];
+};
+
+/**
+ * struct gcov_ctr_info - profiling data per counter type
+ * @num: number of counter values for this type
+ * @values: array of counter values for this type
+ * @merge: merge function for counter values of this type (unused)
+ *
+ * This data is generated by gcc during compilation and doesn''t change
+ * at run-time with the exception of the values array.
+ */
+struct gcov_ctr_info
+{
+    unsigned int num;
+    gcov_type *values;
+    void (*merge)(gcov_type *, unsigned int);
+};
+
+/**
+ * struct gcov_info - profiling data per object file
+ * @version: gcov version magic indicating the gcc version used for compilation
+ * @next: list head for a singly-linked list
+ * @stamp: time stamp
+ * @filename: name of the associated gcov data file
+ * @n_functions: number of instrumented functions
+ * @functions: function data
+ * @ctr_mask: mask specifying which counter types are active
+ * @counts: counter data per counter type
+ *
+ * This data is generated by gcc during compilation and doesn''t change
+ * at run-time with the exception of the next pointer.
+ */
+struct gcov_info
+{
+    unsigned int              version;
+    struct gcov_info          *next;
+    unsigned int              stamp;
+    const char                *filename;
+    unsigned int              n_functions;
+    const struct gcov_fn_info *functions;
+    unsigned int              ctr_mask;
+    struct gcov_ctr_info      counts[0];
+};
+
+#endif /* XEN_PUBLIC_GCOV_H */
diff --git a/xen/include/xen/gcov.h b/xen/include/xen/gcov.h
new file mode 100644
index 0000000..517ed75
--- /dev/null
+++ b/xen/include/xen/gcov.h
@@ -0,0 +1,32 @@
+/*
+ *  Profiling infrastructure declarations.
+ *
+ *  This file is based on gcc-internal definitions. Data structures are
+ *  defined to be compatible with gcc counterparts. For a better
+ *  understanding, refer to gcc source: gcc/gcov-io.h.
+ *
+ *    Copyright IBM Corp. 2009
+ *    Author(s): Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
+ *
+ *    Uses gcc-internal data definitions.
+ */
+
+#ifndef XEN_GCOV_H
+#define XEN_GCOV_H XEN_GCOV_H
+
+#include <public/gcov.h>
+
+/**
+ * Initialize coverage informations
+ * Currently create a linked list of all files compiled in with
+ * coverage informations.
+ */
+#ifdef TEST_COVERAGE
+void __init init_coverage(void);
+#else
+static inline void init_coverage(void)
+{
+}
+#endif
+
+#endif /* GCOV_H */
diff --git a/xen/include/xen/hypercall.h b/xen/include/xen/hypercall.h
index 7c3d719..e62525a 100644
--- a/xen/include/xen/hypercall.h
+++ b/xen/include/xen/hypercall.h
@@ -140,6 +140,9 @@ do_tmem_op(
 extern long
 do_xenoprof_op(int op, XEN_GUEST_HANDLE_PARAM(void) arg);

+extern long
+do_coverage_op(int op, XEN_GUEST_HANDLE_PARAM(void) uarg);
+
 #ifdef CONFIG_COMPAT

 extern int
@@ -163,6 +166,9 @@ extern int
 compat_xenoprof_op(int op, XEN_GUEST_HANDLE_PARAM(void) arg);

 extern int
+compat_coverage_op(int op, XEN_GUEST_HANDLE_PARAM(void) uarg);
+
+extern int
 compat_xen_version(
     int cmd,
     XEN_GUEST_HANDLE_PARAM(void) arg);
--
1.7.9.5