Skip to content

Commit

Permalink
Fully implement dbgshim so it doesn't depend on LD_LIBRARY_PATH (issu…
Browse files Browse the repository at this point in the history
…e #650).

It finds and uses the path coreclr is in to load dbi/dac.
  • Loading branch information
mikem8361 committed Jun 12, 2015
1 parent 0d86302 commit d156b75
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 514 deletions.
1 change: 0 additions & 1 deletion src/debug/debug-pal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ if(CLR_CMAKE_PLATFORM_UNIX)

set(TWO_WAY_PIPE_SOURCES
unix/twowaypipe.cpp
unix/dynamiclibaddress.cpp
)

endif(CLR_CMAKE_PLATFORM_UNIX)
Expand Down
91 changes: 0 additions & 91 deletions src/debug/debug-pal/unix/dynamiclibaddress.cpp

This file was deleted.

76 changes: 27 additions & 49 deletions src/dlls/dbgshim/dbgshim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ void GetTargetCLRMetrics(LPCWSTR szTelestoFullPath,
// Holder will call FreeLibrary()
}

#endif // FEATURE_PAL

// Returns true iff the module represents CoreClr.
bool IsCoreClr(const WCHAR* pModulePath)
{
Expand Down Expand Up @@ -390,6 +392,7 @@ bool IsCoreClrWithGoodHeader(HANDLE hProcess, HMODULE hModule)

if (IsCoreClr(modulePath))
{
#ifndef FEATURE_PAL
// We don't care about the particular error returned, only that
// what we tried wasn't a 'real' coreclr.dll.
EX_TRY
Expand All @@ -401,13 +404,14 @@ bool IsCoreClrWithGoodHeader(HANDLE hProcess, HMODULE hModule)
}
EX_CATCH_HRESULT(hr);
return (hr == S_OK);
#else
return true;
#endif // FEATURE_PAL
}

return false;
}

#endif // !FEATURE_PAL

//-----------------------------------------------------------------------------
// Public API.
//
Expand Down Expand Up @@ -439,7 +443,6 @@ HRESULT EnumerateCLRs(DWORD debuggeePID,
if ((ppHandleArrayOut == NULL) || (ppStringArrayOut == NULL) || (pdwArrayLengthOut == NULL))
return E_INVALIDARG;

#ifndef FEATURE_PAL
HandleHolder hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, debuggeePID);
if (NULL == hProcess)
ThrowHR(E_FAIL);
Expand Down Expand Up @@ -474,9 +477,6 @@ HRESULT EnumerateCLRs(DWORD debuggeePID,

return S_OK;
}
#else
DWORD count = 1;
#endif // FEATURE_PAL

size_t cbEventArrayData = sizeof(HANDLE) * count; // event array data
size_t cbStringArrayData = sizeof(LPWSTR) * count; // string array data
Expand All @@ -494,7 +494,6 @@ HRESULT EnumerateCLRs(DWORD debuggeePID,
WCHAR* pStringData = (WCHAR*) &pOutBuffer[cbEventArrayData + cbStringArrayData];
DWORD idx = 0;

#ifndef FEATURE_PAL
// There's no guarantee that another coreclr hasn't loaded already anyhow,
// so if we get the corner case that the second time through we enumerate
// more coreclrs, just ignore the extras.
Expand All @@ -514,6 +513,7 @@ HRESULT EnumerateCLRs(DWORD debuggeePID,
pStringArray[idx] = &pStringData[idx * MAX_PATH];
GetModuleFileNameEx(hProcess, modules[i], pStringArray[idx], MAX_PATH);

#ifndef FEATURE_PAL
// fill in event handle -- if GetContinueStartupEvent fails, it will still return
// INVALID_HANDLE_VALUE in hContinueStartupEvent, which is what we want. we don't
// want to bail out of the enumeration altogether if we can't get an event from
Expand All @@ -524,6 +524,9 @@ HRESULT EnumerateCLRs(DWORD debuggeePID,
_ASSERTE(SUCCEEDED(hr) == (hContinueStartupEvent != INVALID_HANDLE_VALUE));

pEventArray[idx] = hContinueStartupEvent;
#else
pEventArray[idx] = NULL;
#endif // FEATURE_PAL

idx++;
}
Expand All @@ -548,11 +551,6 @@ HRESULT EnumerateCLRs(DWORD debuggeePID,

// Strings themselves don't need moved.
}
#else
pStringArray[idx] = &pStringData[idx * MAX_PATH];
wcscpy_s(pStringArray[idx], MAX_PATH, MAKEDLLNAME_W(W("coreclr")));
idx++;
#endif // FEATURE_PAL

*ppHandleArrayOut = pEventArray;
*ppStringArrayOut = pStringArray;
Expand All @@ -561,8 +559,6 @@ HRESULT EnumerateCLRs(DWORD debuggeePID,
return S_OK;
}

#ifndef FEATURE_PAL

//-----------------------------------------------------------------------------
// Get the base address of a module from the remote process.
//
Expand All @@ -581,7 +577,9 @@ BYTE* GetRemoteModuleBaseAddress(DWORD dwPID, LPCWSTR szFullModulePath)

HandleHolder hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPID);
if (NULL == hProcess)
{
ThrowHR(E_FAIL);
}

// These shouldn't be freed
HMODULE modules[1000];
Expand Down Expand Up @@ -614,8 +612,6 @@ BYTE* GetRemoteModuleBaseAddress(DWORD dwPID, LPCWSTR szFullModulePath)
return NULL;
}

#endif // FEATURE_PAL

// DBI version: max 8 hex chars
// SEMICOLON: 1
// PID: max 8 hex chars
Expand Down Expand Up @@ -697,15 +693,13 @@ HRESULT CreateVersionStringFromModule(DWORD pidDebuggee,

GetTargetCLRMetrics(szModuleName, &metricsStruct); // throws
dbiVersion = (CorDebugInterfaceVersion) metricsStruct.dwDbiVersion;

hmodTargetCLR = GetRemoteModuleBaseAddress(pidDebuggee, szModuleName); // throws
#else
//TODO: So far on POSIX systems we only support one version of debugging interface
// in future we might want to detect it the same way we do it on Windows.
dbiVersion = CorDebugLatestVersion;
hmodTargetCLR = (BYTE *)GetDynamicLibraryAddressInProcess(pidDebuggee, MAKEDLLNAME_A(MAIN_CLR_MODULE_NAME_A));
#endif // FEATURE_PAL


hmodTargetCLR = GetRemoteModuleBaseAddress(pidDebuggee, szModuleName); // throws
if (hmodTargetCLR == NULL)
{
hr = COR_E_FILENOTFOUND;
Expand Down Expand Up @@ -768,7 +762,6 @@ HRESULT ParseVersionString(LPCWSTR szDebuggeeVersion, CorDebugInterfaceVersion *
return S_OK;
}

#ifndef FEATURE_PAL
//-----------------------------------------------------------------------------
// Appends "\mscordbi.dll" to the path. This converts a directory name into the full path to mscordbi.dll.
//
Expand All @@ -781,6 +774,7 @@ void AppendDbiDllName(SString & szFullDbiPath)
szFullDbiPath.Append(pDbiDllName);
}


//-----------------------------------------------------------------------------
// Return a path to the dbi next to the runtime, if present.
//
Expand Down Expand Up @@ -833,6 +827,7 @@ void GetDbiFilenameNextToRuntime(DWORD pidDebuggee, HMODULE hmodTargetCLR, SStri
szFullCoreClrPath.Set(pCoreClrPath, (COUNT_T)wcslen(pCoreClrPath));
}


//---------------------------------------------------------------------------------------
//
// The current policy is that the DBI DLL must live right next to the coreclr DLL. We check the product
Expand All @@ -848,6 +843,7 @@ void GetDbiFilenameNextToRuntime(DWORD pidDebuggee, HMODULE hmodTargetCLR, SStri

bool CheckDbiAndRuntimeVersion(SString & szFullDbiPath, SString & szFullCoreClrPath)
{
#ifndef FEATURE_PAL
DWORD dwDbiVersionMS = 0;
DWORD dwDbiVersionLS = 0;
DWORD dwCoreClrVersionMS = 0;
Expand All @@ -866,16 +862,11 @@ bool CheckDbiAndRuntimeVersion(SString & szFullDbiPath, SString & szFullCoreClrP
{
return false;
}
}

#else

// Functions that we'll look for in the loaded Mscordbi module.
typedef HRESULT (STDAPICALLTYPE *FPCreateCordbObject)(
int iDebuggerVersion,
IUnknown ** ppCordb);

return true;
#endif // FEATURE_PAL
}


//-----------------------------------------------------------------------------
// Public API.
Expand Down Expand Up @@ -905,6 +896,8 @@ HRESULT CreateDebuggingInterfaceFromVersionEx(
HRESULT hr = S_OK;
HMODULE hMod = NULL;
IUnknown * pCordb = NULL;
FPCoreCLRCreateCordbObject fpCreate2 = NULL;

LOG((LF_CORDB, LL_EVERYTHING, "Calling CreateDebuggerInterfaceFromVersion, ver=%S\n", szDebuggeeVersion));

if ((szDebuggeeVersion == NULL) || (ppCordb == NULL))
Expand All @@ -915,7 +908,6 @@ HRESULT CreateDebuggingInterfaceFromVersionEx(

*ppCordb = NULL;


//
// Step 1: Parse version information into internal data structures
//
Expand All @@ -928,7 +920,6 @@ HRESULT CreateDebuggingInterfaceFromVersionEx(
if (FAILED(hr))
goto Exit;

#ifndef FEATURE_PAL
//
// Step 2: Find the proper Dbi module (mscordbi.dll) and load it.
//
Expand All @@ -954,7 +945,11 @@ HRESULT CreateDebuggingInterfaceFromVersionEx(
// Issue:951525: coreclr mscordbi load fails on downlevel OS since LoadLibraryEx can't find
// dependent forwarder DLLs. Force LoadLibrary to look for dependencies in szFullDbiPath plus the default
// search paths.
#ifndef FEATURE_PAL
hMod = WszLoadLibraryEx(szFullDbiPath, NULL, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
#else
hMod = LoadLibraryExW(szFullDbiPath, NULL, 0);
#endif
}
EX_CATCH_HRESULT(hrIgnore); // failure leaves hMod null

Expand All @@ -977,7 +972,7 @@ HRESULT CreateDebuggingInterfaceFromVersionEx(
//
// Step 3: Now that module is loaded, instantiate an ICorDebug.
//
FPCoreCLRCreateCordbObject fpCreate2 = (FPCoreCLRCreateCordbObject)GetProcAddress(hMod, "CoreCLRCreateCordbObject");
fpCreate2 = (FPCoreCLRCreateCordbObject)GetProcAddress(hMod, "CoreCLRCreateCordbObject");
if (fpCreate2 == NULL)
{
// New-style creation API didn't exist - this DBI must be the wrong version, for the Mix07 protocol
Expand All @@ -987,23 +982,6 @@ HRESULT CreateDebuggingInterfaceFromVersionEx(

// Invoke to instantiate an ICorDebug. This export was introduced after the Mix'07 release.
hr = fpCreate2(iDebuggerVersion, pidDebuggee, hmodTargetCLR, &pCordb);
#else
{
hMod = LoadLibraryExW(MAKEDLLNAME_W(W("mscordbi")), NULL, 0);
if (NULL == hMod)
{
hr = CORDBG_E_DEBUG_COMPONENT_MISSING;
goto Exit;
}
FPCoreCLRCreateCordbObject fpCreate2 = (FPCoreCLRCreateCordbObject)GetProcAddress(hMod, "CoreCLRCreateCordbObject");
if (hmodTargetCLR == NULL || fpCreate2 == NULL)
{
hr = CORDBG_E_INCOMPATIBLE_PROTOCOL;
goto Exit;
}
hr = fpCreate2(iDebuggerVersion, pidDebuggee, hmodTargetCLR, &pCordb);
}
#endif // FEATURE_PAL
_ASSERTE((pCordb == NULL) == FAILED(hr));

Exit:
Expand Down
Loading

0 comments on commit d156b75

Please sign in to comment.