|
NAMEkcov —
interface for collecting kernel code coverage information
SYNOPSISTo compile KCOV into the kernel, place the following lines in your kernel configuration file:options COVERAGE
options KCOV The following header file defines the application interface provided by KCOV:
DESCRIPTIONkcov is a module that enables collection of code
coverage information from the kernel. It relies on code instrumentation
enabled by the COVERAGE kernel option. When
kcov is enabled by a user-mode thread, it collects
coverage information only for that thread, excluding hard interrupt handlers.
As a result, kcov is not suited to collect
comprehensive coverage data for the entire kernel; its main purpose is to
provide input for coverage-guided system call fuzzers.
In typical usage, a user-mode thread first allocates a chunk of
memory to be shared with The shared memory buffer can be treated as a 64-bit unsigned
integer followed by an array of records. The integer records the number of
valid records and is updated by the kernel as coverage information is
recorded. The state of the tracing buffer can be reset by writing the value
0 to this field. The record layout depends on the tracing mode set using the
Two tracing modes are implemented,
The second and third fields record the values of the two variables, and the fourth and final field stores the program counter value of the comparison. IOCTL INTERFACEApplications interact withkcov using the
ioctl(2)
system call. Each thread making use of kcov must use a
separate file descriptor for /dev/kcov. The following
ioctls are defined:
FILESkcov creates the /dev/kcov
device file.
EXAMPLESThe following code sample collects information about basic block coverage for kernel code executed while printing ‘Hello,
world ’.
size_t sz; uint64_t *buf; int fd; fd = open("/dev/kcov", O_RDWR); if (fd == -1) err(1, "open(/dev/kcov)"); sz = 1ul << 20; /* 1MB */ if (ioctl(fd, KIOSETBUFSIZE, sz / KCOV_ENTRY_SIZE) != 0) err(1, "ioctl(KIOSETBUFSIZE)"); buf = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (buf == MAP_FAILED) err(1, "mmap"); /* Enable PC tracing. */ if (ioctl(fd, KIOENABLE, KCOV_MODE_TRACE_PC) != 0) err(1, "ioctl(KIOENABLE)"); /* Clear trace records from the preceding ioctl() call. */ buf[0] = 0; printf("Hello, world!\n"); /* Disable PC tracing. */ if (ioctl(fd, KIODISABLE, 0) != 0) err(1, "ioctl(KIODISABLE)"); for (uint64_t i = 1; i < buf[0]; i++) printf("%#jx\n", (uintmax_t)buf[i]); # ./kcov-test | sed 1d | addr2line -e /usr/lib/debug/boot/kernel/kernel.debug SEE ALSOioctl(2), mmap(2)HISTORYkcov first appeared in FreeBSD
13.0.
BUGSThe FreeBSD implementation ofkcov does not yet support remote tracing.
Visit the GSP FreeBSD Man Page Interface. |