Where to put your code
When you use the functions to load and unload Coverage Validator from your service, it is important that you put the function calls in the correct place in your software.
The correct place to put them is in a 'balanced' location, such that you would expect no memory leaks to occur between the load and the unload function call, assuming the service was working correctly.
Typically, this means that Coverage Validator is:
•loaded as the first action in the service_main() function
•unloaded just before the service control manager is informed of the stopped status
The source code shown below shows an example service_main() function used in a service, demonstrating where to load and unload Coverage Validator.
The long comment covers problems with the way services are stopped and what may be displayed in a debugger if this happens.
The code is extracted from service\service.cpp, part of the full example of an NT service, client and a utility for controlling whether the service uses Coverage Validator.
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);
}
void WINAPI service_main(DWORD dwArgc,
LPTSTR *lpszArgv)
{
if (bLogging)
{
svlCVStub_setLogFileName(SZLOGFILENAME);
svlCVStub_deleteLogFile();
}
// register our service control handler:
sshStatusHandle = RegisterServiceCtrlHandler(TEXT(SZSERVICENAME), service_ctrl);
if (sshStatusHandle != 0)
{
DWORD dwErr = 0;
// **CV_EXAMPLE** start
if (bCoverageValidator)
{
// load Coverage Validator (but if monitoring a 32 bit service with Coverage Validator x64 use svlCVStub_LoadCoverageValidator6432())
if (bLogging)
{
svlCVStub_writeToLogFileW(_T("About to load Coverage Validator\r\n"));
}
SVL_SERVICE_ERROR errCode;
#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 (bLogging)
{
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"));
}
}
// setup a service callback so that the Service Control Manager knows the service
// is starting up even if instrumentation takes longer than 10 seconds (which it will
// for a non-trivial application)
if (bLogging)
svlCVStub_writeToLogFileW(_T("Setting service callback Coverage Validator\r\n"));
errCode = svlCVStub_SetServiceCallback(serviceCallback, // the callback
NULL); // some user data (we don't have any, so set NULL)
if (bLogging)
{
if (errCode != SVL_OK)
{
svlCVStub_writeToLogFileW(_T("Setting service callback failed. \r\n"));
svlCVStub_writeToLogFile(errCode);
}
svlCVStub_writeToLogFileW(_T("Starting Coverage Validator\r\n"));
}
errCode = svlCVStub_StartCoverageValidator();
if (bLogging)
{
if (errCode != SVL_OK)
{
DWORD lastError;
lastError = GetLastError();
svlCVStub_writeToLogFileW(_T("Starting Coverage Validator failed. \r\n"));
svlCVStub_writeToLogFileLastError(lastError);
svlCVStub_writeToLogFile(errCode);
}
svlCVStub_writeToLogFileW(_T("Finished loading Coverage Validator\r\n"));
}
}
else
{
if (bLogging)
svlCVStub_writeToLogFileW(_T("Not using Coverage Validator, DLL will not be loaded\r\n"));
}
// **CV_EXAMPLE** end
// SERVICE_STATUS members that don't change in example
ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
ssStatus.dwServiceSpecificExitCode = 0;
// report the status to the service control manager.
if (ReportStatusToSCMgr(SERVICE_START_PENDING, // service state
NO_ERROR, // exit code
3000)) // wait hint
{
// do work
dwErr = ServiceStart(dwArgc, lpszArgv);
// finished doing work
}
// **CV_EXAMPLE** start
if (bCoverageValidator)
{
// unload Coverage Validator here
// IMPORTANT.
// Because of the way services work, you can find that this thread which is trying to gracefully unload
// Coverage Validator is ripped from under you by the operating system. This prevents Coverage Validator from
// removing all its hooks successfully. If Coverage Validator does not remove all of its hooks successfully
// because this happens, then you may get a crash when the service stops.
//
// An alternative fix is to spawn another thread which then unloads Coverage Validator.
// See the code for ServiceStop() for comments relating to this.
//
// A callstack for such a crash is shown below. If you see this type of crash you need to put you code to
// unload Coverage Validator somewhere else. The stack trace may be different, but a fundamental point is the
// code calling through doexit(), exit() and ExitProcess()
//
//NTDLL! 77f64e70()
//SVLPERFORMANCEVALIDATORSTUB!
//MSVCRT! 78001436()
//MSVCRT! 7800578c()
//DBGHELP! 6d55da25()
//DBGHELP! 6d55de83()
//DBGHELP! 6d53705d()
//DBGHELP! 6d51cc69()
//DBGHELP! 6d51f6e8()
//DBGHELP! 6d524ebf()
//DBGHELP! 6d52a7b0()
//DBGHELP! 6d52b00a()
//DBGHELP! 6d526487()
//DBGHELP! 6d5264d7()
//DBGHELP! 6d5264f7()
//SVLPERFORMANCEVALIDATORSTUB!
//SVLPERFORMANCEVALIDATORSTUB!
//SVLPERFORMANCEVALIDATORSTUB!
//SVLPERFORMANCEVALIDATORSTUB!
//SVLPERFORMANCEVALIDATORSTUB!
//SVLPERFORMANCEVALIDATORSTUB!
//SVLPERFORMANCEVALIDATORSTUB!
//SVLPERFORMANCEVALIDATORSTUB!
//MSVCRT! 78001436()
//MSVCRT! 780057db()
//KERNEL32! 77f19fdb()
//SVLPERFORMANCEVALIDATORSTUB! ExitProcess hook
//doexit(int 0x00000000, int 0x00000000, int 0x00000000) line 392
//exit(int 0x00000000) line 279 + 13 bytes
//mainCRTStartup() line 345
//KERNEL32! 77f1b9ea()
svlCVStub_UnloadCoverageValidator();
}
// **CV_EXAMPLE** end
// try to report the stopped status to the service control manager.
(VOID)ReportStatusToSCMgr(SERVICE_STOPPED, dwErr, 0);
}
return;
}
|