Being Cellfish

Stuff I wished I've found in some blog (and sometimes did)

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

Change of Address
This blog has moved to blog.cellfish.se.

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

Rate This
  • Comments 18

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.
  • So now you know how to get coverage reports for native C++ using Visual Studio 2008 Team System (if not

  • Thanks,

    This was extremely helpful and your instructions were spot on.

    Cheers

    Karan

  • 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?

  • @ 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).

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

    Eric

  • Eric, you're right. I have updated the text.

  • 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

  • @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.

  • 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.

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

  • 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

  • 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.

  • 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?

  • @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.

  • 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.

Page 1 of 2 (18 items) 12