views:

432

answers:

4
+1  Q: 

code coverage in C

I'm looking to have code coverage in C. I cannot rely on tools like gcov as I am working on different platforms/compilers.

Basically I am looking for a strategy to incorporate code coverage into my(own implementation) unit-test framework.

A: 

You might consider building a set of test cases as part of the application itself, or as an alternate build target. Then the application could test itself, with the added benefit of having access to all the "internals," rather than trying to test all the branches using black-box techniques.

Adam Liss
+1  A: 

Do you have so many platform-specific code that you want to measure the unit test coverage on every platform ? I mean that unless you have a lot of platform-specific code, your unit testing coverage should be similar on all your targets so you might not need to measure it on each and every target you have. It could be interesting to move your platform-specific code to platform-specific modules so that the coverage level of the common code is not lowered by the specific code.

Are you looking at branch coverage or function coverage ? If the latter you can just use gcc to instrument your code so that each function outputs its name or its address to a file and write a script to aggregate the results from all the platforms.

Sorry for answering with questions only ... HTH

philippe
+1  A: 

It would be really helpful if you updated your question to describe the platforms and compilers you are using.

A strategy I have used is running the system on an emulator that can trace all the instructions. This instruction trace can then be used to work out the code coverage.

On UNIX you could also run a process using ptrace() to single step the application, and thereby capture the instructions executed.

If you just want function call tracing you could do some nasty hacking of the procedure linkage table to jump to some specialised logging code, but that is probably crazy.

Again, more details on the execution environment and the level of detail would be required to give a useful answer.

benno
+1  A: 

A Page on code-coverage tools says that something called GCT by Brian Marick implements code coverage by source-to-source translation, so you can transform your C code and then build it with your platform-specific compiler. I did not follow up details.

Norman Ramsey