From 219126ce7d0c621307be11640b4ce00aa7253b66 Mon Sep 17 00:00:00 2001 From: Max Charlamb <44248479+max-charlamb@users.noreply.github.com> Date: Tue, 8 Oct 2024 10:26:02 -0400 Subject: [PATCH] [cdac] Implement ISOSDacInterface::GetPEFileBase in cDAC (#108602) Implements ISOSDacInterface::GetPEFileBase in cDAC Module already contains the Base so no change is needed. Loader_1.cs already implements GetILBase to get the Base off of the module. Contributes to #108450 --- src/coreclr/debug/daccess/dacimpl.h | 1 + src/coreclr/debug/daccess/request.cpp | 34 +++++++++++++++++-- .../cdacreader/src/Legacy/SOSDacImpl.cs | 26 +++++++++++++- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/coreclr/debug/daccess/dacimpl.h b/src/coreclr/debug/daccess/dacimpl.h index 0fc5a780e6f32..2d3123a575870 100644 --- a/src/coreclr/debug/daccess/dacimpl.h +++ b/src/coreclr/debug/daccess/dacimpl.h @@ -1241,6 +1241,7 @@ class ClrDataAccess HRESULT GetObjectDataImpl(CLRDATA_ADDRESS addr, struct DacpObjectData *objectData); HRESULT GetObjectExceptionDataImpl(CLRDATA_ADDRESS objAddr, struct DacpExceptionObjectData *data); HRESULT GetObjectStringDataImpl(CLRDATA_ADDRESS obj, unsigned int count, _Inout_updates_z_(count) WCHAR *stringData, unsigned int *pNeeded); + HRESULT GetPEFileBaseImpl(CLRDATA_ADDRESS moduleAddr, CLRDATA_ADDRESS *base); HRESULT GetPEFileNameImpl(CLRDATA_ADDRESS moduleAddr, unsigned int count, _Inout_updates_z_(count) WCHAR *fileName, unsigned int *pNeeded); HRESULT GetUsefulGlobalsImpl(struct DacpUsefulGlobalsData *globalsData); HRESULT GetMethodDescDataImpl(CLRDATA_ADDRESS methodDesc, CLRDATA_ADDRESS ip, struct DacpMethodDescData *data, ULONG cRevertedRejitVersions, DacpReJitData * rgRevertedRejitData, ULONG * pcNeededRevertedRejitData); diff --git a/src/coreclr/debug/daccess/request.cpp b/src/coreclr/debug/daccess/request.cpp index 800db3a2a09e0..252d68a71cf6a 100644 --- a/src/coreclr/debug/daccess/request.cpp +++ b/src/coreclr/debug/daccess/request.cpp @@ -2610,6 +2610,37 @@ ClrDataAccess::GetPEFileBase(CLRDATA_ADDRESS moduleAddr, CLRDATA_ADDRESS *base) SOSDacEnter(); + if (m_cdacSos != NULL) + { + hr = m_cdacSos->GetPEFileBase(moduleAddr, base); + if (FAILED(hr)) + { + hr = GetPEFileBaseImpl(moduleAddr, base); + } +#ifdef _DEBUG + else + { + CLRDATA_ADDRESS baseLocal = 0; + HRESULT hrLocal = GetPEFileBaseImpl(moduleAddr, &baseLocal); + + DacAssertsEnabledHolder assertsEnabled; + _ASSERTE(hr == hrLocal); + _ASSERTE(*base == baseLocal); + } +#endif + } + else + { + hr = GetPEFileBaseImpl(moduleAddr, base); + } + + SOSDacLeave(); + return hr; +} + +HRESULT +ClrDataAccess::GetPEFileBaseImpl(CLRDATA_ADDRESS moduleAddr, CLRDATA_ADDRESS *base) +{ PTR_Module pModule = PTR_Module(TO_TADDR(moduleAddr)); // More fields later? @@ -2622,8 +2653,7 @@ ClrDataAccess::GetPEFileBase(CLRDATA_ADDRESS moduleAddr, CLRDATA_ADDRESS *base) *base = (CLRDATA_ADDRESS)NULL; } - SOSDacLeave(); - return hr; + return S_OK; } DWORD DACGetNumComponents(TADDR addr, ICorDebugDataTarget* target) diff --git a/src/native/managed/cdacreader/src/Legacy/SOSDacImpl.cs b/src/native/managed/cdacreader/src/Legacy/SOSDacImpl.cs index 8f4a13500429e..e56bbc04dd49c 100644 --- a/src/native/managed/cdacreader/src/Legacy/SOSDacImpl.cs +++ b/src/native/managed/cdacreader/src/Legacy/SOSDacImpl.cs @@ -512,7 +512,31 @@ public unsafe int GetObjectStringData(ulong obj, uint count, char* stringData, u } public unsafe int GetOOMData(ulong oomAddr, void* data) => HResults.E_NOTIMPL; public unsafe int GetOOMStaticData(void* data) => HResults.E_NOTIMPL; - public unsafe int GetPEFileBase(ulong addr, ulong* peBase) => HResults.E_NOTIMPL; + + public unsafe int GetPEFileBase(ulong addr, ulong* peBase) + { + try + { + Contracts.ILoader contract = _target.Contracts.Loader; + Contracts.ModuleHandle handle = contract.GetModuleHandle(addr); + Contracts.ModuleFlags flags = contract.GetFlags(handle); + + if (!flags.HasFlag(Contracts.ModuleFlags.ReflectionEmit)) + { + *peBase = contract.GetILBase(handle); + } + else + { + *peBase = 0; + } + } + catch (System.Exception ex) + { + return ex.HResult; + } + + return HResults.S_OK; + } public unsafe int GetPEFileName(ulong addr, uint count, char* fileName, uint* pNeeded) {