Product
Solutions
Resources
Company
Download Trial Book a Demo

Code Coverage for Device Drivers and Kernel

Generate code coverage for a standalone Linux driver module and a kernel file system with RKTracer. Patch the kernel, build with rktracer, run your tests in the kernel, save the coverage data, and produce an HTML report.

Use this guide to generate code coverage for device drivers and the Linux kernel with RKTracer. The first walkthrough covers a standalone driver module (knetstat) tested in the kernel, and the second covers a kernel file system. In both cases you patch the kernel with the RKTracer patch, build with rktracer prefixed to the cross compiler, run your tests, save the coverage data to RK_MEM.raw, then generate the HTML report with rkresults.

In short

Patch the kernel with the RKTracer patch, build with rktracer prefixed to the compiler, run your tests in the kernel, save RK_MEM.raw, then run rkresults on the host to generate the HTML report.

Standalone driver module (knetstat)

This section shows how to generate code coverage for a standalone driver module (knetstat) and test it in the kernel.

Step 1: Patch the Linux kernel

Patch the Linux kernel with the RKTracer patch.

patch the kernel
patch -p1 < /home/rkvalidate/share/rktracer/01-rktracer.patch

Step 2: Build the knetstat driver module

Build the knetstat driver module with rktracer prefixed to the cross compiler.

build the module
make CC="rktracer -r aarch64-linux-gnu-gcc" ARCH=arm64 LD=aarch64-linux-gnu-ld

Step 3: Start the kernel device or emulator

Start the kernel device or a QEMU 64-bit ARM architecture emulator.

Step 4: Copy the instrumented kernel object

Copy the RKTracer-instrumented knetstat.ko kernel object file to the running kernel.

copy the .ko file
scp rkvalidate@192.168.15.129:*.ko

Step 5: Load and test the module

Load and test knetstat.ko with the command:

load the module
insmod knetstat.ko

Step 6: Save the coverage data

Save the code coverage data to the RK_MEM.raw file.

save coverage data
cat /proc/RK_MEM.raw > RK_MEM.raw

Step 7: Copy to host and generate the report

Copy RK_MEM.raw to the host build system, into the knetstat kernel module folder. Then generate the reports using the rkresults command.

generate the report
rkresults

Kernel file system

This section shows how to generate code coverage for a kernel file system using the RKTracer tool.

Step 1: Patch the Linux kernel

Patch the Linux kernel.

patch the kernel
patch -p1 < /home/rkvalidate/share/rktracer/01-rktracer.patch

Step 2: Enable RKTracer in the file system makefile

Add the patch and enable Enable RKTracer RK_MEM[] in the file system makefile:

file system makefile
CC:=rktracer -r ${CC}

Step 3: Test the file system in kernel mode

Test the file system in kernel mode.

run the tests
./runltp -s fs

Step 4: Load the rkmem.ko interface

Load the rkmem.ko interface.

load the interface
insmod /lib/modules/4.19.16/kernel/drivers/char/rkmem.ko

Step 5: Save the coverage data

Save the code coverage data.

save coverage data
cat /proc/RK_MEM.raw > RK_MEM.raw

Step 6: Copy to host and generate the report

Copy RK_MEM.raw to the host and generate the reports using rkresults.

Enable coverage for selected files and functions

You can scope coverage to specific folders, files, or functions by editing rktracer.config.

Coverage for selected folders

To generate coverage reports only for selected folders, given a folder structure such as:

example folders
C:\project\sound\drivers\base\power\
C:\project\sound\drivers\base\power\firmware_loader\
C:\project\sound\drivers\core\
C:\project\sound\security\keys\
C:\project\sound\security\lockdown\

Edit rktracer.config and add:

rktracer.config
ignore *.c
instrument */power/* */core/* */keys/*
never */firmware_loader/* */lockdown/*
  • ignore *.c ignores all C source files from instrumentation.
  • instrument instruments source files from the given folders.
  • never ignores the selected folder.

Coverage for selected functions

To generate coverage reports only for selected functions from three different files, for example fun_X() in source-file-X, fun_Y() in source-file-Y, and fun_Z() in source-file-Z, edit rktracer.config and set:

rktracer.config
ignore *.c
instrument *source-file-X.c *source-file-Y.c *source-file-Z.c
function-ignore *
function-instrument fun_X() fun_Y() fun_Z()
  • ignore *.c ignores all C programming source files.
  • instrument *file-X.c *file-Y.c *file-Z.c instruments only these three source files.
  • function-ignore * then ignores all functions in the above three files.
  • function-instrument fun_X() fun_Y() fun_Z() does not ignore these three functions from instrumentation.

Next steps

Once your driver module or kernel file system reports coverage, the same RKTracer workflow applies across other build systems and targets. Browse the rest of the documentation, or read more about how RKTracer measures coverage through MC/DC.

Stuck on your build?

Open a support ticket and an engineer who knows your toolchain will help. You get a tracked ticket ID by email.