NKJo28 NKJo28 - 1 year ago 181
C++ Question

How to instrument and get code coverage of a Windows service with Visual Studio?

I am trying to get the code coverage for a Windows service written in C++ (let's call it hello.exe) with Visual Studio 2012 Premium tools. So far I have attempted to use the method outlined in the answer to this post: How to use MS code coverage tool in command line?

For your convenience, the steps given were to run:

  1. vsinstr /coverage hello.exe

  2. start vsperfmon /coverage /output:mytestrun.coverage

  3. hello.exe

  4. vsperfcmd /shutdown

I am able to instrument, test and get the .coverage file for hello.exe when I start it as an executable in the command prompt no problem. But if I instead try to start it as a service in step 3 (either through services.msc manually or win32serviceutil.StartService in Python), nothing registers and the coverage file is empty. Is there something i'm missing here?

Extra info: In an effort to solve this I came across what seems to be another code coverage utility called CodeCoverage.exe (Microsoft Visual Studio 11.0\Team Tools\Dynamic Code Coverage Tools). Is that what I'm supposed to be using for this task? Also, I found something seemingly related here https://msdn.microsoft.com/en-us/library/dd255408.aspx, but (forgive me for my ignorance) am not sure whether a .NET service is the same thing as a Windows service.

Answer Source

Answering my own question here, after a lot of trying and pouring through documents I found this:


Basically, a windows Service is different from a process so we have to enable the cross session flag and specify the user when monitoring.

start vsperfmon /coverage /output:mytestrun.coverage /cs /user:”Everyone”

So just instrument the exe as usual, start monitoring with above command, start the service, do testing, stop the service, and shutdown the monitoring.

Hope it helps someone in the future.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download