How to use the xcode's code coverage for kext

Hi,


We want to apply Unit Test upon the kext, specifically to get the code coverage data for the UT. But seems the builtin code coverage test tool from xcode is for the userland code, so the question -- is there any best known method to apply the code coverage tool for the kext?

Replies

What sort of KEXT is this?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

It's a generic kext. We want to know the code coverage percentage when doing the unit test :-) But seems there's no guidance for those areas no matter from Apple or other googled websites...

I generally do this work outside of the kernel. That is, I set up my core kernel code so that it can build both for kernel and user space, and I do my unit tests in user space. At that point I have access to all the standard user space tools.

How do you even do unit tests in kernel space? There’s no infrastructure for that either.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

It's not easy for us to pull the code outside the kernel to userland since we have a tied inferustruct between userspace and kernel space such as ioctl and other stuffs, which can't be mocked in user space. So for the unit test in kernel, actually it should be one for the userspace code, we want to get the code coverage data during runtime when we do that...


Thanks to confirm that!

To be clear, if the code you’re testing is tightly bound to all that kernel infrastructure, I don’t consider it a unit test. By definition, a unit test is meant to test things in isolation.

Regardless, coming back to your main question, code coverage, I don’t think there’s any Apple infrastructure for doing that in kernel code. Normally I’d recommend that you file an enhancement request for such support, but given this year’s announcements at WWDC I doubt there’s much point doing that.

You may be able to hack something together by repurposing Clang’s user space support. Conceptually, code coverage is pretty simple: The compiler instruments the code to update counters as it runs, and there’s runtime support to dump those counters when the program exits. It’s that last bit you’d need to hack on. I don’t have a lot of experience with this but my initial impression is that this would be feasible but not trivial.

A good place to start is the Clang doc on the subject. After that I recommend that you play around a bit on your own and then, if you get stuck, seek help via the Clang support channels.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"