On 10/27/25 20:42, Kuniyuki Iwashima wrote:
While writing a selftest with kselftest_harness.h, I often want to
check which paths are actually exercised.

Let's support generating KCOV coverage data.

We can specify the output directory via the KCOV_OUTPUT environment
variable, and the number of instructions to collect via the KCOV_SLOTS
environment variable.

   # KCOV_OUTPUT=$PWD/kcov KCOV_SLOTS=$((4096 * 2)) \
     ./tools/testing/selftests/net/af_unix/scm_inq

Both variables can also be specified as the make variable.

   # make -C tools/testing/selftests/ \
     KCOV_OUTPUT=$PWD/kcov KCOV_SLOTS=$((4096 * 4)) \
     kselftest_override_timeout=60 TARGETS=net/af_unix run_tests

The coverage data can be simply decoded with addr2line:

   $ cat kcov/* | sort | uniq | addr2line -e vmlinux | grep unix
   net/unix/af_unix.c:1056
   net/unix/af_unix.c:3138
   net/unix/af_unix.c:3834
   net/unix/af_unix.c:3838
   net/unix/af_unix.c:311 (discriminator 2)
   ...

or more nicely with a script embedded in vock [0]:

   $ cat kcov/* | sort | uniq > local.log
   $ python3 ~/kernel/tools/vock/report.py \
     --kernel-src ./ --vmlinux ./vmlinux \
     --mode local --local-log local.log --filter unix
   ...
   ------------------------------- Coverage Report 
--------------------------------
   📄 net/unix/af_unix.c (276 lines)
    ...
   942 | static int unix_setsockopt(struct socket *sock, int level, int optname,
   943 |                           sockptr_t optval, unsigned int optlen)
   944 | {
    ...
   961 |        switch (optname) {
   962 |        case SO_INQ:
   963 >             if (sk->sk_type != SOCK_STREAM)
   964 |                        return -EINVAL;
   965 |
   966 >             if (val > 1 || val < 0)
   967 |                        return -EINVAL;
   968 |
   969 >             WRITE_ONCE(u->recvmsg_inq, val);
   970 |                break;

Link: 
https://github.com/kzall0c/vock/blob/f3d97de9954f9df758c0ab287ca7e24e654288c7/report.py
 #[0]
Signed-off-by: Kuniyuki Iwashima <[email protected]>
---
v2: Support TEST()
v1: 
https://lore.kernel.org/linux-kselftest/[email protected]/
---
  Documentation/dev-tools/kselftest.rst       |  41 ++++++
  tools/testing/selftests/Makefile            |  14 ++-
  tools/testing/selftests/kselftest_harness.h | 133 +++++++++++++++++++-
  3 files changed, 178 insertions(+), 10 deletions(-)


I am okay with kcov support being added. You probably have to fix a few things.
kernel test robot is still complaining on v2.

thanks,
-- Shuah

Reply via email to