Where to put your code
When you use the functions to load and unload Performance 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 Performance 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 Performance 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 Performance 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);
}
//-NAME---------------------------------
// service_main
//.DESCRIPTION..........................
//
// Initializes the service, then calls the function to do the work.
// This function is typically where you will load and unload Performance Validator
//
//.PARAMETERS...........................
//
// dwArgc - number of command line arguments
// lpszArgv - array of command line arguments
//
//.RETURN.CODES.........................
//--------------------------------------
void WINAPI service_main(DWORD dwArgc,
LPTSTR *lpszArgv)
{
if (bLogging)
{
svlPVStub_setLogFileName(SZLOGFILENAME);
svlPVStub_deleteLogFile();
}
// register our service control handler:
sshStatusHandle = RegisterServiceCtrlHandler(TEXT(SZSERVICENAME), service_ctrl);
if (sshStatusHandle != 0)
{
DWORD dwErr = 0;
// **PV_EXAMPLE** start
if (bPerformanceValidator)
{
// load Performance Validator (but if monitoring a 32 bit service with C++ Performance Validator x64 use svlPVStub_LoadPerformanceValidator6432())
if (bLogging)
{
svlPVStub_writeToLogFileW(_T("About to load C++ Performance Validator\r\n"));
}
SVL_SERVICE_ERROR errCode;
#ifdef IS6432
// x86 with x64 GUI
errCode = svlPVStub_LoadPerformanceValidator6432();
#else //#ifdef IS6432
// x86 with x86 GUI
// x64 with x64 GUI
errCode = svlPVStub_LoadPerformanceValidator();
#endif //#ifdef IS6432
if (bLogging)
{
if (errCode != SVL_OK)
{
DWORD lastError;
lastError = GetLastError();
svlPVStub_writeToLogFileW(_T("C++ Performance Validator load failed. \r\n"));
svlPVStub_writeToLogFileLastError(lastError);
svlPVStub_writeToLogFile(errCode);
svlPVStub_dumpPathToLogFile();
}
else
{
svlPVStub_writeToLogFileW(_T("C++ Performance 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)
svlPVStub_writeToLogFileW(_T("Setting service callback C++ Performance Validator\r\n"));
errCode = svlPVStub_SetServiceCallback(serviceCallback, // the callback
NULL); // some user data (we don't have any, so set NULL)
if (bLogging)
{
if (errCode != SVL_OK)
{
svlPVStub_writeToLogFileW(_T("Setting service callback failed. \r\n"));
svlPVStub_writeToLogFile(errCode);
}
svlPVStub_writeToLogFileW(_T("Starting C++ Performance Validator\r\n"));
}
errCode = svlPVStub_StartPerformanceValidator();
if (bLogging)
{
if (errCode != SVL_OK)
{
DWORD lastError;
lastError = GetLastError();
svlPVStub_writeToLogFileW(_T("Starting C++ Performance Validator failed. \r\n"));
svlPVStub_writeToLogFileLastError(lastError);
svlPVStub_writeToLogFile(errCode);
}
svlPVStub_writeToLogFileW(_T("Finished loading C++ Performance Validator\r\n"));
}
}
else
{
if (bLogging)
svlPVStub_writeToLogFileW(_T("Not using C++ Performance Validator, DLL will not be loaded\r\n"));
}
// **PV_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
{
// deliberately allocate some memory so we can see that
// with Performance Validator
char *someLeakedMemory;
someLeakedMemory = (char *)malloc((SIZE_T)3456);
// do work
dwErr = ServiceStart(dwArgc, lpszArgv);
// finished doing work
} //lint !e429 !e550
// **PV_EXAMPLE** start
if (bPerformanceValidator)
{
// unload Performance Validator here
// IMPORTANT.
// Because of the way services work, you can find that this thread which is trying to gracefully unload
// PerformanceValidator is ripped from under you by the operating system. This prevents Performance Validator from
// removing all its hooks successfully. If Performance 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 Performance 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 Performance 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()
svlPVStub_UnloadPerformanceValidator();
}
// **PV_EXAMPLE** end
// try to report the stopped status to the service control manager.
(VOID)ReportStatusToSCMgr(SERVICE_STOPPED, dwErr, 0);
}
return;
}
|