void ProfilerCommunication::RequestInformation(BR buildRequest, PR processResults, DWORD dwTimeout, tstring message) { try { buildRequest(); DWORD dwSignal = m_eventProfilerRequestsInformation.SignalAndWait(m_eventInformationReadyForProfiler, dwTimeout); if (WAIT_OBJECT_0 != dwSignal) throw CommunicationException(dwSignal); m_eventInformationReadyForProfiler.Reset(); BOOL hasMore = FALSE; do { hasMore = processResults(); if (hasMore) { dwSignal = m_eventInformationReadByProfiler.SignalAndWait(m_eventInformationReadyForProfiler, COMM_WAIT_SHORT); if (WAIT_OBJECT_0 != dwSignal) throw CommunicationException(dwSignal); m_eventInformationReadyForProfiler.Reset(); } }while (hasMore); m_eventInformationReadByProfiler.Set(); } catch (CommunicationException ex) { RELTRACE(_T("ProfilerCommunication::RequestInformation(...) => Communication (Chat channel - %s) with host has failed (0x%x)"), message.c_str(), ex.getReason()); hostCommunicationActive = false; } }
/// <summary>Handle <c>ICorProfilerCallback::Shutdown</c></summary> HRESULT STDMETHODCALLTYPE CCodeCoverage::Shutdown( void) { if (m_chainedProfiler != NULL) m_chainedProfiler->Shutdown(); WCHAR szExeName[MAX_PATH]; GetModuleFileNameW(NULL, szExeName, MAX_PATH); RELTRACE(_T("::Shutdown - Nothing left to do but return S_OK(%s)"), szExeName); g_pProfiler = NULL; return S_OK; }
void ProfilerCommunication::SendVisitPoints() { if (!hostCommunicationActive) return; try { DWORD dwSignal = m_eventProfilerHasResults.SignalAndWait(m_eventResultsHaveBeenReceived, COMM_WAIT_SHORT); if (WAIT_OBJECT_0 != dwSignal) throw CommunicationException(dwSignal); m_eventResultsHaveBeenReceived.Reset(); } catch (CommunicationException ex) { RELTRACE(_T("ProfilerCommunication::SendVisitPoints() => Communication (Results channel) with host has failed (0x%x)"), ex.getReason()); hostCommunicationActive = false; } return; }
/// <summary>Handle <c>ICorProfilerCallback::Shutdown</c></summary> HRESULT STDMETHODCALLTYPE CCodeCoverage::Shutdown( void) { if (m_chainedProfiler != NULL) m_chainedProfiler->Shutdown(); if (!m_tracingEnabled){ m_host.SendRemainingThreadBuffers(); } WCHAR szExeName[MAX_PATH]; GetModuleFileNameW(NULL, szExeName, MAX_PATH); RELTRACE(_T("::Shutdown - Nothing left to do but return S_OK(%s)"), szExeName); g_pProfiler = NULL; return S_OK; }
/// <summary>Handle <c>ICorProfilerCallback::Initialize</c></summary> /// <remarks>Initialize the profiling environment and establish connection to the host</remarks> HRESULT STDMETHODCALLTYPE CCodeCoverage::Initialize( /* [in] */ IUnknown *pICorProfilerInfoUnk) { ATLTRACE(_T("::Initialize")); OLECHAR szGuid[40]={0}; int nCount = ::StringFromGUID2(CLSID_CodeCoverage, szGuid, 40); RELTRACE(L" ::Initialize (%s)", szGuid); //::OutputDebugStringW(szGuid); WCHAR szModuleName[MAX_PATH]; GetModuleFileNameW(_AtlModule.m_hModule, szModuleName, MAX_PATH); RELTRACE(L" ::Initialize (%s)", szModuleName); //::OutputDebugStringW(szModuleName); if (g_pProfiler!=NULL) RELTRACE(_T("Another instance of the profiler is running under this process...")); m_profilerInfo = pICorProfilerInfoUnk; if (m_profilerInfo != NULL) ATLTRACE(_T(" ::Initialize (m_profilerInfo OK)")); if (m_profilerInfo == NULL) return E_FAIL; m_profilerInfo2 = pICorProfilerInfoUnk; if (m_profilerInfo2 != NULL) ATLTRACE(_T(" ::Initialize (m_profilerInfo2 OK)")); if (m_profilerInfo2 == NULL) return E_FAIL; m_profilerInfo3 = pICorProfilerInfoUnk; ZeroMemory(&m_runtimeVersion, sizeof(m_runtimeVersion)); if (m_profilerInfo3 != NULL) { ATLTRACE(_T(" ::Initialize (m_profilerInfo3 OK)")); ZeroMemory(&m_runtimeVersion, sizeof(m_runtimeVersion)); m_profilerInfo3->GetRuntimeInformation(NULL, &m_runtimeType, &m_runtimeVersion.usMajorVersion, &m_runtimeVersion.usMinorVersion, &m_runtimeVersion.usBuildNumber, &m_runtimeVersion.usRevisionNumber, 0, NULL, NULL); ATLTRACE(_T(" ::Initialize (Runtime %d)"), m_runtimeType); } TCHAR key[1024]; ::GetEnvironmentVariable(_T("OpenCover_Profiler_Key"), key, 1024); RELTRACE(_T(" ::Initialize (key = %s)"), key); TCHAR ns[1024]; ::GetEnvironmentVariable(_T("OpenCover_Profiler_Namespace"), ns, 1024); ATLTRACE(_T(" ::Initialize (ns = %s)"), ns); if (!m_host.Initialise(key, ns)) { RELTRACE(_T(" ::Initialize => Failed to initialise the profiler communications -> GetLastError() => %d"), ::GetLastError()); return E_FAIL; } DWORD dwMask = 0; dwMask |= COR_PRF_MONITOR_MODULE_LOADS; // Controls the ModuleLoad, ModuleUnload, and ModuleAttachedToAssembly callbacks. dwMask |= COR_PRF_MONITOR_JIT_COMPILATION; // Controls the JITCompilation, JITFunctionPitched, and JITInlining callbacks. dwMask |= COR_PRF_DISABLE_INLINING; // Disables all inlining. dwMask |= COR_PRF_DISABLE_OPTIMIZATIONS; // Disables all code optimizations. dwMask |= COR_PRF_USE_PROFILE_IMAGES; // Don't use NGen images dwMask |= COR_PRF_MONITOR_ENTERLEAVE; // Controls the FunctionEnter, FunctionLeave, and FunctionTailcall callbacks. COM_FAIL_RETURNMSG(m_profilerInfo2->SetEventMask(dwMask), _T(" ::Initialize => SetEventMask(0x%x)")); if(m_profilerInfo3 != NULL) { COM_FAIL_RETURNMSG(m_profilerInfo3->SetFunctionIDMapper2(FunctionMapper2, this), _T(" ::Initialize => SetFunctionIDMapper2(0x%x)")); } else { COM_FAIL_RETURNMSG(m_profilerInfo2->SetFunctionIDMapper(FunctionMapper), _T(" ::Initialize => SetFunctionIDMapper(0x%x)")); } g_pProfiler = this; COM_FAIL_RETURNMSG(m_profilerInfo2->SetEnterLeaveFunctionHooks2( _FunctionEnter2, _FunctionLeave2, _FunctionTailcall2), _T(" ::Initialize => SetEnterLeaveFunctionHooks2(0x%x)")); RELTRACE(_T("::Initialize - Done!")); return S_OK; }
HRESULT STDMETHODCALLTYPE CCodeCoverage::ModuleLoadFinished( /* [in] */ ModuleID moduleId, /* [in] */ HRESULT hrStatus) { CComPtr<IMetaDataEmit> metaDataEmit; COM_FAIL_RETURNMSG(m_profilerInfo->GetModuleMetaData(moduleId, ofRead | ofWrite, IID_IMetaDataEmit, (IUnknown**)&metaDataEmit), _T(" ::ModuleLoadFinished => GetModuleMetaData(0x%x)")); if (metaDataEmit==NULL) return S_OK; CComPtr<IMetaDataImport> metaDataImport; COM_FAIL_RETURNMSG(m_profilerInfo->GetModuleMetaData(moduleId, ofRead | ofWrite, IID_IMetaDataImport, (IUnknown**)&metaDataImport), _T(" ::ModuleLoadFinished => GetModuleMetaData(0x%x)")); if (metaDataImport==NULL) return S_OK; mdTypeDef systemObject = mdTokenNil; if (S_OK == metaDataImport->FindTypeDefByName(L"System.Object", mdTokenNil, &systemObject)) { RELTRACE(_T("::ModuleLoadFinished => Adding methods to mscorlib...")); mdMethodDef systemObjectCtor; COM_FAIL_RETURNMSG(metaDataImport->FindMethod(systemObject, L".ctor", ctorCallSignature, sizeof(ctorCallSignature), &systemObjectCtor), _T(" ::ModuleLoadFinished => FindMethod(0x%x)")); ULONG ulCodeRVA = 0; COM_FAIL_RETURNMSG(metaDataImport->GetMethodProps(systemObjectCtor, NULL, NULL, 0, NULL, NULL, NULL, NULL, &ulCodeRVA, NULL), _T(" ::ModuleLoadFinished => GetMethodProps(0x%x)")); mdCustomAttribute customAttr; mdToken attributeCtor; mdTypeDef attributeTypeDef; mdTypeDef nestToken; COM_FAIL_RETURNMSG(metaDataImport->FindTypeDefByName(CUCKOO_NEST_TYPE_NAME, mdTokenNil, &nestToken), _T(" ::ModuleLoadFinished => FindTypeDefByName(0x%x)")); // create a method that we will mark up with the SecurityCriticalAttribute COM_FAIL_RETURNMSG(metaDataEmit->DefineMethod(nestToken, CUCKOO_CRITICAL_METHOD_NAME, mdPublic | mdStatic | mdHideBySig, visitedMethodCallSignature, sizeof(visitedMethodCallSignature), ulCodeRVA, miIL | miManaged | miPreserveSig | miNoInlining, &m_cuckooCriticalToken), _T(" ::ModuleLoadFinished => DefineMethod(0x%x)")); COM_FAIL_RETURNMSG(metaDataImport->FindTypeDefByName(L"System.Security.SecurityCriticalAttribute", NULL, &attributeTypeDef), _T(" :ModuleLoadFinished => FindTypeDefByName(0x%x)")); if (m_runtimeType == COR_PRF_DESKTOP_CLR) { // for desktop we use the .ctor that takes a SecurityCriticalScope argument as the // default (no arguments) constructor fails with "0x801311C2 - known custom attribute value is bad" // when we try to attach it in .NET2 - .NET4 version doesn't care which one we use mdTypeDef scopeToken; COM_FAIL_RETURNMSG(metaDataImport->FindTypeDefByName(L"System.Security.SecurityCriticalScope", mdTokenNil, &scopeToken), _T(" ::ModuleLoadFinished => FindTypeDefByName(0x%x)")); ULONG sigLength=4; COR_SIGNATURE ctorCallSignatureEnum[] = { IMAGE_CEE_CS_CALLCONV_DEFAULT | IMAGE_CEE_CS_CALLCONV_HASTHIS, 0x01, ELEMENT_TYPE_VOID, ELEMENT_TYPE_VALUETYPE, 0x00,0x00, 0x00, 0x00 // make room for our compressed token - should always be 2 but... }; sigLength += CorSigCompressToken(scopeToken, &ctorCallSignatureEnum[4]); COM_FAIL_RETURNMSG(metaDataImport->FindMember(attributeTypeDef, L".ctor", ctorCallSignatureEnum, sigLength, &attributeCtor), _T(" ::ModuleLoadFinished => FindMember(0x%x)")); unsigned char blob[] = {0x01, 0x00, 0x01, 0x00, 0x00, 0x00}; // prolog U2 plus an enum of I4 (little-endian) COM_FAIL_RETURNMSG(metaDataEmit->DefineCustomAttribute(m_cuckooCriticalToken, attributeCtor, blob, sizeof(blob), &customAttr), _T(" ::ModuleLoadFinished => DefineCustomAttribute(0x%x)")); } else { // silverlight only has one .ctor for this type COM_FAIL_RETURNMSG(metaDataImport->FindMember(attributeTypeDef, L".ctor", ctorCallSignature, sizeof(ctorCallSignature), &attributeCtor), _T(" ::ModuleLoadFinished => FindMember(0x%x)")); COM_FAIL_RETURNMSG(metaDataEmit->DefineCustomAttribute(m_cuckooCriticalToken, attributeCtor, NULL, 0, &customAttr), _T(" ::ModuleLoadFinished => DefineCustomAttribute(0x%x)")); } // create a method that we will mark up with the SecuritySafeCriticalAttribute COM_FAIL_RETURNMSG(metaDataEmit->DefineMethod(nestToken, CUCKOO_SAFE_METHOD_NAME, mdPublic | mdStatic | mdHideBySig, visitedMethodCallSignature, sizeof(visitedMethodCallSignature), ulCodeRVA, miIL | miManaged | miPreserveSig | miNoInlining, &m_cuckooSafeToken), _T(" ::ModuleLoadFinished => DefineMethod(0x%x)")); COM_FAIL_RETURNMSG(metaDataImport->FindTypeDefByName(L"System.Security.SecuritySafeCriticalAttribute", NULL, &attributeTypeDef), _T(" ::ModuleLoadFinished => FindTypeDefByName(0x%x)")); COM_FAIL_RETURNMSG(metaDataImport->FindMember(attributeTypeDef, L".ctor", ctorCallSignature, sizeof(ctorCallSignature), &attributeCtor), _T(" ::ModuleLoadFinished => FindMember(0x%x)")); COM_FAIL_RETURNMSG(metaDataEmit->DefineCustomAttribute(m_cuckooSafeToken, attributeCtor, NULL, 0, &customAttr), _T(" ::ModuleLoadFinished => DefineCustomAttribute(0x%x)")); RELTRACE(_T("::ModuleLoadFinished => Added methods to mscorlib")); } return S_OK; }
HRESULT CCodeCoverage::OpenCoverInitialise(IUnknown *pICorProfilerInfoUnk){ ATLTRACE(_T("::OpenCoverInitialise")); OLECHAR szGuid[40]={0}; int nCount = ::StringFromGUID2(CLSID_CodeCoverage, szGuid, 40); RELTRACE(L" ::Initialize(...) => CLSID == %s", szGuid); //::OutputDebugStringW(szGuid); WCHAR szExeName[MAX_PATH]; GetModuleFileNameW(NULL, szExeName, MAX_PATH); RELTRACE(L" ::Initialize(...) => EXE = %s", szExeName); WCHAR szModuleName[MAX_PATH]; GetModuleFileNameW(_AtlModule.m_hModule, szModuleName, MAX_PATH); RELTRACE(L" ::Initialize(...) => PROFILER = %s", szModuleName); //::OutputDebugStringW(szModuleName); if (g_pProfiler!=NULL) RELTRACE(_T("Another instance of the profiler is running under this process...")); m_profilerInfo = pICorProfilerInfoUnk; if (m_profilerInfo != NULL) ATLTRACE(_T(" ::Initialize (m_profilerInfo OK)")); if (m_profilerInfo == NULL) return E_FAIL; m_profilerInfo2 = pICorProfilerInfoUnk; if (m_profilerInfo2 != NULL) ATLTRACE(_T(" ::Initialize (m_profilerInfo2 OK)")); if (m_profilerInfo2 == NULL) return E_FAIL; m_profilerInfo3 = pICorProfilerInfoUnk; #ifndef _TOOLSETV71 m_profilerInfo4 = pICorProfilerInfoUnk; #endif ZeroMemory(&m_runtimeVersion, sizeof(m_runtimeVersion)); if (m_profilerInfo3 != NULL) { ATLTRACE(_T(" ::Initialize (m_profilerInfo3 OK)")); ZeroMemory(&m_runtimeVersion, sizeof(m_runtimeVersion)); m_profilerInfo3->GetRuntimeInformation(NULL, &m_runtimeType, &m_runtimeVersion.usMajorVersion, &m_runtimeVersion.usMinorVersion, &m_runtimeVersion.usBuildNumber, &m_runtimeVersion.usRevisionNumber, 0, NULL, NULL); ATLTRACE(_T(" ::Initialize (Runtime %d)"), m_runtimeType); } TCHAR key[1024] = {0}; ::GetEnvironmentVariable(_T("OpenCover_Profiler_Key"), key, 1024); RELTRACE(_T(" ::Initialize(...) => key = %s"), key); TCHAR ns[1024] = {0}; ::GetEnvironmentVariable(_T("OpenCover_Profiler_Namespace"), ns, 1024); ATLTRACE(_T(" ::Initialize(...) => ns = %s"), ns); TCHAR instrumentation[1024] = {0}; ::GetEnvironmentVariable(_T("OpenCover_Profiler_Instrumentation"), instrumentation, 1024); ATLTRACE(_T(" ::Initialize(...) => instrumentation = %s"), instrumentation); TCHAR threshold[1024] = {0}; ::GetEnvironmentVariable(_T("OpenCover_Profiler_Threshold"), threshold, 1024); m_threshold = _tcstoul(threshold, NULL, 10); ATLTRACE(_T(" ::Initialize(...) => threshold = %ul"), m_threshold); TCHAR tracebyTest[1024] = {0}; ::GetEnvironmentVariable(_T("OpenCover_Profiler_TraceByTest"), tracebyTest, 1024); m_tracingEnabled = _tcslen(tracebyTest) != 0; ATLTRACE(_T(" ::Initialize(...) => tracingEnabled = %s (%s)"), m_tracingEnabled ? _T("true") : _T("false"), tracebyTest); m_useOldStyle = (tstring(instrumentation) == _T("oldSchool")); if (!m_host.Initialise(key, ns)) { RELTRACE(_T(" ::Initialize => Failed to initialise the profiler communications -> GetLastError() => %d"), ::GetLastError()); return E_FAIL; } OpenCoverSupportInitialize(pICorProfilerInfoUnk); if (m_chainedProfiler == NULL){ DWORD dwMask = AppendProfilerEventMask(0); COM_FAIL_MSG_RETURN_ERROR(m_profilerInfo2->SetEventMask(dwMask), _T(" ::Initialize(...) => SetEventMask => 0x%X")); } if(m_profilerInfo3 != NULL) { COM_FAIL_MSG_RETURN_ERROR(m_profilerInfo3->SetFunctionIDMapper2(FunctionMapper2, this), _T(" ::Initialize(...) => SetFunctionIDMapper2 => 0x%X")); } else { COM_FAIL_MSG_RETURN_ERROR(m_profilerInfo2->SetFunctionIDMapper(FunctionMapper), _T(" ::Initialize(...) => SetFunctionIDMapper => 0x%X")); } g_pProfiler = this; #ifndef _TOOLSETV71 COM_FAIL_MSG_RETURN_ERROR(m_profilerInfo2->SetEnterLeaveFunctionHooks2( _FunctionEnter2, _FunctionLeave2, _FunctionTailcall2), _T(" ::Initialize(...) => SetEnterLeaveFunctionHooks2 => 0x%X")); #endif RELTRACE(_T("::Initialize - Done!")); return S_OK; }
bool ProfilerCommunication::Initialise(TCHAR *key, TCHAR *ns, TCHAR *processName) { _key = key; _processName = processName; std::wstring sharedKey = key; sharedKey.append(_T("-1")); _namespace = ns; _mutexCommunication.Initialise((_namespace + _T("\\OpenCover_Profiler_Communication_Mutex_") + _key).c_str()); if (!_mutexCommunication.IsValid()) return false; USES_CONVERSION; ATLTRACE(_T("ProfilerCommunication::Initialise(...) => Initialised mutexes => %s"), W2CT(sharedKey.c_str())); auto resource_name = (_namespace + _T("\\OpenCover_Profiler_Communication_SendData_Event_") + sharedKey); _eventProfilerRequestsInformation.Initialise(resource_name.c_str()); if (!_eventProfilerRequestsInformation.IsValid()) { RELTRACE(_T("ProfilerCommunication::Initialise(...) => Failed to initialise resource %s => ::GetLastError() = %d"), W2CT(resource_name.c_str()), ::GetLastError()); return false; } resource_name = (_namespace + _T("\\OpenCover_Profiler_Communication_ChunkData_Event_") + sharedKey); _eventInformationReadByProfiler.Initialise(resource_name.c_str()); if (!_eventInformationReadByProfiler.IsValid()) { RELTRACE(_T("ProfilerCommunication::Initialise(...) = >Failed to initialise resource %s => ::GetLastError() = %d"), W2CT(resource_name.c_str()), ::GetLastError()); return false; } resource_name = (_namespace + _T("\\OpenCover_Profiler_Communication_ReceiveData_Event_") + sharedKey); _eventInformationReadyForProfiler.Initialise(resource_name.c_str()); if (!_eventInformationReadyForProfiler.IsValid()) { RELTRACE(_T("ProfilerCommunication::Initialise(...) => Failed to initialise resource %s => ::GetLastError() = %d"), W2CT(resource_name.c_str()), ::GetLastError()); return false; } resource_name = (_namespace + _T("\\OpenCover_Profiler_Communication_MemoryMapFile_") + sharedKey); _memoryCommunication.OpenFileMapping(resource_name.c_str()); if (!_memoryCommunication.IsValid()) { RELTRACE(_T("ProfilerCommunication::Initialise(...) => Failed to initialise resource %s => ::GetLastError() = %d"), W2CT(resource_name.c_str()), ::GetLastError()); return false; } resource_name = (_namespace + _T("\\OpenCover_Profiler_Communication_Semaphore_") + sharedKey); _semapore_communication.Initialise(resource_name.c_str()); if (!_semapore_communication.IsValid()) { RELTRACE(_T("ProfilerCommunication::Initialise(...) => Failed to initialise resource %s => ::GetLastError() = %d"), W2CT(resource_name.c_str()), ::GetLastError()); return false; } _pMSG = static_cast<MSG_Union*>(_memoryCommunication.MapViewOfFile(0, 0, MAX_MSG_SIZE)); _hostCommunicationActive = true; ATLTRACE(_T("ProfilerCommunication::Initialise(...) => Initialised communication interface => %s"), W2CT(sharedKey.c_str())); if (!TrackProcess()){ RELTRACE(_T("ProfilerCommunication::Initialise(...) => ProfilerCommunication => process is not be tracked")); return false; } ULONG bufferId = 0; if (AllocateBuffer(MAX_MSG_SIZE, bufferId)) { std::wstring memoryKey; std::wstringstream stream ; stream << bufferId; stream >> memoryKey; _bufferId = bufferId; memoryKey = _key + memoryKey; ATLTRACE(_T("ProfilerCommunication::Initialise(...) => Re-initialising communication interface => %s"), W2CT(memoryKey.c_str())); resource_name = (_namespace + _T("\\OpenCover_Profiler_Communication_SendData_Event_") + memoryKey); _eventProfilerRequestsInformation.Initialise(resource_name.c_str()); if (!_eventProfilerRequestsInformation.IsValid()) { RELTRACE(_T("ProfilerCommunication::Initialise(...) => Failed to initialise resource %s => ::GetLastError() = %d"), W2CT(resource_name.c_str()), ::GetLastError()); _hostCommunicationActive = false; return false; } resource_name = (_namespace + _T("\\OpenCover_Profiler_Communication_ChunkData_Event_") + memoryKey); _eventInformationReadByProfiler.Initialise(resource_name.c_str()); if (!_eventInformationReadByProfiler.IsValid()) { RELTRACE(_T("ProfilerCommunication::Initialise(...) => Failed to initialise resource %s => ::GetLastError() = %d"), W2CT(resource_name.c_str()), ::GetLastError()); _hostCommunicationActive = false; return false; } resource_name = (_namespace + _T("\\OpenCover_Profiler_Communication_ReceiveData_Event_") + memoryKey); _eventInformationReadyForProfiler.Initialise(resource_name.c_str()); if (!_eventInformationReadyForProfiler.IsValid()) { RELTRACE(_T("ProfilerCommunication::Initialise(...) => Failed to initialise resource %s => ::GetLastError() = %d"), W2CT(resource_name.c_str()), ::GetLastError()); _hostCommunicationActive = false; return false; } resource_name = (_namespace + _T("\\OpenCover_Profiler_Communication_MemoryMapFile_") + memoryKey); _memoryCommunication.OpenFileMapping(resource_name.c_str()); if (!_memoryCommunication.IsValid()) { RELTRACE(_T("ProfilerCommunication::Initialise(...) => Failed to initialise resource %s => ::GetLastError() = %d"), W2CT(resource_name.c_str()), ::GetLastError()); _hostCommunicationActive = false; return false; } _pMSG = static_cast<MSG_Union*>(_memoryCommunication.MapViewOfFile(0, 0, MAX_MSG_SIZE)); resource_name = (_namespace + _T("\\OpenCover_Profiler_Communication_Semaphore_") + memoryKey); _semapore_communication.Initialise(resource_name.c_str()); if (!_semapore_communication.IsValid()) { RELTRACE(_T("ProfilerCommunication::Initialise(...) => Failed to initialise resource %s => ::GetLastError() = %d"), W2CT(resource_name.c_str()), ::GetLastError()); _hostCommunicationActive = false; return false; } ATLTRACE(_T("ProfilerCommunication::Initialise(...) => Re-initialised communication interface => %s"), W2CT(memoryKey.c_str())); resource_name = (_namespace + _T("\\OpenCover_Profiler_Results_SendResults_Event_") + memoryKey); _eventProfilerHasResults.Initialise(resource_name.c_str()); if (!_eventProfilerHasResults.IsValid()) { RELTRACE(_T("ProfilerCommunication::Initialise(...) => Failed to initialise resource %s => ::GetLastError() = %d"), W2CT(resource_name.c_str()), ::GetLastError()); _hostCommunicationActive = false; return false; } resource_name = (_namespace + _T("\\OpenCover_Profiler_Results_ReceiveResults_Event_") + memoryKey); _eventResultsHaveBeenReceived.Initialise(resource_name.c_str()); if (!_eventResultsHaveBeenReceived.IsValid()) { RELTRACE(_T("ProfilerCommunication::Initialise(...) => Failed to initialise resource %s => ::GetLastError() = %d"), W2CT(resource_name.c_str()), ::GetLastError()); _hostCommunicationActive = false; return false; } resource_name = (_namespace + _T("\\OpenCover_Profiler_Results_MemoryMapFile_") + memoryKey); _memoryResults.OpenFileMapping(resource_name.c_str()); if (!_memoryResults.IsValid()) { RELTRACE(_T("ProfilerCommunication::Initialise(...) => Failed to initialise resource %s => ::GetLastError() = %d"), W2CT(resource_name.c_str()), ::GetLastError()); _hostCommunicationActive = false; return false; } _pVisitPoints = static_cast<MSG_SendVisitPoints_Request*>(_memoryResults.MapViewOfFile(0, 0, MAX_MSG_SIZE)); _pVisitPoints->count = 0; resource_name = (_namespace + _T("\\OpenCover_Profiler_Results_Semaphore_") + memoryKey); _semapore_results.Initialise(resource_name.c_str()); if (!_semapore_results.IsValid()) { RELTRACE(_T("ProfilerCommunication::Initialise(...) => Failed to initialise resource %s => ::GetLastError() = %d"), W2CT(resource_name.c_str()), ::GetLastError()); _hostCommunicationActive = false; return false; } RELTRACE(_T("ProfilerCommunication::Initialise(...) => Initialised results interface => %s"), W2CT(memoryKey.c_str())); } else {
/// <summary>Handle <c>ICorProfilerCallback::Initialize</c></summary> /// <remarks>Initialize the profiling environment and establish connection to the host</remarks> HRESULT STDMETHODCALLTYPE CCodeCoverage::Initialize( /* [in] */ IUnknown *pICorProfilerInfoUnk) { ATLTRACE(_T("::Initialize")); OLECHAR szGuid[40]={0}; int nCount = ::StringFromGUID2(CLSID_CodeCoverage, szGuid, 40); RELTRACE(L" ::Initialize(...) => CLSID == %s", szGuid); //::OutputDebugStringW(szGuid); WCHAR szModuleName[MAX_PATH]; GetModuleFileNameW(_AtlModule.m_hModule, szModuleName, MAX_PATH); RELTRACE(L" ::Initialize(...) => PATH = %s", szModuleName); //::OutputDebugStringW(szModuleName); if (g_pProfiler!=NULL) RELTRACE(_T("Another instance of the profiler is running under this process...")); m_profilerInfo = pICorProfilerInfoUnk; if (m_profilerInfo != NULL) ATLTRACE(_T(" ::Initialize (m_profilerInfo OK)")); if (m_profilerInfo == NULL) return E_FAIL; m_profilerInfo2 = pICorProfilerInfoUnk; if (m_profilerInfo2 != NULL) ATLTRACE(_T(" ::Initialize (m_profilerInfo2 OK)")); if (m_profilerInfo2 == NULL) return E_FAIL; m_profilerInfo3 = pICorProfilerInfoUnk; m_profilerInfo4 = pICorProfilerInfoUnk; ZeroMemory(&m_runtimeVersion, sizeof(m_runtimeVersion)); if (m_profilerInfo3 != NULL) { ATLTRACE(_T(" ::Initialize (m_profilerInfo3 OK)")); ZeroMemory(&m_runtimeVersion, sizeof(m_runtimeVersion)); m_profilerInfo3->GetRuntimeInformation(NULL, &m_runtimeType, &m_runtimeVersion.usMajorVersion, &m_runtimeVersion.usMinorVersion, &m_runtimeVersion.usBuildNumber, &m_runtimeVersion.usRevisionNumber, 0, NULL, NULL); ATLTRACE(_T(" ::Initialize (Runtime %d)"), m_runtimeType); } TCHAR key[1024] = {0}; ::GetEnvironmentVariable(_T("OpenCover_Profiler_Key"), key, 1024); RELTRACE(_T(" ::Initialize(...) => key = %s"), key); TCHAR ns[1024] = {0}; ::GetEnvironmentVariable(_T("OpenCover_Profiler_Namespace"), ns, 1024); ATLTRACE(_T(" ::Initialize(...) => ns = %s"), ns); TCHAR instrumentation[1024] = {0}; ::GetEnvironmentVariable(_T("OpenCover_Profiler_Instrumentation"), instrumentation, 1024); ATLTRACE(_T(" ::Initialize(...) => instrumentation = %s"), instrumentation); m_useOldStyle = (tstring(instrumentation) == _T("oldSchool")); if (!m_host.Initialise(key, ns)) { RELTRACE(_T(" ::Initialize => Failed to initialise the profiler communications -> GetLastError() => %d"), ::GetLastError()); return E_FAIL; } DWORD dwMask = 0; dwMask |= COR_PRF_MONITOR_MODULE_LOADS; // Controls the ModuleLoad, ModuleUnload, and ModuleAttachedToAssembly callbacks. dwMask |= COR_PRF_MONITOR_JIT_COMPILATION; // Controls the JITCompilation, JITFunctionPitched, and JITInlining callbacks. dwMask |= COR_PRF_DISABLE_INLINING; // Disables all inlining. dwMask |= COR_PRF_DISABLE_OPTIMIZATIONS; // Disables all code optimizations. dwMask |= COR_PRF_USE_PROFILE_IMAGES; // Don't use NGen images dwMask |= COR_PRF_MONITOR_ENTERLEAVE; // Controls the FunctionEnter, FunctionLeave, and FunctionTailcall callbacks. if (m_useOldStyle) dwMask |= COR_PRF_DISABLE_TRANSPARENCY_CHECKS_UNDER_FULL_TRUST; // Disables security transparency checks that are normally done during just-in-time (JIT) compilation and class loading for full-trust assemblies. This can make some instrumentation easier to perform. if (m_profilerInfo4 != NULL) { ATLTRACE(_T(" ::Initialize (m_profilerInfo4 OK)")); //dwMask |= COR_PRF_ENABLE_REJIT; //dwMask |= COR_PRF_DISABLE_ALL_NGEN_IMAGES; } COM_FAIL_MSG_RETURN_ERROR(m_profilerInfo2->SetEventMask(dwMask), _T(" ::Initialize(...) => SetEventMask => 0x%X")); if(m_profilerInfo3 != NULL) { COM_FAIL_MSG_RETURN_ERROR(m_profilerInfo3->SetFunctionIDMapper2(FunctionMapper2, this), _T(" ::Initialize(...) => SetFunctionIDMapper2 => 0x%X")); } else { COM_FAIL_MSG_RETURN_ERROR(m_profilerInfo2->SetFunctionIDMapper(FunctionMapper), _T(" ::Initialize(...) => SetFunctionIDMapper => 0x%X")); } g_pProfiler = this; COM_FAIL_MSG_RETURN_ERROR(m_profilerInfo2->SetEnterLeaveFunctionHooks2( _FunctionEnter2, _FunctionLeave2, _FunctionTailcall2), _T(" ::Initialize(...) => SetEnterLeaveFunctionHooks2 => 0x%X")); RELTRACE(_T("::Initialize - Done!")); return S_OK; }
/// <summary>Handle <c>ICorProfilerCallback::Shutdown</c></summary> HRESULT STDMETHODCALLTYPE CCodeCoverage::Shutdown( void) { RELTRACE(_T("::Shutdown - Nothing left to do but return S_OK")); g_pProfiler = NULL; return S_OK; }