This tutorial describes how to collect code coverage for a service.
This tutorial covers the following:
Related tutorials:
Code coverage for a .Net Core application.
Code coverage for a .Net Core application child process.
Code coverage for a service.
Code coverage for a service child process.
Code coverage for an IIS ISAPI DLL.
Code coverage for ASP.Net with IIS.
Code coverage for ASP.Net with Web Development Server.
This tutorial applies to all native services and to mixed-mode services that start the service that uses the native Win32 services API.
If your service is written entirely in .Net or .Net Core, or your service is mixed-mode with the startup code written in .Net or .Net core you can skip the part of this tutorial relating to the NT Service API and go straight to the collecting code coverage from a service section.
Coverage Validator ships with an example service in the examples\service folder in the Coverage Validator installation directory.
or
or
The service has already been modified to use the NT Service API. In this tutorial, we’ll describe the modifications you would make to the service to make it work correctly with Coverage Validator.
The NT Service API is a simple API that allows you to load the Coverage Validator profiling DLL and start the process of collecting code coverage.
The API also includes some debugging functions to help provide debugging information via log files (the only way to get data out of a service without a connection to the Coverage Validator user interface).
The purpose of serviceCallback() is to regularly tell the service control manager that the service is alive. This prevents the service from being killed for being unresponsive if the instrumentation of your service takes too long (where too long is defined by the service control manager).
void serviceCallback(void *userParam) { // just tell the Service Control Manager that we are still busy // in this example userParam is not used static DWORD dwCheckPoint = 1; ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; ssStatus.dwServiceSpecificExitCode = 0; ssStatus.dwControlsAccepted = 0; ssStatus.dwCurrentState = dwCurrentState; ssStatus.dwWin32ExitCode = dwWin32ExitCode; ssStatus.dwWaitHint = dwWaitHint; ssStatus.dwCheckPoint = dwCheckPoint++; // Report the status of the service to the service control manager. return SetServiceStatus(sshStatusHandle, &ssStatus); }
svlCVStub_setLogFileName(SZLOGFILENAME); svlCVStub_deleteLogFile();
#ifdef IS6432 // x86 with x64 GUI errCode = svlCVStub_LoadCoverageValidator6432(); #else //#ifdef IS6432 // x86 with x86 GUI // x64 with x64 GUI errCode = svlCVStub_LoadCoverageValidator(); #endif //#ifdef IS6432 if (errCode != SVL_OK) { DWORD lastError; lastError = GetLastError(); svlCVStub_writeToLogFileW(_T("Coverage Validator load failed. \r\n")); svlCVStub_writeToLogFileLastError(lastError); svlCVStub_writeToLogFile(errCode); svlCVStub_dumpPathToLogFile(); } else { svlCVStub_writeToLogFileW(_T("Coverage Validator load success. \r\n")); }
errCode = svlCVStub_SetServiceCallback(serviceCallback, // the callback NULL); // some user data if (errCode != SVL_OK) { svlCVStub_writeToLogFileW(_T("Setting service callback failed. \r\n")); svlCVStub_writeToLogFile(errCode); }
errCode = svlCVStub_StartCoverageValidator(); if (errCode != SVL_OK) { DWORD lastError; lastError = GetLastError(); svlCVStub_writeToLogFileW(_T("Starting Coverage Validator failed. \r\n")); svlCVStub_writeToLogFileLastError(lastError); svlCVStub_writeToLogFile(errCode); } else { svlCVStub_writeToLogFileW(_T("Finished loading Coverage Validator\r\n")); }
Now that the NT Service API has been implemented in your service, we can start collecting code coverage from the service.
Select the service executable you are going to monitor. For this example the application is examples\service\Release_x64\serviceCV_x64.exe.
Choose the appropriate native/mixed-mode/.Net option to specify which types of code you want to collect code coverage for. Mixed-mode is the default, as this collects code coverage for all types of code.
The collect data check box will not be modifiable if you are using breakpoints to instrument the code coverage.
To finish collecting code coverage, you need to stop your service.
Coverage Validator will collect code coverage through the service shutdown procedure and then present you with the complete code coverage data.
There are a few things to check.
svlCVStub_setLogFileName(SZLOGFILENAME);
You have learned how to add the NT Service API to a native service, how to use Coverage Validator to monitor a service, and what to look at to diagnose errors if things don’t work first time.