kcov
kcov copied to clipboard
Trace into C extensions/libraries from Python
Not sure how doable this is, but it'd be incredibly convenient. https://emptysqua.re/blog/code-coverage-python-c-extensions/ is something else I'm looking into.
Interesting idea. Not incredibly easy to implement, but should not be impossible. I guess kcov would have to start the python interpreter and register a breakpoint at dlopen (assuming that's how Python opens C extensions) and then instrument the shared libraries.
I guess an interesting test could be to run kcov on python itself and see if it manages to cover the shared libraries. Or maybe I'm just showing I should google more on how Python extensions are really working :-)
Anyway, there's also a bit of thinking to be done on how to produce the actual report. Kcov doesn't right now report shared libraries separately, but as parts of the main executable. In this case, that would look very strange.
Your hunch was correct I believe.
As it turns out, I don't think any change is necessary from kcov to solve my particular problem (which I didn't explain clearly I believe, made some invalid assumptions).
In short, we use py.test + ctypes to do a bunch of API testing on a C library. Default usage of kcov will not generate coverage, because it'd be generating coverage for your python scripts. I needed to compile python w/ debugging enabled, then explicitly pass in the python executable, and kcov would work just fine off of that.
Of course, it's not ideal since we have to ensure we aren't passing a shell script/python script to kcov, but still generates what we need.
Well, even if it does work it doesn't sound very convenient: recompiling Python is probably not something most people would like to do! And also, you don't get the actual Python coverage then.
By just tracking dlopen and keeping track of when Python dlopens shared libraries, it might be possible to do this in a more convenient way.
So I'll reopen the issue to maybe do something about it in the future.