Skip to contentSkip to conversation

gcov: integrate gcov into kbuild

RFC PATCH In review Amerigo Wang 2 participants 8 messages Sep 4, 2009 – Sep 7, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover gcov: integrate gcov into kbuild

This patchset is mainly to integrate gcov into kbuild system, so that we can use "make foo/bar.c.gcov" to get the gcov files. Also moves the useful scripts in gcov.txt into tools/gcov/ direcotry.

Cc Peter Oberparleiter <[email protected]>
Cc Sam Ravnborg <[email protected]>
---
 .gitignore                    |    1 
 Documentation/gcov.txt        |   66 +++---------------------------------------
 Makefile                      |    8 +++--
 scripts/Makefile.build        |    7 ++++
 scripts/gen_gcov.sh           |   49 +++++++++++++++++++++++++++++++
 tools/gcov/gather_on_build.sh |   24 +++++++++++++++
 tools/gcov/gather_on_test.sh  |   20 ++++++++++++
 7 files changed, 113 insertions(+), 62 deletions(-)
#1 lore raw

PATCH 1/5 gitignore: add .gcov files

Add *.gcov into .gitignore.

---

.gitignore +1 -0
Index: linux-2.6/.gitignore
===================================================================
@@ -28,6 +28,7 @@
 *.lzma
 *.patch
 *.gcno
+*.gcov
 
 #
 # Top-level generic files
#2 lore raw

PATCH 2/5 Makefile: clean .gcov files

In reply to Amerigo Wang

Clean *.gcov files when "make clean".

---

Makefile +2 -1
Index: linux-2.6/Makefile
===================================================================
@@ -1218,7 +1218,8 @@ clean: archclean $(clean-dirs)
 		-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \
 		-o -name '*.symtypes' -o -name 'modules.order' \
 		-o -name 'Module.markers' -o -name '.tmp_*.o.*' \
-		-o -name '*.gcno' \) -type f -print | xargs rm -f
+		-o -name '*.gcno' -o -name '*.gcov' \) \
+		-type f -print | xargs rm -f
 
 # mrproper - Delete all generated files, including .config
 #
#3 lore raw

PATCH 3/5 doc: move the scripts in gcov.txt to tools/gcov/

In reply to Amerigo Wang

Move the two bash scripts in gcov.txt into tools/gcov/, so that we can use them directly.

---

Documentation/gcov.txt +6 -60
Index: linux-2.6/Documentation/gcov.txt
===================================================================
@@ -8,8 +8,6 @@ Using gcov with the Linux kernel
 5. Modules
 6. Separated build and test machines
 7. Troubleshooting
-Appendix A: sample script: gather_on_build.sh
-Appendix B: sample script: gather_on_test.sh
 
 
 1. Introduction
@@ -155,6 +153,8 @@ exact same file system location on the t
 machine. If any of the path components is symbolic link, the actual
 directory needs to be used instead (due to make's CURDIR handling).
 
+Try tools/gcov/gather_on_build.sh.
+
 b) gcov is run on the BUILD machine
 
 The following files need to be copied after each test case from test
@@ -167,6 +167,8 @@ from the gcov directory in sysfs:
 These files can be copied to any location on the build machine. gcov
 must then be called with the -o option pointing to that directory.
 
+Try tools/gcov/gather_on_test.sh.
+
 Example directory setup on the build machine:
 
   /tmp/linux:    kernel source tree
@@ -192,62 +194,6 @@ Problem:  Files copied from sysfs appear
 Cause:    Due to the way seq_file works, some tools such as cp or tar
           may not correctly copy files from sysfs.
 Solution: Use 'cat' to read .gcda files and 'cp -d' to copy links.
-          Alternatively use the mechanism shown in Appendix B.
-
-
-Appendix A: gather_on_build.sh
-==============================
-
-Sample script to gather coverage meta files on the build machine
-(see 6a):
-#!/bin/bash
-
-KSRC=$1
-KOBJ=$2
-DEST=$3
-
-if [ -z "$KSRC" ] || [ -z "$KOBJ" ] || [ -z "$DEST" ]; then
-  echo "Usage: $0 <ksrc directory> <kobj directory> <output.tar.gz>" >&2
-  exit 1
-fi
-
-KSRC=$(cd $KSRC; printf "all:\n\t@echo \${CURDIR}\n" | make -f -)
-KOBJ=$(cd $KOBJ; printf "all:\n\t@echo \${CURDIR}\n" | make -f -)
-
-find $KSRC $KOBJ \( -name '*.gcno' -o -name '*.[ch]' -o -type l \) -a \
-                 -perm /u+r,g+r | tar cfz $DEST -P -T -
-
-if [ $? -eq 0 ] ; then
-  echo "$DEST successfully created, copy to test system and unpack with:"
-  echo "  tar xfz $DEST -P"
-else
-  echo "Could not create file $DEST"
-fi
-
-
-Appendix B: gather_on_test.sh
-=============================
-
-Sample script to gather coverage data files on the test machine
-(see 6b):
-
-#!/bin/bash -e
-
-DEST=$1
-GCDA=/sys/kernel/debug/gcov
-
-if [ -z "$DEST" ] ; then
-  echo "Usage: $0 <output.tar.gz>" >&2
-  exit 1
-fi
-
-TEMPDIR=$(mktemp -d)
-echo Collecting data..
-find $GCDA -type d -exec mkdir -p $TEMPDIR/\{\} \;
-find $GCDA -name '*.gcda' -exec sh -c 'cat < $0 > '$TEMPDIR'/$0' {} \;
-find $GCDA -name '*.gcno' -exec sh -c 'cp -d $0 '$TEMPDIR'/$0' {} \;
-tar czf $DEST -C $TEMPDIR sys
-rm -rf $TEMPDIR
+          Alternatively use the mechanism shown in
+          tools/gcov/gather_on_test.sh.
 
-echo "$DEST successfully created, copy to build system and unpack with:"
-echo "  tar xfz $DEST"
tools/gcov/gather_on_build.sh new file +24 -0
Index: linux-2.6/tools/gcov/gather_on_build.sh
===================================================================
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+KSRC=$1
+KOBJ=$2
+DEST=$3
+
+if [ -z "$KSRC" ] || [ -z "$KOBJ" ] || [ -z "$DEST" ]; then
+  echo "Usage: $0 <ksrc directory> <kobj directory> <output.tar.gz>" >&2
+  exit 1
+fi
+
+KSRC=$(cd $KSRC; printf "all:\n\t@echo \${CURDIR}\n" | make -f -)
+KOBJ=$(cd $KOBJ; printf "all:\n\t@echo \${CURDIR}\n" | make -f -)
+
+find $KSRC $KOBJ \( -name '*.gcno' -o -name '*.[ch]' -o -type l \) -a \
+                 -perm /u+r,g+r | tar cfz $DEST -P -T -
+
+if [ $? -eq 0 ] ; then
+  echo "$DEST successfully created, copy to test system and unpack with:"
+  echo "  tar xfz $DEST -P"
+else
+  echo "Could not create file $DEST"
+fi
+
tools/gcov/gather_on_test.sh new file +20 -0
Index: linux-2.6/tools/gcov/gather_on_test.sh
===================================================================
@@ -0,0 +1,20 @@
+#!/bin/bash -e
+
+DEST=$1
+GCDA=/sys/kernel/debug/gcov
+
+if [ -z "$DEST" ] ; then
+  echo "Usage: $0 <output.tar.gz>" >&2
+  exit 1
+fi
+
+TEMPDIR=$(mktemp -d)
+echo Collecting data..
+find $GCDA -type d -exec mkdir -p $TEMPDIR/\{\} \;
+find $GCDA -name '*.gcda' -exec sh -c 'cat < $0 > '$TEMPDIR'/$0' {} \;
+find $GCDA -name '*.gcno' -exec sh -c 'cp -d $0 '$TEMPDIR'/$0' {} \;
+tar czf $DEST -C $TEMPDIR sys
+rm -rf $TEMPDIR
+
+echo "$DEST successfully created, copy to build system and unpack with:"
+echo "  tar xfz $DEST"
#4 lore raw

PATCH 4/5 scripts: add gen_gcov.sh

In reply to Amerigo Wang

Add scripts/gen_gcov.sh which will be used by the later patch. It is used to generate .gcov file from .c file.

---

scripts/gen_gcov.sh new file +49 -0
Index: linux-2.6/scripts/gen_gcov.sh
===================================================================
@@ -0,0 +1,49 @@
+#!/bin/bash
+# WANG Cong <[email protected]>
+# GPLv2 applies.
+
+GCOV_BASE=/sys/kernel/debug/gcov$(readlink /lib/modules/`uname -r`/build)
+target=""
+trap  "rm -f *.gcov" INT
+
+function usage()
+{
+	echo "$0 [GCOV_BASE_DIR] target_file.c"
+	echo "The default directory is: /sys/kernel/debug/gcov/path/to/compile."
+}
+
+if [ $# -eq 2 ];
+then
+	GCOV_BASE="$1"
+	target="$2"
+elif [ $# -eq 1 ];
+then
+	target="$1"
+	if [ ! -d "$GCOV_BASE" ];
+	then
+		echo "You have to provide the base directory for gcov." >&2
+		exit 1
+	fi
+else
+	usage
+	exit 1
+fi
+
+if [ ! -f "$target" ];
+then
+	echo "File $target doesn't exist.\n" 1>&2
+	exit 1
+fi
+
+target_base_name=$(basename "$target")
+target_dir_name=$(dirname "$target")
+
+${GCOV} -o "$GCOV_BASE/$target_dir_name" "$target_base_name"
+if [ -f "${target_base_name}.gcov" ];
+then
+	mv *.gcov "$target_dir_name"
+	exit 0
+else
+	exit 1
+fi
+
#5 lore raw

PATCH 5/5 Makefile: implement "make foo.c.gcov"

In reply to Amerigo Wang

Implement "make path/to/file.c.gcov", also introduce a new env variable "GCOV_OBJ_DIR" which should point to the objects directory of gcov.
(defaults to /sys/kernel/debug/gcov/path/to/compile)

---

scripts/Makefile.build +7 -0
Index: linux-2.6/scripts/Makefile.build
===================================================================
@@ -169,6 +169,13 @@ cmd_cc_symtypes_c =                     
 $(obj)/%.symtypes : $(src)/%.c FORCE
 	$(call cmd,cc_symtypes_c)
 
+ifdef CONFIG_GCOV_KERNEL
+cmd_gcov_gcov_c       = $(srctree)/scripts/gen_gcov.sh $(GCOV_OBJ_DIR) $<
+
+$(obj)/%.c.gcov: $(src)/%.c FORCE
+	$(call cmd,gcov_gcov_c)
+endif
+
 # C (.c) files
 # The C file is compiled and updated dependency information is generated.
 # (See cmd_cc_o_c + relevant part of rule_cc_o_c)
Makefile +4 -1
Index: linux-2.6/Makefile
===================================================================
@@ -313,6 +313,7 @@ NM		= $(CROSS_COMPILE)nm
 STRIP		= $(CROSS_COMPILE)strip
 OBJCOPY		= $(CROSS_COMPILE)objcopy
 OBJDUMP		= $(CROSS_COMPILE)objdump
+GCOV		= $(CROSS_COMPILE)gcov
 AWK		= awk
 GENKSYMS	= scripts/genksyms/genksyms
 DEPMOD		= /sbin/depmod
@@ -353,7 +354,7 @@ KERNELVERSION = $(VERSION).$(PATCHLEVEL)
 
 export VERSION PATCHLEVEL SUBLEVEL KERNELRELEASE KERNELVERSION
 export ARCH SRCARCH CONFIG_SHELL HOSTCC HOSTCFLAGS CROSS_COMPILE AS LD CC
-export CPP AR NM STRIP OBJCOPY OBJDUMP MAKE AWK GENKSYMS PERL UTS_MACHINE
+export CPP AR NM STRIP OBJCOPY OBJDUMP MAKE AWK GENKSYMS PERL UTS_MACHINE GCOV
 export HOSTCXX HOSTCXXFLAGS LDFLAGS_MODULE CHECK CHECKFLAGS
 
 export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS LDFLAGS
@@ -1517,6 +1518,8 @@ endif
 	$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
 %.o: %.c prepare scripts FORCE
 	$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
+%.c.gcov: %.c prepare scripts FORCE
+	$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
 %.lst: %.c prepare scripts FORCE
 	$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
 %.s: %.S prepare scripts FORCE
#6 lore raw
In reply to Amerigo Wang
Quoted 3 lines · Amerigo Wang

This patchset is mainly to integrate gcov into kbuild system, so that we can use "make foo/bar.c.gcov" to get the gcov files. Also moves the useful scripts in gcov.txt into tools/gcov/ direcotry.

Interesting approach - this should definitely make it more easy for developers to get coverage data for their code. A couple of thoughts/observations:

- does not work when compiling with a separate build directory (make O=)

/sys/kernel/debug/gcov//mnt/out//mnt/linux-2.6.gcno:cannot open graph file make[2]: *** [init/main.c.gcov] Error 1
make[1]: *** [init/main.c.gcov] Error 2
make: *** [sub-make] Error 2

This is most likely due to target_base_name being an absolute path (relative to srctree) in case build and source directories are different.

- gcov call should use option -p, otherwise gcov may overwrite .gcov files if a .c file includes two .h files from different directories.
- an option to generate .gcov files for all .c files in a subdirectory would be nice

Regards,
Peter Oberparleiter

#7 lore raw
Quoted 6 lines · Peter Oberparleiter
Quoted 3 lines · Amerigo Wang

This patchset is mainly to integrate gcov into kbuild system, so that we can use "make foo/bar.c.gcov" to get the gcov files. Also moves the useful scripts in gcov.txt into tools/gcov/ direcotry.

Interesting approach - this should definitely make it more easy for developers to get coverage data for their code. A couple of thoughts/observations:

Thanks!

Quoted 10 lines

- does not work when compiling with a separate build directory (make O=)

/sys/kernel/debug/gcov//mnt/out//mnt/linux-2.6.gcno:cannot open graph file make[2]: *** [init/main.c.gcov] Error 1
make[1]: *** [init/main.c.gcov] Error 2
make: *** [sub-make] Error 2

This is most likely due to target_base_name being an absolute path (relative to srctree) in case build and source directories are different.

Hmm, yeah, I will fix it.

Quoted 2 lines

- gcov call should use option -p, otherwise gcov may overwrite .gcov files if a .c file includes two .h files from different directories.

Ok.

Quoted 2 lines

- an option to generate .gcov files for all .c files in a subdirectory would be nice

Nice idea! I will add this.

Thank you, I will update and resend them!