From 71e90f98fd159cbb84b27752de74a9d7748f9018 Mon Sep 17 00:00:00 2001 From: Adithya Baglody Date: Wed, 29 Aug 2018 16:44:16 +0530 Subject: [PATCH] Gcov: Enable Code coverage reporting over UART. This patch provides support for generating Code coverage reports. The prj.conf needs to enable CONFIG_COVERAGE. Once enabled, the code coverage data dump now comes via UART. This data dump on the UART is triggered once the main thread exits. Next step is to save this data dump on file. Then run scripts/gen_gcov_files.py with the serial console log as argument. The last step would be be to run the gcovr. Use the following cmd gcovr -r . --html -o gcov_report/coverage.html --html-details Currently supported architectures are ARM and x86. Signed-off-by: Adithya Baglody --- arch/Kconfig | 1 + include/linker/common-rom.ld | 12 ++++++++++++ kernel/init.c | 11 +++++++++++ 3 files changed, 24 insertions(+) diff --git a/arch/Kconfig b/arch/Kconfig index a247ddc32c8..0597fe663fa 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -162,6 +162,7 @@ config PRIVILEGED_STACK_TEXT_AREA config KOBJECT_TEXT_AREA int "Size if kobject text area" + default 512 if COVERAGE_GCOV default 256 if (!SIZE_OPTIMIZATIONS || STACK_CANARIES || ARC) default 256 if CODE_DATA_RELOCATION default 128 diff --git a/include/linker/common-rom.ld b/include/linker/common-rom.ld index 53f275fd820..41643247540 100644 --- a/include/linker/common-rom.ld +++ b/include/linker/common-rom.ld @@ -41,6 +41,18 @@ __object_access_end = .; } #endif + +#ifdef CONFIG_COVERAGE_GCOV + /* Section needed by gcov when coverage is turned on.*/ + SECTION_PROLOGUE (gcov, (OPTIONAL),) + { + PROVIDE_HIDDEN (__init_array_start = .); + KEEP (*(SORT(.init_array.*))) + KEEP (*(.init_array*)) + PROVIDE_HIDDEN (__init_array_end = .); + } GROUP_LINK_IN(ROMABLE_REGION) +#endif /* CONFIG_COVERAGE_GCOV */ + SECTION_PROLOGUE (devconfig, (OPTIONAL),) { __devconfig_start = .; diff --git a/kernel/init.c b/kernel/init.c index 9d2d7329482..6511946c973 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -33,6 +33,7 @@ #include #include #include +#include #define IDLE_THREAD_NAME "idle" #define LOG_LEVEL CONFIG_KERNEL_LOG_LEVEL @@ -156,6 +157,10 @@ void _bss_zero(void) (void)memset(&__app_bss_start, 0, ((u32_t) &__app_bss_end - (u32_t) &__app_bss_start)); #endif +#ifdef CONFIG_COVERAGE_GCOV + (void)memset(&__gcov_bss_start, 0, + ((u32_t) &__gcov_bss_end - (u32_t) &__gcov_bss_start)); +#endif } @@ -252,6 +257,9 @@ static void bg_thread_main(void *unused1, void *unused2, void *unused3) main(); + /* Dump coverage data once the main() has exited. */ + gcov_coverage_dump(); + /* Terminate thread normally since it has no more work to do */ _main_thread->base.user_options &= ~K_ESSENTIAL; } @@ -453,6 +461,9 @@ extern uintptr_t __stack_chk_guard; */ FUNC_NORETURN void _Cstart(void) { + /* gcov hook needed to get the coverage report.*/ + gcov_static_init(); + #ifdef CONFIG_MULTITHREADING #ifdef CONFIG_ARCH_HAS_CUSTOM_SWAP_TO_MAIN struct k_thread *dummy_thread = NULL;