gcov: Clang support
PATCH v5 1/1 gcov: Clang support
From: Greg Hackmann <[email protected]>
LLVM uses profiling data that's deliberately similar to GCC, but has a very different way of exporting that data. LLVM calls llvm_gcov_init() once per module, and provides a couple of callbacks that we can use to ask for more data.
We care about the "writeout" callback, which in turn calls back into compiler-rt/this module to dump all the gathered coverage data to disk:
llvm_gcda_start_file()
llvm_gcda_emit_function()
llvm_gcda_emit_arcs()
llvm_gcda_emit_function()
llvm_gcda_emit_arcs()
[... repeats for each function ...]
llvm_gcda_summary_info()
llvm_gcda_end_file()
This design is much more stateless and unstructured than gcc's, and is intended to run at process exit. This forces us to keep some local state about which module we're dealing with at the moment. On the other hand, it also means we don't depend as much on how LLVM represents profiling data internally.
See LLVM's lib/Transforms/Instrumentation/GCOVProfiling.cpp for more details on how this works, particularly GCOVProfiler::emitProfileArcs(), GCOVProfiler::insertCounterWriteout(), and GCOVProfiler::insertFlush().
---
v5:
- Same as v4. Build error when !CONFIG_MODULES was fixed by another commit 41e72eeff32c ("module: add stubs for within_module functions")
kernel/gcov/Kconfig | 3 +- kernel/gcov/Makefile | 1 + kernel/gcov/base.c | 2 +- kernel/gcov/clang.c | 586 ++++++++++++++++++++++++++++++++++++++++++ kernel/gcov/gcc_3_4.c | 12 + kernel/gcov/gcc_4_7.c | 12 + kernel/gcov/gcov.h | 2 + 7 files changed, 616 insertions(+), 2 deletions(-) create mode 100644 kernel/gcov/clang.c
kernel/gcov/Kconfig +2 -1
@@ -53,6 +53,7 @@ config GCOV_PROFILE_ALL choice prompt "Specify GCOV format" depends on GCOV_KERNEL + depends on CC_IS_GCC ---help--- The gcov format is usually determined by the GCC version, and the default is chosen according to your GCC version. However, there are
@@ -62,7 +63,7 @@ choice config GCOV_FORMAT_3_4 bool "GCC 3.4 format" - depends on CC_IS_GCC && GCC_VERSION < 40700 + depends on GCC_VERSION < 40700 ---help--- Select this option to use the format defined by GCC 3.4.
kernel/gcov/Makefile +1 -0
@@ -4,3 +4,4 @@ ccflags-y := -DSRCTREE='"$(srctree)"' -DOBJTREE='"$(objtree)"' obj-y := base.o fs.o obj-$(CONFIG_GCOV_FORMAT_3_4) += gcc_base.o gcc_3_4.o obj-$(CONFIG_GCOV_FORMAT_4_7) += gcc_base.o gcc_4_7.o +obj-$(CONFIG_CC_IS_CLANG) += clang.o
kernel/gcov/base.c +1 -1
@@ -64,7 +64,7 @@ static int gcov_module_notifier(struct notifier_block *nb, unsigned long event,/* Remove entries located in module from linked list. */ while ((info =gcov_info_next (info))) { -if (within_module ((unsigned long )info, mod)) { +if (gcov_info_within_module (info, mod)) {gcov_info_unlink (prev, info);if (gcov_events_enabled)gcov_event (GCOV_REMOVE , info);
kernel/gcov/clang.c new file +586 -0
586 lines not on this page · raw message
kernel/gcov/gcc_3_4.c +12 -0
@@ -137,6 +137,18 @@ void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info) gcov_info_head = info->next; } +/** +* gcov_info_within_module - check if a profiling data set belongs to a module +* @info: profiling data set +* @mod: module +* +* Returns true if profiling data belongs module, false otherwise. +*/ +bool gcov_info_within_module (struct gcov_info *info,struct module *mod) +{ +return within_module ((unsigned long )info, mod); +} +/* Symbolic links to be created for each profiling data file. */ const struct gcov_link gcov_link[] = { {OBJ_TREE ,"gcno" },/* Link to .gcno file in $(objtree). */
kernel/gcov/gcc_4_7.c +12 -0
@@ -150,6 +150,18 @@ void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info) gcov_info_head = info->next; } +/** +* gcov_info_within_module - check if a profiling data set belongs to a module +* @info: profiling data set +* @mod: module +* +* Returns true if profiling data belongs module, false otherwise. +*/ +bool gcov_info_within_module (struct gcov_info *info,struct module *mod) +{ +return within_module ((unsigned long )info, mod); +} +/* Symbolic links to be created for each profiling data file. */ const struct gcov_link gcov_link[] = { {OBJ_TREE ,"gcno" },/* Link to .gcno file in $(objtree). */
kernel/gcov/gcov.h +2 -0
@@ -15,6 +15,7 @@#ifndef GCOV_H #define GCOV_H GCOV_H +#include <linux/module.h> #include <linux/types.h> /*
@@ -46,6 +47,7 @@ unsigned int gcov_info_version(struct gcov_info *info);struct gcov_info *gcov_info_next (struct gcov_info *info);void gcov_info_link (struct gcov_info *info);void gcov_info_unlink (struct gcov_info *prev,struct gcov_info *info); +bool gcov_info_within_module (struct gcov_info *info,struct module *mod);/* Base interface. */ enum gcov_action {
--
2.21.0.392.gf8f6787159e-goog
Quoted 23 lines · On Wed, 17 Apr 2019 15:53:28 -0700 Tri Vo <[email protected]> wrote:
LLVM uses profiling data that's deliberately similar to GCC, but has a very different way of exporting that data. LLVM calls
llvm_gcov_init()once per module, and provides a couple of callbacks that we can use to ask for more data.We care about the "writeout" callback, which in turn calls back into compiler-rt/this module to dump all the gathered coverage data to disk:
llvm_gcda_start_file()
llvm_gcda_emit_function()
llvm_gcda_emit_arcs()
llvm_gcda_emit_function()
llvm_gcda_emit_arcs()
[... repeats for each function ...]
llvm_gcda_summary_info()
llvm_gcda_end_file()This design is much more stateless and unstructured than gcc's, and is intended to run at process exit. This forces us to keep some local state about which module we're dealing with at the moment. On the other hand, it also means we don't depend as much on how LLVM represents profiling data internally.
Quoted 3 lines
See LLVM's
lib/Transforms/Instrumentation/GCOVProfiling.cppfor more details on how this works, particularlyGCOVProfiler::emitProfileArcs(),GCOVProfiler::insertCounterWriteout(), andGCOVProfiler::insertFlush().
checkpatch speaketh truth.
Also, I'll change those Co-authored-by's to the documented Co-developed-by.
From: Andrew Morton <[email protected]>
Subject: gcov-clang-support-checkpatch-fixes
WARNING: Non-standard signature: Co-authored-by:
#31:
WARNING: Non-standard signature: Co-authored-by:
#32:
WARNING: Possible unnecessary 'out of memory' message #158: FILE: kernel/gcov/clang.c:90:
+ if (!info) {
+ pr_warn_ratelimited("failed to allocate gcov info\n");
WARNING: Possible unnecessary 'out of memory' message #193: FILE: kernel/gcov/clang.c:125:
+ if (!info) {
+ pr_warn_ratelimited("failed to allocate gcov function info for %s\n",
WARNING: line over 80 characters
#546: FILE: kernel/gcov/clang.c:478:
+ pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
total: 0 errors, 5 warnings, 663 lines checked
NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.
./patches/gcov-clang-support.patch has style problems, please review.
NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.
Please run checkpatch prior to sending patches
---
kernel/gcov/clang.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
kernel/gcov/clang.c renamed from kernel/gcov/clang.c~gcov-clang-support-checkpatch-fixes +2 -7
@@ -86,10 +86,8 @@ void llvm_gcov_init(llvm_gcov_callback w {struct gcov_info *info =kzalloc (sizeof (*info),GFP_KERNEL ); -if (!info) { -pr_warn_ratelimited ("failed to allocate gcov info\n" ); +if (!info)return ; - }INIT_LIST_HEAD (&info->head);INIT_LIST_HEAD (&info->functions);
@@ -121,11 +119,8 @@ void llvm_gcda_emit_function(u32 ident, {struct gcov_fn_info *info =kzalloc (sizeof (*info),GFP_KERNEL ); -if (!info) { -pr_warn_ratelimited ("failed to allocate gcov function info for %s\n" , - function_name ?:"UNKNOWN" ); +if (!info)return ; - }INIT_LIST_HEAD (&info->head); info->ident = ident;
_
Quoted 65 lines · Andrew Morton
Quoted 23 lines · On Wed, 17 Apr 2019 15:53:28 -0700 Tri Vo <[email protected]> wrote:
LLVM uses profiling data that's deliberately similar to GCC, but has a very different way of exporting that data. LLVM calls
llvm_gcov_init()once per module, and provides a couple of callbacks that we can use to ask for more data.We care about the "writeout" callback, which in turn calls back into compiler-rt/this module to dump all the gathered coverage data to disk:
llvm_gcda_start_file()
llvm_gcda_emit_function()
llvm_gcda_emit_arcs()
llvm_gcda_emit_function()
llvm_gcda_emit_arcs()
[... repeats for each function ...]
llvm_gcda_summary_info()
llvm_gcda_end_file()This design is much more stateless and unstructured than gcc's, and is intended to run at process exit. This forces us to keep some local state about which module we're dealing with at the moment. On the other hand, it also means we don't depend as much on how LLVM represents profiling data internally.
Quoted 3 lines
See LLVM's
lib/Transforms/Instrumentation/GCOVProfiling.cppfor more details on how this works, particularlyGCOVProfiler::emitProfileArcs(),GCOVProfiler::insertCounterWriteout(), andGCOVProfiler::insertFlush().checkpatch speaketh truth.
Also, I'll change those Co-authored-by's to the documented Co-developed-by.
From: Andrew Morton <[email protected]>
Subject: gcov-clang-support-checkpatch-fixesWARNING: Non-standard signature: Co-authored-by:
#31:Co-authored-by Nick Desaulniers <[email protected]>WARNING: Non-standard signature: Co-authored-by:
#32:Co-authored-by Tri Vo <[email protected]>WARNING: Possible unnecessary 'out of memory' message #158: FILE:
kernel/gcov/clang.c:90:(quoted diff) +2
-0+ if (!info) { + pr_warn_ratelimited("failed to allocate gcov info\n");WARNING: Possible unnecessary 'out of memory' message #193: FILE:
kernel/gcov/clang.c:125:(quoted diff) +2
-0+ if (!info) { + pr_warn_ratelimited("failed to allocate gcov function info for %s\n",WARNING: line over 80 characters
#546: FILE:kernel/gcov/clang.c:478:
+ pos +=store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);total: 0 errors, 5 warnings, 663 lines checked
NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.
Quoted 6 lines
./patches/gcov-clang-support.patchhas style problems, please review.NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH inMAINTAINERS.Please run checkpatch prior to sending patches
Thanks! I'll keep that in mind.