From 8b317a94354cf58ba4974012f15e615232f76776 Mon Sep 17 00:00:00 2001 From: David Mason Date: Wed, 15 Apr 2020 15:52:14 -0700 Subject: [PATCH] fix alpine failures. you can dlopen a relative path in glib, but apparently with musl you have to have the full path. So look at /proc/self/maps to find the full path to the loaded coreclr --- .../metadatagetdispenser/metadatagetdispenser.cpp | 56 ++++++++++++++++++---- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/src/coreclr/tests/src/profiler/native/metadatagetdispenser/metadatagetdispenser.cpp b/src/coreclr/tests/src/profiler/native/metadatagetdispenser/metadatagetdispenser.cpp index 97bbe36..2f4d0cf 100644 --- a/src/coreclr/tests/src/profiler/native/metadatagetdispenser/metadatagetdispenser.cpp +++ b/src/coreclr/tests/src/profiler/native/metadatagetdispenser/metadatagetdispenser.cpp @@ -8,6 +8,12 @@ #include #else // WIN32 #include +#include +#include +#include +using std::string; +using std::ifstream; +using std::getline; #ifdef __APPLE__ #include #endif // __APPLE__ @@ -160,7 +166,6 @@ HRESULT MetaDataGetDispenser::GetDispenser(IMetaDataDispenserEx **disp) #else // WIN32 -#ifdef __APPLE__ bool EndsWith(const char *lhs, const char *rhs) { size_t lhsLen = strlen(lhs); @@ -186,7 +191,9 @@ bool EndsWith(const char *lhs, const char *rhs) return true; } -const char *GetCoreCLRPath() + +#ifdef __APPLE__ +string GetCoreCLRPath() { const char *coreclrName = "libcoreclr.dylib"; size_t count = _dyld_image_count(); @@ -201,22 +208,51 @@ const char *GetCoreCLRPath() return coreclrName; } + +#else // __APPLE__ + +string GetLibraryName(string line) +{ + // /proc/self/maps contains lines that look like the following: + // 7efdd3d22000-7efdd3f09000 r-xp 00000000 08:02 14160554 /lib/x86_64-linux-gnu/libc-2.27.so + // We can parse them by looking for the last space and return everything after that + // It assumes that none of the paths have spaces + size_t lastSpace = line.find_last_of(' '); + if (lastSpace == string::npos || lastSpace >= (line.size() - 1)) + { + return string(); + } + + return line.substr(lastSpace + 1, string::npos); +} + +string GetCoreCLRPath() +{ + string line; + ifstream sharedLibs("/proc/self/maps"); + while (getline(sharedLibs, line)) + { + string lib = GetLibraryName(line); + if (EndsWith(lib.c_str(), "libcoreclr.so")) + { + return lib; + } + } + + return string(); +} #endif // __APPLE__ HRESULT MetaDataGetDispenser::GetDispenser(IMetaDataDispenserEx **disp) { -#ifdef __APPLE__ - const char *profilerName = GetCoreCLRPath(); -#else // __APPLE__ - const char *profilerName = "libcoreclr.so"; -#endif // __APPLE__ + string profilerName = GetCoreCLRPath(); - void *coreclr = dlopen(profilerName, RTLD_LAZY | RTLD_NOLOAD); + void *coreclr = dlopen(profilerName.c_str(), RTLD_LAZY | RTLD_NOLOAD); if (coreclr == NULL) { _failures++; char *reason = dlerror(); - printf("Failed to find %s reason=%s\n", profilerName, reason); + printf("Failed to find %s reason=%s\n", profilerName.c_str(), reason); return E_FAIL; } @@ -240,4 +276,4 @@ HRESULT MetaDataGetDispenser::GetDispenser(IMetaDataDispenserEx **disp) printf("Got IMetaDataDispenserEx\n"); return S_OK; } -#endif // WIN32 +#endif // WIN32 \ No newline at end of file -- 2.7.4