gcov: add Clang support
Subsystems (3)
v3 cover gcov: add Clang support
This patch series adds Clang supoprt 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 | 84 +---- kernel/gcov/clang.c | 555 +++++++++++++++++++++++++++++++ kernel/gcov/gcc_base.c | 86 +++++ kernel/gcov/gcov.h | 3 + 7 files changed, 665 insertions(+), 89 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
--
2.20.1.321.g9e740568ce-goog
PATCH v3 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
@@ -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
@@ -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.20.1.321.g9e740568ce-goog
Re: [PATCH v3 1/3] gcov: Clang: move common GCC code into gcc_base.c
Quoted 5 lines · Tri Vo
From: Greg Hackmann <[email protected]>
base.ccontains a few callbacks specific to GCC's gcov implementation. Move these into their own module in preparation for Clang support.
Quoted 7 lines
Signed-off-by Greg Hackmann <[email protected]>Signed-off-by Nick Desaulniers <[email protected]>Signed-off-by Tri Vo <[email protected]>Tested-by Trilok Soni <[email protected]>Tested-by Prasad Sodagudi <[email protected]>Tested-by Tri Vo <[email protected]>---
Looks good!
Signature · 2 lines
Peter Oberparleiter Linux on Z Development - IBM Germany
PATCH v3 2/3 gcov: Clang support
In reply to Tri VoFrom: 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/clang.c | 555 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 558 insertions(+), 1 deletion(-) 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/clang.c new file +555 -0
555 lines not on this page · raw message
Signature · 1 line
2.20.1.321.g9e740568ce-goog
Re: [PATCH v3 2/3] gcov: Clang support
Quoted 46 lines · 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.cppfor more details on how this works, particularlyGCOVProfiler::emitProfileArcs(),GCOVProfiler::insertCounterWriteout(), andGCOVProfiler::insertFlush().Co-authored-by Nick Desaulniers <[email protected]>Co-authored-by Tri Vo <[email protected]>Signed-off-by Greg Hackmann <[email protected]>Signed-off-by Nick Desaulniers <[email protected]>Signed-off-by Tri Vo <[email protected]>Tested-by Trilok Soni <[email protected]>Tested-by Prasad Sodagudi <[email protected]>Tested-by Tri Vo <[email protected]>Tested-by Daniel Mentz <[email protected]>Tested-by Petri Gynther <[email protected]>--- kernel/gcov/Kconfig | 3 +- kernel/gcov/Makefile | 1 + kernel/gcov/clang.c | 555 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 558 insertions(+), 1 deletion(-) create mode 100644 kernel/gcov/clang.c
[...]
Quoted 6 lines
kernel/gcov/clang.c new file +0
-0@@ -0,0 +1,555 @@
[...]
kernel/gcov/clang.c line 56
Quoted 10 lines
kernel/gcov/clang.c new file +10
-0+#include <linux/kernel.h> +#include <linux/list.h> +#include <linux/printk.h> +#include <linux/ratelimit.h> +#include <linux/seq_file.h> +#include <linux/slab.h> +#include <linux/vmalloc.h> +#include "gcov.h" + +#define GCOV_TAG_FUNCTION_LENGTH 3
This define seems to be unused and should be removed.
[...]
kernel/gcov/clang.c line 216
Quoted 9 lines
kernel/gcov/clang.c new file +9
-0+/** +* gcov_info_unlink - unlink/remove profiling data set from the list +* @prev: previous profiling data set +* @info: profiling data set +*/ +void gcov_info_unlink (struct gcov_info *prev,struct gcov_info *info) +{ +if (info) +list_del (&info->head);
The if-clause can be removed since @info is always non-NULL.
[...]
kernel/gcov/clang.c line 274
Quoted 4 lines
kernel/gcov/clang.c new file +4
-0+static struct gcov_fn_info *gcov_fn_info_dup (struct gcov_fn_info *fn) +{ +size_t cv_size;/* counter values size */ +
This empty line should be removed.
[...]
Rest looks good! With these minor changes applied:
Signature · 2 lines
Peter Oberparleiter Linux on Z Development - IBM Germany
PATCH v3 3/3 gcov: docs: add a note on GCC vs Clang differences
In reply to Tri VoDocument 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.
--- Documentation/dev-tools/gcov.rst | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
Documentation/dev-tools/gcov.rst +14 -4
@@ -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.20.1.321.g9e740568ce-goog
Re: [PATCH v3 3/3] gcov: docs: add a note on GCC vs Clang differences
Quoted 6 lines · 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.Signed-off-by Tri Vo <[email protected]>
Thanks for adding this note (I made the mistake myself when testing out these patches that the coverage format is slightly different). And good find removing the documentation on the now obsolete config.
Quoted 43 lines
--- Documentation/dev-tools/gcov.rst | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)Documentation/dev-tools/gcov.rst +14
-5@@ -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 --------------- --2.20.1.321.g9e740568ce-goog
Signature · 2 lines
Thanks, ~Nick Desaulniers
Re: [PATCH v3 3/3] gcov: docs: add a note on GCC vs Clang differences
In reply to Tri VoQuoted 3 lines · 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.
For completeness sake please also add something like the following to the commit message:
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").
Quoted 5 lines
Signed-off-by Tri Vo <[email protected]>--- Documentation/dev-tools/gcov.rst | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
Signature · 2 lines
Peter Oberparleiter Linux on Z Development - IBM Germany
Quoted 40 lines · Tri Vo
This patch series adds Clang supoprt 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 intogcc_base.c
gcov: Clang supportTri Vo (1):
gcov: docs: add a note on GCC vs Clang differencesDocumentation/dev-tools/gcov.rst | 18 +- kernel/gcov/Kconfig | 3 +- kernel/gcov/Makefile | 5 +- kernel/gcov/base.c | 84 +---- kernel/gcov/clang.c | 555 +++++++++++++++++++++++++++++++ kernel/gcov/gcc_base.c | 86 +++++ kernel/gcov/gcov.h | 3 + 7 files changed, 665 insertions(+), 89 deletions(-) create mode 100644 kernel/gcov/clang.c create mode 100644 kernel/gcov/gcc_base.cv2:
- 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()togcc_base.c
- Added missing header togcc_base.c
- Removed unnecessary boundary checks ingcov_info_add().
- Changed counters' allocation to usevmalloc().
- Added check for failed allocation of filename.
- Changedlist_for_each_entry_safetolist_for_each_entrywhen traversing without modifying.
- UpdatedDocumentation/dev-tools/gcov.rst
Quoted 3 lines
--
2.20.1.321.g9e740568ce-goog
Bumping this thread for review.
Signature · 2 lines
Thanks, ~Nick Desaulniers
Quoted 46 lines · Nick Desaulniers
Quoted 40 lines · Tri Vo
This patch series adds Clang supoprt 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 intogcc_base.c
gcov: Clang supportTri Vo (1):
gcov: docs: add a note on GCC vs Clang differencesDocumentation/dev-tools/gcov.rst | 18 +- kernel/gcov/Kconfig | 3 +- kernel/gcov/Makefile | 5 +- kernel/gcov/base.c | 84 +---- kernel/gcov/clang.c | 555 +++++++++++++++++++++++++++++++ kernel/gcov/gcc_base.c | 86 +++++ kernel/gcov/gcov.h | 3 + 7 files changed, 665 insertions(+), 89 deletions(-) create mode 100644 kernel/gcov/clang.c create mode 100644 kernel/gcov/gcc_base.cv2:
- 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()togcc_base.c
- Added missing header togcc_base.c
- Removed unnecessary boundary checks ingcov_info_add().
- Changed counters' allocation to usevmalloc().
- Added check for failed allocation of filename.
- Changedlist_for_each_entry_safetolist_for_each_entrywhen traversing without modifying.
- UpdatedDocumentation/dev-tools/gcov.rstQuoted 3 lines
--
2.20.1.321.g9e740568ce-goog
Quoted 5 lines
Bumping this thread for review.
--
Thanks,
~Nick Desaulniers
Bumping for review; KernelCI is ready to enable clang builds. This is the final build error for arm64 allyesconfig + clang.
Signature · 2 lines
Thanks, ~Nick Desaulniers
Peter Oberparleiter
maintainerQuoted 5 lines · Tri Vo
This patch series adds Clang supoprt 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.
Thanks for the updates! I've provided suggestions for some minor improvements in my other review e-mails.
With those changes applied, the patch set is in my opinion ready for inclusion into the mainline kernel. My suggestion would be to re-post the resulting version while putting Andrew Morton on CC as gcov changes are typically integrated via his tree.
Also I've successfully re-tested this patch set version on s390 using GCC 7.3.0 to ensure that the existing GCC support is still working. Unfortunately I wasn't able to test the Clang version due to some compile problems on s390 (unrelated to this patch set).
If you haven't done so, I would like to suggest to run the following tests with the Clang gcov-kernel version that should catch problems in some corner cases:
1. Unload a module, then use llvm-cov on the associated coverage file.
Expectation: correct llvm-cov output including coverage of module exit code.
2. Unload a module, modify its source, re-compile it and load it again, then use llvm-cov on the associated coverage file.
Expectation: kernel message "discarding saved data", correct llvm-cov output with no coverage of module exit code.
3. Unload a module, then reset all coverage data by writing to /sys/kernel/debug/gcov/reset.
Expectation: all coverage files associated with the module are removed from debugfs.
Signature · 2 lines
Peter Oberparleiter Linux on Z Development - IBM Germany
Quoted 33 lines · Peter Oberparleiter
Quoted 5 lines · Tri Vo
This patch series adds Clang supoprt 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.
Thanks for the updates! I've provided suggestions for some minor improvements in my other review e-mails.
With those changes applied, the patch set is in my opinion ready for inclusion into the mainline kernel. My suggestion would be to re-post the resulting version while putting Andrew Morton on CC as gcov changes are typically integrated via his tree.
Also I've successfully re-tested this patch set version on s390 using GCC 7.3.0 to ensure that the existing GCC support is still working. Unfortunately I wasn't able to test the Clang version due to some compile problems on s390 (unrelated to this patch set).
If you haven't done so, I would like to suggest to run the following tests with the Clang gcov-kernel version that should catch problems in some corner cases:
1. Unload a module, then use llvm-cov on the associated coverage file.
Expectation: correct llvm-cov output including coverage of module exit code.
2. Unload a module, modify its source, re-compile it and load it again, then use llvm-cov on the associated coverage file.
Quoted 8 lines
Expectation: kernel message "discarding saved data", correct llvm-cov output with no coverage of module exit code.
3. Unload a module, then reset all coverage data by writing to
/sys/kernel/debug/gcov/reset.Expectation: all coverage files associated with the module are removed from debugfs.
Thanks for the suggested test cases! The current patchset doesn't seem to handle module loading/unloading correctly. I'll fix that in a follow-up.