Native C++ Code Coverage reports using Visual Studio 2008 Team System


The code coverage tool in Visual Studio 2008 Team System is quite easy to use from within the IDE unless you want code coverage for your native C++ code. In order to generate a code coverage report for native C++ you have to use the command line tools. This is how you do it:



  1. First of all your project must be compiled using the /PROFILE link option. If you bring up your project properties it can be found here:
    Configuration Properties -> Linker -> Advanced -> Profile

  2. The profiler tools can then be found in the following directory:
    C:\Program Files\Microsoft Visual Studio 9.0\Team Tools\Performance Tools

  3. You need to add some instrumentation code to your EXE or DLL file and that is done with this command:
    vsinstr.exe <YOUR_EXE_OR_DLL> /COVERAGE
    This will copy the original file to an “.orig”-file and create a new file with the original name that contains instrumentation code needed to gather coverage data.

  4. Now start the listener with this command:
    VSPerfMon.exe /COVERAGE /OUTPUT:<REPORT_FILE_NAME>

  5. Now run your EXE or some test suite that uses the file you want to calculate coverage for.

  6. The listener started in step four (4) will not stop by it self once your test suite is finished so you have to stop in manually using this command (from a second command prompt):
    VSPerfCmd.exe /SHUTDOWN

  7. When the listener has stopped you just drag-n-drop the created “.coverage”-file into Visual Studio and you can view the results.

Comments (17)

  1. So now you know how to get coverage reports for native C++ using Visual Studio 2008 Team System (if not

  2. Karan says:

    Thanks,

    This was extremely helpful and your instructions were spot on.

    Cheers

    Karan

  3. Kirk says:

    Nice work.  This really helped.

    One thing that I have found:  you can seemingly get overall numbers in Release builds, but you will get more itemized results (per class/method) in Debug mode.  Is this correct?

  4. cellfish says:

    @ Kirk:

    I guess your observation is correct, typicaly since releasebuilds are optimized while Debug builds are not (since you in Debug builds want to preserve the code structure to make step by step debugging meaningful).

  5. Eric says:

    I think the tool for instrumenting is vsinstr.exe rather than vsinst.exe.  That’s what I’ve got on my machine, anyway.

    Eric

  6. cellfish says:

    Eric, you’re right. I have updated the text.

  7. Andreas Bittel says:

    I found out  to get the code coverage with visual studio for c++, I have to use the two files "vinstr" and "vsperfmon". I cannot find these files in my local installed version of visual studio ("performance tool" is not existing).

    Where can I get these two files?

    andreas.bittel@cdn-automotive.com

    tia

    Regards

    Andreas Bittel

  8. cellfish says:

    @Andreas Bittel: Assuming VS2008 you need either the team suite or the test version (I think the code coverage is part of the test version as weel as the team suite). You cannot use any version of VC++.

    I guess that even if you have the team suite, there is a possibility to not install the code coverage tool too.

  9. Sherwood says:

    Thanks for the explanation.

    I finally got it setup in my project. My project has multiple unit tests, with each test file compiled into an executable.  After I run those files, I got a single .coverage file. When I loaded into Visual Studio, it displays the coverage under each executable. I could not find a way to display coverage in a merged manner. For example, a.cpp have a coverage of 20% under test1.exe, 30% under test2.exe. I am interest to see the overall coverage of a.cpp.

  10. cellfish says:

    @Sherwood: Sorry no experience there. I’m using Bullseye for coverage wich has a better way of showing a merged result.

  11. Pavan says:

    Hi when i am running the 5th step i am getting error saying"This application has failed to start because VSCover90.dll was not found.Re-intalling the application may fix this problem."

    How to fix the above error.

    Regards

    Pavan

  12. I'm totally impressed by the code coverage for C/C++ using your method. It's not very convenient to use the command line (and I'd say someone should write an Add-In for this functionality) but the later on nice visualization in the VS editor is absolutely worth it.

  13. Anand says:

    I tried to get code coverage of my C++ exe that use my few dlls.Followed above steps. Expected the coverage report to show the blocks covered in both modules(exe and dlls) but it shows only the blocks covered in exe. Is there anything additional should I do to get the dll module coverage in that report?

  14. cellfish says:

    @Anand: Don't know. This is a 5 year old post. I don't even know which version of VS you are using. Did you do step #3 for all your DLLs? Otherwise the VS forums or stack overflow might be a good place to search.

  15. Anand says:

    Thanks for quick repily!

    I'm using VS2010 and did step #3 for all my dlls. I did quick search in stack overflow but no luck.

    Any tips would be great help.

  16. Anand says:

    I am using VS 2010. Code covergage report shows blocks covered and their percentage. Is there anyway I can see lines of code covered and their percentage in the report.I opened the report in VS 2010.

  17. cellfish says:

    @Anand; Not sure if lines are available for C++ coverage. Doesn't really matter since you can have lines with a lot of statements or spreat a statement over several lines. Blocks is really the only kind of useful number anyway for you.

    I'm sorry StackOverflow did not show anything because I really don't have an idea what could be wrong of all DLLs are instrumented but still not showing coverage. Maybe you can try instrumenting one DLL/EXE at a time and get coverage for each of them in separate runs. If that doe snot give you DLL coverage numbers I suspect the EXE is using DLLs from a different location than you think.