Coverage support
RFC PATCH v4 1/1 Coverage support
Updated set of patches for coverage.
Changes:
- declared __init function in the right place
- use Jan suggestion for changing compiler options. I didn't understand why this change excluded files in xen/common/libelf. I wrote a script to test no references from no-init to init section but didn't find nothing related to coverage stuff. All pointers passed to __gcov_init refer to .data sections as expected
- added utility to get/reset coverage information
Hope to not forget other advice again.
PATCH 1/4 Adding support for coverage information
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
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.
--- .gitignore | 2 + .hgignore | 2 + Config.mk | 3 ++ xen/Rules.mk | 2 + xen/arch/x86/setup.c | 3 ++ xen/arch/x86/xen.lds.S | 7 ++++ xen/common/Makefile | 2 + xen/common/gcov/Makefile | 2 + xen/common/gcov/gcov.c | 94 ++++++++++++++++++++++++++++++++++++++++++++ xen/include/xen/gcov.h | 97 ++++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 214 insertions(+) create mode 100644 xen/common/gcov/Makefile create mode 100644 xen/common/gcov/gcov.c create mode 100644 xen/include/xen/gcov.h
.gitignore +2 -0
@@ -13,6 +13,8 @@ *.tmp *.spot *.spit +*.gcno +*.gcda TAGS GTAGS GRTAGS
.hgignore +2 -0
@@ -17,6 +17,8 @@ .*\.rej$ .*\.spot$ .*\.spit$ +.*\.gcno$ +.*\.gcda$ .*/a\.out$ .*/Modules\.symvers$ .*/cscope\..*$
Config.mk +3 -0
@@ -13,6 +13,9 @@ realpath = $(wildcard $(foreach file,$(1),$(shell cd -P $(dir $(file)) && echo " debug ?= y debug_symbols ?= $(debug) +# Test coverage support +coverage ?= n + XEN_COMPILE_ARCH ?= $(shell uname -m | sed -e s/i.86/x86_32/ \ -e s/i86pc/x86_32/ -e s/amd64/x86_64/ \ -e s/armv7.*/arm32/)
xen/Rules.mk +2 -0
@@ -103,6 +103,8 @@ subdir-all := $(subdir-y) $(subdir-n) $(filter %.init.o,$(obj-y) $(obj-bin-y)): CFLAGS += -DINIT_SECTIONS_ONLY +$(obj-$(coverage)): CFLAGS += -fprofile-arcs -ftest-coverage -DTEST_COVERAGE + 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.
xen/arch/x86/setup.c +3 -0
@@ -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 */
xen/arch/x86/xen.lds.S +7 -0
@@ -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 : {
xen/common/Makefile +2 -0
@@ -59,5 +59,7 @@ subdir-$(CONFIG_COMPAT) += compat subdir-$(x86_64) += hvm +subdir-$(coverage) += gcov + subdir-y += libelf subdir-$(HAS_DEVICE_TREE) += libfdt
xen/common/gcov/Makefile new file +2 -0
@@ -0,0 +1,2 @@ +obj-y += gcov.o +
xen/common/gcov/gcov.c new file +94 -0
94 lines not on this page · raw message
xen/include/xen/gcov.h new file +97 -0
97 lines not on this page · raw message
Signature · 1 line
1.7.9.5
Re: [PATCH 1/4] Adding support for coverage information
xen/common/gcov/gcov.c line 77
Quoted 5 lines · Frediano Ziglio
xen/common/gcov/gcov.c new file +5
-0+void __init init_coverage (void ) +{ +unsigned long n; +for ( n =0 ; n < __CTOR_LIST__.count; ++n ) + __CTOR_LIST__.funcs[n]();
There is nothing coverage specific here, why not run these from a generic function?
Ian.
Re: [PATCH 1/4] Adding support for coverage information
Quoted 10 lines · Ian Campbell
xen/common/gcov/gcov.c line 77
Quoted 5 lines · Frediano Ziglio
xen/common/gcov/gcov.c new file +5
-0+void __init init_coverage (void ) +{ +unsigned long n; +for ( n =0 ; n < __CTOR_LIST__.count; ++n ) + __CTOR_LIST__.funcs[n]();There is nothing coverage specific here, why not run these from a generic function?
Quoted 3 lines
Ian.
You are right, but currently are only used for coverage and constructors are not expected to be useful in Xen.
One more question. The __CTOR_LIST__ name and the counters before the array it's a choice following GCC convention but there is no need to follow it. Do you thing a better name and perhaps just the array with a final NULL of a pointer to the end would be better?
Frediano
PATCH 2/4 Implement code to read coverage informations
In reply to Frediano ZiglioOperations are handled by a sysctl specific operation.
Implement 4 operations
- check if coverage is compiled in
- read size of coverage information
- read coverage information
- reset coverage counters
Information are stored in a single blob in a format specific to Xen designed to make code that generate coverage as small as possible.
This patch add a public header with the structure of blob exported by Xen. This avoid problems distributing header which is GPL2.
--- xen/common/gcov/gcov.c | 151 ++++++++++++++++++++++++++++++++++++++++++- xen/common/sysctl.c | 5 ++ xen/include/public/gcov.h | 92 ++++++++++++++++++++++++++ xen/include/public/sysctl.h | 38 +++++++++++ xen/include/xen/gcov.h | 14 ++++ 5 files changed, 297 insertions(+), 3 deletions(-) create mode 100644 xen/include/public/gcov.h
xen/common/gcov/gcov.c +148 -3
@@ -19,9 +19,11 @@#include <xen/hypercall.h> #include <xen/gcov.h> #include <xen/errno.h> +#include <xen/guest_access.h> +#include <public/xen.h> +#include <public/gcov.h> static struct gcov_info *info_list; -static unsigned num_info =0 ;/* * __gcov_init is called by gcc-generated constructor code for each object
@@ -36,7 +38,6 @@ void __gcov_init(struct gcov_info *info)/* add new profiling data structure to list */ info->next = info_list; info_list = info; - ++num_info; }/*
@@ -77,12 +78,156 @@ void __init init_coverage(void) __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 } +static inline int counter_active (const struct gcov_info *info,unsigned int type) +{ +return (1 << type) & info->ctr_mask; +} + +typedef struct write_iter_t +{ +XEN_GUEST_HANDLE (uint8) ptr; +int real; +uint32_t write_offset; +}write_iter_t ; + +static int write_raw (struct write_iter_t *iter,const void *data, +size_t data_len) +{ +if ( iter->real && +copy_to_guest_offset (iter->ptr, iter->write_offset, + (const unsigned char *) data, data_len) ) +return -EFAULT ; + + iter->write_offset = + (iter->write_offset + data_len + (sizeof (uint32_t ) -1 )) & + -sizeof (uint32_t ); +return 0 ; +} + +#define chk (v) do { ret=(v); if ( ret ) return ret; } while(0) + +static inline int write32 (write_iter_t *iter,uint32_t val) +{ +return write_raw (iter, &val,sizeof (val)); +} + +static int write_string (write_iter_t *iter,const char *s) +{ +int ret; +size_t len =strlen (s); + +chk (write32 (iter, len)); +return write_raw (iter, s, len); +} + +static inline int next_type (const struct gcov_info *info,int *type) +{ +while ( ++*type <XENCOV_COUNTERS && !counter_active (info, *type) ) +continue ; +return *type; +} + +static int write_gcov (write_iter_t *iter) +{ +struct gcov_info *info; +int ret; + +/* reset offset */ + iter->write_offset =0 ; + +/* dump all files */ +for ( info = info_list ; info; info = info->next ) + { +const struct gcov_ctr_info *ctr; +int type; +size_t size_fn =sizeof (struct gcov_fn_info ); + +chk (write32 (iter,XENCOV_TAG_FILE )); +chk (write_string (iter, info->filename)); +chk (write32 (iter, info->version)); +chk (write32 (iter, info->stamp)); + +/* dump counters */ + ctr = info->counts; +for ( type =-1 ;next_type (info, &type) <XENCOV_COUNTERS ; ++ctr ) + { +chk (write32 (iter,XENCOV_TAG_COUNTER (type))); +chk (write32 (iter, ctr->num)); +chk (write_raw (iter, ctr->values, + ctr->num *sizeof (ctr->values[0 ]))); + + size_fn +=sizeof (unsigned ); + } + +/* dump all functions together */ +chk (write32 (iter,XENCOV_TAG_FUNC )); +chk (write32 (iter, info->n_functions)); +chk (write_raw (iter, info->functions, info->n_functions * size_fn)); + } + +/* stop tag */ +chk (write32 (iter,XENCOV_TAG_END )); +return 0 ; +} + +static int reset_counters (void ) +{ +struct gcov_info *info; + +for ( info = info_list ; info; info = info->next ) + { +const struct gcov_ctr_info *ctr; +int type; + +/* reset counters */ + ctr = info->counts; +for ( type =-1 ;next_type (info, &type) <XENCOV_COUNTERS ; ++ctr ) +memset (ctr->values,0 , ctr->num *sizeof (ctr->values[0 ])); + } + +return 0 ; +} + +int sysctl_coverage_op (xen_sysctl_coverage_op_t *op) +{ +long ret =0 ; +write_iter_t iter; + +switch ( op->cmd ) + { +case XEN_SYSCTL_COVERAGE_enabled: +break ; + +case XEN_SYSCTL_COVERAGE_get_total_size: + iter.real =0 ; + +write_gcov (&iter); +if (copy_to_guest (op->u.total_size, &iter.write_offset,1 ) ) + ret = -EFAULT ; +break ; + +case XEN_SYSCTL_COVERAGE_read: + iter.ptr = op->u.raw_info; + iter.real =1 ; + + ret =write_gcov (&iter); +break ; + +case XEN_SYSCTL_COVERAGE_reset: + ret =reset_counters (); +break ; + +default : + ret = -EINVAL ; + } +return ret; +} +/* * Local variables: * mode: C
xen/common/sysctl.c +5 -0
@@ -26,6 +26,7 @@#include <xen/nodemask.h> #include <xsm/xsm.h> #include <xen/pmstat.h> +#include <xen/gcov.h> long do_sysctl (XEN_GUEST_HANDLE_PARAM (xen_sysctl_t) u_sysctl) {
@@ -249,6 +250,10 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) ret =sched_adjust_global (&op->u.scheduler_op);break ; +case XEN_SYSCTL_coverage_op: + ret =sysctl_coverage_op (&op->u.coverage_op); +break ; +default : ret =arch_do_sysctl (op, u_sysctl); copyback =0 ;
xen/include/public/gcov.h new file +92 -0
92 lines not on this page · raw message
xen/include/public/sysctl.h +38 -0
@@ -596,6 +596,42 @@ struct xen_sysctl_scheduler_op {typedef struct xen_sysctl_scheduler_op xen_sysctl_scheduler_op_t ;DEFINE_XEN_GUEST_HANDLE (xen_sysctl_scheduler_op_t); +/* XEN_SYSCTL_coverage_op */ +/* +* Check if coverage informations are available +* return just success or error, no parameters +*/ +#define XEN_SYSCTL_COVERAGE_enabled 0 + +/* +* Get total size of information, to help allocate +* the buffer. The pointer points to a 32 bit value. +*/ +#define XEN_SYSCTL_COVERAGE_get_total_size 1 + +/* +* Read coverage information in a single run +* You must use a tool to split them +*/ +#define XEN_SYSCTL_COVERAGE_read 2 + +/* +* Reset all the coverage counters to 0 +* No parameters. +*/ +#define XEN_SYSCTL_COVERAGE_reset 3 + +struct xen_sysctl_coverage_op { +uint32_t cmd;/* XEN_SYSCTL_COVERAGE_* */ +union { +XEN_GUEST_HANDLE_64 (uint32) total_size;/* OUT */ +XEN_GUEST_HANDLE_64 (uint8) raw_info;/* OUT */ + } u; +}; +typedef struct xen_sysctl_coverage_op xen_sysctl_coverage_op_t ; +DEFINE_XEN_GUEST_HANDLE (xen_sysctl_coverage_op_t); + +struct xen_sysctl {uint32_t cmd;#define XEN_SYSCTL_readconsole 1
@@ -616,6 +652,7 @@ struct xen_sysctl {#define XEN_SYSCTL_numainfo 17#define XEN_SYSCTL_cpupool_op 18#define XEN_SYSCTL_scheduler_op 19 +#define XEN_SYSCTL_coverage_op 20uint32_t interface_version;/* XEN_SYSCTL_INTERFACE_VERSION */ union {struct xen_sysctl_readconsole readconsole;
@@ -636,6 +673,7 @@ struct xen_sysctl {struct xen_sysctl_lockprof_op lockprof_op;struct xen_sysctl_cpupool_op cpupool_op;struct xen_sysctl_scheduler_op scheduler_op; +struct xen_sysctl_coverage_op coverage_op;uint8_t pad[128 ]; } u; };
xen/include/xen/gcov.h +14 -0
@@ -14,6 +14,8 @@#ifndef __XEN_GCOV_H__#define __XEN_GCOV_H__ __XEN_GCOV_H__ +#include <public/sysctl.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
@@ -94,4 +96,16 @@ static inline void init_coverage(void) }#endif +/** +* Sysctl operations for coverage +*/ +#ifdef TEST_COVERAGE +int sysctl_coverage_op (xen_sysctl_coverage_op_t *op); +#else +static inline int sysctl_coverage_op (xen_sysctl_coverage_op_t *op) +{ +return -ENOSYS ; +} +#endif +#endif /* __XEN_GCOV_H__ */
Signature · 1 line
1.7.9.5
Re: [PATCH 2/4] Implement code to read coverage informations
Quoted 28 lines
Quoted 1 line
Quoted 1 line · Frediano Ziglio
xen/include/public/gcov.h new file +27
-0+/** +* File information +* Prefixed with XENCOV_TAG_FILE and a string with filename +*/ +struct xencov_file +{ +uint32_t version; +uint32_t stamp; +} __attribute__((packed)); + +/** +* Counters information +* Prefixed with XENCOV_TAG_COUNTER(n) where n is 0..(XENCOV_COUNTERS-1) +*/ +struct xencov_counter +{ +uint32_t num; +uint64_t values[0 ]; +} __attribute__((packed)); + +/** +* Information for each function +* Prefixed with XENCOV_TAG_FUNC +* Number of counter is equal to the number of counter got before +*/ +struct xencov_function +{
xen/include/public/gcov.h line 90
Quoted 10 lines
xen/include/public/gcov.h new file +10
-0+uint32_t ident; +uint32_t checksum; +uint32_t n_ctrs[0 ]; +} __attribute__((packed)); + +struct xencov_functions +{ +uint32_t num; +uint32_t xencov_function[0 ]; +} __attribute__((packed));
Once again: No use of compiler extensions in public headers.
Jan
Re: [PATCH 2/4] Implement code to read coverage informations
Quoted 41 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · Frediano Ziglio
xen/include/public/gcov.h new file +27
-0+/** +* File information +* Prefixed with XENCOV_TAG_FILE and a string with filename +*/ +struct xencov_file +{ +uint32_t version; +uint32_t stamp; +} __attribute__((packed)); + +/** +* Counters information +* Prefixed with XENCOV_TAG_COUNTER(n) where n is 0..(XENCOV_COUNTERS-1) +*/ +struct xencov_counter +{ +uint32_t num; +uint64_t values[0 ]; +} __attribute__((packed)); + +/** +* Information for each function +* Prefixed with XENCOV_TAG_FUNC +* Number of counter is equal to the number of counter got before +*/ +struct xencov_function +{xen/include/public/gcov.h line 90
Quoted 10 lines
xen/include/public/gcov.h new file +10
-0+uint32_t ident; +uint32_t checksum; +uint32_t n_ctrs[0 ]; +} __attribute__((packed)); + +struct xencov_functions +{ +uint32_t num; +uint32_t xencov_function[0 ]; +} __attribute__((packed));Once again: No use of compiler extensions in public headers.
Quoted 2 lines
Jan
Sorry, I forgot the old mail.
Currently this header describe the output blob exported by Xen. For the array I can just use 1 instead of 0.
I'm changing a bit the format to avoid packing and alignment problems.
Frediano
PATCH 3/4 Check no constructor section
In reply to Frediano ZiglioSafety check for initialization objects.
Constructors are not used so check some module does not use them.
--- xen/Rules.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
xen/Rules.mk +1 -1
@@ -166,7 +166,7 @@ SPECIAL_DATA_SECTIONS := rodata $(foreach n,1 2 4 8,rodata.str1.$(n)) \ $(filter %.init.o,$(obj-y) $(obj-bin-y) $(extra-y)): %.init.o: %.o Makefile $(OBJDUMP) -h $< | sed -n '/[0-9]/{s,00*,0,g;p}' | while read idx name sz rest; do \ case "$$name" in \ - .text|.text.*|.data|.data.*|.bss) \ + .text|.text.*|.data|.data.*|.bss|.ctors) \ test $$sz != 0 || continue; \ echo "Error: size of $<:$$name is 0x$$sz" >&2; \ exit $$(expr $$idx + 1);; \
Signature · 1 line
1.7.9.5
Re: [PATCH 3/4] Check no constructor section
Quoted 3 lines
Quoted 1 line
Quoted 1 line · Frediano Ziglio
Safety check for initialization objects.
Constructors are not used so check some module does not use them.
Why? They're themselves being put into .init.data, so their presence is fine in init-only objects.
Jan
Quoted 13 lines
xen/Rules.mk +1
-1@@ -166,7 +166,7 @@ SPECIAL_DATA_SECTIONS := rodata $(foreach n,1 2 4 8,rodata.str1.$(n)) \ $(filter %.init.o,$(obj-y) $(obj-bin-y) $(extra-y)): %.init.o: %.o Makefile $(OBJDUMP) -h $< | sed -n '/[0-9]/{s,00*,0,g;p}' | while read idx name sz rest; do \ case "$$name" in \ - .text|.text.*|.data|.data.*|.bss) \ + .text|.text.*|.data|.data.*|.bss|.ctors) \ test $$sz != 0 || continue; \xen/Rules.mk +0
-0echo "Error: size of $<:$$name is 0x$$sz" >&2; \ exit $$(expr $$idx + 1);; \
PATCH 4/4 Add small utility to deal with test coverage information from Xen
In reply to Frediano ZiglioCurrently the utility can read and reset coverage informations.
--- .gitignore | 1 + .hgignore | 1 + tools/misc/Makefile | 8 ++- tools/misc/xencov.c | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 tools/misc/xencov.c
.gitignore +1 -0
@@ -223,6 +223,7 @@ tools/misc/gtraceview tools/misc/gtracestat tools/misc/xenlockprof tools/misc/lowmemd +tools/misc/xencov tools/pygrub/build/* tools/python/build/* tools/python/xen/util/path.py
.hgignore +1 -0
@@ -218,6 +218,7 @@ ^tools/misc/gtraceview$ ^tools/misc/gtracestat$ ^tools/misc/xenlockprof$ +^tools/misc/xencov$ ^tools/pygrub/build/.*$ ^tools/python/build/.*$ ^tools/python/xen/util/path\.py$
tools/misc/Makefile +6 -2
@@ -9,7 +9,7 @@ CFLAGS += $(CFLAGS_libxenstore) HDRS = $(wildcard *.h) -TARGETS-y := xenperf xenpm xen-tmem-list-parse gtraceview gtracestat xenlockprof xenwatchdogd +TARGETS-y := xenperf xenpm xen-tmem-list-parse gtraceview gtracestat xenlockprof xenwatchdogd xencov TARGETS-$(CONFIG_X86) += xen-detect xen-hvmctx xen-hvmcrash xen-lowmemd TARGETS-$(CONFIG_MIGRATE) += xen-hptool TARGETS := $(TARGETS-y)
@@ -22,7 +22,8 @@ INSTALL_BIN-y := xencons INSTALL_BIN-$(CONFIG_X86) += xen-detect INSTALL_BIN := $(INSTALL_BIN-y) -INSTALL_SBIN-y := xm xen-bugtool xen-python-path xend xenperf xsview xenpm xen-tmem-list-parse gtraceview gtracestat xenlockprof xenwatchdogd xen-ringwatch +INSTALL_SBIN-y := xm xen-bugtool xen-python-path xend xenperf xsview xenpm xen-tmem-list-parse gtraceview \ + gtracestat xenlockprof xenwatchdogd xen-ringwatch xencov INSTALL_SBIN-$(CONFIG_X86) += xen-hvmctx xen-hvmcrash xen-lowmemd INSTALL_SBIN-$(CONFIG_MIGRATE) += xen-hptool INSTALL_SBIN := $(INSTALL_SBIN-y)
@@ -85,4 +86,7 @@ xen-lowmemd: xen-lowmemd.o gtraceview: gtraceview.o $(CC) $(LDFLAGS) -o $@ $< $(CURSES_LIBS) $(APPEND_LDFLAGS) +xencov: xencov.o + $(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(APPEND_LDFLAGS) + -include $(DEPS)
tools/misc/xencov.c new file +140 -0
140 lines not on this page · raw message
Signature · 1 line
1.7.9.5