From: Igor Kulaychuk Date: Mon, 20 Aug 2018 19:03:29 +0000 (+0300) Subject: Load dbgshim library dynamicaly X-Git-Tag: submit/tizen/20180921.084208~2^2~14 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f7f1a3fce5072a9e6ffdcb46b268058f1e97fe78;p=sdk%2Ftools%2Fnetcoredbg.git Load dbgshim library dynamicaly --- diff --git a/src/debug/netcoredbg/manageddebugger.cpp b/src/debug/netcoredbg/manageddebugger.cpp index 0865ee7..52658b5 100644 --- a/src/debug/netcoredbg/manageddebugger.cpp +++ b/src/debug/netcoredbg/manageddebugger.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include "symbolreader.h" #include "cputil.h" @@ -15,14 +16,72 @@ #include "typeprinter.h" #include "frames.h" -#ifndef __in -#define __in +// From dbgshim.h +struct dbgshim_t +{ + typedef VOID (*PSTARTUP_CALLBACK)(IUnknown *pCordb, PVOID parameter, HRESULT hr); + HRESULT (*CreateProcessForLaunch)(LPWSTR lpCommandLine, BOOL bSuspendProcess, LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory, PDWORD pProcessId, HANDLE *pResumeHandle); + HRESULT (*ResumeProcess)(HANDLE hResumeHandle); + HRESULT (*CloseResumeHandle)(HANDLE hResumeHandle); + HRESULT (*RegisterForRuntimeStartup)(DWORD dwProcessId, PSTARTUP_CALLBACK pfnCallback, PVOID parameter, PVOID *ppUnregisterToken); + HRESULT (*UnregisterForRuntimeStartup)(PVOID pUnregisterToken); + HRESULT (*EnumerateCLRs)(DWORD debuggeePID, HANDLE** ppHandleArrayOut, LPWSTR** ppStringArrayOut, DWORD* pdwArrayLengthOut); + HRESULT (*CloseCLREnumeration)(HANDLE* pHandleArray, LPWSTR* pStringArray, DWORD dwArrayLength); + HRESULT (*CreateVersionStringFromModule)(DWORD pidDebuggee, LPCWSTR szModuleName, LPWSTR pBuffer, DWORD cchBuffer, DWORD* pdwLength); + HRESULT (*CreateDebuggingInterfaceFromVersionEx)(int iDebuggerVersion, LPCWSTR szDebuggeeVersion, IUnknown ** ppCordb); + + dbgshim_t() : + CreateProcessForLaunch(nullptr), + ResumeProcess(nullptr), + CloseResumeHandle(nullptr), + RegisterForRuntimeStartup(nullptr), + UnregisterForRuntimeStartup(nullptr), + EnumerateCLRs(nullptr), + CloseCLREnumeration(nullptr), + CreateVersionStringFromModule(nullptr), + CreateDebuggingInterfaceFromVersionEx(nullptr), + m_module(nullptr) + { + std::string exe = GetExeAbsPath(); + std::size_t dirSepIndex = exe.rfind(DIRECTORY_SEPARATOR_STR_A); + if (dirSepIndex == std::string::npos) + return; + std::string libName = exe.substr(0, dirSepIndex + 1); + +#ifdef FEATURE_PAL +#if defined(__APPLE__) + libName += "libdbgshim.dylib"; +#else + libName += "libdbgshim.so"; #endif -#ifndef __out -#define __out +#else + libName += "dbgshim.dll"; #endif -#include "dbgshim.h" + m_module = DLOpen(libName); + if (!m_module) + throw std::invalid_argument("Unable to load " + libName); + + *((void**)&CreateProcessForLaunch) = DLSym(m_module, "CreateProcessForLaunch"); + *((void**)&ResumeProcess) = DLSym(m_module, "ResumeProcess"); + *((void**)&CloseResumeHandle) = DLSym(m_module, "CloseResumeHandle"); + *((void**)&RegisterForRuntimeStartup) = DLSym(m_module, "RegisterForRuntimeStartup"); + *((void**)&UnregisterForRuntimeStartup) = DLSym(m_module, "UnregisterForRuntimeStartup"); + *((void**)&EnumerateCLRs) = DLSym(m_module, "EnumerateCLRs"); + *((void**)&CloseCLREnumeration) = DLSym(m_module, "CloseCLREnumeration"); + *((void**)&CreateVersionStringFromModule) = DLSym(m_module, "CreateVersionStringFromModule"); + *((void**)&CreateDebuggingInterfaceFromVersionEx) = DLSym(m_module, "CreateDebuggingInterfaceFromVersionEx"); + } + ~dbgshim_t() + { + // if (m_module) + // DLClose(m_module); + } +private: + void *m_module; +}; + +static dbgshim_t g_dbgshim; void ManagedDebugger::NotifyProcessCreated() { @@ -873,7 +932,7 @@ VOID ManagedDebugger::StartupCallback(IUnknown *pCordb, PVOID parameter, HRESULT if (self->m_unregisterToken) { - UnregisterForRuntimeStartup(self->m_unregisterToken); + g_dbgshim.UnregisterForRuntimeStartup(self->m_unregisterToken); self->m_unregisterToken = nullptr; } @@ -903,7 +962,7 @@ static HRESULT InternalEnumerateCLRs( while (numTries < tryCount) { - hr = EnumerateCLRs(pid, ppHandleArray, ppStringArray, pdwArrayLength); + hr = g_dbgshim.EnumerateCLRs(pid, ppHandleArray, ppStringArray, pdwArrayLength); // From dbgshim.cpp: // EnumerateCLRs uses the OS API CreateToolhelp32Snapshot which can return ERROR_BAD_LENGTH or @@ -926,7 +985,7 @@ static HRESULT InternalEnumerateCLRs( return hr; } // Clean up memory allocated in EnumerateCLRs since this path it succeeded - CloseCLREnumeration(*ppHandleArray, *ppStringArray, *pdwArrayLength); + g_dbgshim.CloseCLREnumeration(*ppHandleArray, *ppStringArray, *pdwArrayLength); *ppHandleArray = NULL; *ppStringArray = NULL; @@ -965,7 +1024,7 @@ static std::string GetCLRPath(DWORD pid, int timeoutSec = 3) std::string result = to_utf8(pStringArray[0]); - CloseCLREnumeration(pHandleArray, pStringArray, dwArrayLength); + g_dbgshim.CloseCLREnumeration(pHandleArray, pStringArray, dwArrayLength); return result; } @@ -1042,17 +1101,17 @@ HRESULT ManagedDebugger::RunProcess(std::string fileExec, std::vector(to_utf16(ss.str()).c_str()), + IfFailRet(g_dbgshim.CreateProcessForLaunch(const_cast(to_utf16(ss.str()).c_str()), /* Suspend process */ TRUE, /* Current environment */ NULL, /* Current working directory */ NULL, &m_processId, &resumeHandle)); - IfFailRet(RegisterForRuntimeStartup(m_processId, ManagedDebugger::StartupCallback, this, &m_unregisterToken)); + IfFailRet(g_dbgshim.RegisterForRuntimeStartup(m_processId, ManagedDebugger::StartupCallback, this, &m_unregisterToken)); // Resume the process so that StartupCallback can run - IfFailRet(ResumeProcess(resumeHandle)); - CloseResumeHandle(resumeHandle); + IfFailRet(g_dbgshim.ResumeProcess(resumeHandle)); + g_dbgshim.CloseResumeHandle(resumeHandle); // Wait for ManagedDebugger::StartupCallback to complete @@ -1065,7 +1124,7 @@ HRESULT ManagedDebugger::RunProcess(std::string fileExec, std::vector pCordb; - IfFailRet(CreateDebuggingInterfaceFromVersionEx(CorDebugVersion_4_0, pBuffer, &pCordb)); + IfFailRet(g_dbgshim.CreateDebuggingInterfaceFromVersionEx(CorDebugVersion_4_0, pBuffer, &pCordb)); m_unregisterToken = nullptr; return Startup(pCordb, pid);