Improve function eval related code.
authorMikhail Kurinnoi <m.kurinnoi@samsung.com>
Mon, 18 Jul 2022 12:46:43 +0000 (15:46 +0300)
committerAlexander Soldatov/Platform Lab /SRR/Staff Engineer/Samsung Electronics <soldatov.a@samsung.com>
Tue, 19 Jul 2022 11:29:51 +0000 (14:29 +0300)
src/debugger/evalhelpers.cpp
src/debugger/evalhelpers.h
src/debugger/stepper_async.cpp
src/metadata/modules.cpp

index ee271f82b3d68714a99c804763e27bdd897542b1..7587d3c9cd2154a8b336d3bfba3799360fd72604 100644 (file)
@@ -134,6 +134,15 @@ static HRESULT FindFunction(ICorDebugModule *pModule,
     return pModule->GetFunctionFromToken(methodDef, ppFunction);
 }
 
+HRESULT EvalHelpers::FindMethodInModule(const std::string &moduleName, const WCHAR className[], const WCHAR methodName[], ICorDebugFunction **ppFunction)
+{
+    HRESULT Status;
+    ToRelease<ICorDebugModule> pModule;
+    IfFailRet(m_sharedModules->GetModuleWithName(moduleName, &pModule));
+    IfFailRet(FindFunction(pModule, className, methodName, ppFunction));
+    return S_OK;
+}
+
 static bool TypeHaveStaticMembers(ICorDebugType *pType)
 {
     HRESULT Status;
@@ -327,12 +336,10 @@ HRESULT EvalHelpers::CreatTypeObjectStaticConstructor(
 
         if (!m_pSuppressFinalize)
         {
-            ToRelease<ICorDebugModule> pModule;
-            IfFailRet(m_sharedModules->GetModuleWithName("System.Private.CoreLib.dll", &pModule));
-
+            static const std::string assemblyName = "System.Private.CoreLib.dll";
             static const WCHAR gcName[] = W("System.GC");
             static const WCHAR suppressFinalizeMethodName[] = W("SuppressFinalize");
-            IfFailRet(FindFunction(pModule, gcName, suppressFinalizeMethodName, &m_pSuppressFinalize));
+            IfFailRet(FindMethodInModule(assemblyName, gcName, suppressFinalizeMethodName, &m_pSuppressFinalize));
         }
 
         if (!m_pSuppressFinalize)
index afaee254189a2bb93abb900b595244f087006e1d..fd3071ee9ce5c4dca782fa4a39dffb95a979599a 100644 (file)
@@ -57,6 +57,8 @@ public:
 
     HRESULT CreateString(ICorDebugThread *pThread, const std::string &value, ICorDebugValue **ppNewString);
 
+    HRESULT FindMethodInModule(const std::string &moduleName, const WCHAR className[], const WCHAR methodName[], ICorDebugFunction **ppFunction);
+
     void Cleanup();
 
 private:
index 079c0e7a4480d4e12810213a7cd4d41a5e85ef48..6d259dc41168423e8cf5b0f2e575c0fbbc6bc648 100644 (file)
@@ -152,7 +152,7 @@ static HRESULT GetAsyncIdReference(ICorDebugThread *pThread, ICorDebugFrame *pFr
     // Call 'ObjectIdForDebugger' property getter.\r
     ToRelease<ICorDebugFunction> pFunc;\r
     IfFailRet(pModule->GetFunctionFromToken(mdObjectIdForDebuggerGetter, &pFunc));\r
-    IfFailRet(pEvalHelpers->EvalFunction(pThread, pFunc, pType.GetRef(), 1, pValue.GetRef(), 1, ppValueAsyncIdRef, defaultEvalFlags));\r
+    IfFailRet(pEvalHelpers->EvalFunction(pThread, pFunc, nullptr, 0, pValue.GetRef(), 1, ppValueAsyncIdRef, defaultEvalFlags));\r
 \r
     return S_OK;\r
 }\r
@@ -226,19 +226,12 @@ static HRESULT SetNotificationForWaitCompletion(ICorDebugThread *pThread, ICorDe
     rgbValue[0] = 1; // TRUE\r
     IfFailRet(pGenericValue->SetValue((LPVOID) &(rgbValue[0])));\r
 \r
-\r
     // Call this.<>t__builder.SetNotificationForWaitCompletion(TRUE).\r
-    ToRelease<ICorDebugValue2> pNewBooleanValue2;\r
-    IfFailRet(pNewBoolean->QueryInterface(IID_ICorDebugValue2, (LPVOID *) &pNewBooleanValue2));\r
-    ToRelease<ICorDebugType> pNewBooleanType;\r
-    IfFailRet(pNewBooleanValue2->GetExactType(&pNewBooleanType));\r
-\r
     ToRelease<ICorDebugFunction> pFunc;\r
     IfFailRet(pModule->GetFunctionFromToken(setNotifDef, &pFunc));\r
 \r
-    ICorDebugType *ppArgsType[] = {pType, pNewBooleanType};\r
     ICorDebugValue *ppArgsValue[] = {pBuilderValue, pNewBoolean};\r
-    IfFailRet(pEvalHelpers->EvalFunction(pThread, pFunc, ppArgsType, 2, ppArgsValue, 2, nullptr, defaultEvalFlags));\r
+    IfFailRet(pEvalHelpers->EvalFunction(pThread, pFunc, nullptr, 0, ppArgsValue, 2, nullptr, defaultEvalFlags));\r
 \r
     return S_OK;\r
 }\r
@@ -363,50 +356,18 @@ HRESULT AsyncStepper::DisableAllSteppers()
 HRESULT AsyncStepper::SetBreakpointIntoNotifyDebuggerOfWaitCompletion()\r
 {\r
     HRESULT Status = S_OK;\r
+    static const std::string assemblyName = "System.Private.CoreLib.dll";\r
+    static const WCHAR className[] = W("System.Threading.Tasks.Task");\r
+    static const WCHAR methodName[] = W("NotifyDebuggerOfWaitCompletion");\r
+    ToRelease<ICorDebugFunction> pFunc;\r
+    IfFailRet(m_sharedEvalHelpers->FindMethodInModule(assemblyName, className, methodName, &pFunc));\r
 \r
     ToRelease<ICorDebugModule> pModule;\r
-    IfFailRet(m_sharedModules->GetModuleWithName("System.Private.CoreLib.dll", &pModule));\r
-\r
-    ToRelease<IUnknown> pMDUnknown;\r
-    IfFailRet(pModule->GetMetaDataInterface(IID_IMetaDataImport, &pMDUnknown));\r
-    ToRelease<IMetaDataImport> pMD;\r
-    IfFailRet(pMDUnknown->QueryInterface(IID_IMetaDataImport, (LPVOID*) &pMD));\r
-\r
-    mdTypeDef typeDef = mdTypeDefNil;\r
-    static const WCHAR strTypeDef[] = W("System.Threading.Tasks.Task");\r
-    IfFailRet(pMD->FindTypeDefByName(strTypeDef, mdTypeDefNil, &typeDef));\r
-\r
-    ULONG numMethods = 0;\r
-    HCORENUM hEnum = NULL;\r
-    mdMethodDef methodDef;\r
-    mdMethodDef notifyDef = mdMethodDefNil;\r
-    while(SUCCEEDED(pMD->EnumMethods(&hEnum, typeDef, &methodDef, 1, &numMethods)) && numMethods != 0)\r
-    {\r
-        mdTypeDef memTypeDef;\r
-        ULONG nameLen;\r
-        WCHAR szFunctionName[mdNameLen] = {0};\r
-        if (FAILED(pMD->GetMethodProps(methodDef, &memTypeDef,\r
-                                       szFunctionName, _countof(szFunctionName), &nameLen,\r
-                                       nullptr, nullptr, nullptr, nullptr, nullptr)))\r
-        {\r
-            continue;\r
-        }\r
-\r
-        if (!str_equal(szFunctionName, W("NotifyDebuggerOfWaitCompletion")))\r
-            continue;\r
-\r
-        notifyDef = methodDef;\r
-        break;\r
-    }\r
-    pMD->CloseEnum(hEnum);\r
-\r
-    if (notifyDef == mdMethodDefNil)\r
-        return E_FAIL;\r
-\r
+    IfFailRet(pFunc->GetModule(&pModule));\r
     CORDB_ADDRESS modAddress;\r
     IfFailRet(pModule->GetBaseAddress(&modAddress));\r
-    ToRelease<ICorDebugFunction> pFunc;\r
-    IfFailRet(pModule->GetFunctionFromToken(notifyDef, &pFunc));\r
+    mdMethodDef methodDef;\r
+    IfFailRet(pFunc->GetToken(&methodDef));\r
 \r
     ToRelease<ICorDebugCode> pCode;\r
     IfFailRet(pFunc->GetILCode(&pCode));\r
@@ -419,7 +380,7 @@ HRESULT AsyncStepper::SetBreakpointIntoNotifyDebuggerOfWaitCompletion()
     m_asyncStepNotifyDebuggerOfWaitCompletion.reset(new asyncBreakpoint_t());\r
     m_asyncStepNotifyDebuggerOfWaitCompletion->iCorFuncBreakpoint = iCorFuncBreakpoint.Detach();\r
     m_asyncStepNotifyDebuggerOfWaitCompletion->modAddress = modAddress;\r
-    m_asyncStepNotifyDebuggerOfWaitCompletion->methodToken = notifyDef;\r
+    m_asyncStepNotifyDebuggerOfWaitCompletion->methodToken = methodDef;\r
 \r
     return S_OK;\r
 }\r
index fe2775104e3099b43e4015e057e5c6ca84f8733c..4c7c021293225862a9db56df6ca6055fb8c5bdaf 100644 (file)
@@ -1073,9 +1073,9 @@ static HRESULT GetPdbMethodsRanges(IMetaDataImport *pMDImport, PVOID pSymbolRead
     std::vector<int32_t> normalTokens;
 
     ULONG numTypedefs = 0;
-    HCORENUM fEnum = NULL;
+    HCORENUM hEnum = NULL;
     mdTypeDef typeDef;
-    while(SUCCEEDED(pMDImport->EnumTypeDefs(&fEnum, &typeDef, 1, &numTypedefs)) && numTypedefs != 0)
+    while(SUCCEEDED(pMDImport->EnumTypeDefs(&hEnum, &typeDef, 1, &numTypedefs)) && numTypedefs != 0)
     {
         ULONG numMethods = 0;
         HCORENUM fEnum = NULL;
@@ -1100,7 +1100,7 @@ static HRESULT GetPdbMethodsRanges(IMetaDataImport *pMDImport, PVOID pSymbolRead
         }
         pMDImport->CloseEnum(fEnum);
     }
-    pMDImport->CloseEnum(fEnum);
+    pMDImport->CloseEnum(hEnum);
 
     if (sizeof(std::size_t) > sizeof(std::uint32_t) &&
         (constrTokens.size() > std::numeric_limits<uint32_t>::max() || normalTokens.size() > std::numeric_limits<uint32_t>::max()))