From 5bd5ec84ac57be6b8c1dd693fec4e0f0ce02928e Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Tue, 11 Apr 2023 12:34:42 -0300 Subject: [PATCH 1/7] support for connect to remote port --- src/dbgshim/dbgshim.cpp | 58 +++++++++++++++++++++++++++++++++++++++++ src/dbgshim/dbgshim.h | 7 +++++ 2 files changed, 65 insertions(+) diff --git a/src/dbgshim/dbgshim.cpp b/src/dbgshim/dbgshim.cpp index 5b9ac973f3..1f333aa1ea 100644 --- a/src/dbgshim/dbgshim.cpp +++ b/src/dbgshim/dbgshim.cpp @@ -157,6 +157,11 @@ typedef HRESULT (STDAPICALLTYPE *FPCoreCLRCreateCordbObject3)( HMODULE hmodTargetCLR, IUnknown **ppCordb); +typedef HRESULT (STDAPICALLTYPE *FPCoreCLRCreateCordbObjectRemotePort)( + DWORD port, + LPCSTR assemblyBasePath, + IUnknown **ppCordb); + HRESULT CreateCoreDbg( HMODULE hCLRModule, DWORD processId, @@ -2151,3 +2156,56 @@ CLRCreateInstance( return pDebuggingImpl->QueryInterface(riid, ppInterface); } + +HRESULT CreateCoreDbgRemotePort(HMODULE hDBIModule, DWORD portId, LPCSTR assemblyBasePath, IUnknown **ppCordb) +{ + HRESULT hr = S_OK; + +#if defined(TARGET_WINDOWS) + FPCoreCLRCreateCordbObjectRemotePort fpCreate = + (FPCoreCLRCreateCordbObjectRemotePort)GetProcAddress(hDBIModule, "CoreCLRCreateCordbObject"); +#else + FPCoreCLRCreateCordbObjectRemotePort fpCreate = (FPCoreCLRCreateCordbObjectRemotePort)dlsym (hDBIModule, "CoreCLRCreateCordbObject"); +#endif + + if (fpCreate == NULL) + { + return CORDBG_E_INCOMPATIBLE_PROTOCOL; + } + + return fpCreate(portId, assemblyBasePath, ppCordb); + + return hr; +} + +DLLEXPORT +HRESULT +RegisterForRuntimeStartupRemotePort( + _In_ DWORD dwRemotePortId, + _In_ LPCSTR mscordbiPath, + _In_ LPCSTR assemblyBasePath, + _Out_ IUnknown ** ppCordb) +{ + while (!::IsDebuggerPresent()) + ::Sleep(100); + if (pCordb != NULL) + return S_OK; + + HRESULT hr = S_OK; + HMODULE hMod = NULL; + +#ifdef TARGET_WINDOWS + hMod = LoadLibraryA(mscordbiPath); +#else + hMod = dlopen(mscordbiPath, RTLD_LAZY); +#endif + if (hMod == NULL) + { + hr = CORDBG_E_DEBUG_COMPONENT_MISSING; + return hr; + } + + hr = CreateCoreDbgRemotePort(hMod, dwRemotePortId, assemblyBasePath, &pCordb); + *ppCordb = pCordb; + return S_OK; +} diff --git a/src/dbgshim/dbgshim.h b/src/dbgshim/dbgshim.h index 34c71e5152..5b7d56c6dc 100644 --- a/src/dbgshim/dbgshim.h +++ b/src/dbgshim/dbgshim.h @@ -105,3 +105,10 @@ CreateDebuggingInterfaceFromVersion3( _In_ LPCWSTR szApplicationGroupId, _In_ ICLRDebuggingLibraryProvider3* pLibraryProvider, _Out_ IUnknown ** ppCordb); + +EXTERN_C HRESULT +RegisterForRuntimeStartupRemotePort( + _In_ DWORD dwRemotePortId, + _In_ LPCSTR mscordbiPath, + _In_ LPCSTR assemblyBasePath, + _Out_ IUnknown ** ppCordb); \ No newline at end of file From 918b75eb05ba1e5a48a4aa40e9a5e52906486f6c Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Tue, 11 Apr 2023 12:53:44 -0300 Subject: [PATCH 2/7] Fix compilation Add \n in the last line of dbgshim.h Remove sleep to attach debugger. Add exports. --- src/dbgshim/dbgshim.cpp | 8 +------- src/dbgshim/dbgshim.h | 2 +- src/dbgshim/dbgshim.ntdef | 1 + src/dbgshim/dbgshim_unixexports.src | 1 + 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/dbgshim/dbgshim.cpp b/src/dbgshim/dbgshim.cpp index 1f333aa1ea..aeb40b011b 100644 --- a/src/dbgshim/dbgshim.cpp +++ b/src/dbgshim/dbgshim.cpp @@ -2186,11 +2186,6 @@ RegisterForRuntimeStartupRemotePort( _In_ LPCSTR assemblyBasePath, _Out_ IUnknown ** ppCordb) { - while (!::IsDebuggerPresent()) - ::Sleep(100); - if (pCordb != NULL) - return S_OK; - HRESULT hr = S_OK; HMODULE hMod = NULL; @@ -2205,7 +2200,6 @@ RegisterForRuntimeStartupRemotePort( return hr; } - hr = CreateCoreDbgRemotePort(hMod, dwRemotePortId, assemblyBasePath, &pCordb); - *ppCordb = pCordb; + hr = CreateCoreDbgRemotePort(hMod, dwRemotePortId, assemblyBasePath, ppCordb); return S_OK; } diff --git a/src/dbgshim/dbgshim.h b/src/dbgshim/dbgshim.h index 5b7d56c6dc..b186a5da8c 100644 --- a/src/dbgshim/dbgshim.h +++ b/src/dbgshim/dbgshim.h @@ -111,4 +111,4 @@ RegisterForRuntimeStartupRemotePort( _In_ DWORD dwRemotePortId, _In_ LPCSTR mscordbiPath, _In_ LPCSTR assemblyBasePath, - _Out_ IUnknown ** ppCordb); \ No newline at end of file + _Out_ IUnknown ** ppCordb); diff --git a/src/dbgshim/dbgshim.ntdef b/src/dbgshim/dbgshim.ntdef index 8b6572e1f0..c06fc5e399 100644 --- a/src/dbgshim/dbgshim.ntdef +++ b/src/dbgshim/dbgshim.ntdef @@ -18,3 +18,4 @@ EXPORTS CreateDebuggingInterfaceFromVersion2 CreateDebuggingInterfaceFromVersion3 CLRCreateInstance + RegisterForRuntimeStartupRemotePort diff --git a/src/dbgshim/dbgshim_unixexports.src b/src/dbgshim/dbgshim_unixexports.src index fae2869f38..709b33705f 100644 --- a/src/dbgshim/dbgshim_unixexports.src +++ b/src/dbgshim/dbgshim_unixexports.src @@ -17,3 +17,4 @@ CreateDebuggingInterfaceFromVersionEx CreateDebuggingInterfaceFromVersion2 CreateDebuggingInterfaceFromVersion3 CLRCreateInstance +RegisterForRuntimeStartupRemotePort From 943000fe15810ea133440bba5779576aa848ba15 Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Tue, 11 Apr 2023 13:16:42 -0300 Subject: [PATCH 3/7] Adding include --- src/dbgshim/dbgshim.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/dbgshim/dbgshim.cpp b/src/dbgshim/dbgshim.cpp index aeb40b011b..5eb815fe78 100644 --- a/src/dbgshim/dbgshim.cpp +++ b/src/dbgshim/dbgshim.cpp @@ -30,6 +30,8 @@ #include #endif +#include + #include "dbgshim.h" #include "debugshim.h" From 22e7ee856f67daa89c5c6dca27bf464795f08fe1 Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Tue, 11 Apr 2023 13:47:29 -0300 Subject: [PATCH 4/7] Fix compilation --- src/dbgshim/dbgshim.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/dbgshim/dbgshim.cpp b/src/dbgshim/dbgshim.cpp index 5eb815fe78..3632613920 100644 --- a/src/dbgshim/dbgshim.cpp +++ b/src/dbgshim/dbgshim.cpp @@ -30,8 +30,6 @@ #include #endif -#include - #include "dbgshim.h" #include "debugshim.h" @@ -2194,7 +2192,7 @@ RegisterForRuntimeStartupRemotePort( #ifdef TARGET_WINDOWS hMod = LoadLibraryA(mscordbiPath); #else - hMod = dlopen(mscordbiPath, RTLD_LAZY); + hMod = dlopen(mscordbiPath, 0x00001/*RTLD_LAZY*/); #endif if (hMod == NULL) { From 37620d4aa03703253c16ad619159573067876c11 Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Tue, 11 Apr 2023 14:24:42 -0300 Subject: [PATCH 5/7] fix compilation --- src/dbgshim/dbgshim.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/dbgshim/dbgshim.cpp b/src/dbgshim/dbgshim.cpp index 3632613920..1955125492 100644 --- a/src/dbgshim/dbgshim.cpp +++ b/src/dbgshim/dbgshim.cpp @@ -28,7 +28,14 @@ #ifdef TARGET_WINDOWS #define PSAPI_VERSION 2 #include -#endif +#else +#ifdef __APPLE__ +#include +#include +#else +#include +#endif // __APPLE__ +#endif // TARGET_WINDOWS #include "dbgshim.h" #include "debugshim.h" From 57c2d821bc4805f4ad7892550cbc01b068a57d80 Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Tue, 11 Apr 2023 14:38:46 -0300 Subject: [PATCH 6/7] fix compilation --- src/dbgshim/dbgshim.cpp | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/src/dbgshim/dbgshim.cpp b/src/dbgshim/dbgshim.cpp index 1955125492..1299ebb9dd 100644 --- a/src/dbgshim/dbgshim.cpp +++ b/src/dbgshim/dbgshim.cpp @@ -28,14 +28,7 @@ #ifdef TARGET_WINDOWS #define PSAPI_VERSION 2 #include -#else -#ifdef __APPLE__ -#include -#include -#else -#include -#endif // __APPLE__ -#endif // TARGET_WINDOWS +#endif #include "dbgshim.h" #include "debugshim.h" @@ -2168,13 +2161,8 @@ HRESULT CreateCoreDbgRemotePort(HMODULE hDBIModule, DWORD portId, LPCSTR assembl { HRESULT hr = S_OK; -#if defined(TARGET_WINDOWS) FPCoreCLRCreateCordbObjectRemotePort fpCreate = (FPCoreCLRCreateCordbObjectRemotePort)GetProcAddress(hDBIModule, "CoreCLRCreateCordbObject"); -#else - FPCoreCLRCreateCordbObjectRemotePort fpCreate = (FPCoreCLRCreateCordbObjectRemotePort)dlsym (hDBIModule, "CoreCLRCreateCordbObject"); -#endif - if (fpCreate == NULL) { return CORDBG_E_INCOMPATIBLE_PROTOCOL; @@ -2196,11 +2184,7 @@ RegisterForRuntimeStartupRemotePort( HRESULT hr = S_OK; HMODULE hMod = NULL; -#ifdef TARGET_WINDOWS hMod = LoadLibraryA(mscordbiPath); -#else - hMod = dlopen(mscordbiPath, 0x00001/*RTLD_LAZY*/); -#endif if (hMod == NULL) { hr = CORDBG_E_DEBUG_COMPONENT_MISSING; From 28fa1b1b7f466bbe7dd5cf2a8567b14068ff007a Mon Sep 17 00:00:00 2001 From: Thays Grazia Date: Wed, 12 Apr 2023 11:49:56 -0300 Subject: [PATCH 7/7] addressing @mikem8361 comment --- src/dbgshim/dbgshim.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/dbgshim/dbgshim.cpp b/src/dbgshim/dbgshim.cpp index 1299ebb9dd..a68d1ad650 100644 --- a/src/dbgshim/dbgshim.cpp +++ b/src/dbgshim/dbgshim.cpp @@ -157,7 +157,7 @@ typedef HRESULT (STDAPICALLTYPE *FPCoreCLRCreateCordbObject3)( HMODULE hmodTargetCLR, IUnknown **ppCordb); -typedef HRESULT (STDAPICALLTYPE *FPCoreCLRCreateCordbObjectRemotePort)( +typedef HRESULT (STDAPICALLTYPE *FPCreateRemoteCordbObject)( DWORD port, LPCSTR assemblyBasePath, IUnknown **ppCordb); @@ -2161,8 +2161,8 @@ HRESULT CreateCoreDbgRemotePort(HMODULE hDBIModule, DWORD portId, LPCSTR assembl { HRESULT hr = S_OK; - FPCoreCLRCreateCordbObjectRemotePort fpCreate = - (FPCoreCLRCreateCordbObjectRemotePort)GetProcAddress(hDBIModule, "CoreCLRCreateCordbObject"); + FPCreateRemoteCordbObject fpCreate = + (FPCreateRemoteCordbObject)GetProcAddress(hDBIModule, "CreateRemoteCordbObject"); if (fpCreate == NULL) { return CORDBG_E_INCOMPATIBLE_PROTOCOL;