/// <summary>Handle <c>ICorProfilerCallback::JITCompilationStarted</c></summary> /// <remarks>The 'workhorse' </remarks> HRESULT STDMETHODCALLTYPE CCodeCoverage::JITCompilationStarted( /* [in] */ FunctionID functionId, /* [in] */ BOOL fIsSafeToBlock) { std::wstring modulePath; mdToken functionToken; ModuleID moduleId; AssemblyID assemblyId; if (GetTokenAndModule(functionId, functionToken, moduleId, modulePath, &assemblyId)) { // add the bodies for our cuckoo methods when required if (MSCORLIB_NAME == GetAssemblyName(assemblyId)) { if (m_cuckooCriticalToken == functionToken) { COM_FAIL_RETURNMSG(AddCriticalCuckooBody(moduleId), _T(" ::JITCompilationStarted => AddCriticalCuckooBody(0x%x)")); } if (m_cuckooSafeToken == functionToken) { COM_FAIL_RETURNMSG(AddSafeCuckooBody(moduleId), _T(" ::JITCompilationStarted => AddSafeCuckooBody(0x%x)")); } } if (!m_allowModules[modulePath]) return S_OK; ATLTRACE(_T("::JITCompilationStarted(%X, %d, (%X)%s)"), functionId, functionToken, moduleId, W2CT(modulePath.c_str())); std::vector<SequencePoint> seqPoints; std::vector<BranchPoint> brPoints; mdMethodDef injectedVisitedMethod = RegisterSafeCuckooMethod(moduleId); if (m_host.GetPoints(functionToken, (LPWSTR)modulePath.c_str(), (LPWSTR)m_allowModulesAssemblyMap[modulePath].c_str(), seqPoints, brPoints)) { if (seqPoints.size()==0) return S_OK; LPCBYTE pMethodHeader = NULL; ULONG iMethodSize = 0; COM_FAIL_RETURNMSG(m_profilerInfo2->GetILFunctionBody(moduleId, functionToken, &pMethodHeader, &iMethodSize), _T(" ::JITCompilationStarted => GetILFunctionBody(0x%x)")); IMAGE_COR_ILMETHOD* pMethod = (IMAGE_COR_ILMETHOD*)pMethodHeader; CoverageInstrumentation instumentedMethod(pMethod); instumentedMethod.IncrementStackSize(2); ATLTRACE(_T("::JITCompilationStarted => Instrumenting...")); //seqPoints.clear(); //brPoints.clear(); instumentedMethod.AddSequenceCoverage(injectedVisitedMethod, seqPoints); instumentedMethod.AddBranchCoverage(injectedVisitedMethod, brPoints); instumentedMethod.DumpIL(); CComPtr<IMethodMalloc> methodMalloc; COM_FAIL_RETURNMSG(m_profilerInfo2->GetILFunctionBodyAllocator(moduleId, &methodMalloc), _T(" ::JITCompilationStarted => GetILFunctionBodyAllocator(0x%x)")); IMAGE_COR_ILMETHOD* pNewMethod = (IMAGE_COR_ILMETHOD*)methodMalloc->Alloc(instumentedMethod.GetMethodSize()); instumentedMethod.WriteMethod(pNewMethod); COM_FAIL_RETURNMSG(m_profilerInfo2->SetILFunctionBody(moduleId, functionToken, (LPCBYTE) pNewMethod), _T(" ::JITCompilationStarted => SetILFunctionBody(0x%x)")); ULONG mapSize = instumentedMethod.GetILMapSize(); COR_IL_MAP * pMap = (COR_IL_MAP *)CoTaskMemAlloc(mapSize * sizeof(COR_IL_MAP)); instumentedMethod.PopulateILMap(mapSize, pMap); COM_FAIL_RETURNMSG(m_profilerInfo2->SetILInstrumentedCodeMap(functionId, TRUE, mapSize, pMap), _T(" ::JITCompilationStarted => SetILInstrumentedCodeMap(0x%x)")); // only do this for .NET4 and above as there are issues with earlier runtimes (Access Violations) if (m_runtimeVersion.usMajorVersion >= 4) CoTaskMemFree(pMap); } } return S_OK; }
/// <summary>Handle <c>ICorProfilerCallback::JITCompilationStarted</c></summary> /// <remarks>The 'workhorse' </remarks> HRESULT STDMETHODCALLTYPE CCodeCoverage::JITCompilationStarted( /* [in] */ FunctionID functionId, /* [in] */ BOOL fIsSafeToBlock) { std::wstring modulePath; mdToken functionToken; ModuleID moduleId; AssemblyID assemblyId; if (GetTokenAndModule(functionId, functionToken, moduleId, modulePath, &assemblyId)) { if (OpenCoverSupportRequired(assemblyId, functionId)) OpenCoverSupportCompilation(functionId, functionToken, moduleId, assemblyId, modulePath); CuckooSupportCompilation(assemblyId, functionToken, moduleId); if (m_allowModules[modulePath]) { ATLTRACE(_T("::JITCompilationStarted(%X, ...) => %d, %X => %s"), functionId, functionToken, moduleId, W2CT(modulePath.c_str())); std::vector<SequencePoint> seqPoints; std::vector<BranchPoint> brPoints; if (m_host.GetPoints(functionToken, (LPWSTR)modulePath.c_str(), (LPWSTR)m_allowModulesAssemblyMap[modulePath].c_str(), seqPoints, brPoints)) { if (seqPoints.size() != 0) { LPCBYTE pMethodHeader = NULL; ULONG iMethodSize = 0; COM_FAIL_MSG_RETURN_ERROR(m_profilerInfo2->GetILFunctionBody(moduleId, functionToken, &pMethodHeader, &iMethodSize), _T(" ::JITCompilationStarted(...) => GetILFunctionBody => 0x%X")); IMAGE_COR_ILMETHOD* pMethod = (IMAGE_COR_ILMETHOD*)pMethodHeader; Method instumentedMethod(pMethod); instumentedMethod.IncrementStackSize(2); ATLTRACE(_T("::JITCompilationStarted(...) => Instrumenting...")); //seqPoints.clear(); //brPoints.clear(); // Instrument method InstrumentMethod(moduleId, instumentedMethod, seqPoints, brPoints); //instumentedMethod.DumpIL(); CComPtr<IMethodMalloc> methodMalloc; COM_FAIL_MSG_RETURN_ERROR(m_profilerInfo2->GetILFunctionBodyAllocator(moduleId, &methodMalloc), _T(" ::JITCompilationStarted(...) => GetILFunctionBodyAllocator=> 0x%X")); IMAGE_COR_ILMETHOD* pNewMethod = (IMAGE_COR_ILMETHOD*)methodMalloc->Alloc(instumentedMethod.GetMethodSize()); instumentedMethod.WriteMethod(pNewMethod); COM_FAIL_MSG_RETURN_ERROR(m_profilerInfo2->SetILFunctionBody(moduleId, functionToken, (LPCBYTE)pNewMethod), _T(" ::JITCompilationStarted(...) => SetILFunctionBody => 0x%X")); ULONG mapSize = instumentedMethod.GetILMapSize(); COR_IL_MAP * pMap = (COR_IL_MAP *)CoTaskMemAlloc(mapSize * sizeof(COR_IL_MAP)); instumentedMethod.PopulateILMap(mapSize, pMap); COM_FAIL_MSG_RETURN_ERROR(m_profilerInfo2->SetILInstrumentedCodeMap(functionId, TRUE, mapSize, pMap), _T(" ::JITCompilationStarted(...) => SetILInstrumentedCodeMap => 0x%X")); // only do this for .NET4 and above as there are issues with earlier runtimes (Access Violations) if (m_runtimeVersion.usMajorVersion >= 4) CoTaskMemFree(pMap); // resize the threshold array if (m_threshold != 0) { if (seqPoints.size() > 0) Resize(seqPoints.back().UniqueId + 1); if (brPoints.size() > 0) Resize(brPoints.back().UniqueId + 1); } } } } } if (m_chainedProfiler != NULL) return m_chainedProfiler->JITCompilationStarted(functionId, fIsSafeToBlock); return S_OK; }