From b4463549525f39ba7f7f6c7f8d06d4717777aaf6 Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Mon, 20 Nov 2023 10:22:14 -0800 Subject: [PATCH 1/3] Store entry assembly path for easy access for diagnostics (dumps) --- src/coreclr/debug/daccess/enummem.cpp | 28 +++++++++++++++++++++++++++ src/coreclr/inc/daccess.h | 4 ++-- src/coreclr/inc/dacvars.h | 2 ++ src/coreclr/vm/corhost.cpp | 9 +++++++++ src/coreclr/vm/vars.cpp | 2 ++ src/coreclr/vm/vars.hpp | 3 +++ 6 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/coreclr/debug/daccess/enummem.cpp b/src/coreclr/debug/daccess/enummem.cpp index 44d98beec2de38..7674fe921c7a96 100644 --- a/src/coreclr/debug/daccess/enummem.cpp +++ b/src/coreclr/debug/daccess/enummem.cpp @@ -224,6 +224,34 @@ HRESULT ClrDataAccess::EnumMemCLRStatic(IN CLRDataEnumMemoryFlags flags) ReportMem(g_gcDacGlobals.GetAddr(), sizeof(GcDacVars)); + PTR_WSTR entryAssemblyPath = (PTR_WSTR)g_EntryAssemblyPath; + entryAssemblyPath.EnumMem(); + + // Triage dumps must not include full paths (PII data). Replace entry assembly path with file name only. + if (flags == CLRDATA_ENUM_MEM_TRIAGE) + { + WCHAR* path = entryAssemblyPath; + if (path != NULL) + { + size_t pathLen = u16_strlen(path) + 1; + + // Get the file name based on the last directory separator + const WCHAR* name = u16_strrchr(path, DIRECTORY_SEPARATOR_CHAR_W); + if (name != NULL) + { + name += 1; + size_t len = u16_strlen(name) + 1; + wcscpy_s(path, len, name); + + // Null out the rest of the buffer + for (size_t i = len; i < pathLen; ++i) + path[i] = '\0'; + + DacUpdateMemoryRegion(entryAssemblyPath.GetAddr(), pathLen, (BYTE*)path); + } + } + } + // We need all of the dac variables referenced by the GC DAC global struct. // This struct contains pointers to pointers, so we first dereference the pointers // to obtain the location of the variable that's reported. diff --git a/src/coreclr/inc/daccess.h b/src/coreclr/inc/daccess.h index 3ca0ca40ee42e5..c1251cba51bd28 100644 --- a/src/coreclr/inc/daccess.h +++ b/src/coreclr/inc/daccess.h @@ -1493,10 +1493,10 @@ class __Str16Ptr : public __DPtr } void EnumMem(void) const { - char* str = DacInstantiateStringW(m_addr, maxChars, false); + WCHAR* str = DacInstantiateStringW(m_addr, maxChars, false); if (str) { - DacEnumMemoryRegion(m_addr, strlen(str) + 1); + DacEnumMemoryRegion(m_addr, u16_strlen(str) + 1); } } }; diff --git a/src/coreclr/inc/dacvars.h b/src/coreclr/inc/dacvars.h index d245c9877335ab..5b77893be7f3bc 100644 --- a/src/coreclr/inc/dacvars.h +++ b/src/coreclr/inc/dacvars.h @@ -236,5 +236,7 @@ DEFINE_DACVAR(SIZE_T, dac__g_clrNotificationArguments, ::g_clrNotificationArgume DEFINE_DACVAR(bool, dac__g_metadataUpdatesApplied, ::g_metadataUpdatesApplied) #endif +DEFINE_DACVAR(PTR_WSTR, dac__g_EntryAssemblyPath, ::g_EntryAssemblyPath) + #undef DEFINE_DACVAR #undef DEFINE_DACVAR_NO_DUMP diff --git a/src/coreclr/vm/corhost.cpp b/src/coreclr/vm/corhost.cpp index bb32e93cd0961a..b85331e00ce8d2 100644 --- a/src/coreclr/vm/corhost.cpp +++ b/src/coreclr/vm/corhost.cpp @@ -310,6 +310,15 @@ HRESULT CorHost2::ExecuteAssembly(DWORD dwAppDomainId, _ASSERTE (!pThread->PreemptiveGCDisabled()); + if (g_EntryAssemblyPath == NULL) + { + // Store the entry assembly path for diagnostic purposes (for example, dumps) + size_t len = u16_strlen(pwzAssemblyPath) + 1; + NewArrayHolder path { new WCHAR[len] }; + wcscpy_s(path, len, pwzAssemblyPath); + g_EntryAssemblyPath = path.Extract(); + } + Assembly *pAssembly = AssemblySpec::LoadAssembly(pwzAssemblyPath); #if defined(FEATURE_MULTICOREJIT) diff --git a/src/coreclr/vm/vars.cpp b/src/coreclr/vm/vars.cpp index 99d95f6a977bf9..31e17cacb52e9a 100644 --- a/src/coreclr/vm/vars.cpp +++ b/src/coreclr/vm/vars.cpp @@ -112,6 +112,8 @@ GPTR_IMPL(MethodTable, g_pEHClass); GVAL_IMPL(bool, g_isNewExceptionHandlingEnabled); #endif +GVAL_IMPL_INIT(PTR_WSTR, g_EntryAssemblyPath, NULL); + #ifndef DACCESS_COMPILE // @TODO - PROMOTE. diff --git a/src/coreclr/vm/vars.hpp b/src/coreclr/vm/vars.hpp index 37d9da98eb1f3f..8ecbd1d3f591b1 100644 --- a/src/coreclr/vm/vars.hpp +++ b/src/coreclr/vm/vars.hpp @@ -394,6 +394,9 @@ GPTR_DECL(MethodTable, g_pEHClass); GVAL_DECL(bool, g_isNewExceptionHandlingEnabled); #endif +// Full path to the managed entry assembly - stored for ease of identifying the entry asssembly for diagnostics +GVAL_DECL(PTR_WSTR, g_EntryAssemblyPath); + // Global System Information extern SYSTEM_INFO g_SystemInfo; From 830f78a3a0e934ed230bb4fc65ec50dad3abff35 Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Mon, 20 Nov 2023 11:53:24 -0800 Subject: [PATCH 2/3] Fix unix build --- src/coreclr/inc/daccess.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/coreclr/inc/daccess.h b/src/coreclr/inc/daccess.h index c1251cba51bd28..54ce86551caee9 100644 --- a/src/coreclr/inc/daccess.h +++ b/src/coreclr/inc/daccess.h @@ -574,6 +574,8 @@ #include "crosscomp.h" #endif +#include + // Information stored in the DAC table of interest to the DAC implementation // Note that this information is shared between all instantiations of ClrDataAccess, so initialize // it just once in code:ClrDataAccess.GetDacGlobals (rather than use fields in ClrDataAccess); From 45a71902c51cb34e2bac78d823d2b1ac0c657b23 Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Mon, 20 Nov 2023 11:55:14 -0800 Subject: [PATCH 3/3] Update src/coreclr/debug/daccess/enummem.cpp Co-authored-by: Aaron Robinson --- src/coreclr/debug/daccess/enummem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/debug/daccess/enummem.cpp b/src/coreclr/debug/daccess/enummem.cpp index 7674fe921c7a96..7856a1ff0c096e 100644 --- a/src/coreclr/debug/daccess/enummem.cpp +++ b/src/coreclr/debug/daccess/enummem.cpp @@ -245,7 +245,7 @@ HRESULT ClrDataAccess::EnumMemCLRStatic(IN CLRDataEnumMemoryFlags flags) // Null out the rest of the buffer for (size_t i = len; i < pathLen; ++i) - path[i] = '\0'; + path[i] = W('\0'); DacUpdateMemoryRegion(entryAssemblyPath.GetAddr(), pathLen, (BYTE*)path); }