Skip to content

Commit 92afa01

Browse files
core: Add support for code coverage compilation
The code coverage is performed at runtime by keeping in memory structures holding the releveant data: In order to generate the code coverage, two steps are required: - Instruct the compiler to generate coverage information structure and functions to initialize them: flag --coverage. - Call the constructors added to initialize the coverage information and register them to the libgcov (modification of boot.c) When an object file (.o) is created with code coverage support, a note (.gcno) file is created with it. The note file contains informations used when creating the code coverage report. The support of code coverage in compiled code increases its size (~15%) and the compilation time (~25%). Signed-off-by: Franck LENORMAND <[email protected]>
1 parent e5d442f commit 92afa01

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

core/arch/arm/kernel/boot.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: BSD-2-Clause
22
/*
33
* Copyright (c) 2015-2020, Linaro Limited
4+
* Copyright 2020 NXP
45
*/
56

67
#include <arm.h>
@@ -202,15 +203,23 @@ static void secondary_init_cntfrq(void)
202203
}
203204
#endif
204205

205-
#ifdef CFG_CORE_SANITIZE_KADDRESS
206+
/*
207+
* Gcov needs the constructor to run at init. There is a constructor
208+
* function for each function to gather coverage data.
209+
* These functions are created automatically by the compiler.
210+
* Each constructor function will call __gcov_int() for their function.
211+
*/
212+
#if defined(CFG_CORE_SANITIZE_KADDRESS) || defined(CFG_CORE_GCOV_SUPPORT)
206213
static void init_run_constructors(void)
207214
{
208215
const vaddr_t *ctor;
209216

210217
for (ctor = &__ctor_list; ctor < &__ctor_end; ctor++)
211218
((void (*)(void))(*ctor))();
212219
}
220+
#endif /* CFG_CORE_SANITIZE_KADDRESS || CFG_CORE_GCOV_SUPPORT */
213221

222+
#ifdef CFG_CORE_SANITIZE_KADDRESS
214223
static void init_asan(void)
215224
{
216225

@@ -260,6 +269,9 @@ static void init_asan(void)
260269
#else /*CFG_CORE_SANITIZE_KADDRESS*/
261270
static void init_asan(void)
262271
{
272+
#ifdef CFG_CORE_GCOV_SUPPORT
273+
init_run_constructors();
274+
#endif
263275
}
264276
#endif /*CFG_CORE_SANITIZE_KADDRESS*/
265277

core/core.mk

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ endif
6363
cppflags$(sm) += -Ildelf/include
6464
cppflags$(sm) += -Ilib/libutee/include
6565

66+
ifeq ($(CFG_CORE_GCOV_SUPPORT),y)
67+
cppflags$(sm) += --coverage
68+
cflags$(sm) += --coverage
69+
aflags$(sm) += --coverage
70+
endif
71+
6672
ifeq ($(filter y, $(CFG_CORE_DYN_SHM) $(CFG_CORE_RESERVED_SHM)),)
6773
$(error error: No shared memory configured)
6874
endif

mk/compile.mk

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#
1010
# set objs
1111
# update cleanfiles
12+
# update gcnofiles if CFG_GCOV_SUPPORT is enabled
1213
#
1314
# Generates explicit rules for all objs
1415

@@ -83,6 +84,15 @@ echo-check-$2 := $(cmd-echo-silent)
8384
echo-check-cmd-$2 = $(cmd-echo) $$(subst \",\\\",$$(check-cmd-$2))
8485
endif
8586

87+
ifeq ($(CFG_GCOV_SUPPORT),y)
88+
ifeq ($$(findstring --coverage, $$(comp-flags-$2)), --coverage)
89+
gcnofile := $(basename $2).gcno
90+
cleanfiles := $$(cleanfiles) $(gcnofile)
91+
gcnofiles := $$(gcnofiles) $(gcnofile)
92+
$(basename $2).gcno: $2
93+
endif
94+
endif
95+
8696
else ifeq ($$(filter %.S,$1),$1)
8797
comp-q-$2 := AS # one trailing space
8898
comp-compiler-$2 := $$(CC$(sm))

ta/arch/arm/link.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ link-ldflags += --dynamic-list $(link-out-dir$(sm))/dyn_list
4747
dynlistdep = $(link-out-dir$(sm))/dyn_list
4848
cleanfiles += $(link-out-dir$(sm))/dyn_list
4949

50+
ifeq ($(CFG_GCOV_SUPPORT),y)
51+
libnames += gcov
52+
endif
53+
5054
link-ldadd = $(user-ta-ldadd) $(addprefix -L,$(libdirs))
5155
link-ldadd += --start-group
5256
link-ldadd += $(addprefix -l,$(libnames))

ta/ta.mk

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ endif
8383

8484
base-prefix := $(sm)-
8585

86+
ifeq ($(CFG_GCOV_SUPPORT),y)
87+
libname = gcov
88+
libdir = lib/libgcov
89+
libuuid = a97851f6-c80e-11ea-87d0-0242ac130003
90+
include mk/lib.mk
91+
endif
92+
8693
libname = utils
8794
libdir = lib/libutils
8895
libuuid = 71855bba-6055-4293-a63f-b0963a737360

0 commit comments

Comments
 (0)