Skip to contentSkip to conversation

gcov: add Clang support

PATCH Superseded Tri Vo 1 participant 4 messages Mar 18, 2019

Subsystems (3)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Tri Vo

author
#0 lore raw

v4 cover gcov: add Clang support

This patch series adds Clang support for gcov.

Patch 1 refactors existing code in preparation for Clang support. Patch 2 implements necessary LLVM runtime hooks and gcov kernel interfaces. Patch 3 updates documentation.

Greg Hackmann (2):
gcov: Clang: move common GCC code into gcc_base.c
gcov: Clang support

Tri Vo (1):
gcov: docs: add a note on GCC vs Clang differences

 Documentation/dev-tools/gcov.rst |  18 +-
 kernel/gcov/Kconfig              |   3 +-
 kernel/gcov/Makefile             |   5 +-
 kernel/gcov/base.c               |  86 +----
 kernel/gcov/clang.c              | 586 +++++++++++++++++++++++++++++++
 kernel/gcov/gcc_3_4.c            |  12 +
 kernel/gcov/gcc_4_7.c            |  12 +
 kernel/gcov/gcc_base.c           |  86 +++++
 kernel/gcov/gcov.h               |   5 +
 9 files changed, 723 insertions(+), 90 deletions(-)
 create mode 100644 kernel/gcov/clang.c
 create mode 100644 kernel/gcov/gcc_base.c

v2:
- Reorganized config dependencies, as per Masahiro.

v3:
- Squashed patches 2-4 of v2, as per Nick, Masahiro, and Peter. Addressed comments by Peter:
- Moved __gcov_exit() to gcc_base.c
- Added missing header to gcc_base.c
- Removed unnecessary boundary checks in gcov_info_add().
- Changed counters' allocation to use vmalloc().
- Added check for failed allocation of filename.
- Changed list_for_each_entry_safe to list_for_each_entry when traversing without modifying.
- Updated Documentation/dev-tools/gcov.rst

v4:
Made following changes to pass kernel module test cases suggested by Peter:
- Generic code in base.c unlinks gcov_info data sets while iterating over the gcov_info list. Changed Clang's gcov_info_unlink() to preserve next and prev links of the unlinked gcov_info instance.
- Attributing a given gcov_info to a module is done differently in Clang vs GCC. Generic code in base.c needs to do this. So added gcov_info_within_module() interface to gcov.h to abstract away the difference.
- Clang provides checksums for each function. These checksums are used to determine whether the source files of the compiled kernel module have been modified. Changed gcov_info_is_compatible() to account for function-level checksums.
Addressed comments by Peter:
- Removed unnecessary #define, if-clause, empty line.
- Amended commit message to note documentation was adjusted to account for the removal of config symbol CONFIG_GCOV_FORMAT_AUTODETECT.

--
2.21.0.225.g810b269d1ac-goog

author
#1 lore raw

PATCH v4 1/3 gcov: Clang: move common GCC code into gcc_base.c

From: Greg Hackmann <[email protected]>

base.c contains a few callbacks specific to GCC's gcov implementation. Move these into their own module in preparation for Clang support.

---
 kernel/gcov/Makefile   |  4 +-
 kernel/gcov/base.c     | 84 +----------------------------------------
 kernel/gcov/gcc_base.c | 86 ++++++++++++++++++++++++++++++++++++++++++
 kernel/gcov/gcov.h     |  3 ++
 4 files changed, 93 insertions(+), 84 deletions(-)
 create mode 100644 kernel/gcov/gcc_base.c
kernel/gcov/Makefile +2 -2
index ff06d64df397..45431ed679d1 100644
@@ -2,5 +2,5 @@
 ccflags-y := -DSRCTREE='"$(srctree)"' -DOBJTREE='"$(objtree)"'
 
 obj-y := base.o fs.o
-obj-$(CONFIG_GCOV_FORMAT_3_4) += gcc_3_4.o
-obj-$(CONFIG_GCOV_FORMAT_4_7) += gcc_4_7.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
kernel/gcov/base.c +2 -82

90 lines not on this page · raw message

kernel/gcov/gcc_base.c new file +86 -0

86 lines not on this page · raw message

kernel/gcov/gcov.h +3 -0
index de118ad4a024..0ecf1d664ec3 100644
@@ -83,4 +83,7 @@ struct gcov_link {
 };
 extern const struct gcov_link gcov_link[];
 
+extern int gcov_events_enabled;
+extern struct mutex gcov_lock;
+
 #endif /* GCOV_H */
Signature · 1 line
2.21.0.225.g810b269d1ac-goog
author
#2 lore raw

PATCH v4 2/3 gcov: Clang support

In reply to Tri Vo

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().

---
 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
index 1e3823fa799b..f71c1adcff31 100644
@@ -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
index 45431ed679d1..d66a74b0f100 100644
@@ -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
index 799d42072727..0ffe9f194080 100644
@@ -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
index 1e32e66c9563..64d2dd9f7c64 100644
@@ -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
index ca5e5c0ef853..ec37563674d6 100644
@@ -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
index 0ecf1d664ec3..6ab2c1808c9d 100644
@@ -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 {
Signature · 1 line
2.21.0.225.g810b269d1ac-goog
author
#3 lore raw

PATCH v4 3/3 gcov: docs: add a note on GCC vs Clang differences

In reply to Tri Vo

Document some things of note to gcov users:
1. GCC gcov and Clang llvm-cov tools are not compatible.
2. The use of GCC vs Clang is transparent at build-time.

Also adjust the documentation to account for the removal of config symbol CONFIG_GCOV_FORMAT_AUTODETECT by commit 6a61b70b43c9 ("gcov: remove CONFIG_GCOV_FORMAT_AUTODETECT").

---
 Documentation/dev-tools/gcov.rst | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
Documentation/dev-tools/gcov.rst +14 -4
index 69a7d90c320a..46aae52a41d0 100644
@@ -34,10 +34,6 @@ Configure the kernel with::
         CONFIG_DEBUG_FS=y
         CONFIG_GCOV_KERNEL=y
 
-select the gcc's gcov format, default is autodetect based on gcc version::
-
-        CONFIG_GCOV_FORMAT_AUTODETECT=y
-
 and to get coverage data for the entire kernel::
 
         CONFIG_GCOV_PROFILE_ALL=y
@@ -169,6 +165,20 @@ b) gcov is run on the BUILD machine
       [user@build] gcov -o /tmp/coverage/tmp/out/init main.c
 
 
+Note on compilers
+-----------------
+
+GCC and LLVM gcov tools are not necessarily compatible. Use gcov_ to work with
+GCC-generated .gcno and .gcda files, and use llvm-cov_ for Clang.
+
+.. _gcov: http://gcc.gnu.org/onlinedocs/gcc/Gcov.html
+.. _llvm-cov: https://llvm.org/docs/CommandGuide/llvm-cov.html
+
+Build differences between GCC and Clang gcov are handled by Kconfig. It
+automatically selects the appropriate gcov format depending on the detected
+toolchain.
+
+
 Troubleshooting
 ---------------
 
Signature · 1 line
2.21.0.225.g810b269d1ac-goog